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