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