more fixes
[dana/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 "screen.h"
8 #include "geom.h"
9 #include "plugin.h"
10 #include "misc.h"
11
12 GHashTable *menu_hash = NULL;
13 GList *menu_visible = NULL;
14
15 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
16                          LeaveWindowMask)
17 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
18 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
19                          ButtonPressMask | ButtonReleaseMask)
20
21 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
22 {
23     parse_menu_full(doc, node, data, TRUE);
24 }
25
26
27 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
28                        gboolean newmenu)
29 {
30     Action *act;
31     xmlNodePtr nact;
32
33     gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
34     ObMenu *menu = NULL, *parent;
35
36     if (newmenu == TRUE) {
37         if (!parse_attr_string("id", node, &id))
38             goto parse_menu_fail;
39         if (!parse_attr_string("label", node, &title))
40             goto parse_menu_fail;
41         ob_debug("menu label %s\n", title);
42
43         if (parse_attr_string("plugin", node, &plugin)) {
44             PluginMenuCreateData data;
45             data.doc = doc;
46             data.node = node;
47             data.parent = menu;
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     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
150                                       (GDestroyNotify)menu_destroy_hash_key,
151                                       (GDestroyNotify)menu_destroy_hash_value);
152
153     parse_register("menu", parse_menu, NULL);
154
155 }
156
157 void menu_shutdown()
158 {
159     g_hash_table_destroy(menu_hash);
160 }
161
162 static Window createWindow(Window parent, unsigned long mask,
163                            XSetWindowAttributes *attrib)
164 {
165     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
166                          RrDepth(ob_rr_inst), InputOutput,
167                          RrVisual(ob_rr_inst), mask, attrib);
168                        
169 }
170
171 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent, 
172                       menu_controller_show show, menu_controller_update update,
173                       menu_controller_selected selected,
174                       menu_controller_hide hide,
175                       menu_controller_mouseover mouseover)
176 {
177     XSetWindowAttributes attrib;
178     ObMenu *self;
179
180     self = g_new0(ObMenu, 1);
181     self->obwin.type = Window_Menu;
182     self->label = g_strdup(label);
183     self->name = g_strdup(name);
184     self->parent = parent;
185     self->open_submenu = NULL;
186     self->over = NULL;
187
188     self->entries = NULL;
189     self->shown = FALSE;
190     self->invalid = TRUE;
191
192     /* default controllers */
193     self->show = (show != NULL ? show : menu_show_full);
194     self->hide = (hide != NULL ? hide : menu_hide);
195     self->update = (update != NULL ? update : menu_render);
196     self->mouseover = (mouseover != NULL ? mouseover :
197                        menu_control_mouseover);
198     self->selected = (selected != NULL ? selected : menu_entry_fire);
199
200     self->plugin = NULL;
201     self->plugin_data = NULL;
202
203     attrib.override_redirect = TRUE;
204     attrib.event_mask = FRAME_EVENTMASK;
205     self->frame = createWindow(RootWindow(ob_display, ob_screen),
206                                CWOverrideRedirect|CWEventMask, &attrib);
207     attrib.event_mask = TITLE_EVENTMASK;
208     self->title = createWindow(self->frame, CWEventMask, &attrib);
209     self->items = createWindow(self->frame, 0, &attrib);
210
211     self->a_title = self->a_items = NULL;
212
213     XMapWindow(ob_display, self->title);
214     XMapWindow(ob_display, self->items);
215
216     g_hash_table_insert(window_map, &self->frame, self);
217     g_hash_table_insert(window_map, &self->title, self);
218     g_hash_table_insert(window_map, &self->items, self);
219     g_hash_table_insert(menu_hash, g_strdup(name), self);
220
221     stacking_add(MENU_AS_WINDOW(self));
222     stacking_raise(MENU_AS_WINDOW(self));
223
224     return self;
225 }
226
227 void menu_free(char *name)
228 {
229     g_hash_table_remove(menu_hash, name);
230 }
231
232 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
233                                ObMenuEntryRenderType render_type,
234                                gpointer submenu)
235 {
236     ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
237
238     menu_entry->label = g_strdup(label);
239     menu_entry->render_type = render_type;
240     menu_entry->action = action;
241
242     menu_entry->hilite = FALSE;
243     menu_entry->enabled = TRUE;
244
245     menu_entry->submenu = submenu;
246
247     return menu_entry;
248 }
249
250 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
251 {
252     g_assert(entry != NULL);
253     
254     entry->submenu = submenu;
255
256     if(entry->parent != NULL)
257         entry->parent->invalid = TRUE;
258 }
259
260 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
261 {
262     XSetWindowAttributes attrib;
263
264     g_assert(menu != NULL);
265     g_assert(entry != NULL);
266     g_assert(entry->item == None);
267
268     menu->entries = g_list_append(menu->entries, entry);
269     entry->parent = menu;
270
271     attrib.event_mask = ENTRY_EVENTMASK;
272     entry->item = createWindow(menu->items, CWEventMask, &attrib);
273     XMapWindow(ob_display, entry->item);
274
275     entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
276
277     menu->invalid = TRUE;
278
279     g_hash_table_insert(window_map, &entry->item, menu);
280 }
281
282 void menu_show(char *name, int x, int y, ObClient *client)
283 {
284     ObMenu *self;
285   
286     self = g_hash_table_lookup(menu_hash, name);
287     if (!self) {
288         g_warning("Attempted to show menu '%s' but it does not exist.",
289                   name);
290         return;
291     }
292
293     menu_show_full(self, x, y, client);
294 }  
295
296 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
297 {
298     g_assert(self != NULL);
299        
300     menu_render(self);
301     
302     self->client = client;
303
304     if (!self->shown) {
305         if (!(self->parent && self->parent->shown)) {
306             grab_pointer(TRUE, None);
307             grab_keyboard(TRUE);
308         }
309         menu_visible = g_list_append(menu_visible, self);
310     }
311
312     menu_control_show(self, x, y, client);
313 }
314
315 void menu_hide(ObMenu *self) {
316     if (self->shown) {
317         XUnmapWindow(ob_display, self->frame);
318         self->shown = FALSE;
319         if (self->open_submenu)
320             menu_hide(self->open_submenu);
321         if (self->parent && self->parent->open_submenu == self) {
322             self->over = NULL;
323             self->parent->open_submenu = NULL;
324         }
325
326         if (!(self->parent && self->parent->shown)) {
327             grab_keyboard(FALSE);
328             grab_pointer(FALSE, None);
329         }
330         menu_visible = g_list_remove(menu_visible, self);
331     }
332 }
333
334 void menu_clear(ObMenu *self) {
335     GList *it;
336   
337     for (it = self->entries; it; it = it->next) {
338         ObMenuEntry *entry = it->data;
339         menu_entry_free(entry);
340     }
341     self->entries = NULL;
342     self->invalid = TRUE;
343 }
344
345
346 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
347 {
348     GList *it;
349
350     for (it = menu->entries; it; it = it->next) {
351         ObMenuEntry *entry = it->data;
352         if (entry->item == win)
353             return entry;
354     }
355     return NULL;
356 }
357
358 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
359 {
360     GList *it;
361
362     for (it = menu->entries; it; it = it->next) {
363         ObMenuEntry *entry = it->data;
364         if (entry->submenu == submenu)
365             return entry;
366     }
367     return NULL;
368 }
369
370 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
371 {
372     if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
373         return NULL;
374
375     y -= menu->title_h + ob_rr_theme->bwidth;
376     if (y < 0) return NULL;
377     
378     ob_debug("%d %p\n", y/menu->item_h,
379              g_list_nth_data(menu->entries, y / menu->item_h));
380     return g_list_nth_data(menu->entries, y / menu->item_h);
381 }
382
383 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
384                      unsigned int y)
385 {
386     ObMenu *m;
387
388     if (button > 3) return;
389
390     if (self->action) {
391         self->action->data.any.c = self->parent->client;
392         self->action->func(&self->action->data);
393
394         /* hide the whole thing */
395         m = self->parent;
396         while (m->parent) m = m->parent;
397         menu_hide(m);
398     }
399 }
400
401 /* 
402    Default menu controller action for showing.
403 */
404
405 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
406 {
407     guint i;
408     Rect *a = NULL;
409
410     g_assert(!self->invalid);
411     
412     for (i = 0; i < screen_num_monitors; ++i) {
413         a = screen_physical_area_monitor(i);
414         if (RECT_CONTAINS(*a, x, y))
415             break;
416     }
417     g_assert(a != NULL);
418     self->xin_area = i;
419
420     POINT_SET(self->location,
421               MIN(x, a->x + a->width - 1 - self->size.width), 
422               MIN(y, a->y + a->height - 1 - self->size.height));
423     XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
424
425     if (!self->shown) {
426         XMapWindow(ob_display, self->frame);
427         stacking_raise(MENU_AS_WINDOW(self));
428         self->shown = TRUE;
429     } else if (self->shown && self->open_submenu) {
430         menu_hide(self->open_submenu);
431     }
432 }
433
434 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
435 {
436     int x;
437     Rect *a;
438     ObMenuEntry *e;
439
440     g_assert(self != NULL);
441     
442     if (enter) {
443         /* TODO: we prolly don't need open_submenu */
444         if (self->parent->open_submenu && self->submenu 
445             != self->parent->open_submenu)
446         {
447             e = (ObMenuEntry *) self->parent->over->data;
448             e->hilite = FALSE;
449             menu_entry_render(e);
450             menu_hide(self->parent->open_submenu);
451         }
452         
453         if (self->submenu && self->parent->open_submenu != self->submenu) {
454             self->parent->open_submenu = self->submenu;
455
456             /* shouldn't be invalid since it must be displayed */
457             g_assert(!self->parent->invalid);
458             /* TODO: I don't understand why these bevels should be here.
459                Something must be wrong in the width calculation */
460             x = self->parent->location.x + self->parent->size.width + 
461                 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
462
463             /* need to get the width. is this bad?*/
464             menu_render(self->submenu);
465
466             a = screen_physical_area_monitor(self->parent->xin_area);
467
468             if (self->submenu->size.width + x >= a->x + a->width)
469                 x = self->parent->location.x - self->submenu->size.width - 
470                     ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
471             
472             menu_show_full(self->submenu, x,
473                            self->parent->location.y + self->y,
474                            self->parent->client);
475         }
476         self->hilite = TRUE;
477         self->parent->over = g_list_find(self->parent->entries, self);
478         
479     } else
480         self->hilite = FALSE;
481     
482     menu_entry_render(self);
483 }
484
485 void menu_control_keyboard_nav(unsigned int key)
486 {
487     static ObMenu *current_menu = NULL;
488
489     ObKey obkey = OB_NUM_KEYS;
490
491     /* hrmm. could be fixed */
492     if (key == ob_keycode(OB_KEY_DOWN))
493         obkey = OB_KEY_DOWN;
494     else if (key == ob_keycode(OB_KEY_UP))
495         obkey = OB_KEY_UP;
496     else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
497         obkey = OB_KEY_RIGHT;
498     else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
499         obkey = OB_KEY_LEFT;
500     
501     if (current_menu == NULL)
502         current_menu = menu_visible->data;
503     
504     switch (obkey) {
505     case OB_KEY_DOWN: {
506         if (current_menu->over) {
507             current_menu->mouseover(current_menu->over->data, FALSE);
508             current_menu->over = (current_menu->over->next != NULL ?
509                           current_menu->over->next :
510                           current_menu->entries);
511         }
512         else
513             current_menu->over = current_menu->entries;
514
515         if (current_menu->over)
516             current_menu->mouseover(current_menu->over->data, TRUE);
517         
518         break;
519     }
520     case OB_KEY_UP: {
521         if (current_menu->over) {
522             current_menu->mouseover(current_menu->over->data, FALSE);
523             current_menu->over = (current_menu->over->prev != NULL ?
524                           current_menu->over->prev :
525                 g_list_last(current_menu->entries));
526         } else
527             current_menu->over = g_list_last(current_menu->entries);
528
529         if (current_menu->over)
530             current_menu->mouseover(current_menu->over->data, TRUE);
531         
532         break;
533     }
534     case OB_KEY_RIGHT: {
535         ObMenuEntry *e;
536         if (current_menu->over == NULL)
537             return;
538
539         e = (ObMenuEntry *)current_menu->over->data;
540         if (e->submenu) {
541             current_menu = e->submenu;
542             current_menu->over = current_menu->entries;
543             if (current_menu->over)
544                 current_menu->mouseover(current_menu->over->data, TRUE);
545         } else {
546             current_menu->mouseover(e, FALSE);
547             current_menu->over = NULL;
548             
549             /* zero is enter */
550             menu_entry_fire(e, 0, 0, 0);
551         }
552         break;
553     }
554     case OB_KEY_LEFT: {
555         if (current_menu->over != NULL) {
556             current_menu->mouseover(current_menu->over->data, FALSE);
557             current_menu->over = NULL;
558         }
559         
560         menu_hide(current_menu);
561
562         if (current_menu->parent)
563             current_menu = current_menu->parent;
564         
565         break;
566     }
567     default:
568         if (current_menu)
569             menu_hide(current_menu);
570     }
571     return;
572 }
573
574 void menu_noop()
575 {
576     /* This noop brought to you by OLS 2003 Email Garden. */
577 }