]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/menu.c
* Change xml parsing to pass the parent node, rather than the first
[mikachu/openbox.git] / openbox / menu.c
1 #include "menu.h"
2 #include "openbox.h"
3 #include "stacking.h"
4 #include "client.h"
5 #include "grab.h"
6 #include "screen.h"
7 #include "geom.h"
8 #include "plugin.h"
9 #include "misc.h"
10
11 GHashTable *menu_hash = NULL;
12 GList *menu_visible = NULL;
13
14 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
15                          LeaveWindowMask)
16 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
17 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
18                          ButtonPressMask | ButtonReleaseMask)
19
20 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
21 {
22     parse_menu_full(doc, node, data, TRUE);
23 }
24
25
26 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
27                        gboolean newmenu)
28 {
29     Action *act;
30     xmlNodePtr nact;
31
32     gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
33     ObMenu *menu = NULL, *parent;
34
35     if (newmenu == TRUE) {
36         if (!parse_attr_string("id", node, &id))
37             goto parse_menu_fail;
38         if (!parse_attr_string("label", node, &title))
39             goto parse_menu_fail;
40         g_message("menu label %s", title);
41
42         if (parse_attr_string("plugin", node, &plugin)) {
43             PluginMenuCreateData data = {
44                 .doc = doc,
45                 .node = node,
46                 .parent = menu
47             };
48             parent = plugin_create(plugin, &data);
49             g_free(plugin);
50         } else
51             menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
52             
53         if (data)
54             *((ObMenu**)data) = menu;
55     } else {
56         menu = (ObMenu *)data;
57     }
58
59     node = node->xmlChildrenNode;
60     
61     while (node) {
62         if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
63             if (parse_attr_string("plugin", node, &plugin)) {
64                 PluginMenuCreateData data;
65                 data.doc = doc;
66                 data.node = node;
67                 data.parent = menu;
68                 parent = plugin_create(plugin, &data);
69                 g_free(plugin);
70             } else {
71                 parent = menu;
72                 parse_menu(doc, node, &parent);
73                 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
74                                                             parent));
75             }
76
77         }
78         else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
79             if (parse_attr_string("label", node, &label)) {
80                 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
81                     act = action_parse(doc, nact);
82                 else
83                     act = NULL;
84                 if (act)
85                     menu_add_entry(menu, menu_entry_new(label, act));
86                 else
87                     menu_add_entry(menu, menu_entry_new_separator(label));
88                 g_free(label);
89             }
90         }
91         node = node->next;
92     }
93
94 parse_menu_fail:
95     g_free(id);
96     g_free(title);
97 }
98
99 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
100
101 void menu_destroy_hash_key(ObMenu *menu)
102 {
103     g_free(menu);
104 }
105
106 void menu_destroy_hash_value(ObMenu *self)
107 {
108     GList *it;
109
110     for (it = self->entries; it; it = it->next)
111         menu_entry_free(it->data);
112     g_list_free(self->entries);
113
114     g_free(self->label);
115     g_free(self->name);
116
117     g_hash_table_remove(window_map, &self->title);
118     g_hash_table_remove(window_map, &self->frame);
119     g_hash_table_remove(window_map, &self->items);
120
121     stacking_remove(self);
122
123     RrAppearanceFree(self->a_title);
124     RrAppearanceFree(self->a_items);
125     XDestroyWindow(ob_display, self->title);
126     XDestroyWindow(ob_display, self->frame);
127     XDestroyWindow(ob_display, self->items);
128
129     g_free(self);
130 }
131
132 void menu_entry_free(ObMenuEntry *self)
133 {
134     g_free(self->label);
135     action_free(self->action);
136
137     g_hash_table_remove(window_map, &self->item);
138
139     RrAppearanceFree(self->a_item);
140     RrAppearanceFree(self->a_disabled);
141     RrAppearanceFree(self->a_hilite);
142     XDestroyWindow(ob_display, self->item);
143
144     g_free(self);
145 }
146     
147 void menu_startup()
148 {
149 /*
150     ObMenu *m;
151     ObMenu *s;
152     ObMenu *t;
153     Action *a;
154 */
155
156     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
157                                       (GDestroyNotify)menu_destroy_hash_key,
158                                       (GDestroyNotify)menu_destroy_hash_value);
159
160     parse_register("menu", parse_menu, NULL);
161
162 /*
163     m = menu_new("sex menu", "root", NULL);
164  
165     a = action_from_string("execute");
166     a->data.execute.path = g_strdup("xterm");
167     menu_add_entry(m, menu_entry_new("xterm", a));
168     a = action_from_string("restart");
169     menu_add_entry(m, menu_entry_new("restart", a));
170     menu_add_entry(m, menu_entry_new_separator("--"));
171     a = action_from_string("exit");
172     menu_add_entry(m, menu_entry_new("exit", a));
173 */
174
175     /*
176     s = menu_new("subsex menu", "submenu", m);
177     a = action_from_string("execute");
178     a->data.execute.path = g_strdup("xclock");
179     menu_add_entry(s, menu_entry_new("xclock", a));
180
181     menu_add_entry(m, menu_entry_new_submenu("subz", s));
182
183     s = menu_new("empty", "chub", m);
184     menu_add_entry(m, menu_entry_new_submenu("empty", s));
185
186     s = menu_new("", "s-club", m);
187     menu_add_entry(m, menu_entry_new_submenu("empty", s));
188
189     s = menu_new(NULL, "h-club", m);
190     menu_add_entry(m, menu_entry_new_submenu("empty", s));
191
192     s = menu_new(NULL, "g-club", m);
193
194     a = action_from_string("execute");
195     a->data.execute.path = g_strdup("xterm");
196     menu_add_entry(s, menu_entry_new("xterm", a));
197     a = action_from_string("restart");
198     menu_add_entry(s, menu_entry_new("restart", a));
199     menu_add_entry(s, menu_entry_new_separator("--"));
200     a = action_from_string("exit");
201     menu_add_entry(s, menu_entry_new("exit", a));
202
203     menu_add_entry(m, menu_entry_new_submenu("long", s));
204     */
205 }
206
207 void menu_shutdown()
208 {
209     g_hash_table_destroy(menu_hash);
210 }
211
212 static Window createWindow(Window parent, unsigned long mask,
213                            XSetWindowAttributes *attrib)
214 {
215     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
216                          RrDepth(ob_rr_inst), InputOutput,
217                          RrVisual(ob_rr_inst), mask, attrib);
218                        
219 }
220
221 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent, 
222                     menu_controller_show show, menu_controller_update update)
223 {
224     XSetWindowAttributes attrib;
225     ObMenu *self;
226
227     self = g_new0(ObMenu, 1);
228     self->obwin.type = Window_Menu;
229     self->label = g_strdup(label);
230     self->name = g_strdup(name);
231     self->parent = parent;
232     self->open_submenu = NULL;
233
234     self->entries = NULL;
235     self->shown = FALSE;
236     self->invalid = TRUE;
237
238     /* default controllers */
239     self->show = show;
240     self->hide = NULL;
241     self->update = update;
242     self->mouseover = NULL;
243     self->selected = NULL;
244
245     self->plugin = NULL;
246     self->plugin_data = NULL;
247
248     attrib.override_redirect = TRUE;
249     attrib.event_mask = FRAME_EVENTMASK;
250     self->frame = createWindow(RootWindow(ob_display, ob_screen),
251                                CWOverrideRedirect|CWEventMask, &attrib);
252     attrib.event_mask = TITLE_EVENTMASK;
253     self->title = createWindow(self->frame, CWEventMask, &attrib);
254     self->items = createWindow(self->frame, 0, &attrib);
255
256     self->a_title = self->a_items = NULL;
257
258     XMapWindow(ob_display, self->title);
259     XMapWindow(ob_display, self->items);
260
261     g_hash_table_insert(window_map, &self->frame, self);
262     g_hash_table_insert(window_map, &self->title, self);
263     g_hash_table_insert(window_map, &self->items, self);
264     g_hash_table_insert(menu_hash, g_strdup(name), self);
265
266     stacking_add(MENU_AS_WINDOW(self));
267     stacking_raise(MENU_AS_WINDOW(self));
268
269     return self;
270 }
271
272 void menu_free(char *name)
273 {
274     g_hash_table_remove(menu_hash, name);
275 }
276
277 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
278                                ObMenuEntryRenderType render_type,
279                                gpointer submenu)
280 {
281     ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
282
283     menu_entry->label = g_strdup(label);
284     menu_entry->render_type = render_type;
285     menu_entry->action = action;
286
287     menu_entry->hilite = FALSE;
288     menu_entry->enabled = TRUE;
289
290     menu_entry->submenu = submenu;
291
292     return menu_entry;
293 }
294
295 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
296 {
297     g_assert(entry != NULL);
298     
299     entry->submenu = submenu;
300
301     if(entry->parent != NULL)
302         entry->parent->invalid = TRUE;
303 }
304
305 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
306 {
307     XSetWindowAttributes attrib;
308
309     g_assert(menu != NULL);
310     g_assert(entry != NULL);
311     g_assert(entry->item == None);
312
313     menu->entries = g_list_append(menu->entries, entry);
314     entry->parent = menu;
315
316     attrib.event_mask = ENTRY_EVENTMASK;
317     entry->item = createWindow(menu->items, CWEventMask, &attrib);
318     XMapWindow(ob_display, entry->item);
319
320     entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
321
322     menu->invalid = TRUE;
323
324     g_hash_table_insert(window_map, &entry->item, menu);
325 }
326
327 void menu_show(char *name, int x, int y, ObClient *client)
328 {
329     ObMenu *self;
330   
331     self = g_hash_table_lookup(menu_hash, name);
332     if (!self) {
333         g_warning("Attempted to show menu '%s' but it does not exist.",
334                   name);
335         return;
336     }
337
338     menu_show_full(self, x, y, client);
339 }  
340
341 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
342 {
343     g_assert(self != NULL);
344        
345     menu_render(self);
346     
347     self->client = client;
348
349     if (!self->shown) {
350         if (!(self->parent && self->parent->shown)) {
351             grab_pointer(TRUE, None);
352             grab_keyboard(TRUE);
353         }
354         menu_visible = g_list_append(menu_visible, self);
355     }
356
357     if (self->show) {
358         self->show(self, x, y, client);
359     } else {
360       menu_control_show(self, x, y, client);
361     }
362 }
363
364 void menu_hide(ObMenu *self) {
365     if (self->shown) {
366         XUnmapWindow(ob_display, self->frame);
367         self->shown = FALSE;
368         if (self->open_submenu)
369             menu_hide(self->open_submenu);
370         if (self->parent && self->parent->open_submenu == self)
371             self->parent->open_submenu = NULL;
372
373         if (!(self->parent && self->parent->shown)) {
374             grab_keyboard(FALSE);
375             grab_pointer(FALSE, None);
376         }
377         menu_visible = g_list_remove(menu_visible, self);
378     }
379 }
380
381 void menu_clear(ObMenu *self) {
382     GList *it;
383   
384     for (it = self->entries; it; it = it->next) {
385         ObMenuEntry *entry = it->data;
386         menu_entry_free(entry);
387     }
388     self->entries = NULL;
389     self->invalid = TRUE;
390 }
391
392
393 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
394 {
395     GList *it;
396
397     for (it = menu->entries; it; it = it->next) {
398         ObMenuEntry *entry = it->data;
399         if (entry->item == win)
400             return entry;
401     }
402     return NULL;
403 }
404
405 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
406 {
407     GList *it;
408
409     for (it = menu->entries; it; it = it->next) {
410         ObMenuEntry *entry = it->data;
411         if (entry->submenu == submenu)
412             return entry;
413     }
414     return NULL;
415 }
416
417 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
418 {
419     if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
420         return NULL;
421
422     y -= menu->title_h + ob_rr_theme->bwidth;
423     if (y < 0) return NULL;
424     
425     g_message ("%d %p", y/menu->item_h, g_list_nth_data(menu->entries, y / menu->item_h));
426     return g_list_nth_data(menu->entries, y / menu->item_h);
427 }
428
429 void menu_entry_fire(ObMenuEntry *self)
430 {
431     ObMenu *m;
432
433     if (self->action) {
434         self->action->data.any.c = self->parent->client;
435         self->action->func(&self->action->data);
436
437         /* hide the whole thing */
438         m = self->parent;
439         while (m->parent) m = m->parent;
440         menu_hide(m);
441     }
442 }
443
444 /* 
445    Default menu controller action for showing.
446 */
447
448 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
449 {
450     guint i;
451     Rect *a = NULL;
452
453     g_assert(!self->invalid);
454     
455     for (i = 0; i < screen_num_monitors; ++i) {
456         a = screen_physical_area_monitor(i);
457         if (RECT_CONTAINS(*a, x, y))
458             break;
459     }
460     g_assert(a != NULL);
461     self->xin_area = i;
462
463     POINT_SET(self->location,
464               MIN(x, a->x + a->width - 1 - self->size.width), 
465               MIN(y, a->y + a->height - 1 - self->size.height));
466     XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
467
468     if (!self->shown) {
469         XMapWindow(ob_display, self->frame);
470         stacking_raise(MENU_AS_WINDOW(self));
471         self->shown = TRUE;
472     } else if (self->shown && self->open_submenu) {
473         menu_hide(self->open_submenu);
474     }
475 }
476
477 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
478 {
479     int x;
480     Rect *a;
481     ObMenuEntry *e;
482
483     if (enter) {
484         if (self->parent->open_submenu && self->submenu 
485             != self->parent->open_submenu)
486         {
487             e = menu_find_entry_by_submenu(self->parent,
488                                            self->parent->open_submenu);
489             e->hilite = FALSE;
490             menu_entry_render(e);
491             menu_hide(self->parent->open_submenu);
492         }
493         
494         if (self->submenu && self->parent->open_submenu != self->submenu) {
495             self->parent->open_submenu = self->submenu;
496
497             /* shouldn't be invalid since it must be displayed */
498             g_assert(!self->parent->invalid);
499             /* TODO: I don't understand why these bevels should be here.
500                Something must be wrong in the width calculation */
501             x = self->parent->location.x + self->parent->size.width + 
502                 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
503
504             /* need to get the width. is this bad?*/
505             menu_render(self->submenu);
506
507             a = screen_physical_area_monitor(self->parent->xin_area);
508
509             if (self->submenu->size.width + x >= a->x + a->width)
510                 x = self->parent->location.x - self->submenu->size.width - 
511                     ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
512             
513             menu_show_full(self->submenu, x,
514                            self->parent->location.y + self->y,
515                            self->parent->client);
516         } 
517     }
518
519     if (enter || !self->submenu ||
520         menu_find_entry_by_submenu(self->parent,
521                                    self->parent->open_submenu) != self)
522         self->hilite = enter;
523 }
524
525 ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key)
526 {
527     GList *it = NULL;
528         
529     switch (key) {
530     case OB_KEY_DOWN: {
531         if (over != NULL) {
532             if (over->parent->mouseover)
533                 over->parent->mouseover(over, FALSE);
534             else
535                 menu_control_mouseover(over, FALSE);
536             menu_entry_render(over);
537                 
538             it = over->parent->entries;
539             while (it != NULL && it->data != over)
540                 it = it->next;
541         }
542             
543         if (it && it->next)
544             over = (ObMenuEntry *)it->next->data;
545         else if (over == NULL) {
546             if (menu_visible && ((ObMenu *)menu_visible->data)->entries)
547                 over = (ObMenuEntry *)
548                     (((ObMenu *)menu_visible->data)->entries)->data;
549             else
550                 over = NULL;
551         } else {
552             over = (over->parent->entries != NULL ?
553                     over->parent->entries->data : NULL);
554         }
555
556         if (over) {
557             if (over->parent->mouseover)
558                 over->parent->mouseover(over, TRUE);
559             else
560                 menu_control_mouseover(over, TRUE);
561             menu_entry_render(over);
562         }
563         
564         break;
565     }
566     case OB_KEY_UP: {
567         if (over != NULL) {
568             if (over->parent->mouseover)
569                 over->parent->mouseover(over, FALSE);
570             else
571                 menu_control_mouseover(over, FALSE);
572             menu_entry_render(over);
573                 
574             it = g_list_last(over->parent->entries);
575             while (it != NULL && it->data != over)
576                 it = it->prev;
577         } 
578             
579         if (it && it->prev)
580             over = (ObMenuEntry *)it->prev->data;
581         else if (over == NULL) {
582             it = g_list_last(menu_visible);
583             if (it != NULL) {
584                 it = g_list_last(((ObMenu *)it->data)->entries);
585                 over = (ObMenuEntry *)(it != NULL ? it->data : NULL);
586             }
587         } else
588             over = (over->parent->entries != NULL ?
589                     g_list_last(over->parent->entries)->data :
590                     NULL);
591
592         if (over->parent->mouseover)
593             over->parent->mouseover(over, TRUE);
594         else
595             menu_control_mouseover(over, TRUE);
596         menu_entry_render(over);
597         break;
598     }
599     case OB_KEY_RETURN: {
600         if (over == NULL)
601             return over;
602
603         if (over->submenu) {
604             if (over->parent->mouseover)
605                 over->parent->mouseover(over, FALSE);
606             else
607                 menu_control_mouseover(over, FALSE);
608             menu_entry_render(over);
609
610             if (over->submenu->entries)
611                 over = over->submenu->entries->data;
612
613             if (over->parent->mouseover)
614                 over->parent->mouseover(over, TRUE);
615             else
616                 menu_control_mouseover(over, TRUE);
617             menu_entry_render(over);
618         }
619         else {
620             if (over->parent->mouseover)
621                 over->parent->mouseover(over, FALSE);
622             else
623                 menu_control_mouseover(over, FALSE);
624             menu_entry_render(over);
625
626             menu_entry_fire(over);
627         }
628         break;
629     }
630     case OB_KEY_ESCAPE: {
631         if (over != NULL) {
632             if (over->parent->mouseover)
633                 over->parent->mouseover(over, FALSE);
634             else
635                 menu_control_mouseover(over, FALSE);
636             menu_entry_render(over);
637
638             menu_hide(over->parent);
639         } else {
640             it  = g_list_last(menu_visible);
641             if (it) {
642                 menu_hide((ObMenu *)it->data);
643             }
644         }
645         
646         over = NULL;
647         break;
648     }
649     default:
650         g_error("Unknown key");
651     }
652
653     return over;
654 }