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