]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
fix the offsets used to keep the menus on screen
[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 "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 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
24
25 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
26 gpointer data)
27 {
28 g_message("%s", __FUNCTION__);
29 parse_menu_full(i, doc, node, data, TRUE);
30 }
31
32
33 void parse_menu_full(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
34 gpointer data, gboolean newmenu)
35 {
36 ObAction *act;
37 xmlNodePtr nact;
38
39 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
40 ObMenu *menu = NULL, *parent = NULL;
41
42 if (newmenu == TRUE) {
43 if (!parse_attr_string("id", node, &id))
44 goto parse_menu_fail;
45 if (!parse_attr_string("label", node, &title))
46 goto parse_menu_fail;
47 ob_debug("menu label %s\n", title);
48
49 if (parse_attr_string("plugin", node, &plugin)) {
50 PluginMenuCreateData data;
51 data.parse_inst = i;
52 data.doc = doc;
53 data.node = node;
54 data.parent = menu;
55
56 if (plugin_open_reopen(plugin, i))
57 menu = plugin_create(plugin, &data);
58 g_free(plugin);
59 } else
60 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
61
62 if (data && menu)
63 *((ObMenu**)data) = menu;
64 } else {
65 menu = (ObMenu *)data;
66 }
67
68 node = node->xmlChildrenNode;
69
70 while (node) {
71 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
72 if (parse_attr_string("plugin", node, &plugin)) {
73 PluginMenuCreateData data;
74 data.doc = doc;
75 data.node = node;
76 data.parent = menu;
77 if (plugin_open_reopen(plugin, i))
78 parent = plugin_create(plugin, &data);
79 g_free(plugin);
80 } else {
81 parent = menu;
82 parse_menu(i, doc, node, &parent);
83 }
84
85 if (parent)
86 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
87 parent));
88
89 }
90 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
91 if (parse_attr_string("label", node, &label)) {
92 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
93 act = action_parse(doc, nact);
94 else
95 act = NULL;
96 if (act)
97 menu_add_entry(menu, menu_entry_new(label, act));
98 else
99 menu_add_entry(menu, menu_entry_new_separator(label));
100 g_free(label);
101 }
102 }
103 node = node->next;
104 }
105
106 parse_menu_fail:
107 g_free(id);
108 g_free(title);
109 }
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 + ob_rr_theme->bwidth >=
527 a->x + a->width) {
528 int newparentx = a->x + a->width
529 - self->submenu->size.width
530 - self->parent->size.width
531 - ob_rr_theme->bwidth * 2
532 - ob_rr_theme->menu_overlap;
533
534 x = a->x + a->width - self->submenu->size.width
535 - ob_rr_theme->menu_overlap;
536 XWarpPointer(ob_display, None, None, 0, 0, 0, 0,
537 newparentx - self->parent->location.x, 0);
538
539 menu_show_full(self->parent, newparentx,
540 self->parent->location.y, self->parent->client);
541 }
542
543 menu_show_full(self->submenu, x,
544 self->parent->location.y + self->y +
545 self->parent->title_h + ob_rr_theme->bwidth,
546 self->parent->client);
547 }
548 self->hilite = TRUE;
549 self->parent->over = g_list_find(self->parent->entries, self);
550
551 } else
552 self->hilite = FALSE;
553
554 menu_entry_render(self);
555 }
556
557 void menu_control_keyboard_nav(unsigned int key)
558 {
559 static ObMenu *current_menu = NULL;
560 ObMenuEntry *e = NULL;
561
562 ObKey obkey = OB_NUM_KEYS;
563
564 /* hrmm. could be fixed */
565 if (key == ob_keycode(OB_KEY_DOWN))
566 obkey = OB_KEY_DOWN;
567 else if (key == ob_keycode(OB_KEY_UP))
568 obkey = OB_KEY_UP;
569 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
570 obkey = OB_KEY_RIGHT;
571 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
572 obkey = OB_KEY_LEFT;
573 else if (key == ob_keycode(OB_KEY_RETURN))
574 obkey = OB_KEY_RETURN;
575
576
577 if (current_menu == NULL)
578 current_menu = menu_visible->data;
579
580 switch (obkey) {
581 case OB_KEY_DOWN: {
582 if (current_menu->over) {
583 current_menu->mouseover(current_menu->over->data, FALSE);
584 current_menu->over = (current_menu->over->next != NULL ?
585 current_menu->over->next :
586 current_menu->entries);
587 }
588 else
589 current_menu->over = current_menu->entries;
590
591 if (current_menu->over)
592 current_menu->mouseover(current_menu->over->data, TRUE);
593
594 break;
595 }
596 case OB_KEY_UP: {
597 if (current_menu->over) {
598 current_menu->mouseover(current_menu->over->data, FALSE);
599 current_menu->over = (current_menu->over->prev != NULL ?
600 current_menu->over->prev :
601 g_list_last(current_menu->entries));
602 } else
603 current_menu->over = g_list_last(current_menu->entries);
604
605 if (current_menu->over)
606 current_menu->mouseover(current_menu->over->data, TRUE);
607
608 break;
609 }
610 case OB_KEY_RIGHT: {
611 if (current_menu->over == NULL)
612 return;
613 e = (ObMenuEntry *)current_menu->over->data;
614 if (e->submenu) {
615 current_menu->mouseover(e, TRUE);
616 current_menu = e->submenu;
617 current_menu->over = current_menu->entries;
618 if (current_menu->over)
619 current_menu->mouseover(current_menu->over->data, TRUE);
620 }
621 break;
622 }
623
624 case OB_KEY_RETURN: {
625 if (current_menu->over == NULL)
626 return;
627 e = (ObMenuEntry *)current_menu->over->data;
628
629 current_menu->mouseover(e, FALSE);
630 current_menu->over = NULL;
631 /* zero is enter */
632 menu_entry_fire(e, 0, 0, 0);
633 }
634
635 case OB_KEY_LEFT: {
636 if (current_menu->over != NULL) {
637 current_menu->mouseover(current_menu->over->data, FALSE);
638 current_menu->over = NULL;
639 }
640
641 current_menu->hide(current_menu);
642
643 if (current_menu->parent)
644 current_menu = current_menu->parent;
645
646 break;
647 }
648 default:
649 ((ObMenu *)menu_visible->data)->hide(menu_visible->data);
650 current_menu = NULL;
651 }
652 return;
653 }
654
655 void menu_noop()
656 {
657 /* This noop brought to you by OLS 2003 Email Garden. */
658 }
This page took 0.068228 seconds and 5 git commands to generate.