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