new method for loading menu files etc
[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         XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
174     }
175 }
176
177 static void menu_entry_frame_render(ObMenuEntryFrame *self)
178 {
179     RrAppearance *item_a, *text_a;
180     gint th; /* temp */
181     ObMenu *sub;
182
183     item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
184                !self->entry->data.normal.enabled) ?
185               self->a_disabled :
186               (self == self->frame->selected ?
187                self->a_selected :
188                self->a_normal));
189     switch (self->entry->type) {
190     case OB_MENU_ENTRY_TYPE_NORMAL:
191     case OB_MENU_ENTRY_TYPE_SUBMENU:
192         th = self->frame->item_h;
193         break;
194     case OB_MENU_ENTRY_TYPE_SEPARATOR:
195         th = SEPARATOR_HEIGHT;
196         break;
197     }
198     RECT_SET_SIZE(self->area, self->frame->inner_w, th);
199     XResizeWindow(ob_display, self->window,
200                   self->area.width, self->area.height);
201     item_a->surface.parent = self->frame->a_items;
202     item_a->surface.parentx = self->area.x;
203     item_a->surface.parenty = self->area.y;
204     RrPaint(item_a, self->window, self->area.width, self->area.height);
205
206     text_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
207                !self->entry->data.normal.enabled) ?
208               self->a_text_disabled :
209               (self == self->frame->selected ?
210                self->a_text_selected :
211                self->a_text_normal));
212     switch (self->entry->type) {
213     case OB_MENU_ENTRY_TYPE_NORMAL:
214         text_a->texture[0].data.text.string = self->entry->data.normal.label;
215         break;
216     case OB_MENU_ENTRY_TYPE_SUBMENU:
217         sub = self->entry->data.submenu.submenu;
218         text_a->texture[0].data.text.string = sub ? sub->title : "";
219         break;
220     case OB_MENU_ENTRY_TYPE_SEPARATOR:
221         break;
222     }
223
224     switch (self->entry->type) {
225     case OB_MENU_ENTRY_TYPE_NORMAL:
226     case OB_MENU_ENTRY_TYPE_SUBMENU:
227         XMoveResizeWindow(ob_display, self->text,
228                           self->frame->text_x, 0,
229                           self->frame->text_w, self->frame->item_h);
230         text_a->surface.parent = item_a;
231         text_a->surface.parentx = self->frame->text_x;
232         text_a->surface.parenty = 0;
233         RrPaint(text_a, self->text, self->frame->text_w, self->frame->item_h);
234         XMapWindow(ob_display, self->text);
235         break;
236     case OB_MENU_ENTRY_TYPE_SEPARATOR:
237         XUnmapWindow(ob_display, self->text);
238         break;
239     }
240
241     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
242         self->entry->data.normal.icon_data)
243     {
244         XMoveResizeWindow(ob_display, self->icon, 0, 0,
245                           self->frame->item_h, self->frame->item_h);
246         self->a_icon->texture[0].data.rgba.width =
247             self->entry->data.normal.icon_width;
248         self->a_icon->texture[0].data.rgba.height =
249             self->entry->data.normal.icon_height;
250         self->a_icon->texture[0].data.rgba.data =
251             self->entry->data.normal.icon_data;
252         self->a_icon->surface.parent = item_a;
253         self->a_icon->surface.parentx = 0;
254         self->a_icon->surface.parenty = 0;
255         RrPaint(self->a_icon, self->icon,
256                 self->frame->item_h, self->frame->item_h);
257         XMapWindow(ob_display, self->icon);
258     } else
259         XUnmapWindow(ob_display, self->icon);
260
261     if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
262         XMoveResizeWindow(ob_display, self->bullet,
263                           self->frame->text_x + self->frame->text_w, 0,
264                           self->frame->item_h, self->frame->item_h);
265         self->a_bullet->surface.parent = item_a;
266         self->a_bullet->surface.parentx =
267             self->frame->text_x + self->frame->text_w;
268         self->a_bullet->surface.parenty = 0;
269         RrPaint(self->a_bullet, self->bullet,
270                 self->frame->item_h, self->frame->item_h);
271         XMapWindow(ob_display, self->bullet);
272     } else
273         XUnmapWindow(ob_display, self->bullet);
274 }
275
276 static void menu_frame_render(ObMenuFrame *self)
277 {
278     gint w = 0, h = 0;
279     gint allitems_h = 0;
280     gint tw, th; /* temps */
281     GList *it;
282     gboolean has_icon = FALSE, has_bullet = FALSE;
283     ObMenu *sub;
284
285     XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
286     XSetWindowBorder(ob_display, self->window,
287                      RrColorPixel(ob_rr_theme->b_color));
288
289     if (!self->parent && self->show_title) {
290         XMoveWindow(ob_display, self->title, 
291                     -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
292
293         self->a_title->texture[0].data.text.string = self->menu->title;
294         RrMinsize(self->a_title, &tw, &th);
295         w = MAX(w, tw);
296         h += (self->title_h = th + ob_rr_theme->bwidth);
297
298         XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
299         XSetWindowBorder(ob_display, self->title,
300                          RrColorPixel(ob_rr_theme->b_color));
301     }
302
303     XMoveWindow(ob_display, self->items, 0, h);
304
305     if (self->entries) {
306         ObMenuEntryFrame *e = self->entries->data;
307         e->a_text_normal->texture[0].data.text.string = "";
308         RrMinsize(e->a_text_normal, &tw, &th);
309         self->item_h = th;
310     } else
311         self->item_h = 0;
312
313     for (it = self->entries; it; it = g_list_next(it)) {
314         RrAppearance *text_a;
315         ObMenuEntryFrame *e = it->data;
316
317         RECT_SET_POINT(e->area, 0, allitems_h);
318         XMoveWindow(ob_display, e->window, 0, e->area.y);
319
320         text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
321                    !e->entry->data.normal.enabled) ?
322                   e->a_text_disabled :
323                   (e == self->selected ?
324                    e->a_text_selected :
325                    e->a_text_normal));
326         switch (e->entry->type) {
327         case OB_MENU_ENTRY_TYPE_NORMAL:
328             text_a->texture[0].data.text.string = e->entry->data.normal.label;
329             RrMinsize(text_a, &tw, &th);
330
331             if (e->entry->data.normal.icon_data)
332                 has_icon = TRUE;
333             break;
334         case OB_MENU_ENTRY_TYPE_SUBMENU:
335             sub = e->entry->data.submenu.submenu;
336             text_a->texture[0].data.text.string = sub ? sub->title : "";
337             RrMinsize(text_a, &tw, &th);
338
339             has_bullet = TRUE;
340             break;
341         case OB_MENU_ENTRY_TYPE_SEPARATOR:
342             tw = 0;
343             th = SEPARATOR_HEIGHT;
344             break;
345         }
346         w = MAX(w, tw);
347         h += th;
348         allitems_h += th;
349     }
350
351     self->text_x = 0;
352     self->text_w = w;
353
354     if (self->entries) {
355         if (has_bullet)
356             w += self->item_h;
357         if (has_icon) {
358             w += self->item_h;
359             self->text_x += self->item_h;
360         }
361     }
362
363     if (!w) w = 10;
364     if (!allitems_h) {
365         allitems_h = 3;
366         h += 3;
367     }
368
369     XResizeWindow(ob_display, self->window, w, h);
370     XResizeWindow(ob_display, self->items, w, allitems_h);
371
372     self->inner_w = w;
373
374     if (!self->parent && self->show_title) {
375         XResizeWindow(ob_display, self->title,
376                       w, self->title_h - ob_rr_theme->bwidth);
377         RrPaint(self->a_title, self->title,
378                 w, self->title_h - ob_rr_theme->bwidth);
379         XMapWindow(ob_display, self->title);
380     } else
381         XUnmapWindow(ob_display, self->title);
382
383     RrPaint(self->a_items, self->items, w, allitems_h);
384
385     for (it = self->entries; it; it = g_list_next(it))
386         menu_entry_frame_render(it->data);
387
388     w += ob_rr_theme->bwidth * 2;
389     h += ob_rr_theme->bwidth * 2;
390
391     RECT_SET_SIZE(self->area, w, h);
392 }
393
394 static void menu_frame_update(ObMenuFrame *self)
395 {
396     GList *mit, *fit;
397
398     menu_find_submenus(self->menu);
399
400     self->selected = NULL;
401
402     for (mit = self->menu->entries, fit = self->entries; mit && fit;
403          mit = g_list_next(mit), fit = g_list_next(fit))
404     {
405         ObMenuEntryFrame *f = fit->data;
406         f->entry = mit->data;
407     }
408
409     while (mit) {
410         ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
411         self->entries = g_list_append(self->entries, e);
412         mit = g_list_next(mit);
413     }
414     
415     while (fit) {
416         GList *n = g_list_next(fit);
417         menu_entry_frame_free(fit->data);
418         self->entries = g_list_delete_link(self->entries, fit);
419         fit = n;
420     }
421
422     menu_frame_render(self);
423 }
424
425 void menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
426 {
427     if (parent) {
428         if (parent->child)
429             menu_frame_hide(parent->child);
430         parent->child = self;
431     }
432     self->parent = parent;
433
434     if (menu_frame_visible == NULL) {
435         /* no menus shown yet */
436         grab_pointer(TRUE, None);
437         grab_keyboard(TRUE);
438     }
439
440     if (!g_list_find(menu_frame_visible, self)) {
441         menu_frame_visible = g_list_prepend(menu_frame_visible, self);
442
443         if (self->menu->update_func)
444             self->menu->update_func(self, self->menu->data);
445         menu_frame_update(self);
446     }
447
448     menu_frame_move_on_screen(self);
449
450     XMapWindow(ob_display, self->window);
451 }
452
453 void menu_frame_hide(ObMenuFrame *self)
454 {
455     menu_frame_visible = g_list_remove(menu_frame_visible, self);
456
457     if (self->child)
458         menu_frame_hide(self->child);
459
460     if (self->parent)
461         self->parent->child = NULL;
462     self->parent = NULL;
463
464     if (menu_frame_visible == NULL) {
465         /* last menu shown */
466         grab_pointer(FALSE, None);
467         grab_keyboard(FALSE);
468     }
469
470     XUnmapWindow(ob_display, self->window);
471
472     menu_frame_free(self);
473 }
474
475 void menu_frame_hide_all()
476 {
477     GList *it = g_list_last(menu_frame_visible);
478     if (it) 
479         menu_frame_hide(it->data);
480 }
481
482 void menu_frame_hide_all_client(ObClient *client)
483 {
484     GList *it = g_list_last(menu_frame_visible);
485     if (it) {
486         ObMenuFrame *f = it->data;
487         if (f->client == client)
488             menu_frame_hide(f);
489     }
490 }
491
492
493 ObMenuFrame* menu_frame_under(gint x, gint y)
494 {
495     ObMenuFrame *ret = NULL;
496     GList *it;
497
498     for (it = menu_frame_visible; it; it = g_list_next(it)) {
499         ObMenuFrame *f = it->data;
500
501         if (RECT_CONTAINS(f->area, x, y)) {
502             ret = f;
503             break;
504         }
505     }
506     return ret;
507 }
508
509 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
510 {
511     ObMenuFrame *frame;
512     ObMenuEntryFrame *ret = NULL;
513     GList *it;
514
515     if ((frame = menu_frame_under(x, y))) {
516         x -= ob_rr_theme->bwidth + frame->area.x;
517         y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
518
519         for (it = frame->entries; it; it = g_list_next(it)) {
520             ObMenuEntryFrame *e = it->data;
521
522             if (RECT_CONTAINS(e->area, x, y)) {
523                 ret = e;            
524                 break;
525             }
526         }
527     }
528     return ret;
529 }
530
531 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
532 {
533     ObMenuEntryFrame *old = self->selected;
534     ObMenuFrame *oldchild = self->child;
535
536     if (old == entry) return;
537
538     if (entry && entry->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR)
539         self->selected = entry;
540     else
541         self->selected = NULL;
542
543     if (old)
544         menu_entry_frame_render(old);
545     if (oldchild)
546         menu_frame_hide(oldchild);
547
548     if (self->selected) {
549         menu_entry_frame_render(self->selected);
550
551         if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
552             menu_entry_frame_show_submenu(self->selected);
553     }
554 }
555
556 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
557 {
558     ObMenuFrame *f;
559
560     if (!self->entry->data.submenu.submenu) return;
561
562     f = menu_frame_new(self->entry->data.submenu.submenu,
563                        self->frame->client);
564     menu_frame_move(f,
565                     self->frame->area.x + self->frame->area.width
566                     - ob_rr_theme->menu_overlap,
567                     self->frame->area.y + self->frame->title_h +
568                     self->area.y + ob_rr_theme->menu_overlap);
569     menu_frame_show(f, self->frame);
570 }
571
572 void menu_entry_frame_execute(ObMenuEntryFrame *self, gboolean hide)
573 {
574     if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
575         /* grab all this shizzle, cuz when the menu gets hidden, 'self'
576            gets freed */
577         ObMenuEntry *entry = self->entry;
578         ObMenuExecuteFunc func = self->frame->menu->execute_func;
579         gpointer data = self->frame->menu->data;
580         GSList *acts = self->entry->data.normal.actions;
581         ObClient *client = self->frame->client;
582
583         /* release grabs before executing the shit */
584         menu_frame_hide_all();
585
586         if (func)
587             func(entry, data);
588         else {
589             GSList *it;
590
591             for (it = acts; it; it = g_slist_next(it))
592             {
593                 ObAction *act = it->data;
594                 act->data.any.c = client;
595                 act->func(&act->data);
596             }
597         }
598     }
599 }