]> icculus.org git repositories - dana/openbox.git/blob - openbox/menuframe.c
merge r7648-51 from trunk
[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     g_free(a);
348 }
349
350 static void menu_entry_frame_render(ObMenuEntryFrame *self)
351 {
352     RrAppearance *item_a, *text_a;
353     gint th; /* temp */
354     ObMenu *sub;
355     ObMenuFrame *frame = self->frame;
356
357     switch (self->entry->type) {
358     case OB_MENU_ENTRY_TYPE_NORMAL:
359     case OB_MENU_ENTRY_TYPE_SUBMENU:
360         item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
361                   !self->entry->data.normal.enabled ?
362                   /* disabled */
363                   (self == self->frame->selected ?
364                    self->a_disabled_selected : self->a_disabled) :
365                   /* enabled */
366                   (self == self->frame->selected ?
367                    self->a_selected : self->a_normal));
368         th = ITEM_HEIGHT;
369         break;
370     case OB_MENU_ENTRY_TYPE_SEPARATOR:
371         if (self->entry->data.separator.label) {
372             item_a = self->frame->a_title;
373             th = ob_rr_theme->menu_title_height;
374         } else {
375             item_a = self->a_normal;
376             th = SEPARATOR_HEIGHT + 2*PADDING;
377         }
378         break;
379     default:
380         g_assert_not_reached();
381     }
382     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
383     XResizeWindow(ob_display, self->window,
384                   self->area.width, self->area.height);
385     item_a->surface.parent = self->frame->a_items;
386     item_a->surface.parentx = self->area.x;
387     item_a->surface.parenty = self->area.y;
388     RrPaint(item_a, self->window, self->area.width, self->area.height);
389
390     switch (self->entry->type) {
391     case OB_MENU_ENTRY_TYPE_NORMAL:
392         text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
393                   !self->entry->data.normal.enabled ?
394                   /* disabled */
395                   (self == self->frame->selected ?
396                    self->a_text_disabled_selected : self->a_text_disabled) :
397                   /* enabled */
398                   (self == self->frame->selected ?
399                    self->a_text_selected : self->a_text_normal));
400         text_a->texture[0].data.text.string = self->entry->data.normal.label;
401         if (self->entry->data.normal.shortcut &&
402             (self->frame->menu->show_all_shortcuts ||
403              self->entry->data.normal.shortcut_always_show ||
404              self->entry->data.normal.shortcut_position > 0))
405         {
406             text_a->texture[0].data.text.shortcut = TRUE;
407             text_a->texture[0].data.text.shortcut_pos =
408                 self->entry->data.normal.shortcut_position;
409         } else
410             text_a->texture[0].data.text.shortcut = FALSE;
411         break;
412     case OB_MENU_ENTRY_TYPE_SUBMENU:
413         text_a = (self == self->frame->selected ?
414                   self->a_text_selected :
415                   self->a_text_normal);
416         sub = self->entry->data.submenu.submenu;
417         text_a->texture[0].data.text.string = sub ? sub->title : "";
418         if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
419                               sub->shortcut_always_show ||
420                               sub->shortcut_position > 0))
421         {
422             text_a->texture[0].data.text.shortcut = TRUE;
423             text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
424         } else
425             text_a->texture[0].data.text.shortcut = FALSE;
426         break;
427     case OB_MENU_ENTRY_TYPE_SEPARATOR:
428         if (self->entry->data.separator.label != NULL)
429             text_a = self->a_text_title;
430         else
431             text_a = self->a_text_normal;
432         break;
433     }
434
435     switch (self->entry->type) {
436     case OB_MENU_ENTRY_TYPE_NORMAL:
437         XMoveResizeWindow(ob_display, self->text,
438                           self->frame->text_x, PADDING,
439                           self->frame->text_w,
440                           ITEM_HEIGHT - 2*PADDING);
441         text_a->surface.parent = item_a;
442         text_a->surface.parentx = self->frame->text_x;
443         text_a->surface.parenty = PADDING;
444         RrPaint(text_a, self->text, self->frame->text_w,
445                 ITEM_HEIGHT - 2*PADDING);
446         break;
447     case OB_MENU_ENTRY_TYPE_SUBMENU:
448         XMoveResizeWindow(ob_display, self->text,
449                           self->frame->text_x, PADDING,
450                           self->frame->text_w - ITEM_HEIGHT,
451                           ITEM_HEIGHT - 2*PADDING);
452         text_a->surface.parent = item_a;
453         text_a->surface.parentx = self->frame->text_x;
454         text_a->surface.parenty = PADDING;
455         RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
456                 ITEM_HEIGHT - 2*PADDING);
457         break;
458     case OB_MENU_ENTRY_TYPE_SEPARATOR:
459         if (self->entry->data.separator.label != NULL) {
460             /* labeled separator */
461             XMoveResizeWindow(ob_display, self->text,
462                               ob_rr_theme->paddingx, ob_rr_theme->paddingy,
463                               self->area.width - 2*ob_rr_theme->paddingx,
464                               ob_rr_theme->menu_title_height -
465                               2*ob_rr_theme->paddingy);
466             text_a->surface.parent = item_a;
467             text_a->surface.parentx = ob_rr_theme->paddingx;
468             text_a->surface.parenty = ob_rr_theme->paddingy;
469             RrPaint(text_a, self->text,
470                     self->area.width - 2*ob_rr_theme->paddingx,
471                     ob_rr_theme->menu_title_height -
472                     2*ob_rr_theme->paddingy);
473         } else {
474             /* unlabeled separaator */
475             XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
476                               self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
477             self->a_separator->surface.parent = item_a;
478             self->a_separator->surface.parentx = PADDING;
479             self->a_separator->surface.parenty = PADDING;
480             self->a_separator->texture[0].data.lineart.color =
481                 text_a->texture[0].data.text.color;
482             self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
483             self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
484             self->a_separator->texture[0].data.lineart.x2 =
485                 self->area.width - 4*PADDING;
486             self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
487             RrPaint(self->a_separator, self->text,
488                     self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
489         }
490         break;
491     }
492
493     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
494         self->entry->data.normal.icon_data)
495     {
496         XMoveResizeWindow(ob_display, self->icon,
497                           PADDING, frame->item_margin.top,
498                           ITEM_HEIGHT - frame->item_margin.top
499                           - frame->item_margin.bottom,
500                           ITEM_HEIGHT - frame->item_margin.top
501                           - frame->item_margin.bottom);
502         self->a_icon->texture[0].data.rgba.width =
503             self->entry->data.normal.icon_width;
504         self->a_icon->texture[0].data.rgba.height =
505             self->entry->data.normal.icon_height;
506         self->a_icon->texture[0].data.rgba.alpha =
507             self->entry->data.normal.icon_alpha;
508         self->a_icon->texture[0].data.rgba.data =
509             self->entry->data.normal.icon_data;
510         self->a_icon->surface.parent = item_a;
511         self->a_icon->surface.parentx = PADDING;
512         self->a_icon->surface.parenty = frame->item_margin.top;
513         RrPaint(self->a_icon, self->icon,
514                 ITEM_HEIGHT - frame->item_margin.top
515                 - frame->item_margin.bottom,
516                 ITEM_HEIGHT - frame->item_margin.top
517                 - frame->item_margin.bottom);
518         XMapWindow(ob_display, self->icon);
519     } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
520                self->entry->data.normal.mask)
521     {
522         RrColor *c;
523
524         XMoveResizeWindow(ob_display, self->icon,
525                           PADDING, frame->item_margin.top,
526                           ITEM_HEIGHT - frame->item_margin.top
527                           - frame->item_margin.bottom,
528                           ITEM_HEIGHT - frame->item_margin.top
529                           - frame->item_margin.bottom);
530         self->a_mask->texture[0].data.mask.mask =
531             self->entry->data.normal.mask;
532
533         c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
534              !self->entry->data.normal.enabled ?
535              /* disabled */
536              (self == self->frame->selected ?
537               self->entry->data.normal.mask_disabled_selected_color :
538               self->entry->data.normal.mask_disabled_color) :
539              /* enabled */
540              (self == self->frame->selected ?
541               self->entry->data.normal.mask_selected_color :
542               self->entry->data.normal.mask_normal_color));
543         self->a_mask->texture[0].data.mask.color = c;
544
545         self->a_mask->surface.parent = item_a;
546         self->a_mask->surface.parentx = PADDING;
547         self->a_mask->surface.parenty = frame->item_margin.top;
548         RrPaint(self->a_mask, self->icon,
549                 ITEM_HEIGHT - frame->item_margin.top
550                 - frame->item_margin.bottom,
551                 ITEM_HEIGHT - frame->item_margin.top
552                 - frame->item_margin.bottom);
553         XMapWindow(ob_display, self->icon);
554     } else
555         XUnmapWindow(ob_display, self->icon);
556
557     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
558         RrAppearance *bullet_a;
559         XMoveResizeWindow(ob_display, self->bullet,
560                           self->frame->text_x + self->frame->text_w -
561                           ITEM_HEIGHT + PADDING, PADDING,
562                           ITEM_HEIGHT - 2*PADDING,
563                           ITEM_HEIGHT - 2*PADDING);
564         bullet_a = (self == self->frame->selected ?
565                     self->a_bullet_selected :
566                     self->a_bullet_normal);
567         bullet_a->surface.parent = item_a;
568         bullet_a->surface.parentx =
569             self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
570         bullet_a->surface.parenty = PADDING;
571         RrPaint(bullet_a, self->bullet,
572                 ITEM_HEIGHT - 2*PADDING,
573                 ITEM_HEIGHT - 2*PADDING);
574         XMapWindow(ob_display, self->bullet);
575     } else
576         XUnmapWindow(ob_display, self->bullet);
577
578     XFlush(ob_display);
579 }
580
581 /*! this code is taken from the menu_frame_render. if that changes, this won't
582   work.. */
583 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
584                                         gboolean first_entry,
585                                         gboolean last_entry)
586 {
587     ObMenuEntryType t;
588     gint h = 0;
589
590     h += 2*PADDING;
591
592     if (self)
593         t = self->entry->type;
594     else
595         /* this is the More... entry, it's NORMAL type */
596         t = OB_MENU_ENTRY_TYPE_NORMAL;
597
598     switch (t) {
599     case OB_MENU_ENTRY_TYPE_NORMAL:
600     case OB_MENU_ENTRY_TYPE_SUBMENU:
601         h += ob_rr_theme->menu_font_height;
602         break;
603     case OB_MENU_ENTRY_TYPE_SEPARATOR:
604         if (self->entry->data.separator.label != NULL) {
605             h += ob_rr_theme->menu_title_height +
606                 (ob_rr_theme->mbwidth - PADDING) * 2;
607  
608             /* if the first entry is a labeled separator, then make its border
609                overlap with the menu's outside border */
610             if (first_entry)
611                 h -= ob_rr_theme->mbwidth;
612             /* if the last entry is a labeled separator, then make its border
613                overlap with the menu's outside border */
614             if (last_entry)
615                 h -= ob_rr_theme->mbwidth;
616         } else {
617             h += SEPARATOR_HEIGHT;
618         }
619         break;
620     }
621
622     return h;
623 }
624
625 void menu_frame_render(ObMenuFrame *self)
626 {
627     gint w = 0, h = 0;
628     gint tw, th; /* temps */
629     GList *it;
630     gboolean has_icon = FALSE;
631     ObMenu *sub;
632     ObMenuEntryFrame *e;
633
634     /* find text dimensions */
635
636     STRUT_SET(self->item_margin, 0, 0, 0, 0);
637
638     if (self->entries) {
639         ObMenuEntryFrame *e = self->entries->data;
640         gint l, t, r, b;
641
642         e->a_text_normal->texture[0].data.text.string = "";
643         tw = RrMinWidth(e->a_text_normal);
644         tw += 2*PADDING;
645
646         th = ITEM_HEIGHT;
647
648         RrMargins(e->a_normal, &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_selected, &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, &l, &t, &r, &b);
661         STRUT_SET(self->item_margin,
662                   MAX(self->item_margin.left, l),
663                   MAX(self->item_margin.top, t),
664                   MAX(self->item_margin.right, r),
665                   MAX(self->item_margin.bottom, b));
666         RrMargins(e->a_disabled_selected, &l, &t, &r, &b);
667         STRUT_SET(self->item_margin,
668                   MAX(self->item_margin.left, l),
669                   MAX(self->item_margin.top, t),
670                   MAX(self->item_margin.right, r),
671                   MAX(self->item_margin.bottom, b));
672     }
673
674     /* render the entries */
675
676     for (it = self->entries; it; it = g_list_next(it)) {
677         RrAppearance *text_a;
678         e = it->data;
679
680         /* if the first entry is a labeled separator, then make its border
681            overlap with the menu's outside border */
682         if (it == self->entries &&
683             e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
684             e->entry->data.separator.label)
685         {
686             h -= ob_rr_theme->mbwidth;
687         }
688
689         if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
690             e->entry->data.separator.label)
691         {
692             e->border = ob_rr_theme->mbwidth;
693         }
694
695         RECT_SET_POINT(e->area, 0, h+e->border);
696         XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
697         XSetWindowBorderWidth(ob_display, e->window, e->border);
698         XSetWindowBorder(ob_display, e->window,
699                          RrColorPixel(ob_rr_theme->menu_border_color));
700
701
702         text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
703                   !e->entry->data.normal.enabled ?
704                   /* disabled */
705                   (e == self->selected ?
706                    e->a_text_disabled_selected : e->a_text_disabled) :
707                   /* enabled */
708                   (e == self->selected ?
709                    e->a_text_selected : e->a_text_normal));
710         switch (e->entry->type) {
711         case OB_MENU_ENTRY_TYPE_NORMAL:
712             text_a->texture[0].data.text.string = e->entry->data.normal.label;
713             tw = RrMinWidth(text_a);
714             tw = MIN(tw, MAX_MENU_WIDTH);
715             th = ob_rr_theme->menu_font_height;
716
717             if (e->entry->data.normal.icon_data ||
718                 e->entry->data.normal.mask)
719                 has_icon = TRUE;
720             break;
721         case OB_MENU_ENTRY_TYPE_SUBMENU:
722             sub = e->entry->data.submenu.submenu;
723             text_a->texture[0].data.text.string = sub ? sub->title : "";
724             tw = RrMinWidth(text_a);
725             tw = MIN(tw, MAX_MENU_WIDTH);
726             th = ob_rr_theme->menu_font_height;
727
728             if (e->entry->data.normal.icon_data ||
729                 e->entry->data.normal.mask)
730                 has_icon = TRUE;
731
732             tw += ITEM_HEIGHT - PADDING;
733             break;
734         case OB_MENU_ENTRY_TYPE_SEPARATOR:
735             if (e->entry->data.separator.label != NULL) {
736                 e->a_text_title->texture[0].data.text.string =
737                     e->entry->data.separator.label;
738                 tw = RrMinWidth(e->a_text_title) + 2*ob_rr_theme->paddingx;
739                 tw = MIN(tw, MAX_MENU_WIDTH);
740                 th = ob_rr_theme->menu_title_height +
741                     (ob_rr_theme->mbwidth - PADDING) *2;
742             } else {
743                 tw = 0;
744                 th = SEPARATOR_HEIGHT;
745             }
746             break;
747         }
748         tw += 2*PADDING;
749         th += 2*PADDING;
750         w = MAX(w, tw);
751         h += th;
752     }
753
754     /* if the last entry is a labeled separator, then make its border
755        overlap with the menu's outside border */
756     it = g_list_last(self->entries);
757     e = it ? it->data : NULL;
758     if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
759         e->entry->data.separator.label)
760     {
761         h -= ob_rr_theme->mbwidth;
762     }
763
764     self->text_x = PADDING;
765     self->text_w = w;
766
767     if (self->entries) {
768         if (has_icon) {
769             w += ITEM_HEIGHT + PADDING;
770             self->text_x += ITEM_HEIGHT + PADDING;
771         }
772     }
773
774     if (!w) w = 10;
775     if (!h) h = 3;
776
777     XResizeWindow(ob_display, self->window, w, h);
778
779     self->inner_w = w;
780
781     RrPaint(self->a_items, self->window, w, h);
782
783     for (it = self->entries; it; it = g_list_next(it))
784         menu_entry_frame_render(it->data);
785
786     w += ob_rr_theme->mbwidth * 2;
787     h += ob_rr_theme->mbwidth * 2;
788
789     RECT_SET_SIZE(self->area, w, h);
790
791     XFlush(ob_display);
792 }
793
794 static void menu_frame_update(ObMenuFrame *self)
795 {
796     GList *mit, *fit;
797     Rect *a;
798     gint h;
799
800     menu_pipe_execute(self->menu);
801     menu_find_submenus(self->menu);
802
803     self->selected = NULL;
804
805     /* start at show_from */
806     mit = g_list_nth(self->menu->entries, self->show_from);
807
808     /* go through the menu's and frame's entries and connect the frame entries
809        to the menu entries */
810     for (fit = self->entries; mit && fit;
811          mit = g_list_next(mit), fit = g_list_next(fit))
812     {
813         ObMenuEntryFrame *f = fit->data;
814         f->entry = mit->data;
815     }
816
817     /* if there are more menu entries than in the frame, add them */
818     while (mit) {
819         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
820         self->entries = g_list_append(self->entries, e);
821         mit = g_list_next(mit);
822     }
823
824     /* if there are more frame entries than menu entries then get rid of
825        them */
826     while (fit) {
827         GList *n = g_list_next(fit);
828         menu_entry_frame_free(fit->data);
829         self->entries = g_list_delete_link(self->entries, fit);
830         fit = n;
831     }
832
833     /* * make the menu fit on the screen */
834
835     /* calculate the height of the menu */
836     h = 0;
837     for (fit = self->entries; fit; fit = g_list_next(fit))
838         h += menu_entry_frame_get_height(fit->data,
839                                          fit == self->entries,
840                                          g_list_next(fit) == NULL);
841     /* add the border at the top and bottom */
842     h += ob_rr_theme->mbwidth * 2;
843
844     a = screen_physical_area_monitor(self->monitor);
845
846     if (h > a->height) {
847         GList *flast, *tmp;
848         gboolean last_entry = TRUE;
849
850         /* take the height of our More... entry into account */
851         h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
852
853         /* start at the end of the entries */
854         flast = g_list_last(self->entries);
855
856         /* pull out all the entries from the frame that don't
857            fit on the screen, leaving at least 1 though */
858         while (h > a->height && g_list_previous(flast) != NULL) {
859             /* update the height, without this entry */
860             h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
861
862             /* destroy the entry we're not displaying */
863             tmp = flast;
864             flast = g_list_previous(flast);
865             menu_entry_frame_free(tmp->data);
866             self->entries = g_list_delete_link(self->entries, tmp);
867
868             /* only the first one that we see is the last entry in the menu */
869             last_entry = FALSE;
870         };
871
872         {
873             ObMenuEntry *more_entry;
874             ObMenuEntryFrame *more_frame;
875             /* make the More... menu entry frame which will display in this
876                frame.
877                if self->menu->more_menu is NULL that means that this is already
878                More... menu, so just use ourself.
879             */
880             more_entry = menu_get_more((self->menu->more_menu ?
881                                         self->menu->more_menu :
882                                         self->menu),
883                                        /* continue where we left off */
884                                        self->show_from +
885                                        g_list_length(self->entries));
886             more_frame = menu_entry_frame_new(more_entry, self);
887             /* make it get deleted when the menu frame goes away */
888             menu_entry_unref(more_entry);
889                                        
890             /* add our More... entry to the frame */
891             self->entries = g_list_append(self->entries, more_frame);
892         }
893     }
894
895     g_free(a);
896
897     menu_frame_render(self);
898 }
899
900 static gboolean menu_frame_is_visible(ObMenuFrame *self)
901 {
902     return !!(g_list_find(menu_frame_visible, self));
903 }
904
905 static gboolean menu_frame_show(ObMenuFrame *self)
906 {
907     GList *it;
908
909     /* determine if the underlying menu is already visible */
910     for (it = menu_frame_visible; it; it = g_list_next(it)) {
911         ObMenuFrame *f = it->data;
912         if (f->menu == self->menu)
913             break;
914     }
915     if (!it) {
916         if (self->menu->update_func)
917             if (!self->menu->update_func(self, self->menu->data))
918                 return FALSE;
919     }
920
921     if (menu_frame_visible == NULL) {
922         /* no menus shown yet */
923
924         /* grab the pointer in such a way as to pass through "owner events"
925            so that we can get enter/leave notifies in the menu. */
926         if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
927             return FALSE;
928         if (!grab_keyboard()) {
929             ungrab_pointer();
930             return FALSE;
931         }
932     }
933
934     menu_frame_update(self);
935
936     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
937
938     if (self->menu->show_func)
939         self->menu->show_func(self, self->menu->data);
940
941     return TRUE;
942 }
943
944 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
945                                  gint button)
946 {
947     gint px, py;
948     guint i;
949
950     if (menu_frame_is_visible(self))
951         return TRUE;
952     if (!menu_frame_show(self))
953         return FALSE;
954
955     /* find the monitor the menu is on */
956     for (i = 0; i < screen_num_monitors; ++i) {
957         Rect *a = screen_physical_area_monitor(i);
958         gboolean contains = RECT_CONTAINS(*a, x, y);
959         g_free(a);
960         if (contains) {
961             self->monitor = i;
962             break;
963         }
964     }
965
966     if (self->menu->place_func)
967         self->menu->place_func(self, &x, &y, button, self->menu->data);
968     else
969         menu_frame_place_topmenu(self, &x, &y);
970
971     menu_frame_move(self, x, y);
972
973     XMapWindow(ob_display, self->window);
974
975     if (screen_pointer_pos(&px, &py)) {
976         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
977         if (e && e->frame == self)
978             e->ignore_enters++;
979     }
980
981     return TRUE;
982 }
983
984 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
985                                  ObMenuEntryFrame *parent_entry)
986 {
987     gint x, y, dx, dy;
988     gint px, py;
989
990     if (menu_frame_is_visible(self))
991         return TRUE;
992
993     self->monitor = parent->monitor;
994     self->parent = parent;
995     self->parent_entry = parent_entry;
996
997     /* set up parent's child to be us */
998     if (parent->child)
999         menu_frame_hide(parent->child);
1000     parent->child = self;
1001
1002     if (!menu_frame_show(self))
1003         return FALSE;
1004
1005     menu_frame_place_submenu(self, &x, &y);
1006     menu_frame_move_on_screen(self, x, y, &dx, &dy);
1007
1008     if (dx != 0) {
1009         /*try the other side */
1010         self->direction_right = !self->direction_right;
1011         menu_frame_place_submenu(self, &x, &y);
1012         menu_frame_move_on_screen(self, x, y, &dx, &dy);
1013     }
1014     menu_frame_move(self, x + dx, y + dy);
1015
1016     XMapWindow(ob_display, self->window);
1017
1018     if (screen_pointer_pos(&px, &py)) {
1019         ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1020         if (e && e->frame == self)
1021             e->ignore_enters++;
1022     }
1023
1024     return TRUE;
1025 }
1026
1027 static void menu_frame_hide(ObMenuFrame *self)
1028 {
1029     GList *it = g_list_find(menu_frame_visible, self);
1030
1031     if (!it)
1032         return;
1033
1034     if (self->menu->hide_func)
1035         self->menu->hide_func(self, self->menu->data);
1036
1037     if (self->child)
1038         menu_frame_hide(self->child);
1039
1040     if (self->parent)
1041         self->parent->child = NULL;
1042     self->parent = NULL;
1043     self->parent_entry = NULL;
1044
1045     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1046
1047     if (menu_frame_visible == NULL) {
1048         /* last menu shown */
1049         ungrab_pointer();
1050         ungrab_keyboard();
1051     }
1052
1053     XUnmapWindow(ob_display, self->window);
1054
1055     menu_frame_free(self);
1056 }
1057
1058 void menu_frame_hide_all()
1059 {
1060     GList *it;
1061
1062     if (config_submenu_show_delay) {
1063         /* remove any submenu open requests */
1064         ob_main_loop_timeout_remove(ob_main_loop,
1065                                     menu_entry_frame_submenu_timeout);
1066     }
1067     if ((it = g_list_last(menu_frame_visible)))
1068         menu_frame_hide(it->data);
1069 }
1070
1071 void menu_frame_hide_all_client(ObClient *client)
1072 {
1073     GList *it = g_list_last(menu_frame_visible);
1074     if (it) {
1075         ObMenuFrame *f = it->data;
1076         if (f->client == client)
1077             menu_frame_hide(f);
1078     }
1079 }
1080
1081
1082 ObMenuFrame* menu_frame_under(gint x, gint y)
1083 {
1084     ObMenuFrame *ret = NULL;
1085     GList *it;
1086
1087     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1088         ObMenuFrame *f = it->data;
1089
1090         if (RECT_CONTAINS(f->area, x, y)) {
1091             ret = f;
1092             break;
1093         }
1094     }
1095     return ret;
1096 }
1097
1098 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1099 {
1100     ObMenuFrame *frame;
1101     ObMenuEntryFrame *ret = NULL;
1102     GList *it;
1103
1104     if ((frame = menu_frame_under(x, y))) {
1105         x -= ob_rr_theme->mbwidth + frame->area.x;
1106         y -= ob_rr_theme->mbwidth + frame->area.y;
1107
1108         for (it = frame->entries; it; it = g_list_next(it)) {
1109             ObMenuEntryFrame *e = it->data;
1110
1111             if (RECT_CONTAINS(e->area, x, y)) {
1112                 ret = e;
1113                 break;
1114             }
1115         }
1116     }
1117     return ret;
1118 }
1119
1120 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1121 {
1122     menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1123     return FALSE;
1124 }
1125
1126 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1127                        gboolean immediate)
1128 {
1129     ObMenuEntryFrame *old = self->selected;
1130     ObMenuFrame *oldchild = self->child;
1131
1132     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1133         entry = old;
1134
1135     if (old == entry) return;
1136    
1137     if (config_submenu_show_delay) { 
1138         /* remove any submenu open requests */
1139         ob_main_loop_timeout_remove(ob_main_loop,
1140                                     menu_entry_frame_submenu_timeout);
1141     }
1142
1143     self->selected = entry;
1144
1145     if (old)
1146         menu_entry_frame_render(old);
1147     if (oldchild)
1148         menu_frame_hide(oldchild);
1149
1150     if (self->selected) {
1151         menu_entry_frame_render(self->selected);
1152
1153         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1154             if (config_submenu_show_delay && !immediate) {
1155                 /* initiate a new submenu open request */
1156                 ob_main_loop_timeout_add(ob_main_loop,
1157                                          config_submenu_show_delay * 1000,
1158                                          menu_entry_frame_submenu_timeout,
1159                                          self->selected, g_direct_equal,
1160                                          NULL);
1161             } else {
1162                 menu_entry_frame_show_submenu(self->selected);
1163             }
1164         }
1165     }
1166 }
1167
1168 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1169 {
1170     ObMenuFrame *f;
1171
1172     if (!self->entry->data.submenu.submenu) return;
1173
1174     f = menu_frame_new(self->entry->data.submenu.submenu,
1175                        self->entry->data.submenu.show_from,
1176                        self->frame->client);
1177     /* pass our direction on to our child */
1178     f->direction_right = self->frame->direction_right;
1179
1180     menu_frame_show_submenu(f, self->frame, self);
1181 }
1182
1183 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
1184 {
1185     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1186         self->entry->data.normal.enabled)
1187     {
1188         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1189            gets freed */
1190         ObMenuEntry *entry = self->entry;
1191         ObMenuExecuteFunc func = self->frame->menu->execute_func;
1192         gpointer data = self->frame->menu->data;
1193         GSList *acts = self->entry->data.normal.actions;
1194         ObClient *client = self->frame->client;
1195         ObMenuFrame *frame = self->frame;
1196
1197         /* release grabs before executing the shit */
1198         if (!(state & ControlMask)) {
1199             menu_frame_hide_all();
1200             frame = NULL;
1201         }
1202
1203         if (func)
1204             func(entry, frame, client, state, data, time);
1205         else
1206             action_run(acts, client, state, time);
1207     }
1208 }
1209
1210 void menu_frame_select_previous(ObMenuFrame *self)
1211 {
1212     GList *it = NULL, *start;
1213
1214     if (self->entries) {
1215         start = it = g_list_find(self->entries, self->selected);
1216         while (TRUE) {
1217             ObMenuEntryFrame *e;
1218
1219             it = it ? g_list_previous(it) : g_list_last(self->entries);
1220             if (it == start)
1221                 break;
1222
1223             if (it) {
1224                 e = it->data;
1225                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1226                     break;
1227                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1228                     break;
1229             }
1230         }
1231     }
1232     menu_frame_select(self, it ? it->data : NULL, TRUE);
1233 }
1234
1235 void menu_frame_select_next(ObMenuFrame *self)
1236 {
1237     GList *it = NULL, *start;
1238
1239     if (self->entries) {
1240         start = it = g_list_find(self->entries, self->selected);
1241         while (TRUE) {
1242             ObMenuEntryFrame *e;
1243
1244             it = it ? g_list_next(it) : self->entries;
1245             if (it == start)
1246                 break;
1247
1248             if (it) {
1249                 e = it->data;
1250                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1251                     break;
1252                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1253                     break;
1254             }
1255         }
1256     }
1257     menu_frame_select(self, it ? it->data : NULL, TRUE);
1258 }