9 GHashTable *menu_hash = NULL;
10 GSList *menu_visible = NULL;
12 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
14 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
15 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
16 ButtonPressMask | ButtonReleaseMask)
18 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
22 gchar *id = NULL, *title = NULL, *label = NULL;
25 if (!parse_attr_string("id", node->parent, &id))
27 if (!parse_attr_string("label", node->parent, &title))
30 g_message("menu label %s", title);
32 menu = menu_new(title, id, data ? *((Menu**)data) : NULL);
34 *((Menu**)data) = menu;
37 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
39 parse_menu(doc, node->xmlChildrenNode, &parent);
40 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
43 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
44 if (parse_attr_string("label", node, &label)) {
45 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
46 act = action_parse(doc, nact);
50 menu_add_entry(menu, menu_entry_new(label, act));
52 menu_add_entry(menu, menu_entry_new_separator(label));
64 void menu_control_show(Menu *self, int x, int y, Client *client);
66 void menu_destroy_hash_key(Menu *menu)
71 void menu_destroy_hash_value(Menu *self)
75 for (it = self->entries; it; it = it->next)
76 menu_entry_free(it->data);
77 g_list_free(self->entries);
82 g_hash_table_remove(window_map, &self->title);
83 g_hash_table_remove(window_map, &self->frame);
84 g_hash_table_remove(window_map, &self->items);
86 stacking_remove(self);
88 RrAppearanceFree(self->a_title);
89 XDestroyWindow(ob_display, self->title);
90 XDestroyWindow(ob_display, self->frame);
91 XDestroyWindow(ob_display, self->items);
96 void menu_entry_free(MenuEntry *self)
99 action_free(self->action);
101 g_hash_table_remove(window_map, &self->item);
103 RrAppearanceFree(self->a_item);
104 RrAppearanceFree(self->a_disabled);
105 RrAppearanceFree(self->a_hilite);
106 XDestroyWindow(ob_display, self->item);
120 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
121 (GDestroyNotify)menu_destroy_hash_key,
122 (GDestroyNotify)menu_destroy_hash_value);
124 parse_register("menu", parse_menu, NULL);
127 m = menu_new("sex menu", "root", NULL);
129 a = action_from_string("execute");
130 a->data.execute.path = g_strdup("xterm");
131 menu_add_entry(m, menu_entry_new("xterm", a));
132 a = action_from_string("restart");
133 menu_add_entry(m, menu_entry_new("restart", a));
134 menu_add_entry(m, menu_entry_new_separator("--"));
135 a = action_from_string("exit");
136 menu_add_entry(m, menu_entry_new("exit", a));
140 s = menu_new("subsex menu", "submenu", m);
141 a = action_from_string("execute");
142 a->data.execute.path = g_strdup("xclock");
143 menu_add_entry(s, menu_entry_new("xclock", a));
145 menu_add_entry(m, menu_entry_new_submenu("subz", s));
147 s = menu_new("empty", "chub", m);
148 menu_add_entry(m, menu_entry_new_submenu("empty", s));
150 s = menu_new("", "s-club", m);
151 menu_add_entry(m, menu_entry_new_submenu("empty", s));
153 s = menu_new(NULL, "h-club", m);
154 menu_add_entry(m, menu_entry_new_submenu("empty", s));
156 s = menu_new(NULL, "g-club", m);
158 a = action_from_string("execute");
159 a->data.execute.path = g_strdup("xterm");
160 menu_add_entry(s, menu_entry_new("xterm", a));
161 a = action_from_string("restart");
162 menu_add_entry(s, menu_entry_new("restart", a));
163 menu_add_entry(s, menu_entry_new_separator("--"));
164 a = action_from_string("exit");
165 menu_add_entry(s, menu_entry_new("exit", a));
167 menu_add_entry(m, menu_entry_new_submenu("long", s));
173 g_hash_table_destroy(menu_hash);
176 static Window createWindow(Window parent, unsigned long mask,
177 XSetWindowAttributes *attrib)
179 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
180 RrDepth(ob_rr_inst), InputOutput,
181 RrVisual(ob_rr_inst), mask, attrib);
185 Menu *menu_new_full(char *label, char *name, Menu *parent,
186 menu_controller_show show, menu_controller_update update)
188 XSetWindowAttributes attrib;
191 self = g_new0(Menu, 1);
192 self->obwin.type = Window_Menu;
193 self->label = g_strdup(label);
194 self->name = g_strdup(name);
195 self->parent = parent;
196 self->open_submenu = NULL;
198 self->entries = NULL;
200 self->invalid = TRUE;
202 /* default controllers */
205 self->update = update;
206 self->mouseover = NULL;
207 self->selected = NULL;
210 self->plugin_data = NULL;
212 attrib.override_redirect = TRUE;
213 attrib.event_mask = FRAME_EVENTMASK;
214 self->frame = createWindow(ob_root,
215 CWOverrideRedirect|CWEventMask, &attrib);
216 attrib.event_mask = TITLE_EVENTMASK;
217 self->title = createWindow(self->frame, CWEventMask, &attrib);
218 self->items = createWindow(self->frame, 0, &attrib);
220 self->a_title = self->a_items = NULL;
222 XMapWindow(ob_display, self->title);
223 XMapWindow(ob_display, self->items);
225 g_hash_table_insert(window_map, &self->frame, self);
226 g_hash_table_insert(window_map, &self->title, self);
227 g_hash_table_insert(window_map, &self->items, self);
228 g_hash_table_insert(menu_hash, g_strdup(name), self);
230 stacking_add(MENU_AS_WINDOW(self));
231 stacking_raise(MENU_AS_WINDOW(self));
236 void menu_free(char *name)
238 g_hash_table_remove(menu_hash, name);
241 MenuEntry *menu_entry_new_full(char *label, Action *action,
242 MenuEntryRenderType render_type,
245 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
247 menu_entry->label = g_strdup(label);
248 menu_entry->render_type = render_type;
249 menu_entry->action = action;
251 menu_entry->hilite = FALSE;
252 menu_entry->enabled = TRUE;
254 menu_entry->submenu = submenu;
259 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
261 g_assert(entry != NULL);
263 entry->submenu = submenu;
265 if(entry->parent != NULL)
266 entry->parent->invalid = TRUE;
269 void menu_add_entry(Menu *menu, MenuEntry *entry)
271 XSetWindowAttributes attrib;
273 g_assert(menu != NULL);
274 g_assert(entry != NULL);
275 g_assert(entry->item == None);
277 menu->entries = g_list_append(menu->entries, entry);
278 entry->parent = menu;
280 attrib.event_mask = ENTRY_EVENTMASK;
281 entry->item = createWindow(menu->items, CWEventMask, &attrib);
282 XMapWindow(ob_display, entry->item);
284 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
286 menu->invalid = TRUE;
288 g_hash_table_insert(window_map, &entry->item, menu);
291 void menu_show(char *name, int x, int y, Client *client)
295 self = g_hash_table_lookup(menu_hash, name);
297 g_warning("Attempted to show menu '%s' but it does not exist.",
302 menu_show_full(self, x, y, client);
305 void menu_show_full(Menu *self, int x, int y, Client *client)
307 g_assert(self != NULL);
311 self->client = client;
315 grab_pointer(TRUE, None);
318 menu_visible = g_slist_append(menu_visible, self);
322 self->show(self, x, y, client);
324 menu_control_show(self, x, y, client);
328 void menu_hide(Menu *self) {
330 XUnmapWindow(ob_display, self->frame);
332 if (self->open_submenu)
333 menu_hide(self->open_submenu);
334 if (self->parent && self->parent->open_submenu == self)
335 self->parent->open_submenu = NULL;
338 grab_keyboard(FALSE);
339 grab_pointer(FALSE, None);
341 menu_visible = g_slist_remove(menu_visible, self);
345 void menu_clear(Menu *self) {
348 for (it = self->entries; it; it = it->next) {
349 MenuEntry *entry = it->data;
350 menu_entry_free(entry);
352 self->entries = NULL;
353 self->invalid = TRUE;
357 MenuEntry *menu_find_entry(Menu *menu, Window win)
361 for (it = menu->entries; it; it = it->next) {
362 MenuEntry *entry = it->data;
363 if (entry->item == win)
369 MenuEntry *menu_find_entry_by_pos(Menu *menu, int x, int y)
371 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
374 y -= menu->title_h + ob_rr_theme->bwidth;
375 if (y < 0) return NULL;
377 g_message ("%d %p", y/menu->item_h, g_list_nth_data(menu->entries, y / menu->item_h));
378 return g_list_nth_data(menu->entries, y / menu->item_h);
381 void menu_entry_fire(MenuEntry *self)
386 self->action->data.any.c = self->parent->client;
387 self->action->func(&self->action->data);
389 /* hide the whole thing */
391 while (m->parent) m = m->parent;
397 Default menu controller action for showing.
400 void menu_control_show(Menu *self, int x, int y, Client *client) {
404 g_assert(!self->invalid);
406 for (i = 0; i < screen_num_xin_areas; ++i) {
407 a = screen_physical_area_xinerama(i);
408 if (RECT_CONTAINS(*a, x, y))
414 POINT_SET(self->location,
415 MIN(x, a->x + a->width - 1 - self->size.width),
416 MIN(y, a->y + a->height - 1 - self->size.height));
417 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
420 XMapWindow(ob_display, self->frame);
421 stacking_raise(MENU_AS_WINDOW(self));
423 } else if (self->shown && self->open_submenu) {
424 menu_hide(self->open_submenu);
428 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
432 self->hilite = enter;
435 if (self->parent->open_submenu && self->submenu
436 != self->parent->open_submenu)
437 menu_hide(self->parent->open_submenu);
439 if (self->submenu && self->parent->open_submenu != self->submenu) {
440 self->parent->open_submenu = self->submenu;
442 /* shouldn't be invalid since it must be displayed */
443 g_assert(!self->parent->invalid);
444 /* TODO: I don't understand why these bevels should be here.
445 Something must be wrong in the width calculation */
446 x = self->parent->location.x + self->parent->size.width +
449 /* need to get the width. is this bad?*/
450 menu_render(self->submenu);
452 a = screen_physical_area_xinerama(self->parent->xin_area);
454 if (self->submenu->size.width + x >= a->x + a->width)
455 x = self->parent->location.x - self->submenu->size.width -
458 menu_show_full(self->submenu, x,
459 self->parent->location.y + self->y,
460 self->parent->client);