12 #include "parser/parse.h"
14 GHashTable *menu_hash = NULL;
15 GList *menu_visible = NULL;
17 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
19 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
20 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
21 ButtonPressMask | ButtonReleaseMask)
23 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
26 g_message("%s", __FUNCTION__);
27 parse_menu_full(i, doc, node, data, TRUE);
31 void parse_menu_full(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
32 gpointer data, gboolean newmenu)
37 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
38 ObMenu *menu = NULL, *parent;
40 if (newmenu == TRUE) {
41 if (!parse_attr_string("id", node, &id))
43 if (!parse_attr_string("label", node, &title))
45 ob_debug("menu label %s\n", title);
47 if (parse_attr_string("plugin", node, &plugin)) {
48 PluginMenuCreateData data;
54 if (plugin_open_reopen(plugin, i))
55 parent = plugin_create(plugin, &data);
58 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
61 *((ObMenu**)data) = menu;
63 menu = (ObMenu *)data;
66 node = node->xmlChildrenNode;
69 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
70 if (parse_attr_string("plugin", node, &plugin)) {
71 PluginMenuCreateData data;
75 if (plugin_open_reopen(plugin, i))
76 parent = plugin_create(plugin, &data);
80 parse_menu(i, doc, node, &parent);
81 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
86 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
87 if (parse_attr_string("label", node, &label)) {
88 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
89 act = action_parse(doc, nact);
93 menu_add_entry(menu, menu_entry_new(label, act));
95 menu_add_entry(menu, menu_entry_new_separator(label));
107 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
109 void menu_destroy_hash_key(ObMenu *menu)
114 void menu_destroy_hash_value(ObMenu *self)
118 if (self->destroy) self->destroy(self);
120 for (it = self->entries; it; it = it->next)
121 menu_entry_free(it->data);
122 g_list_free(self->entries);
127 g_hash_table_remove(window_map, &self->title);
128 g_hash_table_remove(window_map, &self->frame);
129 g_hash_table_remove(window_map, &self->items);
131 stacking_remove(self);
133 RrAppearanceFree(self->a_title);
134 RrAppearanceFree(self->a_items);
135 XDestroyWindow(ob_display, self->title);
136 XDestroyWindow(ob_display, self->frame);
137 XDestroyWindow(ob_display, self->items);
142 void menu_entry_free(ObMenuEntry *self)
145 action_free(self->action);
147 g_hash_table_remove(window_map, &self->item);
149 RrAppearanceFree(self->a_item);
150 RrAppearanceFree(self->a_disabled);
151 RrAppearanceFree(self->a_hilite);
152 RrAppearanceFree(self->a_submenu);
153 XDestroyWindow(ob_display, self->item);
154 XDestroyWindow(ob_display, self->submenu_pic);
158 void menu_startup(ObParseInst *i)
160 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
161 (GDestroyNotify)menu_destroy_hash_key,
162 (GDestroyNotify)menu_destroy_hash_value);
167 g_hash_table_destroy(menu_hash);
176 gboolean loaded = FALSE;
180 if (config_menu_path)
182 parse_load(config_menu_path, "openbox_menu", &doc, &node)))
183 g_warning("Failed to load menu from '%s'", config_menu_path);
185 p = g_build_filename(g_get_home_dir(), ".openbox", "menu", NULL);
187 parse_load(p, "openbox_menu", &doc, &node)))
188 g_warning("Failed to load menu from '%s'", p);
192 p = g_build_filename(RCDIR, "menu", NULL);
194 parse_load(p, "openbox_menu", &doc, &node)))
195 g_warning("Failed to load menu from '%s'", p);
200 parse_register(i, "menu", parse_menu, NULL);
201 parse_tree(i, doc, node->xmlChildrenNode);
207 static Window createWindow(Window parent, unsigned long mask,
208 XSetWindowAttributes *attrib)
210 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
211 RrDepth(ob_rr_inst), InputOutput,
212 RrVisual(ob_rr_inst), mask, attrib);
216 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
217 menu_controller_show show, menu_controller_update update,
218 menu_controller_selected selected,
219 menu_controller_hide hide,
220 menu_controller_mouseover mouseover,
221 menu_controller_destroy destroy)
223 XSetWindowAttributes attrib;
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;
234 self->entries = NULL;
236 self->invalid = TRUE;
238 /* default controllers */
239 self->destroy = destroy;
240 self->show = (show != NULL ? show : menu_show_full);
241 self->hide = (hide != NULL ? hide : menu_hide);
242 self->update = (update != NULL ? update : menu_render);
243 self->mouseover = (mouseover != NULL ? mouseover :
244 menu_control_mouseover);
245 self->selected = (selected != NULL ? selected : menu_entry_fire);
248 self->plugin_data = NULL;
250 attrib.override_redirect = TRUE;
251 attrib.event_mask = FRAME_EVENTMASK;
252 self->frame = createWindow(RootWindow(ob_display, ob_screen),
253 CWOverrideRedirect|CWEventMask, &attrib);
254 attrib.event_mask = TITLE_EVENTMASK;
255 self->title = createWindow(self->frame, CWEventMask, &attrib);
256 self->items = createWindow(self->frame, 0, &attrib);
258 self->a_title = self->a_items = NULL;
260 XMapWindow(ob_display, self->title);
261 XMapWindow(ob_display, self->items);
263 g_hash_table_insert(window_map, &self->frame, self);
264 g_hash_table_insert(window_map, &self->title, self);
265 g_hash_table_insert(window_map, &self->items, self);
266 g_hash_table_insert(menu_hash, g_strdup(name), self);
268 stacking_add(MENU_AS_WINDOW(self));
269 stacking_raise(MENU_AS_WINDOW(self));
274 void menu_free(char *name)
276 g_hash_table_remove(menu_hash, name);
279 ObMenuEntry *menu_entry_new_full(char *label, ObAction *action,
280 ObMenuEntryRenderType render_type,
283 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
285 menu_entry->label = g_strdup(label);
286 menu_entry->render_type = render_type;
287 menu_entry->action = action;
289 menu_entry->hilite = FALSE;
290 menu_entry->enabled = TRUE;
292 menu_entry->submenu = submenu;
297 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
299 g_assert(entry != NULL);
301 entry->submenu = submenu;
303 if(entry->parent != NULL)
304 entry->parent->invalid = TRUE;
307 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
309 XSetWindowAttributes attrib;
311 g_assert(menu != NULL);
312 g_assert(entry != NULL);
313 g_assert(entry->item == None);
315 menu->entries = g_list_append(menu->entries, entry);
316 entry->parent = menu;
318 attrib.event_mask = ENTRY_EVENTMASK;
319 entry->item = createWindow(menu->items, CWEventMask, &attrib);
320 entry->submenu_pic = createWindow(menu->items, CWEventMask, &attrib);
321 XMapWindow(ob_display, entry->item);
322 XMapWindow(ob_display, entry->submenu_pic);
324 entry->a_item = entry->a_disabled = entry->a_hilite = entry->a_submenu
327 menu->invalid = TRUE;
329 g_hash_table_insert(window_map, &entry->item, menu);
330 g_hash_table_insert(window_map, &entry->submenu_pic, menu);
333 void menu_show(char *name, int x, int y, ObClient *client)
337 self = g_hash_table_lookup(menu_hash, name);
339 g_warning("Attempted to show menu '%s' but it does not exist.",
344 menu_show_full(self, x, y, client);
347 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
349 g_assert(self != NULL);
353 self->client = client;
356 if (!(self->parent && self->parent->shown)) {
357 grab_pointer(TRUE, None);
360 menu_visible = g_list_append(menu_visible, self);
363 menu_control_show(self, x, y, client);
366 void menu_hide(ObMenu *self) {
368 XUnmapWindow(ob_display, self->frame);
370 if (self->open_submenu)
371 self->open_submenu->hide(self->open_submenu);
372 if (self->parent && self->parent->open_submenu == self) {
373 self->parent->open_submenu = NULL;
376 if (!(self->parent && self->parent->shown)) {
377 grab_keyboard(FALSE);
378 grab_pointer(FALSE, None);
380 menu_visible = g_list_remove(menu_visible, self);
382 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
383 menu_entry_render(self->over->data);
389 void menu_clear(ObMenu *self) {
392 for (it = self->entries; it; it = it->next) {
393 ObMenuEntry *entry = it->data;
394 menu_entry_free(entry);
396 self->entries = NULL;
397 self->invalid = TRUE;
401 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
405 for (it = menu->entries; it; it = it->next) {
406 ObMenuEntry *entry = it->data;
407 if (entry->item == win)
413 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
417 for (it = menu->entries; it; it = it->next) {
418 ObMenuEntry *entry = it->data;
419 if (entry->submenu == submenu)
425 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
427 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
430 y -= menu->title_h + ob_rr_theme->bwidth;
431 if (y < 0) return NULL;
433 ob_debug("%d %p\n", y/menu->item_h,
434 g_list_nth_data(menu->entries, y / menu->item_h));
435 return g_list_nth_data(menu->entries, y / menu->item_h);
438 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
443 /* ignore wheel scrolling */
444 if (button == 4 || button == 5) return;
447 self->action->data.any.c = self->parent->client;
448 self->action->func(&self->action->data);
450 /* hide the whole thing */
452 while (m->parent) m = m->parent;
458 Default menu controller action for showing.
461 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
466 g_assert(!self->invalid);
468 for (i = 0; i < screen_num_monitors; ++i) {
469 a = screen_physical_area_monitor(i);
470 if (RECT_CONTAINS(*a, x, y))
476 POINT_SET(self->location,
477 MIN(x, a->x + a->width - 1 - self->size.width),
478 MIN(y, a->y + a->height - 1 - self->size.height));
479 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
482 XMapWindow(ob_display, self->frame);
483 stacking_raise(MENU_AS_WINDOW(self));
485 } else if (self->shown && self->open_submenu) {
486 self->open_submenu->hide(self->open_submenu);
490 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
496 g_assert(self != NULL);
499 /* TODO: we prolly don't need open_submenu */
500 if (self->parent->open_submenu && self->submenu
501 != self->parent->open_submenu)
503 e = (ObMenuEntry *) self->parent->over->data;
505 menu_entry_render(e);
506 self->parent->open_submenu->hide(self->parent->open_submenu);
509 if (self->submenu && self->parent->open_submenu != self->submenu) {
510 self->parent->open_submenu = self->submenu;
512 /* shouldn't be invalid since it must be displayed */
513 g_assert(!self->parent->invalid);
514 /* TODO: I don't understand why these bevels should be here.
515 Something must be wrong in the width calculation */
516 x = self->parent->location.x + self->parent->size.width +
517 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
519 /* need to get the width. is this bad?*/
520 self->submenu->update(self->submenu);
522 a = screen_physical_area_monitor(self->parent->xin_area);
524 if (self->submenu->size.width + x >= a->x + a->width) {
525 int newparentx = a->x + a->width
526 - self->submenu->size.width
527 - self->parent->size.width
528 - ob_rr_theme->bwidth
529 - ob_rr_theme->menu_overlap;
531 x = a->x + a->width - self->submenu->size.width
532 - ob_rr_theme->menu_overlap;
533 XWarpPointer(ob_display, None, None, 0, 0, 0, 0,
534 newparentx - self->parent->location.x, 0);
536 menu_show_full(self->parent, newparentx,
537 self->parent->location.y, self->parent->client);
540 menu_show_full(self->submenu, x,
541 self->parent->location.y + self->y,
542 self->parent->client);
545 self->parent->over = g_list_find(self->parent->entries, self);
548 self->hilite = FALSE;
550 menu_entry_render(self);
553 void menu_control_keyboard_nav(unsigned int key)
555 static ObMenu *current_menu = NULL;
556 ObMenuEntry *e = NULL;
558 ObKey obkey = OB_NUM_KEYS;
560 /* hrmm. could be fixed */
561 if (key == ob_keycode(OB_KEY_DOWN))
563 else if (key == ob_keycode(OB_KEY_UP))
565 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
566 obkey = OB_KEY_RIGHT;
567 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
569 else if (key == ob_keycode(OB_KEY_RETURN))
570 obkey = OB_KEY_RETURN;
573 if (current_menu == NULL)
574 current_menu = menu_visible->data;
578 if (current_menu->over) {
579 current_menu->mouseover(current_menu->over->data, FALSE);
580 current_menu->over = (current_menu->over->next != NULL ?
581 current_menu->over->next :
582 current_menu->entries);
585 current_menu->over = current_menu->entries;
587 if (current_menu->over)
588 current_menu->mouseover(current_menu->over->data, TRUE);
593 if (current_menu->over) {
594 current_menu->mouseover(current_menu->over->data, FALSE);
595 current_menu->over = (current_menu->over->prev != NULL ?
596 current_menu->over->prev :
597 g_list_last(current_menu->entries));
599 current_menu->over = g_list_last(current_menu->entries);
601 if (current_menu->over)
602 current_menu->mouseover(current_menu->over->data, TRUE);
607 if (current_menu->over == NULL)
609 e = (ObMenuEntry *)current_menu->over->data;
611 current_menu->mouseover(e, TRUE);
612 current_menu = e->submenu;
613 current_menu->over = current_menu->entries;
614 if (current_menu->over)
615 current_menu->mouseover(current_menu->over->data, TRUE);
620 case OB_KEY_RETURN: {
621 if (current_menu->over == NULL)
623 e = (ObMenuEntry *)current_menu->over->data;
625 current_menu->mouseover(e, FALSE);
626 current_menu->over = NULL;
628 menu_entry_fire(e, 0, 0, 0);
632 if (current_menu->over != NULL) {
633 current_menu->mouseover(current_menu->over->data, FALSE);
634 current_menu->over = NULL;
637 current_menu->hide(current_menu);
639 if (current_menu->parent)
640 current_menu = current_menu->parent;
645 ((ObMenu *)menu_visible->data)->hide(menu_visible->data);
653 /* This noop brought to you by OLS 2003 Email Garden. */