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 "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 submenu_show_timeout(gpointer data);
52 static void menu_frame_hide(ObMenuFrame *self);
53
54 static gboolean submenu_hide_timeout(gpointer data);
55
56 static Window createWindow(Window parent, gulong mask,
57                            XSetWindowAttributes *attrib)
58 {
59     return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
60                          RrDepth(ob_rr_inst), InputOutput,
61                          RrVisual(ob_rr_inst), mask, attrib);
62 }
63
64 void menu_frame_startup(gboolean reconfig)
65 {
66     gint i;
67
68     a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
69     RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
70     for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
71         a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
72         a_sep->texture[i].data.lineart.color =
73             ob_rr_theme->menu_sep_color;
74     }
75
76     if (reconfig) return;
77
78     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
79 }
80
81 void menu_frame_shutdown(gboolean reconfig)
82 {
83     RrAppearanceFree(a_sep);
84
85     if (reconfig) return;
86
87     g_hash_table_destroy(menu_frame_map);
88 }
89
90 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
91 {
92     ObMenuFrame *self;
93     XSetWindowAttributes attr;
94
95     self = g_new0(ObMenuFrame, 1);
96     self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
97     self->menu = menu;
98     self->selected = 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 /*! Stop hiding an open submenu.
989     @child The OnMenuFrame of the submenu to be hidden
990 */
991 static void remove_submenu_hide_timeout(ObMenuFrame *child)
992 {
993     obt_main_loop_timeout_remove_data(ob_main_loop, submenu_hide_timeout,
994                                       child, FALSE);
995 }
996
997 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
998                                  ObMenuEntryFrame *parent_entry)
999 {
1000     gint x, y, dx, dy;
1001     gint px, py;
1002
1003     if (menu_frame_is_visible(self))
1004         return TRUE;
1005
1006     self->monitor = parent->monitor;
1007     self->parent = parent;
1008     self->parent_entry = parent_entry;
1009
1010     /* set up parent's child to be us */
1011     if ((parent->child) != self) {
1012         if (parent->child)
1013             menu_frame_hide(parent->child);
1014         parent->child = self;
1015         parent->child_entry = parent_entry;
1016     }
1017
1018     if (!menu_frame_show(self))
1019         return FALSE;
1020
1021     menu_frame_place_submenu(self, &x, &y);
1022     menu_frame_move_on_screen(self, x, y, &dx, &dy);
1023
1024     if (dx != 0) {
1025         /*try the other side */
1026         self->direction_right = !self->direction_right;
1027         menu_frame_place_submenu(self, &x, &y);
1028         menu_frame_move_on_screen(self, x, y, &dx, &dy);
1029     }
1030     menu_frame_move(self, x + dx, y + dy);
1031
1032     XMapWindow(obt_display, self->window);
1033
1034     if (screen_pointer_pos(&px, &py)) {
1035         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1036         if (e && e->frame == self)
1037             e->ignore_enters++;
1038     }
1039
1040     return TRUE;
1041 }
1042
1043 static void menu_frame_hide(ObMenuFrame *self)
1044 {
1045     GList *it = g_list_find(menu_frame_visible, self);
1046     gulong ignore_start;
1047
1048     if (!it)
1049         return;
1050
1051     if (self->menu->hide_func)
1052         self->menu->hide_func(self, self->menu->data);
1053
1054     if (self->child)
1055         menu_frame_hide(self->child);
1056
1057     if (self->parent) {
1058         remove_submenu_hide_timeout(self);
1059
1060         self->parent->child = NULL;
1061         self->parent->child_entry = NULL;
1062     }
1063     self->parent = NULL;
1064     self->parent_entry = NULL;
1065
1066     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1067
1068     if (menu_frame_visible == NULL) {
1069         /* last menu shown */
1070         ungrab_pointer();
1071         ungrab_keyboard();
1072     }
1073
1074     ignore_start = event_start_ignore_all_enters();
1075     XUnmapWindow(obt_display, self->window);
1076     event_end_ignore_all_enters(ignore_start);
1077
1078     menu_frame_free(self);
1079 }
1080
1081 void menu_frame_hide_all(void)
1082 {
1083     GList *it;
1084
1085     if (config_submenu_show_delay) {
1086         /* remove any submenu open requests */
1087         obt_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1088     }
1089     if ((it = g_list_last(menu_frame_visible)))
1090         menu_frame_hide(it->data);
1091 }
1092
1093 void menu_frame_hide_all_client(ObClient *client)
1094 {
1095     GList *it = g_list_last(menu_frame_visible);
1096     if (it) {
1097         ObMenuFrame *f = it->data;
1098         if (f->client == client) {
1099             if (config_submenu_show_delay) {
1100                 /* remove any submenu open requests */
1101                 obt_main_loop_timeout_remove(ob_main_loop,
1102                                              submenu_show_timeout);
1103             }
1104             menu_frame_hide(f);
1105         }
1106     }
1107 }
1108
1109 ObMenuFrame* menu_frame_under(gint x, gint y)
1110 {
1111     ObMenuFrame *ret = NULL;
1112     GList *it;
1113
1114     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1115         ObMenuFrame *f = it->data;
1116
1117         if (RECT_CONTAINS(f->area, x, y)) {
1118             ret = f;
1119             break;
1120         }
1121     }
1122     return ret;
1123 }
1124
1125 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1126 {
1127     ObMenuFrame *frame;
1128     ObMenuEntryFrame *ret = NULL;
1129     GList *it;
1130
1131     if ((frame = menu_frame_under(x, y))) {
1132         x -= ob_rr_theme->mbwidth + frame->area.x;
1133         y -= ob_rr_theme->mbwidth + frame->area.y;
1134
1135         for (it = frame->entries; it; it = g_list_next(it)) {
1136             ObMenuEntryFrame *e = it->data;
1137
1138             if (RECT_CONTAINS(e->area, x, y)) {
1139                 ret = e;
1140                 break;
1141             }
1142         }
1143     }
1144     return ret;
1145 }
1146
1147 static gboolean submenu_show_timeout(gpointer data)
1148 {
1149     g_assert(menu_frame_visible);
1150     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1151     return FALSE;
1152 }
1153
1154 static gboolean submenu_hide_timeout(gpointer data)
1155 {
1156     g_assert(menu_frame_visible);
1157     menu_frame_hide((ObMenuFrame*)data);
1158     return FALSE;
1159 }
1160
1161 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1162                        gboolean immediate)
1163 {
1164     ObMenuEntryFrame *old = self->selected;
1165     ObMenuFrame *oldchild = self->child;
1166     ObMenuEntryFrame *oldchild_entry = self->child_entry;
1167
1168     /* if the user selected a separator, ignore it and reselect what we had
1169        selected before */
1170     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1171         entry = old;
1172
1173     if (old == entry &&
1174         (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1175         return;
1176
1177     /* if the user left this menu but we have a submenu open, move the
1178        selection back to that submenu */
1179     if (!entry && oldchild_entry)
1180         entry = oldchild_entry;
1181
1182     if (config_submenu_show_delay) {
1183         /* remove any submenu open requests */
1184         obt_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1185     }
1186
1187     self->selected = entry;
1188
1189     if (old)
1190         menu_entry_frame_render(old);
1191
1192     if (oldchild_entry) {
1193         /* There is an open submenu */
1194         if (oldchild_entry == self->selected) {
1195             /* The open submenu has been reselected, so stop hiding the
1196                submenu */
1197             remove_submenu_hide_timeout(oldchild);
1198         }
1199         else if (oldchild_entry == old) {
1200             /* The open submenu was selected and is no longer, so hide the
1201                submenu */
1202             if (immediate || config_submenu_hide_delay == 0)
1203                 menu_frame_hide(oldchild);
1204             else if (config_submenu_hide_delay > 0)
1205                 obt_main_loop_timeout_add(ob_main_loop,
1206                                           config_submenu_hide_delay * 1000,
1207                                           submenu_hide_timeout,
1208                                           oldchild, g_direct_equal,
1209                                           NULL);
1210         }
1211     }
1212
1213     if (self->selected) {
1214         menu_entry_frame_render(self->selected);
1215
1216         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1217             /* only show if the submenu isn't already showing */
1218             if (oldchild_entry != self->selected) {
1219                 if (immediate || config_submenu_hide_delay == 0)
1220                     menu_entry_frame_show_submenu(self->selected);
1221                 else if (config_submenu_hide_delay > 0)
1222                     obt_main_loop_timeout_add(ob_main_loop,
1223                                               config_submenu_show_delay * 1000,
1224                                               submenu_show_timeout,
1225                                               self->selected, g_direct_equal,
1226                                               NULL);
1227             }
1228             /* hide the grandchildren of this menu. and move the cursor to
1229                the current menu */
1230             else if (immediate && self->child && self->child->child) {
1231                 menu_frame_hide(self->child->child);
1232                 menu_frame_select(self->child, NULL, TRUE);
1233             }
1234         }
1235     }
1236 }
1237
1238 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1239 {
1240     ObMenuFrame *f;
1241
1242     if (!self->entry->data.submenu.submenu) return;
1243
1244     f = menu_frame_new(self->entry->data.submenu.submenu,
1245                        self->entry->data.submenu.show_from,
1246                        self->frame->client);
1247     /* pass our direction on to our child */
1248     f->direction_right = self->frame->direction_right;
1249
1250     menu_frame_show_submenu(f, self->frame, self);
1251 }
1252
1253 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1254 {
1255     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1256         self->entry->data.normal.enabled)
1257     {
1258         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1259            gets freed */
1260         ObMenuEntry *entry = self->entry;
1261         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1262         gpointer data = self->frame->menu->data;
1263         GSList *acts = self->entry->data.normal.actions;
1264         ObClient *client = self->frame->client;
1265         ObMenuFrame *frame = self->frame;
1266
1267         /* release grabs before executing the shit */
1268         if (!(state & ControlMask)) {
1269             menu_frame_hide_all();
1270             frame = NULL;
1271         }
1272
1273         if (func)
1274             func(entry, frame, client, state, data);
1275         else
1276             actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1277                              state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1278     }
1279 }
1280
1281 void menu_frame_select_previous(ObMenuFrame *self)
1282 {
1283     GList *it = NULL, *start;
1284
1285     if (self->entries) {
1286         start = it = g_list_find(self->entries, self->selected);
1287         while (TRUE) {
1288             ObMenuEntryFrame *e;
1289
1290             it = it ? g_list_previous(it) : g_list_last(self->entries);
1291             if (it == start)
1292                 break;
1293
1294             if (it) {
1295                 e = it->data;
1296                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1297                     break;
1298                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1299                     break;
1300             }
1301         }
1302     }
1303     menu_frame_select(self, it ? it->data : NULL, TRUE);
1304 }
1305
1306 void menu_frame_select_next(ObMenuFrame *self)
1307 {
1308     GList *it = NULL, *start;
1309
1310     if (self->entries) {
1311         start = it = g_list_find(self->entries, self->selected);
1312         while (TRUE) {
1313             ObMenuEntryFrame *e;
1314
1315             it = it ? g_list_next(it) : self->entries;
1316             if (it == start)
1317                 break;
1318
1319             if (it) {
1320                 e = it->data;
1321                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1322                     break;
1323                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1324                     break;
1325             }
1326         }
1327     }
1328     menu_frame_select(self, it ? it->data : NULL, TRUE);
1329 }