make openbox base-dir spec compliant, and change the theme dir structure, so that...
[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);
402         w = MAX(w, tw);
403         h += (self->title_h = th + ob_rr_theme->bwidth);
404
405         XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
406         XSetWindowBorder(ob_display, self->title,
407                          RrColorPixel(ob_rr_theme->b_color));
408     }
409
410     XMoveWindow(ob_display, self->items, 0, h);
411
412     STRUT_SET(self->item_margin, 0, 0, 0, 0);
413
414     if (self->entries) {
415         ObMenuEntryFrame *e = self->entries->data;
416         gint l, t, r, b;
417
418         e->a_text_normal->texture[0].data.text.string = "";
419         RrMinsize(e->a_text_normal, &tw, &th);
420         tw += 2*PADDING;
421         th += 2*PADDING;
422         self->item_h = th;
423
424         RrMargins(e->a_normal, &l, &t, &r, &b);
425         STRUT_SET(self->item_margin,
426                   MAX(self->item_margin.left, l),
427                   MAX(self->item_margin.top, t),
428                   MAX(self->item_margin.right, r),
429                   MAX(self->item_margin.bottom, b));
430         RrMargins(e->a_selected, &l, &t, &r, &b);
431         STRUT_SET(self->item_margin,
432                   MAX(self->item_margin.left, l),
433                   MAX(self->item_margin.top, t),
434                   MAX(self->item_margin.right, r),
435                   MAX(self->item_margin.bottom, b));
436         RrMargins(e->a_disabled, &l, &t, &r, &b);
437         STRUT_SET(self->item_margin,
438                   MAX(self->item_margin.left, l),
439                   MAX(self->item_margin.top, t),
440                   MAX(self->item_margin.right, r),
441                   MAX(self->item_margin.bottom, b));
442     } else
443         self->item_h = 0;
444
445     for (it = self->entries; it; it = g_list_next(it)) {
446         RrAppearance *text_a;
447         ObMenuEntryFrame *e = it->data;
448
449         RECT_SET_POINT(e->area, 0, allitems_h);
450         XMoveWindow(ob_display, e->window, 0, e->area.y);
451
452         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
453                    !e->entry->data.normal.enabled) ?
454                   e->a_text_disabled :
455                   (e == self->selected ?
456                    e->a_text_selected :
457                    e->a_text_normal));
458         switch (e->entry->type) {
459         case OB_MENU_ENTRY_TYPE_NORMAL:
460             text_a->texture[0].data.text.string = e->entry->data.normal.label;
461             RrMinsize(text_a, &tw, &th);
462             tw = MIN(tw, MAX_MENU_WIDTH);
463
464             if (e->entry->data.normal.icon_data ||
465                 e->entry->data.normal.mask)
466                 has_icon = TRUE;
467             break;
468         case OB_MENU_ENTRY_TYPE_SUBMENU:
469             sub = e->entry->data.submenu.submenu;
470             text_a->texture[0].data.text.string = sub ? sub->title : "";
471             RrMinsize(text_a, &tw, &th);
472             tw = MIN(tw, MAX_MENU_WIDTH);
473
474             if (e->entry->data.normal.icon_data ||
475                 e->entry->data.normal.mask)
476                 has_icon = TRUE;
477
478             tw += self->item_h - PADDING;
479             break;
480         case OB_MENU_ENTRY_TYPE_SEPARATOR:
481             tw = 0;
482             th = SEPARATOR_HEIGHT;
483             break;
484         }
485         tw += 2*PADDING;
486         th += 2*PADDING;
487         w = MAX(w, tw);
488         h += th;
489         allitems_h += th;
490     }
491
492     self->text_x = PADDING;
493     self->text_w = w;
494
495     if (self->entries) {
496         if (has_icon) {
497             w += self->item_h + PADDING;
498             self->text_x += self->item_h + PADDING;
499         }
500     }
501
502     if (!w) w = 10;
503     if (!allitems_h) {
504         allitems_h = 3;
505         h += 3;
506     }
507
508     XResizeWindow(ob_display, self->window, w, h);
509     XResizeWindow(ob_display, self->items, w, allitems_h);
510
511     self->inner_w = w;
512
513     if (!self->parent && self->show_title) {
514         XResizeWindow(ob_display, self->title,
515                       w, self->title_h - ob_rr_theme->bwidth);
516         RrPaint(self->a_title, self->title,
517                 w, self->title_h - ob_rr_theme->bwidth);
518         XMapWindow(ob_display, self->title);
519     } else
520         XUnmapWindow(ob_display, self->title);
521
522     RrPaint(self->a_items, self->items, w, allitems_h);
523
524     for (it = self->entries; it; it = g_list_next(it))
525         menu_entry_frame_render(it->data);
526
527     w += ob_rr_theme->bwidth * 2;
528     h += ob_rr_theme->bwidth * 2;
529
530     RECT_SET_SIZE(self->area, w, h);
531
532     XFlush(ob_display);
533 }
534
535 static void menu_frame_update(ObMenuFrame *self)
536 {
537     GList *mit, *fit;
538
539     menu_pipe_execute(self->menu);
540     menu_find_submenus(self->menu);
541
542     self->selected = NULL;
543
544     for (mit = self->menu->entries, fit = self->entries; mit && fit;
545          mit = g_list_next(mit), fit = g_list_next(fit))
546     {
547         ObMenuEntryFrame *f = fit->data;
548         f->entry = mit->data;
549     }
550
551     while (mit) {
552         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
553         self->entries = g_list_append(self->entries, e);
554         mit = g_list_next(mit);
555     }
556     
557     while (fit) {
558         GList *n = g_list_next(fit);
559         menu_entry_frame_free(fit->data);
560         self->entries = g_list_delete_link(self->entries, fit);
561         fit = n;
562     }
563
564     menu_frame_render(self);
565 }
566
567 void menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
568 {
569     GList *it;
570
571     if (g_list_find(menu_frame_visible, self))
572         return;
573
574     if (parent) {
575         if (parent->child)
576             menu_frame_hide(parent->child);
577         parent->child = self;
578     }
579     self->parent = parent;
580
581     if (menu_frame_visible == NULL) {
582         /* no menus shown yet */
583         grab_pointer(TRUE, OB_CURSOR_NONE);
584         grab_keyboard(TRUE);
585     }
586
587     /* determine if the underlying menu is already visible */
588     for (it = menu_frame_visible; it; it = g_list_next(it)) {
589         ObMenuFrame *f = it->data;
590         if (f->menu == self->menu)
591             break;
592     }
593     if (!it) {
594         if (self->menu->update_func)
595             self->menu->update_func(self, self->menu->data);
596     }
597
598     menu_frame_update(self);
599
600     menu_frame_visible = g_list_prepend(menu_frame_visible, self);
601
602     menu_frame_move_on_screen(self);
603
604     XMapWindow(ob_display, self->window);
605 }
606
607 void menu_frame_hide(ObMenuFrame *self)
608 {
609     GList *it = g_list_find(menu_frame_visible, self);
610
611     if (!it)
612         return;
613
614     if (self->child)
615         menu_frame_hide(self->child);
616
617     if (self->parent)
618         self->parent->child = NULL;
619     self->parent = NULL;
620
621     menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
622
623     if (menu_frame_visible == NULL) {
624         /* last menu shown */
625         grab_pointer(FALSE, OB_CURSOR_NONE);
626         grab_keyboard(FALSE);
627     }
628
629     XUnmapWindow(ob_display, self->window);
630
631     menu_frame_free(self);
632 }
633
634 void menu_frame_hide_all()
635 {
636     GList *it = g_list_last(menu_frame_visible);
637     if (it) 
638         menu_frame_hide(it->data);
639 }
640
641 void menu_frame_hide_all_client(ObClient *client)
642 {
643     GList *it = g_list_last(menu_frame_visible);
644     if (it) {
645         ObMenuFrame *f = it->data;
646         if (f->client == client)
647             menu_frame_hide(f);
648     }
649 }
650
651
652 ObMenuFrame* menu_frame_under(gint x, gint y)
653 {
654     ObMenuFrame *ret = NULL;
655     GList *it;
656
657     for (it = menu_frame_visible; it; it = g_list_next(it)) {
658         ObMenuFrame *f = it->data;
659
660         if (RECT_CONTAINS(f->area, x, y)) {
661             ret = f;
662             break;
663         }
664     }
665     return ret;
666 }
667
668 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
669 {
670     ObMenuFrame *frame;
671     ObMenuEntryFrame *ret = NULL;
672     GList *it;
673
674     if ((frame = menu_frame_under(x, y))) {
675         x -= ob_rr_theme->bwidth + frame->area.x;
676         y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
677
678         for (it = frame->entries; it; it = g_list_next(it)) {
679             ObMenuEntryFrame *e = it->data;
680
681             if (RECT_CONTAINS(e->area, x, y)) {
682                 ret = e;            
683                 break;
684             }
685         }
686     }
687     return ret;
688 }
689
690 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
691 {
692     ObMenuEntryFrame *old = self->selected;
693     ObMenuFrame *oldchild = self->child;
694
695     if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
696         entry = old;
697
698     if (old == entry) return;
699
700     self->selected = entry;
701
702     if (old)
703         menu_entry_frame_render(old);
704     if (oldchild)
705         menu_frame_hide(oldchild);
706
707     if (self->selected) {
708         menu_entry_frame_render(self->selected);
709
710         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
711             menu_entry_frame_show_submenu(self->selected);
712     }
713 }
714
715 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
716 {
717     ObMenuFrame *f;
718
719     if (!self->entry->data.submenu.submenu) return;
720
721     f = menu_frame_new(self->entry->data.submenu.submenu,
722                        self->frame->client);
723     menu_frame_move(f,
724                     self->frame->area.x + self->frame->area.width
725                     - ob_rr_theme->menu_overlap - ob_rr_theme->bwidth,
726                     self->frame->area.y + self->frame->title_h +
727                     self->area.y + ob_rr_theme->menu_overlap);
728     menu_frame_show(f, self->frame);
729 }
730
731 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
732 {
733     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
734         self->entry->data.normal.enabled)
735     {
736         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
737            gets freed */
738         ObMenuEntry *entry = self->entry;
739         ObMenuExecuteFunc func = self->frame->menu->execute_func;
740         gpointer data = self->frame->menu->data;
741         GSList *acts = self->entry->data.normal.actions;
742         ObClient *client = self->frame->client;
743
744         /* release grabs before executing the shit */
745         if (!(state & ControlMask))
746             menu_frame_hide_all();
747
748         if (func)
749             func(entry, state, data);
750         else {
751             GSList *it;
752
753             for (it = acts; it; it = g_slist_next(it))
754                 action_run(it->data, client, state);
755         }
756     }
757 }
758
759 void menu_frame_select_previous(ObMenuFrame *self)
760 {
761     GList *it = NULL, *start;
762
763     if (self->entries) {
764         start = it = g_list_find(self->entries, self->selected);
765         while (TRUE) {
766             ObMenuEntryFrame *e;
767
768             it = it ? g_list_previous(it) : g_list_last(self->entries);
769             if (it == start)
770                 break;
771
772             if (it) {
773                 e = it->data;
774                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
775                     break;
776                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
777                     e->entry->data.normal.enabled)
778                     break;
779             }
780         }
781     }
782     menu_frame_select(self, it ? it->data : NULL);
783 }
784
785 void menu_frame_select_next(ObMenuFrame *self)
786 {
787     GList *it = NULL, *start;
788
789     if (self->entries) {
790         start = it = g_list_find(self->entries, self->selected);
791         while (TRUE) {
792             ObMenuEntryFrame *e;
793
794             it = it ? g_list_next(it) : self->entries;
795             if (it == start)
796                 break;
797
798             if (it) {
799                 e = it->data;
800                 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
801                     break;
802                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
803                     e->entry->data.normal.enabled)
804                     break;
805             }
806         }
807     }
808     menu_frame_select(self, it ? it->data : NULL);
809 }