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,
61 gboolean *always_show);
64 static void client_dest(ObClient *client, gpointer data)
66 /* menus can be associated with a client, so close any that are since
67 we are disappearing now */
68 menu_frame_hide_all_client(client);
71 void menu_startup(gboolean reconfig)
75 gboolean loaded = FALSE;
78 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
79 (GDestroyNotify)menu_destroy_hash_value);
81 client_list_menu_startup(reconfig);
82 client_list_combined_menu_startup(reconfig);
83 client_menu_startup();
85 menu_parse_inst = parse_startup();
87 menu_parse_state.parent = NULL;
88 menu_parse_state.pipe_creator = NULL;
89 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
90 parse_register(menu_parse_inst, "item", parse_menu_item,
92 parse_register(menu_parse_inst, "separator",
93 parse_menu_separator, &menu_parse_state);
95 for (it = config_menu_files; it; it = g_slist_next(it)) {
96 if (parse_load_menu(it->data, &doc, &node)) {
98 parse_tree(menu_parse_inst, doc, node->children);
101 g_message(_("Unable to find a valid menu file '%s'"),
102 (const gchar*)it->data);
105 if (parse_load_menu("menu.xml", &doc, &node)) {
106 parse_tree(menu_parse_inst, doc, node->children);
109 g_message(_("Unable to find a valid menu file '%s'"),
113 g_assert(menu_parse_state.parent == NULL);
116 client_add_destroy_notify(client_dest, NULL);
119 void menu_shutdown(gboolean reconfig)
122 client_remove_destroy_notify(client_dest);
124 parse_shutdown(menu_parse_inst);
125 menu_parse_inst = NULL;
127 client_list_menu_shutdown(reconfig);
128 client_list_combined_menu_shutdown(reconfig);
130 menu_frame_hide_all();
131 g_hash_table_destroy(menu_hash);
135 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
138 return menu->pipe_creator != NULL;
141 static void clear_cache(gpointer key, gpointer val, gpointer data)
145 menu_clear_entries(menu);
148 void menu_clear_pipe_caches()
150 /* delete any pipe menus' submenus */
151 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
152 /* empty the top level pipe menus */
153 g_hash_table_foreach(menu_hash, clear_cache, NULL);
156 void menu_pipe_execute(ObMenu *self)
165 if (self->entries) /* the entries are already created and cached */
168 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
169 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
170 self->execute, err->message);
175 if (parse_load_mem(output, strlen(output),
176 "openbox_pipe_menu", &doc, &node))
178 menu_parse_state.pipe_creator = self;
179 menu_parse_state.parent = self;
180 parse_tree(menu_parse_inst, doc, node->children);
183 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
189 static ObMenu* menu_from_name(gchar *name)
193 g_assert(name != NULL);
195 if (!(self = g_hash_table_lookup(menu_hash, name)))
196 g_message(_("Attempted to access menu '%s' but it does not exist"),
201 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
202 ((c) >= 'A' && (c) <= 'Z') || \
203 ((c) >= 'a' && (c) <= 'z'))
205 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
206 gchar **strippedlabel, guint *position,
207 gboolean *always_show)
209 gunichar shortcut = 0;
212 *always_show = FALSE;
214 g_assert(strippedlabel != NULL);
217 *strippedlabel = NULL;
221 *strippedlabel = g_strdup(label);
223 /* if allow_shortcut is false, then you can't use the &, instead you
224 have to just use the first valid character
227 i = strchr(*strippedlabel, '&');
228 if (allow_shortcut && i != NULL) {
229 /* there is an ampersand in the string */
231 /* you have to use a printable ascii character for shortcuts
232 don't allow space either, so you can have like "a & b"
234 if (VALID_SHORTCUT(*(i+1))) {
235 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
236 *position = i - *strippedlabel;
239 /* remove the & from the string */
240 for (; *i != '\0'; ++i)
244 /* there is no ampersand, so find the first valid character to use
247 for (i = *strippedlabel; *i != '\0'; ++i)
248 if (VALID_SHORTCUT(*i)) {
249 *position = i - *strippedlabel;
250 shortcut = g_unichar_tolower(g_utf8_get_char(i));
258 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
261 ObMenuParseState *state = data;
265 if (parse_attr_string("label", node, &label)) {
268 for (node = node->children; node; node = node->next)
269 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
270 ObAction *a = action_parse
271 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
273 acts = g_slist_append(acts, a);
275 menu_add_normal(state->parent, -1, label, acts, FALSE);
281 static void parse_menu_separator(ObParseInst *i,
282 xmlDocPtr doc, xmlNodePtr node,
285 ObMenuParseState *state = data;
290 if (!parse_attr_string("label", node, &label))
293 menu_add_separator(state->parent, -1, label);
298 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
301 ObMenuParseState *state = data;
302 gchar *name = NULL, *title = NULL, *script = NULL;
305 if (!parse_attr_string("id", node, &name))
306 goto parse_menu_fail;
308 if (!g_hash_table_lookup(menu_hash, name)) {
309 if (!parse_attr_string("label", node, &title))
310 goto parse_menu_fail;
312 if ((menu = menu_new(name, title, FALSE, NULL))) {
313 menu->pipe_creator = state->pipe_creator;
314 if (parse_attr_string("execute", node, &script)) {
315 menu->execute = parse_expand_tilde(script);
320 state->parent = menu;
321 parse_tree(i, doc, node->children);
328 menu_add_submenu(state->parent, -1, name);
336 ObMenu* menu_new(const gchar *name, const gchar *title,
337 gboolean allow_shortcut_selection, gpointer data)
341 self = g_new0(ObMenu, 1);
342 self->name = g_strdup(name);
345 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
346 &self->title, &self->shortcut_position,
347 &self->shortcut_always_show);
349 g_hash_table_replace(menu_hash, self->name, self);
351 /* Each menu has a single more_menu. When the menu spills past what
352 can fit on the screen, a new menu frame entry is created from this
353 more_menu, and a new menu frame for the submenu is created for this
354 menu, also pointing to the more_menu.
356 This can be done multiple times using the same more_menu.
358 more_menu->more_menu will always be NULL, since there is only 1 for
360 self->more_menu = g_new0(ObMenu, 1);
361 self->more_menu->name = _("More...");
362 self->more_menu->title = _("More...");
363 self->more_menu->data = data;
364 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
366 self->more_menu->show_func = self->show_func;
367 self->more_menu->hide_func = self->hide_func;
368 self->more_menu->update_func = self->update_func;
369 self->more_menu->execute_func = self->execute_func;
370 self->more_menu->destroy_func = self->destroy_func;
371 self->more_menu->place_func = self->place_func;
376 static void menu_destroy_hash_value(ObMenu *self)
378 /* make sure its not visible */
383 for (it = menu_frame_visible; it; it = g_list_next(it)) {
386 menu_frame_hide_all();
390 if (self->destroy_func)
391 self->destroy_func(self, self->data);
393 menu_clear_entries(self);
396 g_free(self->execute);
397 g_free(self->more_menu);
402 void menu_free(ObMenu *menu)
405 g_hash_table_remove(menu_hash, menu->name);
408 static gboolean menu_hide_delay_func(gpointer data)
410 menu_can_hide = TRUE;
411 return FALSE; /* no repeat */
414 void menu_show(gchar *name, gint x, gint y, gint button, ObClient *client)
419 if (!(self = menu_from_name(name))
420 || keyboard_interactively_grabbed()) return;
422 /* if the requested menu is already the top visible menu, then don't
424 if (menu_frame_visible) {
425 frame = menu_frame_visible->data;
426 if (frame->menu == self)
430 menu_frame_hide_all();
432 /* clear the pipe menus when showing a new menu */
433 menu_clear_pipe_caches();
435 frame = menu_frame_new(self, 0, client);
436 if (!menu_frame_show_topmenu(frame, x, y, button))
437 menu_frame_free(frame);
439 /* select the first entry if it's not a submenu and we opened
440 * the menu with the keyboard, and skip all headers */
441 GList *it = frame->entries;
443 ObMenuEntryFrame *e = it->data;
444 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
445 menu_frame_select(frame, e, FALSE);
447 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
448 it = g_list_next(it);
455 menu_can_hide = TRUE;
457 menu_can_hide = FALSE;
458 ob_main_loop_timeout_add(ob_main_loop,
459 config_menu_hide_delay * 1000,
460 menu_hide_delay_func,
461 NULL, g_direct_equal, NULL);
465 gboolean menu_hide_delay_reached()
467 return menu_can_hide;
470 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
476 self = g_new0(ObMenuEntry, 1);
483 case OB_MENU_ENTRY_TYPE_NORMAL:
484 self->data.normal.enabled = TRUE;
486 case OB_MENU_ENTRY_TYPE_SUBMENU:
487 case OB_MENU_ENTRY_TYPE_SEPARATOR:
494 void menu_entry_ref(ObMenuEntry *self)
499 void menu_entry_unref(ObMenuEntry *self)
501 if (self && --self->ref == 0) {
502 switch (self->type) {
503 case OB_MENU_ENTRY_TYPE_NORMAL:
504 g_free(self->data.normal.label);
505 while (self->data.normal.actions) {
506 action_unref(self->data.normal.actions->data);
507 self->data.normal.actions =
508 g_slist_delete_link(self->data.normal.actions,
509 self->data.normal.actions);
512 case OB_MENU_ENTRY_TYPE_SUBMENU:
513 g_free(self->data.submenu.name);
515 case OB_MENU_ENTRY_TYPE_SEPARATOR:
523 void menu_clear_entries(ObMenu *self)
526 /* assert that the menu isn't visible */
531 for (it = menu_frame_visible; it; it = g_list_next(it)) {
533 g_assert(f->menu != self);
538 while (self->entries) {
539 menu_entry_unref(self->entries->data);
540 self->entries = g_list_delete_link(self->entries, self->entries);
542 self->more_menu->entries = self->entries; /* keep it in sync */
545 void menu_entry_remove(ObMenuEntry *self)
547 self->menu->entries = g_list_remove(self->menu->entries, self);
548 menu_entry_unref(self);
551 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
552 GSList *actions, gboolean allow_shortcut)
556 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
557 e->data.normal.actions = actions;
559 menu_entry_set_label(e, label, allow_shortcut);
561 self->entries = g_list_append(self->entries, e);
562 self->more_menu->entries = self->entries; /* keep it in sync */
566 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
569 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
570 /* points to itself */
571 e->data.submenu.name = g_strdup(self->name);
572 e->data.submenu.submenu = self;
573 e->data.submenu.show_from = show_from;
577 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
581 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
582 e->data.submenu.name = g_strdup(submenu);
584 self->entries = g_list_append(self->entries, e);
585 self->more_menu->entries = self->entries; /* keep it in sync */
589 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
593 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
595 menu_entry_set_label(e, label, FALSE);
597 self->entries = g_list_append(self->entries, e);
598 self->more_menu->entries = self->entries; /* keep it in sync */
602 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
604 self->show_func = func;
605 self->more_menu->show_func = func; /* keep it in sync */
608 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
610 self->hide_func = func;
611 self->more_menu->hide_func = func; /* keep it in sync */
614 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
616 self->update_func = func;
617 self->more_menu->update_func = func; /* keep it in sync */
620 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
622 self->execute_func = func;
623 self->more_menu->execute_func = func; /* keep it in sync */
626 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
628 self->destroy_func = func;
629 self->more_menu->destroy_func = func; /* keep it in sync */
632 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
634 self->place_func = func;
635 self->more_menu->place_func = func; /* keep it in sync */
638 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
640 ObMenuEntry *ret = NULL;
643 for (it = self->entries; it; it = g_list_next(it)) {
644 ObMenuEntry *e = it->data;
654 void menu_find_submenus(ObMenu *self)
658 for (it = self->entries; it; it = g_list_next(it)) {
659 ObMenuEntry *e = it->data;
661 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
662 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
666 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
667 gboolean allow_shortcut)
669 switch (self->type) {
670 case OB_MENU_ENTRY_TYPE_SEPARATOR:
671 g_free(self->data.separator.label);
672 self->data.separator.label = g_strdup(label);
674 case OB_MENU_ENTRY_TYPE_NORMAL:
675 g_free(self->data.normal.label);
676 self->data.normal.shortcut =
677 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
678 &self->data.normal.shortcut_position,
679 &self->data.normal.shortcut_always_show);
682 g_assert_not_reached();
686 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
688 self->show_all_shortcuts = show;