]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
do the menu_can_hide thing properly. was such a hack before? and the variable was...
[chaz/openbox] / openbox / menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "menu.h"
22 #include "openbox.h"
23 #include "mainloop.h"
24 #include "stacking.h"
25 #include "client.h"
26 #include "config.h"
27 #include "screen.h"
28 #include "menuframe.h"
29 #include "keyboard.h"
30 #include "geom.h"
31 #include "misc.h"
32 #include "client_menu.h"
33 #include "client_list_menu.h"
34 #include "client_list_combined_menu.h"
35 #include "gettext.h"
36 #include "parser/parse.h"
37
38 typedef struct _ObMenuParseState ObMenuParseState;
39
40 struct _ObMenuParseState
41 {
42 ObMenu *parent;
43 ObMenu *pipe_creator;
44 };
45
46 static GHashTable *menu_hash = NULL;
47 static ObParseInst *menu_parse_inst;
48 static ObMenuParseState menu_parse_state;
49 static gboolean menu_can_hide = FALSE;
50
51 static void menu_destroy_hash_value(ObMenu *self);
52 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
53 gpointer data);
54 static void parse_menu_separator(ObParseInst *i,
55 xmlDocPtr doc, xmlNodePtr node,
56 gpointer data);
57 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
58 gpointer data);
59 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
60 gchar **strippedlabel, guint *position);
61
62
63 static void client_dest(ObClient *client, gpointer data)
64 {
65 /* menus can be associated with a client, so close any that are since
66 we are disappearing now */
67 menu_frame_hide_all_client(client);
68 }
69
70 void menu_startup(gboolean reconfig)
71 {
72 xmlDocPtr doc;
73 xmlNodePtr node;
74 gboolean loaded = FALSE;
75 GSList *it;
76
77 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
78 (GDestroyNotify)menu_destroy_hash_value);
79
80 client_list_menu_startup(reconfig);
81 client_list_combined_menu_startup(reconfig);
82 client_menu_startup();
83
84 menu_parse_inst = parse_startup();
85
86 menu_parse_state.parent = NULL;
87 menu_parse_state.pipe_creator = NULL;
88 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
89 parse_register(menu_parse_inst, "item", parse_menu_item,
90 &menu_parse_state);
91 parse_register(menu_parse_inst, "separator",
92 parse_menu_separator, &menu_parse_state);
93
94 for (it = config_menu_files; it; it = g_slist_next(it)) {
95 if (parse_load_menu(it->data, &doc, &node)) {
96 loaded = TRUE;
97 parse_tree(menu_parse_inst, doc, node->children);
98 xmlFreeDoc(doc);
99 } else
100 g_message(_("Unable to find a valid menu file '%s'"),
101 (const gchar*)it->data);
102 }
103 if (!loaded) {
104 if (parse_load_menu("menu.xml", &doc, &node)) {
105 parse_tree(menu_parse_inst, doc, node->children);
106 xmlFreeDoc(doc);
107 } else
108 g_message(_("Unable to find a valid menu file '%s'"),
109 "menu.xml");
110 }
111
112 g_assert(menu_parse_state.parent == NULL);
113
114 if (!reconfig)
115 client_add_destroy_notify(client_dest, NULL);
116 }
117
118 void menu_shutdown(gboolean reconfig)
119 {
120 if (!reconfig)
121 client_remove_destroy_notify(client_dest);
122
123 parse_shutdown(menu_parse_inst);
124 menu_parse_inst = NULL;
125
126 client_list_menu_shutdown(reconfig);
127 client_list_combined_menu_shutdown(reconfig);
128
129 menu_frame_hide_all();
130 g_hash_table_destroy(menu_hash);
131 menu_hash = NULL;
132 }
133
134 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
135 {
136 ObMenu *menu = val;
137 return menu->pipe_creator == data;
138 }
139
140 void menu_pipe_execute(ObMenu *self)
141 {
142 xmlDocPtr doc;
143 xmlNodePtr node;
144 gchar *output;
145 GError *err = NULL;
146
147 if (!self->execute)
148 return;
149
150 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
151 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
152 self->execute, err->message);
153 g_error_free(err);
154 return;
155 }
156
157 if (parse_load_mem(output, strlen(output),
158 "openbox_pipe_menu", &doc, &node))
159 {
160 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
161 menu_clear_entries(self);
162
163 menu_parse_state.pipe_creator = self;
164 menu_parse_state.parent = self;
165 parse_tree(menu_parse_inst, doc, node->children);
166 xmlFreeDoc(doc);
167 } else {
168 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
169 }
170
171 g_free(output);
172 }
173
174 static ObMenu* menu_from_name(gchar *name)
175 {
176 ObMenu *self = NULL;
177
178 g_assert(name != NULL);
179
180 if (!(self = g_hash_table_lookup(menu_hash, name)))
181 g_message(_("Attempted to access menu '%s' but it does not exist"),
182 name);
183 return self;
184 }
185
186 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
187 ((c) >= 'A' && (c) <= 'Z') || \
188 ((c) >= 'a' && (c) <= 'z'))
189
190 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
191 gchar **strippedlabel, guint *position)
192 {
193 gunichar shortcut = 0;
194
195 *position = 0;
196
197 g_assert(strippedlabel != NULL);
198
199 if (label == NULL) {
200 *strippedlabel = NULL;
201 } else {
202 gchar *i;
203
204 *strippedlabel = g_strdup(label);
205
206 /* if allow_shortcut is false, then you can't use the &, instead you
207 have to just use the first valid character
208 */
209
210 i = strchr(*strippedlabel, '&');
211 if (allow_shortcut && i != NULL) {
212 /* there is an ampersand in the string */
213
214 /* you have to use a printable ascii character for shortcuts
215 don't allow space either, so you can have like "a & b"
216 */
217 if (VALID_SHORTCUT(*(i+1))) {
218 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
219 *position = i - *strippedlabel;
220
221 /* remove the & from the string */
222 for (; *i != '\0'; ++i)
223 *i = *(i+1);
224 }
225 } else {
226 /* there is no ampersand, so find the first valid character to use
227 instead */
228
229 for (i = *strippedlabel; *i != '\0'; ++i)
230 if (VALID_SHORTCUT(*i)) {
231 *position = i - *strippedlabel;
232 shortcut = g_unichar_tolower(g_utf8_get_char(i));
233 break;
234 }
235 }
236 }
237 return shortcut;
238 }
239
240 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
241 gpointer data)
242 {
243 ObMenuParseState *state = data;
244 gchar *label;
245
246 if (state->parent) {
247 if (parse_attr_string("label", node, &label)) {
248 GSList *acts = NULL;
249
250 for (node = node->children; node; node = node->next)
251 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
252 ObAction *a = action_parse
253 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
254 if (a)
255 acts = g_slist_append(acts, a);
256 }
257 menu_add_normal(state->parent, -1, label, acts, FALSE);
258 g_free(label);
259 }
260 }
261 }
262
263 static void parse_menu_separator(ObParseInst *i,
264 xmlDocPtr doc, xmlNodePtr node,
265 gpointer data)
266 {
267 ObMenuParseState *state = data;
268
269 if (state->parent) {
270 gchar *label;
271
272 if (!parse_attr_string("label", node, &label))
273 label = NULL;
274
275 menu_add_separator(state->parent, -1, label);
276 g_free(label);
277 }
278 }
279
280 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
281 gpointer data)
282 {
283 ObMenuParseState *state = data;
284 gchar *name = NULL, *title = NULL, *script = NULL;
285 ObMenu *menu;
286
287 if (!parse_attr_string("id", node, &name))
288 goto parse_menu_fail;
289
290 if (!g_hash_table_lookup(menu_hash, name)) {
291 if (!parse_attr_string("label", node, &title))
292 goto parse_menu_fail;
293
294 if ((menu = menu_new(name, title, FALSE, NULL))) {
295 menu->pipe_creator = state->pipe_creator;
296 if (parse_attr_string("execute", node, &script)) {
297 menu->execute = parse_expand_tilde(script);
298 } else {
299 ObMenu *old;
300
301 old = state->parent;
302 state->parent = menu;
303 parse_tree(i, doc, node->children);
304 state->parent = old;
305 }
306 }
307 }
308
309 if (state->parent)
310 menu_add_submenu(state->parent, -1, name);
311
312 parse_menu_fail:
313 g_free(name);
314 g_free(title);
315 g_free(script);
316 }
317
318 ObMenu* menu_new(const gchar *name, const gchar *title,
319 gboolean allow_shortcut_selection, gpointer data)
320 {
321 ObMenu *self;
322
323 self = g_new0(ObMenu, 1);
324 self->name = g_strdup(name);
325 self->data = data;
326
327 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
328 &self->title, &self->shortcut_position);
329
330 g_hash_table_replace(menu_hash, self->name, self);
331
332 /* Each menu has a single more_menu. When the menu spills past what
333 can fit on the screen, a new menu frame entry is created from this
334 more_menu, and a new menu frame for the submenu is created for this
335 menu, also pointing to the more_menu.
336
337 This can be done multiple times using the same more_menu.
338
339 more_menu->more_menu will always be NULL, since there is only 1 for
340 each menu. */
341 self->more_menu = g_new0(ObMenu, 1);
342 self->more_menu->name = _("More...");
343 self->more_menu->title = _("More...");
344 self->more_menu->data = data;
345 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
346
347 self->more_menu->show_func = self->show_func;
348 self->more_menu->hide_func = self->hide_func;
349 self->more_menu->update_func = self->update_func;
350 self->more_menu->execute_func = self->execute_func;
351 self->more_menu->destroy_func = self->destroy_func;
352 self->more_menu->place_func = self->place_func;
353
354 return self;
355 }
356
357 static void menu_destroy_hash_value(ObMenu *self)
358 {
359 /* make sure its not visible */
360 {
361 GList *it;
362 ObMenuFrame *f;
363
364 for (it = menu_frame_visible; it; it = g_list_next(it)) {
365 f = it->data;
366 if (f->menu == self)
367 menu_frame_hide_all();
368 }
369 }
370
371 if (self->destroy_func)
372 self->destroy_func(self, self->data);
373
374 menu_clear_entries(self);
375 g_free(self->name);
376 g_free(self->title);
377 g_free(self->execute);
378 g_free(self->more_menu);
379
380 g_free(self);
381 }
382
383 void menu_free(ObMenu *menu)
384 {
385 if (menu)
386 g_hash_table_remove(menu_hash, menu->name);
387 }
388
389 static gboolean menu_hide_delay_func(gpointer data)
390 {
391 menu_can_hide = TRUE;
392 return FALSE; /* no repeat */
393 }
394
395 void menu_show(gchar *name, gint x, gint y, gint button, ObClient *client)
396 {
397 ObMenu *self;
398 ObMenuFrame *frame;
399
400 if (!(self = menu_from_name(name))
401 || keyboard_interactively_grabbed()) return;
402
403 /* if the requested menu is already the top visible menu, then don't
404 bother */
405 if (menu_frame_visible) {
406 frame = menu_frame_visible->data;
407 if (frame->menu == self)
408 return;
409 }
410
411 menu_frame_hide_all();
412
413 frame = menu_frame_new(self, 0, client);
414 if (!menu_frame_show_topmenu(frame, x, y, button))
415 menu_frame_free(frame);
416 else if (!button) {
417 /* select the first entry if it's not a submenu and we opened
418 * the menu with the keyboard, and skip all headers */
419 GList *it = frame->entries;
420 while (it) {
421 ObMenuEntryFrame *e = it->data;
422 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
423 menu_frame_select(frame, e, FALSE);
424 break;
425 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
426 it = g_list_next(it);
427 else
428 break;
429 }
430 }
431
432 if (!button)
433 menu_can_hide = TRUE;
434 else {
435 menu_can_hide = FALSE;
436 ob_main_loop_timeout_add(ob_main_loop,
437 config_menu_hide_delay * 1000,
438 menu_hide_delay_func,
439 NULL, g_direct_equal, NULL);
440 }
441 }
442
443 gboolean menu_hide_delay_reached()
444 {
445 return menu_can_hide;
446 }
447
448 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
449 {
450 ObMenuEntry *self;
451
452 g_assert(menu);
453
454 self = g_new0(ObMenuEntry, 1);
455 self->ref = 1;
456 self->type = type;
457 self->menu = menu;
458 self->id = id;
459
460 switch (type) {
461 case OB_MENU_ENTRY_TYPE_NORMAL:
462 self->data.normal.enabled = TRUE;
463 break;
464 case OB_MENU_ENTRY_TYPE_SUBMENU:
465 case OB_MENU_ENTRY_TYPE_SEPARATOR:
466 break;
467 }
468
469 return self;
470 }
471
472 void menu_entry_ref(ObMenuEntry *self)
473 {
474 ++self->ref;
475 }
476
477 void menu_entry_unref(ObMenuEntry *self)
478 {
479 if (self && --self->ref == 0) {
480 switch (self->type) {
481 case OB_MENU_ENTRY_TYPE_NORMAL:
482 g_free(self->data.normal.label);
483 while (self->data.normal.actions) {
484 action_unref(self->data.normal.actions->data);
485 self->data.normal.actions =
486 g_slist_delete_link(self->data.normal.actions,
487 self->data.normal.actions);
488 }
489 break;
490 case OB_MENU_ENTRY_TYPE_SUBMENU:
491 g_free(self->data.submenu.name);
492 break;
493 case OB_MENU_ENTRY_TYPE_SEPARATOR:
494 break;
495 }
496
497 g_free(self);
498 }
499 }
500
501 void menu_clear_entries(ObMenu *self)
502 {
503 #ifdef DEBUG
504 /* assert that the menu isn't visible */
505 {
506 GList *it;
507 ObMenuFrame *f;
508
509 for (it = menu_frame_visible; it; it = g_list_next(it)) {
510 f = it->data;
511 g_assert(f->menu != self);
512 }
513 }
514 #endif
515
516 while (self->entries) {
517 menu_entry_unref(self->entries->data);
518 self->entries = g_list_delete_link(self->entries, self->entries);
519 }
520 self->more_menu->entries = self->entries; /* keep it in sync */
521 }
522
523 void menu_entry_remove(ObMenuEntry *self)
524 {
525 self->menu->entries = g_list_remove(self->menu->entries, self);
526 menu_entry_unref(self);
527 }
528
529 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
530 GSList *actions, gboolean allow_shortcut)
531 {
532 ObMenuEntry *e;
533
534 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
535 e->data.normal.actions = actions;
536
537 menu_entry_set_label(e, label, allow_shortcut);
538
539 self->entries = g_list_append(self->entries, e);
540 self->more_menu->entries = self->entries; /* keep it in sync */
541 return e;
542 }
543
544 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
545 {
546 ObMenuEntry *e;
547 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
548 /* points to itself */
549 e->data.submenu.name = g_strdup(self->name);
550 e->data.submenu.submenu = self;
551 e->data.submenu.show_from = show_from;
552 return e;
553 }
554
555 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
556 {
557 ObMenuEntry *e;
558
559 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
560 e->data.submenu.name = g_strdup(submenu);
561
562 self->entries = g_list_append(self->entries, e);
563 self->more_menu->entries = self->entries; /* keep it in sync */
564 return e;
565 }
566
567 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
568 {
569 ObMenuEntry *e;
570
571 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
572
573 menu_entry_set_label(e, label, FALSE);
574
575 self->entries = g_list_append(self->entries, e);
576 self->more_menu->entries = self->entries; /* keep it in sync */
577 return e;
578 }
579
580 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
581 {
582 self->show_func = func;
583 self->more_menu->show_func = func; /* keep it in sync */
584 }
585
586 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
587 {
588 self->hide_func = func;
589 self->more_menu->hide_func = func; /* keep it in sync */
590 }
591
592 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
593 {
594 self->update_func = func;
595 self->more_menu->update_func = func; /* keep it in sync */
596 }
597
598 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
599 {
600 self->execute_func = func;
601 self->more_menu->execute_func = func; /* keep it in sync */
602 }
603
604 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
605 {
606 self->destroy_func = func;
607 self->more_menu->destroy_func = func; /* keep it in sync */
608 }
609
610 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
611 {
612 self->place_func = func;
613 self->more_menu->place_func = func; /* keep it in sync */
614 }
615
616 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
617 {
618 ObMenuEntry *ret = NULL;
619 GList *it;
620
621 for (it = self->entries; it; it = g_list_next(it)) {
622 ObMenuEntry *e = it->data;
623
624 if (e->id == id) {
625 ret = e;
626 break;
627 }
628 }
629 return ret;
630 }
631
632 void menu_find_submenus(ObMenu *self)
633 {
634 GList *it;
635
636 for (it = self->entries; it; it = g_list_next(it)) {
637 ObMenuEntry *e = it->data;
638
639 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
640 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
641 }
642 }
643
644 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
645 gboolean allow_shortcut)
646 {
647 switch (self->type) {
648 case OB_MENU_ENTRY_TYPE_SEPARATOR:
649 g_free(self->data.separator.label);
650 self->data.separator.label = g_strdup(label);
651 break;
652 case OB_MENU_ENTRY_TYPE_NORMAL:
653 g_free(self->data.normal.label);
654 self->data.normal.shortcut =
655 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
656 &self->data.normal.shortcut_position);
657 break;
658 default:
659 g_assert_not_reached();
660 }
661 }
662
663 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
664 {
665 self->show_all_shortcuts = show;
666 }
This page took 0.072107 seconds and 5 git commands to generate.