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