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