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