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