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