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