]> icculus.org git repositories - dana/openbox.git/blob - openbox/menuframe.c
read the per-app position x/y values from the right xml node
[dana/openbox.git] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    menuframe.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "menuframe.h"
21 #include "client.h"
22 #include "menu.h"
23 #include "screen.h"
24 #include "action.h"
25 #include "action_list.h"
26 #include "action_list_run.h"
27 #include "event.h"
28 #include "grab.h"
29 #include "openbox.h"
30 #include "config.h"
31 #include "obt/prop.h"
32 #include "obt/keyboard.h"
33 #include "obrender/theme.h"
34
35 #define PADDING 2
36 #define MAX_MENU_WIDTH 400
37
38 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
39
40 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
41                          LeaveWindowMask)
42 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
43                          ButtonPressMask | ButtonReleaseMask | \
44                          PointerMotionMask)
45
46 GList *menu_frame_visible;
47 GHashTable *menu_frame_map;
48
49 static RrAppearance *a_sep;
50 static guint submenu_show_timer = 0;
51 static guint submenu_hide_timer = 0;
52
53 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
54                                               ObMenuFrame *frame);
55 static void menu_entry_frame_free(ObMenuEntryFrame *self);
56 static void menu_frame_update(ObMenuFrame *self);
57 static gboolean submenu_show_timeout(gpointer data);
58 static void menu_frame_hide(ObMenuFrame *self);
59
60 static gboolean submenu_hide_timeout(gpointer data);
61
62 static Window createWindow(Window parent, gulong mask,
63                            XSetWindowAttributes *attrib)
64 {
65     return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
66                          RrDepth(ob_rr_inst), InputOutput,
67                          RrVisual(ob_rr_inst), mask, attrib);
68 }
69
70 static void client_dest(ObClient *client, gpointer data)
71 {
72     GList *it;
73
74     /* menus can be associated with a client, so null those refs since
75        we are disappearing now */
76     for (it = menu_frame_visible; it; it = g_list_next(it)) {
77         ObMenuFrame *f = it->data;
78         if (f->client == client)
79             f->client = NULL;
80     }
81 }
82
83 void menu_frame_startup(gboolean reconfig)
84 {
85     gint i;
86
87     a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
88     RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
89     for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
90         a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
91         a_sep->texture[i].data.lineart.color =
92             ob_rr_theme->menu_sep_color;
93     }
94
95     if (reconfig) return;
96
97     client_add_destroy_notify(client_dest, NULL);
98     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
99 }
100
101 void menu_frame_shutdown(gboolean reconfig)
102 {
103     RrAppearanceFree(a_sep);
104
105     if (reconfig) return;
106
107     client_remove_destroy_notify(client_dest);
108     g_hash_table_destroy(menu_frame_map);
109 }
110
111 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
112 {
113     ObMenuFrame *self;
114     XSetWindowAttributes attr;
115
116     self = g_slice_new0(ObMenuFrame);
117     self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
118     self->menu = menu;
119     self->selected = NULL;
120     self->client = client;
121     self->direction_right = TRUE;
122     self->show_from = show_from;
123
124     attr.event_mask = FRAME_EVENTMASK;
125     self->window = createWindow(obt_root(ob_screen),
126                                 CWEventMask, &attr);
127
128     /* make it a popup menu type window */
129     OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
130                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
131
132     XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
133     XSetWindowBorder(obt_display, self->window,
134                      RrColorPixel(ob_rr_theme->menu_border_color));
135
136     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
137
138     window_add(&self->window, MENUFRAME_AS_WINDOW(self));
139     stacking_add(MENUFRAME_AS_WINDOW(self));
140
141     return self;
142 }
143
144 void menu_frame_free(ObMenuFrame *self)
145 {
146     if (self) {
147         while (self->entries) {
148             menu_entry_frame_free(self->entries->data);
149             self->entries = g_list_delete_link(self->entries, self->entries);
150         }
151
152         stacking_remove(MENUFRAME_AS_WINDOW(self));
153         window_remove(self->window);
154
155         RrAppearanceFree(self->a_items);
156
157         XDestroyWindow(obt_display, self->window);
158
159         g_slice_free(ObMenuFrame, self);
160     }
161 }
162
163 ObtIC* menu_frame_ic(ObMenuFrame *self)
164 {
165     /* menus are always used through a grab right now, so they can always use
166        the grab input context */
167     return grab_input_context();
168 }
169
170 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
171                                               ObMenuFrame *frame)
172 {
173     ObMenuEntryFrame *self;
174     XSetWindowAttributes attr;
175
176     self = g_slice_new0(ObMenuEntryFrame);
177     self->entry = entry;
178     self->frame = frame;
179
180     menu_entry_ref(entry);
181
182     attr.event_mask = ENTRY_EVENTMASK;
183     self->window = createWindow(self->frame->window, CWEventMask, &attr);
184     self->text = createWindow(self->window, 0, NULL);
185     g_hash_table_insert(menu_frame_map, &self->window, self);
186     g_hash_table_insert(menu_frame_map, &self->text, self);
187     if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
188         (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
189         self->icon = createWindow(self->window, 0, NULL);
190         g_hash_table_insert(menu_frame_map, &self->icon, self);
191     }
192     if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
193         self->bullet = createWindow(self->window, 0, NULL);
194         g_hash_table_insert(menu_frame_map, &self->bullet, self);
195     }
196
197     XMapWindow(obt_display, self->window);
198     XMapWindow(obt_display, self->text);
199
200     window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
201
202     return self;
203 }
204
205 static void menu_entry_frame_free(ObMenuEntryFrame *self)
206 {
207     if (self) {
208         menu_entry_unref(self->entry);
209
210         window_remove(self->window);
211
212         XDestroyWindow(obt_display, self->text);
213         XDestroyWindow(obt_display, self->window);
214         g_hash_table_remove(menu_frame_map, &self->text);
215         g_hash_table_remove(menu_frame_map, &self->window);
216         if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
217             (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
218             XDestroyWindow(obt_display, self->icon);
219             g_hash_table_remove(menu_frame_map, &self->icon);
220         }
221         if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
222             XDestroyWindow(obt_display, self->bullet);
223             g_hash_table_remove(menu_frame_map, &self->bullet);
224         }
225
226         g_slice_free(ObMenuEntryFrame, self);
227     }
228 }
229
230 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
231 {
232     RECT_SET_POINT(self->area, x, y);
233     self->monitor = screen_find_monitor_point(x, y);
234     XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
235 }
236
237 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
238 {
239     gint dx, dy;
240
241     if (config_menu_middle) {
242         gint myx;
243
244         myx = *x;
245         *y -= self->area.height / 2;
246
247         /* try to the right of the cursor */
248         menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
249         self->direction_right = TRUE;
250         if (dx != 0) {
251             /* try to the left of the cursor */
252             myx = *x - self->area.width;
253             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
254             self->direction_right = FALSE;
255         }
256         if (dx != 0) {
257             /* if didnt fit on either side so just use what it says */
258             myx = *x;
259             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
260             self->direction_right = TRUE;
261         }
262         *x = myx + dx;
263         *y += dy;
264     } else {
265         gint myx, myy;
266
267         myx = *x;
268         myy = *y;
269
270         /* try to the bottom right of the cursor */
271         menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
272         self->direction_right = TRUE;
273         if (dx != 0 || dy != 0) {
274             /* try to the bottom left of the cursor */
275             myx = *x - self->area.width;
276             myy = *y;
277             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
278             self->direction_right = FALSE;
279         }
280         if (dx != 0 || dy != 0) {
281             /* try to the top right of the cursor */
282             myx = *x;
283             myy = *y - self->area.height;
284             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
285             self->direction_right = TRUE;
286         }
287         if (dx != 0 || dy != 0) {
288             /* try to the top left of the cursor */
289             myx = *x - self->area.width;
290             myy = *y - self->area.height;
291             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
292             self->direction_right = FALSE;
293         }
294         if (dx != 0 || dy != 0) {
295             /* if didnt fit on either side so just use what it says */
296             myx = *x;
297             myy = *y;
298             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
299             self->direction_right = TRUE;
300         }
301         *x = myx + dx;
302         *y = myy + dy;
303     }
304 }
305
306 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
307 {
308     gint overlapx, overlapy;
309     gint bwidth;
310
311     overlapx = ob_rr_theme->menu_overlap_x;
312     overlapy = ob_rr_theme->menu_overlap_y;
313     bwidth = ob_rr_theme->mbwidth;
314
315     if (self->direction_right)
316         *x = self->parent->area.x + self->parent->area.width -
317             overlapx - bwidth;
318     else
319         *x = self->parent->area.x - self->area.width + overlapx + bwidth;
320
321     *y = self->parent->area.y + self->parent_entry->area.y;
322     if (config_menu_middle)
323         *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
324     else
325         *y += overlapy;
326 }
327
328 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
329                                gint *dx, gint *dy)
330 {
331     const Rect *a = NULL;
332     Rect search = self->area;
333     gint pos, half, monitor;
334
335     *dx = *dy = 0;
336     RECT_SET_POINT(search, x, y);
337
338     if (self->parent)
339         monitor = self->parent->monitor;
340     else
341         monitor = screen_find_monitor(&search);
342
343     a = screen_physical_area_monitor(monitor);
344
345     half = g_list_length(self->entries) / 2;
346     pos = g_list_index(self->entries, self->selected);
347
348     /* if in the bottom half then check this stuff first, will keep the bottom
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     *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
355     *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
356     /* if in the top half then check this stuff last, will keep the top
357        edge of the menu visible */
358     if (pos <= half) {
359         *dx = MAX(*dx, a->x - x);
360         *dy = MAX(*dy, a->y - y);
361     }
362 }
363
364 static void menu_entry_frame_render(ObMenuEntryFrame *self)
365 {
366     RrAppearance *item_a, *text_a;
367     gint th; /* temp */
368     ObMenu *sub;
369     ObMenuFrame *frame = self->frame;
370
371     switch (self->entry->type) {
372     case OB_MENU_ENTRY_TYPE_NORMAL:
373     case OB_MENU_ENTRY_TYPE_SUBMENU:
374         item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
375                   !self->entry->data.normal.enabled ?
376                   /* disabled */
377                   (self == self->frame->selected ?
378                    ob_rr_theme->a_menu_disabled_selected :
379                    ob_rr_theme->a_menu_disabled) :
380                   /* enabled */
381                   (self == self->frame->selected ?
382                    ob_rr_theme->a_menu_selected :
383                    ob_rr_theme->a_menu_normal));
384         th = ITEM_HEIGHT;
385         break;
386     case OB_MENU_ENTRY_TYPE_SEPARATOR:
387         if (self->entry->data.separator.label) {
388             item_a = ob_rr_theme->a_menu_title;
389             th = ob_rr_theme->menu_title_height;
390         } else {
391             item_a = ob_rr_theme->a_menu_normal;
392             th = ob_rr_theme->menu_sep_width +
393                 2*ob_rr_theme->menu_sep_paddingy;
394         }
395         break;
396     default:
397         g_assert_not_reached();
398     }
399
400     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
401     XResizeWindow(obt_display, self->window,
402                   self->area.width, self->area.height);
403     item_a->surface.parent = self->frame->a_items;
404     item_a->surface.parentx = self->area.x;
405     item_a->surface.parenty = self->area.y;
406     RrPaint(item_a, self->window, self->area.width, self->area.height);
407
408     switch (self->entry->type) {
409     case OB_MENU_ENTRY_TYPE_NORMAL:
410         text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
411                   !self->entry->data.normal.enabled ?
412                   /* disabled */
413                   (self == self->frame->selected ?
414                    ob_rr_theme->a_menu_text_disabled_selected :
415                    ob_rr_theme->a_menu_text_disabled) :
416                   /* enabled */
417                   (self == self->frame->selected ?
418                    ob_rr_theme->a_menu_text_selected :
419                    ob_rr_theme->a_menu_text_normal));
420         text_a->texture[0].data.text.string = self->entry->data.normal.label;
421         if (self->entry->data.normal.shortcut &&
422             (self->frame->menu->show_all_shortcuts ||
423              self->entry->data.normal.shortcut_always_show ||
424              self->entry->data.normal.shortcut_position > 0))
425         {
426             text_a->texture[0].data.text.shortcut = TRUE;
427             text_a->texture[0].data.text.shortcut_pos =
428                 self->entry->data.normal.shortcut_position;
429         } else
430             text_a->texture[0].data.text.shortcut = FALSE;
431         break;
432     case OB_MENU_ENTRY_TYPE_SUBMENU:
433         text_a = (self == self->frame->selected ?
434                   ob_rr_theme->a_menu_text_selected :
435                   ob_rr_theme->a_menu_text_normal);
436         sub = self->entry->data.submenu.submenu;
437         text_a->texture[0].data.text.string = sub ? sub->title : "";
438         if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
439                               sub->shortcut_always_show ||
440                               sub->shortcut_position > 0))
441         {
442             text_a->texture[0].data.text.shortcut = TRUE;
443             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
444         } else
445             text_a->texture[0].data.text.shortcut = FALSE;
446         break;
447     case OB_MENU_ENTRY_TYPE_SEPARATOR:
448         if (self->entry->data.separator.label != NULL) {
449             text_a = ob_rr_theme->a_menu_text_title;
450             text_a->texture[0].data.text.string =
451                 self->entry->data.separator.label;
452         }
453         else
454             text_a = ob_rr_theme->a_menu_text_normal;
455         break;
456     default:
457         g_assert_not_reached();
458     }
459
460     switch (self->entry->type) {
461     case OB_MENU_ENTRY_TYPE_NORMAL:
462         XMoveResizeWindow(obt_display, self->text,
463                           self->frame->text_x, PADDING,
464                           self->frame->text_w,
465                           ITEM_HEIGHT - 2*PADDING);
466         text_a->surface.parent = item_a;
467         text_a->surface.parentx = self->frame->text_x;
468         text_a->surface.parenty = PADDING;
469         RrPaint(text_a, self->text, self->frame->text_w,
470                 ITEM_HEIGHT - 2*PADDING);
471         break;
472     case OB_MENU_ENTRY_TYPE_SUBMENU:
473         XMoveResizeWindow(obt_display, self->text,
474                           self->frame->text_x, PADDING,
475                           self->frame->text_w - ITEM_HEIGHT,
476                           ITEM_HEIGHT - 2*PADDING);
477         text_a->surface.parent = item_a;
478         text_a->surface.parentx = self->frame->text_x;
479         text_a->surface.parenty = PADDING;
480         RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
481                 ITEM_HEIGHT - 2*PADDING);
482         break;
483     case OB_MENU_ENTRY_TYPE_SEPARATOR:
484         if (self->entry->data.separator.label != NULL) {
485             /* labeled separator */
486             XMoveResizeWindow(obt_display, self->text,
487                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
488                               self->area.width - 2*ob_rr_theme->paddingx,
489                               ob_rr_theme->menu_title_height -
490                               2*ob_rr_theme->paddingy);
491             text_a->surface.parent = item_a;
492             text_a->surface.parentx = ob_rr_theme->paddingx;
493             text_a->surface.parenty = ob_rr_theme->paddingy;
494             RrPaint(text_a, self->text,
495                     self->area.width - 2*ob_rr_theme->paddingx,
496                     ob_rr_theme->menu_title_height -
497                     2*ob_rr_theme->paddingy);
498         } else {
499             gint i;
500
501             /* unlabeled separator */
502             XMoveResizeWindow(obt_display, self->text, 0, 0,
503                               self->area.width,
504                               ob_rr_theme->menu_sep_width +
505                               2*ob_rr_theme->menu_sep_paddingy);
506
507             a_sep->surface.parent = item_a;
508             a_sep->surface.parentx = 0;
509             a_sep->surface.parenty = 0;
510             for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
511                 a_sep->texture[i].data.lineart.x1 =
512                     ob_rr_theme->menu_sep_paddingx;
513                 a_sep->texture[i].data.lineart.y1 =
514                     ob_rr_theme->menu_sep_paddingy + i;
515                 a_sep->texture[i].data.lineart.x2 =
516                     self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
517                 a_sep->texture[i].data.lineart.y2 =
518                     ob_rr_theme->menu_sep_paddingy + i;
519             }
520
521             RrPaint(a_sep, self->text, self->area.width,
522                     ob_rr_theme->menu_sep_width +
523                     2*ob_rr_theme->menu_sep_paddingy);
524         }
525         break;
526     default:
527         g_assert_not_reached();
528     }
529
530     if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
531          (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
532         self->entry->data.normal.icon)
533     {
534         RrAppearance *clear;
535
536         XMoveResizeWindow(obt_display, self->icon,
537                           PADDING, frame->item_margin.top,
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
543         clear = ob_rr_theme->a_clear_tex;
544         RrAppearanceClearTextures(clear);
545         clear->texture[0].type = RR_TEXTURE_IMAGE;
546         clear->texture[0].data.image.image =
547             self->entry->data.normal.icon;
548         clear->texture[0].data.image.alpha =
549             self->entry->data.normal.icon_alpha;
550         clear->surface.parent = item_a;
551         clear->surface.parentx = PADDING;
552         clear->surface.parenty = frame->item_margin.top;
553         RrPaint(clear, self->icon,
554                 ITEM_HEIGHT - frame->item_margin.top
555                 - frame->item_margin.bottom,
556                 ITEM_HEIGHT - frame->item_margin.top
557                 - frame->item_margin.bottom);
558         XMapWindow(obt_display, self->icon);
559     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
560                self->entry->data.normal.mask)
561     {
562         RrColor *c;
563         RrAppearance *clear;
564
565         XMoveResizeWindow(obt_display, self->icon,
566                           PADDING, frame->item_margin.top,
567                           ITEM_HEIGHT - frame->item_margin.top
568                           - frame->item_margin.bottom,
569                           ITEM_HEIGHT - frame->item_margin.top
570                           - frame->item_margin.bottom);
571
572         clear = ob_rr_theme->a_clear_tex;
573         RrAppearanceClearTextures(clear);
574         clear->texture[0].type = RR_TEXTURE_MASK;
575         clear->texture[0].data.mask.mask =
576             self->entry->data.normal.mask;
577
578         c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
579              !self->entry->data.normal.enabled ?
580              /* disabled */
581              (self == self->frame->selected ?
582               self->entry->data.normal.mask_disabled_selected_color :
583               self->entry->data.normal.mask_disabled_color) :
584              /* enabled */
585              (self == self->frame->selected ?
586               self->entry->data.normal.mask_selected_color :
587               self->entry->data.normal.mask_normal_color));
588         clear->texture[0].data.mask.color = c;
589
590         clear->surface.parent = item_a;
591         clear->surface.parentx = PADDING;
592         clear->surface.parenty = frame->item_margin.top;
593         RrPaint(clear, self->icon,
594                 ITEM_HEIGHT - frame->item_margin.top
595                 - frame->item_margin.bottom,
596                 ITEM_HEIGHT - frame->item_margin.top
597                 - frame->item_margin.bottom);
598         XMapWindow(obt_display, self->icon);
599     } else
600         XUnmapWindow(obt_display, self->icon);
601
602     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
603         RrAppearance *bullet_a;
604         XMoveResizeWindow(obt_display, self->bullet,
605                           self->frame->text_x + self->frame->text_w -
606                           ITEM_HEIGHT + PADDING, PADDING,
607                           ITEM_HEIGHT - 2*PADDING,
608                           ITEM_HEIGHT - 2*PADDING);
609         bullet_a = (self == self->frame->selected ?
610                     ob_rr_theme->a_menu_bullet_selected :
611                     ob_rr_theme->a_menu_bullet_normal);
612         bullet_a->surface.parent = item_a;
613         bullet_a->surface.parentx =
614             self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
615         bullet_a->surface.parenty = PADDING;
616         RrPaint(bullet_a, self->bullet,
617                 ITEM_HEIGHT - 2*PADDING,
618                 ITEM_HEIGHT - 2*PADDING);
619         XMapWindow(obt_display, self->bullet);
620     } else
621         XUnmapWindow(obt_display, self->bullet);
622
623     XFlush(obt_display);
624 }
625
626 /*! this code is taken from the menu_frame_render. if that changes, this won't
627   work.. */
628 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
629                                         gboolean first_entry,
630                                         gboolean last_entry)
631 {
632     ObMenuEntryType t;
633     gint h = 0;
634
635     h += 2*PADDING;
636
637     if (self)
638         t = self->entry->type;
639     else
640         /* this is the More... entry, it's NORMAL type */
641         t = OB_MENU_ENTRY_TYPE_NORMAL;
642
643     switch (t) {
644     case OB_MENU_ENTRY_TYPE_NORMAL:
645     case OB_MENU_ENTRY_TYPE_SUBMENU:
646         h += ob_rr_theme->menu_font_height;
647         break;
648     case OB_MENU_ENTRY_TYPE_SEPARATOR:
649         if (self->entry->data.separator.label != NULL) {
650             h += ob_rr_theme->menu_title_height +
651                 (ob_rr_theme->mbwidth - PADDING) * 2;
652
653             /* if the first entry is a labeled separator, then make its border
654                overlap with the menu's outside border */
655             if (first_entry)
656                 h -= ob_rr_theme->mbwidth;
657             /* if the last entry is a labeled separator, then make its border
658                overlap with the menu's outside border */
659             if (last_entry)
660                 h -= ob_rr_theme->mbwidth;
661         } else {
662             h += ob_rr_theme->menu_sep_width +
663                 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
664         }
665         break;
666     }
667
668     return h;
669 }
670
671 void menu_frame_render(ObMenuFrame *self)
672 {
673     gint w = 0, h = 0;
674     gint tw, th; /* temps */
675     GList *it;
676     gboolean has_icon = FALSE;
677     ObMenu *sub;
678     ObMenuEntryFrame *e;
679
680     /* find text dimensions */
681
682     STRUT_SET(self->item_margin, 0, 0, 0, 0);
683
684     if (self->entries) {
685         gint l, t, r, b;
686
687         e = self->entries->data;
688         ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
689         tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
690         tw += 2*PADDING;
691
692         th = ITEM_HEIGHT;
693
694         RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
695         STRUT_SET(self->item_margin,
696                   MAX(self->item_margin.left, l),
697                   MAX(self->item_margin.top, t),
698                   MAX(self->item_margin.right, r),
699                   MAX(self->item_margin.bottom, b));
700         RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
701         STRUT_SET(self->item_margin,
702                   MAX(self->item_margin.left, l),
703                   MAX(self->item_margin.top, t),
704                   MAX(self->item_margin.right, r),
705                   MAX(self->item_margin.bottom, b));
706         RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
707         STRUT_SET(self->item_margin,
708                   MAX(self->item_margin.left, l),
709                   MAX(self->item_margin.top, t),
710                   MAX(self->item_margin.right, r),
711                   MAX(self->item_margin.bottom, b));
712         RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
713         STRUT_SET(self->item_margin,
714                   MAX(self->item_margin.left, l),
715                   MAX(self->item_margin.top, t),
716                   MAX(self->item_margin.right, r),
717                   MAX(self->item_margin.bottom, b));
718     }
719
720     /* render the entries */
721
722     for (it = self->entries; it; it = g_list_next(it)) {
723         RrAppearance *text_a;
724         e = it->data;
725
726         /* if the first entry is a labeled separator, then make its border
727            overlap with the menu's outside border */
728         if (it == self->entries &&
729             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
730             e->entry->data.separator.label)
731         {
732             h -= ob_rr_theme->mbwidth;
733         }
734
735         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
736             e->entry->data.separator.label)
737         {
738             e->border = ob_rr_theme->mbwidth;
739         }
740
741         RECT_SET_POINT(e->area, 0, h+e->border);
742         XMoveWindow(obt_display, e->window,
743                     e->area.x-e->border, e->area.y-e->border);
744         XSetWindowBorderWidth(obt_display, e->window, e->border);
745         XSetWindowBorder(obt_display, e->window,
746                          RrColorPixel(ob_rr_theme->menu_border_color));
747
748         text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
749                   !e->entry->data.normal.enabled ?
750                   /* disabled */
751                   (e == self->selected ?
752                    ob_rr_theme->a_menu_text_disabled_selected :
753                    ob_rr_theme->a_menu_text_disabled) :
754                   /* enabled */
755                   (e == self->selected ?
756                    ob_rr_theme->a_menu_text_selected : 
757                    ob_rr_theme->a_menu_text_normal));
758         switch (e->entry->type) {
759         case OB_MENU_ENTRY_TYPE_NORMAL:
760             text_a->texture[0].data.text.string = e->entry->data.normal.label;
761             tw = RrMinWidth(text_a);
762             tw = MIN(tw, MAX_MENU_WIDTH);
763             th = ob_rr_theme->menu_font_height;
764
765             if (e->entry->data.normal.icon ||
766                 e->entry->data.normal.mask)
767                 has_icon = TRUE;
768             break;
769         case OB_MENU_ENTRY_TYPE_SUBMENU:
770             sub = e->entry->data.submenu.submenu;
771             text_a->texture[0].data.text.string = sub ? sub->title : "";
772             tw = RrMinWidth(text_a);
773             tw = MIN(tw, MAX_MENU_WIDTH);
774             th = ob_rr_theme->menu_font_height;
775
776             if (e->entry->data.normal.icon ||
777                 e->entry->data.normal.mask)
778                 has_icon = TRUE;
779
780             tw += ITEM_HEIGHT - PADDING;
781             break;
782         case OB_MENU_ENTRY_TYPE_SEPARATOR:
783             if (e->entry->data.separator.label != NULL) {
784                 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
785                     e->entry->data.separator.label;
786                 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
787                     2*ob_rr_theme->paddingx;
788                 tw = MIN(tw, MAX_MENU_WIDTH);
789                 th = ob_rr_theme->menu_title_height +
790                     (ob_rr_theme->mbwidth - PADDING) *2;
791             } else {
792                 tw = 0;
793                 th = ob_rr_theme->menu_sep_width +
794                     2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
795             }
796             break;
797         default:
798             g_assert_not_reached();
799         }
800         tw += 2*PADDING;
801         th += 2*PADDING;
802         w = MAX(w, tw);
803         h += th;
804     }
805
806     /* if the last entry is a labeled separator, then make its border
807        overlap with the menu's outside border */
808     it = g_list_last(self->entries);
809     e = it ? it->data : NULL;
810     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
811         e->entry->data.separator.label)
812     {
813         h -= ob_rr_theme->mbwidth;
814     }
815
816     self->text_x = PADDING;
817     self->text_w = w;
818
819     if (self->entries) {
820         if (has_icon) {
821             w += ITEM_HEIGHT + PADDING;
822             self->text_x += ITEM_HEIGHT + PADDING;
823         }
824     }
825
826     if (!w) w = 10;
827     if (!h) h = 3;
828
829     XResizeWindow(obt_display, self->window, w, h);
830
831     self->inner_w = w;
832
833     RrPaint(self->a_items, self->window, w, h);
834
835     for (it = self->entries; it; it = g_list_next(it))
836         menu_entry_frame_render(it->data);
837
838     w += ob_rr_theme->mbwidth * 2;
839     h += ob_rr_theme->mbwidth * 2;
840
841     RECT_SET_SIZE(self->area, w, h);
842
843     XFlush(obt_display);
844 }
845
846 static void menu_frame_update(ObMenuFrame *self)
847 {
848     GList *mit, *fit;
849     const Rect *a;
850     gint h;
851
852     menu_pipe_execute(self->menu);
853     menu_find_submenus(self->menu);
854
855     self->selected = NULL;
856
857     /* start at show_from */
858     mit = g_list_nth(self->menu->entries, self->show_from);
859
860     /* go through the menu's and frame's entries and connect the frame entries
861        to the menu entries */
862     for (fit = self->entries; mit && fit;
863          mit = g_list_next(mit), fit = g_list_next(fit))
864     {
865         ObMenuEntryFrame *f = fit->data;
866         f->entry = mit->data;
867     }
868
869     /* if there are more menu entries than in the frame, add them */
870     while (mit) {
871         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
872         self->entries = g_list_append(self->entries, e);
873         mit = g_list_next(mit);
874     }
875
876     /* if there are more frame entries than menu entries then get rid of
877        them */
878     while (fit) {
879         GList *n = g_list_next(fit);
880         menu_entry_frame_free(fit->data);
881         self->entries = g_list_delete_link(self->entries, fit);
882         fit = n;
883     }
884
885     /* * make the menu fit on the screen */
886
887     /* calculate the height of the menu */
888     h = 0;
889     for (fit = self->entries; fit; fit = g_list_next(fit))
890         h += menu_entry_frame_get_height(fit->data,
891                                          fit == self->entries,
892                                          g_list_next(fit) == NULL);
893     /* add the border at the top and bottom */
894     h += ob_rr_theme->mbwidth * 2;
895
896     a = screen_physical_area_monitor(self->monitor);
897
898     if (h > a->height) {
899         GList *flast, *tmp;
900         gboolean last_entry = TRUE;
901
902         /* take the height of our More... entry into account */
903         h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
904
905         /* start at the end of the entries */
906         flast = g_list_last(self->entries);
907
908         /* pull out all the entries from the frame that don't
909            fit on the screen, leaving at least 1 though */
910         while (h > a->height && g_list_previous(flast) != NULL) {
911             /* update the height, without this entry */
912             h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
913
914             /* destroy the entry we're not displaying */
915             tmp = flast;
916             flast = g_list_previous(flast);
917             menu_entry_frame_free(tmp->data);
918             self->entries = g_list_delete_link(self->entries, tmp);
919
920             /* only the first one that we see is the last entry in the menu */
921             last_entry = FALSE;
922         };
923
924         {
925             ObMenuEntry *more_entry;
926             ObMenuEntryFrame *more_frame;
927             /* make the More... menu entry frame which will display in this
928                frame.
929                if self->menu->more_menu is NULL that means that this is already
930                More... menu, so just use ourself.
931             */
932             more_entry = menu_get_more((self->menu->more_menu ?
933                                         self->menu->more_menu :
934                                         self->menu),
935                                        /* continue where we left off */
936                                        self->show_from +
937                                        g_list_length(self->entries));
938             more_frame = menu_entry_frame_new(more_entry, self);
939             /* make it get deleted when the menu frame goes away */
940             menu_entry_unref(more_entry);
941
942             /* add our More... entry to the frame */
943             self->entries = g_list_append(self->entries, more_frame);
944         }
945     }
946
947     menu_frame_render(self);
948 }
949
950 static gboolean menu_frame_is_visible(ObMenuFrame *self)
951 {
952     return !!(g_list_find(menu_frame_visible, self));
953 }
954
955 static gboolean menu_frame_show(ObMenuFrame *self)
956 {
957     GList *it;
958
959     /* determine if the underlying menu is already visible */
960     for (it = menu_frame_visible; it; it = g_list_next(it)) {
961         ObMenuFrame *f = it->data;
962         if (f->menu == self->menu)
963             break;
964     }
965     if (!it) {
966         if (self->menu->update_func)
967             if (!self->menu->update_func(self, self->menu->data))
968                 return FALSE;
969     }
970
971     if (menu_frame_visible == NULL) {
972         /* no menus shown yet */
973
974         /* grab the pointer in such a way as to pass through "owner events"
975            so that we can get enter/leave notifies in the menu. */
976         if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
977             return FALSE;
978         if (!grab_keyboard()) {
979             ungrab_pointer();
980             return FALSE;
981         }
982     }
983
984     menu_frame_update(self);
985
986     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
987
988     if (self->menu->show_func)
989         self->menu->show_func(self, self->menu->data);
990
991     return TRUE;
992 }
993
994 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
995                                  gboolean mouse)
996 {
997     gint px, py;
998
999     if (menu_frame_is_visible(self))
1000         return TRUE;
1001     if (!menu_frame_show(self))
1002         return FALSE;
1003
1004     if (self->menu->place_func)
1005         self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1006     else
1007         menu_frame_place_topmenu(self, &x, &y);
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_hide_delay == 0)
1248                     menu_entry_frame_show_submenu(self->selected);
1249                 else if (config_submenu_hide_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         ObActionList *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             action_list_run(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 }