1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
28 #include "menuframe.h"
32 #include "client_menu.h"
33 #include "client_list_menu.h"
34 #include "client_list_combined_menu.h"
36 #include "parser/parse.h"
38 typedef struct _ObMenuParseState ObMenuParseState;
40 struct _ObMenuParseState
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;
51 static void menu_destroy_hash_value(ObMenu *self);
52 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
54 static void parse_menu_separator(ObParseInst *i,
55 xmlDocPtr doc, xmlNodePtr node,
57 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
59 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
60 gchar **strippedlabel, guint *position);
63 static void client_dest(ObClient *client, gpointer data)
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);
70 void menu_startup(gboolean reconfig)
74 gboolean loaded = FALSE;
77 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
78 (GDestroyNotify)menu_destroy_hash_value);
80 client_list_menu_startup(reconfig);
81 client_list_combined_menu_startup(reconfig);
82 client_menu_startup();
84 menu_parse_inst = parse_startup();
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,
91 parse_register(menu_parse_inst, "separator",
92 parse_menu_separator, &menu_parse_state);
94 for (it = config_menu_files; it; it = g_slist_next(it)) {
95 if (parse_load_menu(it->data, &doc, &node)) {
97 parse_tree(menu_parse_inst, doc, node->children);
100 g_message(_("Unable to find a valid menu file '%s'"),
101 (const gchar*)it->data);
104 if (parse_load_menu("menu.xml", &doc, &node)) {
105 parse_tree(menu_parse_inst, doc, node->children);
108 g_message(_("Unable to find a valid menu file '%s'"),
112 g_assert(menu_parse_state.parent == NULL);
115 client_add_destroy_notify(client_dest, NULL);
118 void menu_shutdown(gboolean reconfig)
121 client_remove_destroy_notify(client_dest);
123 parse_shutdown(menu_parse_inst);
124 menu_parse_inst = NULL;
126 client_list_menu_shutdown(reconfig);
127 client_list_combined_menu_shutdown(reconfig);
129 menu_frame_hide_all();
130 g_hash_table_destroy(menu_hash);
134 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
137 return menu->pipe_creator == data;
140 void menu_pipe_execute(ObMenu *self)
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);
157 if (parse_load_mem(output, strlen(output),
158 "openbox_pipe_menu", &doc, &node))
160 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
161 menu_clear_entries(self);
163 menu_parse_state.pipe_creator = self;
164 menu_parse_state.parent = self;
165 parse_tree(menu_parse_inst, doc, node->children);
168 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
174 static ObMenu* menu_from_name(gchar *name)
178 g_assert(name != NULL);
180 if (!(self = g_hash_table_lookup(menu_hash, name)))
181 g_message(_("Attempted to access menu '%s' but it does not exist"),
186 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
187 ((c) >= 'A' && (c) <= 'Z') || \
188 ((c) >= 'a' && (c) <= 'z'))
190 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
191 gchar **strippedlabel, guint *position)
193 gunichar shortcut = 0;
197 g_assert(strippedlabel != NULL);
200 *strippedlabel = NULL;
204 *strippedlabel = g_strdup(label);
206 /* if allow_shortcut is false, then you can't use the &, instead you
207 have to just use the first valid character
210 i = strchr(*strippedlabel, '&');
211 if (allow_shortcut && i != NULL) {
212 /* there is an ampersand in the string */
214 /* you have to use a printable ascii character for shortcuts
215 don't allow space either, so you can have like "a & b"
217 if (VALID_SHORTCUT(*(i+1))) {
218 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
219 *position = i - *strippedlabel;
221 /* remove the & from the string */
222 for (; *i != '\0'; ++i)
226 /* there is no ampersand, so find the first valid character to use
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));
240 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
243 ObMenuParseState *state = data;
247 if (parse_attr_string("label", node, &label)) {
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);
255 acts = g_slist_append(acts, a);
257 menu_add_normal(state->parent, -1, label, acts, FALSE);
263 static void parse_menu_separator(ObParseInst *i,
264 xmlDocPtr doc, xmlNodePtr node,
267 ObMenuParseState *state = data;
272 if (!parse_attr_string("label", node, &label))
275 menu_add_separator(state->parent, -1, label);
280 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
283 ObMenuParseState *state = data;
284 gchar *name = NULL, *title = NULL, *script = NULL;
287 if (!parse_attr_string("id", node, &name))
288 goto parse_menu_fail;
290 if (!g_hash_table_lookup(menu_hash, name)) {
291 if (!parse_attr_string("label", node, &title))
292 goto parse_menu_fail;
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);
302 state->parent = menu;
303 parse_tree(i, doc, node->children);
310 menu_add_submenu(state->parent, -1, name);
318 ObMenu* menu_new(const gchar *name, const gchar *title,
319 gboolean allow_shortcut_selection, gpointer data)
323 self = g_new0(ObMenu, 1);
324 self->name = g_strdup(name);
327 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
328 &self->title, &self->shortcut_position);
330 g_hash_table_replace(menu_hash, self->name, self);
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.
337 This can be done multiple times using the same more_menu.
339 more_menu->more_menu will always be NULL, since there is only 1 for
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"));
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;
357 static void menu_destroy_hash_value(ObMenu *self)
359 /* make sure its not visible */
364 for (it = menu_frame_visible; it; it = g_list_next(it)) {
367 menu_frame_hide_all();
371 if (self->destroy_func)
372 self->destroy_func(self, self->data);
374 menu_clear_entries(self);
377 g_free(self->execute);
378 g_free(self->more_menu);
383 void menu_free(ObMenu *menu)
386 g_hash_table_remove(menu_hash, menu->name);
389 static gboolean menu_hide_delay_func(gpointer data)
391 menu_can_hide = TRUE;
392 return FALSE; /* no repeat */
395 void menu_show(gchar *name, gint x, gint y, gint button, ObClient *client)
400 if (!(self = menu_from_name(name))
401 || keyboard_interactively_grabbed()) return;
403 /* if the requested menu is already the top visible menu, then don't
405 if (menu_frame_visible) {
406 frame = menu_frame_visible->data;
407 if (frame->menu == self)
411 menu_frame_hide_all();
413 frame = menu_frame_new(self, 0, client);
414 if (!menu_frame_show_topmenu(frame, x, y, button))
415 menu_frame_free(frame);
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;
421 ObMenuEntryFrame *e = it->data;
422 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
423 menu_frame_select(frame, e, FALSE);
425 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
426 it = g_list_next(it);
433 menu_can_hide = TRUE;
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);
443 gboolean menu_hide_delay_reached()
445 return menu_can_hide;
448 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
454 self = g_new0(ObMenuEntry, 1);
461 case OB_MENU_ENTRY_TYPE_NORMAL:
462 self->data.normal.enabled = TRUE;
464 case OB_MENU_ENTRY_TYPE_SUBMENU:
465 case OB_MENU_ENTRY_TYPE_SEPARATOR:
472 void menu_entry_ref(ObMenuEntry *self)
477 void menu_entry_unref(ObMenuEntry *self)
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);
490 case OB_MENU_ENTRY_TYPE_SUBMENU:
491 g_free(self->data.submenu.name);
493 case OB_MENU_ENTRY_TYPE_SEPARATOR:
501 void menu_clear_entries(ObMenu *self)
504 /* assert that the menu isn't visible */
509 for (it = menu_frame_visible; it; it = g_list_next(it)) {
511 g_assert(f->menu != self);
516 while (self->entries) {
517 menu_entry_unref(self->entries->data);
518 self->entries = g_list_delete_link(self->entries, self->entries);
520 self->more_menu->entries = self->entries; /* keep it in sync */
523 void menu_entry_remove(ObMenuEntry *self)
525 self->menu->entries = g_list_remove(self->menu->entries, self);
526 menu_entry_unref(self);
529 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
530 GSList *actions, gboolean allow_shortcut)
534 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
535 e->data.normal.actions = actions;
537 menu_entry_set_label(e, label, allow_shortcut);
539 self->entries = g_list_append(self->entries, e);
540 self->more_menu->entries = self->entries; /* keep it in sync */
544 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
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;
555 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
559 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
560 e->data.submenu.name = g_strdup(submenu);
562 self->entries = g_list_append(self->entries, e);
563 self->more_menu->entries = self->entries; /* keep it in sync */
567 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
571 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
573 menu_entry_set_label(e, label, FALSE);
575 self->entries = g_list_append(self->entries, e);
576 self->more_menu->entries = self->entries; /* keep it in sync */
580 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
582 self->show_func = func;
583 self->more_menu->show_func = func; /* keep it in sync */
586 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
588 self->hide_func = func;
589 self->more_menu->hide_func = func; /* keep it in sync */
592 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
594 self->update_func = func;
595 self->more_menu->update_func = func; /* keep it in sync */
598 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
600 self->execute_func = func;
601 self->more_menu->execute_func = func; /* keep it in sync */
604 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
606 self->destroy_func = func;
607 self->more_menu->destroy_func = func; /* keep it in sync */
610 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
612 self->place_func = func;
613 self->more_menu->place_func = func; /* keep it in sync */
616 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
618 ObMenuEntry *ret = NULL;
621 for (it = self->entries; it; it = g_list_next(it)) {
622 ObMenuEntry *e = it->data;
632 void menu_find_submenus(ObMenu *self)
636 for (it = self->entries; it; it = g_list_next(it)) {
637 ObMenuEntry *e = it->data;
639 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
640 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
644 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
645 gboolean allow_shortcut)
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);
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);
659 g_assert_not_reached();
663 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
665 self->show_all_shortcuts = show;