]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
hardcode the button names for themes
[chaz/openbox] / openbox / menu.c
1 #include "menu.h"
2 #include "openbox.h"
3 #include "stacking.h"
4 #include "grab.h"
5 #include "screen.h"
6 #include "geom.h"
7 #include "plugin.h"
8
9 GHashTable *menu_hash = NULL;
10 GSList *menu_visible = NULL;
11
12 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
13 LeaveWindowMask)
14 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
15 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
16 ButtonPressMask | ButtonReleaseMask)
17
18 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
19 {
20 Action *act;
21 xmlNodePtr nact;
22 gchar *id = NULL, *title = NULL, *label = NULL;
23 Menu *menu, *parent;
24
25 if (!parse_attr_string("id", node->parent, &id))
26 goto parse_menu_fail;
27 if (!parse_attr_string("label", node->parent, &title))
28 goto parse_menu_fail;
29
30 g_message("menu label %s", title);
31
32 menu = menu_new(title, id, data ? *((Menu**)data) : NULL);
33 if (data)
34 *((Menu**)data) = menu;
35
36 while (node) {
37 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
38 parent = menu;
39 parse_menu(doc, node->xmlChildrenNode, &parent);
40 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
41 parent));
42 }
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);
47 else
48 act = NULL;
49 if (act)
50 menu_add_entry(menu, menu_entry_new(label, act));
51 else
52 menu_add_entry(menu, menu_entry_new_separator(label));
53 g_free(label);
54 }
55 }
56 node = node->next;
57 }
58
59 parse_menu_fail:
60 g_free(id);
61 g_free(title);
62 }
63
64 void menu_control_show(Menu *self, int x, int y, Client *client);
65
66 void menu_destroy_hash_key(Menu *menu)
67 {
68 g_free(menu);
69 }
70
71 void menu_destroy_hash_value(Menu *self)
72 {
73 GList *it;
74
75 for (it = self->entries; it; it = it->next)
76 menu_entry_free(it->data);
77 g_list_free(self->entries);
78
79 g_free(self->label);
80 g_free(self->name);
81
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);
85
86 stacking_remove(self);
87
88 RrAppearanceFree(self->a_title);
89 XDestroyWindow(ob_display, self->title);
90 XDestroyWindow(ob_display, self->frame);
91 XDestroyWindow(ob_display, self->items);
92
93 g_free(self);
94 }
95
96 void menu_entry_free(MenuEntry *self)
97 {
98 g_free(self->label);
99 action_free(self->action);
100
101 g_hash_table_remove(window_map, &self->item);
102
103 RrAppearanceFree(self->a_item);
104 RrAppearanceFree(self->a_disabled);
105 RrAppearanceFree(self->a_hilite);
106 XDestroyWindow(ob_display, self->item);
107
108 g_free(self);
109 }
110
111 void menu_startup()
112 {
113 /*
114 Menu *m;
115 Menu *s;
116 Menu *t;
117 Action *a;
118 */
119
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);
123
124 parse_register("menu", parse_menu, NULL);
125
126 /*
127 m = menu_new("sex menu", "root", NULL);
128
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));
137 */
138
139 /*
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));
144
145 menu_add_entry(m, menu_entry_new_submenu("subz", s));
146
147 s = menu_new("empty", "chub", m);
148 menu_add_entry(m, menu_entry_new_submenu("empty", s));
149
150 s = menu_new("", "s-club", m);
151 menu_add_entry(m, menu_entry_new_submenu("empty", s));
152
153 s = menu_new(NULL, "h-club", m);
154 menu_add_entry(m, menu_entry_new_submenu("empty", s));
155
156 s = menu_new(NULL, "g-club", m);
157
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));
166
167 menu_add_entry(m, menu_entry_new_submenu("long", s));
168 */
169 }
170
171 void menu_shutdown()
172 {
173 g_hash_table_destroy(menu_hash);
174 }
175
176 static Window createWindow(Window parent, unsigned long mask,
177 XSetWindowAttributes *attrib)
178 {
179 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
180 RrDepth(ob_rr_inst), InputOutput,
181 RrVisual(ob_rr_inst), mask, attrib);
182
183 }
184
185 Menu *menu_new_full(char *label, char *name, Menu *parent,
186 menu_controller_show show, menu_controller_update update)
187 {
188 XSetWindowAttributes attrib;
189 Menu *self;
190
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;
197
198 self->entries = NULL;
199 self->shown = FALSE;
200 self->invalid = TRUE;
201
202 /* default controllers */
203 self->show = show;
204 self->hide = NULL;
205 self->update = update;
206 self->mouseover = NULL;
207 self->selected = NULL;
208
209 self->plugin = NULL;
210 self->plugin_data = NULL;
211
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);
219
220 self->a_title = self->a_items = NULL;
221
222 XMapWindow(ob_display, self->title);
223 XMapWindow(ob_display, self->items);
224
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);
229
230 stacking_add(MENU_AS_WINDOW(self));
231 stacking_raise(MENU_AS_WINDOW(self));
232
233 return self;
234 }
235
236 void menu_free(char *name)
237 {
238 g_hash_table_remove(menu_hash, name);
239 }
240
241 MenuEntry *menu_entry_new_full(char *label, Action *action,
242 MenuEntryRenderType render_type,
243 gpointer submenu)
244 {
245 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
246
247 menu_entry->label = g_strdup(label);
248 menu_entry->render_type = render_type;
249 menu_entry->action = action;
250
251 menu_entry->hilite = FALSE;
252 menu_entry->enabled = TRUE;
253
254 menu_entry->submenu = submenu;
255
256 return menu_entry;
257 }
258
259 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
260 {
261 g_assert(entry != NULL);
262
263 entry->submenu = submenu;
264
265 if(entry->parent != NULL)
266 entry->parent->invalid = TRUE;
267 }
268
269 void menu_add_entry(Menu *menu, MenuEntry *entry)
270 {
271 XSetWindowAttributes attrib;
272
273 g_assert(menu != NULL);
274 g_assert(entry != NULL);
275 g_assert(entry->item == None);
276
277 menu->entries = g_list_append(menu->entries, entry);
278 entry->parent = menu;
279
280 attrib.event_mask = ENTRY_EVENTMASK;
281 entry->item = createWindow(menu->items, CWEventMask, &attrib);
282 XMapWindow(ob_display, entry->item);
283
284 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
285
286 menu->invalid = TRUE;
287
288 g_hash_table_insert(window_map, &entry->item, menu);
289 }
290
291 void menu_show(char *name, int x, int y, Client *client)
292 {
293 Menu *self;
294
295 self = g_hash_table_lookup(menu_hash, name);
296 if (!self) {
297 g_warning("Attempted to show menu '%s' but it does not exist.",
298 name);
299 return;
300 }
301
302 menu_show_full(self, x, y, client);
303 }
304
305 void menu_show_full(Menu *self, int x, int y, Client *client)
306 {
307 g_assert(self != NULL);
308
309 menu_render(self);
310
311 self->client = client;
312
313 if (!self->shown) {
314 if (!self->parent) {
315 grab_pointer(TRUE, None);
316 grab_keyboard(TRUE);
317 }
318 menu_visible = g_slist_append(menu_visible, self);
319 }
320
321 if (self->show) {
322 self->show(self, x, y, client);
323 } else {
324 menu_control_show(self, x, y, client);
325 }
326 }
327
328 void menu_hide(Menu *self) {
329 if (self->shown) {
330 XUnmapWindow(ob_display, self->frame);
331 self->shown = FALSE;
332 if (self->open_submenu)
333 menu_hide(self->open_submenu);
334 if (self->parent && self->parent->open_submenu == self)
335 self->parent->open_submenu = NULL;
336
337 if (!self->parent) {
338 grab_keyboard(FALSE);
339 grab_pointer(FALSE, None);
340 }
341 menu_visible = g_slist_remove(menu_visible, self);
342 }
343 }
344
345 void menu_clear(Menu *self) {
346 GList *it;
347
348 for (it = self->entries; it; it = it->next) {
349 MenuEntry *entry = it->data;
350 menu_entry_free(entry);
351 }
352 self->entries = NULL;
353 self->invalid = TRUE;
354 }
355
356
357 MenuEntry *menu_find_entry(Menu *menu, Window win)
358 {
359 GList *it;
360
361 for (it = menu->entries; it; it = it->next) {
362 MenuEntry *entry = it->data;
363 if (entry->item == win)
364 return entry;
365 }
366 return NULL;
367 }
368
369 MenuEntry *menu_find_entry_by_pos(Menu *menu, int x, int y)
370 {
371 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
372 return NULL;
373
374 y -= menu->title_h + ob_rr_theme->bwidth;
375 if (y < 0) return NULL;
376
377 g_message ("%d %p", y/menu->item_h, g_list_nth_data(menu->entries, y / menu->item_h));
378 return g_list_nth_data(menu->entries, y / menu->item_h);
379 }
380
381 void menu_entry_fire(MenuEntry *self)
382 {
383 Menu *m;
384
385 if (self->action) {
386 self->action->data.any.c = self->parent->client;
387 self->action->func(&self->action->data);
388
389 /* hide the whole thing */
390 m = self->parent;
391 while (m->parent) m = m->parent;
392 menu_hide(m);
393 }
394 }
395
396 /*
397 Default menu controller action for showing.
398 */
399
400 void menu_control_show(Menu *self, int x, int y, Client *client) {
401 guint i;
402 Rect *a = NULL;
403
404 g_assert(!self->invalid);
405
406 for (i = 0; i < screen_num_xin_areas; ++i) {
407 a = screen_physical_area_xinerama(i);
408 if (RECT_CONTAINS(*a, x, y))
409 break;
410 }
411 g_assert(a != NULL);
412 self->xin_area = i;
413
414 POINT_SET(self->location,
415 MIN(x, a->x + a->width - 1 - self->size.width),
416 MIN(y, a->y + a->height - 1 - self->size.height));
417 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
418
419 if (!self->shown) {
420 XMapWindow(ob_display, self->frame);
421 stacking_raise(MENU_AS_WINDOW(self));
422 self->shown = TRUE;
423 } else if (self->shown && self->open_submenu) {
424 menu_hide(self->open_submenu);
425 }
426 }
427
428 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
429 int x;
430 Rect *a;
431
432 self->hilite = enter;
433
434 if (enter) {
435 if (self->parent->open_submenu && self->submenu
436 != self->parent->open_submenu)
437 menu_hide(self->parent->open_submenu);
438
439 if (self->submenu) {
440 self->parent->open_submenu = self->submenu;
441
442 /* shouldn't be invalid since it must be displayed */
443 g_assert(!self->parent->invalid);
444 /* TODO: I don't understand why these bevels should be here.
445 Something must be wrong in the width calculation */
446 x = self->parent->location.x + self->parent->size.width +
447 ob_rr_theme->bwidth;
448
449 /* need to get the width. is this bad?*/
450 menu_render(self->submenu);
451
452 a = screen_physical_area_xinerama(self->parent->xin_area);
453
454 if (self->submenu->size.width + x >= a->x + a->width)
455 x = self->parent->location.x - self->submenu->size.width -
456 ob_rr_theme->bwidth;
457
458 menu_show_full(self->submenu, x,
459 self->parent->location.y + self->y,
460 self->parent->client);
461 }
462 }
463 }
This page took 0.059863 seconds and 4 git commands to generate.