9 GHashTable *menu_hash = NULL;
11 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
12 LeaveWindowMask | ExposureMask)
13 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask | ExposureMask)
14 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
15 ButtonPressMask | ButtonReleaseMask | ExposureMask)
17 void menu_control_show(Menu *self, int x, int y, Client *client);
19 void menu_destroy_hash_key(Menu *menu)
24 void menu_destroy_hash_value(Menu *self)
28 for (it = self->entries; it; it = it->next)
29 menu_entry_free(it->data);
30 g_list_free(self->entries);
35 g_hash_table_remove(window_map, &self->w_title);
36 g_hash_table_remove(window_map, &self->w_frame);
37 g_hash_table_remove(window_map, &self->w_items);
39 stacking_remove(self);
41 RrSurfaceFree(self->s_title);
42 RrSurfaceFree(self->s_items);
43 RrSurfaceFree(self->s_frame);
45 XDestroyWindow(ob_display, self->w_frame);
50 void menu_entry_free(MenuEntry *self)
53 action_free(self->action);
55 g_hash_table_remove(window_map, &self->w_item);
56 g_hash_table_remove(window_map, &self->w_disabled);
57 g_hash_table_remove(window_map, &self->w_hilite);
59 RrSurfaceFree(self->s_item);
60 RrSurfaceFree(self->s_disabled);
61 RrSurfaceFree(self->s_hilite);
75 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
76 (GDestroyNotify)menu_destroy_hash_key,
77 (GDestroyNotify)menu_destroy_hash_value);
79 m = menu_new("sex menu", "root", NULL);
81 a = action_from_string("execute");
82 a->data.execute.path = g_strdup("xterm");
83 menu_add_entry(m, menu_entry_new("xterm", a));
84 a = action_from_string("restart");
85 menu_add_entry(m, menu_entry_new("restart", a));
86 menu_add_entry(m, menu_entry_new_separator("--"));
87 a = action_from_string("exit");
88 menu_add_entry(m, menu_entry_new("exit", a));
91 s = menu_new("subsex menu", "submenu", m);
92 a = action_from_string("execute");
93 a->data.execute.path = g_strdup("xclock");
94 menu_add_entry(s, menu_entry_new("xclock", a));
96 menu_add_entry(m, menu_entry_new_submenu("subz", s));
98 s = menu_new("empty", "chub", m);
99 menu_add_entry(m, menu_entry_new_submenu("empty", s));
101 s = menu_new("", "s-club", m);
102 menu_add_entry(m, menu_entry_new_submenu("empty", s));
104 s = menu_new(NULL, "h-club", m);
105 menu_add_entry(m, menu_entry_new_submenu("empty", s));
107 s = menu_new(NULL, "g-club", m);
109 a = action_from_string("execute");
110 a->data.execute.path = g_strdup("xterm");
111 menu_add_entry(s, menu_entry_new("xterm", a));
112 a = action_from_string("restart");
113 menu_add_entry(s, menu_entry_new("restart", a));
114 menu_add_entry(s, menu_entry_new_separator("--"));
115 a = action_from_string("exit");
116 menu_add_entry(s, menu_entry_new("exit", a));
118 menu_add_entry(m, menu_entry_new_submenu("long", s));
124 g_hash_table_destroy(menu_hash);
127 static Window createWindow(Window parent, unsigned long mask,
128 XSetWindowAttributes *attrib)
130 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
131 RrInstanceDepth(ob_render_inst), InputOutput,
132 RrInstanceVisual(ob_render_inst),
137 Menu *menu_new_full(char *label, char *name, Menu *parent,
138 menu_controller_show show, menu_controller_update update)
140 XSetWindowAttributes attrib;
143 self = g_new0(Menu, 1);
144 self->obwin.type = Window_Menu;
145 self->label = g_strdup(label);
146 self->name = g_strdup(name);
147 self->parent = parent;
148 self->open_submenu = NULL;
150 self->entries = NULL;
152 self->invalid = TRUE;
154 /* default controllers */
157 self->update = update;
158 self->mouseover = NULL;
159 self->selected = NULL;
162 self->plugin_data = NULL;
164 attrib.override_redirect = TRUE;
165 attrib.event_mask = FRAME_EVENTMASK;
166 self->w_frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask,
168 self->s_frame = RrSurfaceNew(ob_render_inst, RR_SURFACE_PLANAR,
171 /* XXX COPY THESE FROM THE THEME@&#*(&!# */
172 self->s_title = RrSurfaceNewChild(RR_SURFACE_PLANAR, self->s_frame, 1);
173 self->w_title = RrSurfaceWindow(self->s_title);
174 self->s_items = RrSurfaceNewChild(RR_SURFACE_PLANAR, self->s_frame, 0);
175 self->w_items = RrSurfaceWindow(self->s_items);
176 /* XXX LIKE THIS SORTA...
177 self->a_title = appearance_copy(theme_a_menu_title);
178 self->a_items = appearance_copy(theme_a_menu);
182 XSelectInput(ob_display, self->w_title, TITLE_EVENTMASK);
184 g_hash_table_insert(window_map, &self->w_frame, self);
185 g_hash_table_insert(window_map, &self->w_title, self);
186 g_hash_table_insert(window_map, &self->w_items, self);
187 g_hash_table_insert(menu_hash, g_strdup(name), self);
189 stacking_add(MENU_AS_WINDOW(self));
190 stacking_raise(MENU_AS_WINDOW(self));
195 void menu_free(char *name)
197 g_hash_table_remove(menu_hash, name);
200 MenuEntry *menu_entry_new_full(char *label, Action *action,
201 MenuEntryRenderType render_type,
204 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
206 menu_entry->label = g_strdup(label);
207 menu_entry->render_type = render_type;
208 menu_entry->action = action;
210 menu_entry->hilite = FALSE;
211 menu_entry->enabled = TRUE;
213 menu_entry->submenu = submenu;
218 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
220 g_assert(entry != NULL);
222 entry->submenu = submenu;
224 if(entry->parent != NULL)
225 entry->parent->invalid = TRUE;
228 void menu_add_entry(Menu *menu, MenuEntry *entry)
230 XSetWindowAttributes attrib;
232 g_assert(menu != NULL);
233 g_assert(entry != NULL);
234 g_assert(entry->s_item == NULL);
236 menu->entries = g_list_append(menu->entries, entry);
237 entry->parent = menu;
239 attrib.event_mask = ENTRY_EVENTMASK;
240 /* XXX COPY FROM THEME SURFACES @#&!#&*!@ */
241 entry->s_item = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items, 1);
242 entry->w_item = RrSurfaceWindow(entry->s_item);
243 entry->s_disabled = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items,1);
244 entry->w_disabled = RrSurfaceWindow(entry->s_disabled);
245 entry->s_hilite = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items, 1);
246 entry->w_hilite = RrSurfaceWindow(entry->s_hilite);
247 /* XXX LIKE THIS SORTA....
248 entry->a_item = appearance_copy(theme_a_menu_item);
249 entry->a_disabled = appearance_copy(theme_a_menu_disabled);
250 entry->a_hilite = appearance_copy(theme_a_menu_hilite);
254 XSelectInput(ob_display, entry->w_item, ENTRY_EVENTMASK);
255 XSelectInput(ob_display, entry->w_disabled, ENTRY_EVENTMASK);
256 XSelectInput(ob_display, entry->w_hilite, ENTRY_EVENTMASK);
258 menu->invalid = TRUE;
260 g_hash_table_insert(window_map, &entry->w_item, menu);
261 g_hash_table_insert(window_map, &entry->w_disabled, menu);
262 g_hash_table_insert(window_map, &entry->w_hilite, menu);
265 void menu_show(char *name, int x, int y, Client *client)
269 self = g_hash_table_lookup(menu_hash, name);
271 g_warning("Attempted to show menu '%s' but it does not exist.",
276 menu_show_full(self, x, y, client);
279 void menu_show_full(Menu *self, int x, int y, Client *client)
281 g_assert(self != NULL);
285 self->client = client;
288 self->show(self, x, y, client);
290 menu_control_show(self, x, y, client);
294 void menu_hide(Menu *self) {
296 RrSurfaceHide(self->s_frame);
298 if (self->open_submenu)
299 menu_hide(self->open_submenu);
300 if (self->parent && self->parent->open_submenu == self)
301 self->parent->open_submenu = NULL;
306 void menu_clear(Menu *self) {
309 for (it = self->entries; it; it = it->next) {
310 MenuEntry *entry = it->data;
311 menu_entry_free(entry);
313 self->entries = NULL;
314 self->invalid = TRUE;
318 MenuEntry *menu_find_entry(Menu *menu, Window win)
322 for (it = menu->entries; it; it = it->next) {
323 MenuEntry *entry = it->data;
324 if (entry->w_item == win ||
325 entry->w_disabled == win ||
326 entry->w_hilite == win)
332 void menu_entry_fire(MenuEntry *self)
337 self->action->data.any.c = self->parent->client;
338 self->action->func(&self->action->data);
340 /* hide the whole thing */
342 while (m->parent) m = m->parent;
348 Default menu controller action for showing.
351 void menu_control_show(Menu *self, int x, int y, Client *client) {
352 g_assert(!self->invalid);
354 POINT_SET(self->location, x, y);
358 RrSurfaceShow(self->s_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;
384 /* need to get the width. is this bad?*/
385 menu_render(self->submenu);
387 if (self->submenu->size.width + x > screen_physical_size.width)
388 x = self->parent->location.x - self->submenu->size.width;
390 menu_show_full(self->submenu, x,
391 self->parent->location.y + self->y,
392 self->parent->client);