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