]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
better string matching for duplicate title numbering. this will check against the...
[chaz/openbox] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "menuframe.h"
21 #include "client.h"
22 #include "menu.h"
23 #include "screen.h"
24 #include "grab.h"
25 #include "openbox.h"
26 #include "mainloop.h"
27 #include "config.h"
28 #include "render/theme.h"
29
30 #define PADDING 2
31 #define SEPARATOR_HEIGHT 3
32 #define MAX_MENU_WIDTH 400
33
34 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
35 LeaveWindowMask)
36 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
37 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
38 ButtonPressMask | ButtonReleaseMask)
39
40 GList *menu_frame_visible;
41
42 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
43 ObMenuFrame *frame);
44 static void menu_entry_frame_free(ObMenuEntryFrame *self);
45 static void menu_frame_render(ObMenuFrame *self);
46 static void menu_frame_update(ObMenuFrame *self);
47 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
48
49 static Window createWindow(Window parent, gulong mask,
50 XSetWindowAttributes *attrib)
51 {
52 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
53 RrDepth(ob_rr_inst), InputOutput,
54 RrVisual(ob_rr_inst), mask, attrib);
55 }
56
57 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
58 {
59 ObMenuFrame *self;
60 XSetWindowAttributes attr;
61
62 self = g_new0(ObMenuFrame, 1);
63 self->type = Window_Menu;
64 self->menu = menu;
65 self->selected = NULL;
66 self->show_title = TRUE;
67 self->client = client;
68
69 attr.event_mask = FRAME_EVENTMASK;
70 self->window = createWindow(RootWindow(ob_display, ob_screen),
71 CWEventMask, &attr);
72 attr.event_mask = TITLE_EVENTMASK;
73 self->title = createWindow(self->window, CWEventMask, &attr);
74 self->items = createWindow(self->window, 0, NULL);
75
76 XMapWindow(ob_display, self->items);
77
78 self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
79 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
80
81 stacking_add(MENU_AS_WINDOW(self));
82
83 return self;
84 }
85
86 void menu_frame_free(ObMenuFrame *self)
87 {
88 if (self) {
89 while (self->entries) {
90 menu_entry_frame_free(self->entries->data);
91 self->entries = g_list_delete_link(self->entries, self->entries);
92 }
93
94 stacking_remove(MENU_AS_WINDOW(self));
95
96 XDestroyWindow(ob_display, self->items);
97 XDestroyWindow(ob_display, self->title);
98 XDestroyWindow(ob_display, self->window);
99
100 RrAppearanceFree(self->a_items);
101 RrAppearanceFree(self->a_title);
102
103 g_free(self);
104 }
105 }
106
107 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
108 ObMenuFrame *frame)
109 {
110 ObMenuEntryFrame *self;
111 XSetWindowAttributes attr;
112
113 self = g_new0(ObMenuEntryFrame, 1);
114 self->entry = entry;
115 self->frame = frame;
116
117 attr.event_mask = ENTRY_EVENTMASK;
118 self->window = createWindow(self->frame->items, CWEventMask, &attr);
119 self->text = createWindow(self->window, 0, NULL);
120 if (entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
121 self->icon = createWindow(self->window, 0, NULL);
122 self->bullet = createWindow(self->window, 0, NULL);
123 }
124
125 XMapWindow(ob_display, self->window);
126 XMapWindow(ob_display, self->text);
127
128 self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
129 self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
130 self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
131
132 if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
133 self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
134 self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
135 } else {
136 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
137 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
138 self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
139 self->a_mask->texture[0].type = RR_TEXTURE_MASK;
140 self->a_bullet_normal =
141 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
142 self->a_bullet_selected =
143 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
144 }
145
146 self->a_text_normal =
147 RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
148 self->a_text_disabled =
149 RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
150 self->a_text_selected =
151 RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
152
153 return self;
154 }
155
156 static void menu_entry_frame_free(ObMenuEntryFrame *self)
157 {
158 if (self) {
159 XDestroyWindow(ob_display, self->text);
160 XDestroyWindow(ob_display, self->window);
161 if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
162 XDestroyWindow(ob_display, self->icon);
163 XDestroyWindow(ob_display, self->bullet);
164 }
165
166 RrAppearanceFree(self->a_normal);
167 RrAppearanceFree(self->a_disabled);
168 RrAppearanceFree(self->a_selected);
169
170 RrAppearanceFree(self->a_separator);
171 RrAppearanceFree(self->a_icon);
172 RrAppearanceFree(self->a_mask);
173 RrAppearanceFree(self->a_text_normal);
174 RrAppearanceFree(self->a_text_disabled);
175 RrAppearanceFree(self->a_text_selected);
176 RrAppearanceFree(self->a_bullet_normal);
177 RrAppearanceFree(self->a_bullet_selected);
178
179 g_free(self);
180 }
181 }
182
183 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
184 {
185 RECT_SET_POINT(self->area, x, y);
186 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
187 }
188
189 void menu_frame_move_on_screen(ObMenuFrame *self)
190 {
191 Rect *a = NULL;
192 gint dx = 0, dy = 0;
193 gint pos, half;
194
195 a = screen_physical_area_monitor(self->monitor);
196
197 half = g_list_length(self->entries) / 2;
198 pos = g_list_index(self->entries, self->selected);
199
200 /* if in the bottom half then check this shit first, will keep the bottom
201 edge of the menu visible */
202 if (pos > half) {
203 dx = MAX(dx, a->x - self->area.x);
204 dy = MAX(dy, a->y - self->area.y);
205 }
206 dx = MIN(dx, (a->x + a->width) - (self->area.x + self->area.width));
207 dy = MIN(dy, (a->y + a->height) - (self->area.y + self->area.height));
208 /* if in the top half then check this shit last, will keep the top
209 edge of the menu visible */
210 if (pos <= half) {
211 dx = MAX(dx, a->x - self->area.x);
212 dy = MAX(dy, a->y - self->area.y);
213 }
214
215 if (dx || dy) {
216 ObMenuFrame *f;
217
218 /* move the current menu frame to fit, but dont touch parents yet */
219 menu_frame_move(self, self->area.x + dx, self->area.y + dy);
220 if (!config_menu_xorstyle)
221 dy = 0; /* if we want to be like xor, move parents in y- *
222 * and x-direction, otherwise just in x-dir */
223 for (f = self->parent; f; f = f->parent)
224 menu_frame_move(f, f->area.x + dx, f->area.y + dy);
225 for (f = self->child; f; f = f->child)
226 menu_frame_move(f, f->area.x + dx, f->area.y + dy);
227 if (config_menu_warppointer)
228 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
229 }
230 }
231
232 static void menu_entry_frame_render(ObMenuEntryFrame *self)
233 {
234 RrAppearance *item_a, *text_a;
235 gint th; /* temp */
236 ObMenu *sub;
237 ObMenuFrame *frame = self->frame;
238
239 item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
240 !self->entry->data.normal.enabled) ?
241 self->a_disabled :
242 (self == self->frame->selected ?
243 self->a_selected :
244 self->a_normal));
245 switch (self->entry->type) {
246 case OB_MENU_ENTRY_TYPE_NORMAL:
247 case OB_MENU_ENTRY_TYPE_SUBMENU:
248 th = self->frame->item_h;
249 break;
250 case OB_MENU_ENTRY_TYPE_SEPARATOR:
251 th = SEPARATOR_HEIGHT + 2*PADDING;
252 break;
253 default:
254 g_assert_not_reached();
255 }
256 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
257 XResizeWindow(ob_display, self->window,
258 self->area.width, self->area.height);
259 item_a->surface.parent = self->frame->a_items;
260 item_a->surface.parentx = self->area.x;
261 item_a->surface.parenty = self->area.y;
262 RrPaint(item_a, self->window, self->area.width, self->area.height);
263
264 text_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
265 !self->entry->data.normal.enabled) ?
266 self->a_text_disabled :
267 (self == self->frame->selected ?
268 self->a_text_selected :
269 self->a_text_normal));
270 switch (self->entry->type) {
271 case OB_MENU_ENTRY_TYPE_NORMAL:
272 text_a->texture[0].data.text.string = self->entry->data.normal.label;
273 break;
274 case OB_MENU_ENTRY_TYPE_SUBMENU:
275 sub = self->entry->data.submenu.submenu;
276 text_a->texture[0].data.text.string = sub ? sub->title : "";
277 break;
278 case OB_MENU_ENTRY_TYPE_SEPARATOR:
279 break;
280 }
281
282 switch (self->entry->type) {
283 case OB_MENU_ENTRY_TYPE_NORMAL:
284 XMoveResizeWindow(ob_display, self->text,
285 self->frame->text_x, PADDING,
286 self->frame->text_w,
287 self->frame->item_h - 2*PADDING);
288 text_a->surface.parent = item_a;
289 text_a->surface.parentx = self->frame->text_x;
290 text_a->surface.parenty = PADDING;
291 RrPaint(text_a, self->text, self->frame->text_w,
292 self->frame->item_h - 2*PADDING);
293 break;
294 case OB_MENU_ENTRY_TYPE_SUBMENU:
295 XMoveResizeWindow(ob_display, self->text,
296 self->frame->text_x, PADDING,
297 self->frame->text_w - self->frame->item_h,
298 self->frame->item_h - 2*PADDING);
299 text_a->surface.parent = item_a;
300 text_a->surface.parentx = self->frame->text_x;
301 text_a->surface.parenty = PADDING;
302 RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
303 self->frame->item_h - 2*PADDING);
304 break;
305 case OB_MENU_ENTRY_TYPE_SEPARATOR:
306 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
307 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
308 self->a_separator->surface.parent = item_a;
309 self->a_separator->surface.parentx = PADDING;
310 self->a_separator->surface.parenty = PADDING;
311 self->a_separator->texture[0].data.lineart.color =
312 text_a->texture[0].data.text.color;
313 self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
314 self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT / 2;
315 self->a_separator->texture[0].data.lineart.x2 =
316 self->area.width - 4*PADDING;
317 self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT / 2;
318 RrPaint(self->a_separator, self->text,
319 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
320 break;
321 }
322
323 if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
324 self->entry->data.normal.icon_data)
325 {
326 XMoveResizeWindow(ob_display, self->icon,
327 PADDING, frame->item_margin.top,
328 self->frame->item_h - frame->item_margin.top
329 - frame->item_margin.bottom,
330 self->frame->item_h - frame->item_margin.top
331 - frame->item_margin.bottom);
332 self->a_icon->texture[0].data.rgba.width =
333 self->entry->data.normal.icon_width;
334 self->a_icon->texture[0].data.rgba.height =
335 self->entry->data.normal.icon_height;
336 self->a_icon->texture[0].data.rgba.data =
337 self->entry->data.normal.icon_data;
338 self->a_icon->surface.parent = item_a;
339 self->a_icon->surface.parentx = PADDING;
340 self->a_icon->surface.parenty = frame->item_margin.top;
341 RrPaint(self->a_icon, self->icon,
342 self->frame->item_h - frame->item_margin.top
343 - frame->item_margin.bottom,
344 self->frame->item_h - frame->item_margin.top
345 - frame->item_margin.bottom);
346 XMapWindow(ob_display, self->icon);
347 } else if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
348 self->entry->data.normal.mask)
349 {
350 RrColor *c;
351
352 XMoveResizeWindow(ob_display, self->icon,
353 PADDING, frame->item_margin.top,
354 self->frame->item_h - frame->item_margin.top
355 - frame->item_margin.bottom,
356 self->frame->item_h - frame->item_margin.top
357 - frame->item_margin.bottom);
358 self->a_mask->texture[0].data.mask.mask =
359 self->entry->data.normal.mask;
360
361 c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
362 !self->entry->data.normal.enabled) ?
363 self->entry->data.normal.mask_disabled_color :
364 (self == self->frame->selected ?
365 self->entry->data.normal.mask_selected_color :
366 self->entry->data.normal.mask_normal_color));
367 self->a_mask->texture[0].data.mask.color = c;
368
369 self->a_mask->surface.parent = item_a;
370 self->a_mask->surface.parentx = PADDING;
371 self->a_mask->surface.parenty = frame->item_margin.top;
372 RrPaint(self->a_mask, self->icon,
373 self->frame->item_h - frame->item_margin.top
374 - frame->item_margin.bottom,
375 self->frame->item_h - frame->item_margin.top
376 - frame->item_margin.bottom);
377 XMapWindow(ob_display, self->icon);
378 } else
379 XUnmapWindow(ob_display, self->icon);
380
381 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
382 RrAppearance *bullet_a;
383 XMoveResizeWindow(ob_display, self->bullet,
384 self->frame->text_x + self->frame->text_w
385 - self->frame->item_h + PADDING, PADDING,
386 self->frame->item_h - 2*PADDING,
387 self->frame->item_h - 2*PADDING);
388 bullet_a = (self == self->frame->selected ?
389 self->a_bullet_selected :
390 self->a_bullet_normal);
391 bullet_a->surface.parent = item_a;
392 bullet_a->surface.parentx =
393 self->frame->text_x + self->frame->text_w - self->frame->item_h
394 + PADDING;
395 bullet_a->surface.parenty = PADDING;
396 RrPaint(bullet_a, self->bullet,
397 self->frame->item_h - 2*PADDING,
398 self->frame->item_h - 2*PADDING);
399 XMapWindow(ob_display, self->bullet);
400 } else
401 XUnmapWindow(ob_display, self->bullet);
402
403 XFlush(ob_display);
404 }
405
406 static void menu_frame_render(ObMenuFrame *self)
407 {
408 gint w = 0, h = 0;
409 gint allitems_h = 0;
410 gint tw, th; /* temps */
411 GList *it;
412 gboolean has_icon = FALSE;
413 ObMenu *sub;
414
415 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
416 XSetWindowBorder(ob_display, self->window,
417 RrColorPixel(ob_rr_theme->b_color));
418
419 if (!self->parent && self->show_title) {
420 XMoveWindow(ob_display, self->title,
421 -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
422
423 self->a_title->texture[0].data.text.string = self->menu->title;
424 RrMinsize(self->a_title, &tw, &th);
425 tw = MIN(tw, MAX_MENU_WIDTH) + ob_rr_theme->padding * 2;
426 w = MAX(w, tw);
427
428 th = ob_rr_theme->menu_title_height;
429 h += (self->title_h = th + ob_rr_theme->bwidth);
430
431 XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
432 XSetWindowBorder(ob_display, self->title,
433 RrColorPixel(ob_rr_theme->b_color));
434 }
435
436 XMoveWindow(ob_display, self->items, 0, h);
437
438 STRUT_SET(self->item_margin, 0, 0, 0, 0);
439
440 if (self->entries) {
441 ObMenuEntryFrame *e = self->entries->data;
442 gint l, t, r, b;
443
444 e->a_text_normal->texture[0].data.text.string = "";
445 RrMinsize(e->a_text_normal, &tw, &th);
446 tw += 2*PADDING;
447 th += 2*PADDING;
448 self->item_h = th;
449
450 RrMargins(e->a_normal, &l, &t, &r, &b);
451 STRUT_SET(self->item_margin,
452 MAX(self->item_margin.left, l),
453 MAX(self->item_margin.top, t),
454 MAX(self->item_margin.right, r),
455 MAX(self->item_margin.bottom, b));
456 RrMargins(e->a_selected, &l, &t, &r, &b);
457 STRUT_SET(self->item_margin,
458 MAX(self->item_margin.left, l),
459 MAX(self->item_margin.top, t),
460 MAX(self->item_margin.right, r),
461 MAX(self->item_margin.bottom, b));
462 RrMargins(e->a_disabled, &l, &t, &r, &b);
463 STRUT_SET(self->item_margin,
464 MAX(self->item_margin.left, l),
465 MAX(self->item_margin.top, t),
466 MAX(self->item_margin.right, r),
467 MAX(self->item_margin.bottom, b));
468 } else
469 self->item_h = 0;
470
471 for (it = self->entries; it; it = g_list_next(it)) {
472 RrAppearance *text_a;
473 ObMenuEntryFrame *e = it->data;
474
475 RECT_SET_POINT(e->area, 0, allitems_h);
476 XMoveWindow(ob_display, e->window, 0, e->area.y);
477
478 text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
479 !e->entry->data.normal.enabled) ?
480 e->a_text_disabled :
481 (e == self->selected ?
482 e->a_text_selected :
483 e->a_text_normal));
484 switch (e->entry->type) {
485 case OB_MENU_ENTRY_TYPE_NORMAL:
486 text_a->texture[0].data.text.string = e->entry->data.normal.label;
487 RrMinsize(text_a, &tw, &th);
488 tw = MIN(tw, MAX_MENU_WIDTH);
489
490 if (e->entry->data.normal.icon_data ||
491 e->entry->data.normal.mask)
492 has_icon = TRUE;
493 break;
494 case OB_MENU_ENTRY_TYPE_SUBMENU:
495 sub = e->entry->data.submenu.submenu;
496 text_a->texture[0].data.text.string = sub ? sub->title : "";
497 RrMinsize(text_a, &tw, &th);
498 tw = MIN(tw, MAX_MENU_WIDTH);
499
500 if (e->entry->data.normal.icon_data ||
501 e->entry->data.normal.mask)
502 has_icon = TRUE;
503
504 tw += self->item_h - PADDING;
505 break;
506 case OB_MENU_ENTRY_TYPE_SEPARATOR:
507 tw = 0;
508 th = SEPARATOR_HEIGHT;
509 break;
510 }
511 tw += 2*PADDING;
512 th += 2*PADDING;
513 w = MAX(w, tw);
514 h += th;
515 allitems_h += th;
516 }
517
518 self->text_x = PADDING;
519 self->text_w = w;
520
521 if (self->entries) {
522 if (has_icon) {
523 w += self->item_h + PADDING;
524 self->text_x += self->item_h + PADDING;
525 }
526 }
527
528 if (!w) w = 10;
529 if (!allitems_h) {
530 allitems_h = 3;
531 h += 3;
532 }
533
534 XResizeWindow(ob_display, self->window, w, h);
535 XResizeWindow(ob_display, self->items, w, allitems_h);
536
537 self->inner_w = w;
538
539 if (!self->parent && self->show_title) {
540 XResizeWindow(ob_display, self->title,
541 w, self->title_h - ob_rr_theme->bwidth);
542 RrPaint(self->a_title, self->title,
543 w, self->title_h - ob_rr_theme->bwidth);
544 XMapWindow(ob_display, self->title);
545 } else
546 XUnmapWindow(ob_display, self->title);
547
548 RrPaint(self->a_items, self->items, w, allitems_h);
549
550 for (it = self->entries; it; it = g_list_next(it))
551 menu_entry_frame_render(it->data);
552
553 w += ob_rr_theme->bwidth * 2;
554 h += ob_rr_theme->bwidth * 2;
555
556 RECT_SET_SIZE(self->area, w, h);
557
558 XFlush(ob_display);
559 }
560
561 static void menu_frame_update(ObMenuFrame *self)
562 {
563 GList *mit, *fit;
564
565 menu_pipe_execute(self->menu);
566 menu_find_submenus(self->menu);
567
568 self->selected = NULL;
569
570 for (mit = self->menu->entries, fit = self->entries; mit && fit;
571 mit = g_list_next(mit), fit = g_list_next(fit))
572 {
573 ObMenuEntryFrame *f = fit->data;
574 f->entry = mit->data;
575 }
576
577 while (mit) {
578 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
579 self->entries = g_list_append(self->entries, e);
580 mit = g_list_next(mit);
581 }
582
583 while (fit) {
584 GList *n = g_list_next(fit);
585 menu_entry_frame_free(fit->data);
586 self->entries = g_list_delete_link(self->entries, fit);
587 fit = n;
588 }
589
590 menu_frame_render(self);
591 }
592
593 gboolean menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
594 {
595 GList *it;
596
597 if (g_list_find(menu_frame_visible, self))
598 return TRUE;
599
600 if (menu_frame_visible == NULL) {
601 /* no menus shown yet */
602 if (!grab_pointer(TRUE, OB_CURSOR_NONE))
603 return FALSE;
604 if (!grab_keyboard(TRUE)) {
605 grab_pointer(FALSE, OB_CURSOR_NONE);
606 return FALSE;
607 }
608 }
609
610 if (parent) {
611 self->monitor = parent->monitor;
612 if (parent->child)
613 menu_frame_hide(parent->child);
614 parent->child = self;
615 }
616 self->parent = parent;
617
618 /* determine if the underlying menu is already visible */
619 for (it = menu_frame_visible; it; it = g_list_next(it)) {
620 ObMenuFrame *f = it->data;
621 if (f->menu == self->menu)
622 break;
623 }
624 if (!it) {
625 if (self->menu->update_func)
626 self->menu->update_func(self, self->menu->data);
627 }
628
629 menu_frame_update(self);
630
631 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
632
633 if (config_menu_middle) {
634 if (self->parent)
635 menu_frame_move(self, self->area.x, self->area.y
636 - self->area.height/2
637 + self->item_h/2);
638 else if (self->show_title)
639 menu_frame_move(self, self->area.x - self->area.width/2,
640 self->area.y - self->title_h*3/4);
641 }
642
643 menu_frame_move_on_screen(self);
644
645 XMapWindow(ob_display, self->window);
646
647 return TRUE;
648 }
649
650 void menu_frame_hide(ObMenuFrame *self)
651 {
652 GList *it = g_list_find(menu_frame_visible, self);
653
654 if (!it)
655 return;
656
657 if (self->child)
658 menu_frame_hide(self->child);
659
660 if (self->parent)
661 self->parent->child = NULL;
662 self->parent = NULL;
663
664 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
665
666 if (menu_frame_visible == NULL) {
667 /* last menu shown */
668 grab_pointer(FALSE, OB_CURSOR_NONE);
669 grab_keyboard(FALSE);
670 }
671
672 XUnmapWindow(ob_display, self->window);
673
674 menu_frame_free(self);
675 }
676
677 void menu_frame_hide_all()
678 {
679 GList *it;
680
681 if (config_submenu_show_delay) {
682 /* remove any submenu open requests */
683 ob_main_loop_timeout_remove(ob_main_loop,
684 menu_entry_frame_submenu_timeout);
685 }
686 if ((it = g_list_last(menu_frame_visible)))
687 menu_frame_hide(it->data);
688 }
689
690 void menu_frame_hide_all_client(ObClient *client)
691 {
692 GList *it = g_list_last(menu_frame_visible);
693 if (it) {
694 ObMenuFrame *f = it->data;
695 if (f->client == client)
696 menu_frame_hide(f);
697 }
698 }
699
700
701 ObMenuFrame* menu_frame_under(gint x, gint y)
702 {
703 ObMenuFrame *ret = NULL;
704 GList *it;
705
706 for (it = menu_frame_visible; it; it = g_list_next(it)) {
707 ObMenuFrame *f = it->data;
708
709 if (RECT_CONTAINS(f->area, x, y)) {
710 ret = f;
711 break;
712 }
713 }
714 return ret;
715 }
716
717 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
718 {
719 ObMenuFrame *frame;
720 ObMenuEntryFrame *ret = NULL;
721 GList *it;
722
723 if ((frame = menu_frame_under(x, y))) {
724 x -= ob_rr_theme->bwidth + frame->area.x;
725 y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
726
727 for (it = frame->entries; it; it = g_list_next(it)) {
728 ObMenuEntryFrame *e = it->data;
729
730 if (RECT_CONTAINS(e->area, x, y)) {
731 ret = e;
732 break;
733 }
734 }
735 }
736 return ret;
737 }
738
739 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
740 {
741 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
742 return FALSE;
743 }
744
745 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
746 {
747 ObMenuEntryFrame *old = self->selected;
748 ObMenuFrame *oldchild = self->child;
749
750 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
751 entry = old;
752
753 if (old == entry) return;
754
755 if (config_submenu_show_delay) {
756 /* remove any submenu open requests */
757 ob_main_loop_timeout_remove(ob_main_loop,
758 menu_entry_frame_submenu_timeout);
759 }
760
761 self->selected = entry;
762
763 if (old)
764 menu_entry_frame_render(old);
765 if (oldchild)
766 menu_frame_hide(oldchild);
767
768 if (self->selected) {
769 menu_entry_frame_render(self->selected);
770
771 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
772 if (config_submenu_show_delay) {
773 /* initiate a new submenu open request */
774 ob_main_loop_timeout_add(ob_main_loop,
775 config_submenu_show_delay * 1000,
776 menu_entry_frame_submenu_timeout,
777 self->selected,
778 NULL);
779 } else {
780 menu_entry_frame_show_submenu(self->selected);
781 }
782 }
783 }
784 }
785
786 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
787 {
788 ObMenuFrame *f;
789
790 if (!self->entry->data.submenu.submenu) return;
791
792 f = menu_frame_new(self->entry->data.submenu.submenu,
793 self->frame->client);
794 menu_frame_move(f,
795 self->frame->area.x
796 + self->frame->area.width
797 - ob_rr_theme->menu_overlap
798 - ob_rr_theme->bwidth,
799 self->frame->area.y
800 + self->frame->title_h
801 + self->area.y
802 + (config_menu_middle ? 1 : ob_rr_theme->menu_overlap));
803 menu_frame_show(f, self->frame);
804 }
805
806 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
807 {
808 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
809 self->entry->data.normal.enabled)
810 {
811 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
812 gets freed */
813 ObMenuEntry *entry = self->entry;
814 ObMenuExecuteFunc func = self->frame->menu->execute_func;
815 gpointer data = self->frame->menu->data;
816 GSList *acts = self->entry->data.normal.actions;
817 ObClient *client = self->frame->client;
818
819 /* release grabs before executing the shit */
820 if (!(state & ControlMask))
821 menu_frame_hide_all();
822
823 if (func)
824 func(entry, state, data);
825 else
826 action_run(acts, client, state);
827 }
828 }
829
830 void menu_frame_select_previous(ObMenuFrame *self)
831 {
832 GList *it = NULL, *start;
833
834 if (self->entries) {
835 start = it = g_list_find(self->entries, self->selected);
836 while (TRUE) {
837 ObMenuEntryFrame *e;
838
839 it = it ? g_list_previous(it) : g_list_last(self->entries);
840 if (it == start)
841 break;
842
843 if (it) {
844 e = it->data;
845 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
846 break;
847 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
848 e->entry->data.normal.enabled)
849 break;
850 }
851 }
852 }
853 menu_frame_select(self, it ? it->data : NULL);
854 }
855
856 void menu_frame_select_next(ObMenuFrame *self)
857 {
858 GList *it = NULL, *start;
859
860 if (self->entries) {
861 start = it = g_list_find(self->entries, self->selected);
862 while (TRUE) {
863 ObMenuEntryFrame *e;
864
865 it = it ? g_list_next(it) : self->entries;
866 if (it == start)
867 break;
868
869 if (it) {
870 e = it->data;
871 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
872 break;
873 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
874 e->entry->data.normal.enabled)
875 break;
876 }
877 }
878 }
879 menu_frame_select(self, it ? it->data : NULL);
880 }
This page took 0.0759379999999999 seconds and 4 git commands to generate.