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