]> icculus.org git repositories - dana/openbox.git/blob - openbox/menuframe.c
end interactive grabs durign reconfigure instead of leaving everything in a hung...
[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 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
213 {
214     gint dx, dy;
215
216     if (config_menu_middle) {
217         gint myx;
218
219         myx = *x;
220         *y -= self->area.height / 2;
221
222         /* try to the right of the cursor */
223         menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
224         self->direction_right = TRUE;
225         if (dx != 0) {
226             /* try to the left of the cursor */
227             myx = *x - self->area.width;
228             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
229             self->direction_right = FALSE;
230         }
231         if (dx != 0) {
232             /* if didnt fit on either side so just use what it says */
233             myx = *x;
234             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
235             self->direction_right = TRUE;
236         }
237         *x = myx + dx;
238         *y += dy;
239     } else {
240         gint myx, myy;
241
242         myx = *x;
243         myy = *y;
244
245         /* try to the bottom right of the cursor */
246         menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
247         self->direction_right = TRUE;
248         if (dx != 0 || dy != 0) {
249             /* try to the bottom left of the cursor */
250             myx = *x - self->area.width;
251             myy = *y;
252             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
253             self->direction_right = FALSE;
254         }
255         if (dx != 0 || dy != 0) {
256             /* try to the top right of the cursor */
257             myx = *x;
258             myy = *y - self->area.height;
259             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
260             self->direction_right = TRUE;
261         }
262         if (dx != 0 || dy != 0) {
263             /* try to the top left of the cursor */
264             myx = *x - self->area.width;
265             myy = *y - self->area.height;
266             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267             self->direction_right = FALSE;
268         }
269         if (dx != 0 || dy != 0) {
270             /* if didnt fit on either side so just use what it says */
271             myx = *x;
272             myy = *y;
273             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
274             self->direction_right = TRUE;
275         }
276         *x = myx + dx;
277         *y = myy + dy;
278     }
279 }
280
281 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
282 {
283     gint overlap;
284     gint bwidth;
285
286     overlap = ob_rr_theme->menu_overlap;
287     bwidth = ob_rr_theme->mbwidth;
288
289     if (self->direction_right)
290         *x = self->parent->area.x + self->parent->area.width -
291             overlap - bwidth;
292     else
293         *x = self->parent->area.x - self->area.width + overlap + bwidth;
294
295     *y = self->parent->area.y + self->parent_entry->area.y;
296     if (config_menu_middle)
297         *y -= (self->area.height - (bwidth * 2) - self->item_h) / 2;
298     else
299         *y += overlap;
300 }
301
302 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
303                                gint *dx, gint *dy)
304 {
305     Rect *a = NULL;
306     gint pos, half;
307
308     *dx = *dy = 0;
309
310     a = screen_physical_area_monitor(self->monitor);
311
312     half = g_list_length(self->entries) / 2;
313     pos = g_list_index(self->entries, self->selected);
314
315     /* if in the bottom half then check this stuff first, will keep the bottom
316        edge of the menu visible */
317     if (pos > half) {
318         *dx = MAX(*dx, a->x - x);
319         *dy = MAX(*dy, a->y - y);
320     }
321     *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
322     *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
323     /* if in the top half then check this stuff last, will keep the top
324        edge of the menu visible */
325     if (pos <= half) {
326         *dx = MAX(*dx, a->x - x);
327         *dy = MAX(*dy, a->y - y);
328     }
329 }
330
331 static void menu_entry_frame_render(ObMenuEntryFrame *self)
332 {
333     RrAppearance *item_a, *text_a;
334     gint th; /* temp */
335     ObMenu *sub;
336     ObMenuFrame *frame = self->frame;
337
338     switch (self->entry->type) {
339     case OB_MENU_ENTRY_TYPE_NORMAL:
340     case OB_MENU_ENTRY_TYPE_SUBMENU:
341         item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
342                    !self->entry->data.normal.enabled) ?
343                   self->a_disabled :
344                   (self == self->frame->selected ?
345                    self->a_selected :
346                    self->a_normal));
347         th = self->frame->item_h;
348         break;
349     case OB_MENU_ENTRY_TYPE_SEPARATOR:
350         if (self->entry->data.separator.label) {
351             item_a = self->frame->a_title;
352             th = ob_rr_theme->menu_title_height;
353         } else {
354             item_a = self->a_normal;
355             th = SEPARATOR_HEIGHT + 2*PADDING;
356         }
357         break;
358     default:
359         g_assert_not_reached();
360     }
361     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
362     XResizeWindow(ob_display, self->window,
363                   self->area.width, self->area.height);
364     item_a->surface.parent = self->frame->a_items;
365     item_a->surface.parentx = self->area.x;
366     item_a->surface.parenty = self->area.y;
367     RrPaint(item_a, self->window, self->area.width, self->area.height);
368
369     switch (self->entry->type) {
370     case OB_MENU_ENTRY_TYPE_NORMAL:
371         text_a = (!self->entry->data.normal.enabled ?
372                   self->a_text_disabled :
373                   (self == self->frame->selected ?
374                    self->a_text_selected :
375                    self->a_text_normal));
376         text_a->texture[0].data.text.string = self->entry->data.normal.label;
377         if (self->entry->data.normal.shortcut &&
378             (self->frame->menu->show_all_shortcuts ||
379              self->entry->data.normal.shortcut_position > 0))
380         {
381             text_a->texture[0].data.text.shortcut = TRUE;
382             text_a->texture[0].data.text.shortcut_pos =
383                 self->entry->data.normal.shortcut_position;
384         } else
385             text_a->texture[0].data.text.shortcut = FALSE;
386         break;
387     case OB_MENU_ENTRY_TYPE_SUBMENU:
388         text_a = (self == self->frame->selected ?
389                   self->a_text_selected :
390                   self->a_text_normal);
391         sub = self->entry->data.submenu.submenu;
392         text_a->texture[0].data.text.string = sub ? sub->title : "";
393         if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
394                               sub->shortcut_position > 0))
395         {
396             text_a->texture[0].data.text.shortcut = TRUE;
397             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
398         } else
399             text_a->texture[0].data.text.shortcut = FALSE;
400         break;
401     case OB_MENU_ENTRY_TYPE_SEPARATOR:
402         if (self->entry->data.separator.label != NULL)
403             text_a = self->a_text_title;
404         else
405             text_a = self->a_text_normal;
406         break;
407     }
408
409     switch (self->entry->type) {
410     case OB_MENU_ENTRY_TYPE_NORMAL:
411         XMoveResizeWindow(ob_display, self->text,
412                           self->frame->text_x, PADDING,
413                           self->frame->text_w,
414                           self->frame->item_h - 2*PADDING);
415         text_a->surface.parent = item_a;
416         text_a->surface.parentx = self->frame->text_x;
417         text_a->surface.parenty = PADDING;
418         RrPaint(text_a, self->text, self->frame->text_w,
419                 self->frame->item_h - 2*PADDING);
420         break;
421     case OB_MENU_ENTRY_TYPE_SUBMENU:
422         XMoveResizeWindow(ob_display, self->text,
423                           self->frame->text_x, PADDING,
424                           self->frame->text_w - self->frame->item_h,
425                           self->frame->item_h - 2*PADDING);
426         text_a->surface.parent = item_a;
427         text_a->surface.parentx = self->frame->text_x;
428         text_a->surface.parenty = PADDING;
429         RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
430                 self->frame->item_h - 2*PADDING);
431         break;
432     case OB_MENU_ENTRY_TYPE_SEPARATOR:
433         if (self->entry->data.separator.label != NULL) {
434             /* labeled separator */
435             XMoveResizeWindow(ob_display, self->text,
436                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
437                               self->area.width - 2*ob_rr_theme->paddingx,
438                               ob_rr_theme->menu_title_height -
439                               2*ob_rr_theme->paddingy);
440             text_a->surface.parent = item_a;
441             text_a->surface.parentx = ob_rr_theme->paddingx;
442             text_a->surface.parenty = ob_rr_theme->paddingy;
443             RrPaint(text_a, self->text,
444                     self->area.width - 2*ob_rr_theme->paddingx,
445                     ob_rr_theme->menu_title_height -
446                     2*ob_rr_theme->paddingy);
447         } else {
448             /* unlabeled separaator */
449             XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
450                               self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
451             self->a_separator->surface.parent = item_a;
452             self->a_separator->surface.parentx = PADDING;
453             self->a_separator->surface.parenty = PADDING;
454             self->a_separator->texture[0].data.lineart.color =
455                 text_a->texture[0].data.text.color;
456             self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
457             self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
458             self->a_separator->texture[0].data.lineart.x2 =
459                 self->area.width - 4*PADDING;
460             self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
461             RrPaint(self->a_separator, self->text,
462                     self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
463         }
464         break;
465     }
466
467     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
468         self->entry->data.normal.icon_data)
469     {
470         XMoveResizeWindow(ob_display, self->icon,
471                           PADDING, frame->item_margin.top,
472                           self->frame->item_h - frame->item_margin.top
473                           - frame->item_margin.bottom,
474                           self->frame->item_h - frame->item_margin.top
475                           - frame->item_margin.bottom);
476         self->a_icon->texture[0].data.rgba.width =
477             self->entry->data.normal.icon_width;
478         self->a_icon->texture[0].data.rgba.height =
479             self->entry->data.normal.icon_height;
480         self->a_icon->texture[0].data.rgba.data =
481             self->entry->data.normal.icon_data;
482         self->a_icon->surface.parent = item_a;
483         self->a_icon->surface.parentx = PADDING;
484         self->a_icon->surface.parenty = frame->item_margin.top;
485         RrPaint(self->a_icon, self->icon,
486                 self->frame->item_h - frame->item_margin.top
487                 - frame->item_margin.bottom,
488                 self->frame->item_h - frame->item_margin.top
489                 - frame->item_margin.bottom);
490         XMapWindow(ob_display, self->icon);
491     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
492                self->entry->data.normal.mask)
493     {
494         RrColor *c;
495
496         XMoveResizeWindow(ob_display, self->icon,
497                           PADDING, frame->item_margin.top,
498                           self->frame->item_h - frame->item_margin.top
499                           - frame->item_margin.bottom,
500                           self->frame->item_h - frame->item_margin.top
501                           - frame->item_margin.bottom);
502         self->a_mask->texture[0].data.mask.mask =
503             self->entry->data.normal.mask;
504
505         c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
506               !self->entry->data.normal.enabled) ?
507              self->entry->data.normal.mask_disabled_color :
508              (self == self->frame->selected ?
509               self->entry->data.normal.mask_selected_color :
510               self->entry->data.normal.mask_normal_color));
511         self->a_mask->texture[0].data.mask.color = c;
512
513         self->a_mask->surface.parent = item_a;
514         self->a_mask->surface.parentx = PADDING;
515         self->a_mask->surface.parenty = frame->item_margin.top;
516         RrPaint(self->a_mask, self->icon,
517                 self->frame->item_h - frame->item_margin.top
518                 - frame->item_margin.bottom,
519                 self->frame->item_h - frame->item_margin.top
520                 - frame->item_margin.bottom);
521         XMapWindow(ob_display, self->icon);
522     } else
523         XUnmapWindow(ob_display, self->icon);
524
525     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
526         RrAppearance *bullet_a;
527         XMoveResizeWindow(ob_display, self->bullet,
528                           self->frame->text_x + self->frame->text_w
529                           - self->frame->item_h + PADDING, PADDING,
530                           self->frame->item_h - 2*PADDING,
531                           self->frame->item_h - 2*PADDING);
532         bullet_a = (self == self->frame->selected ?
533                     self->a_bullet_selected :
534                     self->a_bullet_normal);
535         bullet_a->surface.parent = item_a;
536         bullet_a->surface.parentx =
537             self->frame->text_x + self->frame->text_w - self->frame->item_h
538             + PADDING;
539         bullet_a->surface.parenty = PADDING;
540         RrPaint(bullet_a, self->bullet,
541                 self->frame->item_h - 2*PADDING,
542                 self->frame->item_h - 2*PADDING);
543         XMapWindow(ob_display, self->bullet);
544     } else
545         XUnmapWindow(ob_display, self->bullet);
546
547     XFlush(ob_display);
548 }
549
550 static void menu_frame_render(ObMenuFrame *self)
551 {
552     gint w = 0, h = 0;
553     gint tw, th; /* temps */
554     GList *it;
555     gboolean has_icon = FALSE;
556     ObMenu *sub;
557     ObMenuEntryFrame *e;
558
559     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
560     XSetWindowBorder(ob_display, self->window,
561                      RrColorPixel(ob_rr_theme->menu_b_color));
562
563     /* find text dimensions */
564
565     STRUT_SET(self->item_margin, 0, 0, 0, 0);
566
567     if (self->entries) {
568         ObMenuEntryFrame *e = self->entries->data;
569         gint l, t, r, b;
570
571         e->a_text_normal->texture[0].data.text.string = "";
572         RrMinSize(e->a_text_normal, &tw, &th);
573         tw += 2*PADDING;
574         th += 2*PADDING;
575         self->item_h = th;
576
577         RrMargins(e->a_normal, &l, &t, &r, &b);
578         STRUT_SET(self->item_margin,
579                   MAX(self->item_margin.left, l),
580                   MAX(self->item_margin.top, t),
581                   MAX(self->item_margin.right, r),
582                   MAX(self->item_margin.bottom, b));
583         RrMargins(e->a_selected, &l, &t, &r, &b);
584         STRUT_SET(self->item_margin,
585                   MAX(self->item_margin.left, l),
586                   MAX(self->item_margin.top, t),
587                   MAX(self->item_margin.right, r),
588                   MAX(self->item_margin.bottom, b));
589         RrMargins(e->a_disabled, &l, &t, &r, &b);
590         STRUT_SET(self->item_margin,
591                   MAX(self->item_margin.left, l),
592                   MAX(self->item_margin.top, t),
593                   MAX(self->item_margin.right, r),
594                   MAX(self->item_margin.bottom, b));
595     } else
596         self->item_h = 0;
597
598     /* render the entries */
599
600     for (it = self->entries; it; it = g_list_next(it)) {
601         RrAppearance *text_a;
602         e = it->data;
603
604         /* if the first entry is a labeled separator, then make its border
605            overlap with the menu's outside border */
606         if (it == self->entries &&
607             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
608             e->entry->data.separator.label)
609         {
610             h -= ob_rr_theme->mbwidth;
611         }
612
613         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
614             e->entry->data.separator.label)
615         {
616             e->border = ob_rr_theme->mbwidth;
617         }
618
619         RECT_SET_POINT(e->area, 0, h+e->border);
620         XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
621         XSetWindowBorderWidth(ob_display, e->window, e->border);
622         XSetWindowBorder(ob_display, e->window,
623                          RrColorPixel(ob_rr_theme->menu_b_color));
624
625         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
626                    !e->entry->data.normal.enabled) ?
627                   e->a_text_disabled :
628                   (e == self->selected ?
629                    e->a_text_selected :
630                    e->a_text_normal));
631         switch (e->entry->type) {
632         case OB_MENU_ENTRY_TYPE_NORMAL:
633             text_a->texture[0].data.text.string = e->entry->data.normal.label;
634             RrMinSize(text_a, &tw, &th);
635             tw = MIN(tw, MAX_MENU_WIDTH);
636
637             if (e->entry->data.normal.icon_data ||
638                 e->entry->data.normal.mask)
639                 has_icon = TRUE;
640             break;
641         case OB_MENU_ENTRY_TYPE_SUBMENU:
642             sub = e->entry->data.submenu.submenu;
643             text_a->texture[0].data.text.string = sub ? sub->title : "";
644             RrMinSize(text_a, &tw, &th);
645             tw = MIN(tw, MAX_MENU_WIDTH);
646
647             if (e->entry->data.normal.icon_data ||
648                 e->entry->data.normal.mask)
649                 has_icon = TRUE;
650
651             tw += self->item_h - PADDING;
652             break;
653         case OB_MENU_ENTRY_TYPE_SEPARATOR:
654             if (e->entry->data.separator.label != NULL) {
655                 e->a_text_title->texture[0].data.text.string =
656                     e->entry->data.separator.label;
657                 RrMinSize(e->a_text_title, &tw, &th);
658                 tw = MIN(tw, MAX_MENU_WIDTH);
659                 th = ob_rr_theme->menu_title_height +
660                     (ob_rr_theme->mbwidth - PADDING) *2;
661             } else {
662                 tw = 0;
663                 th = SEPARATOR_HEIGHT;
664             }
665             break;
666         }
667         tw += 2*PADDING;
668         th += 2*PADDING;
669         w = MAX(w, tw);
670         h += th;
671     }
672
673     /* if the last entry is a labeled separator, then make its border
674        overlap with the menu's outside border */
675     it = g_list_last(self->entries);
676     e = it ? it->data : NULL;
677     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
678         e->entry->data.separator.label)
679     {
680         h -= ob_rr_theme->mbwidth;
681     }
682
683     self->text_x = PADDING;
684     self->text_w = w;
685
686     if (self->entries) {
687         if (has_icon) {
688             w += self->item_h + PADDING;
689             self->text_x += self->item_h + PADDING;
690         }
691     }
692
693     if (!w) w = 10;
694     if (!h) h = 3;
695
696     XResizeWindow(ob_display, self->window, w, h);
697
698     self->inner_w = w;
699
700     RrPaint(self->a_items, self->window, w, h);
701
702     for (it = self->entries; it; it = g_list_next(it))
703         menu_entry_frame_render(it->data);
704
705     w += ob_rr_theme->mbwidth * 2;
706     h += ob_rr_theme->mbwidth * 2;
707
708     RECT_SET_SIZE(self->area, w, h);
709
710     XFlush(ob_display);
711 }
712
713 static void menu_frame_update(ObMenuFrame *self)
714 {
715     GList *mit, *fit;
716
717     menu_pipe_execute(self->menu);
718     menu_find_submenus(self->menu);
719
720     self->selected = NULL;
721
722     for (mit = self->menu->entries, fit = self->entries; mit && fit;
723          mit = g_list_next(mit), fit = g_list_next(fit))
724     {
725         ObMenuEntryFrame *f = fit->data;
726         f->entry = mit->data;
727     }
728
729     while (mit) {
730         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
731         self->entries = g_list_append(self->entries, e);
732         mit = g_list_next(mit);
733     }
734     
735     while (fit) {
736         GList *n = g_list_next(fit);
737         menu_entry_frame_free(fit->data);
738         self->entries = g_list_delete_link(self->entries, fit);
739         fit = n;
740     }
741
742     menu_frame_render(self);
743 }
744
745 static gboolean menu_frame_is_visible(ObMenuFrame *self)
746 {
747     return !!(g_list_find(menu_frame_visible, self));
748 }
749
750 static gboolean menu_frame_show(ObMenuFrame *self)
751 {
752     GList *it;
753
754     if (menu_frame_visible == NULL) {
755         /* no menus shown yet */
756         if (!grab_pointer(TRUE, TRUE, OB_CURSOR_POINTER))
757             return FALSE;
758         if (!grab_keyboard(TRUE)) {
759             grab_pointer(FALSE, TRUE, OB_CURSOR_POINTER);
760             return FALSE;
761         }
762     }
763
764     /* determine if the underlying menu is already visible */
765     for (it = menu_frame_visible; it; it = g_list_next(it)) {
766         ObMenuFrame *f = it->data;
767         if (f->menu == self->menu)
768             break;
769     }
770     if (!it) {
771         if (self->menu->update_func)
772             self->menu->update_func(self, self->menu->data);
773     }
774
775     menu_frame_update(self);
776
777     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
778
779     return TRUE;
780 }
781
782 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
783                                  gint button)
784 {
785     guint i;
786
787     if (menu_frame_is_visible(self))
788         return TRUE;
789     if (!menu_frame_show(self))
790         return FALSE;
791
792     /* find the monitor the menu is on */
793     for (i = 0; i < screen_num_monitors; ++i) {
794         Rect *a = screen_physical_area_monitor(i);
795         if (RECT_CONTAINS(*a, x, y)) {
796             self->monitor = i;
797             break;
798         }
799     }
800
801     if (self->menu->place_func)
802         self->menu->place_func(self, &x, &y, button, self->menu->data);
803     else
804         menu_frame_place_topmenu(self, &x, &y);
805
806     menu_frame_move(self, x, y);
807
808     XMapWindow(ob_display, self->window);
809
810     return TRUE;
811 }
812
813 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
814                                  ObMenuEntryFrame *parent_entry)
815 {
816     ObMenuEntryFrame *e;
817     gint x, y, dx, dy;
818
819     if (menu_frame_is_visible(self))
820         return TRUE;
821
822     self->monitor = parent->monitor;
823     self->parent = parent;
824     self->parent_entry = parent_entry;
825
826     /* set up parent's child to be us */
827     if (parent->child)
828         menu_frame_hide(parent->child);
829     parent->child = self;
830
831     if (!menu_frame_show(self))
832         return FALSE;
833
834     menu_frame_place_submenu(self, &x, &y);
835     menu_frame_move_on_screen(self, x, y, &dx, &dy);
836
837     if (dx != 0) {
838         /*try the other side */
839         self->direction_right = !self->direction_right;
840         menu_frame_place_submenu(self, &x, &y);
841         menu_frame_move_on_screen(self, x, y, &dx, &dy);
842     }
843     menu_frame_move(self, x + dx, y + dy);
844
845     XMapWindow(ob_display, self->window);
846
847     if (screen_pointer_pos(&dx, &dy) && (e = menu_entry_frame_under(dx, dy)) &&
848         e->frame == self)
849         ++e->ignore_enters;
850
851     return TRUE;
852 }
853
854 void menu_frame_hide(ObMenuFrame *self)
855 {
856     GList *it = g_list_find(menu_frame_visible, self);
857
858     if (!it)
859         return;
860
861     if (self->child)
862         menu_frame_hide(self->child);
863
864     if (self->parent)
865         self->parent->child = NULL;
866     self->parent = NULL;
867     self->parent_entry = NULL;
868
869     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
870
871     if (menu_frame_visible == NULL) {
872         /* last menu shown */
873         grab_pointer(FALSE, TRUE, OB_CURSOR_NONE);
874         grab_keyboard(FALSE);
875     }
876
877     XUnmapWindow(ob_display, self->window);
878
879     menu_frame_free(self);
880 }
881
882 void menu_frame_hide_all()
883 {
884     GList *it;
885
886     if (config_submenu_show_delay) {
887         /* remove any submenu open requests */
888         ob_main_loop_timeout_remove(ob_main_loop,
889                                     menu_entry_frame_submenu_timeout);
890     }
891     if ((it = g_list_last(menu_frame_visible)))
892         menu_frame_hide(it->data);
893 }
894
895 void menu_frame_hide_all_client(ObClient *client)
896 {
897     GList *it = g_list_last(menu_frame_visible);
898     if (it) {
899         ObMenuFrame *f = it->data;
900         if (f->client == client)
901             menu_frame_hide(f);
902     }
903 }
904
905
906 ObMenuFrame* menu_frame_under(gint x, gint y)
907 {
908     ObMenuFrame *ret = NULL;
909     GList *it;
910
911     for (it = menu_frame_visible; it; it = g_list_next(it)) {
912         ObMenuFrame *f = it->data;
913
914         if (RECT_CONTAINS(f->area, x, y)) {
915             ret = f;
916             break;
917         }
918     }
919     return ret;
920 }
921
922 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
923 {
924     ObMenuFrame *frame;
925     ObMenuEntryFrame *ret = NULL;
926     GList *it;
927
928     if ((frame = menu_frame_under(x, y))) {
929         x -= ob_rr_theme->mbwidth + frame->area.x;
930         y -= ob_rr_theme->mbwidth + frame->area.y;
931
932         for (it = frame->entries; it; it = g_list_next(it)) {
933             ObMenuEntryFrame *e = it->data;
934
935             if (RECT_CONTAINS(e->area, x, y)) {
936                 ret = e;            
937                 break;
938             }
939         }
940     }
941     return ret;
942 }
943
944 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
945 {
946     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
947     return FALSE;
948 }
949
950 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
951                        gboolean immediate)
952 {
953     ObMenuEntryFrame *old = self->selected;
954     ObMenuFrame *oldchild = self->child;
955
956     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
957         entry = old;
958
959     if (old == entry) return;
960    
961     if (config_submenu_show_delay) { 
962         /* remove any submenu open requests */
963         ob_main_loop_timeout_remove(ob_main_loop,
964                                     menu_entry_frame_submenu_timeout);
965     }
966
967     self->selected = entry;
968
969     if (old)
970         menu_entry_frame_render(old);
971     if (oldchild)
972         menu_frame_hide(oldchild);
973
974     if (self->selected) {
975         menu_entry_frame_render(self->selected);
976
977         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
978             if (config_submenu_show_delay && !immediate) {
979                 /* initiate a new submenu open request */
980                 ob_main_loop_timeout_add(ob_main_loop,
981                                          config_submenu_show_delay * 1000,
982                                          menu_entry_frame_submenu_timeout,
983                                          self->selected, g_direct_equal,
984                                          NULL);
985             } else {
986                 menu_entry_frame_show_submenu(self->selected);
987             }
988         }
989     }
990 }
991
992 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
993 {
994     ObMenuFrame *f;
995
996     if (!self->entry->data.submenu.submenu) return;
997
998     f = menu_frame_new(self->entry->data.submenu.submenu,
999                        self->frame->client);
1000     /* pass our direction on to our child */
1001     f->direction_right = self->frame->direction_right;
1002
1003     menu_frame_show_submenu(f, self->frame, self);
1004 }
1005
1006 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
1007 {
1008     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1009         self->entry->data.normal.enabled)
1010     {
1011         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1012            gets freed */
1013         ObMenuEntry *entry = self->entry;
1014         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1015         gpointer data = self->frame->menu->data;
1016         GSList *acts = self->entry->data.normal.actions;
1017         ObClient *client = self->frame->client;
1018
1019         /* release grabs before executing the shit */
1020         if (!(state & ControlMask))
1021             menu_frame_hide_all();
1022
1023         if (func)
1024             func(entry, state, data, time);
1025         else
1026             action_run(acts, client, state, time);
1027     }
1028 }
1029
1030 void menu_frame_select_previous(ObMenuFrame *self)
1031 {
1032     GList *it = NULL, *start;
1033
1034     if (self->entries) {
1035         start = it = g_list_find(self->entries, self->selected);
1036         while (TRUE) {
1037             ObMenuEntryFrame *e;
1038
1039             it = it ? g_list_previous(it) : g_list_last(self->entries);
1040             if (it == start)
1041                 break;
1042
1043             if (it) {
1044                 e = it->data;
1045                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1046                     break;
1047                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1048                     e->entry->data.normal.enabled)
1049                     break;
1050             }
1051         }
1052     }
1053     menu_frame_select(self, it ? it->data : NULL, TRUE);
1054 }
1055
1056 void menu_frame_select_next(ObMenuFrame *self)
1057 {
1058     GList *it = NULL, *start;
1059
1060     if (self->entries) {
1061         start = it = g_list_find(self->entries, self->selected);
1062         while (TRUE) {
1063             ObMenuEntryFrame *e;
1064
1065             it = it ? g_list_next(it) : self->entries;
1066             if (it == start)
1067                 break;
1068
1069             if (it) {
1070                 e = it->data;
1071                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1072                     break;
1073                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1074                     e->entry->data.normal.enabled)
1075                     break;
1076             }
1077         }
1078     }
1079     menu_frame_select(self, it ? it->data : NULL, TRUE);
1080 }