10 GHashTable *menu_hash = NULL;
11 GSList *menu_visible = NULL;
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17 ButtonPressMask | ButtonReleaseMask)
19 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
21 parse_menu_full(doc, node, data, TRUE);
25 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
31 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
32 ObMenu *menu = NULL, *parent;
34 if (newmenu == TRUE) {
35 if (!parse_attr_string("id", node->parent, &id))
37 if (!parse_attr_string("label", node->parent, &title))
40 g_message("menu label %s", title);
42 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
45 *((ObMenu**)data) = menu;
47 menu = (ObMenu *)data;
51 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
52 if (parse_attr_string("plugin", node, &plugin)) {
53 PluginMenuCreateData data = {
58 parent = plugin_create(plugin, &data);
61 parse_menu(doc, node->xmlChildrenNode, &parent);
62 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
67 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
68 if (parse_attr_string("label", node, &label)) {
69 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
70 act = action_parse(doc, nact);
74 menu_add_entry(menu, menu_entry_new(label, act));
76 menu_add_entry(menu, menu_entry_new_separator(label));
88 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
90 void menu_destroy_hash_key(ObMenu *menu)
95 void menu_destroy_hash_value(ObMenu *self)
99 for (it = self->entries; it; it = it->next)
100 menu_entry_free(it->data);
101 g_list_free(self->entries);
106 g_hash_table_remove(window_map, &self->title);
107 g_hash_table_remove(window_map, &self->frame);
108 g_hash_table_remove(window_map, &self->items);
110 stacking_remove(self);
112 RrAppearanceFree(self->a_title);
113 XDestroyWindow(ob_display, self->title);
114 XDestroyWindow(ob_display, self->frame);
115 XDestroyWindow(ob_display, self->items);
120 void menu_entry_free(ObMenuEntry *self)
123 action_free(self->action);
125 g_hash_table_remove(window_map, &self->item);
127 RrAppearanceFree(self->a_item);
128 RrAppearanceFree(self->a_disabled);
129 RrAppearanceFree(self->a_hilite);
130 XDestroyWindow(ob_display, self->item);
144 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
145 (GDestroyNotify)menu_destroy_hash_key,
146 (GDestroyNotify)menu_destroy_hash_value);
148 parse_register("menu", parse_menu, NULL);
151 m = menu_new("sex menu", "root", NULL);
153 a = action_from_string("execute");
154 a->data.execute.path = g_strdup("xterm");
155 menu_add_entry(m, menu_entry_new("xterm", a));
156 a = action_from_string("restart");
157 menu_add_entry(m, menu_entry_new("restart", a));
158 menu_add_entry(m, menu_entry_new_separator("--"));
159 a = action_from_string("exit");
160 menu_add_entry(m, menu_entry_new("exit", a));
164 s = menu_new("subsex menu", "submenu", m);
165 a = action_from_string("execute");
166 a->data.execute.path = g_strdup("xclock");
167 menu_add_entry(s, menu_entry_new("xclock", a));
169 menu_add_entry(m, menu_entry_new_submenu("subz", s));
171 s = menu_new("empty", "chub", m);
172 menu_add_entry(m, menu_entry_new_submenu("empty", s));
174 s = menu_new("", "s-club", m);
175 menu_add_entry(m, menu_entry_new_submenu("empty", s));
177 s = menu_new(NULL, "h-club", m);
178 menu_add_entry(m, menu_entry_new_submenu("empty", s));
180 s = menu_new(NULL, "g-club", m);
182 a = action_from_string("execute");
183 a->data.execute.path = g_strdup("xterm");
184 menu_add_entry(s, menu_entry_new("xterm", a));
185 a = action_from_string("restart");
186 menu_add_entry(s, menu_entry_new("restart", a));
187 menu_add_entry(s, menu_entry_new_separator("--"));
188 a = action_from_string("exit");
189 menu_add_entry(s, menu_entry_new("exit", a));
191 menu_add_entry(m, menu_entry_new_submenu("long", s));
197 g_hash_table_destroy(menu_hash);
200 static Window createWindow(Window parent, unsigned long mask,
201 XSetWindowAttributes *attrib)
203 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
204 RrDepth(ob_rr_inst), InputOutput,
205 RrVisual(ob_rr_inst), mask, attrib);
209 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
210 menu_controller_show show, menu_controller_update update)
212 XSetWindowAttributes attrib;
215 self = g_new0(ObMenu, 1);
216 self->obwin.type = Window_Menu;
217 self->label = g_strdup(label);
218 self->name = g_strdup(name);
219 self->parent = parent;
220 self->open_submenu = NULL;
222 self->entries = NULL;
224 self->invalid = TRUE;
226 /* default controllers */
229 self->update = update;
230 self->mouseover = NULL;
231 self->selected = NULL;
234 self->plugin_data = NULL;
236 attrib.override_redirect = TRUE;
237 attrib.event_mask = FRAME_EVENTMASK;
238 self->frame = createWindow(RootWindow(ob_display, ob_screen),
239 CWOverrideRedirect|CWEventMask, &attrib);
240 attrib.event_mask = TITLE_EVENTMASK;
241 self->title = createWindow(self->frame, CWEventMask, &attrib);
242 self->items = createWindow(self->frame, 0, &attrib);
244 self->a_title = self->a_items = NULL;
246 XMapWindow(ob_display, self->title);
247 XMapWindow(ob_display, self->items);
249 g_hash_table_insert(window_map, &self->frame, self);
250 g_hash_table_insert(window_map, &self->title, self);
251 g_hash_table_insert(window_map, &self->items, self);
252 g_hash_table_insert(menu_hash, g_strdup(name), self);
254 stacking_add(MENU_AS_WINDOW(self));
255 stacking_raise(MENU_AS_WINDOW(self));
260 void menu_free(char *name)
262 g_hash_table_remove(menu_hash, name);
265 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
266 ObMenuEntryRenderType render_type,
269 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
271 menu_entry->label = g_strdup(label);
272 menu_entry->render_type = render_type;
273 menu_entry->action = action;
275 menu_entry->hilite = FALSE;
276 menu_entry->enabled = TRUE;
278 menu_entry->submenu = submenu;
283 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
285 g_assert(entry != NULL);
287 entry->submenu = submenu;
289 if(entry->parent != NULL)
290 entry->parent->invalid = TRUE;
293 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
295 XSetWindowAttributes attrib;
297 g_assert(menu != NULL);
298 g_assert(entry != NULL);
299 g_assert(entry->item == None);
301 menu->entries = g_list_append(menu->entries, entry);
302 entry->parent = menu;
304 attrib.event_mask = ENTRY_EVENTMASK;
305 entry->item = createWindow(menu->items, CWEventMask, &attrib);
306 XMapWindow(ob_display, entry->item);
308 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
310 menu->invalid = TRUE;
312 g_hash_table_insert(window_map, &entry->item, menu);
315 void menu_show(char *name, int x, int y, ObClient *client)
319 self = g_hash_table_lookup(menu_hash, name);
321 g_warning("Attempted to show menu '%s' but it does not exist.",
326 menu_show_full(self, x, y, client);
329 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
331 g_assert(self != NULL);
335 self->client = client;
339 grab_pointer(TRUE, None);
342 menu_visible = g_slist_append(menu_visible, self);
346 self->show(self, x, y, client);
348 menu_control_show(self, x, y, client);
352 void menu_hide(ObMenu *self) {
354 XUnmapWindow(ob_display, self->frame);
356 if (self->open_submenu)
357 menu_hide(self->open_submenu);
358 if (self->parent && self->parent->open_submenu == self)
359 self->parent->open_submenu = NULL;
362 grab_keyboard(FALSE);
363 grab_pointer(FALSE, None);
365 menu_visible = g_slist_remove(menu_visible, self);
369 void menu_clear(ObMenu *self) {
372 for (it = self->entries; it; it = it->next) {
373 ObMenuEntry *entry = it->data;
374 menu_entry_free(entry);
376 self->entries = NULL;
377 self->invalid = TRUE;
381 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
385 for (it = menu->entries; it; it = it->next) {
386 ObMenuEntry *entry = it->data;
387 if (entry->item == win)
393 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
395 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
398 y -= menu->title_h + ob_rr_theme->bwidth;
399 if (y < 0) return NULL;
401 g_message ("%d %p", y/menu->item_h, g_list_nth_data(menu->entries, y / menu->item_h));
402 return g_list_nth_data(menu->entries, y / menu->item_h);
405 void menu_entry_fire(ObMenuEntry *self)
410 self->action->data.any.c = self->parent->client;
411 self->action->func(&self->action->data);
413 /* hide the whole thing */
415 while (m->parent) m = m->parent;
421 Default menu controller action for showing.
424 void menu_control_show(ObMenu *self, int x, int y, ObClient *client) {
428 g_assert(!self->invalid);
430 for (i = 0; i < screen_num_monitors; ++i) {
431 a = screen_physical_area_monitor(i);
432 if (RECT_CONTAINS(*a, x, y))
438 POINT_SET(self->location,
439 MIN(x, a->x + a->width - 1 - self->size.width),
440 MIN(y, a->y + a->height - 1 - self->size.height));
441 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
444 XMapWindow(ob_display, self->frame);
445 stacking_raise(MENU_AS_WINDOW(self));
447 } else if (self->shown && self->open_submenu) {
448 menu_hide(self->open_submenu);
452 void menu_control_mouseover(ObMenuEntry *self, gboolean enter) {
456 self->hilite = enter;
459 if (self->parent->open_submenu && self->submenu
460 != self->parent->open_submenu)
461 menu_hide(self->parent->open_submenu);
463 if (self->submenu && self->parent->open_submenu != self->submenu) {
464 self->parent->open_submenu = self->submenu;
466 /* shouldn't be invalid since it must be displayed */
467 g_assert(!self->parent->invalid);
468 /* TODO: I don't understand why these bevels should be here.
469 Something must be wrong in the width calculation */
470 x = self->parent->location.x + self->parent->size.width +
473 /* need to get the width. is this bad?*/
474 menu_render(self->submenu);
476 a = screen_physical_area_monitor(self->parent->xin_area);
478 if (self->submenu->size.width + x >= a->x + a->width)
479 x = self->parent->location.x - self->submenu->size.width -
482 menu_show_full(self->submenu, x,
483 self->parent->location.y + self->y,
484 self->parent->client);