load menu plugins automatically from whats in the menu file
[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 void menu_frame_render(ObMenuFrame *self);
20 static void menu_frame_update(ObMenuFrame *self);
21
22 static Window createWindow(Window parent, unsigned long mask,
23                            XSetWindowAttributes *attrib)
24 {
25     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
26                          RrDepth(ob_rr_inst), InputOutput,
27                          RrVisual(ob_rr_inst), mask, attrib);
28                        
29 }
30
31 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
32 {
33     ObMenuFrame *self;
34     XSetWindowAttributes attr;
35
36     self = g_new0(ObMenuFrame, 1);
37     self->type = Window_Menu;
38     self->menu = menu;
39     self->selected = NULL;
40     self->show_title = TRUE;
41     self->client = client;
42
43     attr.event_mask = FRAME_EVENTMASK;
44     self->window = createWindow(RootWindow(ob_display, ob_screen),
45                                    CWEventMask, &attr);
46     attr.event_mask = TITLE_EVENTMASK;
47     self->title = createWindow(self->window, CWEventMask, &attr);
48     self->items = createWindow(self->window, 0, NULL);
49
50     XMapWindow(ob_display, self->items);
51
52     self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
53     self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
54
55     stacking_add(MENU_AS_WINDOW(self));
56
57     return self;
58 }
59
60 void menu_frame_free(ObMenuFrame *self)
61 {
62     if (self) {
63         stacking_remove(MENU_AS_WINDOW(self));
64
65         XDestroyWindow(ob_display, self->items);
66         XDestroyWindow(ob_display, self->title);
67         XDestroyWindow(ob_display, self->window);
68
69         RrAppearanceFree(self->a_items);
70         RrAppearanceFree(self->a_title);
71
72         g_free(self);
73     }
74 }
75
76 ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry, ObMenuFrame *frame)
77 {
78     ObMenuEntryFrame *self;
79     XSetWindowAttributes attr;
80
81     self = g_new0(ObMenuEntryFrame, 1);
82     self->entry = entry;
83     self->frame = frame;
84
85     attr.event_mask = ENTRY_EVENTMASK;
86     self->window = createWindow(self->frame->items, CWEventMask, &attr);
87     self->icon = createWindow(self->window, 0, NULL);
88     self->text = createWindow(self->window, 0, NULL);
89     self->bullet = createWindow(self->window, 0, NULL);
90
91     XMapWindow(ob_display, self->window);
92     XMapWindow(ob_display, self->text);
93
94     self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_item);
95     self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
96     self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_hilite);
97
98     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
99     self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
100     self->a_bullet = RrAppearanceCopy(ob_rr_theme->a_menu_bullet);
101     self->a_bullet->texture[0].type = RR_TEXTURE_MASK;
102
103     self->a_text_normal =
104         RrAppearanceCopy(ob_rr_theme->a_menu_text_item);
105     self->a_text_disabled =
106         RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
107     self->a_text_selected =
108         RrAppearanceCopy(ob_rr_theme->a_menu_text_hilite);
109
110     return self;
111 }
112
113 void menu_entry_frame_free(ObMenuEntryFrame *self)
114 {
115     if (self) {
116         XDestroyWindow(ob_display, self->icon);
117         XDestroyWindow(ob_display, self->text);
118         XDestroyWindow(ob_display, self->bullet);
119         XDestroyWindow(ob_display, self->window);
120
121         RrAppearanceFree(self->a_normal);
122         RrAppearanceFree(self->a_disabled);
123         RrAppearanceFree(self->a_selected);
124
125         RrAppearanceFree(self->a_icon);
126         RrAppearanceFree(self->a_text_normal);
127         RrAppearanceFree(self->a_text_disabled);
128         RrAppearanceFree(self->a_text_selected);
129         RrAppearanceFree(self->a_bullet);
130
131         g_free(self);
132     }
133 }
134
135 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
136 {
137     /* XXX screen constraints */
138     RECT_SET_POINT(self->area, x, y);
139     XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
140 }
141
142 void menu_frame_move_on_screen(ObMenuFrame *self)
143 {
144     Rect *a;
145     guint i;
146     gint dx = 0, dy = 0;
147
148     for (i = 0; i < screen_num_monitors; ++i) {
149         a = screen_physical_area_monitor(i);
150         if (RECT_INTERSECTS_RECT(*a, self->area))
151             break;
152     }
153     if (a) a = screen_physical_area_monitor(0);
154
155     dx = MIN(0, (a->x + a->width) - (self->area.x + self->area.width));
156     dy = MIN(0, (a->y + a->height) - (self->area.y + self->area.height));
157     if (!dx) dx = MAX(0, a->x - self->area.x);
158     if (!dy) dy = MAX(0, a->y - self->area.y);
159
160     if (dx || dy) {
161         ObMenuFrame *f;
162
163         for (f = self; f; f = f->parent)
164             menu_frame_move(f, f->area.x + dx, f->area.y + dy);
165         XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
166     }
167 }
168
169 static void menu_entry_frame_render(ObMenuEntryFrame *self)
170 {
171     RrAppearance *item_a, *text_a;
172     gint th; /* temp */
173
174     item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
175                !self->entry->data.normal.enabled) ?
176               self->a_disabled :
177               (self == self->frame->selected ?
178                self->a_selected :
179                self->a_normal));
180     switch (self->entry->type) {
181     case OB_MENU_ENTRY_TYPE_NORMAL:
182     case OB_MENU_ENTRY_TYPE_SUBMENU:
183         th = self->frame->item_h;
184         break;
185     case OB_MENU_ENTRY_TYPE_SEPARATOR:
186         th = SEPARATOR_HEIGHT;
187         break;
188     }
189     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
190     XResizeWindow(ob_display, self->window,
191                   self->area.width, self->area.height);
192     item_a->surface.parent = self->frame->a_items;
193     item_a->surface.parentx = self->area.x;
194     item_a->surface.parenty = self->area.y;
195     RrPaint(item_a, self->window, self->area.width, self->area.height);
196
197     text_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
198                !self->entry->data.normal.enabled) ?
199               self->a_text_disabled :
200               (self == self->frame->selected ?
201                self->a_text_selected :
202                self->a_text_normal));
203     switch (self->entry->type) {
204     case OB_MENU_ENTRY_TYPE_NORMAL:
205         text_a->texture[0].data.text.string = self->entry->data.normal.label;
206         break;
207     case OB_MENU_ENTRY_TYPE_SUBMENU:
208         text_a->texture[0].data.text.string =
209             self->entry->data.submenu.submenu->title;
210         break;
211     case OB_MENU_ENTRY_TYPE_SEPARATOR:
212         break;
213     }
214
215     switch (self->entry->type) {
216     case OB_MENU_ENTRY_TYPE_NORMAL:
217     case OB_MENU_ENTRY_TYPE_SUBMENU:
218         XMoveResizeWindow(ob_display, self->text,
219                           self->frame->text_x, 0,
220                           self->frame->text_w, self->frame->item_h);
221         text_a->surface.parent = item_a;
222         text_a->surface.parentx = self->frame->text_x;
223         text_a->surface.parenty = 0;
224         RrPaint(text_a, self->text, self->frame->text_w, self->frame->item_h);
225         XMapWindow(ob_display, self->text);
226         break;
227     case OB_MENU_ENTRY_TYPE_SEPARATOR:
228         XUnmapWindow(ob_display, self->text);
229         break;
230     }
231
232     /* XXX draw icons */
233     if (0) {
234         XMoveResizeWindow(ob_display, self->icon, 0, 0,
235                           self->frame->item_h, self->frame->item_h);
236         /* XXX set the RGBA surface stuff */
237         self->a_icon->surface.parent = item_a;
238         self->a_icon->surface.parentx = 0;
239         self->a_icon->surface.parenty = 0;
240         RrPaint(self->a_icon, self->icon,
241                 self->frame->item_h, self->frame->item_h);
242         XMapWindow(ob_display, self->icon);
243     } else
244         XUnmapWindow(ob_display, self->icon);
245
246     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
247         XMoveResizeWindow(ob_display, self->bullet,
248                           self->frame->text_x + self->frame->text_w, 0,
249                           self->frame->item_h, self->frame->item_h);
250         self->a_bullet->surface.parent = item_a;
251         self->a_bullet->surface.parentx =
252             self->frame->text_x + self->frame->text_w;
253         self->a_bullet->surface.parenty = 0;
254         RrPaint(self->a_bullet, self->bullet,
255                 self->frame->item_h, self->frame->item_h);
256         XMapWindow(ob_display, self->bullet);
257     } else
258         XUnmapWindow(ob_display, self->bullet);
259 }
260
261 static void menu_frame_render(ObMenuFrame *self)
262 {
263     gint w = 0, h = 0;
264     gint allitems_h = 0;
265     gint tw, th; /* temps */
266     GList *it;
267     gboolean has_icon = FALSE, has_bullet = FALSE;
268
269     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
270     XSetWindowBorder(ob_display, self->window,
271                      RrColorPixel(ob_rr_theme->b_color));
272
273     if (!self->parent && self->show_title) {
274         XMoveWindow(ob_display, self->title, 
275                     -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
276
277         self->a_title->texture[0].data.text.string = self->menu->title;
278         RrMinsize(self->a_title, &tw, &th);
279         w = MAX(w, tw);
280         h += (self->title_h = th + ob_rr_theme->bwidth);
281
282         XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
283         XSetWindowBorder(ob_display, self->title,
284                          RrColorPixel(ob_rr_theme->b_color));
285     }
286
287     XMoveWindow(ob_display, self->items, 0, h);
288
289     if (self->entries) {
290         ObMenuEntryFrame *e = self->entries->data;
291         e->a_text_normal->texture[0].data.text.string = "";
292         RrMinsize(e->a_text_normal, &tw, &th);
293         self->item_h = th;
294     } else
295         self->item_h = 0;
296
297     for (it = self->entries; it; it = g_list_next(it)) {
298         RrAppearance *text_a;
299         ObMenuEntryFrame *e = it->data;
300
301         RECT_SET_POINT(e->area, 0, allitems_h);
302         XMoveWindow(ob_display, e->window, 0, e->area.y);
303
304         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
305                    !e->entry->data.normal.enabled) ?
306                   e->a_text_disabled :
307                   (e == self->selected ?
308                    e->a_text_selected :
309                    e->a_text_normal));
310         switch (e->entry->type) {
311         case OB_MENU_ENTRY_TYPE_NORMAL:
312             text_a->texture[0].data.text.string = e->entry->data.normal.label;
313             RrMinsize(text_a, &tw, &th);
314
315             /* XXX has_icon = TRUE; */
316             break;
317         case OB_MENU_ENTRY_TYPE_SUBMENU:
318             text_a->texture[0].data.text.string =
319                 e->entry->data.submenu.submenu->title;
320             RrMinsize(text_a, &tw, &th);
321
322             has_bullet = TRUE;
323             break;
324         case OB_MENU_ENTRY_TYPE_SEPARATOR:
325             tw = 0;
326             th = SEPARATOR_HEIGHT;
327             break;
328         }
329         w = MAX(w, tw);
330         h += th;
331         allitems_h += th;
332     }
333
334     self->text_x = 0;
335     self->text_w = w;
336
337     if (self->entries) {
338         if (has_bullet)
339             w += self->item_h;
340         if (has_icon) {
341             w += self->item_h;
342             self->text_x += self->item_h;
343         }
344     }
345
346     if (!w) w = 10;
347     if (!allitems_h) allitems_h = 3;
348     if (!h) h = 3;
349
350     XResizeWindow(ob_display, self->window, w, h);
351     XResizeWindow(ob_display, self->items, w, allitems_h);
352
353     self->inner_w = w;
354
355     if (!self->parent && self->show_title) {
356         XResizeWindow(ob_display, self->title,
357                       w, self->title_h - ob_rr_theme->bwidth);
358         RrPaint(self->a_title, self->title,
359                 w, self->title_h - ob_rr_theme->bwidth);
360         XMapWindow(ob_display, self->title);
361     } else
362         XUnmapWindow(ob_display, self->title);
363
364     RrPaint(self->a_items, self->items, w, allitems_h);
365
366     for (it = self->entries; it; it = g_list_next(it))
367         menu_entry_frame_render(it->data);
368
369     w += ob_rr_theme->bwidth * 2;
370     h += ob_rr_theme->bwidth * 2;
371
372     RECT_SET_SIZE(self->area, w, h);
373 }
374
375 static void menu_frame_update(ObMenuFrame *self)
376 {
377     GList *mit, *fit;
378
379     self->selected = NULL;
380
381     for (mit = self->menu->entries, fit = self->entries; mit && fit;
382          mit = g_list_next(mit), fit = g_list_next(fit))
383     {
384         ObMenuEntryFrame *f = fit->data;
385         f->entry = mit->data;
386     }
387
388     while (mit) {
389         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
390         self->entries = g_list_append(self->entries, e);
391         mit = g_list_next(mit);
392     }
393     
394     while (fit) {
395         GList *n = g_list_next(fit);
396         menu_entry_frame_free(fit->data);
397         self->entries = g_list_delete_link(self->entries, fit);
398         fit = n;
399     }
400
401     menu_frame_render(self);
402 }
403
404 void menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
405 {
406     if (parent) {
407         if (parent->child)
408             menu_frame_hide(parent->child);
409         parent->child = self;
410     }
411     self->parent = parent;
412
413     if (menu_frame_visible == NULL) {
414         /* no menus shown yet */
415         grab_pointer(TRUE, None);
416         grab_keyboard(TRUE);
417     }
418
419     if (!g_list_find(menu_frame_visible, self)) {
420         menu_frame_visible = g_list_prepend(menu_frame_visible, self);
421
422         if (self->menu->update_func)
423             self->menu->update_func(self, self->menu->data);
424         menu_frame_update(self);
425     }
426
427     menu_frame_move_on_screen(self);
428
429     XMapWindow(ob_display, self->window);
430 }
431
432 void menu_frame_hide(ObMenuFrame *self)
433 {
434     menu_frame_visible = g_list_remove(menu_frame_visible, self);
435
436     if (self->child)
437         menu_frame_hide(self->child);
438
439     if (self->parent)
440         self->parent->child = NULL;
441     self->parent = NULL;
442
443     if (menu_frame_visible == NULL) {
444         /* last menu shown */
445         grab_pointer(FALSE, None);
446         grab_keyboard(FALSE);
447     }
448
449     XUnmapWindow(ob_display, self->window);
450
451     menu_frame_free(self);
452 }
453
454 void menu_frame_hide_all()
455 {
456     GList *it = g_list_last(menu_frame_visible);
457     if (it) 
458         menu_frame_hide(it->data);
459 }
460
461 void menu_frame_hide_all_client(ObClient *client)
462 {
463     GList *it = g_list_last(menu_frame_visible);
464     if (it) {
465         ObMenuFrame *f = it->data;
466         if (f->client == client)
467             menu_frame_hide(f);
468     }
469 }
470
471
472 ObMenuFrame* menu_frame_under(gint x, gint y)
473 {
474     ObMenuFrame *ret = NULL;
475     GList *it;
476
477     for (it = menu_frame_visible; it; it = g_list_next(it)) {
478         ObMenuFrame *f = it->data;
479
480         if (RECT_CONTAINS(f->area, x, y)) {
481             ret = f;
482             break;
483         }
484     }
485     return ret;
486 }
487
488 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
489 {
490     ObMenuFrame *frame;
491     ObMenuEntryFrame *ret = NULL;
492     GList *it;
493
494     if ((frame = menu_frame_under(x, y))) {
495         x -= ob_rr_theme->bwidth + frame->area.x;
496         y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
497
498         for (it = frame->entries; it; it = g_list_next(it)) {
499             ObMenuEntryFrame *e = it->data;
500
501             if (RECT_CONTAINS(e->area, x, y)) {
502                 ret = e;            
503                 break;
504             }
505         }
506     }
507     return ret;
508 }
509
510 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
511 {
512     ObMenuEntryFrame *old = self->selected;
513     ObMenuFrame *oldchild = self->child;
514
515     if (old == entry) return;
516
517     if (entry && entry->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR)
518         self->selected = entry;
519     else
520         self->selected = NULL;
521
522     if (old)
523         menu_entry_frame_render(old);
524     if (oldchild)
525         menu_frame_hide(oldchild);
526
527     if (self->selected) {
528         menu_entry_frame_render(self->selected);
529
530         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
531             menu_entry_frame_show_submenu(self->selected);
532     }
533 }
534
535 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
536 {
537     ObMenuFrame *f;
538
539     f = menu_frame_new(self->entry->data.submenu.submenu,
540                        self->frame->client);
541     menu_frame_move(f,
542                     self->frame->area.x + self->frame->area.width
543                     - ob_rr_theme->menu_overlap,
544                     self->frame->area.y + self->frame->title_h +
545                     self->area.y + ob_rr_theme->menu_overlap);
546     menu_frame_show(f, self->frame);
547 }
548
549 void menu_entry_frame_execute(ObMenuEntryFrame *self)
550 {
551     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
552         GSList *it;
553
554         /* release grabs before executing the shit */
555         menu_frame_hide_all();
556
557         for (it = self->entry->data.normal.actions; it;
558              it = g_slist_next(it))
559         {
560             ObAction *act = it->data;
561             act->data.any.c = self->frame->client;
562             act->func(&act->data);
563         }
564     }
565 }