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