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