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) {
323 self->parent->open_submenu = NULL;
326 if (!(self->parent && self->parent->shown)) {
327 grab_keyboard(FALSE);
328 grab_pointer(FALSE, None);
330 menu_visible = g_list_remove(menu_visible, self);
334 void menu_clear(ObMenu *self) {
337 for (it = self->entries; it; it = it->next) {
338 ObMenuEntry *entry = it->data;
339 menu_entry_free(entry);
341 self->entries = NULL;
342 self->invalid = TRUE;
346 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
350 for (it = menu->entries; it; it = it->next) {
351 ObMenuEntry *entry = it->data;
352 if (entry->item == win)
358 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
362 for (it = menu->entries; it; it = it->next) {
363 ObMenuEntry *entry = it->data;
364 if (entry->submenu == submenu)
370 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
372 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
375 y -= menu->title_h + ob_rr_theme->bwidth;
376 if (y < 0) return NULL;
378 ob_debug("%d %p\n", y/menu->item_h,
379 g_list_nth_data(menu->entries, y / menu->item_h));
380 return g_list_nth_data(menu->entries, y / menu->item_h);
383 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
388 if (button > 3) return;
391 self->action->data.any.c = self->parent->client;
392 self->action->func(&self->action->data);
394 /* hide the whole thing */
396 while (m->parent) m = m->parent;
402 Default menu controller action for showing.
405 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
410 g_assert(!self->invalid);
412 for (i = 0; i < screen_num_monitors; ++i) {
413 a = screen_physical_area_monitor(i);
414 if (RECT_CONTAINS(*a, x, y))
420 POINT_SET(self->location,
421 MIN(x, a->x + a->width - 1 - self->size.width),
422 MIN(y, a->y + a->height - 1 - self->size.height));
423 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
426 XMapWindow(ob_display, self->frame);
427 stacking_raise(MENU_AS_WINDOW(self));
429 } else if (self->shown && self->open_submenu) {
430 menu_hide(self->open_submenu);
434 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
440 g_assert(self != NULL);
443 /* TODO: we prolly don't need open_submenu */
444 if (self->parent->open_submenu && self->submenu
445 != self->parent->open_submenu)
447 e = (ObMenuEntry *) self->parent->over->data;
449 menu_entry_render(e);
450 menu_hide(self->parent->open_submenu);
453 if (self->submenu && self->parent->open_submenu != self->submenu) {
454 self->parent->open_submenu = self->submenu;
456 /* shouldn't be invalid since it must be displayed */
457 g_assert(!self->parent->invalid);
458 /* TODO: I don't understand why these bevels should be here.
459 Something must be wrong in the width calculation */
460 x = self->parent->location.x + self->parent->size.width +
461 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
463 /* need to get the width. is this bad?*/
464 menu_render(self->submenu);
466 a = screen_physical_area_monitor(self->parent->xin_area);
468 if (self->submenu->size.width + x >= a->x + a->width)
469 x = self->parent->location.x - self->submenu->size.width -
470 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
472 menu_show_full(self->submenu, x,
473 self->parent->location.y + self->y,
474 self->parent->client);
477 self->parent->over = g_list_find(self->parent->entries, self);
480 self->hilite = FALSE;
482 menu_entry_render(self);
485 void menu_control_keyboard_nav(unsigned int key)
487 static ObMenu *current_menu = NULL;
489 ObKey obkey = OB_NUM_KEYS;
491 /* hrmm. could be fixed */
492 if (key == ob_keycode(OB_KEY_DOWN))
494 else if (key == ob_keycode(OB_KEY_UP))
496 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
497 obkey = OB_KEY_RIGHT;
498 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
501 if (current_menu == NULL)
502 current_menu = menu_visible->data;
506 if (current_menu->over) {
507 current_menu->mouseover(current_menu->over->data, FALSE);
508 current_menu->over = (current_menu->over->next != NULL ?
509 current_menu->over->next :
510 current_menu->entries);
513 current_menu->over = current_menu->entries;
515 if (current_menu->over)
516 current_menu->mouseover(current_menu->over->data, TRUE);
521 if (current_menu->over) {
522 current_menu->mouseover(current_menu->over->data, FALSE);
523 current_menu->over = (current_menu->over->prev != NULL ?
524 current_menu->over->prev :
525 g_list_last(current_menu->entries));
527 current_menu->over = g_list_last(current_menu->entries);
529 if (current_menu->over)
530 current_menu->mouseover(current_menu->over->data, TRUE);
536 if (current_menu->over == NULL)
539 e = (ObMenuEntry *)current_menu->over->data;
541 current_menu = e->submenu;
542 current_menu->over = current_menu->entries;
543 if (current_menu->over)
544 current_menu->mouseover(current_menu->over->data, TRUE);
546 current_menu->mouseover(e, FALSE);
547 current_menu->over = NULL;
550 menu_entry_fire(e, 0, 0, 0);
555 if (current_menu->over == NULL)
557 current_menu->mouseover(current_menu->over->data, FALSE);
558 current_menu->over = NULL;
560 menu_hide(current_menu);
562 if (current_menu->parent)
563 current_menu = current_menu->parent;
573 /* This noop brought to you by OLS 2003 Email Garden. */