5 #include "render/theme.h"
10 static GHashTable *menu_hash = NULL;
11 GHashTable *menu_map = NULL;
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17 ButtonPressMask | ButtonReleaseMask)
19 void menu_control_show(Menu *self, int x, int y, Client *client);
21 void menu_destroy_hash_key(gpointer data)
26 void menu_destroy_hash_value(Menu *self)
30 for (it = self->entries; it; it = it->next)
31 menu_entry_free(it->data);
32 g_list_free(self->entries);
37 g_hash_table_remove(menu_map, &self->title);
38 g_hash_table_remove(menu_map, &self->frame);
39 g_hash_table_remove(menu_map, &self->items);
41 appearance_free(self->a_title);
42 XDestroyWindow(ob_display, self->title);
43 XDestroyWindow(ob_display, self->frame);
44 XDestroyWindow(ob_display, self->items);
49 void menu_entry_free(MenuEntry *self)
52 action_free(self->action);
54 g_hash_table_remove(menu_map, &self->item);
56 appearance_free(self->a_item);
57 appearance_free(self->a_disabled);
58 appearance_free(self->a_hilite);
59 XDestroyWindow(ob_display, self->item);
71 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
72 menu_destroy_hash_key,
73 (GDestroyNotify)menu_destroy_hash_value);
74 menu_map = g_hash_table_new(g_int_hash, g_int_equal);
76 m = menu_new(NULL, "root", NULL);
78 a = action_from_string("execute");
79 a->data.execute.path = g_strdup("xterm");
80 menu_add_entry(m, menu_entry_new("xterm", a));
81 a = action_from_string("restart");
82 menu_add_entry(m, menu_entry_new("restart", a));
83 menu_add_entry(m, menu_entry_new_separator("--"));
84 a = action_from_string("exit");
85 menu_add_entry(m, menu_entry_new("exit", a));
86 s = menu_new("subsex menu", "submenu", m);
87 a = action_from_string("execute");
88 a->data.execute.path = g_strdup("xclock");
89 menu_add_entry(s, menu_entry_new("xclock", a));
91 menu_add_entry(m, menu_entry_new_submenu("subz", s));
94 t = (Menu *)plugin_create("timed_menu");
95 a = action_from_string("execute");
96 a->data.execute.path = g_strdup("xeyes");
97 menu_add_entry(t, menu_entry_new("xeyes", a));
98 menu_add_entry(m, menu_entry_new_submenu("timed", t));
101 s = menu_new("empty", "chub", m);
102 menu_add_entry(m, menu_entry_new_submenu("empty", s));
104 s = menu_new("", "s-club", m);
105 menu_add_entry(m, menu_entry_new_submenu("empty", s));
107 s = menu_new(NULL, "h-club", m);
108 menu_add_entry(m, menu_entry_new_submenu("empty", s));
110 s = menu_new(NULL, "g-club", m);
112 a = action_from_string("execute");
113 a->data.execute.path = g_strdup("xterm");
114 menu_add_entry(s, menu_entry_new("xterm", a));
115 a = action_from_string("restart");
116 menu_add_entry(s, menu_entry_new("restart", a));
117 menu_add_entry(s, menu_entry_new_separator("--"));
118 a = action_from_string("exit");
119 menu_add_entry(s, menu_entry_new("exit", a));
121 menu_add_entry(m, menu_entry_new_submenu("long", s));
123 m = menu_new(NULL, "client", NULL);
124 a = action_from_string("iconify");
125 menu_add_entry(m, menu_entry_new("iconify", a));
126 a = action_from_string("toggleshade");
127 menu_add_entry(m, menu_entry_new("(un)shade", a));
128 a = action_from_string("togglemaximizefull");
129 menu_add_entry(m, menu_entry_new("(un)maximize", a));
130 a = action_from_string("close");
131 menu_add_entry(m, menu_entry_new("close", a));
137 g_hash_table_destroy(menu_hash);
138 g_hash_table_destroy(menu_map);
141 static Window createWindow(Window parent, unsigned long mask,
142 XSetWindowAttributes *attrib)
144 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
145 render_depth, InputOutput, render_visual,
150 Menu *menu_new_full(char *label, char *name, Menu *parent,
151 menu_controller_show show, menu_controller_update update)
153 XSetWindowAttributes attrib;
156 self = g_new0(Menu, 1);
157 self->label = g_strdup(label);
158 self->name = g_strdup(name);
159 self->parent = parent;
160 self->open_submenu = NULL;
162 self->entries = NULL;
164 self->invalid = TRUE;
166 /* default controllers */
169 self->update = update;
170 self->mouseover = NULL;
171 self->selected = NULL;
174 self->plugin_data = NULL;
176 attrib.override_redirect = TRUE;
177 attrib.event_mask = FRAME_EVENTMASK;
178 self->frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask, &attrib);
179 attrib.event_mask = TITLE_EVENTMASK;
180 self->title = createWindow(self->frame, CWEventMask, &attrib);
181 self->items = createWindow(self->frame, 0, &attrib);
183 XSetWindowBorderWidth(ob_display, self->frame, theme_bwidth);
184 XSetWindowBackground(ob_display, self->frame, theme_b_color->pixel);
185 XSetWindowBorderWidth(ob_display, self->title, theme_bwidth);
186 XSetWindowBorder(ob_display, self->frame, theme_b_color->pixel);
187 XSetWindowBorder(ob_display, self->title, theme_b_color->pixel);
189 XMapWindow(ob_display, self->title);
190 XMapWindow(ob_display, self->items);
192 self->a_title = appearance_copy(theme_a_menu_title);
193 self->a_items = appearance_copy(theme_a_menu);
195 g_hash_table_insert(menu_map, &self->frame, self);
196 g_hash_table_insert(menu_map, &self->title, self);
197 g_hash_table_insert(menu_map, &self->items, self);
198 g_hash_table_insert(menu_hash, g_strdup(name), self);
202 void menu_free(char *name)
204 g_hash_table_remove(menu_hash, name);
207 MenuEntry *menu_entry_new_full(char *label, Action *action,
208 MenuEntryRenderType render_type,
211 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
213 menu_entry->label = g_strdup(label);
214 menu_entry->render_type = render_type;
215 menu_entry->action = action;
217 menu_entry->hilite = FALSE;
218 menu_entry->enabled = TRUE;
220 menu_entry->submenu = submenu;
225 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
227 g_assert(entry != NULL);
229 entry->submenu = submenu;
231 if(entry->parent != NULL)
232 entry->parent->invalid = TRUE;
235 void menu_add_entry(Menu *menu, MenuEntry *entry)
237 XSetWindowAttributes attrib;
239 g_assert(menu != NULL);
240 g_assert(entry != NULL);
241 g_assert(entry->item == None);
243 menu->entries = g_list_append(menu->entries, entry);
244 entry->parent = menu;
246 attrib.event_mask = ENTRY_EVENTMASK;
247 entry->item = createWindow(menu->items, CWEventMask, &attrib);
248 XMapWindow(ob_display, entry->item);
249 entry->a_item = appearance_copy(theme_a_menu_item);
250 entry->a_disabled = appearance_copy(theme_a_menu_disabled);
251 entry->a_hilite = appearance_copy(theme_a_menu_hilite);
253 menu->invalid = TRUE;
255 g_hash_table_insert(menu_map, &entry->item, menu);
258 void menu_show(char *name, int x, int y, Client *client)
262 self = g_hash_table_lookup(menu_hash, name);
264 g_warning("Attempted to show menu '%s' but it does not exist.",
269 menu_show_full(self, x, y, client);
272 void menu_show_full(Menu *self, int x, int y, Client *client)
274 g_assert(self != NULL);
278 self->client = client;
281 self->show(self, x, y, client);
283 menu_control_show(self, x, y, client);
288 void menu_hide(Menu *self) {
290 XUnmapWindow(ob_display, self->frame);
292 if (self->open_submenu)
293 menu_hide(self->open_submenu);
294 if (self->parent && self->parent->open_submenu == self)
295 self->parent->open_submenu = NULL;
300 void menu_clear(Menu *self) {
303 for (it = self->entries; it; it = it->next) {
304 MenuEntry *entry = it->data;
305 menu_entry_free(entry);
307 self->entries = NULL;
311 MenuEntry *menu_find_entry(Menu *menu, Window win)
315 for (it = menu->entries; it; it = it->next) {
316 MenuEntry *entry = it->data;
317 if (entry->item == win)
323 void menu_entry_fire(MenuEntry *self)
328 self->action->data.any.c = self->parent->client;
329 self->action->func(&self->action->data);
331 /* hide the whole thing */
333 while (m->parent) m = m->parent;
339 Default menu controller action for showing.
342 void menu_control_show(Menu *self, int x, int y, Client *client) {
343 g_assert(!self->invalid);
345 XMoveWindow(ob_display, self->frame,
346 MIN(x, screen_physical_size.width - self->size.width),
347 MIN(y, screen_physical_size.height - self->size.height));
348 POINT_SET(self->location,
349 MIN(x, screen_physical_size.width - self->size.width),
350 MIN(y, screen_physical_size.height - self->size.height));
353 stacking_raise_internal(self->frame);
354 XMapWindow(ob_display, self->frame);
356 } else if (self->shown && self->open_submenu) {
357 menu_hide(self->open_submenu);
361 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
363 self->hilite = enter;
366 if (self->parent->open_submenu && self->submenu
367 != self->parent->open_submenu)
368 menu_hide(self->parent->open_submenu);
371 self->parent->open_submenu = self->submenu;
373 /* shouldn't be invalid since it must be displayed */
374 g_assert(!self->parent->invalid);
375 /* TODO: I don't understand why these bevels should be here.
376 Something must be wrong in the width calculation */
377 x = self->parent->location.x + self->parent->size.width +
380 /* need to get the width. is this bad?*/
381 menu_render(self->submenu);
383 if (self->submenu->size.width + x > screen_physical_size.width)
384 x = self->parent->location.x - self->submenu->size.width -
387 menu_show_full(self->submenu, x,
388 self->parent->location.y + self->y, NULL);