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