]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
use ob_debug for any debug printing and only display the output when its a debug...
[chaz/openbox] / 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 /*
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 ObMenuEntry *e;
372
373 self->parent->open_submenu = NULL;
374
375 e = menu_find_entry_by_submenu(self->parent, self);
376 if (self->parent->mouseover)
377 self->parent->mouseover(e, FALSE);
378 else
379 menu_control_mouseover(e, FALSE);
380 menu_entry_render(e);
381 }
382
383 if (!(self->parent && self->parent->shown)) {
384 grab_keyboard(FALSE);
385 grab_pointer(FALSE, None);
386 }
387 menu_visible = g_list_remove(menu_visible, self);
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)
441 {
442 ObMenu *m;
443
444 if (self->action) {
445 self->action->data.any.c = self->parent->client;
446 self->action->func(&self->action->data);
447
448 /* hide the whole thing */
449 m = self->parent;
450 while (m->parent) m = m->parent;
451 menu_hide(m);
452 }
453 }
454
455 /*
456 Default menu controller action for showing.
457 */
458
459 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
460 {
461 guint i;
462 Rect *a = NULL;
463
464 g_assert(!self->invalid);
465
466 for (i = 0; i < screen_num_monitors; ++i) {
467 a = screen_physical_area_monitor(i);
468 if (RECT_CONTAINS(*a, x, y))
469 break;
470 }
471 g_assert(a != NULL);
472 self->xin_area = i;
473
474 POINT_SET(self->location,
475 MIN(x, a->x + a->width - 1 - self->size.width),
476 MIN(y, a->y + a->height - 1 - self->size.height));
477 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
478
479 if (!self->shown) {
480 XMapWindow(ob_display, self->frame);
481 stacking_raise(MENU_AS_WINDOW(self));
482 self->shown = TRUE;
483 } else if (self->shown && self->open_submenu) {
484 menu_hide(self->open_submenu);
485 }
486 }
487
488 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
489 {
490 int x;
491 Rect *a;
492 ObMenuEntry *e;
493
494 if (enter) {
495 if (self->parent->open_submenu && self->submenu
496 != self->parent->open_submenu)
497 {
498 e = menu_find_entry_by_submenu(self->parent,
499 self->parent->open_submenu);
500 e->hilite = FALSE;
501 menu_entry_render(e);
502 menu_hide(self->parent->open_submenu);
503 }
504
505 if (self->submenu && self->parent->open_submenu != self->submenu) {
506 self->parent->open_submenu = self->submenu;
507
508 /* shouldn't be invalid since it must be displayed */
509 g_assert(!self->parent->invalid);
510 /* TODO: I don't understand why these bevels should be here.
511 Something must be wrong in the width calculation */
512 x = self->parent->location.x + self->parent->size.width +
513 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
514
515 /* need to get the width. is this bad?*/
516 menu_render(self->submenu);
517
518 a = screen_physical_area_monitor(self->parent->xin_area);
519
520 if (self->submenu->size.width + x >= a->x + a->width)
521 x = self->parent->location.x - self->submenu->size.width -
522 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
523
524 menu_show_full(self->submenu, x,
525 self->parent->location.y + self->y,
526 self->parent->client);
527 }
528 }
529
530 if (enter || !self->submenu ||
531 menu_find_entry_by_submenu(self->parent,
532 self->parent->open_submenu) != self)
533 self->hilite = enter;
534 }
535
536 ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key)
537 {
538 GList *it = NULL;
539
540 switch (key) {
541 case OB_KEY_DOWN: {
542 if (over != NULL) {
543 if (over->parent->mouseover)
544 over->parent->mouseover(over, FALSE);
545 else
546 menu_control_mouseover(over, FALSE);
547 menu_entry_render(over);
548
549 it = over->parent->entries;
550 while (it != NULL && it->data != over)
551 it = it->next;
552 }
553
554 if (it && it->next)
555 over = (ObMenuEntry *)it->next->data;
556 else if (over == NULL) {
557 if (menu_visible && ((ObMenu *)menu_visible->data)->entries)
558 over = (ObMenuEntry *)
559 (((ObMenu *)menu_visible->data)->entries)->data;
560 else
561 over = NULL;
562 } else {
563 over = (over->parent->entries != NULL ?
564 over->parent->entries->data : NULL);
565 }
566
567 if (over) {
568 if (over->parent->mouseover)
569 over->parent->mouseover(over, TRUE);
570 else
571 menu_control_mouseover(over, TRUE);
572 menu_entry_render(over);
573 }
574
575 break;
576 }
577 case OB_KEY_UP: {
578 if (over != NULL) {
579 if (over->parent->mouseover)
580 over->parent->mouseover(over, FALSE);
581 else
582 menu_control_mouseover(over, FALSE);
583 menu_entry_render(over);
584
585 it = g_list_last(over->parent->entries);
586 while (it != NULL && it->data != over)
587 it = it->prev;
588 }
589
590 if (it && it->prev)
591 over = (ObMenuEntry *)it->prev->data;
592 else if (over == NULL) {
593 it = g_list_last(menu_visible);
594 if (it != NULL) {
595 it = g_list_last(((ObMenu *)it->data)->entries);
596 over = (ObMenuEntry *)(it != NULL ? it->data : NULL);
597 }
598 } else
599 over = (over->parent->entries != NULL ?
600 g_list_last(over->parent->entries)->data :
601 NULL);
602
603 if (over->parent->mouseover)
604 over->parent->mouseover(over, TRUE);
605 else
606 menu_control_mouseover(over, TRUE);
607 menu_entry_render(over);
608 break;
609 }
610 case OB_KEY_RETURN: {
611 if (over == NULL)
612 return over;
613
614 if (over->submenu) {
615 if (over->parent->mouseover)
616 over->parent->mouseover(over, FALSE);
617 else
618 menu_control_mouseover(over, FALSE);
619 menu_entry_render(over);
620
621 if (over->submenu->entries)
622 over = over->submenu->entries->data;
623
624 if (over->parent->mouseover)
625 over->parent->mouseover(over, TRUE);
626 else
627 menu_control_mouseover(over, TRUE);
628 menu_entry_render(over);
629 }
630 else {
631 if (over->parent->mouseover)
632 over->parent->mouseover(over, FALSE);
633 else
634 menu_control_mouseover(over, FALSE);
635 menu_entry_render(over);
636
637 menu_entry_fire(over);
638 }
639 break;
640 }
641 case OB_KEY_ESCAPE: {
642 if (over != NULL) {
643 if (over->parent->mouseover)
644 over->parent->mouseover(over, FALSE);
645 else
646 menu_control_mouseover(over, FALSE);
647 menu_entry_render(over);
648
649 menu_hide(over->parent);
650 } else {
651 it = g_list_last(menu_visible);
652 if (it) {
653 menu_hide((ObMenu *)it->data);
654 }
655 }
656
657 over = NULL;
658 break;
659 }
660 default:
661 g_error("Unknown key");
662 }
663
664 return over;
665 }
This page took 0.070614 seconds and 5 git commands to generate.