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