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