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 void menu_control_show(Menu *self, int x, int y, Client *client);
20 void menu_destroy_hash_key(Menu *menu)
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 RrAppearanceFree(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 RrAppearanceFree(self->a_item);
58 RrAppearanceFree(self->a_disabled);
59 RrAppearanceFree(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("sex menu", "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));
88 s = menu_new("subsex menu", "submenu", m);
89 a = action_from_string("execute");
90 a->data.execute.path = g_strdup("xclock");
91 menu_add_entry(s, menu_entry_new("xclock", a));
93 menu_add_entry(m, menu_entry_new_submenu("subz", s));
95 s = menu_new("empty", "chub", m);
96 menu_add_entry(m, menu_entry_new_submenu("empty", s));
98 s = menu_new("", "s-club", m);
99 menu_add_entry(m, menu_entry_new_submenu("empty", s));
101 s = menu_new(NULL, "h-club", m);
102 menu_add_entry(m, menu_entry_new_submenu("empty", s));
104 s = menu_new(NULL, "g-club", m);
106 a = action_from_string("execute");
107 a->data.execute.path = g_strdup("xterm");
108 menu_add_entry(s, menu_entry_new("xterm", a));
109 a = action_from_string("restart");
110 menu_add_entry(s, menu_entry_new("restart", a));
111 menu_add_entry(s, menu_entry_new_separator("--"));
112 a = action_from_string("exit");
113 menu_add_entry(s, menu_entry_new("exit", a));
115 menu_add_entry(m, menu_entry_new_submenu("long", s));
121 g_hash_table_destroy(menu_hash);
124 static Window createWindow(Window parent, unsigned long mask,
125 XSetWindowAttributes *attrib)
127 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
128 RrDepth(ob_rr_inst), InputOutput,
129 RrVisual(ob_rr_inst), mask, attrib);
133 Menu *menu_new_full(char *label, char *name, Menu *parent,
134 menu_controller_show show, menu_controller_update update)
136 XSetWindowAttributes attrib;
139 self = g_new0(Menu, 1);
140 self->obwin.type = Window_Menu;
141 self->label = g_strdup(label);
142 self->name = g_strdup(name);
143 self->parent = parent;
144 self->open_submenu = NULL;
146 self->entries = NULL;
148 self->invalid = TRUE;
150 /* default controllers */
153 self->update = update;
154 self->mouseover = NULL;
155 self->selected = NULL;
158 self->plugin_data = NULL;
160 attrib.override_redirect = TRUE;
161 attrib.event_mask = FRAME_EVENTMASK;
162 self->frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask, &attrib);
163 attrib.event_mask = TITLE_EVENTMASK;
164 self->title = createWindow(self->frame, CWEventMask, &attrib);
165 self->items = createWindow(self->frame, 0, &attrib);
167 XSetWindowBorderWidth(ob_display, self->frame, ob_rr_theme->bwidth);
168 XSetWindowBackground(ob_display, self->frame, ob_rr_theme->b_color->pixel);
169 XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
170 XSetWindowBorder(ob_display, self->frame, ob_rr_theme->b_color->pixel);
171 XSetWindowBorder(ob_display, self->title, ob_rr_theme->b_color->pixel);
173 XMapWindow(ob_display, self->title);
174 XMapWindow(ob_display, self->items);
176 self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
177 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
179 g_hash_table_insert(window_map, &self->frame, self);
180 g_hash_table_insert(window_map, &self->title, self);
181 g_hash_table_insert(window_map, &self->items, self);
182 g_hash_table_insert(menu_hash, g_strdup(name), self);
184 stacking_add(MENU_AS_WINDOW(self));
185 stacking_raise(MENU_AS_WINDOW(self));
190 void menu_free(char *name)
192 g_hash_table_remove(menu_hash, name);
195 MenuEntry *menu_entry_new_full(char *label, Action *action,
196 MenuEntryRenderType render_type,
199 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
201 menu_entry->label = g_strdup(label);
202 menu_entry->render_type = render_type;
203 menu_entry->action = action;
205 menu_entry->hilite = FALSE;
206 menu_entry->enabled = TRUE;
208 menu_entry->submenu = submenu;
213 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
215 g_assert(entry != NULL);
217 entry->submenu = submenu;
219 if(entry->parent != NULL)
220 entry->parent->invalid = TRUE;
223 void menu_add_entry(Menu *menu, MenuEntry *entry)
225 XSetWindowAttributes attrib;
227 g_assert(menu != NULL);
228 g_assert(entry != NULL);
229 g_assert(entry->item == None);
231 menu->entries = g_list_append(menu->entries, entry);
232 entry->parent = menu;
234 attrib.event_mask = ENTRY_EVENTMASK;
235 entry->item = createWindow(menu->items, CWEventMask, &attrib);
236 XMapWindow(ob_display, entry->item);
237 entry->a_item = RrAppearanceCopy(ob_rr_theme->a_menu_item);
238 entry->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
239 entry->a_hilite = RrAppearanceCopy(ob_rr_theme->a_menu_hilite);
241 menu->invalid = TRUE;
243 g_hash_table_insert(window_map, &entry->item, menu);
246 void menu_show(char *name, int x, int y, Client *client)
250 self = g_hash_table_lookup(menu_hash, name);
252 g_warning("Attempted to show menu '%s' but it does not exist.",
257 menu_show_full(self, x, y, client);
260 void menu_show_full(Menu *self, int x, int y, Client *client)
262 g_assert(self != NULL);
266 self->client = client;
269 self->show(self, x, y, client);
271 menu_control_show(self, x, y, client);
275 void menu_hide(Menu *self) {
277 XUnmapWindow(ob_display, self->frame);
279 if (self->open_submenu)
280 menu_hide(self->open_submenu);
281 if (self->parent && self->parent->open_submenu == self)
282 self->parent->open_submenu = NULL;
287 void menu_clear(Menu *self) {
290 for (it = self->entries; it; it = it->next) {
291 MenuEntry *entry = it->data;
292 menu_entry_free(entry);
294 self->entries = NULL;
295 self->invalid = TRUE;
299 MenuEntry *menu_find_entry(Menu *menu, Window win)
303 for (it = menu->entries; it; it = it->next) {
304 MenuEntry *entry = it->data;
305 if (entry->item == win)
311 void menu_entry_fire(MenuEntry *self)
316 self->action->data.any.c = self->parent->client;
317 self->action->func(&self->action->data);
319 /* hide the whole thing */
321 while (m->parent) m = m->parent;
327 Default menu controller action for showing.
330 void menu_control_show(Menu *self, int x, int y, Client *client) {
331 g_assert(!self->invalid);
333 XMoveWindow(ob_display, self->frame,
334 MIN(x, screen_physical_size.width - self->size.width),
335 MIN(y, screen_physical_size.height - self->size.height));
336 POINT_SET(self->location,
337 MIN(x, screen_physical_size.width - self->size.width),
338 MIN(y, screen_physical_size.height - self->size.height));
341 XMapWindow(ob_display, self->frame);
342 stacking_raise(MENU_AS_WINDOW(self));
344 } else if (self->shown && self->open_submenu) {
345 menu_hide(self->open_submenu);
349 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
351 self->hilite = enter;
354 if (self->parent->open_submenu && self->submenu
355 != self->parent->open_submenu)
356 menu_hide(self->parent->open_submenu);
359 self->parent->open_submenu = self->submenu;
361 /* shouldn't be invalid since it must be displayed */
362 g_assert(!self->parent->invalid);
363 /* TODO: I don't understand why these bevels should be here.
364 Something must be wrong in the width calculation */
365 x = self->parent->location.x + self->parent->size.width +
368 /* need to get the width. is this bad?*/
369 menu_render(self->submenu);
371 if (self->submenu->size.width + x > screen_physical_size.width)
372 x = self->parent->location.x - self->submenu->size.width -
375 menu_show_full(self->submenu, x,
376 self->parent->location.y + self->y,
377 self->parent->client);