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