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