dont go to disabled menu entries with keyboard shortcuts
[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 "grab.h"
25 #include "openbox.h"
26 #include "mainloop.h"
27 #include "config.h"
28 #include "render/theme.h"
29
30 #define PADDING 2
31 #define SEPARATOR_HEIGHT 3
32 #define MAX_MENU_WIDTH 400
33
34 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
35                          LeaveWindowMask)
36 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
37                          ButtonPressMask | ButtonReleaseMask)
38
39 GList *menu_frame_visible;
40
41 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
42                                               ObMenuFrame *frame);
43 static void menu_entry_frame_free(ObMenuEntryFrame *self);
44 static void menu_frame_render(ObMenuFrame *self);
45 static void menu_frame_update(ObMenuFrame *self);
46 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
47
48 static Window createWindow(Window parent, gulong mask,
49                            XSetWindowAttributes *attrib)
50 {
51     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
52                          RrDepth(ob_rr_inst), InputOutput,
53                          RrVisual(ob_rr_inst), mask, attrib);
54 }
55
56 GHashTable *menu_frame_map;
57
58 void menu_frame_startup(gboolean reconfig)
59 {
60     if (reconfig) return;
61
62     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
63 }
64
65 void menu_frame_shutdown(gboolean reconfig)
66 {
67     if (reconfig) return;
68
69     g_hash_table_destroy(menu_frame_map);
70 }
71
72 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
73 {
74     ObMenuFrame *self;
75     XSetWindowAttributes attr;
76
77     self = g_new0(ObMenuFrame, 1);
78     self->type = Window_Menu;
79     self->menu = menu;
80     self->selected = NULL;
81     self->client = client;
82     self->direction_right = TRUE;
83
84     attr.event_mask = FRAME_EVENTMASK;
85     self->window = createWindow(RootWindow(ob_display, ob_screen),
86                                 CWEventMask, &attr);
87
88     self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
89     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
90
91     stacking_add(MENU_AS_WINDOW(self));
92
93     return self;
94 }
95
96 void menu_frame_free(ObMenuFrame *self)
97 {
98     if (self) {
99         while (self->entries) {
100             menu_entry_frame_free(self->entries->data);
101             self->entries = g_list_delete_link(self->entries, self->entries);
102         }
103
104         stacking_remove(MENU_AS_WINDOW(self));
105
106         XDestroyWindow(ob_display, self->window);
107
108         RrAppearanceFree(self->a_items);
109         RrAppearanceFree(self->a_title);
110
111         g_free(self);
112     }
113 }
114
115 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
116                                               ObMenuFrame *frame)
117 {
118     ObMenuEntryFrame *self;
119     XSetWindowAttributes attr;
120
121     self = g_new0(ObMenuEntryFrame, 1);
122     self->entry = entry;
123     self->frame = frame;
124
125     attr.event_mask = ENTRY_EVENTMASK;
126     self->window = createWindow(self->frame->window, CWEventMask, &attr);
127     self->text = createWindow(self->window, 0, NULL);
128     g_hash_table_insert(menu_frame_map, &self->window, self);
129     g_hash_table_insert(menu_frame_map, &self->text, self);
130     if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
131         self->icon = createWindow(self->window, 0, NULL);
132         g_hash_table_insert(menu_frame_map, &self->icon, self);
133     }
134     if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
135         self->bullet = createWindow(self->window, 0, NULL);
136         g_hash_table_insert(menu_frame_map, &self->bullet, self);
137     }
138
139     XMapWindow(ob_display, self->window);
140     XMapWindow(ob_display, self->text);
141
142     self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
143     self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
144     self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
145
146     if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
147         self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
148         self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
149     } else {
150         self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
151         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
152         self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
153         self->a_mask->texture[0].type = RR_TEXTURE_MASK;
154         self->a_bullet_normal =
155             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
156         self->a_bullet_selected =
157             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
158     }
159
160     self->a_text_normal =
161         RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
162     self->a_text_disabled =
163         RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
164     self->a_text_selected =
165         RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
166     self->a_text_title =
167         RrAppearanceCopy(ob_rr_theme->a_menu_text_title);
168
169     return self;
170 }
171
172 static void menu_entry_frame_free(ObMenuEntryFrame *self)
173 {
174     if (self) {
175         XDestroyWindow(ob_display, self->text);
176         XDestroyWindow(ob_display, self->window);
177         g_hash_table_remove(menu_frame_map, &self->text);
178         g_hash_table_remove(menu_frame_map, &self->window);
179         if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
180             XDestroyWindow(ob_display, self->icon);
181             g_hash_table_remove(menu_frame_map, &self->icon);
182         }
183         if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
184             XDestroyWindow(ob_display, self->bullet);
185             g_hash_table_remove(menu_frame_map, &self->bullet);
186         }
187
188         RrAppearanceFree(self->a_normal);
189         RrAppearanceFree(self->a_disabled);
190         RrAppearanceFree(self->a_selected);
191
192         RrAppearanceFree(self->a_separator);
193         RrAppearanceFree(self->a_icon);
194         RrAppearanceFree(self->a_mask);
195         RrAppearanceFree(self->a_text_normal);
196         RrAppearanceFree(self->a_text_disabled);
197         RrAppearanceFree(self->a_text_selected);
198         RrAppearanceFree(self->a_text_title);
199         RrAppearanceFree(self->a_bullet_normal);
200         RrAppearanceFree(self->a_bullet_selected);
201
202         g_free(self);
203     }
204 }
205
206 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
207 {
208     RECT_SET_POINT(self->area, x, y);
209     XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
210 }
211
212 void menu_frame_place_topmenu(ObMenuFrame *self, gint x, gint y)
213 {
214     if (self->client && x < 0 && y < 0) {
215         x = self->client->frame->area.x + self->client->frame->size.left;
216         y = self->client->frame->area.y + self->client->frame->size.top;
217     } else {
218         if (config_menu_middle)
219             y -= self->area.height / 2;
220     }
221     menu_frame_move(self, x, y);
222 }
223
224 void menu_frame_place_submenu(ObMenuFrame *self)
225 {
226     gint x, y;
227     gint overlap;
228     gint bwidth;
229
230     overlap = ob_rr_theme->menu_overlap;
231     bwidth = ob_rr_theme->mbwidth;
232
233     if (self->direction_right)
234         x = self->parent->area.x + self->parent->area.width - overlap - bwidth;
235     else
236         x = self->parent->area.x - self->area.width + overlap + bwidth;
237
238     y = self->parent->area.y + self->parent_entry->area.y;
239     if (config_menu_middle)
240         y -= (self->area.height - (bwidth * 2) - self->item_h) / 2;
241     else
242         y += overlap;
243
244     menu_frame_move(self, x, y);
245 }
246
247 void menu_frame_move_on_screen(ObMenuFrame *self, gint *dx, gint *dy)
248 {
249     Rect *a = NULL;
250     gint pos, half;
251
252     *dx = *dy = 0;
253
254     a = screen_physical_area_monitor(self->monitor);
255
256     half = g_list_length(self->entries) / 2;
257     pos = g_list_index(self->entries, self->selected);
258
259     /* if in the bottom half then check this stuff first, will keep the bottom
260        edge of the menu visible */
261     if (pos > half) {
262         *dx = MAX(*dx, a->x - self->area.x);
263         *dy = MAX(*dy, a->y - self->area.y);
264     }
265     *dx = MIN(*dx, (a->x + a->width) - (self->area.x + self->area.width));
266     *dy = MIN(*dy, (a->y + a->height) - (self->area.y + self->area.height));
267     /* if in the top half then check this stuff last, will keep the top
268        edge of the menu visible */
269     if (pos <= half) {
270         *dx = MAX(*dx, a->x - self->area.x);
271         *dy = MAX(*dy, a->y - self->area.y);
272     }
273 }
274
275 static void menu_entry_frame_render(ObMenuEntryFrame *self)
276 {
277     RrAppearance *item_a, *text_a;
278     gint th; /* temp */
279     ObMenu *sub;
280     ObMenuFrame *frame = self->frame;
281
282     switch (self->entry->type) {
283     case OB_MENU_ENTRY_TYPE_NORMAL:
284     case OB_MENU_ENTRY_TYPE_SUBMENU:
285         item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
286                    !self->entry->data.normal.enabled) ?
287                   self->a_disabled :
288                   (self == self->frame->selected ?
289                    self->a_selected :
290                    self->a_normal));
291         th = self->frame->item_h;
292         break;
293     case OB_MENU_ENTRY_TYPE_SEPARATOR:
294         if (self->entry->data.separator.label) {
295             item_a = self->frame->a_title;
296             th = ob_rr_theme->menu_title_height;
297         } else {
298             item_a = self->a_normal;
299             th = SEPARATOR_HEIGHT + 2*PADDING;
300         }
301         break;
302     default:
303         g_assert_not_reached();
304     }
305     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
306     XResizeWindow(ob_display, self->window,
307                   self->area.width, self->area.height);
308     item_a->surface.parent = self->frame->a_items;
309     item_a->surface.parentx = self->area.x;
310     item_a->surface.parenty = self->area.y;
311     RrPaint(item_a, self->window, self->area.width, self->area.height);
312
313     switch (self->entry->type) {
314     case OB_MENU_ENTRY_TYPE_NORMAL:
315         text_a = (!self->entry->data.normal.enabled ?
316                   self->a_text_disabled :
317                   (self == self->frame->selected ?
318                    self->a_text_selected :
319                    self->a_text_normal));
320         text_a->texture[0].data.text.string = self->entry->data.normal.label;
321         if (self->entry->data.normal.shortcut &&
322             (self->frame->menu->show_all_shortcuts ||
323              self->entry->data.normal.shortcut_position > 0))
324         {
325             text_a->texture[0].data.text.shortcut = TRUE;
326             text_a->texture[0].data.text.shortcut_pos =
327                 self->entry->data.normal.shortcut_position;
328         } else
329             text_a->texture[0].data.text.shortcut = FALSE;
330         break;
331     case OB_MENU_ENTRY_TYPE_SUBMENU:
332         text_a = (self == self->frame->selected ?
333                   self->a_text_selected :
334                   self->a_text_normal);
335         sub = self->entry->data.submenu.submenu;
336         text_a->texture[0].data.text.string = sub ? sub->title : "";
337         if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
338                               sub->shortcut_position > 0))
339         {
340             text_a->texture[0].data.text.shortcut = TRUE;
341             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
342         } else
343             text_a->texture[0].data.text.shortcut = FALSE;
344         break;
345     case OB_MENU_ENTRY_TYPE_SEPARATOR:
346         if (self->entry->data.separator.label != NULL)
347             text_a = self->a_text_title;
348         else
349             text_a = self->a_text_normal;
350         break;
351     }
352
353     switch (self->entry->type) {
354     case OB_MENU_ENTRY_TYPE_NORMAL:
355         XMoveResizeWindow(ob_display, self->text,
356                           self->frame->text_x, PADDING,
357                           self->frame->text_w,
358                           self->frame->item_h - 2*PADDING);
359         text_a->surface.parent = item_a;
360         text_a->surface.parentx = self->frame->text_x;
361         text_a->surface.parenty = PADDING;
362         RrPaint(text_a, self->text, self->frame->text_w,
363                 self->frame->item_h - 2*PADDING);
364         break;
365     case OB_MENU_ENTRY_TYPE_SUBMENU:
366         XMoveResizeWindow(ob_display, self->text,
367                           self->frame->text_x, PADDING,
368                           self->frame->text_w - self->frame->item_h,
369                           self->frame->item_h - 2*PADDING);
370         text_a->surface.parent = item_a;
371         text_a->surface.parentx = self->frame->text_x;
372         text_a->surface.parenty = PADDING;
373         RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
374                 self->frame->item_h - 2*PADDING);
375         break;
376     case OB_MENU_ENTRY_TYPE_SEPARATOR:
377         if (self->entry->data.separator.label != NULL) {
378             /* labeled separator */
379             XMoveResizeWindow(ob_display, self->text,
380                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
381                               self->area.width - 2*ob_rr_theme->paddingx,
382                               ob_rr_theme->menu_title_height -
383                               2*ob_rr_theme->paddingy);
384             text_a->surface.parent = item_a;
385             text_a->surface.parentx = ob_rr_theme->paddingx;
386             text_a->surface.parenty = ob_rr_theme->paddingy;
387             RrPaint(text_a, self->text,
388                     self->area.width - 2*ob_rr_theme->paddingx,
389                     ob_rr_theme->menu_title_height -
390                     2*ob_rr_theme->paddingy);
391         } else {
392             /* unlabeled separaator */
393             XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
394                               self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
395             self->a_separator->surface.parent = item_a;
396             self->a_separator->surface.parentx = PADDING;
397             self->a_separator->surface.parenty = PADDING;
398             self->a_separator->texture[0].data.lineart.color =
399                 text_a->texture[0].data.text.color;
400             self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
401             self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
402             self->a_separator->texture[0].data.lineart.x2 =
403                 self->area.width - 4*PADDING;
404             self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
405             RrPaint(self->a_separator, self->text,
406                     self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
407         }
408         break;
409     }
410
411     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
412         self->entry->data.normal.icon_data)
413     {
414         XMoveResizeWindow(ob_display, self->icon,
415                           PADDING, frame->item_margin.top,
416                           self->frame->item_h - frame->item_margin.top
417                           - frame->item_margin.bottom,
418                           self->frame->item_h - frame->item_margin.top
419                           - frame->item_margin.bottom);
420         self->a_icon->texture[0].data.rgba.width =
421             self->entry->data.normal.icon_width;
422         self->a_icon->texture[0].data.rgba.height =
423             self->entry->data.normal.icon_height;
424         self->a_icon->texture[0].data.rgba.data =
425             self->entry->data.normal.icon_data;
426         self->a_icon->surface.parent = item_a;
427         self->a_icon->surface.parentx = PADDING;
428         self->a_icon->surface.parenty = frame->item_margin.top;
429         RrPaint(self->a_icon, self->icon,
430                 self->frame->item_h - frame->item_margin.top
431                 - frame->item_margin.bottom,
432                 self->frame->item_h - frame->item_margin.top
433                 - frame->item_margin.bottom);
434         XMapWindow(ob_display, self->icon);
435     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
436                self->entry->data.normal.mask)
437     {
438         RrColor *c;
439
440         XMoveResizeWindow(ob_display, self->icon,
441                           PADDING, frame->item_margin.top,
442                           self->frame->item_h - frame->item_margin.top
443                           - frame->item_margin.bottom,
444                           self->frame->item_h - frame->item_margin.top
445                           - frame->item_margin.bottom);
446         self->a_mask->texture[0].data.mask.mask =
447             self->entry->data.normal.mask;
448
449         c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
450               !self->entry->data.normal.enabled) ?
451              self->entry->data.normal.mask_disabled_color :
452              (self == self->frame->selected ?
453               self->entry->data.normal.mask_selected_color :
454               self->entry->data.normal.mask_normal_color));
455         self->a_mask->texture[0].data.mask.color = c;
456
457         self->a_mask->surface.parent = item_a;
458         self->a_mask->surface.parentx = PADDING;
459         self->a_mask->surface.parenty = frame->item_margin.top;
460         RrPaint(self->a_mask, self->icon,
461                 self->frame->item_h - frame->item_margin.top
462                 - frame->item_margin.bottom,
463                 self->frame->item_h - frame->item_margin.top
464                 - frame->item_margin.bottom);
465         XMapWindow(ob_display, self->icon);
466     } else
467         XUnmapWindow(ob_display, self->icon);
468
469     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
470         RrAppearance *bullet_a;
471         XMoveResizeWindow(ob_display, self->bullet,
472                           self->frame->text_x + self->frame->text_w
473                           - self->frame->item_h + PADDING, PADDING,
474                           self->frame->item_h - 2*PADDING,
475                           self->frame->item_h - 2*PADDING);
476         bullet_a = (self == self->frame->selected ?
477                     self->a_bullet_selected :
478                     self->a_bullet_normal);
479         bullet_a->surface.parent = item_a;
480         bullet_a->surface.parentx =
481             self->frame->text_x + self->frame->text_w - self->frame->item_h
482             + PADDING;
483         bullet_a->surface.parenty = PADDING;
484         RrPaint(bullet_a, self->bullet,
485                 self->frame->item_h - 2*PADDING,
486                 self->frame->item_h - 2*PADDING);
487         XMapWindow(ob_display, self->bullet);
488     } else
489         XUnmapWindow(ob_display, self->bullet);
490
491     XFlush(ob_display);
492 }
493
494 static void menu_frame_render(ObMenuFrame *self)
495 {
496     gint w = 0, h = 0;
497     gint tw, th; /* temps */
498     GList *it;
499     gboolean has_icon = FALSE;
500     ObMenu *sub;
501     ObMenuEntryFrame *e;
502
503     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
504     XSetWindowBorder(ob_display, self->window,
505                      RrColorPixel(ob_rr_theme->menu_b_color));
506
507     /* find text dimensions */
508
509     STRUT_SET(self->item_margin, 0, 0, 0, 0);
510
511     if (self->entries) {
512         ObMenuEntryFrame *e = self->entries->data;
513         gint l, t, r, b;
514
515         e->a_text_normal->texture[0].data.text.string = "";
516         RrMinsize(e->a_text_normal, &tw, &th);
517         tw += 2*PADDING;
518         th += 2*PADDING;
519         self->item_h = th;
520
521         RrMargins(e->a_normal, &l, &t, &r, &b);
522         STRUT_SET(self->item_margin,
523                   MAX(self->item_margin.left, l),
524                   MAX(self->item_margin.top, t),
525                   MAX(self->item_margin.right, r),
526                   MAX(self->item_margin.bottom, b));
527         RrMargins(e->a_selected, &l, &t, &r, &b);
528         STRUT_SET(self->item_margin,
529                   MAX(self->item_margin.left, l),
530                   MAX(self->item_margin.top, t),
531                   MAX(self->item_margin.right, r),
532                   MAX(self->item_margin.bottom, b));
533         RrMargins(e->a_disabled, &l, &t, &r, &b);
534         STRUT_SET(self->item_margin,
535                   MAX(self->item_margin.left, l),
536                   MAX(self->item_margin.top, t),
537                   MAX(self->item_margin.right, r),
538                   MAX(self->item_margin.bottom, b));
539     } else
540         self->item_h = 0;
541
542     /* render the entries */
543
544     for (it = self->entries; it; it = g_list_next(it)) {
545         RrAppearance *text_a;
546         e = it->data;
547
548         /* if the first entry is a labeled separator, then make its border
549            overlap with the menu's outside border */
550         if (it == self->entries &&
551             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
552             e->entry->data.separator.label)
553         {
554             h -= ob_rr_theme->mbwidth;
555         }
556
557         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
558             e->entry->data.separator.label)
559         {
560             e->border = ob_rr_theme->mbwidth;
561         }
562
563         RECT_SET_POINT(e->area, 0, h+e->border);
564         XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
565         XSetWindowBorderWidth(ob_display, e->window, e->border);
566         XSetWindowBorder(ob_display, e->window,
567                          RrColorPixel(ob_rr_theme->menu_b_color));
568
569         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
570                    !e->entry->data.normal.enabled) ?
571                   e->a_text_disabled :
572                   (e == self->selected ?
573                    e->a_text_selected :
574                    e->a_text_normal));
575         switch (e->entry->type) {
576         case OB_MENU_ENTRY_TYPE_NORMAL:
577             text_a->texture[0].data.text.string = e->entry->data.normal.label;
578             RrMinsize(text_a, &tw, &th);
579             tw = MIN(tw, MAX_MENU_WIDTH);
580
581             if (e->entry->data.normal.icon_data ||
582                 e->entry->data.normal.mask)
583                 has_icon = TRUE;
584             break;
585         case OB_MENU_ENTRY_TYPE_SUBMENU:
586             sub = e->entry->data.submenu.submenu;
587             text_a->texture[0].data.text.string = sub ? sub->title : "";
588             RrMinsize(text_a, &tw, &th);
589             tw = MIN(tw, MAX_MENU_WIDTH);
590
591             if (e->entry->data.normal.icon_data ||
592                 e->entry->data.normal.mask)
593                 has_icon = TRUE;
594
595             tw += self->item_h - PADDING;
596             break;
597         case OB_MENU_ENTRY_TYPE_SEPARATOR:
598             if (e->entry->data.separator.label != NULL) {
599                 e->a_text_title->texture[0].data.text.string =
600                     e->entry->data.separator.label;
601                 RrMinsize(e->a_text_title, &tw, &th);
602                 tw = MIN(tw, MAX_MENU_WIDTH);
603                 th = ob_rr_theme->menu_title_height +
604                     (ob_rr_theme->mbwidth - PADDING) *2;
605             } else {
606                 tw = 0;
607                 th = SEPARATOR_HEIGHT;
608             }
609             break;
610         }
611         tw += 2*PADDING;
612         th += 2*PADDING;
613         w = MAX(w, tw);
614         h += th;
615     }
616
617     /* if the last entry is a labeled separator, then make its border
618        overlap with the menu's outside border */
619     it = g_list_last(self->entries);
620     e = it ? it->data : NULL;
621     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
622         e->entry->data.separator.label)
623     {
624         h -= ob_rr_theme->mbwidth;
625     }
626
627     self->text_x = PADDING;
628     self->text_w = w;
629
630     if (self->entries) {
631         if (has_icon) {
632             w += self->item_h + PADDING;
633             self->text_x += self->item_h + PADDING;
634         }
635     }
636
637     if (!w) w = 10;
638     if (!h) h = 3;
639
640     XResizeWindow(ob_display, self->window, w, h);
641
642     self->inner_w = w;
643
644     RrPaint(self->a_items, self->window, w, h);
645
646     for (it = self->entries; it; it = g_list_next(it))
647         menu_entry_frame_render(it->data);
648
649     w += ob_rr_theme->mbwidth * 2;
650     h += ob_rr_theme->mbwidth * 2;
651
652     RECT_SET_SIZE(self->area, w, h);
653
654     XFlush(ob_display);
655 }
656
657 static void menu_frame_update(ObMenuFrame *self)
658 {
659     GList *mit, *fit;
660
661     menu_pipe_execute(self->menu);
662     menu_find_submenus(self->menu);
663
664     self->selected = NULL;
665
666     for (mit = self->menu->entries, fit = self->entries; mit && fit;
667          mit = g_list_next(mit), fit = g_list_next(fit))
668     {
669         ObMenuEntryFrame *f = fit->data;
670         f->entry = mit->data;
671     }
672
673     while (mit) {
674         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
675         self->entries = g_list_append(self->entries, e);
676         mit = g_list_next(mit);
677     }
678     
679     while (fit) {
680         GList *n = g_list_next(fit);
681         menu_entry_frame_free(fit->data);
682         self->entries = g_list_delete_link(self->entries, fit);
683         fit = n;
684     }
685
686     menu_frame_render(self);
687 }
688
689 static gboolean menu_frame_is_visible(ObMenuFrame *self)
690 {
691     return !!(g_list_find(menu_frame_visible, self));
692 }
693
694 static gboolean menu_frame_show(ObMenuFrame *self)
695 {
696     GList *it;
697
698     if (menu_frame_visible == NULL) {
699         /* no menus shown yet */
700         if (!grab_pointer(TRUE, TRUE, OB_CURSOR_POINTER))
701             return FALSE;
702         if (!grab_keyboard(TRUE)) {
703             grab_pointer(FALSE, TRUE, OB_CURSOR_POINTER);
704             return FALSE;
705         }
706     }
707
708     /* determine if the underlying menu is already visible */
709     for (it = menu_frame_visible; it; it = g_list_next(it)) {
710         ObMenuFrame *f = it->data;
711         if (f->menu == self->menu)
712             break;
713     }
714     if (!it) {
715         if (self->menu->update_func)
716             self->menu->update_func(self, self->menu->data);
717     }
718
719     menu_frame_update(self);
720
721     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
722
723     return TRUE;
724 }
725
726 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y)
727 {
728     gint dx, dy;
729     guint i;
730
731     if (menu_frame_is_visible(self))
732         return TRUE;
733     if (!menu_frame_show(self))
734         return FALSE;
735
736     menu_frame_place_topmenu(self, x, y);
737
738     /* find the monitor the menu is on */
739     for (i = 0; i < screen_num_monitors; ++i) {
740         Rect *a = screen_physical_area_monitor(i);
741         if (RECT_CONTAINS(*a, x, y)) {
742             self->monitor = i;
743             break;
744         }
745     }
746
747     menu_frame_move_on_screen(self, &dx, &dy);
748     menu_frame_move(self, self->area.x + dx, self->area.y + dy);
749
750     XMapWindow(ob_display, self->window);
751
752     return TRUE;
753 }
754
755 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
756                                  ObMenuEntryFrame *parent_entry)
757 {
758     ObMenuEntryFrame *e;
759     gint dx, dy;
760
761     if (menu_frame_is_visible(self))
762         return TRUE;
763
764     self->monitor = parent->monitor;
765     self->parent = parent;
766     self->parent_entry = parent_entry;
767
768     /* set up parent's child to be us */
769     if (parent->child)
770         menu_frame_hide(parent->child);
771     parent->child = self;
772
773     if (!menu_frame_show(self))
774         return FALSE;
775
776     menu_frame_place_submenu(self);
777     menu_frame_move_on_screen(self, &dx, &dy);
778
779     if (dx == 0) {
780         menu_frame_move(self, self->area.x, self->area.y + dy);
781     } else {
782         gboolean dir;
783
784         /* flip the direction in which we're placing submenus */
785         if (dx > 0)
786             dir = TRUE;
787         else
788             dir = FALSE;
789
790         /* if it changed, then replace the menu on the opposite side,
791            and try keep it on the screen too */
792         if (dir != self->direction_right) {
793             self->direction_right = dir;
794             menu_frame_place_submenu(self);
795             menu_frame_move_on_screen(self, &dx, &dy);
796             menu_frame_move(self, self->area.x + dx, self->area.y + dy);
797         }
798     }
799
800     XMapWindow(ob_display, self->window);
801
802     if (screen_pointer_pos(&dx, &dy) && (e = menu_entry_frame_under(dx, dy)) &&
803         e->frame == self)
804         ++e->ignore_enters;
805
806     return TRUE;
807 }
808
809 void menu_frame_hide(ObMenuFrame *self)
810 {
811     GList *it = g_list_find(menu_frame_visible, self);
812
813     if (!it)
814         return;
815
816     if (self->child)
817         menu_frame_hide(self->child);
818
819     if (self->parent)
820         self->parent->child = NULL;
821     self->parent = NULL;
822     self->parent_entry = NULL;
823
824     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
825
826     if (menu_frame_visible == NULL) {
827         /* last menu shown */
828         grab_pointer(FALSE, TRUE, OB_CURSOR_NONE);
829         grab_keyboard(FALSE);
830     }
831
832     XUnmapWindow(ob_display, self->window);
833
834     menu_frame_free(self);
835 }
836
837 void menu_frame_hide_all()
838 {
839     GList *it;
840
841     if (config_submenu_show_delay) {
842         /* remove any submenu open requests */
843         ob_main_loop_timeout_remove(ob_main_loop,
844                                     menu_entry_frame_submenu_timeout);
845     }
846     if ((it = g_list_last(menu_frame_visible)))
847         menu_frame_hide(it->data);
848 }
849
850 void menu_frame_hide_all_client(ObClient *client)
851 {
852     GList *it = g_list_last(menu_frame_visible);
853     if (it) {
854         ObMenuFrame *f = it->data;
855         if (f->client == client)
856             menu_frame_hide(f);
857     }
858 }
859
860
861 ObMenuFrame* menu_frame_under(gint x, gint y)
862 {
863     ObMenuFrame *ret = NULL;
864     GList *it;
865
866     for (it = menu_frame_visible; it; it = g_list_next(it)) {
867         ObMenuFrame *f = it->data;
868
869         if (RECT_CONTAINS(f->area, x, y)) {
870             ret = f;
871             break;
872         }
873     }
874     return ret;
875 }
876
877 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
878 {
879     ObMenuFrame *frame;
880     ObMenuEntryFrame *ret = NULL;
881     GList *it;
882
883     if ((frame = menu_frame_under(x, y))) {
884         x -= ob_rr_theme->mbwidth + frame->area.x;
885         y -= ob_rr_theme->mbwidth + frame->area.y;
886
887         for (it = frame->entries; it; it = g_list_next(it)) {
888             ObMenuEntryFrame *e = it->data;
889
890             if (RECT_CONTAINS(e->area, x, y)) {
891                 ret = e;            
892                 break;
893             }
894         }
895     }
896     return ret;
897 }
898
899 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
900 {
901     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
902     return FALSE;
903 }
904
905 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
906                        gboolean immediate)
907 {
908     ObMenuEntryFrame *old = self->selected;
909     ObMenuFrame *oldchild = self->child;
910
911     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
912         entry = old;
913
914     if (old == entry) return;
915    
916     if (config_submenu_show_delay) { 
917         /* remove any submenu open requests */
918         ob_main_loop_timeout_remove(ob_main_loop,
919                                     menu_entry_frame_submenu_timeout);
920     }
921
922     self->selected = entry;
923
924     if (old)
925         menu_entry_frame_render(old);
926     if (oldchild)
927         menu_frame_hide(oldchild);
928
929     if (self->selected) {
930         menu_entry_frame_render(self->selected);
931
932         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
933             if (config_submenu_show_delay && !immediate) {
934                 /* initiate a new submenu open request */
935                 ob_main_loop_timeout_add(ob_main_loop,
936                                          config_submenu_show_delay * 1000,
937                                          menu_entry_frame_submenu_timeout,
938                                          self->selected, g_direct_equal,
939                                          NULL);
940             } else {
941                 menu_entry_frame_show_submenu(self->selected);
942             }
943         }
944     }
945 }
946
947 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
948 {
949     ObMenuFrame *f;
950
951     if (!self->entry->data.submenu.submenu) return;
952
953     f = menu_frame_new(self->entry->data.submenu.submenu,
954                        self->frame->client);
955     /* pass our direction on to our child */
956     f->direction_right = self->frame->direction_right;
957
958     menu_frame_show_submenu(f, self->frame, self);
959 }
960
961 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
962 {
963     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
964         self->entry->data.normal.enabled)
965     {
966         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
967            gets freed */
968         ObMenuEntry *entry = self->entry;
969         ObMenuExecuteFunc func = self->frame->menu->execute_func;
970         gpointer data = self->frame->menu->data;
971         GSList *acts = self->entry->data.normal.actions;
972         ObClient *client = self->frame->client;
973
974         /* release grabs before executing the shit */
975         if (!(state & ControlMask))
976             menu_frame_hide_all();
977
978         if (func)
979             func(entry, state, data, time);
980         else
981             action_run(acts, client, state, time);
982     }
983 }
984
985 void menu_frame_select_previous(ObMenuFrame *self)
986 {
987     GList *it = NULL, *start;
988
989     if (self->entries) {
990         start = it = g_list_find(self->entries, self->selected);
991         while (TRUE) {
992             ObMenuEntryFrame *e;
993
994             it = it ? g_list_previous(it) : g_list_last(self->entries);
995             if (it == start)
996                 break;
997
998             if (it) {
999                 e = it->data;
1000                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1001                     break;
1002                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1003                     e->entry->data.normal.enabled)
1004                     break;
1005             }
1006         }
1007     }
1008     menu_frame_select(self, it ? it->data : NULL, TRUE);
1009 }
1010
1011 void menu_frame_select_next(ObMenuFrame *self)
1012 {
1013     GList *it = NULL, *start;
1014
1015     if (self->entries) {
1016         start = it = g_list_find(self->entries, self->selected);
1017         while (TRUE) {
1018             ObMenuEntryFrame *e;
1019
1020             it = it ? g_list_next(it) : self->entries;
1021             if (it == start)
1022                 break;
1023
1024             if (it) {
1025                 e = it->data;
1026                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1027                     break;
1028                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1029                     e->entry->data.normal.enabled)
1030                     break;
1031             }
1032         }
1033     }
1034     menu_frame_select(self, it ? it->data : NULL, TRUE);
1035 }