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