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