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