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