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