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