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