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);
149 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
150 (GDestroyNotify)menu_destroy_hash_key,
151 (GDestroyNotify)menu_destroy_hash_value);
153 parse_register("menu", parse_menu, NULL);
159 g_hash_table_destroy(menu_hash);
162 static Window createWindow(Window parent, unsigned long mask,
163 XSetWindowAttributes *attrib)
165 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
166 RrDepth(ob_rr_inst), InputOutput,
167 RrVisual(ob_rr_inst), mask, attrib);
171 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
172 menu_controller_show show, menu_controller_update update,
173 menu_controller_selected selected,
174 menu_controller_hide hide,
175 menu_controller_mouseover mouseover)
177 XSetWindowAttributes attrib;
180 self = g_new0(ObMenu, 1);
181 self->obwin.type = Window_Menu;
182 self->label = g_strdup(label);
183 self->name = g_strdup(name);
184 self->parent = parent;
185 self->open_submenu = NULL;
188 self->entries = NULL;
190 self->invalid = TRUE;
192 /* default controllers */
193 self->show = (show != NULL ? show : menu_show_full);
194 self->hide = (hide != NULL ? hide : menu_hide);
195 self->update = (update != NULL ? update : menu_render);
196 self->mouseover = (mouseover != NULL ? mouseover :
197 menu_control_mouseover);
198 self->selected = (selected != NULL ? selected : menu_entry_fire);
201 self->plugin_data = NULL;
203 attrib.override_redirect = TRUE;
204 attrib.event_mask = FRAME_EVENTMASK;
205 self->frame = createWindow(RootWindow(ob_display, ob_screen),
206 CWOverrideRedirect|CWEventMask, &attrib);
207 attrib.event_mask = TITLE_EVENTMASK;
208 self->title = createWindow(self->frame, CWEventMask, &attrib);
209 self->items = createWindow(self->frame, 0, &attrib);
211 self->a_title = self->a_items = NULL;
213 XMapWindow(ob_display, self->title);
214 XMapWindow(ob_display, self->items);
216 g_hash_table_insert(window_map, &self->frame, self);
217 g_hash_table_insert(window_map, &self->title, self);
218 g_hash_table_insert(window_map, &self->items, self);
219 g_hash_table_insert(menu_hash, g_strdup(name), self);
221 stacking_add(MENU_AS_WINDOW(self));
222 stacking_raise(MENU_AS_WINDOW(self));
227 void menu_free(char *name)
229 g_hash_table_remove(menu_hash, name);
232 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
233 ObMenuEntryRenderType render_type,
236 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
238 menu_entry->label = g_strdup(label);
239 menu_entry->render_type = render_type;
240 menu_entry->action = action;
242 menu_entry->hilite = FALSE;
243 menu_entry->enabled = TRUE;
245 menu_entry->submenu = submenu;
250 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
252 g_assert(entry != NULL);
254 entry->submenu = submenu;
256 if(entry->parent != NULL)
257 entry->parent->invalid = TRUE;
260 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
262 XSetWindowAttributes attrib;
264 g_assert(menu != NULL);
265 g_assert(entry != NULL);
266 g_assert(entry->item == None);
268 menu->entries = g_list_append(menu->entries, entry);
269 entry->parent = menu;
271 attrib.event_mask = ENTRY_EVENTMASK;
272 entry->item = createWindow(menu->items, CWEventMask, &attrib);
273 XMapWindow(ob_display, entry->item);
275 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
277 menu->invalid = TRUE;
279 g_hash_table_insert(window_map, &entry->item, menu);
282 void menu_show(char *name, int x, int y, ObClient *client)
286 self = g_hash_table_lookup(menu_hash, name);
288 g_warning("Attempted to show menu '%s' but it does not exist.",
293 menu_show_full(self, x, y, client);
296 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
298 g_assert(self != NULL);
302 self->client = client;
305 if (!(self->parent && self->parent->shown)) {
306 grab_pointer(TRUE, None);
309 menu_visible = g_list_append(menu_visible, self);
312 menu_control_show(self, x, y, client);
315 void menu_hide(ObMenu *self) {
317 XUnmapWindow(ob_display, self->frame);
319 if (self->open_submenu)
320 menu_hide(self->open_submenu);
321 if (self->parent && self->parent->open_submenu == self) {
322 self->parent->open_submenu = NULL;
325 if (!(self->parent && self->parent->shown)) {
326 grab_keyboard(FALSE);
327 grab_pointer(FALSE, None);
329 menu_visible = g_list_remove(menu_visible, self);
331 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
332 menu_entry_render(self->over->data);
338 void menu_clear(ObMenu *self) {
341 for (it = self->entries; it; it = it->next) {
342 ObMenuEntry *entry = it->data;
343 menu_entry_free(entry);
345 self->entries = NULL;
346 self->invalid = TRUE;
350 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
354 for (it = menu->entries; it; it = it->next) {
355 ObMenuEntry *entry = it->data;
356 if (entry->item == win)
362 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
366 for (it = menu->entries; it; it = it->next) {
367 ObMenuEntry *entry = it->data;
368 if (entry->submenu == submenu)
374 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
376 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
379 y -= menu->title_h + ob_rr_theme->bwidth;
380 if (y < 0) return NULL;
382 ob_debug("%d %p\n", y/menu->item_h,
383 g_list_nth_data(menu->entries, y / menu->item_h));
384 return g_list_nth_data(menu->entries, y / menu->item_h);
387 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
392 if (button != 1) return;
395 self->action->data.any.c = self->parent->client;
396 self->action->func(&self->action->data);
398 /* hide the whole thing */
400 while (m->parent) m = m->parent;
406 Default menu controller action for showing.
409 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
414 g_assert(!self->invalid);
416 for (i = 0; i < screen_num_monitors; ++i) {
417 a = screen_physical_area_monitor(i);
418 if (RECT_CONTAINS(*a, x, y))
424 POINT_SET(self->location,
425 MIN(x, a->x + a->width - 1 - self->size.width),
426 MIN(y, a->y + a->height - 1 - self->size.height));
427 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
430 XMapWindow(ob_display, self->frame);
431 stacking_raise(MENU_AS_WINDOW(self));
433 } else if (self->shown && self->open_submenu) {
434 menu_hide(self->open_submenu);
438 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
444 g_assert(self != NULL);
447 /* TODO: we prolly don't need open_submenu */
448 if (self->parent->open_submenu && self->submenu
449 != self->parent->open_submenu)
451 e = (ObMenuEntry *) self->parent->over->data;
453 menu_entry_render(e);
454 menu_hide(self->parent->open_submenu);
457 if (self->submenu && self->parent->open_submenu != self->submenu) {
458 self->parent->open_submenu = self->submenu;
460 /* shouldn't be invalid since it must be displayed */
461 g_assert(!self->parent->invalid);
462 /* TODO: I don't understand why these bevels should be here.
463 Something must be wrong in the width calculation */
464 x = self->parent->location.x + self->parent->size.width +
465 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
467 /* need to get the width. is this bad?*/
468 self->parent->update(self->submenu);
470 a = screen_physical_area_monitor(self->parent->xin_area);
472 if (self->submenu->size.width + x >= a->x + a->width)
473 x = self->parent->location.x - self->submenu->size.width -
474 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
476 menu_show_full(self->submenu, x,
477 self->parent->location.y + self->y,
478 self->parent->client);
481 self->parent->over = g_list_find(self->parent->entries, self);
484 self->hilite = FALSE;
486 menu_entry_render(self);
489 void menu_control_keyboard_nav(unsigned int key)
491 static ObMenu *current_menu = NULL;
492 ObMenuEntry *e = NULL;
494 ObKey obkey = OB_NUM_KEYS;
496 /* hrmm. could be fixed */
497 if (key == ob_keycode(OB_KEY_DOWN))
499 else if (key == ob_keycode(OB_KEY_UP))
501 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
502 obkey = OB_KEY_RIGHT;
503 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
506 if (current_menu == NULL)
507 current_menu = menu_visible->data;
511 if (current_menu->over) {
512 current_menu->mouseover(current_menu->over->data, FALSE);
513 current_menu->over = (current_menu->over->next != NULL ?
514 current_menu->over->next :
515 current_menu->entries);
518 current_menu->over = current_menu->entries;
520 if (current_menu->over)
521 current_menu->mouseover(current_menu->over->data, TRUE);
526 if (current_menu->over) {
527 current_menu->mouseover(current_menu->over->data, FALSE);
528 current_menu->over = (current_menu->over->prev != NULL ?
529 current_menu->over->prev :
530 g_list_last(current_menu->entries));
532 current_menu->over = g_list_last(current_menu->entries);
534 if (current_menu->over)
535 current_menu->mouseover(current_menu->over->data, TRUE);
540 if (current_menu->over == NULL)
542 e = (ObMenuEntry *)current_menu->over->data;
544 current_menu = e->submenu;
545 current_menu->over = current_menu->entries;
546 if (current_menu->over)
547 current_menu->mouseover(current_menu->over->data, TRUE);
552 case OB_KEY_RETURN: {
553 if (current_menu->over == NULL)
555 e = (ObMenuEntry *)current_menu->over->data;
557 current_menu->mouseover(e, FALSE);
558 current_menu->over = NULL;
560 menu_entry_fire(e, 0, 0, 0);
564 if (current_menu->over != NULL) {
565 current_menu->mouseover(current_menu->over->data, FALSE);
566 current_menu->over = NULL;
569 menu_hide(current_menu);
571 if (current_menu->parent)
572 current_menu = current_menu->parent;
578 menu_hide(current_menu);
585 /* This noop brought to you by OLS 2003 Email Garden. */