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;
49 if (plugin_open_reopen(plugin))
50 parent = plugin_create(plugin, &data);
53 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
56 *((ObMenu**)data) = menu;
58 menu = (ObMenu *)data;
61 node = node->xmlChildrenNode;
64 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
65 if (parse_attr_string("plugin", node, &plugin)) {
66 PluginMenuCreateData data;
70 if (plugin_open_reopen(plugin))
71 parent = plugin_create(plugin, &data);
75 parse_menu(doc, node, &parent);
76 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
81 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
82 if (parse_attr_string("label", node, &label)) {
83 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
84 act = action_parse(doc, nact);
88 menu_add_entry(menu, menu_entry_new(label, act));
90 menu_add_entry(menu, menu_entry_new_separator(label));
102 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
104 void menu_destroy_hash_key(ObMenu *menu)
109 void menu_destroy_hash_value(ObMenu *self)
113 for (it = self->entries; it; it = it->next)
114 menu_entry_free(it->data);
115 g_list_free(self->entries);
120 g_hash_table_remove(window_map, &self->title);
121 g_hash_table_remove(window_map, &self->frame);
122 g_hash_table_remove(window_map, &self->items);
124 stacking_remove(self);
126 RrAppearanceFree(self->a_title);
127 RrAppearanceFree(self->a_items);
128 XDestroyWindow(ob_display, self->title);
129 XDestroyWindow(ob_display, self->frame);
130 XDestroyWindow(ob_display, self->items);
135 void menu_entry_free(ObMenuEntry *self)
138 action_free(self->action);
140 g_hash_table_remove(window_map, &self->item);
142 RrAppearanceFree(self->a_item);
143 RrAppearanceFree(self->a_disabled);
144 RrAppearanceFree(self->a_hilite);
145 XDestroyWindow(ob_display, self->item);
152 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
153 (GDestroyNotify)menu_destroy_hash_key,
154 (GDestroyNotify)menu_destroy_hash_value);
156 parse_register("menu", parse_menu, NULL);
162 g_hash_table_destroy(menu_hash);
165 static Window createWindow(Window parent, unsigned long mask,
166 XSetWindowAttributes *attrib)
168 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
169 RrDepth(ob_rr_inst), InputOutput,
170 RrVisual(ob_rr_inst), mask, attrib);
174 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
175 menu_controller_show show, menu_controller_update update,
176 menu_controller_selected selected,
177 menu_controller_hide hide,
178 menu_controller_mouseover mouseover)
180 XSetWindowAttributes attrib;
183 self = g_new0(ObMenu, 1);
184 self->obwin.type = Window_Menu;
185 self->label = g_strdup(label);
186 self->name = g_strdup(name);
187 self->parent = parent;
188 self->open_submenu = NULL;
191 self->entries = NULL;
193 self->invalid = TRUE;
195 /* default controllers */
196 self->show = (show != NULL ? show : menu_show_full);
197 self->hide = (hide != NULL ? hide : menu_hide);
198 self->update = (update != NULL ? update : menu_render);
199 self->mouseover = (mouseover != NULL ? mouseover :
200 menu_control_mouseover);
201 self->selected = (selected != NULL ? selected : menu_entry_fire);
204 self->plugin_data = NULL;
206 attrib.override_redirect = TRUE;
207 attrib.event_mask = FRAME_EVENTMASK;
208 self->frame = createWindow(RootWindow(ob_display, ob_screen),
209 CWOverrideRedirect|CWEventMask, &attrib);
210 attrib.event_mask = TITLE_EVENTMASK;
211 self->title = createWindow(self->frame, CWEventMask, &attrib);
212 self->items = createWindow(self->frame, 0, &attrib);
214 self->a_title = self->a_items = NULL;
216 XMapWindow(ob_display, self->title);
217 XMapWindow(ob_display, self->items);
219 g_hash_table_insert(window_map, &self->frame, self);
220 g_hash_table_insert(window_map, &self->title, self);
221 g_hash_table_insert(window_map, &self->items, self);
222 g_hash_table_insert(menu_hash, g_strdup(name), self);
224 stacking_add(MENU_AS_WINDOW(self));
225 stacking_raise(MENU_AS_WINDOW(self));
230 void menu_free(char *name)
232 g_hash_table_remove(menu_hash, name);
235 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
236 ObMenuEntryRenderType render_type,
239 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
241 menu_entry->label = g_strdup(label);
242 menu_entry->render_type = render_type;
243 menu_entry->action = action;
245 menu_entry->hilite = FALSE;
246 menu_entry->enabled = TRUE;
248 menu_entry->submenu = submenu;
253 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
255 g_assert(entry != NULL);
257 entry->submenu = submenu;
259 if(entry->parent != NULL)
260 entry->parent->invalid = TRUE;
263 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
265 XSetWindowAttributes attrib;
267 g_assert(menu != NULL);
268 g_assert(entry != NULL);
269 g_assert(entry->item == None);
271 menu->entries = g_list_append(menu->entries, entry);
272 entry->parent = menu;
274 attrib.event_mask = ENTRY_EVENTMASK;
275 entry->item = createWindow(menu->items, CWEventMask, &attrib);
276 XMapWindow(ob_display, entry->item);
278 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
280 menu->invalid = TRUE;
282 g_hash_table_insert(window_map, &entry->item, menu);
285 void menu_show(char *name, int x, int y, ObClient *client)
289 self = g_hash_table_lookup(menu_hash, name);
291 g_warning("Attempted to show menu '%s' but it does not exist.",
296 menu_show_full(self, x, y, client);
299 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
301 g_assert(self != NULL);
305 self->client = client;
308 if (!(self->parent && self->parent->shown)) {
309 grab_pointer(TRUE, None);
312 menu_visible = g_list_append(menu_visible, self);
315 menu_control_show(self, x, y, client);
318 void menu_hide(ObMenu *self) {
320 XUnmapWindow(ob_display, self->frame);
322 if (self->open_submenu)
323 menu_hide(self->open_submenu);
324 if (self->parent && self->parent->open_submenu == self) {
325 self->parent->open_submenu = NULL;
328 if (!(self->parent && self->parent->shown)) {
329 grab_keyboard(FALSE);
330 grab_pointer(FALSE, None);
332 menu_visible = g_list_remove(menu_visible, self);
334 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
335 menu_entry_render(self->over->data);
341 void menu_clear(ObMenu *self) {
344 for (it = self->entries; it; it = it->next) {
345 ObMenuEntry *entry = it->data;
346 menu_entry_free(entry);
348 self->entries = NULL;
349 self->invalid = TRUE;
353 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
357 for (it = menu->entries; it; it = it->next) {
358 ObMenuEntry *entry = it->data;
359 if (entry->item == win)
365 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
369 for (it = menu->entries; it; it = it->next) {
370 ObMenuEntry *entry = it->data;
371 if (entry->submenu == submenu)
377 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
379 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
382 y -= menu->title_h + ob_rr_theme->bwidth;
383 if (y < 0) return NULL;
385 ob_debug("%d %p\n", y/menu->item_h,
386 g_list_nth_data(menu->entries, y / menu->item_h));
387 return g_list_nth_data(menu->entries, y / menu->item_h);
390 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
395 if (button != 1) return;
398 self->action->data.any.c = self->parent->client;
399 self->action->func(&self->action->data);
401 /* hide the whole thing */
403 while (m->parent) m = m->parent;
409 Default menu controller action for showing.
412 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
417 g_assert(!self->invalid);
419 for (i = 0; i < screen_num_monitors; ++i) {
420 a = screen_physical_area_monitor(i);
421 if (RECT_CONTAINS(*a, x, y))
427 POINT_SET(self->location,
428 MIN(x, a->x + a->width - 1 - self->size.width),
429 MIN(y, a->y + a->height - 1 - self->size.height));
430 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
433 XMapWindow(ob_display, self->frame);
434 stacking_raise(MENU_AS_WINDOW(self));
436 } else if (self->shown && self->open_submenu) {
437 menu_hide(self->open_submenu);
441 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
447 g_assert(self != NULL);
450 /* TODO: we prolly don't need open_submenu */
451 if (self->parent->open_submenu && self->submenu
452 != self->parent->open_submenu)
454 e = (ObMenuEntry *) self->parent->over->data;
456 menu_entry_render(e);
457 menu_hide(self->parent->open_submenu);
460 if (self->submenu && self->parent->open_submenu != self->submenu) {
461 self->parent->open_submenu = self->submenu;
463 /* shouldn't be invalid since it must be displayed */
464 g_assert(!self->parent->invalid);
465 /* TODO: I don't understand why these bevels should be here.
466 Something must be wrong in the width calculation */
467 x = self->parent->location.x + self->parent->size.width +
468 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
470 /* need to get the width. is this bad?*/
471 self->parent->update(self->submenu);
473 a = screen_physical_area_monitor(self->parent->xin_area);
475 if (self->submenu->size.width + x >= a->x + a->width)
476 x = self->parent->location.x - self->submenu->size.width -
477 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
479 menu_show_full(self->submenu, x,
480 self->parent->location.y + self->y,
481 self->parent->client);
484 self->parent->over = g_list_find(self->parent->entries, self);
487 self->hilite = FALSE;
489 menu_entry_render(self);
492 void menu_control_keyboard_nav(unsigned int key)
494 static ObMenu *current_menu = NULL;
495 ObMenuEntry *e = NULL;
497 ObKey obkey = OB_NUM_KEYS;
499 /* hrmm. could be fixed */
500 if (key == ob_keycode(OB_KEY_DOWN))
502 else if (key == ob_keycode(OB_KEY_UP))
504 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
505 obkey = OB_KEY_RIGHT;
506 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
509 if (current_menu == NULL)
510 current_menu = menu_visible->data;
514 if (current_menu->over) {
515 current_menu->mouseover(current_menu->over->data, FALSE);
516 current_menu->over = (current_menu->over->next != NULL ?
517 current_menu->over->next :
518 current_menu->entries);
521 current_menu->over = current_menu->entries;
523 if (current_menu->over)
524 current_menu->mouseover(current_menu->over->data, TRUE);
529 if (current_menu->over) {
530 current_menu->mouseover(current_menu->over->data, FALSE);
531 current_menu->over = (current_menu->over->prev != NULL ?
532 current_menu->over->prev :
533 g_list_last(current_menu->entries));
535 current_menu->over = g_list_last(current_menu->entries);
537 if (current_menu->over)
538 current_menu->mouseover(current_menu->over->data, TRUE);
543 if (current_menu->over == NULL)
545 e = (ObMenuEntry *)current_menu->over->data;
547 current_menu = e->submenu;
548 current_menu->over = current_menu->entries;
549 if (current_menu->over)
550 current_menu->mouseover(current_menu->over->data, TRUE);
555 case OB_KEY_RETURN: {
556 if (current_menu->over == NULL)
558 e = (ObMenuEntry *)current_menu->over->data;
560 current_menu->mouseover(e, FALSE);
561 current_menu->over = NULL;
563 menu_entry_fire(e, 0, 0, 0);
567 if (current_menu->over != NULL) {
568 current_menu->mouseover(current_menu->over->data, FALSE);
569 current_menu->over = NULL;
572 menu_hide(current_menu);
574 if (current_menu->parent)
575 current_menu = current_menu->parent;
581 menu_hide(current_menu);
588 /* This noop brought to you by OLS 2003 Email Garden. */