5 #include "render/theme.h"
10 static 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 void menu_control_show(Menu *self, int x, int y, Client *client);
20 void menu_destroy_hash_key(gpointer data)
25 void menu_destroy_hash_value(Menu *self)
29 for (it = self->entries; it; it = it->next)
30 menu_entry_free(it->data);
31 g_list_free(self->entries);
36 g_hash_table_remove(window_map, &self->title);
37 g_hash_table_remove(window_map, &self->frame);
38 g_hash_table_remove(window_map, &self->items);
40 stacking_remove(self);
42 appearance_free(self->a_title);
43 XDestroyWindow(ob_display, self->title);
44 XDestroyWindow(ob_display, self->frame);
45 XDestroyWindow(ob_display, self->items);
50 void menu_entry_free(MenuEntry *self)
53 action_free(self->action);
55 g_hash_table_remove(window_map, &self->item);
57 appearance_free(self->a_item);
58 appearance_free(self->a_disabled);
59 appearance_free(self->a_hilite);
60 XDestroyWindow(ob_display, self->item);
72 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
73 menu_destroy_hash_key,
74 (GDestroyNotify)menu_destroy_hash_value);
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));
93 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);
140 static Window createWindow(Window parent, unsigned long mask,
141 XSetWindowAttributes *attrib)
143 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
144 render_depth, InputOutput, render_visual,
149 Menu *menu_new_full(char *label, char *name, Menu *parent,
150 menu_controller_show show, menu_controller_update update)
152 XSetWindowAttributes attrib;
155 self = g_new0(Menu, 1);
156 self->obwin.type = Window_Menu;
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(window_map, &self->frame, self);
196 g_hash_table_insert(window_map, &self->title, self);
197 g_hash_table_insert(window_map, &self->items, self);
198 g_hash_table_insert(menu_hash, g_strdup(name), self);
200 stacking_add(MENU_AS_WINDOW(self));
201 stacking_raise(MENU_AS_WINDOW(self));
206 void menu_free(char *name)
208 g_hash_table_remove(menu_hash, name);
211 MenuEntry *menu_entry_new_full(char *label, Action *action,
212 MenuEntryRenderType render_type,
215 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
217 menu_entry->label = g_strdup(label);
218 menu_entry->render_type = render_type;
219 menu_entry->action = action;
221 menu_entry->hilite = FALSE;
222 menu_entry->enabled = TRUE;
224 menu_entry->submenu = submenu;
229 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
231 g_assert(entry != NULL);
233 entry->submenu = submenu;
235 if(entry->parent != NULL)
236 entry->parent->invalid = TRUE;
239 void menu_add_entry(Menu *menu, MenuEntry *entry)
241 XSetWindowAttributes attrib;
243 g_assert(menu != NULL);
244 g_assert(entry != NULL);
245 g_assert(entry->item == None);
247 menu->entries = g_list_append(menu->entries, entry);
248 entry->parent = menu;
250 attrib.event_mask = ENTRY_EVENTMASK;
251 entry->item = createWindow(menu->items, CWEventMask, &attrib);
252 XMapWindow(ob_display, entry->item);
253 entry->a_item = appearance_copy(theme_a_menu_item);
254 entry->a_disabled = appearance_copy(theme_a_menu_disabled);
255 entry->a_hilite = appearance_copy(theme_a_menu_hilite);
257 menu->invalid = TRUE;
259 g_hash_table_insert(window_map, &entry->item, menu);
262 void menu_show(char *name, int x, int y, Client *client)
266 self = g_hash_table_lookup(menu_hash, name);
268 g_warning("Attempted to show menu '%s' but it does not exist.",
273 menu_show_full(self, x, y, client);
276 void menu_show_full(Menu *self, int x, int y, Client *client)
278 g_assert(self != NULL);
282 self->client = client;
285 self->show(self, x, y, client);
287 menu_control_show(self, x, y, client);
292 void menu_hide(Menu *self) {
294 XUnmapWindow(ob_display, self->frame);
296 if (self->open_submenu)
297 menu_hide(self->open_submenu);
298 if (self->parent && self->parent->open_submenu == self)
299 self->parent->open_submenu = NULL;
304 void menu_clear(Menu *self) {
307 for (it = self->entries; it; it = it->next) {
308 MenuEntry *entry = it->data;
309 menu_entry_free(entry);
311 self->entries = NULL;
312 self->invalid = TRUE;
316 MenuEntry *menu_find_entry(Menu *menu, Window win)
320 for (it = menu->entries; it; it = it->next) {
321 MenuEntry *entry = it->data;
322 if (entry->item == win)
328 void menu_entry_fire(MenuEntry *self)
333 self->action->data.any.c = self->parent->client;
334 self->action->func(&self->action->data);
336 /* hide the whole thing */
338 while (m->parent) m = m->parent;
344 Default menu controller action for showing.
347 void menu_control_show(Menu *self, int x, int y, Client *client) {
348 g_assert(!self->invalid);
350 XMoveWindow(ob_display, self->frame,
351 MIN(x, screen_physical_size.width - self->size.width),
352 MIN(y, screen_physical_size.height - self->size.height));
353 POINT_SET(self->location,
354 MIN(x, screen_physical_size.width - self->size.width),
355 MIN(y, screen_physical_size.height - self->size.height));
358 XMapWindow(ob_display, self->frame);
359 stacking_raise(MENU_AS_WINDOW(self));
361 } else if (self->shown && self->open_submenu) {
362 menu_hide(self->open_submenu);
366 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
368 self->hilite = enter;
371 if (self->parent->open_submenu && self->submenu
372 != self->parent->open_submenu)
373 menu_hide(self->parent->open_submenu);
376 self->parent->open_submenu = self->submenu;
378 /* shouldn't be invalid since it must be displayed */
379 g_assert(!self->parent->invalid);
380 /* TODO: I don't understand why these bevels should be here.
381 Something must be wrong in the width calculation */
382 x = self->parent->location.x + self->parent->size.width +
385 /* need to get the width. is this bad?*/
386 menu_render(self->submenu);
388 if (self->submenu->size.width + x > screen_physical_size.width)
389 x = self->parent->location.x - self->submenu->size.width -
392 menu_show_full(self->submenu, x,
393 self->parent->location.y + self->y, NULL);