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