]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
kill non-opaque move/resize
[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 /* ignore wheel scrolling */
396 if (button == 4 || button == 5) return;
397
398 if (self->action) {
399 self->action->data.any.c = self->parent->client;
400 self->action->func(&self->action->data);
401
402 /* hide the whole thing */
403 m = self->parent;
404 while (m->parent) m = m->parent;
405 m->hide(m);
406 }
407 }
408
409 /*
410 Default menu controller action for showing.
411 */
412
413 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
414 {
415 guint i;
416 Rect *a = NULL;
417
418 g_assert(!self->invalid);
419
420 for (i = 0; i < screen_num_monitors; ++i) {
421 a = screen_physical_area_monitor(i);
422 if (RECT_CONTAINS(*a, x, y))
423 break;
424 }
425 g_assert(a != NULL);
426 self->xin_area = i;
427
428 POINT_SET(self->location,
429 MIN(x, a->x + a->width - 1 - self->size.width),
430 MIN(y, a->y + a->height - 1 - self->size.height));
431 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
432
433 if (!self->shown) {
434 XMapWindow(ob_display, self->frame);
435 stacking_raise(MENU_AS_WINDOW(self));
436 self->shown = TRUE;
437 } else if (self->shown && self->open_submenu) {
438 self->open_submenu->hide(self->open_submenu);
439 }
440 }
441
442 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
443 {
444 int x;
445 Rect *a;
446 ObMenuEntry *e;
447
448 g_assert(self != NULL);
449
450 if (enter) {
451 /* TODO: we prolly don't need open_submenu */
452 if (self->parent->open_submenu && self->submenu
453 != self->parent->open_submenu)
454 {
455 e = (ObMenuEntry *) self->parent->over->data;
456 e->hilite = FALSE;
457 menu_entry_render(e);
458 self->parent->open_submenu->hide(self->parent->open_submenu);
459 }
460
461 if (self->submenu && self->parent->open_submenu != self->submenu) {
462 self->parent->open_submenu = self->submenu;
463
464 /* shouldn't be invalid since it must be displayed */
465 g_assert(!self->parent->invalid);
466 /* TODO: I don't understand why these bevels should be here.
467 Something must be wrong in the width calculation */
468 x = self->parent->location.x + self->parent->size.width +
469 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
470
471 /* need to get the width. is this bad?*/
472 self->parent->update(self->submenu);
473
474 a = screen_physical_area_monitor(self->parent->xin_area);
475
476 if (self->submenu->size.width + x >= a->x + a->width)
477 x = self->parent->location.x - self->submenu->size.width -
478 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
479
480 menu_show_full(self->submenu, x,
481 self->parent->location.y + self->y,
482 self->parent->client);
483 }
484 self->hilite = TRUE;
485 self->parent->over = g_list_find(self->parent->entries, self);
486
487 } else
488 self->hilite = FALSE;
489
490 menu_entry_render(self);
491 }
492
493 void menu_control_keyboard_nav(unsigned int key)
494 {
495 static ObMenu *current_menu = NULL;
496 ObMenuEntry *e = NULL;
497
498 ObKey obkey = OB_NUM_KEYS;
499
500 /* hrmm. could be fixed */
501 if (key == ob_keycode(OB_KEY_DOWN))
502 obkey = OB_KEY_DOWN;
503 else if (key == ob_keycode(OB_KEY_UP))
504 obkey = OB_KEY_UP;
505 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
506 obkey = OB_KEY_RIGHT;
507 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
508 obkey = OB_KEY_LEFT;
509 else if (key == ob_keycode(OB_KEY_RETURN))
510 obkey = OB_KEY_RETURN;
511
512
513 if (current_menu == NULL)
514 current_menu = menu_visible->data;
515
516 switch (obkey) {
517 case OB_KEY_DOWN: {
518 if (current_menu->over) {
519 current_menu->mouseover(current_menu->over->data, FALSE);
520 current_menu->over = (current_menu->over->next != NULL ?
521 current_menu->over->next :
522 current_menu->entries);
523 }
524 else
525 current_menu->over = current_menu->entries;
526
527 if (current_menu->over)
528 current_menu->mouseover(current_menu->over->data, TRUE);
529
530 break;
531 }
532 case OB_KEY_UP: {
533 if (current_menu->over) {
534 current_menu->mouseover(current_menu->over->data, FALSE);
535 current_menu->over = (current_menu->over->prev != NULL ?
536 current_menu->over->prev :
537 g_list_last(current_menu->entries));
538 } else
539 current_menu->over = g_list_last(current_menu->entries);
540
541 if (current_menu->over)
542 current_menu->mouseover(current_menu->over->data, TRUE);
543
544 break;
545 }
546 case OB_KEY_RIGHT: {
547 if (current_menu->over == NULL)
548 return;
549 e = (ObMenuEntry *)current_menu->over->data;
550 if (e->submenu) {
551 current_menu->mouseover(e, TRUE);
552 current_menu = e->submenu;
553 current_menu->over = current_menu->entries;
554 if (current_menu->over)
555 current_menu->mouseover(current_menu->over->data, TRUE);
556 }
557 break;
558 }
559
560 case OB_KEY_RETURN: {
561 if (current_menu->over == NULL)
562 return;
563 e = (ObMenuEntry *)current_menu->over->data;
564
565 current_menu->mouseover(e, FALSE);
566 current_menu->over = NULL;
567 /* zero is enter */
568 menu_entry_fire(e, 0, 0, 0);
569 }
570
571 case OB_KEY_LEFT: {
572 if (current_menu->over != NULL) {
573 current_menu->mouseover(current_menu->over->data, FALSE);
574 current_menu->over = NULL;
575 }
576
577 current_menu->hide(current_menu);
578
579 if (current_menu->parent)
580 current_menu = current_menu->parent;
581
582 break;
583 }
584 default:
585 ((ObMenu *)menu_visible->data)->hide(menu_visible->data);
586 current_menu = NULL;
587 }
588 return;
589 }
590
591 void menu_noop()
592 {
593 /* This noop brought to you by OLS 2003 Email Garden. */
594 }
This page took 0.062383 seconds and 4 git commands to generate.