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