use the theme padding for the menu title's size
[dana/openbox.git] / openbox / menuframe.c
1 #include "menuframe.h"
2 #include "client.h"
3 #include "menu.h"
4 #include "screen.h"
5 #include "grab.h"
6 #include "openbox.h"
7 #include "render/theme.h"
8
9 #define PADDING 2
10 #define SEPARATOR_HEIGHT 3
11 #define MAX_MENU_WIDTH 400
12
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
14                          LeaveWindowMask)
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17                          ButtonPressMask | ButtonReleaseMask)
18
19 GList *menu_frame_visible;
20
21 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
22                                               ObMenuFrame *frame);
23 static void menu_entry_frame_free(ObMenuEntryFrame *self);
24 static void menu_frame_render(ObMenuFrame *self);
25 static void menu_frame_update(ObMenuFrame *self);
26
27 static Window createWindow(Window parent, unsigned long mask,
28                            XSetWindowAttributes *attrib)
29 {
30     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
31                          RrDepth(ob_rr_inst), InputOutput,
32                          RrVisual(ob_rr_inst), mask, attrib);
33 }
34
35 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
36 {
37     ObMenuFrame *self;
38     XSetWindowAttributes attr;
39
40     self = g_new0(ObMenuFrame, 1);
41     self->type = Window_Menu;
42     self->menu = menu;
43     self->selected = NULL;
44     self->show_title = TRUE;
45     self->client = client;
46
47     attr.event_mask = FRAME_EVENTMASK;
48     self->window = createWindow(RootWindow(ob_display, ob_screen),
49                                    CWEventMask, &attr);
50     attr.event_mask = TITLE_EVENTMASK;
51     self->title = createWindow(self->window, CWEventMask, &attr);
52     self->items = createWindow(self->window, 0, NULL);
53
54     XMapWindow(ob_display, self->items);
55
56     self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
57     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
58
59     stacking_add(MENU_AS_WINDOW(self));
60
61     return self;
62 }
63
64 void menu_frame_free(ObMenuFrame *self)
65 {
66     if (self) {
67         while (self->entries) {
68             menu_entry_frame_free(self->entries->data);
69             self->entries = g_list_delete_link(self->entries, self->entries);
70         }
71
72         stacking_remove(MENU_AS_WINDOW(self));
73
74         XDestroyWindow(ob_display, self->items);
75         XDestroyWindow(ob_display, self->title);
76         XDestroyWindow(ob_display, self->window);
77
78         RrAppearanceFree(self->a_items);
79         RrAppearanceFree(self->a_title);
80
81         g_free(self);
82     }
83 }
84
85 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
86                                               ObMenuFrame *frame)
87 {
88     ObMenuEntryFrame *self;
89     XSetWindowAttributes attr;
90
91     self = g_new0(ObMenuEntryFrame, 1);
92     self->entry = entry;
93     self->frame = frame;
94
95     attr.event_mask = ENTRY_EVENTMASK;
96     self->window = createWindow(self->frame->items, CWEventMask, &attr);
97     self->text = createWindow(self->window, 0, NULL);
98     if (entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
99         self->icon = createWindow(self->window, 0, NULL);
100         self->bullet = createWindow(self->window, 0, NULL);
101     }
102
103     XMapWindow(ob_display, self->window);
104     XMapWindow(ob_display, self->text);
105
106     self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
107     self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
108     self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
109
110     if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
111         self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
112         self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
113     } else {
114         self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
115         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
116         self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
117         self->a_mask->texture[0].type = RR_TEXTURE_MASK;
118         self->a_bullet_normal =
119             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
120         self->a_bullet_selected =
121             RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
122     }
123
124     self->a_text_normal =
125         RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
126     self->a_text_disabled =
127         RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
128     self->a_text_selected =
129         RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
130
131     return self;
132 }
133
134 static void menu_entry_frame_free(ObMenuEntryFrame *self)
135 {
136     if (self) {
137         XDestroyWindow(ob_display, self->text);
138         XDestroyWindow(ob_display, self->window);
139         if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
140             XDestroyWindow(ob_display, self->icon);
141             XDestroyWindow(ob_display, self->bullet);
142         }
143
144         RrAppearanceFree(self->a_normal);
145         RrAppearanceFree(self->a_disabled);
146         RrAppearanceFree(self->a_selected);
147
148         RrAppearanceFree(self->a_separator);
149         RrAppearanceFree(self->a_icon);
150         RrAppearanceFree(self->a_mask);
151         RrAppearanceFree(self->a_text_normal);
152         RrAppearanceFree(self->a_text_disabled);
153         RrAppearanceFree(self->a_text_selected);
154         RrAppearanceFree(self->a_bullet_normal);
155         RrAppearanceFree(self->a_bullet_selected);
156
157         g_free(self);
158     }
159 }
160
161 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
162 {
163     RECT_SET_POINT(self->area, x, y);
164     XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
165 }
166
167 void menu_frame_move_on_screen(ObMenuFrame *self)
168 {
169     Rect *a;
170     guint i;
171     gint dx = 0, dy = 0;
172     gint pos, half;
173
174     for (i = 0; i < screen_num_monitors; ++i) {
175         a = screen_physical_area_monitor(i);
176         if (RECT_INTERSECTS_RECT(*a, self->area))
177             break;
178     }
179     if (!a) a = screen_physical_area_monitor(0);
180
181     half = g_list_length(self->entries) / 2;
182     pos = g_list_index(self->entries, self->selected);
183
184     /* if in the bottom half then check this shit first, will keep the bottom
185        edge of the menu visible */
186     if (pos > half) {
187         dx = MAX(dx, a->x - self->area.x);
188         dy = MAX(dy, a->y - self->area.y);
189     }
190     dx = MIN(dx, (a->x + a->width) - (self->area.x + self->area.width));
191     dy = MIN(dy, (a->y + a->height) - (self->area.y + self->area.height));
192     /* if in the top half then check this shit last, will keep the top
193        edge of the menu visible */
194     if (pos <= half) {
195         dx = MAX(dx, a->x - self->area.x);
196         dy = MAX(dy, a->y - self->area.y);
197     }
198
199     if (dx || dy) {
200         ObMenuFrame *f;
201
202         for (f = self; f; f = f->parent)
203             menu_frame_move(f, f->area.x + dx, f->area.y + dy);
204         for (f = self->child; f; f = f->child)
205             menu_frame_move(f, f->area.x + dx, f->area.y + dy);
206         XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
207     }
208 }
209
210 static void menu_entry_frame_render(ObMenuEntryFrame *self)
211 {
212     RrAppearance *item_a, *text_a;
213     gint th; /* temp */
214     ObMenu *sub;
215     ObMenuFrame *frame = self->frame;
216
217     item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
218                !self->entry->data.normal.enabled) ?
219               self->a_disabled :
220               (self == self->frame->selected ?
221                self->a_selected :
222                self->a_normal));
223     switch (self->entry->type) {
224     case OB_MENU_ENTRY_TYPE_NORMAL:
225     case OB_MENU_ENTRY_TYPE_SUBMENU:
226         th = self->frame->item_h;
227         break;
228     case OB_MENU_ENTRY_TYPE_SEPARATOR:
229         th = SEPARATOR_HEIGHT + 2*PADDING;
230         break;
231     }
232     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
233     XResizeWindow(ob_display, self->window,
234                   self->area.width, self->area.height);
235     item_a->surface.parent = self->frame->a_items;
236     item_a->surface.parentx = self->area.x;
237     item_a->surface.parenty = self->area.y;
238     RrPaint(item_a, self->window, self->area.width, self->area.height);
239
240     text_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
241                !self->entry->data.normal.enabled) ?
242               self->a_text_disabled :
243               (self == self->frame->selected ?
244                self->a_text_selected :
245                self->a_text_normal));
246     switch (self->entry->type) {
247     case OB_MENU_ENTRY_TYPE_NORMAL:
248         text_a->texture[0].data.text.string = self->entry->data.normal.label;
249         break;
250     case OB_MENU_ENTRY_TYPE_SUBMENU:
251         sub = self->entry->data.submenu.submenu;
252         text_a->texture[0].data.text.string = sub ? sub->title : "";
253         break;
254     case OB_MENU_ENTRY_TYPE_SEPARATOR:
255         break;
256     }
257
258     switch (self->entry->type) {
259     case OB_MENU_ENTRY_TYPE_NORMAL:
260         XMoveResizeWindow(ob_display, self->text,
261                           self->frame->text_x, PADDING,
262                           self->frame->text_w,
263                           self->frame->item_h - 2*PADDING);
264         text_a->surface.parent = item_a;
265         text_a->surface.parentx = self->frame->text_x;
266         text_a->surface.parenty = PADDING;
267         RrPaint(text_a, self->text, self->frame->text_w,
268                 self->frame->item_h - 2*PADDING);
269         break;
270     case OB_MENU_ENTRY_TYPE_SUBMENU:
271         XMoveResizeWindow(ob_display, self->text,
272                           self->frame->text_x, PADDING,
273                           self->frame->text_w - self->frame->item_h,
274                           self->frame->item_h - 2*PADDING);
275         text_a->surface.parent = item_a;
276         text_a->surface.parentx = self->frame->text_x;
277         text_a->surface.parenty = PADDING;
278         RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
279                 self->frame->item_h - 2*PADDING);
280         break;
281     case OB_MENU_ENTRY_TYPE_SEPARATOR:
282         XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
283                           self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
284         self->a_separator->surface.parent = item_a;
285         self->a_separator->surface.parentx = PADDING;
286         self->a_separator->surface.parenty = PADDING;
287         self->a_separator->texture[0].data.lineart.color =
288             text_a->texture[0].data.text.color;
289         self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
290         self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT / 2;
291         self->a_separator->texture[0].data.lineart.x2 =
292             self->area.width - 4*PADDING;
293         self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT / 2;
294         RrPaint(self->a_separator, self->text,
295                 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
296         break;
297     }
298
299     if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
300         self->entry->data.normal.icon_data)
301     {
302         XMoveResizeWindow(ob_display, self->icon,
303                           PADDING, frame->item_margin.top,
304                           self->frame->item_h - frame->item_margin.top
305                           - frame->item_margin.bottom,
306                           self->frame->item_h - frame->item_margin.top
307                           - frame->item_margin.bottom);
308         self->a_icon->texture[0].data.rgba.width =
309             self->entry->data.normal.icon_width;
310         self->a_icon->texture[0].data.rgba.height =
311             self->entry->data.normal.icon_height;
312         self->a_icon->texture[0].data.rgba.data =
313             self->entry->data.normal.icon_data;
314         self->a_icon->surface.parent = item_a;
315         self->a_icon->surface.parentx = PADDING;
316         self->a_icon->surface.parenty = frame->item_margin.top;
317         RrPaint(self->a_icon, self->icon,
318                 self->frame->item_h - frame->item_margin.top
319                 - frame->item_margin.bottom,
320                 self->frame->item_h - frame->item_margin.top
321                 - frame->item_margin.bottom);
322         XMapWindow(ob_display, self->icon);
323     } else if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
324                self->entry->data.normal.mask)
325     {
326         RrColor *c;
327
328         XMoveResizeWindow(ob_display, self->icon,
329                           PADDING, frame->item_margin.top,
330                           self->frame->item_h - frame->item_margin.top
331                           - frame->item_margin.bottom,
332                           self->frame->item_h - frame->item_margin.top
333                           - frame->item_margin.bottom);
334         self->a_mask->texture[0].data.mask.mask =
335             self->entry->data.normal.mask;
336
337         c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
338               !self->entry->data.normal.enabled) ?
339              self->entry->data.normal.mask_disabled_color :
340              (self == self->frame->selected ?
341               self->entry->data.normal.mask_selected_color :
342               self->entry->data.normal.mask_normal_color));
343         self->a_mask->texture[0].data.mask.color = c;
344
345         self->a_mask->surface.parent = item_a;
346         self->a_mask->surface.parentx = PADDING;
347         self->a_mask->surface.parenty = frame->item_margin.top;
348         RrPaint(self->a_mask, self->icon,
349                 self->frame->item_h - frame->item_margin.top
350                 - frame->item_margin.bottom,
351                 self->frame->item_h - frame->item_margin.top
352                 - frame->item_margin.bottom);
353         XMapWindow(ob_display, self->icon);
354     } else
355         XUnmapWindow(ob_display, self->icon);
356
357     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
358         RrAppearance *bullet_a;
359         XMoveResizeWindow(ob_display, self->bullet,
360                           self->frame->text_x + self->frame->text_w
361                           - self->frame->item_h + PADDING, PADDING,
362                           self->frame->item_h - 2*PADDING,
363                           self->frame->item_h - 2*PADDING);
364         bullet_a = (self == self->frame->selected ?
365                     self->a_bullet_selected :
366                     self->a_bullet_normal);
367         bullet_a->surface.parent = item_a;
368         bullet_a->surface.parentx =
369             self->frame->text_x + self->frame->text_w - self->frame->item_h
370             + PADDING;
371         bullet_a->surface.parenty = PADDING;
372         RrPaint(bullet_a, self->bullet,
373                 self->frame->item_h - 2*PADDING,
374                 self->frame->item_h - 2*PADDING);
375         XMapWindow(ob_display, self->bullet);
376     } else
377         XUnmapWindow(ob_display, self->bullet);
378
379     XFlush(ob_display);
380 }
381
382 static void menu_frame_render(ObMenuFrame *self)
383 {
384     gint w = 0, h = 0;
385     gint allitems_h = 0;
386     gint tw, th; /* temps */
387     GList *it;
388     gboolean has_icon = FALSE;
389     ObMenu *sub;
390
391     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
392     XSetWindowBorder(ob_display, self->window,
393                      RrColorPixel(ob_rr_theme->b_color));
394
395     if (!self->parent && self->show_title) {
396         XMoveWindow(ob_display, self->title, 
397                     -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
398
399         self->a_title->texture[0].data.text.string = self->menu->title;
400         RrMinsize(self->a_title, &tw, &th);
401         tw = MIN(tw, MAX_MENU_WIDTH) + ob_rr_theme->padding * 2;
402         w = MAX(w, tw);
403
404         th = ob_rr_theme->mtitlefont_height + ob_rr_theme->padding * 2;
405         h += (self->title_h = th + ob_rr_theme->bwidth);
406
407         XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
408         XSetWindowBorder(ob_display, self->title,
409                          RrColorPixel(ob_rr_theme->b_color));
410     }
411
412     XMoveWindow(ob_display, self->items, 0, h);
413
414     STRUT_SET(self->item_margin, 0, 0, 0, 0);
415
416     if (self->entries) {
417         ObMenuEntryFrame *e = self->entries->data;
418         gint l, t, r, b;
419
420         e->a_text_normal->texture[0].data.text.string = "";
421         RrMinsize(e->a_text_normal, &tw, &th);
422         tw += 2*PADDING;
423         th += 2*PADDING;
424         self->item_h = th;
425
426         RrMargins(e->a_normal, &l, &t, &r, &b);
427         STRUT_SET(self->item_margin,
428                   MAX(self->item_margin.left, l),
429                   MAX(self->item_margin.top, t),
430                   MAX(self->item_margin.right, r),
431                   MAX(self->item_margin.bottom, b));
432         RrMargins(e->a_selected, &l, &t, &r, &b);
433         STRUT_SET(self->item_margin,
434                   MAX(self->item_margin.left, l),
435                   MAX(self->item_margin.top, t),
436                   MAX(self->item_margin.right, r),
437                   MAX(self->item_margin.bottom, b));
438         RrMargins(e->a_disabled, &l, &t, &r, &b);
439         STRUT_SET(self->item_margin,
440                   MAX(self->item_margin.left, l),
441                   MAX(self->item_margin.top, t),
442                   MAX(self->item_margin.right, r),
443                   MAX(self->item_margin.bottom, b));
444     } else
445         self->item_h = 0;
446
447     for (it = self->entries; it; it = g_list_next(it)) {
448         RrAppearance *text_a;
449         ObMenuEntryFrame *e = it->data;
450
451         RECT_SET_POINT(e->area, 0, allitems_h);
452         XMoveWindow(ob_display, e->window, 0, e->area.y);
453
454         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
455                    !e->entry->data.normal.enabled) ?
456                   e->a_text_disabled :
457                   (e == self->selected ?
458                    e->a_text_selected :
459                    e->a_text_normal));
460         switch (e->entry->type) {
461         case OB_MENU_ENTRY_TYPE_NORMAL:
462             text_a->texture[0].data.text.string = e->entry->data.normal.label;
463             RrMinsize(text_a, &tw, &th);
464             tw = MIN(tw, MAX_MENU_WIDTH);
465
466             if (e->entry->data.normal.icon_data ||
467                 e->entry->data.normal.mask)
468                 has_icon = TRUE;
469             break;
470         case OB_MENU_ENTRY_TYPE_SUBMENU:
471             sub = e->entry->data.submenu.submenu;
472             text_a->texture[0].data.text.string = sub ? sub->title : "";
473             RrMinsize(text_a, &tw, &th);
474             tw = MIN(tw, MAX_MENU_WIDTH);
475
476             if (e->entry->data.normal.icon_data ||
477                 e->entry->data.normal.mask)
478                 has_icon = TRUE;
479
480             tw += self->item_h - PADDING;
481             break;
482         case OB_MENU_ENTRY_TYPE_SEPARATOR:
483             tw = 0;
484             th = SEPARATOR_HEIGHT;
485             break;
486         }
487         tw += 2*PADDING;
488         th += 2*PADDING;
489         w = MAX(w, tw);
490         h += th;
491         allitems_h += th;
492     }
493
494     self->text_x = PADDING;
495     self->text_w = w;
496
497     if (self->entries) {
498         if (has_icon) {
499             w += self->item_h + PADDING;
500             self->text_x += self->item_h + PADDING;
501         }
502     }
503
504     if (!w) w = 10;
505     if (!allitems_h) {
506         allitems_h = 3;
507         h += 3;
508     }
509
510     XResizeWindow(ob_display, self->window, w, h);
511     XResizeWindow(ob_display, self->items, w, allitems_h);
512
513     self->inner_w = w;
514
515     if (!self->parent && self->show_title) {
516         XResizeWindow(ob_display, self->title,
517                       w, self->title_h - ob_rr_theme->bwidth);
518         RrPaint(self->a_title, self->title,
519                 w, self->title_h - ob_rr_theme->bwidth);
520         XMapWindow(ob_display, self->title);
521     } else
522         XUnmapWindow(ob_display, self->title);
523
524     RrPaint(self->a_items, self->items, w, allitems_h);
525
526     for (it = self->entries; it; it = g_list_next(it))
527         menu_entry_frame_render(it->data);
528
529     w += ob_rr_theme->bwidth * 2;
530     h += ob_rr_theme->bwidth * 2;
531
532     RECT_SET_SIZE(self->area, w, h);
533
534     XFlush(ob_display);
535 }
536
537 static void menu_frame_update(ObMenuFrame *self)
538 {
539     GList *mit, *fit;
540
541     menu_pipe_execute(self->menu);
542     menu_find_submenus(self->menu);
543
544     self->selected = NULL;
545
546     for (mit = self->menu->entries, fit = self->entries; mit && fit;
547          mit = g_list_next(mit), fit = g_list_next(fit))
548     {
549         ObMenuEntryFrame *f = fit->data;
550         f->entry = mit->data;
551     }
552
553     while (mit) {
554         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
555         self->entries = g_list_append(self->entries, e);
556         mit = g_list_next(mit);
557     }
558     
559     while (fit) {
560         GList *n = g_list_next(fit);
561         menu_entry_frame_free(fit->data);
562         self->entries = g_list_delete_link(self->entries, fit);
563         fit = n;
564     }
565
566     menu_frame_render(self);
567 }
568
569 void menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
570 {
571     GList *it;
572
573     if (g_list_find(menu_frame_visible, self))
574         return;
575
576     if (parent) {
577         if (parent->child)
578             menu_frame_hide(parent->child);
579         parent->child = self;
580     }
581     self->parent = parent;
582
583     if (menu_frame_visible == NULL) {
584         /* no menus shown yet */
585         grab_pointer(TRUE, OB_CURSOR_NONE);
586         grab_keyboard(TRUE);
587     }
588
589     /* determine if the underlying menu is already visible */
590     for (it = menu_frame_visible; it; it = g_list_next(it)) {
591         ObMenuFrame *f = it->data;
592         if (f->menu == self->menu)
593             break;
594     }
595     if (!it) {
596         if (self->menu->update_func)
597             self->menu->update_func(self, self->menu->data);
598     }
599
600     menu_frame_update(self);
601
602     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
603
604     menu_frame_move_on_screen(self);
605
606     XMapWindow(ob_display, self->window);
607 }
608
609 void menu_frame_hide(ObMenuFrame *self)
610 {
611     GList *it = g_list_find(menu_frame_visible, self);
612
613     if (!it)
614         return;
615
616     if (self->child)
617         menu_frame_hide(self->child);
618
619     if (self->parent)
620         self->parent->child = NULL;
621     self->parent = NULL;
622
623     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
624
625     if (menu_frame_visible == NULL) {
626         /* last menu shown */
627         grab_pointer(FALSE, OB_CURSOR_NONE);
628         grab_keyboard(FALSE);
629     }
630
631     XUnmapWindow(ob_display, self->window);
632
633     menu_frame_free(self);
634 }
635
636 void menu_frame_hide_all()
637 {
638     GList *it = g_list_last(menu_frame_visible);
639     if (it) 
640         menu_frame_hide(it->data);
641 }
642
643 void menu_frame_hide_all_client(ObClient *client)
644 {
645     GList *it = g_list_last(menu_frame_visible);
646     if (it) {
647         ObMenuFrame *f = it->data;
648         if (f->client == client)
649             menu_frame_hide(f);
650     }
651 }
652
653
654 ObMenuFrame* menu_frame_under(gint x, gint y)
655 {
656     ObMenuFrame *ret = NULL;
657     GList *it;
658
659     for (it = menu_frame_visible; it; it = g_list_next(it)) {
660         ObMenuFrame *f = it->data;
661
662         if (RECT_CONTAINS(f->area, x, y)) {
663             ret = f;
664             break;
665         }
666     }
667     return ret;
668 }
669
670 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
671 {
672     ObMenuFrame *frame;
673     ObMenuEntryFrame *ret = NULL;
674     GList *it;
675
676     if ((frame = menu_frame_under(x, y))) {
677         x -= ob_rr_theme->bwidth + frame->area.x;
678         y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
679
680         for (it = frame->entries; it; it = g_list_next(it)) {
681             ObMenuEntryFrame *e = it->data;
682
683             if (RECT_CONTAINS(e->area, x, y)) {
684                 ret = e;            
685                 break;
686             }
687         }
688     }
689     return ret;
690 }
691
692 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
693 {
694     ObMenuEntryFrame *old = self->selected;
695     ObMenuFrame *oldchild = self->child;
696
697     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
698         entry = old;
699
700     if (old == entry) return;
701
702     self->selected = entry;
703
704     if (old)
705         menu_entry_frame_render(old);
706     if (oldchild)
707         menu_frame_hide(oldchild);
708
709     if (self->selected) {
710         menu_entry_frame_render(self->selected);
711
712         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
713             menu_entry_frame_show_submenu(self->selected);
714     }
715 }
716
717 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
718 {
719     ObMenuFrame *f;
720
721     if (!self->entry->data.submenu.submenu) return;
722
723     f = menu_frame_new(self->entry->data.submenu.submenu,
724                        self->frame->client);
725     menu_frame_move(f,
726                     self->frame->area.x + self->frame->area.width
727                     - ob_rr_theme->menu_overlap - ob_rr_theme->bwidth,
728                     self->frame->area.y + self->frame->title_h +
729                     self->area.y + ob_rr_theme->menu_overlap);
730     menu_frame_show(f, self->frame);
731 }
732
733 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
734 {
735     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
736         self->entry->data.normal.enabled)
737     {
738         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
739            gets freed */
740         ObMenuEntry *entry = self->entry;
741         ObMenuExecuteFunc func = self->frame->menu->execute_func;
742         gpointer data = self->frame->menu->data;
743         GSList *acts = self->entry->data.normal.actions;
744         ObClient *client = self->frame->client;
745
746         /* release grabs before executing the shit */
747         if (!(state & ControlMask))
748             menu_frame_hide_all();
749
750         if (func)
751             func(entry, state, data);
752         else {
753             GSList *it;
754
755             for (it = acts; it; it = g_slist_next(it))
756                 action_run(it->data, client, state);
757         }
758     }
759 }
760
761 void menu_frame_select_previous(ObMenuFrame *self)
762 {
763     GList *it = NULL, *start;
764
765     if (self->entries) {
766         start = it = g_list_find(self->entries, self->selected);
767         while (TRUE) {
768             ObMenuEntryFrame *e;
769
770             it = it ? g_list_previous(it) : g_list_last(self->entries);
771             if (it == start)
772                 break;
773
774             if (it) {
775                 e = it->data;
776                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
777                     break;
778                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
779                     e->entry->data.normal.enabled)
780                     break;
781             }
782         }
783     }
784     menu_frame_select(self, it ? it->data : NULL);
785 }
786
787 void menu_frame_select_next(ObMenuFrame *self)
788 {
789     GList *it = NULL, *start;
790
791     if (self->entries) {
792         start = it = g_list_find(self->entries, self->selected);
793         while (TRUE) {
794             ObMenuEntryFrame *e;
795
796             it = it ? g_list_next(it) : self->entries;
797             if (it == start)
798                 break;
799
800             if (it) {
801                 e = it->data;
802                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
803                     break;
804                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
805                     e->entry->data.normal.enabled)
806                     break;
807             }
808         }
809     }
810     menu_frame_select(self, it ? it->data : NULL);
811 }