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 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
25 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
28 g_message("%s", __FUNCTION__);
29 parse_menu_full(i, doc, node, data, TRUE);
33 void parse_menu_full(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
34 gpointer data, gboolean newmenu)
39 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
40 ObMenu *menu = NULL, *parent = NULL;
42 if (newmenu == TRUE) {
43 if (!parse_attr_string("id", node, &id))
45 if (!parse_attr_string("label", node, &title))
47 ob_debug("menu label %s\n", title);
49 if (parse_attr_string("plugin", node, &plugin)) {
50 PluginMenuCreateData data;
56 if (plugin_open_reopen(plugin, i))
57 menu = plugin_create(plugin, &data);
60 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
63 *((ObMenu**)data) = menu;
65 menu = (ObMenu *)data;
68 node = node->xmlChildrenNode;
71 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
72 if (parse_attr_string("plugin", node, &plugin)) {
73 PluginMenuCreateData data;
77 if (plugin_open_reopen(plugin, i))
78 parent = plugin_create(plugin, &data);
82 parse_menu(i, doc, node, &parent);
86 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
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);
97 menu_add_entry(menu, menu_entry_new(label, act));
99 menu_add_entry(menu, menu_entry_new_separator(label));
111 void menu_destroy_hash_key(ObMenu *menu)
116 void menu_destroy_hash_value(ObMenu *self)
120 if (self->destroy) self->destroy(self);
122 for (it = self->entries; it; it = it->next)
123 menu_entry_free(it->data);
124 g_list_free(self->entries);
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);
133 stacking_remove(self);
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);
144 void menu_entry_free(ObMenuEntry *self)
147 action_free(self->action);
149 g_hash_table_remove(window_map, &self->item);
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);
160 void menu_startup(ObParseInst *i)
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);
169 g_hash_table_destroy(menu_hash);
178 gboolean loaded = FALSE;
182 if (config_menu_path)
184 parse_load(config_menu_path, "openbox_menu", &doc, &node)))
185 g_warning("Failed to load menu from '%s'", config_menu_path);
187 p = g_build_filename(g_get_home_dir(), ".openbox", "menu", NULL);
189 parse_load(p, "openbox_menu", &doc, &node)))
190 g_warning("Failed to load menu from '%s'", p);
194 p = g_build_filename(RCDIR, "menu", NULL);
196 parse_load(p, "openbox_menu", &doc, &node)))
197 g_warning("Failed to load menu from '%s'", p);
202 parse_register(i, "menu", parse_menu, NULL);
203 parse_tree(i, doc, node->xmlChildrenNode);
209 static Window createWindow(Window parent, unsigned long mask,
210 XSetWindowAttributes *attrib)
212 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
213 RrDepth(ob_rr_inst), InputOutput,
214 RrVisual(ob_rr_inst), mask, attrib);
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)
225 XSetWindowAttributes attrib;
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;
236 self->entries = NULL;
238 self->invalid = TRUE;
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);
250 self->plugin_data = NULL;
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);
260 self->a_title = self->a_items = NULL;
262 XMapWindow(ob_display, self->title);
263 XMapWindow(ob_display, self->items);
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);
270 stacking_add(MENU_AS_WINDOW(self));
271 stacking_raise(MENU_AS_WINDOW(self));
276 void menu_free(char *name)
278 g_hash_table_remove(menu_hash, name);
281 ObMenuEntry *menu_entry_new_full(char *label, ObAction *action,
282 ObMenuEntryRenderType render_type,
285 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
287 menu_entry->label = g_strdup(label);
288 menu_entry->render_type = render_type;
289 menu_entry->action = action;
291 menu_entry->hilite = FALSE;
292 menu_entry->enabled = TRUE;
294 menu_entry->submenu = submenu;
299 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
301 g_assert(entry != NULL);
303 entry->submenu = submenu;
305 if(entry->parent != NULL)
306 entry->parent->invalid = TRUE;
309 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
311 XSetWindowAttributes attrib;
313 g_assert(menu != NULL);
314 g_assert(entry != NULL);
315 g_assert(entry->item == None);
317 menu->entries = g_list_append(menu->entries, entry);
318 entry->parent = menu;
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);
326 entry->a_item = entry->a_disabled = entry->a_hilite = entry->a_submenu
329 menu->invalid = TRUE;
331 g_hash_table_insert(window_map, &entry->item, menu);
332 g_hash_table_insert(window_map, &entry->submenu_pic, menu);
335 void menu_show(char *name, int x, int y, ObClient *client)
339 self = g_hash_table_lookup(menu_hash, name);
341 g_warning("Attempted to show menu '%s' but it does not exist.",
346 menu_show_full(self, x, y, client);
349 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
351 g_assert(self != NULL);
355 self->client = client;
358 if (!(self->parent && self->parent->shown)) {
359 grab_pointer(TRUE, None);
362 menu_visible = g_list_append(menu_visible, self);
365 menu_control_show(self, x, y, client);
368 void menu_hide(ObMenu *self) {
370 XUnmapWindow(ob_display, self->frame);
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;
378 if (!(self->parent && self->parent->shown)) {
379 grab_keyboard(FALSE);
380 grab_pointer(FALSE, None);
382 menu_visible = g_list_remove(menu_visible, self);
384 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
385 menu_entry_render(self->over->data);
391 void menu_clear(ObMenu *self) {
394 for (it = self->entries; it; it = it->next) {
395 ObMenuEntry *entry = it->data;
396 menu_entry_free(entry);
398 self->entries = NULL;
399 self->invalid = TRUE;
403 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
407 for (it = menu->entries; it; it = it->next) {
408 ObMenuEntry *entry = it->data;
409 if (entry->item == win)
415 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
419 for (it = menu->entries; it; it = it->next) {
420 ObMenuEntry *entry = it->data;
421 if (entry->submenu == submenu)
427 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
429 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
432 y -= menu->title_h + ob_rr_theme->bwidth;
433 if (y < 0) return NULL;
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);
440 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
445 /* ignore wheel scrolling */
446 if (button == 4 || button == 5) return;
449 self->action->data.any.c = self->parent->client;
450 self->action->func(&self->action->data);
452 /* hide the whole thing */
454 while (m->parent) m = m->parent;
460 Default menu controller action for showing.
463 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
468 g_assert(!self->invalid);
470 for (i = 0; i < screen_num_monitors; ++i) {
471 a = screen_physical_area_monitor(i);
472 if (RECT_CONTAINS(*a, x, y))
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);
484 XMapWindow(ob_display, self->frame);
485 stacking_raise(MENU_AS_WINDOW(self));
487 } else if (self->shown && self->open_submenu) {
488 self->open_submenu->hide(self->open_submenu);
492 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
498 g_assert(self != NULL);
501 /* TODO: we prolly don't need open_submenu */
502 if (self->parent->open_submenu && self->submenu
503 != self->parent->open_submenu)
505 e = (ObMenuEntry *) self->parent->over->data;
507 menu_entry_render(e);
508 self->parent->open_submenu->hide(self->parent->open_submenu);
511 if (self->submenu && self->parent->open_submenu != self->submenu) {
512 self->parent->open_submenu = self->submenu;
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;
521 /* need to get the width. is this bad?*/
522 self->submenu->update(self->submenu);
524 a = screen_physical_area_monitor(self->parent->xin_area);
526 if (self->submenu->size.width + x + ob_rr_theme->bwidth >=
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;
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);
539 menu_show_full(self->parent, newparentx,
540 self->parent->location.y, self->parent->client);
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);
549 self->parent->over = g_list_find(self->parent->entries, self);
552 self->hilite = FALSE;
554 menu_entry_render(self);
557 void menu_control_keyboard_nav(unsigned int key)
559 static ObMenu *current_menu = NULL;
560 ObMenuEntry *e = NULL;
562 ObKey obkey = OB_NUM_KEYS;
564 /* hrmm. could be fixed */
565 if (key == ob_keycode(OB_KEY_DOWN))
567 else if (key == ob_keycode(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 */
573 else if (key == ob_keycode(OB_KEY_RETURN))
574 obkey = OB_KEY_RETURN;
577 if (current_menu == NULL)
578 current_menu = menu_visible->data;
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);
589 current_menu->over = current_menu->entries;
591 if (current_menu->over)
592 current_menu->mouseover(current_menu->over->data, TRUE);
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));
603 current_menu->over = g_list_last(current_menu->entries);
605 if (current_menu->over)
606 current_menu->mouseover(current_menu->over->data, TRUE);
611 if (current_menu->over == NULL)
613 e = (ObMenuEntry *)current_menu->over->data;
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);
624 case OB_KEY_RETURN: {
625 if (current_menu->over == NULL)
627 e = (ObMenuEntry *)current_menu->over->data;
629 current_menu->mouseover(e, FALSE);
630 current_menu->over = NULL;
632 menu_entry_fire(e, 0, 0, 0);
636 if (current_menu->over != NULL) {
637 current_menu->mouseover(current_menu->over->data, FALSE);
638 current_menu->over = NULL;
641 current_menu->hide(current_menu);
643 if (current_menu->parent)
644 current_menu = current_menu->parent;
649 ((ObMenu *)menu_visible->data)->hide(menu_visible->data);
657 /* This noop brought to you by OLS 2003 Email Garden. */