Fix menu placement to avoid dead xinerama areas, possibly break other stuff
[dana/openbox.git] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    menuframe.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "menuframe.h"
21 #include "client.h"
22 #include "menu.h"
23 #include "screen.h"
24 #include "actions.h"
25 #include "event.h"
26 #include "grab.h"
27 #include "openbox.h"
28 #include "config.h"
29 #include "obt/prop.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
32
33 #define PADDING 2
34 #define MAX_MENU_WIDTH 400
35
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39                          LeaveWindowMask)
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41                          ButtonPressMask | ButtonReleaseMask)
42
43 GList *menu_frame_visible;
44 GHashTable *menu_frame_map;
45
46 static RrAppearance *a_sep;
47 static guint submenu_show_timer = 0;
48 static guint submenu_hide_timer = 0;
49
50 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
51                                               ObMenuFrame *frame);
52 static void menu_entry_frame_free(ObMenuEntryFrame *self);
53 static void menu_frame_update(ObMenuFrame *self);
54 static gboolean submenu_show_timeout(gpointer data);
55 static void menu_frame_hide(ObMenuFrame *self);
56
57 static gboolean submenu_hide_timeout(gpointer data);
58
59 static Window createWindow(Window parent, gulong mask,
60                            XSetWindowAttributes *attrib)
61 {
62     return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
63                          RrDepth(ob_rr_inst), InputOutput,
64                          RrVisual(ob_rr_inst), mask, attrib);
65 }
66
67 static void client_dest(ObClient *client, gpointer data)
68 {
69     GList *it;
70
71     /* menus can be associated with a client, so null those refs since
72        we are disappearing now */
73     for (it = menu_frame_visible; it; it = g_list_next(it)) {
74         ObMenuFrame *f = it->data;
75         if (f->client == client)
76             f->client = NULL;
77     }
78 }
79
80 void menu_frame_startup(gboolean reconfig)
81 {
82     gint i;
83
84     a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
85     RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
86     for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
87         a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
88         a_sep->texture[i].data.lineart.color =
89             ob_rr_theme->menu_sep_color;
90     }
91
92     if (reconfig) return;
93
94     client_add_destroy_notify(client_dest, NULL);
95     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
96 }
97
98 void menu_frame_shutdown(gboolean reconfig)
99 {
100     RrAppearanceFree(a_sep);
101
102     if (reconfig) return;
103
104     client_remove_destroy_notify(client_dest);
105     g_hash_table_destroy(menu_frame_map);
106 }
107
108 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
109 {
110     ObMenuFrame *self;
111     XSetWindowAttributes attr;
112
113     self = g_slice_new0(ObMenuFrame);
114     self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
115     self->menu = menu;
116     self->selected = NULL;
117     self->client = client;
118     self->direction_right = TRUE;
119     self->show_from = show_from;
120
121     attr.event_mask = FRAME_EVENTMASK;
122     self->window = createWindow(obt_root(ob_screen),
123                                 CWEventMask, &attr);
124
125     /* make it a popup menu type window */
126     OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
127                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
128
129     XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
130     XSetWindowBorder(obt_display, self->window,
131                      RrColorPixel(ob_rr_theme->menu_border_color));
132
133     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
134
135     window_add(&self->window, MENUFRAME_AS_WINDOW(self));
136     stacking_add(MENUFRAME_AS_WINDOW(self));
137
138     return self;
139 }
140
141 void menu_frame_free(ObMenuFrame *self)
142 {
143     if (self) {
144         while (self->entries) {
145             menu_entry_frame_free(self->entries->data);
146             self->entries = g_list_delete_link(self->entries, self->entries);
147         }
148
149         stacking_remove(MENUFRAME_AS_WINDOW(self));
150         window_remove(self->window);
151
152         RrAppearanceFree(self->a_items);
153
154         XDestroyWindow(obt_display, self->window);
155
156         g_slice_free(ObMenuFrame, self);
157     }
158 }
159
160 ObtIC* menu_frame_ic(ObMenuFrame *self)
161 {
162     /* menus are always used through a grab right now, so they can always use
163        the grab input context */
164     return grab_input_context();
165 }
166
167 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
168                                               ObMenuFrame *frame)
169 {
170     ObMenuEntryFrame *self;
171     XSetWindowAttributes attr;
172
173     self = g_slice_new0(ObMenuEntryFrame);
174     self->entry = entry;
175     self->frame = frame;
176
177     menu_entry_ref(entry);
178
179     attr.event_mask = ENTRY_EVENTMASK;
180     self->window = createWindow(self->frame->window, CWEventMask, &attr);
181     self->text = createWindow(self->window, 0, NULL);
182     g_hash_table_insert(menu_frame_map, &self->window, self);
183     g_hash_table_insert(menu_frame_map, &self->text, self);
184     if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
185         self->icon = createWindow(self->window, 0, NULL);
186         g_hash_table_insert(menu_frame_map, &self->icon, self);
187     }
188     if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
189         self->bullet = createWindow(self->window, 0, NULL);
190         g_hash_table_insert(menu_frame_map, &self->bullet, self);
191     }
192
193     XMapWindow(obt_display, self->window);
194     XMapWindow(obt_display, self->text);
195
196     window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
197
198     return self;
199 }
200
201 static void menu_entry_frame_free(ObMenuEntryFrame *self)
202 {
203     if (self) {
204         menu_entry_unref(self->entry);
205
206         window_remove(self->window);
207
208         XDestroyWindow(obt_display, self->text);
209         XDestroyWindow(obt_display, self->window);
210         g_hash_table_remove(menu_frame_map, &self->text);
211         g_hash_table_remove(menu_frame_map, &self->window);
212         if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
213             XDestroyWindow(obt_display, self->icon);
214             g_hash_table_remove(menu_frame_map, &self->icon);
215         }
216         if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
217             XDestroyWindow(obt_display, self->bullet);
218             g_hash_table_remove(menu_frame_map, &self->bullet);
219         }
220
221         g_slice_free(ObMenuEntryFrame, self);
222     }
223 }
224
225 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
226 {
227     RECT_SET_POINT(self->area, x, y);
228     self->monitor = screen_find_monitor_point(x, y);
229     XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
230 }
231
232 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
233 {
234     gint dx, dy;
235
236     if (config_menu_middle) {
237         gint myx;
238
239         myx = *x;
240         *y -= self->area.height / 2;
241
242         /* try to the right of the cursor */
243         menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
244         self->direction_right = TRUE;
245         if (dx != 0) {
246             /* try to the left of the cursor */
247             myx = *x - self->area.width;
248             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
249             self->direction_right = FALSE;
250         }
251         if (dx != 0) {
252             /* if didnt fit on either side so just use what it says */
253             myx = *x;
254             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
255             self->direction_right = TRUE;
256         }
257         *x = myx + dx;
258         *y += dy;
259     } else {
260         gint myx, myy;
261
262         myx = *x;
263         myy = *y;
264
265         /* try to the bottom right of the cursor */
266         menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267         self->direction_right = TRUE;
268         if (dx != 0 || dy != 0) {
269             /* try to the bottom left of the cursor */
270             myx = *x - self->area.width;
271             myy = *y;
272             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
273             self->direction_right = FALSE;
274         }
275         if (dx != 0 || dy != 0) {
276             /* try to the top right of the cursor */
277             myx = *x;
278             myy = *y - self->area.height;
279             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
280             self->direction_right = TRUE;
281         }
282         if (dx != 0 || dy != 0) {
283             /* try to the top left of the cursor */
284             myx = *x - self->area.width;
285             myy = *y - self->area.height;
286             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
287             self->direction_right = FALSE;
288         }
289         if (dx != 0 || dy != 0) {
290             /* if didnt fit on either side so just use what it says */
291             myx = *x;
292             myy = *y;
293             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
294             self->direction_right = TRUE;
295         }
296         *x = myx + dx;
297         *y = myy + dy;
298     }
299 }
300
301 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
302 {
303     gint overlapx, overlapy;
304     gint bwidth;
305
306     overlapx = ob_rr_theme->menu_overlap_x;
307     overlapy = ob_rr_theme->menu_overlap_y;
308     bwidth = ob_rr_theme->mbwidth;
309
310     if (self->direction_right)
311         *x = self->parent->area.x + self->parent->area.width -
312             overlapx - bwidth;
313     else
314         *x = self->parent->area.x - self->area.width + overlapx + bwidth;
315
316     *y = self->parent->area.y + self->parent_entry->area.y;
317     if (config_menu_middle)
318         *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
319     else
320         *y += overlapy;
321 }
322
323 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
324                                gint *dx, gint *dy)
325 {
326     const Rect *a = NULL;
327     Rect search = self->area;
328     gint pos, half, monitor;
329
330     *dx = *dy = 0;
331     RECT_SET_POINT(search, x, y);
332
333     if (self->parent)
334         monitor = self->parent->monitor;
335     else
336         monitor = screen_find_monitor(&search);
337
338     a = screen_physical_area_monitor(monitor);
339
340     half = g_list_length(self->entries) / 2;
341     pos = g_list_index(self->entries, self->selected);
342
343     /* if in the bottom half then check this stuff first, will keep the bottom
344        edge of the menu visible */
345     if (pos > half) {
346         *dx = MAX(*dx, a->x - x);
347         *dy = MAX(*dy, a->y - y);
348     }
349     *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
350     *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
351     /* if in the top half then check this stuff last, will keep the top
352        edge of the menu visible */
353     if (pos <= half) {
354         *dx = MAX(*dx, a->x - x);
355         *dy = MAX(*dy, a->y - y);
356     }
357 }
358
359 static void menu_entry_frame_render(ObMenuEntryFrame *self)
360 {
361     RrAppearance *item_a, *text_a;
362     gint th; /* temp */
363     ObMenu *sub;
364     ObMenuFrame *frame = self->frame;
365
366     switch (self->entry->type) {
367     case OB_MENU_ENTRY_TYPE_NORMAL:
368     case OB_MENU_ENTRY_TYPE_SUBMENU:
369         item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
370                   !self->entry->data.normal.enabled ?
371                   /* disabled */
372                   (self == self->frame->selected ?
373                    ob_rr_theme->a_menu_disabled_selected :
374                    ob_rr_theme->a_menu_disabled) :
375                   /* enabled */
376                   (self == self->frame->selected ?
377                    ob_rr_theme->a_menu_selected :
378                    ob_rr_theme->a_menu_normal));
379         th = ITEM_HEIGHT;
380         break;
381     case OB_MENU_ENTRY_TYPE_SEPARATOR:
382         if (self->entry->data.separator.label) {
383             item_a = ob_rr_theme->a_menu_title;
384             th = ob_rr_theme->menu_title_height;
385         } else {
386             item_a = ob_rr_theme->a_menu_normal;
387             th = ob_rr_theme->menu_sep_width +
388                 2*ob_rr_theme->menu_sep_paddingy;
389         }
390         break;
391     default:
392         g_assert_not_reached();
393     }
394
395     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
396     XResizeWindow(obt_display, self->window,
397                   self->area.width, self->area.height);
398     item_a->surface.parent = self->frame->a_items;
399     item_a->surface.parentx = self->area.x;
400     item_a->surface.parenty = self->area.y;
401     RrPaint(item_a, self->window, self->area.width, self->area.height);
402
403     switch (self->entry->type) {
404     case OB_MENU_ENTRY_TYPE_NORMAL:
405         text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
406                   !self->entry->data.normal.enabled ?
407                   /* disabled */
408                   (self == self->frame->selected ?
409                    ob_rr_theme->a_menu_text_disabled_selected :
410                    ob_rr_theme->a_menu_text_disabled) :
411                   /* enabled */
412                   (self == self->frame->selected ?
413                    ob_rr_theme->a_menu_text_selected :
414                    ob_rr_theme->a_menu_text_normal));
415         text_a->texture[0].data.text.string = self->entry->data.normal.label;
416         if (self->entry->data.normal.shortcut &&
417             (self->frame->menu->show_all_shortcuts ||
418              self->entry->data.normal.shortcut_always_show ||
419              self->entry->data.normal.shortcut_position > 0))
420         {
421             text_a->texture[0].data.text.shortcut = TRUE;
422             text_a->texture[0].data.text.shortcut_pos =
423                 self->entry->data.normal.shortcut_position;
424         } else
425             text_a->texture[0].data.text.shortcut = FALSE;
426         break;
427     case OB_MENU_ENTRY_TYPE_SUBMENU:
428         text_a = (self == self->frame->selected ?
429                   ob_rr_theme->a_menu_text_selected :
430                   ob_rr_theme->a_menu_text_normal);
431         sub = self->entry->data.submenu.submenu;
432         text_a->texture[0].data.text.string = sub ? sub->title : "";
433         if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
434                               sub->shortcut_always_show ||
435                               sub->shortcut_position > 0))
436         {
437             text_a->texture[0].data.text.shortcut = TRUE;
438             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
439         } else
440             text_a->texture[0].data.text.shortcut = FALSE;
441         break;
442     case OB_MENU_ENTRY_TYPE_SEPARATOR:
443         if (self->entry->data.separator.label != NULL) {
444             text_a = ob_rr_theme->a_menu_text_title;
445             text_a->texture[0].data.text.string =
446                 self->entry->data.separator.label;
447         }
448         else
449             text_a = ob_rr_theme->a_menu_text_normal;
450         break;
451     default:
452         g_assert_not_reached();
453     }
454
455     switch (self->entry->type) {
456     case OB_MENU_ENTRY_TYPE_NORMAL:
457         XMoveResizeWindow(obt_display, self->text,
458                           self->frame->text_x, PADDING,
459                           self->frame->text_w,
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,
465                 ITEM_HEIGHT - 2*PADDING);
466         break;
467     case OB_MENU_ENTRY_TYPE_SUBMENU:
468         XMoveResizeWindow(obt_display, self->text,
469                           self->frame->text_x, PADDING,
470                           self->frame->text_w - ITEM_HEIGHT,
471                           ITEM_HEIGHT - 2*PADDING);
472         text_a->surface.parent = item_a;
473         text_a->surface.parentx = self->frame->text_x;
474         text_a->surface.parenty = PADDING;
475         RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
476                 ITEM_HEIGHT - 2*PADDING);
477         break;
478     case OB_MENU_ENTRY_TYPE_SEPARATOR:
479         if (self->entry->data.separator.label != NULL) {
480             /* labeled separator */
481             XMoveResizeWindow(obt_display, self->text,
482                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
483                               self->area.width - 2*ob_rr_theme->paddingx,
484                               ob_rr_theme->menu_title_height -
485                               2*ob_rr_theme->paddingy);
486             text_a->surface.parent = item_a;
487             text_a->surface.parentx = ob_rr_theme->paddingx;
488             text_a->surface.parenty = ob_rr_theme->paddingy;
489             RrPaint(text_a, self->text,
490                     self->area.width - 2*ob_rr_theme->paddingx,
491                     ob_rr_theme->menu_title_height -
492                     2*ob_rr_theme->paddingy);
493         } else {
494             gint i;
495
496             /* unlabeled separator */
497             XMoveResizeWindow(obt_display, self->text, 0, 0,
498                               self->area.width,
499                               ob_rr_theme->menu_sep_width +
500                               2*ob_rr_theme->menu_sep_paddingy);
501
502             a_sep->surface.parent = item_a;
503             a_sep->surface.parentx = 0;
504             a_sep->surface.parenty = 0;
505             for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
506                 a_sep->texture[i].data.lineart.x1 =
507                     ob_rr_theme->menu_sep_paddingx;
508                 a_sep->texture[i].data.lineart.y1 =
509                     ob_rr_theme->menu_sep_paddingy + i;
510                 a_sep->texture[i].data.lineart.x2 =
511                     self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
512                 a_sep->texture[i].data.lineart.y2 =
513                     ob_rr_theme->menu_sep_paddingy + i;
514             }
515
516             RrPaint(a_sep, self->text, self->area.width,
517                     ob_rr_theme->menu_sep_width +
518                     2*ob_rr_theme->menu_sep_paddingy);
519         }
520         break;
521     default:
522         g_assert_not_reached();
523     }
524
525     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
526         self->entry->data.normal.icon)
527     {
528         RrAppearance *clear;
529
530         XMoveResizeWindow(obt_display, self->icon,
531                           PADDING, frame->item_margin.top,
532                           ITEM_HEIGHT - frame->item_margin.top
533                           - frame->item_margin.bottom,
534                           ITEM_HEIGHT - frame->item_margin.top
535                           - frame->item_margin.bottom);
536
537         clear = ob_rr_theme->a_clear_tex;
538         RrAppearanceClearTextures(clear);
539         clear->texture[0].type = RR_TEXTURE_IMAGE;
540         clear->texture[0].data.image.image =
541             self->entry->data.normal.icon;
542         clear->texture[0].data.image.alpha =
543             self->entry->data.normal.icon_alpha;
544         clear->surface.parent = item_a;
545         clear->surface.parentx = PADDING;
546         clear->surface.parenty = frame->item_margin.top;
547         RrPaint(clear, self->icon,
548                 ITEM_HEIGHT - frame->item_margin.top
549                 - frame->item_margin.bottom,
550                 ITEM_HEIGHT - frame->item_margin.top
551                 - frame->item_margin.bottom);
552         XMapWindow(obt_display, self->icon);
553     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
554                self->entry->data.normal.mask)
555     {
556         RrColor *c;
557         RrAppearance *clear;
558
559         XMoveResizeWindow(obt_display, self->icon,
560                           PADDING, frame->item_margin.top,
561                           ITEM_HEIGHT - frame->item_margin.top
562                           - frame->item_margin.bottom,
563                           ITEM_HEIGHT - frame->item_margin.top
564                           - frame->item_margin.bottom);
565
566         clear = ob_rr_theme->a_clear_tex;
567         RrAppearanceClearTextures(clear);
568         clear->texture[0].type = RR_TEXTURE_MASK;
569         clear->texture[0].data.mask.mask =
570             self->entry->data.normal.mask;
571
572         c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
573              !self->entry->data.normal.enabled ?
574              /* disabled */
575              (self == self->frame->selected ?
576               self->entry->data.normal.mask_disabled_selected_color :
577               self->entry->data.normal.mask_disabled_color) :
578              /* enabled */
579              (self == self->frame->selected ?
580               self->entry->data.normal.mask_selected_color :
581               self->entry->data.normal.mask_normal_color));
582         clear->texture[0].data.mask.color = c;
583
584         clear->surface.parent = item_a;
585         clear->surface.parentx = PADDING;
586         clear->surface.parenty = frame->item_margin.top;
587         RrPaint(clear, self->icon,
588                 ITEM_HEIGHT - frame->item_margin.top
589                 - frame->item_margin.bottom,
590                 ITEM_HEIGHT - frame->item_margin.top
591                 - frame->item_margin.bottom);
592         XMapWindow(obt_display, self->icon);
593     } else
594         XUnmapWindow(obt_display, self->icon);
595
596     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
597         RrAppearance *bullet_a;
598         XMoveResizeWindow(obt_display, self->bullet,
599                           self->frame->text_x + self->frame->text_w -
600                           ITEM_HEIGHT + PADDING, PADDING,
601                           ITEM_HEIGHT - 2*PADDING,
602                           ITEM_HEIGHT - 2*PADDING);
603         bullet_a = (self == self->frame->selected ?
604                     ob_rr_theme->a_menu_bullet_selected :
605                     ob_rr_theme->a_menu_bullet_normal);
606         bullet_a->surface.parent = item_a;
607         bullet_a->surface.parentx =
608             self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
609         bullet_a->surface.parenty = PADDING;
610         RrPaint(bullet_a, self->bullet,
611                 ITEM_HEIGHT - 2*PADDING,
612                 ITEM_HEIGHT - 2*PADDING);
613         XMapWindow(obt_display, self->bullet);
614     } else
615         XUnmapWindow(obt_display, self->bullet);
616
617     XFlush(obt_display);
618 }
619
620 /*! this code is taken from the menu_frame_render. if that changes, this won't
621   work.. */
622 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
623                                         gboolean first_entry,
624                                         gboolean last_entry)
625 {
626     ObMenuEntryType t;
627     gint h = 0;
628
629     h += 2*PADDING;
630
631     if (self)
632         t = self->entry->type;
633     else
634         /* this is the More... entry, it's NORMAL type */
635         t = OB_MENU_ENTRY_TYPE_NORMAL;
636
637     switch (t) {
638     case OB_MENU_ENTRY_TYPE_NORMAL:
639     case OB_MENU_ENTRY_TYPE_SUBMENU:
640         h += ob_rr_theme->menu_font_height;
641         break;
642     case OB_MENU_ENTRY_TYPE_SEPARATOR:
643         if (self->entry->data.separator.label != NULL) {
644             h += ob_rr_theme->menu_title_height +
645                 (ob_rr_theme->mbwidth - PADDING) * 2;
646
647             /* if the first entry is a labeled separator, then make its border
648                overlap with the menu's outside border */
649             if (first_entry)
650                 h -= ob_rr_theme->mbwidth;
651             /* if the last entry is a labeled separator, then make its border
652                overlap with the menu's outside border */
653             if (last_entry)
654                 h -= ob_rr_theme->mbwidth;
655         } else {
656             h += ob_rr_theme->menu_sep_width +
657                 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
658         }
659         break;
660     }
661
662     return h;
663 }
664
665 void menu_frame_render(ObMenuFrame *self)
666 {
667     gint w = 0, h = 0;
668     gint tw, th; /* temps */
669     GList *it;
670     gboolean has_icon = FALSE;
671     ObMenu *sub;
672     ObMenuEntryFrame *e;
673
674     /* find text dimensions */
675
676     STRUT_SET(self->item_margin, 0, 0, 0, 0);
677
678     if (self->entries) {
679         gint l, t, r, b;
680
681         e = self->entries->data;
682         ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
683         tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
684         tw += 2*PADDING;
685
686         th = ITEM_HEIGHT;
687
688         RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
689         STRUT_SET(self->item_margin,
690                   MAX(self->item_margin.left, l),
691                   MAX(self->item_margin.top, t),
692                   MAX(self->item_margin.right, r),
693                   MAX(self->item_margin.bottom, b));
694         RrMargins(ob_rr_theme->a_menu_selected, &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_disabled, &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_selected, &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     }
713
714     /* render the entries */
715
716     for (it = self->entries; it; it = g_list_next(it)) {
717         RrAppearance *text_a;
718         e = it->data;
719
720         /* if the first entry is a labeled separator, then make its border
721            overlap with the menu's outside border */
722         if (it == self->entries &&
723             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
724             e->entry->data.separator.label)
725         {
726             h -= ob_rr_theme->mbwidth;
727         }
728
729         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
730             e->entry->data.separator.label)
731         {
732             e->border = ob_rr_theme->mbwidth;
733         }
734
735         RECT_SET_POINT(e->area, 0, h+e->border);
736         XMoveWindow(obt_display, e->window,
737                     e->area.x-e->border, e->area.y-e->border);
738         XSetWindowBorderWidth(obt_display, e->window, e->border);
739         XSetWindowBorder(obt_display, e->window,
740                          RrColorPixel(ob_rr_theme->menu_border_color));
741
742         text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
743                   !e->entry->data.normal.enabled ?
744                   /* disabled */
745                   (e == self->selected ?
746                    ob_rr_theme->a_menu_text_disabled_selected :
747                    ob_rr_theme->a_menu_text_disabled) :
748                   /* enabled */
749                   (e == self->selected ?
750                    ob_rr_theme->a_menu_text_selected : 
751                    ob_rr_theme->a_menu_text_normal));
752         switch (e->entry->type) {
753         case OB_MENU_ENTRY_TYPE_NORMAL:
754             text_a->texture[0].data.text.string = e->entry->data.normal.label;
755             tw = RrMinWidth(text_a);
756             tw = MIN(tw, MAX_MENU_WIDTH);
757             th = ob_rr_theme->menu_font_height;
758
759             if (e->entry->data.normal.icon ||
760                 e->entry->data.normal.mask)
761                 has_icon = TRUE;
762             break;
763         case OB_MENU_ENTRY_TYPE_SUBMENU:
764             sub = e->entry->data.submenu.submenu;
765             text_a->texture[0].data.text.string = sub ? sub->title : "";
766             tw = RrMinWidth(text_a);
767             tw = MIN(tw, MAX_MENU_WIDTH);
768             th = ob_rr_theme->menu_font_height;
769
770             if (e->entry->data.normal.icon ||
771                 e->entry->data.normal.mask)
772                 has_icon = TRUE;
773
774             tw += ITEM_HEIGHT - PADDING;
775             break;
776         case OB_MENU_ENTRY_TYPE_SEPARATOR:
777             if (e->entry->data.separator.label != NULL) {
778                 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
779                     e->entry->data.separator.label;
780                 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
781                     2*ob_rr_theme->paddingx;
782                 tw = MIN(tw, MAX_MENU_WIDTH);
783                 th = ob_rr_theme->menu_title_height +
784                     (ob_rr_theme->mbwidth - PADDING) *2;
785             } else {
786                 tw = 0;
787                 th = ob_rr_theme->menu_sep_width +
788                     2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
789             }
790             break;
791         default:
792             g_assert_not_reached();
793         }
794         tw += 2*PADDING;
795         th += 2*PADDING;
796         w = MAX(w, tw);
797         h += th;
798     }
799
800     /* if the last entry is a labeled separator, then make its border
801        overlap with the menu's outside border */
802     it = g_list_last(self->entries);
803     e = it ? it->data : NULL;
804     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
805         e->entry->data.separator.label)
806     {
807         h -= ob_rr_theme->mbwidth;
808     }
809
810     self->text_x = PADDING;
811     self->text_w = w;
812
813     if (self->entries) {
814         if (has_icon) {
815             w += ITEM_HEIGHT + PADDING;
816             self->text_x += ITEM_HEIGHT + PADDING;
817         }
818     }
819
820     if (!w) w = 10;
821     if (!h) h = 3;
822
823     XResizeWindow(obt_display, self->window, w, h);
824
825     self->inner_w = w;
826
827     RrPaint(self->a_items, self->window, w, h);
828
829     for (it = self->entries; it; it = g_list_next(it))
830         menu_entry_frame_render(it->data);
831
832     w += ob_rr_theme->mbwidth * 2;
833     h += ob_rr_theme->mbwidth * 2;
834
835     RECT_SET_SIZE(self->area, w, h);
836
837     XFlush(obt_display);
838 }
839
840 static void menu_frame_update(ObMenuFrame *self)
841 {
842     GList *mit, *fit;
843     const Rect *a;
844     gint h;
845
846     menu_pipe_execute(self->menu);
847     menu_find_submenus(self->menu);
848
849     self->selected = NULL;
850
851     /* start at show_from */
852     mit = g_list_nth(self->menu->entries, self->show_from);
853
854     /* go through the menu's and frame's entries and connect the frame entries
855        to the menu entries */
856     for (fit = self->entries; mit && fit;
857          mit = g_list_next(mit), fit = g_list_next(fit))
858     {
859         ObMenuEntryFrame *f = fit->data;
860         f->entry = mit->data;
861     }
862
863     /* if there are more menu entries than in the frame, add them */
864     while (mit) {
865         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
866         self->entries = g_list_append(self->entries, e);
867         mit = g_list_next(mit);
868     }
869
870     /* if there are more frame entries than menu entries then get rid of
871        them */
872     while (fit) {
873         GList *n = g_list_next(fit);
874         menu_entry_frame_free(fit->data);
875         self->entries = g_list_delete_link(self->entries, fit);
876         fit = n;
877     }
878
879     /* * make the menu fit on the screen */
880
881     /* calculate the height of the menu */
882     h = 0;
883     for (fit = self->entries; fit; fit = g_list_next(fit))
884         h += menu_entry_frame_get_height(fit->data,
885                                          fit == self->entries,
886                                          g_list_next(fit) == NULL);
887     /* add the border at the top and bottom */
888     h += ob_rr_theme->mbwidth * 2;
889
890     a = screen_physical_area_monitor(self->monitor);
891
892     if (h > a->height) {
893         GList *flast, *tmp;
894         gboolean last_entry = TRUE;
895
896         /* take the height of our More... entry into account */
897         h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
898
899         /* start at the end of the entries */
900         flast = g_list_last(self->entries);
901
902         /* pull out all the entries from the frame that don't
903            fit on the screen, leaving at least 1 though */
904         while (h > a->height && g_list_previous(flast) != NULL) {
905             /* update the height, without this entry */
906             h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
907
908             /* destroy the entry we're not displaying */
909             tmp = flast;
910             flast = g_list_previous(flast);
911             menu_entry_frame_free(tmp->data);
912             self->entries = g_list_delete_link(self->entries, tmp);
913
914             /* only the first one that we see is the last entry in the menu */
915             last_entry = FALSE;
916         };
917
918         {
919             ObMenuEntry *more_entry;
920             ObMenuEntryFrame *more_frame;
921             /* make the More... menu entry frame which will display in this
922                frame.
923                if self->menu->more_menu is NULL that means that this is already
924                More... menu, so just use ourself.
925             */
926             more_entry = menu_get_more((self->menu->more_menu ?
927                                         self->menu->more_menu :
928                                         self->menu),
929                                        /* continue where we left off */
930                                        self->show_from +
931                                        g_list_length(self->entries));
932             more_frame = menu_entry_frame_new(more_entry, self);
933             /* make it get deleted when the menu frame goes away */
934             menu_entry_unref(more_entry);
935
936             /* add our More... entry to the frame */
937             self->entries = g_list_append(self->entries, more_frame);
938         }
939     }
940
941     menu_frame_render(self);
942 }
943
944 static gboolean menu_frame_is_visible(ObMenuFrame *self)
945 {
946     return !!(g_list_find(menu_frame_visible, self));
947 }
948
949 static gboolean menu_frame_show(ObMenuFrame *self)
950 {
951     GList *it;
952
953     /* determine if the underlying menu is already visible */
954     for (it = menu_frame_visible; it; it = g_list_next(it)) {
955         ObMenuFrame *f = it->data;
956         if (f->menu == self->menu)
957             break;
958     }
959     if (!it) {
960         if (self->menu->update_func)
961             if (!self->menu->update_func(self, self->menu->data))
962                 return FALSE;
963     }
964
965     if (menu_frame_visible == NULL) {
966         /* no menus shown yet */
967
968         /* grab the pointer in such a way as to pass through "owner events"
969            so that we can get enter/leave notifies in the menu. */
970         if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
971             return FALSE;
972         if (!grab_keyboard()) {
973             ungrab_pointer();
974             return FALSE;
975         }
976     }
977
978     menu_frame_update(self);
979
980     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
981
982     if (self->menu->show_func)
983         self->menu->show_func(self, self->menu->data);
984
985     return TRUE;
986 }
987
988 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
989                                  gboolean mouse)
990 {
991     gint px, py;
992
993     if (menu_frame_is_visible(self))
994         return TRUE;
995     if (!menu_frame_show(self))
996         return FALSE;
997
998     if (self->menu->place_func)
999         self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1000     else
1001         menu_frame_place_topmenu(self, &x, &y);
1002
1003     menu_frame_move(self, x, y);
1004
1005     XMapWindow(obt_display, self->window);
1006
1007     if (screen_pointer_pos(&px, &py)) {
1008         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1009         if (e && e->frame == self)
1010             e->ignore_enters++;
1011     }
1012
1013     return TRUE;
1014 }
1015
1016 /*! Stop hiding an open submenu.
1017     @child The OnMenuFrame of the submenu to be hidden
1018 */
1019 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1020 {
1021     if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1022     submenu_hide_timer = 0;
1023 }
1024
1025 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1026                                  ObMenuEntryFrame *parent_entry)
1027 {
1028     gint x, y, dx, dy;
1029     gint px, py;
1030
1031     if (menu_frame_is_visible(self))
1032         return TRUE;
1033
1034     self->monitor = parent->monitor;
1035     self->parent = parent;
1036     self->parent_entry = parent_entry;
1037
1038     /* set up parent's child to be us */
1039     if ((parent->child) != self) {
1040         if (parent->child)
1041             menu_frame_hide(parent->child);
1042         parent->child = self;
1043         parent->child_entry = parent_entry;
1044     }
1045
1046     if (!menu_frame_show(self))
1047         return FALSE;
1048
1049     menu_frame_place_submenu(self, &x, &y);
1050     menu_frame_move_on_screen(self, x, y, &dx, &dy);
1051
1052     if (dx != 0) {
1053         /*try the other side */
1054         self->direction_right = !self->direction_right;
1055         menu_frame_place_submenu(self, &x, &y);
1056         menu_frame_move_on_screen(self, x, y, &dx, &dy);
1057     }
1058     menu_frame_move(self, x + dx, y + dy);
1059
1060     XMapWindow(obt_display, self->window);
1061
1062     if (screen_pointer_pos(&px, &py)) {
1063         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1064         if (e && e->frame == self)
1065             e->ignore_enters++;
1066     }
1067
1068     return TRUE;
1069 }
1070
1071 static void menu_frame_hide(ObMenuFrame *self)
1072 {
1073     ObMenu *const menu = self->menu;
1074     GList *it = g_list_find(menu_frame_visible, self);
1075     gulong ignore_start;
1076
1077     if (!it)
1078         return;
1079
1080     if (menu->hide_func)
1081         menu->hide_func(self, menu->data);
1082
1083     if (self->child)
1084         menu_frame_hide(self->child);
1085
1086     if (self->parent) {
1087         remove_submenu_hide_timeout(self);
1088
1089         self->parent->child = NULL;
1090         self->parent->child_entry = NULL;
1091     }
1092     self->parent = NULL;
1093     self->parent_entry = NULL;
1094
1095     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1096
1097     if (menu_frame_visible == NULL) {
1098         /* last menu shown */
1099         ungrab_pointer();
1100         ungrab_keyboard();
1101     }
1102
1103     ignore_start = event_start_ignore_all_enters();
1104     XUnmapWindow(obt_display, self->window);
1105     event_end_ignore_all_enters(ignore_start);
1106
1107     menu_frame_free(self);
1108
1109     if (menu->cleanup_func)
1110         menu->cleanup_func(menu, menu->data);
1111 }
1112
1113 void menu_frame_hide_all(void)
1114 {
1115     GList *it;
1116
1117     if (config_submenu_show_delay) {
1118         /* remove any submenu open requests */
1119         if (submenu_show_timer) g_source_remove(submenu_show_timer);
1120         submenu_show_timer = 0;
1121     }
1122     if ((it = g_list_last(menu_frame_visible)))
1123         menu_frame_hide(it->data);
1124 }
1125
1126 ObMenuFrame* menu_frame_under(gint x, gint y)
1127 {
1128     ObMenuFrame *ret = NULL;
1129     GList *it;
1130
1131     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1132         ObMenuFrame *f = it->data;
1133
1134         if (RECT_CONTAINS(f->area, x, y)) {
1135             ret = f;
1136             break;
1137         }
1138     }
1139     return ret;
1140 }
1141
1142 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1143 {
1144     ObMenuFrame *frame;
1145     ObMenuEntryFrame *ret = NULL;
1146     GList *it;
1147
1148     if ((frame = menu_frame_under(x, y))) {
1149         x -= ob_rr_theme->mbwidth + frame->area.x;
1150         y -= ob_rr_theme->mbwidth + frame->area.y;
1151
1152         for (it = frame->entries; it; it = g_list_next(it)) {
1153             ObMenuEntryFrame *e = it->data;
1154
1155             if (RECT_CONTAINS(e->area, x, y)) {
1156                 ret = e;
1157                 break;
1158             }
1159         }
1160     }
1161     return ret;
1162 }
1163
1164 static gboolean submenu_show_timeout(gpointer data)
1165 {
1166     g_assert(menu_frame_visible);
1167     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1168     return FALSE;
1169 }
1170
1171 static gboolean submenu_hide_timeout(gpointer data)
1172 {
1173     g_assert(menu_frame_visible);
1174     menu_frame_hide((ObMenuFrame*)data);
1175     return FALSE;
1176 }
1177
1178 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1179                        gboolean immediate)
1180 {
1181     ObMenuEntryFrame *old = self->selected;
1182     ObMenuFrame *oldchild = self->child;
1183     ObMenuEntryFrame *oldchild_entry = self->child_entry;
1184
1185     /* if the user selected a separator, ignore it and reselect what we had
1186        selected before */
1187     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1188         entry = old;
1189
1190     if (old == entry &&
1191         (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1192         return;
1193
1194     /* if the user left this menu but we have a submenu open, move the
1195        selection back to that submenu */
1196     if (!entry && oldchild_entry)
1197         entry = oldchild_entry;
1198
1199     if (config_submenu_show_delay) {
1200         /* remove any submenu open requests */
1201         if (submenu_show_timer) g_source_remove(submenu_show_timer);
1202         submenu_show_timer = 0;
1203     }
1204
1205     self->selected = entry;
1206
1207     if (old)
1208         menu_entry_frame_render(old);
1209
1210     if (oldchild_entry) {
1211         /* There is an open submenu */
1212         if (oldchild_entry == self->selected) {
1213             /* The open submenu has been reselected, so stop hiding the
1214                submenu */
1215             remove_submenu_hide_timeout(oldchild);
1216         }
1217         else if (oldchild_entry == old) {
1218             /* The open submenu was selected and is no longer, so hide the
1219                submenu */
1220             if (immediate || config_submenu_hide_delay == 0)
1221                 menu_frame_hide(oldchild);
1222             else if (config_submenu_hide_delay > 0) {
1223                 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1224                 submenu_hide_timer =
1225                     g_timeout_add_full(G_PRIORITY_DEFAULT,
1226                                        config_submenu_hide_delay,
1227                                        submenu_hide_timeout, oldchild, NULL);
1228             }
1229         }
1230     }
1231
1232     if (self->selected) {
1233         menu_entry_frame_render(self->selected);
1234
1235         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1236             /* only show if the submenu isn't already showing */
1237             if (oldchild_entry != self->selected) {
1238                 if (immediate || config_submenu_hide_delay == 0)
1239                     menu_entry_frame_show_submenu(self->selected);
1240                 else if (config_submenu_hide_delay > 0) {
1241                     if (submenu_show_timer)
1242                         g_source_remove(submenu_show_timer);
1243                     submenu_show_timer =
1244                         g_timeout_add_full(G_PRIORITY_DEFAULT,
1245                                            config_submenu_show_delay,
1246                                            submenu_show_timeout,
1247                                            self->selected, NULL);
1248                 }
1249             }
1250             /* hide the grandchildren of this menu. and move the cursor to
1251                the current menu */
1252             else if (immediate && self->child && self->child->child) {
1253                 menu_frame_hide(self->child->child);
1254                 menu_frame_select(self->child, NULL, TRUE);
1255             }
1256         }
1257     }
1258 }
1259
1260 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1261 {
1262     ObMenuFrame *f;
1263
1264     if (!self->entry->data.submenu.submenu) return;
1265
1266     f = menu_frame_new(self->entry->data.submenu.submenu,
1267                        self->entry->data.submenu.show_from,
1268                        self->frame->client);
1269     /* pass our direction on to our child */
1270     f->direction_right = self->frame->direction_right;
1271
1272     menu_frame_show_submenu(f, self->frame, self);
1273 }
1274
1275 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1276 {
1277     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1278         self->entry->data.normal.enabled)
1279     {
1280         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1281            gets freed */
1282         ObMenuEntry *entry = self->entry;
1283         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1284         gpointer data = self->frame->menu->data;
1285         GSList *acts = self->entry->data.normal.actions;
1286         ObClient *client = self->frame->client;
1287         ObMenuFrame *frame = self->frame;
1288         guint mods = obt_keyboard_only_modmasks(state);
1289
1290         /* release grabs before executing the shit */
1291         if (!(mods & ControlMask)) {
1292             event_cancel_all_key_grabs();
1293             frame = NULL;
1294         }
1295
1296         if (func)
1297             func(entry, frame, client, state, data);
1298         else
1299             actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1300                              state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1301     }
1302 }
1303
1304 void menu_frame_select_previous(ObMenuFrame *self)
1305 {
1306     GList *it = NULL, *start;
1307
1308     if (self->entries) {
1309         start = it = g_list_find(self->entries, self->selected);
1310         while (TRUE) {
1311             ObMenuEntryFrame *e;
1312
1313             it = it ? g_list_previous(it) : g_list_last(self->entries);
1314             if (it == start)
1315                 break;
1316
1317             if (it) {
1318                 e = it->data;
1319                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1320                     break;
1321                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1322                     break;
1323             }
1324         }
1325     }
1326     menu_frame_select(self, it ? it->data : NULL, FALSE);
1327 }
1328
1329 void menu_frame_select_next(ObMenuFrame *self)
1330 {
1331     GList *it = NULL, *start;
1332
1333     if (self->entries) {
1334         start = it = g_list_find(self->entries, self->selected);
1335         while (TRUE) {
1336             ObMenuEntryFrame *e;
1337
1338             it = it ? g_list_next(it) : self->entries;
1339             if (it == start)
1340                 break;
1341
1342             if (it) {
1343                 e = it->data;
1344                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1345                     break;
1346                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1347                     break;
1348             }
1349         }
1350     }
1351     menu_frame_select(self, it ? it->data : NULL, FALSE);
1352 }
1353
1354 void menu_frame_select_first(ObMenuFrame *self)
1355 {
1356     GList *it = NULL;
1357
1358     if (self->entries) {
1359         for (it = self->entries; it; it = g_list_next(it)) {
1360             ObMenuEntryFrame *e = it->data;
1361             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1362                 break;
1363             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1364                 break;
1365         }
1366     }
1367     menu_frame_select(self, it ? it->data : NULL, FALSE);
1368 }
1369
1370 void menu_frame_select_last(ObMenuFrame *self)
1371 {
1372     GList *it = NULL;
1373
1374     if (self->entries) {
1375         for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1376             ObMenuEntryFrame *e = it->data;
1377             if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1378                 break;
1379             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1380                 break;
1381         }
1382     }
1383     menu_frame_select(self, it ? it->data : NULL, FALSE);
1384 }