Resolve gravity coords in menuframe so it can use the widths
[mikachu/openbox.git] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    menuframe.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana 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 "actions.h"
25 #include "event.h"
26 #include "grab.h"
27 #include "openbox.h"
28 #include "config.h"
29 #include "obt/prop.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
32
33 #define PADDING 2
34 #define MAX_MENU_WIDTH 400
35
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39                          LeaveWindowMask)
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41                          ButtonPressMask | ButtonReleaseMask | \
42                          PointerMotionMask)
43
44 GList *menu_frame_visible;
45 GHashTable *menu_frame_map;
46
47 static RrAppearance *a_sep;
48 static guint submenu_show_timer = 0;
49 static guint submenu_hide_timer = 0;
50
51 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
52                                               ObMenuFrame *frame);
53 static void menu_entry_frame_free(ObMenuEntryFrame *self);
54 static void menu_frame_update(ObMenuFrame *self);
55 static gboolean submenu_show_timeout(gpointer data);
56 static void menu_frame_hide(ObMenuFrame *self);
57
58 static gboolean submenu_hide_timeout(gpointer data);
59
60 static Window createWindow(Window parent, gulong mask,
61                            XSetWindowAttributes *attrib)
62 {
63     return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
64                          RrDepth(ob_rr_inst), InputOutput,
65                          RrVisual(ob_rr_inst), mask, attrib);
66 }
67
68 static void client_dest(ObClient *client, gpointer data)
69 {
70     GList *it;
71
72     /* menus can be associated with a client, so null those refs since
73        we are disappearing now */
74     for (it = menu_frame_visible; it; it = g_list_next(it)) {
75         ObMenuFrame *f = it->data;
76         if (f->client == client)
77             f->client = NULL;
78     }
79 }
80
81 void menu_frame_startup(gboolean reconfig)
82 {
83     gint i;
84
85     a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
86     RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
87     for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
88         a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
89         a_sep->texture[i].data.lineart.color =
90             ob_rr_theme->menu_sep_color;
91     }
92
93     if (reconfig) return;
94
95     client_add_destroy_notify(client_dest, NULL);
96     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
97 }
98
99 void menu_frame_shutdown(gboolean reconfig)
100 {
101     RrAppearanceFree(a_sep);
102
103     if (reconfig) return;
104
105     client_remove_destroy_notify(client_dest);
106     g_hash_table_destroy(menu_frame_map);
107 }
108
109 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
110 {
111     ObMenuFrame *self;
112     XSetWindowAttributes attr;
113
114     self = g_slice_new0(ObMenuFrame);
115     self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
116     self->menu = menu;
117     self->selected = NULL;
118     self->client = client;
119     self->direction_right = TRUE;
120     self->show_from = show_from;
121
122     attr.event_mask = FRAME_EVENTMASK;
123     self->window = createWindow(obt_root(ob_screen),
124                                 CWEventMask, &attr);
125
126     /* make it a popup menu type window */
127     OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
128                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
129
130     XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
131     XSetWindowBorder(obt_display, self->window,
132                      RrColorPixel(ob_rr_theme->menu_border_color));
133
134     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
135
136     window_add(&self->window, MENUFRAME_AS_WINDOW(self));
137     stacking_add(MENUFRAME_AS_WINDOW(self));
138
139     return self;
140 }
141
142 void menu_frame_free(ObMenuFrame *self)
143 {
144     if (self) {
145         while (self->entries) {
146             menu_entry_frame_free(self->entries->data);
147             self->entries = g_list_delete_link(self->entries, self->entries);
148         }
149
150         stacking_remove(MENUFRAME_AS_WINDOW(self));
151         window_remove(self->window);
152
153         RrAppearanceFree(self->a_items);
154
155         XDestroyWindow(obt_display, self->window);
156
157         g_slice_free(ObMenuFrame, self);
158     }
159 }
160
161 ObtIC* menu_frame_ic(ObMenuFrame *self)
162 {
163     /* menus are always used through a grab right now, so they can always use
164        the grab input context */
165     return grab_input_context();
166 }
167
168 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
169                                               ObMenuFrame *frame)
170 {
171     ObMenuEntryFrame *self;
172     XSetWindowAttributes attr;
173
174     self = g_slice_new0(ObMenuEntryFrame);
175     self->entry = entry;
176     self->frame = frame;
177
178     menu_entry_ref(entry);
179
180     attr.event_mask = ENTRY_EVENTMASK;
181     self->window = createWindow(self->frame->window, CWEventMask, &attr);
182     self->text = createWindow(self->window, 0, NULL);
183     g_hash_table_insert(menu_frame_map, &self->window, self);
184     g_hash_table_insert(menu_frame_map, &self->text, self);
185     if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
186         (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
187         self->icon = createWindow(self->window, 0, NULL);
188         g_hash_table_insert(menu_frame_map, &self->icon, self);
189     }
190     if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
191         self->bullet = createWindow(self->window, 0, NULL);
192         g_hash_table_insert(menu_frame_map, &self->bullet, self);
193     }
194
195     XMapWindow(obt_display, self->window);
196     XMapWindow(obt_display, self->text);
197
198     window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
199
200     return self;
201 }
202
203 static void menu_entry_frame_free(ObMenuEntryFrame *self)
204 {
205     if (self) {
206         menu_entry_unref(self->entry);
207
208         window_remove(self->window);
209
210         XDestroyWindow(obt_display, self->text);
211         XDestroyWindow(obt_display, self->window);
212         g_hash_table_remove(menu_frame_map, &self->text);
213         g_hash_table_remove(menu_frame_map, &self->window);
214         if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
215             (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
216             XDestroyWindow(obt_display, self->icon);
217             g_hash_table_remove(menu_frame_map, &self->icon);
218         }
219         if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
220             XDestroyWindow(obt_display, self->bullet);
221             g_hash_table_remove(menu_frame_map, &self->bullet);
222         }
223
224         g_slice_free(ObMenuEntryFrame, self);
225     }
226 }
227
228 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
229 {
230     RECT_SET_POINT(self->area, x, y);
231     self->monitor = screen_find_monitor_point(x, y);
232     XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
233 }
234
235 static void calc_position(ObMenuFrame *self, GravityPoint *position,
236                           gint *x, gint *y, gint monitor)
237 {
238     const Rect *area = screen_physical_area_monitor(monitor);
239
240     if (position->x.center)
241         *x = area->width / 2 - self->area.width / 2;
242     else {
243         *x = position->x.pos;
244         if (position->x.denom)
245             *x = (*x * area->width) / position->x.denom;
246         if (position->x.opposite)
247             *x = area->width - self->area.width - *x;
248     }
249
250     if (position->y.center)
251         *y = area->height / 2 - self->area.height / 2;
252     else {
253         *y = position->y.pos;
254         if (position->y.denom)
255             *y = (*y * area->height) / position->y.denom;
256         if (position->y.opposite)
257             *y = area->height - self->area.height - *y;
258     }
259
260     *x += area->x;
261     *y += area->y;
262 }
263
264 static void menu_frame_place_topmenu(ObMenuFrame *self, GravityPoint *pos,
265                                      gint *x, gint *y, gint monitor)
266 {
267     gint dx, dy;
268
269     calc_position(self, pos, x, y, monitor);
270
271     if (config_menu_middle) {
272         gint myx;
273
274         myx = *x;
275         *y -= self->area.height / 2;
276
277         /* try to the right of the cursor */
278         menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
279         self->direction_right = TRUE;
280         if (dx != 0) {
281             /* try to the left of the cursor */
282             myx = *x - self->area.width;
283             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
284             self->direction_right = FALSE;
285         }
286         if (dx != 0) {
287             /* if didnt fit on either side so just use what it says */
288             myx = *x;
289             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
290             self->direction_right = TRUE;
291         }
292         *x = myx + dx;
293         *y += dy;
294     } else {
295         gint myx, myy;
296
297         myx = *x;
298         myy = *y;
299
300         /* try to the bottom right of the cursor */
301         menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
302         self->direction_right = TRUE;
303         if (dx != 0 || dy != 0) {
304             /* try to the bottom left of the cursor */
305             myx = *x - self->area.width;
306             myy = *y;
307             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
308             self->direction_right = FALSE;
309         }
310         if (dx != 0 || dy != 0) {
311             /* try to the top right of the cursor */
312             myx = *x;
313             myy = *y - self->area.height;
314             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
315             self->direction_right = TRUE;
316         }
317         if (dx != 0 || dy != 0) {
318             /* try to the top left of the cursor */
319             myx = *x - self->area.width;
320             myy = *y - self->area.height;
321             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
322             self->direction_right = FALSE;
323         }
324         if (dx != 0 || dy != 0) {
325             /* if didnt fit on either side so just use what it says */
326             myx = *x;
327             myy = *y;
328             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
329             self->direction_right = TRUE;
330         }
331         *x = myx + dx;
332         *y = myy + dy;
333     }
334 }
335
336 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
337 {
338     gint overlapx, overlapy;
339     gint bwidth;
340
341     overlapx = ob_rr_theme->menu_overlap_x;
342     overlapy = ob_rr_theme->menu_overlap_y;
343     bwidth = ob_rr_theme->mbwidth;
344
345     if (self->direction_right)
346         *x = self->parent->area.x + self->parent->area.width -
347             overlapx - bwidth;
348     else
349         *x = self->parent->area.x - self->area.width + overlapx + bwidth;
350
351     *y = self->parent->area.y + self->parent_entry->area.y;
352     if (config_menu_middle)
353         *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
354     else
355         *y += overlapy;
356 }
357
358 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
359                                gint *dx, gint *dy)
360 {
361     const Rect *a = NULL;
362     Rect search = self->area;
363     gint pos, half, monitor;
364
365     *dx = *dy = 0;
366     RECT_SET_POINT(search, x, y);
367
368     if (self->parent)
369         monitor = self->parent->monitor;
370     else
371         monitor = screen_find_monitor(&search);
372
373     a = screen_physical_area_monitor(monitor);
374
375     half = g_list_length(self->entries) / 2;
376     pos = g_list_index(self->entries, self->selected);
377
378     /* if in the bottom half then check this stuff first, will keep the bottom
379        edge of the menu visible */
380     if (pos > half) {
381         *dx = MAX(*dx, a->x - x);
382         *dy = MAX(*dy, a->y - y);
383     }
384     *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
385     *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
386     /* if in the top half then check this stuff last, will keep the top
387        edge of the menu visible */
388     if (pos <= half) {
389         *dx = MAX(*dx, a->x - x);
390         *dy = MAX(*dy, a->y - y);
391     }
392 }
393
394 static void menu_entry_frame_render(ObMenuEntryFrame *self)
395 {
396     RrAppearance *item_a, *text_a;
397     gint th; /* temp */
398     ObMenu *sub;
399     ObMenuFrame *frame = self->frame;
400
401     switch (self->entry->type) {
402     case OB_MENU_ENTRY_TYPE_NORMAL:
403     case OB_MENU_ENTRY_TYPE_SUBMENU:
404         item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
405                   !self->entry->data.normal.enabled ?
406                   /* disabled */
407                   (self == self->frame->selected ?
408                    ob_rr_theme->a_menu_disabled_selected :
409                    ob_rr_theme->a_menu_disabled) :
410                   /* enabled */
411                   (self == self->frame->selected ?
412                    ob_rr_theme->a_menu_selected :
413                    ob_rr_theme->a_menu_normal));
414         th = ITEM_HEIGHT;
415         break;
416     case OB_MENU_ENTRY_TYPE_SEPARATOR:
417         if (self->entry->data.separator.label) {
418             item_a = ob_rr_theme->a_menu_title;
419             th = ob_rr_theme->menu_title_height;
420         } else {
421             item_a = ob_rr_theme->a_menu_normal;
422             th = ob_rr_theme->menu_sep_width +
423                 2*ob_rr_theme->menu_sep_paddingy;
424         }
425         break;
426     default:
427         g_assert_not_reached();
428     }
429
430     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
431     XResizeWindow(obt_display, self->window,
432                   self->area.width, self->area.height);
433     item_a->surface.parent = self->frame->a_items;
434     item_a->surface.parentx = self->area.x;
435     item_a->surface.parenty = self->area.y;
436     RrPaint(item_a, self->window, self->area.width, self->area.height);
437
438     switch (self->entry->type) {
439     case OB_MENU_ENTRY_TYPE_NORMAL:
440         text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
441                   !self->entry->data.normal.enabled ?
442                   /* disabled */
443                   (self == self->frame->selected ?
444                    ob_rr_theme->a_menu_text_disabled_selected :
445                    ob_rr_theme->a_menu_text_disabled) :
446                   /* enabled */
447                   (self == self->frame->selected ?
448                    ob_rr_theme->a_menu_text_selected :
449                    ob_rr_theme->a_menu_text_normal));
450         text_a->texture[0].data.text.string = self->entry->data.normal.label;
451         if (self->entry->data.normal.shortcut &&
452             (self->frame->menu->show_all_shortcuts ||
453              self->entry->data.normal.shortcut_always_show ||
454              self->entry->data.normal.shortcut_position > 0))
455         {
456             text_a->texture[0].data.text.shortcut = TRUE;
457             text_a->texture[0].data.text.shortcut_pos =
458                 self->entry->data.normal.shortcut_position;
459         } else
460             text_a->texture[0].data.text.shortcut = FALSE;
461         break;
462     case OB_MENU_ENTRY_TYPE_SUBMENU:
463         text_a = (self == self->frame->selected ?
464                   ob_rr_theme->a_menu_text_selected :
465                   ob_rr_theme->a_menu_text_normal);
466         sub = self->entry->data.submenu.submenu;
467         text_a->texture[0].data.text.string = sub ? sub->title : "";
468         if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
469                               sub->shortcut_always_show ||
470                               sub->shortcut_position > 0))
471         {
472             text_a->texture[0].data.text.shortcut = TRUE;
473             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
474         } else
475             text_a->texture[0].data.text.shortcut = FALSE;
476         break;
477     case OB_MENU_ENTRY_TYPE_SEPARATOR:
478         if (self->entry->data.separator.label != NULL) {
479             text_a = ob_rr_theme->a_menu_text_title;
480             text_a->texture[0].data.text.string =
481                 self->entry->data.separator.label;
482         }
483         else
484             text_a = ob_rr_theme->a_menu_text_normal;
485         break;
486     default:
487         g_assert_not_reached();
488     }
489
490     switch (self->entry->type) {
491     case OB_MENU_ENTRY_TYPE_NORMAL:
492         XMoveResizeWindow(obt_display, self->text,
493                           self->frame->text_x, PADDING,
494                           self->frame->text_w,
495                           ITEM_HEIGHT - 2*PADDING);
496         text_a->surface.parent = item_a;
497         text_a->surface.parentx = self->frame->text_x;
498         text_a->surface.parenty = PADDING;
499         RrPaint(text_a, self->text, self->frame->text_w,
500                 ITEM_HEIGHT - 2*PADDING);
501         break;
502     case OB_MENU_ENTRY_TYPE_SUBMENU:
503         XMoveResizeWindow(obt_display, self->text,
504                           self->frame->text_x, PADDING,
505                           self->frame->text_w - ITEM_HEIGHT,
506                           ITEM_HEIGHT - 2*PADDING);
507         text_a->surface.parent = item_a;
508         text_a->surface.parentx = self->frame->text_x;
509         text_a->surface.parenty = PADDING;
510         RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
511                 ITEM_HEIGHT - 2*PADDING);
512         break;
513     case OB_MENU_ENTRY_TYPE_SEPARATOR:
514         if (self->entry->data.separator.label != NULL) {
515             /* labeled separator */
516             XMoveResizeWindow(obt_display, self->text,
517                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
518                               self->area.width - 2*ob_rr_theme->paddingx,
519                               ob_rr_theme->menu_title_height -
520                               2*ob_rr_theme->paddingy);
521             text_a->surface.parent = item_a;
522             text_a->surface.parentx = ob_rr_theme->paddingx;
523             text_a->surface.parenty = ob_rr_theme->paddingy;
524             RrPaint(text_a, self->text,
525                     self->area.width - 2*ob_rr_theme->paddingx,
526                     ob_rr_theme->menu_title_height -
527                     2*ob_rr_theme->paddingy);
528         } else {
529             gint i;
530
531             /* unlabeled separator */
532             XMoveResizeWindow(obt_display, self->text, 0, 0,
533                               self->area.width,
534                               ob_rr_theme->menu_sep_width +
535                               2*ob_rr_theme->menu_sep_paddingy);
536
537             a_sep->surface.parent = item_a;
538             a_sep->surface.parentx = 0;
539             a_sep->surface.parenty = 0;
540             for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
541                 a_sep->texture[i].data.lineart.x1 =
542                     ob_rr_theme->menu_sep_paddingx;
543                 a_sep->texture[i].data.lineart.y1 =
544                     ob_rr_theme->menu_sep_paddingy + i;
545                 a_sep->texture[i].data.lineart.x2 =
546                     self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
547                 a_sep->texture[i].data.lineart.y2 =
548                     ob_rr_theme->menu_sep_paddingy + i;
549             }
550
551             RrPaint(a_sep, self->text, self->area.width,
552                     ob_rr_theme->menu_sep_width +
553                     2*ob_rr_theme->menu_sep_paddingy);
554         }
555         break;
556     default:
557         g_assert_not_reached();
558     }
559
560     if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
561          (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
562         self->entry->data.normal.icon)
563     {
564         RrAppearance *clear;
565
566         XMoveResizeWindow(obt_display, self->icon,
567                           PADDING, frame->item_margin.top,
568                           ITEM_HEIGHT - frame->item_margin.top
569                           - frame->item_margin.bottom,
570                           ITEM_HEIGHT - frame->item_margin.top
571                           - frame->item_margin.bottom);
572
573         clear = ob_rr_theme->a_clear_tex;
574         RrAppearanceClearTextures(clear);
575         clear->texture[0].type = RR_TEXTURE_IMAGE;
576         clear->texture[0].data.image.image =
577             self->entry->data.normal.icon;
578         clear->texture[0].data.image.alpha =
579             self->entry->data.normal.icon_alpha;
580         clear->surface.parent = item_a;
581         clear->surface.parentx = PADDING;
582         clear->surface.parenty = frame->item_margin.top;
583         RrPaint(clear, self->icon,
584                 ITEM_HEIGHT - frame->item_margin.top
585                 - frame->item_margin.bottom,
586                 ITEM_HEIGHT - frame->item_margin.top
587                 - frame->item_margin.bottom);
588         XMapWindow(obt_display, self->icon);
589     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
590                self->entry->data.normal.mask)
591     {
592         RrColor *c;
593         RrAppearance *clear;
594
595         XMoveResizeWindow(obt_display, self->icon,
596                           PADDING, frame->item_margin.top,
597                           ITEM_HEIGHT - frame->item_margin.top
598                           - frame->item_margin.bottom,
599                           ITEM_HEIGHT - frame->item_margin.top
600                           - frame->item_margin.bottom);
601
602         clear = ob_rr_theme->a_clear_tex;
603         RrAppearanceClearTextures(clear);
604         clear->texture[0].type = RR_TEXTURE_MASK;
605         clear->texture[0].data.mask.mask =
606             self->entry->data.normal.mask;
607
608         c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
609              !self->entry->data.normal.enabled ?
610              /* disabled */
611              (self == self->frame->selected ?
612               self->entry->data.normal.mask_disabled_selected_color :
613               self->entry->data.normal.mask_disabled_color) :
614              /* enabled */
615              (self == self->frame->selected ?
616               self->entry->data.normal.mask_selected_color :
617               self->entry->data.normal.mask_normal_color));
618         clear->texture[0].data.mask.color = c;
619
620         clear->surface.parent = item_a;
621         clear->surface.parentx = PADDING;
622         clear->surface.parenty = frame->item_margin.top;
623         RrPaint(clear, self->icon,
624                 ITEM_HEIGHT - frame->item_margin.top
625                 - frame->item_margin.bottom,
626                 ITEM_HEIGHT - frame->item_margin.top
627                 - frame->item_margin.bottom);
628         XMapWindow(obt_display, self->icon);
629     } else
630         XUnmapWindow(obt_display, self->icon);
631
632     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
633         RrAppearance *bullet_a;
634         XMoveResizeWindow(obt_display, self->bullet,
635                           self->frame->text_x + self->frame->text_w -
636                           ITEM_HEIGHT + PADDING, PADDING,
637                           ITEM_HEIGHT - 2*PADDING,
638                           ITEM_HEIGHT - 2*PADDING);
639         bullet_a = (self == self->frame->selected ?
640                     ob_rr_theme->a_menu_bullet_selected :
641                     ob_rr_theme->a_menu_bullet_normal);
642         bullet_a->surface.parent = item_a;
643         bullet_a->surface.parentx =
644             self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
645         bullet_a->surface.parenty = PADDING;
646         RrPaint(bullet_a, self->bullet,
647                 ITEM_HEIGHT - 2*PADDING,
648                 ITEM_HEIGHT - 2*PADDING);
649         XMapWindow(obt_display, self->bullet);
650     } else
651         XUnmapWindow(obt_display, self->bullet);
652
653     XFlush(obt_display);
654 }
655
656 /*! this code is taken from the menu_frame_render. if that changes, this won't
657   work.. */
658 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
659                                         gboolean first_entry,
660                                         gboolean last_entry)
661 {
662     ObMenuEntryType t;
663     gint h = 0;
664
665     h += 2*PADDING;
666
667     if (self)
668         t = self->entry->type;
669     else
670         /* this is the More... entry, it's NORMAL type */
671         t = OB_MENU_ENTRY_TYPE_NORMAL;
672
673     switch (t) {
674     case OB_MENU_ENTRY_TYPE_NORMAL:
675     case OB_MENU_ENTRY_TYPE_SUBMENU:
676         h += ob_rr_theme->menu_font_height;
677         break;
678     case OB_MENU_ENTRY_TYPE_SEPARATOR:
679         if (self->entry->data.separator.label != NULL) {
680             h += ob_rr_theme->menu_title_height +
681                 (ob_rr_theme->mbwidth - PADDING) * 2;
682
683             /* if the first entry is a labeled separator, then make its border
684                overlap with the menu's outside border */
685             if (first_entry)
686                 h -= ob_rr_theme->mbwidth;
687             /* if the last entry is a labeled separator, then make its border
688                overlap with the menu's outside border */
689             if (last_entry)
690                 h -= ob_rr_theme->mbwidth;
691         } else {
692             h += ob_rr_theme->menu_sep_width +
693                 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
694         }
695         break;
696     }
697
698     return h;
699 }
700
701 void menu_frame_render(ObMenuFrame *self)
702 {
703     gint w = 0, h = 0;
704     gint tw, th; /* temps */
705     GList *it;
706     gboolean has_icon = FALSE;
707     ObMenu *sub;
708     ObMenuEntryFrame *e;
709
710     /* find text dimensions */
711
712     STRUT_SET(self->item_margin, 0, 0, 0, 0);
713
714     if (self->entries) {
715         gint l, t, r, b;
716
717         e = self->entries->data;
718         ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
719         tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
720         tw += 2*PADDING;
721
722         th = ITEM_HEIGHT;
723
724         RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
725         STRUT_SET(self->item_margin,
726                   MAX(self->item_margin.left, l),
727                   MAX(self->item_margin.top, t),
728                   MAX(self->item_margin.right, r),
729                   MAX(self->item_margin.bottom, b));
730         RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
731         STRUT_SET(self->item_margin,
732                   MAX(self->item_margin.left, l),
733                   MAX(self->item_margin.top, t),
734                   MAX(self->item_margin.right, r),
735                   MAX(self->item_margin.bottom, b));
736         RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
737         STRUT_SET(self->item_margin,
738                   MAX(self->item_margin.left, l),
739                   MAX(self->item_margin.top, t),
740                   MAX(self->item_margin.right, r),
741                   MAX(self->item_margin.bottom, b));
742         RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
743         STRUT_SET(self->item_margin,
744                   MAX(self->item_margin.left, l),
745                   MAX(self->item_margin.top, t),
746                   MAX(self->item_margin.right, r),
747                   MAX(self->item_margin.bottom, b));
748     }
749
750     /* render the entries */
751
752     for (it = self->entries; it; it = g_list_next(it)) {
753         RrAppearance *text_a;
754         e = it->data;
755
756         /* if the first entry is a labeled separator, then make its border
757            overlap with the menu's outside border */
758         if (it == self->entries &&
759             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
760             e->entry->data.separator.label)
761         {
762             h -= ob_rr_theme->mbwidth;
763         }
764
765         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
766             e->entry->data.separator.label)
767         {
768             e->border = ob_rr_theme->mbwidth;
769         }
770
771         RECT_SET_POINT(e->area, 0, h+e->border);
772         XMoveWindow(obt_display, e->window,
773                     e->area.x-e->border, e->area.y-e->border);
774         XSetWindowBorderWidth(obt_display, e->window, e->border);
775         XSetWindowBorder(obt_display, e->window,
776                          RrColorPixel(ob_rr_theme->menu_border_color));
777
778         text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
779                   !e->entry->data.normal.enabled ?
780                   /* disabled */
781                   (e == self->selected ?
782                    ob_rr_theme->a_menu_text_disabled_selected :
783                    ob_rr_theme->a_menu_text_disabled) :
784                   /* enabled */
785                   (e == self->selected ?
786                    ob_rr_theme->a_menu_text_selected : 
787                    ob_rr_theme->a_menu_text_normal));
788         switch (e->entry->type) {
789         case OB_MENU_ENTRY_TYPE_NORMAL:
790             text_a->texture[0].data.text.string = e->entry->data.normal.label;
791             tw = RrMinWidth(text_a);
792             tw = MIN(tw, MAX_MENU_WIDTH);
793             th = ob_rr_theme->menu_font_height;
794
795             if (e->entry->data.normal.icon ||
796                 e->entry->data.normal.mask)
797                 has_icon = TRUE;
798             break;
799         case OB_MENU_ENTRY_TYPE_SUBMENU:
800             sub = e->entry->data.submenu.submenu;
801             text_a->texture[0].data.text.string = sub ? sub->title : "";
802             tw = RrMinWidth(text_a);
803             tw = MIN(tw, MAX_MENU_WIDTH);
804             th = ob_rr_theme->menu_font_height;
805
806             if (e->entry->data.normal.icon ||
807                 e->entry->data.normal.mask)
808                 has_icon = TRUE;
809
810             tw += ITEM_HEIGHT - PADDING;
811             break;
812         case OB_MENU_ENTRY_TYPE_SEPARATOR:
813             if (e->entry->data.separator.label != NULL) {
814                 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
815                     e->entry->data.separator.label;
816                 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
817                     2*ob_rr_theme->paddingx;
818                 tw = MIN(tw, MAX_MENU_WIDTH);
819                 th = ob_rr_theme->menu_title_height +
820                     (ob_rr_theme->mbwidth - PADDING) *2;
821             } else {
822                 tw = 0;
823                 th = ob_rr_theme->menu_sep_width +
824                     2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
825             }
826             break;
827         default:
828             g_assert_not_reached();
829         }
830         tw += 2*PADDING;
831         th += 2*PADDING;
832         w = MAX(w, tw);
833         h += th;
834     }
835
836     /* if the last entry is a labeled separator, then make its border
837        overlap with the menu's outside border */
838     it = g_list_last(self->entries);
839     e = it ? it->data : NULL;
840     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
841         e->entry->data.separator.label)
842     {
843         h -= ob_rr_theme->mbwidth;
844     }
845
846     self->text_x = PADDING;
847     self->text_w = w;
848
849     if (self->entries) {
850         if (has_icon) {
851             w += ITEM_HEIGHT + PADDING;
852             self->text_x += ITEM_HEIGHT + PADDING;
853         }
854     }
855
856     if (!w) w = 10;
857     if (!h) h = 3;
858
859     XResizeWindow(obt_display, self->window, w, h);
860
861     self->inner_w = w;
862
863     RrPaint(self->a_items, self->window, w, h);
864
865     for (it = self->entries; it; it = g_list_next(it))
866         menu_entry_frame_render(it->data);
867
868     w += ob_rr_theme->mbwidth * 2;
869     h += ob_rr_theme->mbwidth * 2;
870
871     RECT_SET_SIZE(self->area, w, h);
872
873     XFlush(obt_display);
874 }
875
876 static void menu_frame_update(ObMenuFrame *self)
877 {
878     GList *mit, *fit;
879     const Rect *a;
880     gint h;
881
882     menu_pipe_execute(self->menu);
883     menu_find_submenus(self->menu);
884
885     self->selected = NULL;
886
887     /* start at show_from */
888     mit = g_list_nth(self->menu->entries, self->show_from);
889
890     /* go through the menu's and frame's entries and connect the frame entries
891        to the menu entries */
892     for (fit = self->entries; mit && fit;
893          mit = g_list_next(mit), fit = g_list_next(fit))
894     {
895         ObMenuEntryFrame *f = fit->data;
896         f->entry = mit->data;
897     }
898
899     /* if there are more menu entries than in the frame, add them */
900     while (mit) {
901         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
902         self->entries = g_list_append(self->entries, e);
903         mit = g_list_next(mit);
904     }
905
906     /* if there are more frame entries than menu entries then get rid of
907        them */
908     while (fit) {
909         GList *n = g_list_next(fit);
910         menu_entry_frame_free(fit->data);
911         self->entries = g_list_delete_link(self->entries, fit);
912         fit = n;
913     }
914
915     /* * make the menu fit on the screen */
916
917     /* calculate the height of the menu */
918     h = 0;
919     for (fit = self->entries; fit; fit = g_list_next(fit))
920         h += menu_entry_frame_get_height(fit->data,
921                                          fit == self->entries,
922                                          g_list_next(fit) == NULL);
923     /* add the border at the top and bottom */
924     h += ob_rr_theme->mbwidth * 2;
925
926     a = screen_physical_area_monitor(self->monitor);
927
928     if (h > a->height) {
929         GList *flast, *tmp;
930         gboolean last_entry = TRUE;
931
932         /* take the height of our More... entry into account */
933         h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
934
935         /* start at the end of the entries */
936         flast = g_list_last(self->entries);
937
938         /* pull out all the entries from the frame that don't
939            fit on the screen, leaving at least 1 though */
940         while (h > a->height && g_list_previous(flast) != NULL) {
941             /* update the height, without this entry */
942             h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
943
944             /* destroy the entry we're not displaying */
945             tmp = flast;
946             flast = g_list_previous(flast);
947             menu_entry_frame_free(tmp->data);
948             self->entries = g_list_delete_link(self->entries, tmp);
949
950             /* only the first one that we see is the last entry in the menu */
951             last_entry = FALSE;
952         };
953
954         {
955             ObMenuEntry *more_entry;
956             ObMenuEntryFrame *more_frame;
957             /* make the More... menu entry frame which will display in this
958                frame.
959                if self->menu->more_menu is NULL that means that this is already
960                More... menu, so just use ourself.
961             */
962             more_entry = menu_get_more((self->menu->more_menu ?
963                                         self->menu->more_menu :
964                                         self->menu),
965                                        /* continue where we left off */
966                                        self->show_from +
967                                        g_list_length(self->entries));
968             more_frame = menu_entry_frame_new(more_entry, self);
969             /* make it get deleted when the menu frame goes away */
970             menu_entry_unref(more_entry);
971
972             /* add our More... entry to the frame */
973             self->entries = g_list_append(self->entries, more_frame);
974         }
975     }
976
977     menu_frame_render(self);
978 }
979
980 static gboolean menu_frame_is_visible(ObMenuFrame *self)
981 {
982     return !!(g_list_find(menu_frame_visible, self));
983 }
984
985 static gboolean menu_frame_show(ObMenuFrame *self)
986 {
987     GList *it;
988
989     /* determine if the underlying menu is already visible */
990     for (it = menu_frame_visible; it; it = g_list_next(it)) {
991         ObMenuFrame *f = it->data;
992         if (f->menu == self->menu)
993             break;
994     }
995     if (!it) {
996         if (self->menu->update_func)
997             if (!self->menu->update_func(self, self->menu->data))
998                 return FALSE;
999     }
1000
1001     if (menu_frame_visible == NULL) {
1002         /* no menus shown yet */
1003
1004         /* grab the pointer in such a way as to pass through "owner events"
1005            so that we can get enter/leave notifies in the menu. */
1006         if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
1007             return FALSE;
1008         if (!grab_keyboard()) {
1009             ungrab_pointer();
1010             return FALSE;
1011         }
1012     }
1013
1014     menu_frame_update(self);
1015
1016     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
1017
1018     if (self->menu->show_func)
1019         self->menu->show_func(self, self->menu->data);
1020
1021     return TRUE;
1022 }
1023
1024 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
1025                                  gint monitor, gboolean mouse)
1026 {
1027     gint px, py;
1028     gint x, y;
1029
1030     if (menu_frame_is_visible(self))
1031         return TRUE;
1032     if (!menu_frame_show(self))
1033         return FALSE;
1034
1035     if (self->menu->place_func) {
1036         x = pos.x.pos;
1037         y = pos.y.pos;
1038         self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1039     } else {
1040         menu_frame_place_topmenu(self, &pos, &x, &y, monitor);
1041     }
1042
1043     menu_frame_move(self, x, y);
1044
1045     XMapWindow(obt_display, self->window);
1046
1047     if (screen_pointer_pos(&px, &py)) {
1048         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1049         if (e && e->frame == self)
1050             e->ignore_enters++;
1051     }
1052
1053     return TRUE;
1054 }
1055
1056 /*! Stop hiding an open submenu.
1057     @child The OnMenuFrame of the submenu to be hidden
1058 */
1059 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1060 {
1061     if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1062     submenu_hide_timer = 0;
1063 }
1064
1065 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1066                                  ObMenuEntryFrame *parent_entry)
1067 {
1068     gint x, y, dx, dy;
1069     gint px, py;
1070
1071     if (menu_frame_is_visible(self))
1072         return TRUE;
1073
1074     self->monitor = parent->monitor;
1075     self->parent = parent;
1076     self->parent_entry = parent_entry;
1077
1078     /* set up parent's child to be us */
1079     if ((parent->child) != self) {
1080         if (parent->child)
1081             menu_frame_hide(parent->child);
1082         parent->child = self;
1083         parent->child_entry = parent_entry;
1084     }
1085
1086     if (!menu_frame_show(self)) {
1087         parent->child = NULL;
1088         parent->child_entry = NULL;
1089         return FALSE;
1090     }
1091
1092     menu_frame_place_submenu(self, &x, &y);
1093     menu_frame_move_on_screen(self, x, y, &dx, &dy);
1094
1095     if (dx != 0) {
1096         /*try the other side */
1097         self->direction_right = !self->direction_right;
1098         menu_frame_place_submenu(self, &x, &y);
1099         menu_frame_move_on_screen(self, x, y, &dx, &dy);
1100     }
1101     menu_frame_move(self, x + dx, y + dy);
1102
1103     XMapWindow(obt_display, self->window);
1104
1105     if (screen_pointer_pos(&px, &py)) {
1106         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1107         if (e && e->frame == self)
1108             e->ignore_enters++;
1109     }
1110
1111     return TRUE;
1112 }
1113
1114 static void menu_frame_hide(ObMenuFrame *self)
1115 {
1116     ObMenu *const menu = self->menu;
1117     GList *it = g_list_find(menu_frame_visible, self);
1118     gulong ignore_start;
1119
1120     if (!it)
1121         return;
1122
1123     if (menu->hide_func)
1124         menu->hide_func(self, menu->data);
1125
1126     if (self->child)
1127         menu_frame_hide(self->child);
1128
1129     if (self->parent) {
1130         remove_submenu_hide_timeout(self);
1131
1132         self->parent->child = NULL;
1133         self->parent->child_entry = NULL;
1134     }
1135     self->parent = NULL;
1136     self->parent_entry = NULL;
1137
1138     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1139
1140     if (menu_frame_visible == NULL) {
1141         /* last menu shown */
1142         ungrab_pointer();
1143         ungrab_keyboard();
1144     }
1145
1146     ignore_start = event_start_ignore_all_enters();
1147     XUnmapWindow(obt_display, self->window);
1148     event_end_ignore_all_enters(ignore_start);
1149
1150     menu_frame_free(self);
1151
1152     if (menu->cleanup_func)
1153         menu->cleanup_func(menu, menu->data);
1154 }
1155
1156 void menu_frame_hide_all(void)
1157 {
1158     GList *it;
1159
1160     if (config_submenu_show_delay) {
1161         /* remove any submenu open requests */
1162         if (submenu_show_timer) g_source_remove(submenu_show_timer);
1163         submenu_show_timer = 0;
1164     }
1165     if ((it = g_list_last(menu_frame_visible)))
1166         menu_frame_hide(it->data);
1167 }
1168
1169 ObMenuFrame* menu_frame_under(gint x, gint y)
1170 {
1171     ObMenuFrame *ret = NULL;
1172     GList *it;
1173
1174     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1175         ObMenuFrame *f = it->data;
1176
1177         if (RECT_CONTAINS(f->area, x, y)) {
1178             ret = f;
1179             break;
1180         }
1181     }
1182     return ret;
1183 }
1184
1185 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1186 {
1187     ObMenuFrame *frame;
1188     ObMenuEntryFrame *ret = NULL;
1189     GList *it;
1190
1191     if ((frame = menu_frame_under(x, y))) {
1192         x -= ob_rr_theme->mbwidth + frame->area.x;
1193         y -= ob_rr_theme->mbwidth + frame->area.y;
1194
1195         for (it = frame->entries; it; it = g_list_next(it)) {
1196             ObMenuEntryFrame *e = it->data;
1197
1198             if (RECT_CONTAINS(e->area, x, y)) {
1199                 ret = e;
1200                 break;
1201             }
1202         }
1203     }
1204     return ret;
1205 }
1206
1207 static gboolean submenu_show_timeout(gpointer data)
1208 {
1209     g_assert(menu_frame_visible);
1210     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1211     return FALSE;
1212 }
1213
1214 static gboolean submenu_hide_timeout(gpointer data)
1215 {
1216     g_assert(menu_frame_visible);
1217     menu_frame_hide((ObMenuFrame*)data);
1218     return FALSE;
1219 }
1220
1221 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1222                        gboolean immediate)
1223 {
1224     ObMenuEntryFrame *old = self->selected;
1225     ObMenuFrame *oldchild = self->child;
1226     ObMenuEntryFrame *oldchild_entry = self->child_entry;
1227
1228     /* if the user selected a separator, ignore it and reselect what we had
1229        selected before */
1230     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1231         entry = old;
1232
1233     if (old == entry &&
1234         (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1235         return;
1236
1237     /* if the user left this menu but we have a submenu open, move the
1238        selection back to that submenu */
1239     if (!entry && oldchild_entry)
1240         entry = oldchild_entry;
1241
1242     if (config_submenu_show_delay) {
1243         /* remove any submenu open requests */
1244         if (submenu_show_timer) g_source_remove(submenu_show_timer);
1245         submenu_show_timer = 0;
1246     }
1247
1248     self->selected = entry;
1249
1250     if (old)
1251         menu_entry_frame_render(old);
1252
1253     if (oldchild_entry) {
1254         /* There is an open submenu */
1255         if (oldchild_entry == self->selected) {
1256             /* The open submenu has been reselected, so stop hiding the
1257                submenu */
1258             remove_submenu_hide_timeout(oldchild);
1259         }
1260         else if (oldchild_entry == old) {
1261             /* The open submenu was selected and is no longer, so hide the
1262                submenu */
1263             if (immediate || config_submenu_hide_delay == 0)
1264                 menu_frame_hide(oldchild);
1265             else if (config_submenu_hide_delay > 0) {
1266                 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1267                 submenu_hide_timer =
1268                     g_timeout_add_full(G_PRIORITY_DEFAULT,
1269                                        config_submenu_hide_delay,
1270                                        submenu_hide_timeout, oldchild, NULL);
1271             }
1272         }
1273     }
1274
1275     if (self->selected) {
1276         menu_entry_frame_render(self->selected);
1277
1278         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1279             /* only show if the submenu isn't already showing */
1280             if (oldchild_entry != self->selected) {
1281                 if (immediate || config_submenu_show_delay == 0)
1282                     menu_entry_frame_show_submenu(self->selected);
1283                 else if (config_submenu_show_delay > 0) {
1284                     if (submenu_show_timer)
1285                         g_source_remove(submenu_show_timer);
1286                     submenu_show_timer =
1287                         g_timeout_add_full(G_PRIORITY_DEFAULT,
1288                                            config_submenu_show_delay,
1289                                            submenu_show_timeout,
1290                                            self->selected, NULL);
1291                 }
1292             }
1293             /* hide the grandchildren of this menu. and move the cursor to
1294                the current menu */
1295             else if (immediate && self->child && self->child->child) {
1296                 menu_frame_hide(self->child->child);
1297                 menu_frame_select(self->child, NULL, TRUE);
1298             }
1299         }
1300     }
1301 }
1302
1303 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1304 {
1305     ObMenuFrame *f;
1306
1307     if (!self->entry->data.submenu.submenu) return;
1308
1309     f = menu_frame_new(self->entry->data.submenu.submenu,
1310                        self->entry->data.submenu.show_from,
1311                        self->frame->client);
1312     /* pass our direction on to our child */
1313     f->direction_right = self->frame->direction_right;
1314
1315     if (!menu_frame_show_submenu(f, self->frame, self))
1316         menu_frame_free(f);
1317 }
1318
1319 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1320 {
1321     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1322         self->entry->data.normal.enabled)
1323     {
1324         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1325            gets freed */
1326         ObMenuEntry *entry = self->entry;
1327         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1328         gpointer data = self->frame->menu->data;
1329         GSList *acts = self->entry->data.normal.actions;
1330         ObClient *client = self->frame->client;
1331         ObMenuFrame *frame = self->frame;
1332         guint mods = obt_keyboard_only_modmasks(state);
1333
1334         /* release grabs before executing the shit */
1335         if (!(mods & ControlMask)) {
1336             event_cancel_all_key_grabs();
1337             frame = NULL;
1338         }
1339
1340         if (func)
1341             func(entry, frame, client, state, data);
1342         else
1343             actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1344                              state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1345     }
1346 }
1347
1348 void menu_frame_select_previous(ObMenuFrame *self)
1349 {
1350     GList *it = NULL, *start;
1351
1352     if (self->entries) {
1353         start = it = g_list_find(self->entries, self->selected);
1354         while (TRUE) {
1355             ObMenuEntryFrame *e;
1356
1357             it = it ? g_list_previous(it) : g_list_last(self->entries);
1358             if (it == start)
1359                 break;
1360
1361             if (it) {
1362                 e = it->data;
1363                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1364                     break;
1365                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1366                     break;
1367             }
1368         }
1369     }
1370     menu_frame_select(self, it ? it->data : NULL, FALSE);
1371 }
1372
1373 void menu_frame_select_next(ObMenuFrame *self)
1374 {
1375     GList *it = NULL, *start;
1376
1377     if (self->entries) {
1378         start = it = g_list_find(self->entries, self->selected);
1379         while (TRUE) {
1380             ObMenuEntryFrame *e;
1381
1382             it = it ? g_list_next(it) : self->entries;
1383             if (it == start)
1384                 break;
1385
1386             if (it) {
1387                 e = it->data;
1388                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1389                     break;
1390                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1391                     break;
1392             }
1393         }
1394     }
1395     menu_frame_select(self, it ? it->data : NULL, FALSE);
1396 }
1397
1398 void menu_frame_select_first(ObMenuFrame *self)
1399 {
1400     GList *it = NULL;
1401
1402     if (self->entries) {
1403         for (it = self->entries; it; it = g_list_next(it)) {
1404             ObMenuEntryFrame *e = it->data;
1405             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1406                 break;
1407             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1408                 break;
1409         }
1410     }
1411     menu_frame_select(self, it ? it->data : NULL, FALSE);
1412 }
1413
1414 void menu_frame_select_last(ObMenuFrame *self)
1415 {
1416     GList *it = NULL;
1417
1418     if (self->entries) {
1419         for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1420             ObMenuEntryFrame *e = it->data;
1421             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1422                 break;
1423             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1424                 break;
1425         }
1426     }
1427     menu_frame_select(self, it ? it->data : NULL, FALSE);
1428 }