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