]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
lwindow needs a type
[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
187 self->entries = NULL;
188 self->shown = FALSE;
189 self->invalid = TRUE;
190
191 /* default controllers */
192 self->show = (show != NULL ? show : menu_show_full);
193 self->hide = (hide != NULL ? hide : menu_hide);
194 self->update = (update != NULL ? update : menu_render);
195 self->mouseover = (mouseover != NULL ? mouseover :
196 menu_control_mouseover);
197 self->selected = (selected != NULL ? selected : menu_entry_fire);
198
199 self->plugin = NULL;
200 self->plugin_data = NULL;
201
202 attrib.override_redirect = TRUE;
203 attrib.event_mask = FRAME_EVENTMASK;
204 self->frame = createWindow(RootWindow(ob_display, ob_screen),
205 CWOverrideRedirect|CWEventMask, &attrib);
206 attrib.event_mask = TITLE_EVENTMASK;
207 self->title = createWindow(self->frame, CWEventMask, &attrib);
208 self->items = createWindow(self->frame, 0, &attrib);
209
210 self->a_title = self->a_items = NULL;
211
212 XMapWindow(ob_display, self->title);
213 XMapWindow(ob_display, self->items);
214
215 g_hash_table_insert(window_map, &self->frame, self);
216 g_hash_table_insert(window_map, &self->title, self);
217 g_hash_table_insert(window_map, &self->items, self);
218 g_hash_table_insert(menu_hash, g_strdup(name), self);
219
220 stacking_add(MENU_AS_WINDOW(self));
221 stacking_raise(MENU_AS_WINDOW(self));
222
223 return self;
224 }
225
226 void menu_free(char *name)
227 {
228 g_hash_table_remove(menu_hash, name);
229 }
230
231 ObMenuEntry *menu_entry_new_full(char *label, Action *action,
232 ObMenuEntryRenderType render_type,
233 gpointer submenu)
234 {
235 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
236
237 menu_entry->label = g_strdup(label);
238 menu_entry->render_type = render_type;
239 menu_entry->action = action;
240
241 menu_entry->hilite = FALSE;
242 menu_entry->enabled = TRUE;
243
244 menu_entry->submenu = submenu;
245
246 return menu_entry;
247 }
248
249 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
250 {
251 g_assert(entry != NULL);
252
253 entry->submenu = submenu;
254
255 if(entry->parent != NULL)
256 entry->parent->invalid = TRUE;
257 }
258
259 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
260 {
261 XSetWindowAttributes attrib;
262
263 g_assert(menu != NULL);
264 g_assert(entry != NULL);
265 g_assert(entry->item == None);
266
267 menu->entries = g_list_append(menu->entries, entry);
268 entry->parent = menu;
269
270 attrib.event_mask = ENTRY_EVENTMASK;
271 entry->item = createWindow(menu->items, CWEventMask, &attrib);
272 XMapWindow(ob_display, entry->item);
273
274 entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
275
276 menu->invalid = TRUE;
277
278 g_hash_table_insert(window_map, &entry->item, menu);
279 }
280
281 void menu_show(char *name, int x, int y, ObClient *client)
282 {
283 ObMenu *self;
284
285 self = g_hash_table_lookup(menu_hash, name);
286 if (!self) {
287 g_warning("Attempted to show menu '%s' but it does not exist.",
288 name);
289 return;
290 }
291
292 menu_show_full(self, x, y, client);
293 }
294
295 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
296 {
297 g_assert(self != NULL);
298
299 menu_render(self);
300
301 self->client = client;
302
303 if (!self->shown) {
304 if (!(self->parent && self->parent->shown)) {
305 grab_pointer(TRUE, None);
306 grab_keyboard(TRUE);
307 }
308 menu_visible = g_list_append(menu_visible, self);
309 }
310
311 menu_control_show(self, x, y, client);
312 }
313
314 void menu_hide(ObMenu *self) {
315 if (self->shown) {
316 XUnmapWindow(ob_display, self->frame);
317 self->shown = FALSE;
318 if (self->open_submenu)
319 menu_hide(self->open_submenu);
320 if (self->parent && self->parent->open_submenu == self) {
321 ObMenuEntry *e;
322
323 self->parent->open_submenu = NULL;
324
325 e = menu_find_entry_by_submenu(self->parent, self);
326 self->parent->mouseover(e, FALSE);
327 }
328
329 if (!(self->parent && self->parent->shown)) {
330 grab_keyboard(FALSE);
331 grab_pointer(FALSE, None);
332 }
333 menu_visible = g_list_remove(menu_visible, self);
334 }
335 }
336
337 void menu_clear(ObMenu *self) {
338 GList *it;
339
340 for (it = self->entries; it; it = it->next) {
341 ObMenuEntry *entry = it->data;
342 menu_entry_free(entry);
343 }
344 self->entries = NULL;
345 self->invalid = TRUE;
346 }
347
348
349 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
350 {
351 GList *it;
352
353 for (it = menu->entries; it; it = it->next) {
354 ObMenuEntry *entry = it->data;
355 if (entry->item == win)
356 return entry;
357 }
358 return NULL;
359 }
360
361 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
362 {
363 GList *it;
364
365 for (it = menu->entries; it; it = it->next) {
366 ObMenuEntry *entry = it->data;
367 if (entry->submenu == submenu)
368 return entry;
369 }
370 return NULL;
371 }
372
373 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
374 {
375 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
376 return NULL;
377
378 y -= menu->title_h + ob_rr_theme->bwidth;
379 if (y < 0) return NULL;
380
381 ob_debug("%d %p\n", y/menu->item_h,
382 g_list_nth_data(menu->entries, y / menu->item_h));
383 return g_list_nth_data(menu->entries, y / menu->item_h);
384 }
385
386 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
387 unsigned int y)
388 {
389 ObMenu *m;
390
391 if (button > 3) return;
392
393 if (self->action) {
394 self->action->data.any.c = self->parent->client;
395 self->action->func(&self->action->data);
396
397 /* hide the whole thing */
398 m = self->parent;
399 while (m->parent) m = m->parent;
400 menu_hide(m);
401 }
402 }
403
404 /*
405 Default menu controller action for showing.
406 */
407
408 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
409 {
410 guint i;
411 Rect *a = NULL;
412
413 g_assert(!self->invalid);
414
415 for (i = 0; i < screen_num_monitors; ++i) {
416 a = screen_physical_area_monitor(i);
417 if (RECT_CONTAINS(*a, x, y))
418 break;
419 }
420 g_assert(a != NULL);
421 self->xin_area = i;
422
423 POINT_SET(self->location,
424 MIN(x, a->x + a->width - 1 - self->size.width),
425 MIN(y, a->y + a->height - 1 - self->size.height));
426 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
427
428 if (!self->shown) {
429 XMapWindow(ob_display, self->frame);
430 stacking_raise(MENU_AS_WINDOW(self));
431 self->shown = TRUE;
432 } else if (self->shown && self->open_submenu) {
433 menu_hide(self->open_submenu);
434 }
435 }
436
437 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
438 {
439 int x;
440 Rect *a;
441 ObMenuEntry *e;
442
443 if (enter) {
444 if (self->parent->open_submenu && self->submenu
445 != self->parent->open_submenu)
446 {
447 e = menu_find_entry_by_submenu(self->parent,
448 self->parent->open_submenu);
449 e->hilite = FALSE;
450 menu_entry_render(e);
451 menu_hide(self->parent->open_submenu);
452 }
453
454 if (self->submenu && self->parent->open_submenu != self->submenu) {
455 self->parent->open_submenu = self->submenu;
456
457 /* shouldn't be invalid since it must be displayed */
458 g_assert(!self->parent->invalid);
459 /* TODO: I don't understand why these bevels should be here.
460 Something must be wrong in the width calculation */
461 x = self->parent->location.x + self->parent->size.width +
462 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
463
464 /* need to get the width. is this bad?*/
465 menu_render(self->submenu);
466
467 a = screen_physical_area_monitor(self->parent->xin_area);
468
469 if (self->submenu->size.width + x >= a->x + a->width)
470 x = self->parent->location.x - self->submenu->size.width -
471 ob_rr_theme->bwidth + ob_rr_theme->menu_overlap;
472
473 menu_show_full(self->submenu, x,
474 self->parent->location.y + self->y,
475 self->parent->client);
476 }
477 }
478
479 if (enter || !self->submenu ||
480 menu_find_entry_by_submenu(self->parent,
481 self->parent->open_submenu) != self)
482 self->hilite = enter;
483
484 menu_entry_render(self);
485 }
486
487 ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key)
488 {
489 GList *it = NULL;
490
491 switch (key) {
492 case OB_KEY_DOWN: {
493 if (over != NULL) {
494 over->parent->mouseover(over, FALSE);
495
496 it = over->parent->entries;
497 while (it != NULL && it->data != over)
498 it = it->next;
499 }
500
501 if (it && it->next)
502 over = (ObMenuEntry *)it->next->data;
503 else if (over == NULL) {
504 if (menu_visible && ((ObMenu *)menu_visible->data)->entries)
505 over = (ObMenuEntry *)
506 (((ObMenu *)menu_visible->data)->entries)->data;
507 else
508 over = NULL;
509 } else {
510 over = (over->parent->entries != NULL ?
511 over->parent->entries->data : NULL);
512 }
513
514 if (over)
515 over->parent->mouseover(over, TRUE);
516
517 break;
518 }
519 case OB_KEY_UP: {
520 if (over != NULL) {
521 over->parent->mouseover(over, FALSE);
522
523 it = g_list_last(over->parent->entries);
524 while (it != NULL && it->data != over)
525 it = it->prev;
526 }
527
528 if (it && it->prev)
529 over = (ObMenuEntry *)it->prev->data;
530 else if (over == NULL) {
531 it = g_list_last(menu_visible);
532 if (it != NULL) {
533 it = g_list_last(((ObMenu *)it->data)->entries);
534 over = (ObMenuEntry *)(it != NULL ? it->data : NULL);
535 }
536 } else
537 over = (over->parent->entries != NULL ?
538 g_list_last(over->parent->entries)->data :
539 NULL);
540
541 over->parent->mouseover(over, TRUE);
542 break;
543 }
544 case OB_KEY_RIGHT: {
545 if (over == NULL)
546 return over;
547
548 if (over->submenu) {
549 over->parent->mouseover(over, FALSE);
550
551 if (over->submenu->entries)
552 over = over->submenu->entries->data;
553
554 over->parent->mouseover(over, TRUE);
555 }
556 else {
557 over->parent->mouseover(over, FALSE);
558
559 /* zero is enter */
560 menu_entry_fire(over, 0, 0, 0);
561 }
562 break;
563 }
564 case OB_KEY_LEFT: {
565 if (over != NULL) {
566 over->parent->mouseover(over, FALSE);
567 menu_hide(over->parent);
568 } else {
569 it = g_list_last(menu_visible);
570 if (it) {
571 menu_hide((ObMenu *)it->data);
572 }
573 }
574
575 over = NULL;
576 break;
577 }
578 default:
579 g_error("Unknown key");
580 }
581
582 return over;
583 }
584
585 void menu_noop()
586 {
587 /* This noop brought to you by OLS 2003 Email Garden. */
588 }
This page took 0.058556 seconds and 4 git commands to generate.