]> icculus.org git repositories - dana/openbox.git/blob - openbox/menuframe.c
small change to the render API. RrMinsize became RrMinSize. added RrMinWidth and...
[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 FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
35                          LeaveWindowMask)
36 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
37                          ButtonPressMask | ButtonReleaseMask)
38
39 GList *menu_frame_visible;
40
41 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
42                                               ObMenuFrame *frame);
43 static void menu_entry_frame_free(ObMenuEntryFrame *self);
44 static void menu_frame_render(ObMenuFrame *self);
45 static void menu_frame_update(ObMenuFrame *self);
46 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
47
48 static Window createWindow(Window parent, gulong mask,
49                            XSetWindowAttributes *attrib)
50 {
51     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
52                          RrDepth(ob_rr_inst), InputOutput,
53                          RrVisual(ob_rr_inst), mask, attrib);
54 }
55
56 GHashTable *menu_frame_map;
57
58 void menu_frame_startup(gboolean reconfig)
59 {
60     if (reconfig) return;
61
62     menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
63 }
64
65 void menu_frame_shutdown(gboolean reconfig)
66 {
67     if (reconfig) return;
68
69     g_hash_table_destroy(menu_frame_map);
70 }
71
72 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
73 {
74     ObMenuFrame *self;
75     XSetWindowAttributes attr;
76
77     self = g_new0(ObMenuFrame, 1);
78     self->type = Window_Menu;
79     self->menu = menu;
80     self->selected = NULL;
81     self->client = client;
82     self->direction_right = TRUE;
83
84     attr.event_mask = FRAME_EVENTMASK;
85     self->window = createWindow(RootWindow(ob_display, ob_screen),
86                                 CWEventMask, &attr);
87
88     self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
89     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
90
91     stacking_add(MENU_AS_WINDOW(self));
92
93     return self;
94 }
95
96 void menu_frame_free(ObMenuFrame *self)
97 {
98     if (self) {
99         while (self->entries) {
100             menu_entry_frame_free(self->entries->data);
101             self->entries = g_list_delete_link(self->entries, self->entries);
102         }
103
104         stacking_remove(MENU_AS_WINDOW(self));
105
106         XDestroyWindow(ob_display, self->window);
107
108         RrAppearanceFree(self->a_items);
109         RrAppearanceFree(self->a_title);
110
111         g_free(self);
112     }
113 }
114
115 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
116                                               ObMenuFrame *frame)
117 {
118     ObMenuEntryFrame *self;
119     XSetWindowAttributes attr;
120
121     self = g_new0(ObMenuEntryFrame, 1);
122     self->entry = entry;
123     self->frame = frame;
124
125     attr.event_mask = ENTRY_EVENTMASK;
126     self->window = createWindow(self->frame->window, CWEventMask, &attr);
127     self->text = createWindow(self->window, 0, NULL);
128     g_hash_table_insert(menu_frame_map, &self->window, self);
129     g_hash_table_insert(menu_frame_map, &self->text, self);
130     if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
131         self->icon = createWindow(self->window, 0, NULL);
132         g_hash_table_insert(menu_frame_map, &self->icon, self);
133     }
134     if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
135         self->bullet = createWindow(self->window, 0, NULL);
136         g_hash_table_insert(menu_frame_map, &self->bullet, self);
137     }
138
139     XMapWindow(ob_display, self->window);
140     XMapWindow(ob_display, self->text);
141
142     self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
143     self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
144     self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
145
146     if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
147         self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
148         self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
149     } else {
150         self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
151         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
152         self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
153         self->a_mask->texture[0].type = RR_TEXTURE_MASK;
154         self->a_bullet_normal =
155             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
156         self->a_bullet_selected =
157             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
158     }
159
160     self->a_text_normal =
161         RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
162     self->a_text_disabled =
163         RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
164     self->a_text_selected =
165         RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
166     self->a_text_title =
167         RrAppearanceCopy(ob_rr_theme->a_menu_text_title);
168
169     return self;
170 }
171
172 static void menu_entry_frame_free(ObMenuEntryFrame *self)
173 {
174     if (self) {
175         XDestroyWindow(ob_display, self->text);
176         XDestroyWindow(ob_display, self->window);
177         g_hash_table_remove(menu_frame_map, &self->text);
178         g_hash_table_remove(menu_frame_map, &self->window);
179         if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
180             XDestroyWindow(ob_display, self->icon);
181             g_hash_table_remove(menu_frame_map, &self->icon);
182         }
183         if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
184             XDestroyWindow(ob_display, self->bullet);
185             g_hash_table_remove(menu_frame_map, &self->bullet);
186         }
187
188         RrAppearanceFree(self->a_normal);
189         RrAppearanceFree(self->a_disabled);
190         RrAppearanceFree(self->a_selected);
191
192         RrAppearanceFree(self->a_separator);
193         RrAppearanceFree(self->a_icon);
194         RrAppearanceFree(self->a_mask);
195         RrAppearanceFree(self->a_text_normal);
196         RrAppearanceFree(self->a_text_disabled);
197         RrAppearanceFree(self->a_text_selected);
198         RrAppearanceFree(self->a_text_title);
199         RrAppearanceFree(self->a_bullet_normal);
200         RrAppearanceFree(self->a_bullet_selected);
201
202         g_free(self);
203     }
204 }
205
206 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
207 {
208     RECT_SET_POINT(self->area, x, y);
209     XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
210 }
211
212 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
213 {
214     gint dx, dy;
215
216     if (config_menu_middle) {
217         gint myx;
218
219         myx = *x;
220         *y -= self->area.height / 2;
221
222         /* try to the right of the cursor */
223         menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
224         if (dx != 0) {
225             /* try to the left of the cursor */
226             myx = *x - self->area.width;
227             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
228         }
229         if (dx != 0) {
230             /* if didnt fit on either side so just use what it says */
231             myx = *x;
232             menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
233         }
234         *x = myx + dx;
235         *y += dy;
236     } else {
237         gint myx, myy;
238
239         myx = *x;
240         myy = *y;
241
242         /* try to the bottom right of the cursor */
243         menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
244         if (dx != 0 || dy != 0) {
245             /* try to the bottom left of the cursor */
246             myx = *x - self->area.width;
247             myy = *y;
248             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
249         }
250         if (dx != 0 || dy != 0) {
251             /* try to the top right of the cursor */
252             myx = *x;
253             myy = *y - self->area.height;
254             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
255         }
256         if (dx != 0 || dy != 0) {
257             /* try to the top left of the cursor */
258             myx = *x - self->area.width;
259             myy = *y - self->area.height;
260             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
261         }
262         if (dx != 0 || dy != 0) {
263             /* if didnt fit on either side so just use what it says */
264             myx = *x;
265             myy = *y;
266             menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267         }
268         *x = myx + dx;
269         *y = myy + dy;
270     }
271 }
272
273 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
274 {
275     gint overlap;
276     gint bwidth;
277
278     overlap = ob_rr_theme->menu_overlap;
279     bwidth = ob_rr_theme->mbwidth;
280
281     if (self->direction_right)
282         *x = self->parent->area.x + self->parent->area.width -
283             overlap - bwidth;
284     else
285         *x = self->parent->area.x - self->area.width + overlap + bwidth;
286
287     *y = self->parent->area.y + self->parent_entry->area.y;
288     if (config_menu_middle)
289         *y -= (self->area.height - (bwidth * 2) - self->item_h) / 2;
290     else
291         *y += overlap;
292 }
293
294 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
295                                gint *dx, gint *dy)
296 {
297     Rect *a = NULL;
298     gint pos, half;
299
300     *dx = *dy = 0;
301
302     a = screen_physical_area_monitor(self->monitor);
303
304     half = g_list_length(self->entries) / 2;
305     pos = g_list_index(self->entries, self->selected);
306
307     /* if in the bottom half then check this stuff first, will keep the bottom
308        edge of the menu visible */
309     if (pos > half) {
310         *dx = MAX(*dx, a->x - x);
311         *dy = MAX(*dy, a->y - y);
312     }
313     *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
314     *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
315     /* if in the top half then check this stuff last, will keep the top
316        edge of the menu visible */
317     if (pos <= half) {
318         *dx = MAX(*dx, a->x - x);
319         *dy = MAX(*dy, a->y - y);
320     }
321 }
322
323 static void menu_entry_frame_render(ObMenuEntryFrame *self)
324 {
325     RrAppearance *item_a, *text_a;
326     gint th; /* temp */
327     ObMenu *sub;
328     ObMenuFrame *frame = self->frame;
329
330     switch (self->entry->type) {
331     case OB_MENU_ENTRY_TYPE_NORMAL:
332     case OB_MENU_ENTRY_TYPE_SUBMENU:
333         item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
334                    !self->entry->data.normal.enabled) ?
335                   self->a_disabled :
336                   (self == self->frame->selected ?
337                    self->a_selected :
338                    self->a_normal));
339         th = self->frame->item_h;
340         break;
341     case OB_MENU_ENTRY_TYPE_SEPARATOR:
342         if (self->entry->data.separator.label) {
343             item_a = self->frame->a_title;
344             th = ob_rr_theme->menu_title_height;
345         } else {
346             item_a = self->a_normal;
347             th = SEPARATOR_HEIGHT + 2*PADDING;
348         }
349         break;
350     default:
351         g_assert_not_reached();
352     }
353     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
354     XResizeWindow(ob_display, self->window,
355                   self->area.width, self->area.height);
356     item_a->surface.parent = self->frame->a_items;
357     item_a->surface.parentx = self->area.x;
358     item_a->surface.parenty = self->area.y;
359     RrPaint(item_a, self->window, self->area.width, self->area.height);
360
361     switch (self->entry->type) {
362     case OB_MENU_ENTRY_TYPE_NORMAL:
363         text_a = (!self->entry->data.normal.enabled ?
364                   self->a_text_disabled :
365                   (self == self->frame->selected ?
366                    self->a_text_selected :
367                    self->a_text_normal));
368         text_a->texture[0].data.text.string = self->entry->data.normal.label;
369         if (self->entry->data.normal.shortcut &&
370             (self->frame->menu->show_all_shortcuts ||
371              self->entry->data.normal.shortcut_position > 0))
372         {
373             text_a->texture[0].data.text.shortcut = TRUE;
374             text_a->texture[0].data.text.shortcut_pos =
375                 self->entry->data.normal.shortcut_position;
376         } else
377             text_a->texture[0].data.text.shortcut = FALSE;
378         break;
379     case OB_MENU_ENTRY_TYPE_SUBMENU:
380         text_a = (self == self->frame->selected ?
381                   self->a_text_selected :
382                   self->a_text_normal);
383         sub = self->entry->data.submenu.submenu;
384         text_a->texture[0].data.text.string = sub ? sub->title : "";
385         if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
386                               sub->shortcut_position > 0))
387         {
388             text_a->texture[0].data.text.shortcut = TRUE;
389             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
390         } else
391             text_a->texture[0].data.text.shortcut = FALSE;
392         break;
393     case OB_MENU_ENTRY_TYPE_SEPARATOR:
394         if (self->entry->data.separator.label != NULL)
395             text_a = self->a_text_title;
396         else
397             text_a = self->a_text_normal;
398         break;
399     }
400
401     switch (self->entry->type) {
402     case OB_MENU_ENTRY_TYPE_NORMAL:
403         XMoveResizeWindow(ob_display, self->text,
404                           self->frame->text_x, PADDING,
405                           self->frame->text_w,
406                           self->frame->item_h - 2*PADDING);
407         text_a->surface.parent = item_a;
408         text_a->surface.parentx = self->frame->text_x;
409         text_a->surface.parenty = PADDING;
410         RrPaint(text_a, self->text, self->frame->text_w,
411                 self->frame->item_h - 2*PADDING);
412         break;
413     case OB_MENU_ENTRY_TYPE_SUBMENU:
414         XMoveResizeWindow(ob_display, self->text,
415                           self->frame->text_x, PADDING,
416                           self->frame->text_w - self->frame->item_h,
417                           self->frame->item_h - 2*PADDING);
418         text_a->surface.parent = item_a;
419         text_a->surface.parentx = self->frame->text_x;
420         text_a->surface.parenty = PADDING;
421         RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
422                 self->frame->item_h - 2*PADDING);
423         break;
424     case OB_MENU_ENTRY_TYPE_SEPARATOR:
425         if (self->entry->data.separator.label != NULL) {
426             /* labeled separator */
427             XMoveResizeWindow(ob_display, self->text,
428                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
429                               self->area.width - 2*ob_rr_theme->paddingx,
430                               ob_rr_theme->menu_title_height -
431                               2*ob_rr_theme->paddingy);
432             text_a->surface.parent = item_a;
433             text_a->surface.parentx = ob_rr_theme->paddingx;
434             text_a->surface.parenty = ob_rr_theme->paddingy;
435             RrPaint(text_a, self->text,
436                     self->area.width - 2*ob_rr_theme->paddingx,
437                     ob_rr_theme->menu_title_height -
438                     2*ob_rr_theme->paddingy);
439         } else {
440             /* unlabeled separaator */
441             XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
442                               self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
443             self->a_separator->surface.parent = item_a;
444             self->a_separator->surface.parentx = PADDING;
445             self->a_separator->surface.parenty = PADDING;
446             self->a_separator->texture[0].data.lineart.color =
447                 text_a->texture[0].data.text.color;
448             self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
449             self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
450             self->a_separator->texture[0].data.lineart.x2 =
451                 self->area.width - 4*PADDING;
452             self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
453             RrPaint(self->a_separator, self->text,
454                     self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
455         }
456         break;
457     }
458
459     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
460         self->entry->data.normal.icon_data)
461     {
462         XMoveResizeWindow(ob_display, self->icon,
463                           PADDING, frame->item_margin.top,
464                           self->frame->item_h - frame->item_margin.top
465                           - frame->item_margin.bottom,
466                           self->frame->item_h - frame->item_margin.top
467                           - frame->item_margin.bottom);
468         self->a_icon->texture[0].data.rgba.width =
469             self->entry->data.normal.icon_width;
470         self->a_icon->texture[0].data.rgba.height =
471             self->entry->data.normal.icon_height;
472         self->a_icon->texture[0].data.rgba.data =
473             self->entry->data.normal.icon_data;
474         self->a_icon->surface.parent = item_a;
475         self->a_icon->surface.parentx = PADDING;
476         self->a_icon->surface.parenty = frame->item_margin.top;
477         RrPaint(self->a_icon, self->icon,
478                 self->frame->item_h - frame->item_margin.top
479                 - frame->item_margin.bottom,
480                 self->frame->item_h - frame->item_margin.top
481                 - frame->item_margin.bottom);
482         XMapWindow(ob_display, self->icon);
483     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
484                self->entry->data.normal.mask)
485     {
486         RrColor *c;
487
488         XMoveResizeWindow(ob_display, self->icon,
489                           PADDING, frame->item_margin.top,
490                           self->frame->item_h - frame->item_margin.top
491                           - frame->item_margin.bottom,
492                           self->frame->item_h - frame->item_margin.top
493                           - frame->item_margin.bottom);
494         self->a_mask->texture[0].data.mask.mask =
495             self->entry->data.normal.mask;
496
497         c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
498               !self->entry->data.normal.enabled) ?
499              self->entry->data.normal.mask_disabled_color :
500              (self == self->frame->selected ?
501               self->entry->data.normal.mask_selected_color :
502               self->entry->data.normal.mask_normal_color));
503         self->a_mask->texture[0].data.mask.color = c;
504
505         self->a_mask->surface.parent = item_a;
506         self->a_mask->surface.parentx = PADDING;
507         self->a_mask->surface.parenty = frame->item_margin.top;
508         RrPaint(self->a_mask, self->icon,
509                 self->frame->item_h - frame->item_margin.top
510                 - frame->item_margin.bottom,
511                 self->frame->item_h - frame->item_margin.top
512                 - frame->item_margin.bottom);
513         XMapWindow(ob_display, self->icon);
514     } else
515         XUnmapWindow(ob_display, self->icon);
516
517     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
518         RrAppearance *bullet_a;
519         XMoveResizeWindow(ob_display, self->bullet,
520                           self->frame->text_x + self->frame->text_w
521                           - self->frame->item_h + PADDING, PADDING,
522                           self->frame->item_h - 2*PADDING,
523                           self->frame->item_h - 2*PADDING);
524         bullet_a = (self == self->frame->selected ?
525                     self->a_bullet_selected :
526                     self->a_bullet_normal);
527         bullet_a->surface.parent = item_a;
528         bullet_a->surface.parentx =
529             self->frame->text_x + self->frame->text_w - self->frame->item_h
530             + PADDING;
531         bullet_a->surface.parenty = PADDING;
532         RrPaint(bullet_a, self->bullet,
533                 self->frame->item_h - 2*PADDING,
534                 self->frame->item_h - 2*PADDING);
535         XMapWindow(ob_display, self->bullet);
536     } else
537         XUnmapWindow(ob_display, self->bullet);
538
539     XFlush(ob_display);
540 }
541
542 static void menu_frame_render(ObMenuFrame *self)
543 {
544     gint w = 0, h = 0;
545     gint tw, th; /* temps */
546     GList *it;
547     gboolean has_icon = FALSE;
548     ObMenu *sub;
549     ObMenuEntryFrame *e;
550
551     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
552     XSetWindowBorder(ob_display, self->window,
553                      RrColorPixel(ob_rr_theme->menu_b_color));
554
555     /* find text dimensions */
556
557     STRUT_SET(self->item_margin, 0, 0, 0, 0);
558
559     if (self->entries) {
560         ObMenuEntryFrame *e = self->entries->data;
561         gint l, t, r, b;
562
563         e->a_text_normal->texture[0].data.text.string = "";
564         RrMinSize(e->a_text_normal, &tw, &th);
565         tw += 2*PADDING;
566         th += 2*PADDING;
567         self->item_h = th;
568
569         RrMargins(e->a_normal, &l, &t, &r, &b);
570         STRUT_SET(self->item_margin,
571                   MAX(self->item_margin.left, l),
572                   MAX(self->item_margin.top, t),
573                   MAX(self->item_margin.right, r),
574                   MAX(self->item_margin.bottom, b));
575         RrMargins(e->a_selected, &l, &t, &r, &b);
576         STRUT_SET(self->item_margin,
577                   MAX(self->item_margin.left, l),
578                   MAX(self->item_margin.top, t),
579                   MAX(self->item_margin.right, r),
580                   MAX(self->item_margin.bottom, b));
581         RrMargins(e->a_disabled, &l, &t, &r, &b);
582         STRUT_SET(self->item_margin,
583                   MAX(self->item_margin.left, l),
584                   MAX(self->item_margin.top, t),
585                   MAX(self->item_margin.right, r),
586                   MAX(self->item_margin.bottom, b));
587     } else
588         self->item_h = 0;
589
590     /* render the entries */
591
592     for (it = self->entries; it; it = g_list_next(it)) {
593         RrAppearance *text_a;
594         e = it->data;
595
596         /* if the first entry is a labeled separator, then make its border
597            overlap with the menu's outside border */
598         if (it == self->entries &&
599             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
600             e->entry->data.separator.label)
601         {
602             h -= ob_rr_theme->mbwidth;
603         }
604
605         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
606             e->entry->data.separator.label)
607         {
608             e->border = ob_rr_theme->mbwidth;
609         }
610
611         RECT_SET_POINT(e->area, 0, h+e->border);
612         XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
613         XSetWindowBorderWidth(ob_display, e->window, e->border);
614         XSetWindowBorder(ob_display, e->window,
615                          RrColorPixel(ob_rr_theme->menu_b_color));
616
617         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
618                    !e->entry->data.normal.enabled) ?
619                   e->a_text_disabled :
620                   (e == self->selected ?
621                    e->a_text_selected :
622                    e->a_text_normal));
623         switch (e->entry->type) {
624         case OB_MENU_ENTRY_TYPE_NORMAL:
625             text_a->texture[0].data.text.string = e->entry->data.normal.label;
626             RrMinSize(text_a, &tw, &th);
627             tw = MIN(tw, MAX_MENU_WIDTH);
628
629             if (e->entry->data.normal.icon_data ||
630                 e->entry->data.normal.mask)
631                 has_icon = TRUE;
632             break;
633         case OB_MENU_ENTRY_TYPE_SUBMENU:
634             sub = e->entry->data.submenu.submenu;
635             text_a->texture[0].data.text.string = sub ? sub->title : "";
636             RrMinSize(text_a, &tw, &th);
637             tw = MIN(tw, MAX_MENU_WIDTH);
638
639             if (e->entry->data.normal.icon_data ||
640                 e->entry->data.normal.mask)
641                 has_icon = TRUE;
642
643             tw += self->item_h - PADDING;
644             break;
645         case OB_MENU_ENTRY_TYPE_SEPARATOR:
646             if (e->entry->data.separator.label != NULL) {
647                 e->a_text_title->texture[0].data.text.string =
648                     e->entry->data.separator.label;
649                 RrMinSize(e->a_text_title, &tw, &th);
650                 tw = MIN(tw, MAX_MENU_WIDTH);
651                 th = ob_rr_theme->menu_title_height +
652                     (ob_rr_theme->mbwidth - PADDING) *2;
653             } else {
654                 tw = 0;
655                 th = SEPARATOR_HEIGHT;
656             }
657             break;
658         }
659         tw += 2*PADDING;
660         th += 2*PADDING;
661         w = MAX(w, tw);
662         h += th;
663     }
664
665     /* if the last entry is a labeled separator, then make its border
666        overlap with the menu's outside border */
667     it = g_list_last(self->entries);
668     e = it ? it->data : NULL;
669     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
670         e->entry->data.separator.label)
671     {
672         h -= ob_rr_theme->mbwidth;
673     }
674
675     self->text_x = PADDING;
676     self->text_w = w;
677
678     if (self->entries) {
679         if (has_icon) {
680             w += self->item_h + PADDING;
681             self->text_x += self->item_h + PADDING;
682         }
683     }
684
685     if (!w) w = 10;
686     if (!h) h = 3;
687
688     XResizeWindow(ob_display, self->window, w, h);
689
690     self->inner_w = w;
691
692     RrPaint(self->a_items, self->window, w, h);
693
694     for (it = self->entries; it; it = g_list_next(it))
695         menu_entry_frame_render(it->data);
696
697     w += ob_rr_theme->mbwidth * 2;
698     h += ob_rr_theme->mbwidth * 2;
699
700     RECT_SET_SIZE(self->area, w, h);
701
702     XFlush(ob_display);
703 }
704
705 static void menu_frame_update(ObMenuFrame *self)
706 {
707     GList *mit, *fit;
708
709     menu_pipe_execute(self->menu);
710     menu_find_submenus(self->menu);
711
712     self->selected = NULL;
713
714     for (mit = self->menu->entries, fit = self->entries; mit && fit;
715          mit = g_list_next(mit), fit = g_list_next(fit))
716     {
717         ObMenuEntryFrame *f = fit->data;
718         f->entry = mit->data;
719     }
720
721     while (mit) {
722         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
723         self->entries = g_list_append(self->entries, e);
724         mit = g_list_next(mit);
725     }
726     
727     while (fit) {
728         GList *n = g_list_next(fit);
729         menu_entry_frame_free(fit->data);
730         self->entries = g_list_delete_link(self->entries, fit);
731         fit = n;
732     }
733
734     menu_frame_render(self);
735 }
736
737 static gboolean menu_frame_is_visible(ObMenuFrame *self)
738 {
739     return !!(g_list_find(menu_frame_visible, self));
740 }
741
742 static gboolean menu_frame_show(ObMenuFrame *self)
743 {
744     GList *it;
745
746     if (menu_frame_visible == NULL) {
747         /* no menus shown yet */
748         if (!grab_pointer(TRUE, TRUE, OB_CURSOR_POINTER))
749             return FALSE;
750         if (!grab_keyboard(TRUE)) {
751             grab_pointer(FALSE, TRUE, OB_CURSOR_POINTER);
752             return FALSE;
753         }
754     }
755
756     /* determine if the underlying menu is already visible */
757     for (it = menu_frame_visible; it; it = g_list_next(it)) {
758         ObMenuFrame *f = it->data;
759         if (f->menu == self->menu)
760             break;
761     }
762     if (!it) {
763         if (self->menu->update_func)
764             self->menu->update_func(self, self->menu->data);
765     }
766
767     menu_frame_update(self);
768
769     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
770
771     return TRUE;
772 }
773
774 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
775                                  gint button)
776 {
777     guint i;
778
779     if (menu_frame_is_visible(self))
780         return TRUE;
781     if (!menu_frame_show(self))
782         return FALSE;
783
784     /* find the monitor the menu is on */
785     for (i = 0; i < screen_num_monitors; ++i) {
786         Rect *a = screen_physical_area_monitor(i);
787         if (RECT_CONTAINS(*a, x, y)) {
788             self->monitor = i;
789             break;
790         }
791     }
792
793     if (self->menu->place_func)
794         self->menu->place_func(self, &x, &y, button, self->menu->data);
795     else
796         menu_frame_place_topmenu(self, &x, &y);
797
798     menu_frame_move(self, x, y);
799
800     XMapWindow(ob_display, self->window);
801
802     return TRUE;
803 }
804
805 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
806                                  ObMenuEntryFrame *parent_entry)
807 {
808     ObMenuEntryFrame *e;
809     gint x, y, dx, dy;
810
811     if (menu_frame_is_visible(self))
812         return TRUE;
813
814     self->monitor = parent->monitor;
815     self->parent = parent;
816     self->parent_entry = parent_entry;
817
818     /* set up parent's child to be us */
819     if (parent->child)
820         menu_frame_hide(parent->child);
821     parent->child = self;
822
823     if (!menu_frame_show(self))
824         return FALSE;
825
826     menu_frame_place_submenu(self, &x, &y);
827     menu_frame_move_on_screen(self, x, y, &dx, &dy);
828
829     if (dx != 0) {
830         /*try the other side */
831         self->direction_right = !self->direction_right;
832         menu_frame_place_submenu(self, &x, &y);
833         menu_frame_move_on_screen(self, x, y, &dx, &dy);
834     }
835     menu_frame_move(self, x + dx, y + dy);
836
837     XMapWindow(ob_display, self->window);
838
839     if (screen_pointer_pos(&dx, &dy) && (e = menu_entry_frame_under(dx, dy)) &&
840         e->frame == self)
841         ++e->ignore_enters;
842
843     return TRUE;
844 }
845
846 void menu_frame_hide(ObMenuFrame *self)
847 {
848     GList *it = g_list_find(menu_frame_visible, self);
849
850     if (!it)
851         return;
852
853     if (self->child)
854         menu_frame_hide(self->child);
855
856     if (self->parent)
857         self->parent->child = NULL;
858     self->parent = NULL;
859     self->parent_entry = NULL;
860
861     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
862
863     if (menu_frame_visible == NULL) {
864         /* last menu shown */
865         grab_pointer(FALSE, TRUE, OB_CURSOR_NONE);
866         grab_keyboard(FALSE);
867     }
868
869     XUnmapWindow(ob_display, self->window);
870
871     menu_frame_free(self);
872 }
873
874 void menu_frame_hide_all()
875 {
876     GList *it;
877
878     if (config_submenu_show_delay) {
879         /* remove any submenu open requests */
880         ob_main_loop_timeout_remove(ob_main_loop,
881                                     menu_entry_frame_submenu_timeout);
882     }
883     if ((it = g_list_last(menu_frame_visible)))
884         menu_frame_hide(it->data);
885 }
886
887 void menu_frame_hide_all_client(ObClient *client)
888 {
889     GList *it = g_list_last(menu_frame_visible);
890     if (it) {
891         ObMenuFrame *f = it->data;
892         if (f->client == client)
893             menu_frame_hide(f);
894     }
895 }
896
897
898 ObMenuFrame* menu_frame_under(gint x, gint y)
899 {
900     ObMenuFrame *ret = NULL;
901     GList *it;
902
903     for (it = menu_frame_visible; it; it = g_list_next(it)) {
904         ObMenuFrame *f = it->data;
905
906         if (RECT_CONTAINS(f->area, x, y)) {
907             ret = f;
908             break;
909         }
910     }
911     return ret;
912 }
913
914 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
915 {
916     ObMenuFrame *frame;
917     ObMenuEntryFrame *ret = NULL;
918     GList *it;
919
920     if ((frame = menu_frame_under(x, y))) {
921         x -= ob_rr_theme->mbwidth + frame->area.x;
922         y -= ob_rr_theme->mbwidth + frame->area.y;
923
924         for (it = frame->entries; it; it = g_list_next(it)) {
925             ObMenuEntryFrame *e = it->data;
926
927             if (RECT_CONTAINS(e->area, x, y)) {
928                 ret = e;            
929                 break;
930             }
931         }
932     }
933     return ret;
934 }
935
936 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
937 {
938     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
939     return FALSE;
940 }
941
942 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
943                        gboolean immediate)
944 {
945     ObMenuEntryFrame *old = self->selected;
946     ObMenuFrame *oldchild = self->child;
947
948     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
949         entry = old;
950
951     if (old == entry) return;
952    
953     if (config_submenu_show_delay) { 
954         /* remove any submenu open requests */
955         ob_main_loop_timeout_remove(ob_main_loop,
956                                     menu_entry_frame_submenu_timeout);
957     }
958
959     self->selected = entry;
960
961     if (old)
962         menu_entry_frame_render(old);
963     if (oldchild)
964         menu_frame_hide(oldchild);
965
966     if (self->selected) {
967         menu_entry_frame_render(self->selected);
968
969         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
970             if (config_submenu_show_delay && !immediate) {
971                 /* initiate a new submenu open request */
972                 ob_main_loop_timeout_add(ob_main_loop,
973                                          config_submenu_show_delay * 1000,
974                                          menu_entry_frame_submenu_timeout,
975                                          self->selected, g_direct_equal,
976                                          NULL);
977             } else {
978                 menu_entry_frame_show_submenu(self->selected);
979             }
980         }
981     }
982 }
983
984 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
985 {
986     ObMenuFrame *f;
987
988     if (!self->entry->data.submenu.submenu) return;
989
990     f = menu_frame_new(self->entry->data.submenu.submenu,
991                        self->frame->client);
992     /* pass our direction on to our child */
993     f->direction_right = self->frame->direction_right;
994
995     menu_frame_show_submenu(f, self->frame, self);
996 }
997
998 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
999 {
1000     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1001         self->entry->data.normal.enabled)
1002     {
1003         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1004            gets freed */
1005         ObMenuEntry *entry = self->entry;
1006         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1007         gpointer data = self->frame->menu->data;
1008         GSList *acts = self->entry->data.normal.actions;
1009         ObClient *client = self->frame->client;
1010
1011         /* release grabs before executing the shit */
1012         if (!(state & ControlMask))
1013             menu_frame_hide_all();
1014
1015         if (func)
1016             func(entry, state, data, time);
1017         else
1018             action_run(acts, client, state, time);
1019     }
1020 }
1021
1022 void menu_frame_select_previous(ObMenuFrame *self)
1023 {
1024     GList *it = NULL, *start;
1025
1026     if (self->entries) {
1027         start = it = g_list_find(self->entries, self->selected);
1028         while (TRUE) {
1029             ObMenuEntryFrame *e;
1030
1031             it = it ? g_list_previous(it) : g_list_last(self->entries);
1032             if (it == start)
1033                 break;
1034
1035             if (it) {
1036                 e = it->data;
1037                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1038                     break;
1039                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1040                     e->entry->data.normal.enabled)
1041                     break;
1042             }
1043         }
1044     }
1045     menu_frame_select(self, it ? it->data : NULL, TRUE);
1046 }
1047
1048 void menu_frame_select_next(ObMenuFrame *self)
1049 {
1050     GList *it = NULL, *start;
1051
1052     if (self->entries) {
1053         start = it = g_list_find(self->entries, self->selected);
1054         while (TRUE) {
1055             ObMenuEntryFrame *e;
1056
1057             it = it ? g_list_next(it) : self->entries;
1058             if (it == start)
1059                 break;
1060
1061             if (it) {
1062                 e = it->data;
1063                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1064                     break;
1065                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1066                     e->entry->data.normal.enabled)
1067                     break;
1068             }
1069         }
1070     }
1071     menu_frame_select(self, it ? it->data : NULL, TRUE);
1072 }