5 #include "render/theme.h"
10 GHashTable *menu_hash = 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;
314 self->show(self, x, y, client);
316 menu_control_show(self, x, y, client);
320 void menu_hide(Menu *self) {
322 XUnmapWindow(ob_display, self->frame);
324 if (self->open_submenu)
325 menu_hide(self->open_submenu);
326 if (self->parent && self->parent->open_submenu == self)
327 self->parent->open_submenu = NULL;
332 void menu_clear(Menu *self) {
335 for (it = self->entries; it; it = it->next) {
336 MenuEntry *entry = it->data;
337 menu_entry_free(entry);
339 self->entries = NULL;
340 self->invalid = TRUE;
344 MenuEntry *menu_find_entry(Menu *menu, Window win)
348 for (it = menu->entries; it; it = it->next) {
349 MenuEntry *entry = it->data;
350 if (entry->item == win)
356 void menu_entry_fire(MenuEntry *self)
361 self->action->data.any.c = self->parent->client;
362 self->action->func(&self->action->data);
364 /* hide the whole thing */
366 while (m->parent) m = m->parent;
372 Default menu controller action for showing.
375 void menu_control_show(Menu *self, int x, int y, Client *client) {
376 g_assert(!self->invalid);
378 XMoveWindow(ob_display, self->frame,
379 MIN(x, screen_physical_size.width - self->size.width),
380 MIN(y, screen_physical_size.height - self->size.height));
381 POINT_SET(self->location,
382 MIN(x, screen_physical_size.width - self->size.width),
383 MIN(y, screen_physical_size.height - self->size.height));
386 XMapWindow(ob_display, self->frame);
387 stacking_raise(MENU_AS_WINDOW(self));
389 } else if (self->shown && self->open_submenu) {
390 menu_hide(self->open_submenu);
394 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
396 self->hilite = enter;
399 if (self->parent->open_submenu && self->submenu
400 != self->parent->open_submenu)
401 menu_hide(self->parent->open_submenu);
404 self->parent->open_submenu = self->submenu;
406 /* shouldn't be invalid since it must be displayed */
407 g_assert(!self->parent->invalid);
408 /* TODO: I don't understand why these bevels should be here.
409 Something must be wrong in the width calculation */
410 x = self->parent->location.x + self->parent->size.width +
413 /* need to get the width. is this bad?*/
414 menu_render(self->submenu);
416 if (self->submenu->size.width + x > screen_physical_size.width)
417 x = self->parent->location.x - self->submenu->size.width -
420 menu_show_full(self->submenu, x,
421 self->parent->location.y + self->y,
422 self->parent->client);