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