]> icculus.org git repositories - dana/openbox.git/blob - openbox/menuframe.c
Use c_pfocus in prompt_startup
[dana/openbox.git] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    menuframe.c for the Openbox window manager
4    Copyright (c) 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         e = self->entries->data;
658         ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
659         tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
660         tw += 2*PADDING;
661
662         th = ITEM_HEIGHT;
663
664         RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
665         STRUT_SET(self->item_margin,
666                   MAX(self->item_margin.left, l),
667                   MAX(self->item_margin.top, t),
668                   MAX(self->item_margin.right, r),
669                   MAX(self->item_margin.bottom, b));
670         RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
671         STRUT_SET(self->item_margin,
672                   MAX(self->item_margin.left, l),
673                   MAX(self->item_margin.top, t),
674                   MAX(self->item_margin.right, r),
675                   MAX(self->item_margin.bottom, b));
676         RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
677         STRUT_SET(self->item_margin,
678                   MAX(self->item_margin.left, l),
679                   MAX(self->item_margin.top, t),
680                   MAX(self->item_margin.right, r),
681                   MAX(self->item_margin.bottom, b));
682         RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
683         STRUT_SET(self->item_margin,
684                   MAX(self->item_margin.left, l),
685                   MAX(self->item_margin.top, t),
686                   MAX(self->item_margin.right, r),
687                   MAX(self->item_margin.bottom, b));
688     }
689
690     /* render the entries */
691
692     for (it = self->entries; it; it = g_list_next(it)) {
693         RrAppearance *text_a;
694         e = it->data;
695
696         /* if the first entry is a labeled separator, then make its border
697            overlap with the menu's outside border */
698         if (it == self->entries &&
699             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
700             e->entry->data.separator.label)
701         {
702             h -= ob_rr_theme->mbwidth;
703         }
704
705         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
706             e->entry->data.separator.label)
707         {
708             e->border = ob_rr_theme->mbwidth;
709         }
710
711         RECT_SET_POINT(e->area, 0, h+e->border);
712         XMoveWindow(ob_display, e->window,
713                     e->area.x-e->border, e->area.y-e->border);
714         XSetWindowBorderWidth(ob_display, e->window, e->border);
715         XSetWindowBorder(ob_display, e->window,
716                          RrColorPixel(ob_rr_theme->menu_border_color));
717
718         text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
719                   !e->entry->data.normal.enabled ?
720                   /* disabled */
721                   (e == self->selected ?
722                    ob_rr_theme->a_menu_text_disabled_selected :
723                    ob_rr_theme->a_menu_text_disabled) :
724                   /* enabled */
725                   (e == self->selected ?
726                    ob_rr_theme->a_menu_text_selected : 
727                    ob_rr_theme->a_menu_text_normal));
728         switch (e->entry->type) {
729         case OB_MENU_ENTRY_TYPE_NORMAL:
730             text_a->texture[0].data.text.string = e->entry->data.normal.label;
731             tw = RrMinWidth(text_a);
732             tw = MIN(tw, MAX_MENU_WIDTH);
733             th = ob_rr_theme->menu_font_height;
734
735             if (e->entry->data.normal.icon ||
736                 e->entry->data.normal.mask)
737                 has_icon = TRUE;
738             break;
739         case OB_MENU_ENTRY_TYPE_SUBMENU:
740             sub = e->entry->data.submenu.submenu;
741             text_a->texture[0].data.text.string = sub ? sub->title : "";
742             tw = RrMinWidth(text_a);
743             tw = MIN(tw, MAX_MENU_WIDTH);
744             th = ob_rr_theme->menu_font_height;
745
746             if (e->entry->data.normal.icon ||
747                 e->entry->data.normal.mask)
748                 has_icon = TRUE;
749
750             tw += ITEM_HEIGHT - PADDING;
751             break;
752         case OB_MENU_ENTRY_TYPE_SEPARATOR:
753             if (e->entry->data.separator.label != NULL) {
754                 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
755                     e->entry->data.separator.label;
756                 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
757                     2*ob_rr_theme->paddingx;
758                 tw = MIN(tw, MAX_MENU_WIDTH);
759                 th = ob_rr_theme->menu_title_height +
760                     (ob_rr_theme->mbwidth - PADDING) *2;
761             } else {
762                 tw = 0;
763                 th = ob_rr_theme->menu_sep_width +
764                     2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
765             }
766             break;
767         }
768         tw += 2*PADDING;
769         th += 2*PADDING;
770         w = MAX(w, tw);
771         h += th;
772     }
773
774     /* if the last entry is a labeled separator, then make its border
775        overlap with the menu's outside border */
776     it = g_list_last(self->entries);
777     e = it ? it->data : NULL;
778     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
779         e->entry->data.separator.label)
780     {
781         h -= ob_rr_theme->mbwidth;
782     }
783
784     self->text_x = PADDING;
785     self->text_w = w;
786
787     if (self->entries) {
788         if (has_icon) {
789             w += ITEM_HEIGHT + PADDING;
790             self->text_x += ITEM_HEIGHT + PADDING;
791         }
792     }
793
794     if (!w) w = 10;
795     if (!h) h = 3;
796
797     XResizeWindow(ob_display, self->window, w, h);
798
799     self->inner_w = w;
800
801     RrPaint(self->a_items, self->window, w, h);
802
803     for (it = self->entries; it; it = g_list_next(it))
804         menu_entry_frame_render(it->data);
805
806     w += ob_rr_theme->mbwidth * 2;
807     h += ob_rr_theme->mbwidth * 2;
808
809     RECT_SET_SIZE(self->area, w, h);
810
811     XFlush(ob_display);
812 }
813
814 static void menu_frame_update(ObMenuFrame *self)
815 {
816     GList *mit, *fit;
817     Rect *a;
818     gint h;
819
820     menu_pipe_execute(self->menu);
821     menu_find_submenus(self->menu);
822
823     self->selected = NULL;
824
825     /* start at show_from */
826     mit = g_list_nth(self->menu->entries, self->show_from);
827
828     /* go through the menu's and frame's entries and connect the frame entries
829        to the menu entries */
830     for (fit = self->entries; mit && fit;
831          mit = g_list_next(mit), fit = g_list_next(fit))
832     {
833         ObMenuEntryFrame *f = fit->data;
834         f->entry = mit->data;
835     }
836
837     /* if there are more menu entries than in the frame, add them */
838     while (mit) {
839         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
840         self->entries = g_list_append(self->entries, e);
841         mit = g_list_next(mit);
842     }
843
844     /* if there are more frame entries than menu entries then get rid of
845        them */
846     while (fit) {
847         GList *n = g_list_next(fit);
848         menu_entry_frame_free(fit->data);
849         self->entries = g_list_delete_link(self->entries, fit);
850         fit = n;
851     }
852
853     /* * make the menu fit on the screen */
854
855     /* calculate the height of the menu */
856     h = 0;
857     for (fit = self->entries; fit; fit = g_list_next(fit))
858         h += menu_entry_frame_get_height(fit->data,
859                                          fit == self->entries,
860                                          g_list_next(fit) == NULL);
861     /* add the border at the top and bottom */
862     h += ob_rr_theme->mbwidth * 2;
863
864     a = screen_physical_area_monitor(self->monitor);
865
866     if (h > a->height) {
867         GList *flast, *tmp;
868         gboolean last_entry = TRUE;
869
870         /* take the height of our More... entry into account */
871         h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
872
873         /* start at the end of the entries */
874         flast = g_list_last(self->entries);
875
876         /* pull out all the entries from the frame that don't
877            fit on the screen, leaving at least 1 though */
878         while (h > a->height && g_list_previous(flast) != NULL) {
879             /* update the height, without this entry */
880             h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
881
882             /* destroy the entry we're not displaying */
883             tmp = flast;
884             flast = g_list_previous(flast);
885             menu_entry_frame_free(tmp->data);
886             self->entries = g_list_delete_link(self->entries, tmp);
887
888             /* only the first one that we see is the last entry in the menu */
889             last_entry = FALSE;
890         };
891
892         {
893             ObMenuEntry *more_entry;
894             ObMenuEntryFrame *more_frame;
895             /* make the More... menu entry frame which will display in this
896                frame.
897                if self->menu->more_menu is NULL that means that this is already
898                More... menu, so just use ourself.
899             */
900             more_entry = menu_get_more((self->menu->more_menu ?
901                                         self->menu->more_menu :
902                                         self->menu),
903                                        /* continue where we left off */
904                                        self->show_from +
905                                        g_list_length(self->entries));
906             more_frame = menu_entry_frame_new(more_entry, self);
907             /* make it get deleted when the menu frame goes away */
908             menu_entry_unref(more_entry);
909
910             /* add our More... entry to the frame */
911             self->entries = g_list_append(self->entries, more_frame);
912         }
913     }
914
915     g_free(a);
916
917     menu_frame_render(self);
918 }
919
920 static gboolean menu_frame_is_visible(ObMenuFrame *self)
921 {
922     return !!(g_list_find(menu_frame_visible, self));
923 }
924
925 static gboolean menu_frame_show(ObMenuFrame *self)
926 {
927     GList *it;
928
929     /* determine if the underlying menu is already visible */
930     for (it = menu_frame_visible; it; it = g_list_next(it)) {
931         ObMenuFrame *f = it->data;
932         if (f->menu == self->menu)
933             break;
934     }
935     if (!it) {
936         if (self->menu->update_func)
937             if (!self->menu->update_func(self, self->menu->data))
938                 return FALSE;
939     }
940
941     if (menu_frame_visible == NULL) {
942         /* no menus shown yet */
943
944         /* grab the pointer in such a way as to pass through "owner events"
945            so that we can get enter/leave notifies in the menu. */
946         if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
947             return FALSE;
948         if (!grab_keyboard()) {
949             ungrab_pointer();
950             return FALSE;
951         }
952     }
953
954     menu_frame_update(self);
955
956     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
957
958     if (self->menu->show_func)
959         self->menu->show_func(self, self->menu->data);
960
961     return TRUE;
962 }
963
964 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
965                                  gboolean mouse)
966 {
967     gint px, py;
968
969     if (menu_frame_is_visible(self))
970         return TRUE;
971     if (!menu_frame_show(self))
972         return FALSE;
973
974     if (self->menu->place_func)
975         self->menu->place_func(self, &x, &y, mouse, self->menu->data);
976     else
977         menu_frame_place_topmenu(self, &x, &y);
978
979     menu_frame_move(self, x, y);
980
981     XMapWindow(ob_display, self->window);
982
983     if (screen_pointer_pos(&px, &py)) {
984         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
985         if (e && e->frame == self)
986             e->ignore_enters++;
987     }
988
989     return TRUE;
990 }
991
992 /*! Stop hiding an open submenu.
993     @child The OnMenuFrame of the submenu to be hidden
994 */
995 static void remove_submenu_hide_timeout(ObMenuFrame *child)
996 {
997     ob_main_loop_timeout_remove_data(ob_main_loop, submenu_hide_timeout,
998                                      child, FALSE);
999 }
1000
1001 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1002                                  ObMenuEntryFrame *parent_entry)
1003 {
1004     gint x, y, dx, dy;
1005     gint px, py;
1006
1007     if (menu_frame_is_visible(self))
1008         return TRUE;
1009
1010     self->monitor = parent->monitor;
1011     self->parent = parent;
1012     self->parent_entry = parent_entry;
1013
1014     /* set up parent's child to be us */
1015     if ((parent->child) != self) {
1016         if (parent->child)
1017             menu_frame_hide(parent->child);
1018         parent->child = self;
1019         parent->child_entry = parent_entry;
1020     }
1021
1022     if (!menu_frame_show(self))
1023         return FALSE;
1024
1025     menu_frame_place_submenu(self, &x, &y);
1026     menu_frame_move_on_screen(self, x, y, &dx, &dy);
1027
1028     if (dx != 0) {
1029         /*try the other side */
1030         self->direction_right = !self->direction_right;
1031         menu_frame_place_submenu(self, &x, &y);
1032         menu_frame_move_on_screen(self, x, y, &dx, &dy);
1033     }
1034     menu_frame_move(self, x + dx, y + dy);
1035
1036     XMapWindow(ob_display, self->window);
1037
1038     if (screen_pointer_pos(&px, &py)) {
1039         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1040         if (e && e->frame == self)
1041             e->ignore_enters++;
1042     }
1043
1044     return TRUE;
1045 }
1046
1047 static void menu_frame_hide(ObMenuFrame *self)
1048 {
1049     GList *it = g_list_find(menu_frame_visible, self);
1050     gulong ignore_start;
1051
1052     if (!it)
1053         return;
1054
1055     if (self->menu->hide_func)
1056         self->menu->hide_func(self, self->menu->data);
1057
1058     if (self->child)
1059         menu_frame_hide(self->child);
1060
1061     if (self->parent) {
1062         remove_submenu_hide_timeout(self);
1063
1064         self->parent->child = NULL;
1065         self->parent->child_entry = NULL;
1066     }
1067     self->parent = NULL;
1068     self->parent_entry = NULL;
1069
1070     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1071
1072     if (menu_frame_visible == NULL) {
1073         /* last menu shown */
1074         ungrab_pointer();
1075         ungrab_keyboard();
1076     }
1077
1078     ignore_start = event_start_ignore_all_enters();
1079     XUnmapWindow(ob_display, self->window);
1080     event_end_ignore_all_enters(ignore_start);
1081
1082     menu_frame_free(self);
1083 }
1084
1085 void menu_frame_hide_all(void)
1086 {
1087     GList *it;
1088
1089     if (config_submenu_show_delay) {
1090         /* remove any submenu open requests */
1091         ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1092     }
1093     if ((it = g_list_last(menu_frame_visible)))
1094         menu_frame_hide(it->data);
1095 }
1096
1097 ObMenuFrame* menu_frame_under(gint x, gint y)
1098 {
1099     ObMenuFrame *ret = NULL;
1100     GList *it;
1101
1102     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1103         ObMenuFrame *f = it->data;
1104
1105         if (RECT_CONTAINS(f->area, x, y)) {
1106             ret = f;
1107             break;
1108         }
1109     }
1110     return ret;
1111 }
1112
1113 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1114 {
1115     ObMenuFrame *frame;
1116     ObMenuEntryFrame *ret = NULL;
1117     GList *it;
1118
1119     if ((frame = menu_frame_under(x, y))) {
1120         x -= ob_rr_theme->mbwidth + frame->area.x;
1121         y -= ob_rr_theme->mbwidth + frame->area.y;
1122
1123         for (it = frame->entries; it; it = g_list_next(it)) {
1124             ObMenuEntryFrame *e = it->data;
1125
1126             if (RECT_CONTAINS(e->area, x, y)) {
1127                 ret = e;
1128                 break;
1129             }
1130         }
1131     }
1132     return ret;
1133 }
1134
1135 static gboolean submenu_show_timeout(gpointer data)
1136 {
1137     g_assert(menu_frame_visible);
1138     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1139     return FALSE;
1140 }
1141
1142 static gboolean submenu_hide_timeout(gpointer data)
1143 {
1144     g_assert(menu_frame_visible);
1145     menu_frame_hide((ObMenuFrame*)data);
1146     return FALSE;
1147 }
1148
1149 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1150                        gboolean immediate)
1151 {
1152     ObMenuEntryFrame *old = self->selected;
1153     ObMenuFrame *oldchild = self->child;
1154     ObMenuEntryFrame *oldchild_entry = self->child_entry;
1155
1156     /* if the user selected a separator, ignore it and reselect what we had
1157        selected before */
1158     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1159         entry = old;
1160
1161     if (old == entry &&
1162         (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1163         return;
1164
1165     /* if the user left this menu but we have a submenu open, move the
1166        selection back to that submenu */
1167     if (!entry && oldchild_entry)
1168         entry = oldchild_entry;
1169
1170     if (config_submenu_show_delay) {
1171         /* remove any submenu open requests */
1172         ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1173     }
1174
1175     self->selected = entry;
1176
1177     if (old)
1178         menu_entry_frame_render(old);
1179
1180     if (oldchild_entry) {
1181         /* There is an open submenu */
1182         if (oldchild_entry == self->selected) {
1183             /* The open submenu has been reselected, so stop hiding the
1184                submenu */
1185             remove_submenu_hide_timeout(oldchild);
1186         }
1187         else if (oldchild_entry == old) {
1188             /* The open submenu was selected and is no longer, so hide the
1189                submenu */
1190             if (immediate || config_submenu_hide_delay == 0)
1191                 menu_frame_hide(oldchild);
1192             else if (config_submenu_hide_delay > 0)
1193                 ob_main_loop_timeout_add(ob_main_loop,
1194                                          config_submenu_hide_delay * 1000,
1195                                          submenu_hide_timeout,
1196                                          oldchild, g_direct_equal,
1197                                          NULL);
1198         }
1199     }
1200
1201     if (self->selected) {
1202         menu_entry_frame_render(self->selected);
1203
1204         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1205             /* only show if the submenu isn't already showing */
1206             if (oldchild_entry != self->selected) {
1207                 if (immediate || config_submenu_hide_delay == 0)
1208                     menu_entry_frame_show_submenu(self->selected);
1209                 else if (config_submenu_hide_delay > 0)
1210                     ob_main_loop_timeout_add(ob_main_loop,
1211                                              config_submenu_show_delay * 1000,
1212                                              submenu_show_timeout,
1213                                              self->selected, g_direct_equal,
1214                                              NULL);
1215             }
1216             /* hide the grandchildren of this menu. and move the cursor to
1217                the current menu */
1218             else if (immediate && self->child && self->child->child) {
1219                 menu_frame_hide(self->child->child);
1220                 menu_frame_select(self->child, NULL, TRUE);
1221             }
1222         }
1223     }
1224 }
1225
1226 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1227 {
1228     ObMenuFrame *f;
1229
1230     if (!self->entry->data.submenu.submenu) return;
1231
1232     f = menu_frame_new(self->entry->data.submenu.submenu,
1233                        self->entry->data.submenu.show_from,
1234                        self->frame->client);
1235     /* pass our direction on to our child */
1236     f->direction_right = self->frame->direction_right;
1237
1238     menu_frame_show_submenu(f, self->frame, self);
1239 }
1240
1241 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1242 {
1243     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1244         self->entry->data.normal.enabled)
1245     {
1246         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1247            gets freed */
1248         ObMenuEntry *entry = self->entry;
1249         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1250         gpointer data = self->frame->menu->data;
1251         GSList *acts = self->entry->data.normal.actions;
1252         ObClient *client = self->frame->client;
1253         ObMenuFrame *frame = self->frame;
1254
1255         /* release grabs before executing the shit */
1256         if (!(state & ControlMask)) {
1257             event_cancel_all_key_grabs();
1258             frame = NULL;
1259         }
1260
1261         if (func)
1262             func(entry, frame, client, state, data);
1263         else
1264             actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1265                              state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1266     }
1267 }
1268
1269 void menu_frame_select_previous(ObMenuFrame *self)
1270 {
1271     GList *it = NULL, *start;
1272
1273     if (self->entries) {
1274         start = it = g_list_find(self->entries, self->selected);
1275         while (TRUE) {
1276             ObMenuEntryFrame *e;
1277
1278             it = it ? g_list_previous(it) : g_list_last(self->entries);
1279             if (it == start)
1280                 break;
1281
1282             if (it) {
1283                 e = it->data;
1284                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1285                     break;
1286                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1287                     break;
1288             }
1289         }
1290     }
1291     menu_frame_select(self, it ? it->data : NULL, FALSE);
1292 }
1293
1294 void menu_frame_select_next(ObMenuFrame *self)
1295 {
1296     GList *it = NULL, *start;
1297
1298     if (self->entries) {
1299         start = it = g_list_find(self->entries, self->selected);
1300         while (TRUE) {
1301             ObMenuEntryFrame *e;
1302
1303             it = it ? g_list_next(it) : self->entries;
1304             if (it == start)
1305                 break;
1306
1307             if (it) {
1308                 e = it->data;
1309                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1310                     break;
1311                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1312                     break;
1313             }
1314         }
1315     }
1316     menu_frame_select(self, it ? it->data : NULL, FALSE);
1317 }
1318
1319 void menu_frame_select_first(ObMenuFrame *self)
1320 {
1321     GList *it = NULL;
1322
1323     if (self->entries) {
1324         for (it = self->entries; it; it = g_list_next(it)) {
1325             ObMenuEntryFrame *e = it->data;
1326             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1327                 break;
1328             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1329                 break;
1330         }
1331     }
1332     menu_frame_select(self, it ? it->data : NULL, FALSE);
1333 }
1334
1335 void menu_frame_select_last(ObMenuFrame *self)
1336 {
1337     GList *it = NULL;
1338
1339     if (self->entries) {
1340         for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1341             ObMenuEntryFrame *e = it->data;
1342             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1343                 break;
1344             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1345                 break;
1346         }
1347     }
1348     menu_frame_select(self, it ? it->data : NULL, FALSE);
1349 }