12 GHashTable *menu_hash = NULL;
13 GList *menu_visible = NULL;
15 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
17 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
18 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
19 ButtonPressMask | ButtonReleaseMask)
21 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
23 parse_menu_full(doc, node, data, TRUE);
27 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
33 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
34 ObMenu *menu = NULL, *parent;
36 if (newmenu == TRUE) {
37 if (!parse_attr_string("id", node, &id))
39 if (!parse_attr_string("label", node, &title))
41 ob_debug("menu label %s\n", title);
43 if (parse_attr_string("plugin", node, &plugin)) {
44 PluginMenuCreateData data;
48 parent = plugin_create(plugin, &data);
51 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
54 *((ObMenu**)data) = menu;
56 menu = (ObMenu *)data;
59 node = node->xmlChildrenNode;
62 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
63 if (parse_attr_string("plugin", node, &plugin)) {
64 PluginMenuCreateData data;
68 parent = plugin_create(plugin, &data);
72 parse_menu(doc, node, &parent);
73 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
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);
85 menu_add_entry(menu, menu_entry_new(label, act));
87 menu_add_entry(menu, menu_entry_new_separator(label));
99 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
101 void menu_destroy_hash_key(ObMenu *menu)
106 void menu_destroy_hash_value(ObMenu *self)
110 for (it = self->entries; it; it = it->next)
111 menu_entry_free(it->data);
112 g_list_free(self->entries);
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);
121 stacking_remove(self);
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);
132 void menu_entry_free(ObMenuEntry *self)
135 action_free(self->action);
137 g_hash_table_remove(window_map, &self->item);
139 RrAppearanceFree(self->a_item);
140 RrAppearanceFree(self->a_disabled);
141 RrAppearanceFree(self->a_hilite);
142 XDestroyWindow(ob_display, self->item);
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);
160 parse_register("menu", parse_menu, NULL);
163 m = menu_new("sex menu", "root", NULL);
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));
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));
181 menu_add_entry(m, menu_entry_new_submenu("subz", s));
183 s = menu_new("empty", "chub", m);
184 menu_add_entry(m, menu_entry_new_submenu("empty", s));
186 s = menu_new("", "s-club", m);
187 menu_add_entry(m, menu_entry_new_submenu("empty", s));
189 s = menu_new(NULL, "h-club", m);
190 menu_add_entry(m, menu_entry_new_submenu("empty", s));
192 s = menu_new(NULL, "g-club", m);
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));
203 menu_add_entry(m, menu_entry_new_submenu("long", s));
209 g_hash_table_destroy(menu_hash);
212 static Window createWindow(Window parent, unsigned long mask,
213 XSetWindowAttributes *attrib)
215 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
216 RrDepth(ob_rr_inst), InputOutput,
217 RrVisual(ob_rr_inst), mask, attrib);
221 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
222 menu_controller_show show, menu_controller_update update)
224 XSetWindowAttributes attrib;
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;
234 self->entries = NULL;
236 self->invalid = TRUE;
238 /* default controllers */
241 self->update = update;
242 self->mouseover = NULL;
243 self->selected = NULL;
246 self->plugin_data = NULL;
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);
256 self->a_title = self->a_items = NULL;
258 XMapWindow(ob_display, self->title);
259 XMapWindow(ob_display, self->items);
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);
266 stacking_add(MENU_AS_WINDOW(self));
267 stacking_raise(MENU_AS_WINDOW(self));
272 void menu_free(char *name)
274 g_hash_table_remove(menu_hash, name);
277 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
278 ObMenuEntryRenderType render_type,
281 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
283 menu_entry->label = g_strdup(label);
284 menu_entry->render_type = render_type;
285 menu_entry->action = action;
287 menu_entry->hilite = FALSE;
288 menu_entry->enabled = TRUE;
290 menu_entry->submenu = submenu;
295 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
297 g_assert(entry != NULL);
299 entry->submenu = submenu;
301 if(entry->parent != NULL)
302 entry->parent->invalid = TRUE;
305 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
307 XSetWindowAttributes attrib;
309 g_assert(menu != NULL);
310 g_assert(entry != NULL);
311 g_assert(entry->item == None);
313 menu->entries = g_list_append(menu->entries, entry);
314 entry->parent = menu;
316 attrib.event_mask = ENTRY_EVENTMASK;
317 entry->item = createWindow(menu->items, CWEventMask, &attrib);
318 XMapWindow(ob_display, entry->item);
320 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
322 menu->invalid = TRUE;
324 g_hash_table_insert(window_map, &entry->item, menu);
327 void menu_show(char *name, int x, int y, ObClient *client)
331 self = g_hash_table_lookup(menu_hash, name);
333 g_warning("Attempted to show menu '%s' but it does not exist.",
338 menu_show_full(self, x, y, client);
341 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
343 g_assert(self != NULL);
347 self->client = client;
350 if (!(self->parent && self->parent->shown)) {
351 grab_pointer(TRUE, None);
354 menu_visible = g_list_append(menu_visible, self);
358 self->show(self, x, y, client);
360 menu_control_show(self, x, y, client);
364 void menu_hide(ObMenu *self) {
366 XUnmapWindow(ob_display, self->frame);
368 if (self->open_submenu)
369 menu_hide(self->open_submenu);
370 if (self->parent && self->parent->open_submenu == self) {
373 self->parent->open_submenu = NULL;
375 e = menu_find_entry_by_submenu(self->parent, self);
376 if (self->parent->mouseover)
377 self->parent->mouseover(e, FALSE);
379 menu_control_mouseover(e, FALSE);
380 menu_entry_render(e);
383 if (!(self->parent && self->parent->shown)) {
384 grab_keyboard(FALSE);
385 grab_pointer(FALSE, None);
387 menu_visible = g_list_remove(menu_visible, self);
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)
445 self->action->data.any.c = self->parent->client;
446 self->action->func(&self->action->data);
448 /* hide the whole thing */
450 while (m->parent) m = m->parent;
456 Default menu controller action for showing.
459 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
464 g_assert(!self->invalid);
466 for (i = 0; i < screen_num_monitors; ++i) {
467 a = screen_physical_area_monitor(i);
468 if (RECT_CONTAINS(*a, x, y))
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);
480 XMapWindow(ob_display, self->frame);
481 stacking_raise(MENU_AS_WINDOW(self));
483 } else if (self->shown && self->open_submenu) {
484 menu_hide(self->open_submenu);
488 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
495 if (self->parent->open_submenu && self->submenu
496 != self->parent->open_submenu)
498 e = menu_find_entry_by_submenu(self->parent,
499 self->parent->open_submenu);
501 menu_entry_render(e);
502 menu_hide(self->parent->open_submenu);
505 if (self->submenu && self->parent->open_submenu != self->submenu) {
506 self->parent->open_submenu = self->submenu;
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;
515 /* need to get the width. is this bad?*/
516 menu_render(self->submenu);
518 a = screen_physical_area_monitor(self->parent->xin_area);
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;
524 menu_show_full(self->submenu, x,
525 self->parent->location.y + self->y,
526 self->parent->client);
530 if (enter || !self->submenu ||
531 menu_find_entry_by_submenu(self->parent,
532 self->parent->open_submenu) != self)
533 self->hilite = enter;
536 ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key)
543 if (over->parent->mouseover)
544 over->parent->mouseover(over, FALSE);
546 menu_control_mouseover(over, FALSE);
547 menu_entry_render(over);
549 it = over->parent->entries;
550 while (it != NULL && it->data != over)
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;
563 over = (over->parent->entries != NULL ?
564 over->parent->entries->data : NULL);
568 if (over->parent->mouseover)
569 over->parent->mouseover(over, TRUE);
571 menu_control_mouseover(over, TRUE);
572 menu_entry_render(over);
579 if (over->parent->mouseover)
580 over->parent->mouseover(over, FALSE);
582 menu_control_mouseover(over, FALSE);
583 menu_entry_render(over);
585 it = g_list_last(over->parent->entries);
586 while (it != NULL && it->data != over)
591 over = (ObMenuEntry *)it->prev->data;
592 else if (over == NULL) {
593 it = g_list_last(menu_visible);
595 it = g_list_last(((ObMenu *)it->data)->entries);
596 over = (ObMenuEntry *)(it != NULL ? it->data : NULL);
599 over = (over->parent->entries != NULL ?
600 g_list_last(over->parent->entries)->data :
603 if (over->parent->mouseover)
604 over->parent->mouseover(over, TRUE);
606 menu_control_mouseover(over, TRUE);
607 menu_entry_render(over);
610 case OB_KEY_RETURN: {
615 if (over->parent->mouseover)
616 over->parent->mouseover(over, FALSE);
618 menu_control_mouseover(over, FALSE);
619 menu_entry_render(over);
621 if (over->submenu->entries)
622 over = over->submenu->entries->data;
624 if (over->parent->mouseover)
625 over->parent->mouseover(over, TRUE);
627 menu_control_mouseover(over, TRUE);
628 menu_entry_render(over);
631 if (over->parent->mouseover)
632 over->parent->mouseover(over, FALSE);
634 menu_control_mouseover(over, FALSE);
635 menu_entry_render(over);
637 menu_entry_fire(over);
641 case OB_KEY_ESCAPE: {
643 if (over->parent->mouseover)
644 over->parent->mouseover(over, FALSE);
646 menu_control_mouseover(over, FALSE);
647 menu_entry_render(over);
649 menu_hide(over->parent);
651 it = g_list_last(menu_visible);
653 menu_hide((ObMenu *)it->data);
661 g_error("Unknown key");