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