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