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