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)
23 gchar *id = NULL, *title = NULL, *label = NULL;
26 if (!parse_attr_string("id", node->parent, &id))
28 if (!parse_attr_string("label", node->parent, &title))
31 g_message("menu label %s", title);
33 menu = menu_new(title, id, data ? *((Menu**)data) : NULL);
35 *((Menu**)data) = menu;
38 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
40 parse_menu(doc, node->xmlChildrenNode, &parent);
41 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
44 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
45 if (parse_attr_string("label", node, &label)) {
46 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
47 act = action_parse(doc, nact);
51 menu_add_entry(menu, menu_entry_new(label, act));
53 menu_add_entry(menu, menu_entry_new_separator(label));
65 void menu_control_show(Menu *self, int x, int y, ObClient *client);
67 void menu_destroy_hash_key(Menu *menu)
72 void menu_destroy_hash_value(Menu *self)
76 for (it = self->entries; it; it = it->next)
77 menu_entry_free(it->data);
78 g_list_free(self->entries);
83 g_hash_table_remove(window_map, &self->title);
84 g_hash_table_remove(window_map, &self->frame);
85 g_hash_table_remove(window_map, &self->items);
87 stacking_remove(self);
89 RrAppearanceFree(self->a_title);
90 XDestroyWindow(ob_display, self->title);
91 XDestroyWindow(ob_display, self->frame);
92 XDestroyWindow(ob_display, self->items);
97 void menu_entry_free(MenuEntry *self)
100 action_free(self->action);
102 g_hash_table_remove(window_map, &self->item);
104 RrAppearanceFree(self->a_item);
105 RrAppearanceFree(self->a_disabled);
106 RrAppearanceFree(self->a_hilite);
107 XDestroyWindow(ob_display, self->item);
121 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
122 (GDestroyNotify)menu_destroy_hash_key,
123 (GDestroyNotify)menu_destroy_hash_value);
125 parse_register("menu", parse_menu, NULL);
128 m = menu_new("sex menu", "root", NULL);
130 a = action_from_string("execute");
131 a->data.execute.path = g_strdup("xterm");
132 menu_add_entry(m, menu_entry_new("xterm", a));
133 a = action_from_string("restart");
134 menu_add_entry(m, menu_entry_new("restart", a));
135 menu_add_entry(m, menu_entry_new_separator("--"));
136 a = action_from_string("exit");
137 menu_add_entry(m, menu_entry_new("exit", a));
141 s = menu_new("subsex menu", "submenu", m);
142 a = action_from_string("execute");
143 a->data.execute.path = g_strdup("xclock");
144 menu_add_entry(s, menu_entry_new("xclock", a));
146 menu_add_entry(m, menu_entry_new_submenu("subz", s));
148 s = menu_new("empty", "chub", m);
149 menu_add_entry(m, menu_entry_new_submenu("empty", s));
151 s = menu_new("", "s-club", m);
152 menu_add_entry(m, menu_entry_new_submenu("empty", s));
154 s = menu_new(NULL, "h-club", m);
155 menu_add_entry(m, menu_entry_new_submenu("empty", s));
157 s = menu_new(NULL, "g-club", m);
159 a = action_from_string("execute");
160 a->data.execute.path = g_strdup("xterm");
161 menu_add_entry(s, menu_entry_new("xterm", a));
162 a = action_from_string("restart");
163 menu_add_entry(s, menu_entry_new("restart", a));
164 menu_add_entry(s, menu_entry_new_separator("--"));
165 a = action_from_string("exit");
166 menu_add_entry(s, menu_entry_new("exit", a));
168 menu_add_entry(m, menu_entry_new_submenu("long", s));
174 g_hash_table_destroy(menu_hash);
177 static Window createWindow(Window parent, unsigned long mask,
178 XSetWindowAttributes *attrib)
180 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
181 RrDepth(ob_rr_inst), InputOutput,
182 RrVisual(ob_rr_inst), mask, attrib);
186 Menu *menu_new_full(char *label, char *name, Menu *parent,
187 menu_controller_show show, menu_controller_update update)
189 XSetWindowAttributes attrib;
192 self = g_new0(Menu, 1);
193 self->obwin.type = Window_Menu;
194 self->label = g_strdup(label);
195 self->name = g_strdup(name);
196 self->parent = parent;
197 self->open_submenu = NULL;
199 self->entries = NULL;
201 self->invalid = TRUE;
203 /* default controllers */
206 self->update = update;
207 self->mouseover = NULL;
208 self->selected = NULL;
211 self->plugin_data = NULL;
213 attrib.override_redirect = TRUE;
214 attrib.event_mask = FRAME_EVENTMASK;
215 self->frame = createWindow(ob_root,
216 CWOverrideRedirect|CWEventMask, &attrib);
217 attrib.event_mask = TITLE_EVENTMASK;
218 self->title = createWindow(self->frame, CWEventMask, &attrib);
219 self->items = createWindow(self->frame, 0, &attrib);
221 self->a_title = self->a_items = NULL;
223 XMapWindow(ob_display, self->title);
224 XMapWindow(ob_display, self->items);
226 g_hash_table_insert(window_map, &self->frame, self);
227 g_hash_table_insert(window_map, &self->title, self);
228 g_hash_table_insert(window_map, &self->items, self);
229 g_hash_table_insert(menu_hash, g_strdup(name), self);
231 stacking_add(MENU_AS_WINDOW(self));
232 stacking_raise(MENU_AS_WINDOW(self));
237 void menu_free(char *name)
239 g_hash_table_remove(menu_hash, name);
242 MenuEntry *menu_entry_new_full(char *label, Action *action,
243 MenuEntryRenderType render_type,
246 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
248 menu_entry->label = g_strdup(label);
249 menu_entry->render_type = render_type;
250 menu_entry->action = action;
252 menu_entry->hilite = FALSE;
253 menu_entry->enabled = TRUE;
255 menu_entry->submenu = submenu;
260 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
262 g_assert(entry != NULL);
264 entry->submenu = submenu;
266 if(entry->parent != NULL)
267 entry->parent->invalid = TRUE;
270 void menu_add_entry(Menu *menu, MenuEntry *entry)
272 XSetWindowAttributes attrib;
274 g_assert(menu != NULL);
275 g_assert(entry != NULL);
276 g_assert(entry->item == None);
278 menu->entries = g_list_append(menu->entries, entry);
279 entry->parent = menu;
281 attrib.event_mask = ENTRY_EVENTMASK;
282 entry->item = createWindow(menu->items, CWEventMask, &attrib);
283 XMapWindow(ob_display, entry->item);
285 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
287 menu->invalid = TRUE;
289 g_hash_table_insert(window_map, &entry->item, menu);
292 void menu_show(char *name, int x, int y, ObClient *client)
296 self = g_hash_table_lookup(menu_hash, name);
298 g_warning("Attempted to show menu '%s' but it does not exist.",
303 menu_show_full(self, x, y, client);
306 void menu_show_full(Menu *self, int x, int y, ObClient *client)
308 g_assert(self != NULL);
312 self->client = client;
316 grab_pointer(TRUE, None);
319 menu_visible = g_slist_append(menu_visible, self);
323 self->show(self, x, y, client);
325 menu_control_show(self, x, y, client);
329 void menu_hide(Menu *self) {
331 XUnmapWindow(ob_display, self->frame);
333 if (self->open_submenu)
334 menu_hide(self->open_submenu);
335 if (self->parent && self->parent->open_submenu == self)
336 self->parent->open_submenu = NULL;
339 grab_keyboard(FALSE);
340 grab_pointer(FALSE, None);
342 menu_visible = g_slist_remove(menu_visible, self);
346 void menu_clear(Menu *self) {
349 for (it = self->entries; it; it = it->next) {
350 MenuEntry *entry = it->data;
351 menu_entry_free(entry);
353 self->entries = NULL;
354 self->invalid = TRUE;
358 MenuEntry *menu_find_entry(Menu *menu, Window win)
362 for (it = menu->entries; it; it = it->next) {
363 MenuEntry *entry = it->data;
364 if (entry->item == win)
370 MenuEntry *menu_find_entry_by_pos(Menu *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 g_message ("%d %p", y/menu->item_h, g_list_nth_data(menu->entries, y / menu->item_h));
379 return g_list_nth_data(menu->entries, y / menu->item_h);
382 void menu_entry_fire(MenuEntry *self)
387 self->action->data.any.c = self->parent->client;
388 self->action->func(&self->action->data);
390 /* hide the whole thing */
392 while (m->parent) m = m->parent;
398 Default menu controller action for showing.
401 void menu_control_show(Menu *self, int x, int y, ObClient *client) {
405 g_assert(!self->invalid);
407 for (i = 0; i < screen_num_monitors; ++i) {
408 a = screen_physical_area_monitor(i);
409 if (RECT_CONTAINS(*a, x, y))
415 POINT_SET(self->location,
416 MIN(x, a->x + a->width - 1 - self->size.width),
417 MIN(y, a->y + a->height - 1 - self->size.height));
418 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
421 XMapWindow(ob_display, self->frame);
422 stacking_raise(MENU_AS_WINDOW(self));
424 } else if (self->shown && self->open_submenu) {
425 menu_hide(self->open_submenu);
429 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
433 self->hilite = enter;
436 if (self->parent->open_submenu && self->submenu
437 != self->parent->open_submenu)
438 menu_hide(self->parent->open_submenu);
440 if (self->submenu && self->parent->open_submenu != self->submenu) {
441 self->parent->open_submenu = self->submenu;
443 /* shouldn't be invalid since it must be displayed */
444 g_assert(!self->parent->invalid);
445 /* TODO: I don't understand why these bevels should be here.
446 Something must be wrong in the width calculation */
447 x = self->parent->location.x + self->parent->size.width +
450 /* need to get the width. is this bad?*/
451 menu_render(self->submenu);
453 a = screen_physical_area_monitor(self->parent->xin_area);
455 if (self->submenu->size.width + x >= a->x + a->width)
456 x = self->parent->location.x - self->submenu->size.width -
459 menu_show_full(self->submenu, x,
460 self->parent->location.y + self->y,
461 self->parent->client);