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