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