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