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