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