]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
Menu stuff.
[chaz/openbox] / openbox / menu.c
1 #include "debug.h"
2 #include "menu.h"
3 #include "openbox.h"
4 #include "stacking.h"
5 #include "client.h"
6 #include "grab.h"
7 #include "screen.h"
8 #include "geom.h"
9 #include "plugin.h"
10 #include "misc.h"
11
12 GHashTable *menu_hash = NULL;
13 GList *menu_visible = NULL;
14
15 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
16 LeaveWindowMask)
17 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
18 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
19 ButtonPressMask | ButtonReleaseMask)
20
21 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
22 {
23 parse_menu_full(doc, node, data, TRUE);
24 }
25
26
27 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
28 gboolean newmenu)
29 {
30 Action *act;
31 xmlNodePtr nact;
32
33 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
34 ObMenu *menu = NULL, *parent;
35
36 if (newmenu == TRUE) {
37 if (!parse_attr_string("id", node, &id))
38 goto parse_menu_fail;
39 if (!parse_attr_string("label", node, &title))
40 goto parse_menu_fail;
41 ob_debug("menu label %s\n", title);
42
43 if (parse_attr_string("plugin", node, &plugin)) {
44 PluginMenuCreateData data;
45 data.doc = doc;
46 data.node = node;
47 data.parent = menu;
48 parent = plugin_create(plugin, &data);
49 g_free(plugin);
50 } else
51 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
52
53 if (data)
54 *((ObMenu**)data) = menu;
55 } else {
56 menu = (ObMenu *)data;
57 }
58
59 node = node->xmlChildrenNode;
60
61 while (node) {
62 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
63 if (parse_attr_string("plugin", node, &plugin)) {
64 PluginMenuCreateData data;
65 data.doc = doc;
66 data.node = node;
67 data.parent = menu;
68 parent = plugin_create(plugin, &data);
69 g_free(plugin);
70 } else {
71 parent = menu;
72 parse_menu(doc, node, &parent);
73 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
74 parent));
75 }
76
77 }
78 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
79 if (parse_attr_string("label", node, &label)) {
80 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
81 act = action_parse(doc, nact);
82 else
83 act = NULL;
84 if (act)
85 menu_add_entry(menu, menu_entry_new(label, act));
86 else
87 menu_add_entry(menu, menu_entry_new_separator(label));
88 g_free(label);
89 }
90 }
91 node = node->next;
92 }
93
94 parse_menu_fail:
95 g_free(id);
96 g_free(title);
97 }
98
99 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
100
101 void menu_destroy_hash_key(ObMenu *menu)
102 {
103 g_free(menu);
104 }
105
106 void menu_destroy_hash_value(ObMenu *self)
107 {
108 GList *it;
109
110 for (it = self->entries; it; it = it->next)
111 menu_entry_free(it->data);
112 g_list_free(self->entries);
113
114 g_free(self->label);
115 g_free(self->name);
116
117 g_hash_table_remove(window_map, &self->title);
118 g_hash_table_remove(window_map, &self->frame);
119 g_hash_table_remove(window_map, &self->items);
120
121 stacking_remove(self);
122
123 RrAppearanceFree(self->a_title);
124 RrAppearanceFree(self->a_items);
125 XDestroyWindow(ob_display, self->title);
126 XDestroyWindow(ob_display, self->frame);
127 XDestroyWindow(ob_display, self->items);
128
129 g_free(self);
130 }
131
132 void menu_entry_free(ObMenuEntry *self)
133 {
134 g_free(self->label);
135 action_free(self->action);
136
137 g_hash_table_remove(window_map, &self->item);
138
139 RrAppearanceFree(self->a_item);
140 RrAppearanceFree(self->a_disabled);
141 RrAppearanceFree(self->a_hilite);
142 XDestroyWindow(ob_display, self->item);
143
144 g_free(self);
145 }
146
147 void menu_startup()
148 {
149 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
150 (GDestroyNotify)menu_destroy_hash_key,
151 (GDestroyNotify)menu_destroy_hash_value);
152
153 parse_register("menu", parse_menu, NULL);
154
155 }
156
157 void menu_shutdown()
158 {
159 g_hash_table_destroy(menu_hash);
160 }
161
162 static Window createWindow(Window parent, unsigned long mask,
163 XSetWindowAttributes *attrib)
164 {
165 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
166 RrDepth(ob_rr_inst), InputOutput,
167 RrVisual(ob_rr_inst), mask, attrib);
168
169 }
170
171 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
172 menu_controller_show show, menu_controller_update update,
173 menu_controller_selected selected,
174 menu_controller_hide hide,
175 menu_controller_mouseover mouseover)
176 {
177 XSetWindowAttributes attrib;
178 ObMenu *self;
179
180 self = g_new0(ObMenu, 1);
181 self->obwin.type = Window_Menu;
182 self->label = g_strdup(label);
183 self->name = g_strdup(name);
184 self->parent = parent;
185 self->open_submenu = NULL;
186 self->over = NULL;
187
188 self->entries = NULL;
189 self->shown = FALSE;
190 self->invalid = TRUE;
191
192 /* default controllers */
193 self->show = (show != NULL ? show : menu_show_full);
194 self->hide = (hide != NULL ? hide : menu_hide);
195 self->update = (update != NULL ? update : menu_render);
196 self->mouseover = (mouseover != NULL ? mouseover :
197 menu_control_mouseover);
198 self->selected = (selected != NULL ? selected : menu_entry_fire);
199
200 self->plugin = NULL;
201 self->plugin_data = NULL;
202
203 attrib.override_redirect = TRUE;
204 attrib.event_mask = FRAME_EVENTMASK;
205 self->frame = createWindow(RootWindow(ob_display, ob_screen),
206 CWOverrideRedirect|CWEventMask, &attrib);
207 attrib.event_mask = TITLE_EVENTMASK;
208 self->title = createWindow(self->frame, CWEventMask, &attrib);
209 self->items = createWindow(self->frame, 0, &attrib);
210
211 self->a_title = self->a_items = NULL;
212
213 XMapWindow(ob_display, self->title);
214 XMapWindow(ob_display, self->items);
215
216 g_hash_table_insert(window_map, &self->frame, self);
217 g_hash_table_insert(window_map, &self->title, self);
218 g_hash_table_insert(window_map, &self->items, self);
219 g_hash_table_insert(menu_hash, g_strdup(name), self);
220
221 stacking_add(MENU_AS_WINDOW(self));
222 stacking_raise(MENU_AS_WINDOW(self));
223
224 return self;
225 }
226
227 void menu_free(char *name)
228 {
229 g_hash_table_remove(menu_hash, name);
230 }
231
232 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
233 ObMenuEntryRenderType render_type,
234 gpointer submenu)
235 {
236 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
237
238 menu_entry->label = g_strdup(label);
239 menu_entry->render_type = render_type;
240 menu_entry->action = action;
241
242 menu_entry->hilite = FALSE;
243 menu_entry->enabled = TRUE;
244
245 menu_entry->submenu = submenu;
246
247 return menu_entry;
248 }
249
250 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
251 {
252 g_assert(entry != NULL);
253
254 entry->submenu = submenu;
255
256 if(entry->parent != NULL)
257 entry->parent->invalid = TRUE;
258 }
259
260 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
261 {
262 XSetWindowAttributes attrib;
263
264 g_assert(menu != NULL);
265 g_assert(entry != NULL);
266 g_assert(entry->item == None);
267
268 menu->entries = g_list_append(menu->entries, entry);
269 entry->parent = menu;
270
271 attrib.event_mask = ENTRY_EVENTMASK;
272 entry->item = createWindow(menu->items, CWEventMask, &attrib);
273 XMapWindow(ob_display, entry->item);
274
275 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
276
277 menu->invalid = TRUE;
278
279 g_hash_table_insert(window_map, &entry->item, menu);
280 }
281
282 void menu_show(char *name, int x, int y, ObClient *client)
283 {
284 ObMenu *self;
285
286 self = g_hash_table_lookup(menu_hash, name);
287 if (!self) {
288 g_warning("Attempted to show menu '%s' but it does not exist.",
289 name);
290 return;
291 }
292
293 menu_show_full(self, x, y, client);
294 }
295
296 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
297 {
298 g_assert(self != NULL);
299
300 self->update(self);
301
302 self->client = client;
303
304 if (!self->shown) {
305 if (!(self->parent && self->parent->shown)) {
306 grab_pointer(TRUE, None);
307 grab_keyboard(TRUE);
308 }
309 menu_visible = g_list_append(menu_visible, self);
310 }
311
312 menu_control_show(self, x, y, client);
313 }
314
315 void menu_hide(ObMenu *self) {
316 if (self->shown) {
317 XUnmapWindow(ob_display, self->frame);
318 self->shown = FALSE;
319 if (self->open_submenu)
320 menu_hide(self->open_submenu);
321 if (self->parent && self->parent->open_submenu == self) {
322 self->parent->open_submenu = NULL;
323 }
324
325 if (!(self->parent && self->parent->shown)) {
326 grab_keyboard(FALSE);
327 grab_pointer(FALSE, None);
328 }
329 menu_visible = g_list_remove(menu_visible, self);
330 if (self->over) {
331 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
332 menu_entry_render(self->over->data);
333 self->over = NULL;
334 }
335 }
336 }
337
338 void menu_clear(ObMenu *self) {
339 GList *it;
340
341 for (it = self->entries; it; it = it->next) {
342 ObMenuEntry *entry = it->data;
343 menu_entry_free(entry);
344 }
345 self->entries = NULL;
346 self->invalid = TRUE;
347 }
348
349
350 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
351 {
352 GList *it;
353
354 for (it = menu->entries; it; it = it->next) {
355 ObMenuEntry *entry = it->data;
356 if (entry->item == win)
357 return entry;
358 }
359 return NULL;
360 }
361
362 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
363 {
364 GList *it;
365
366 for (it = menu->entries; it; it = it->next) {
367 ObMenuEntry *entry = it->data;
368 if (entry->submenu == submenu)
369 return entry;
370 }
371 return NULL;
372 }
373
374 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
375 {
376 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
377 return NULL;
378
379 y -= menu->title_h + ob_rr_theme->bwidth;
380 if (y < 0) return NULL;
381
382 ob_debug("%d %p\n", y/menu->item_h,
383 g_list_nth_data(menu->entries, y / menu->item_h));
384 return g_list_nth_data(menu->entries, y / menu->item_h);
385 }
386
387 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
388 unsigned int y)
389 {
390 ObMenu *m;
391
392 if (button != 1) return;
393
394 if (self->action) {
395 self->action->data.any.c = self->parent->client;
396 self->action->func(&self->action->data);
397
398 /* hide the whole thing */
399 m = self->parent;
400 while (m->parent) m = m->parent;
401 menu_hide(m);
402 }
403 }
404
405 /*
406 Default menu controller action for showing.
407 */
408
409 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
410 {
411 guint i;
412 Rect *a = NULL;
413
414 g_assert(!self->invalid);
415
416 for (i = 0; i < screen_num_monitors; ++i) {
417 a = screen_physical_area_monitor(i);
418 if (RECT_CONTAINS(*a, x, y))
419 break;
420 }
421 g_assert(a != NULL);
422 self->xin_area = i;
423
424 POINT_SET(self->location,
425 MIN(x, a->x + a->width - 1 - self->size.width),
426 MIN(y, a->y + a->height - 1 - self->size.height));
427 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
428
429 if (!self->shown) {
430 XMapWindow(ob_display, self->frame);
431 stacking_raise(MENU_AS_WINDOW(self));
432 self->shown = TRUE;
433 } else if (self->shown && self->open_submenu) {
434 menu_hide(self->open_submenu);
435 }
436 }
437
438 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
439 {
440 int x;
441 Rect *a;
442 ObMenuEntry *e;
443
444 g_assert(self != NULL);
445
446 if (enter) {
447 /* TODO: we prolly don't need open_submenu */
448 if (self->parent->open_submenu && self->submenu
449 != self->parent->open_submenu)
450 {
451 e = (ObMenuEntry *) self->parent->over->data;
452 e->hilite = FALSE;
453 menu_entry_render(e);
454 menu_hide(self->parent->open_submenu);
455 }
456
457 if (self->submenu && self->parent->open_submenu != self->submenu) {
458 self->parent->open_submenu = self->submenu;
459
460 /* shouldn't be invalid since it must be displayed */
461 g_assert(!self->parent->invalid);
462 /* TODO: I don't understand why these bevels should be here.
463 Something must be wrong in the width calculation */
464 x = self->parent->location.x + self->parent->size.width +
465 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
466
467 /* need to get the width. is this bad?*/
468 self->parent->update(self->submenu);
469
470 a = screen_physical_area_monitor(self->parent->xin_area);
471
472 if (self->submenu->size.width + x >= a->x + a->width)
473 x = self->parent->location.x - self->submenu->size.width -
474 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
475
476 menu_show_full(self->submenu, x,
477 self->parent->location.y + self->y,
478 self->parent->client);
479 }
480 self->hilite = TRUE;
481 self->parent->over = g_list_find(self->parent->entries, self);
482
483 } else
484 self->hilite = FALSE;
485
486 menu_entry_render(self);
487 }
488
489 void menu_control_keyboard_nav(unsigned int key)
490 {
491 static ObMenu *current_menu = NULL;
492 ObMenuEntry *e = NULL;
493
494 ObKey obkey = OB_NUM_KEYS;
495
496 /* hrmm. could be fixed */
497 if (key == ob_keycode(OB_KEY_DOWN))
498 obkey = OB_KEY_DOWN;
499 else if (key == ob_keycode(OB_KEY_UP))
500 obkey = OB_KEY_UP;
501 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
502 obkey = OB_KEY_RIGHT;
503 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
504 obkey = OB_KEY_LEFT;
505
506 if (current_menu == NULL)
507 current_menu = menu_visible->data;
508
509 switch (obkey) {
510 case OB_KEY_DOWN: {
511 if (current_menu->over) {
512 current_menu->mouseover(current_menu->over->data, FALSE);
513 current_menu->over = (current_menu->over->next != NULL ?
514 current_menu->over->next :
515 current_menu->entries);
516 }
517 else
518 current_menu->over = current_menu->entries;
519
520 if (current_menu->over)
521 current_menu->mouseover(current_menu->over->data, TRUE);
522
523 break;
524 }
525 case OB_KEY_UP: {
526 if (current_menu->over) {
527 current_menu->mouseover(current_menu->over->data, FALSE);
528 current_menu->over = (current_menu->over->prev != NULL ?
529 current_menu->over->prev :
530 g_list_last(current_menu->entries));
531 } else
532 current_menu->over = g_list_last(current_menu->entries);
533
534 if (current_menu->over)
535 current_menu->mouseover(current_menu->over->data, TRUE);
536
537 break;
538 }
539 case OB_KEY_RIGHT: {
540 if (current_menu->over == NULL)
541 return;
542 e = (ObMenuEntry *)current_menu->over->data;
543 if (e->submenu) {
544 current_menu = e->submenu;
545 current_menu->over = current_menu->entries;
546 if (current_menu->over)
547 current_menu->mouseover(current_menu->over->data, TRUE);
548 }
549 break;
550 }
551
552 case OB_KEY_RETURN: {
553 if (current_menu->over == NULL)
554 return;
555 e = (ObMenuEntry *)current_menu->over->data;
556
557 current_menu->mouseover(e, FALSE);
558 current_menu->over = NULL;
559 /* zero is enter */
560 menu_entry_fire(e, 0, 0, 0);
561
562 }
563 case OB_KEY_LEFT: {
564 if (current_menu->over != NULL) {
565 current_menu->mouseover(current_menu->over->data, FALSE);
566 current_menu->over = NULL;
567 }
568
569 menu_hide(current_menu);
570
571 if (current_menu->parent)
572 current_menu = current_menu->parent;
573
574 break;
575 }
576 default:
577 if (current_menu)
578 menu_hide(current_menu);
579 }
580 return;
581 }
582
583 void menu_noop()
584 {
585 /* This noop brought to you by OLS 2003 Email Garden. */
586 }
This page took 0.058527 seconds and 5 git commands to generate.