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.
29 #include "menuframe.h"
33 #include "client_menu.h"
34 #include "client_list_menu.h"
35 #include "client_list_combined_menu.h"
38 #include "obt/paths.h"
40 typedef struct _ObMenuParseState ObMenuParseState;
42 struct _ObMenuParseState
48 static GHashTable *menu_hash = NULL;
49 static ObtXmlInst *menu_parse_inst;
50 static ObMenuParseState menu_parse_state;
51 static gboolean menu_can_hide = FALSE;
53 static void menu_destroy_hash_value(ObMenu *self);
54 static void parse_menu_item(xmlNodePtr node, gpointer data);
55 static void parse_menu_separator(xmlNodePtr node, gpointer data);
56 static void parse_menu(xmlNodePtr node, gpointer data);
57 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
58 gchar **strippedlabel, guint *position,
59 gboolean *always_show);
61 void menu_startup(gboolean reconfig)
63 gboolean loaded = FALSE;
66 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
67 (GDestroyNotify)menu_destroy_hash_value);
69 client_list_menu_startup(reconfig);
70 client_list_combined_menu_startup(reconfig);
71 client_menu_startup();
73 menu_parse_inst = obt_xml_instance_new();
75 menu_parse_state.parent = NULL;
76 menu_parse_state.pipe_creator = NULL;
77 obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
78 obt_xml_register(menu_parse_inst, "item", parse_menu_item,
80 obt_xml_register(menu_parse_inst, "separator",
81 parse_menu_separator, &menu_parse_state);
83 for (it = config_menu_files; it; it = g_slist_next(it)) {
84 if (obt_xml_load_config_file(menu_parse_inst,
90 obt_xml_tree_from_root(menu_parse_inst);
91 obt_xml_close(menu_parse_inst);
93 g_message(_("Unable to find a valid menu file \"%s\""),
94 (const gchar*)it->data);
97 if (obt_xml_load_config_file(menu_parse_inst,
102 obt_xml_tree_from_root(menu_parse_inst);
103 obt_xml_close(menu_parse_inst);
105 g_message(_("Unable to find a valid menu file \"%s\""),
109 g_assert(menu_parse_state.parent == NULL);
112 void menu_shutdown(gboolean reconfig)
114 obt_xml_instance_unref(menu_parse_inst);
115 menu_parse_inst = NULL;
117 client_list_menu_shutdown(reconfig);
118 client_list_combined_menu_shutdown(reconfig);
120 menu_frame_hide_all();
121 g_hash_table_destroy(menu_hash);
125 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
128 return menu->pipe_creator != NULL;
131 static void clear_cache(gpointer key, gpointer val, gpointer data)
135 menu_clear_entries(menu);
138 void menu_clear_pipe_caches(void)
140 /* delete any pipe menus' submenus */
141 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
142 /* empty the top level pipe menus */
143 g_hash_table_foreach(menu_hash, clear_cache, NULL);
146 void menu_pipe_execute(ObMenu *self)
153 if (self->entries) /* the entries are already created and cached */
156 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
157 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
158 self->execute, err->message);
163 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
164 "openbox_pipe_menu"))
166 menu_parse_state.pipe_creator = self;
167 menu_parse_state.parent = self;
168 obt_xml_tree_from_root(menu_parse_inst);
169 obt_xml_close(menu_parse_inst);
171 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
177 static ObMenu* menu_from_name(gchar *name)
181 g_assert(name != NULL);
183 if (!(self = g_hash_table_lookup(menu_hash, name)))
184 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
189 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
190 ((c) >= 'A' && (c) <= 'Z') || \
191 ((c) >= 'a' && (c) <= 'z'))
193 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
194 gchar **strippedlabel, guint *position,
195 gboolean *always_show)
197 gunichar shortcut = 0;
200 *always_show = FALSE;
202 g_assert(strippedlabel != NULL);
205 *strippedlabel = NULL;
210 *strippedlabel = g_strdup(label);
212 /* if allow_shortcut is false, then you can't use the '_', instead you
213 have to just use the first valid character
216 /* allow __ to escape an underscore */
221 if (i && *(i+1) == '_') {
224 /* remove the escape '_' from the string */
225 for (j = i; *j != '\0'; ++j)
233 if (allow_shortcut && i != NULL) {
234 /* there is an underscore in the string */
236 /* you have to use a printable ascii character for shortcuts
237 don't allow space either, so you can have like "a _ b"
239 if (VALID_SHORTCUT(*(i+1))) {
240 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
241 *position = i - *strippedlabel;
244 /* remove the '_' from the string */
245 for (; *i != '\0'; ++i)
247 } else if (*(i+1) == '\0') {
248 /* no default shortcut if the '_' is the last character
249 (eg. "Exit_") for menu entries that you don't want
250 to be executed by mistake
255 /* there is no underscore, so find the first valid character to use
258 for (i = *strippedlabel; *i != '\0'; ++i)
259 if (VALID_SHORTCUT(*i)) {
260 *position = i - *strippedlabel;
261 shortcut = g_unichar_tolower(g_utf8_get_char(i));
269 static void parse_menu_item(xmlNodePtr node, gpointer data)
271 ObMenuParseState *state = data;
277 /* Don't try to extract "icon" attribute if icons in user-defined
278 menus are not enabled. */
280 if (obt_xml_attr_string(node, "label", &label)) {
284 c = obt_xml_find_node(node->children, "action");
286 ObActionsAct *action = actions_parse(c);
288 acts = g_slist_append(acts, action);
289 c = obt_xml_find_node(node->next, "action");
291 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
293 if (config_menu_show_icons &&
294 obt_xml_attr_string(node, "icon", &icon))
298 ic = RrImageCacheFindName(ob_rr_icons, icon);
302 ic = RrImageNew(ob_rr_icons);
303 if (!RrImageAddPictureName(ic, icon)) {
304 RrImageUnref(ic); /* no need to keep it around */
308 e->data.normal.icon = ic;
310 if (e->data.normal.icon)
311 e->data.normal.icon_alpha = 0xff;
320 static void parse_menu_separator(xmlNodePtr node, gpointer data)
322 ObMenuParseState *state = data;
327 if (!obt_xml_attr_string(node, "label", &label))
330 menu_add_separator(state->parent, -1, label);
335 static void parse_menu(xmlNodePtr node, gpointer data)
337 ObMenuParseState *state = data;
338 gchar *name = NULL, *title = NULL, *script = NULL;
341 if (!obt_xml_attr_string(node, "id", &name))
342 goto parse_menu_fail;
344 if (!g_hash_table_lookup(menu_hash, name)) {
345 if (!obt_xml_attr_string(node, "label", &title))
346 goto parse_menu_fail;
348 if ((menu = menu_new(name, title, TRUE, NULL))) {
349 menu->pipe_creator = state->pipe_creator;
350 if (obt_xml_attr_string(node, "execute", &script)) {
351 menu->execute = obt_paths_expand_tilde(script);
356 state->parent = menu;
357 obt_xml_tree(menu_parse_inst, node->children);
364 menu_add_submenu(state->parent, -1, name);
372 ObMenu* menu_new(const gchar *name, const gchar *title,
373 gboolean allow_shortcut_selection, gpointer data)
377 self = g_slice_new0(ObMenu);
378 self->name = g_strdup(name);
381 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
382 &self->title, &self->shortcut_position,
383 &self->shortcut_always_show);
385 g_hash_table_replace(menu_hash, self->name, self);
387 /* Each menu has a single more_menu. When the menu spills past what
388 can fit on the screen, a new menu frame entry is created from this
389 more_menu, and a new menu frame for the submenu is created for this
390 menu, also pointing to the more_menu.
392 This can be done multiple times using the same more_menu.
394 more_menu->more_menu will always be NULL, since there is only 1 for
396 self->more_menu = g_slice_new0(ObMenu);
397 self->more_menu->name = _("More...");
398 self->more_menu->title = _("More...");
399 self->more_menu->data = data;
400 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
405 static void menu_destroy_hash_value(ObMenu *self)
407 /* make sure its not visible */
412 for (it = menu_frame_visible; it; it = g_list_next(it)) {
415 menu_frame_hide_all();
419 if (self->destroy_func)
420 self->destroy_func(self, self->data);
422 menu_clear_entries(self);
425 g_free(self->execute);
426 g_slice_free(ObMenu, self->more_menu);
428 g_slice_free(ObMenu, self);
431 void menu_free(ObMenu *menu)
434 g_hash_table_remove(menu_hash, menu->name);
437 static gboolean menu_hide_delay_func(gpointer data)
439 menu_can_hide = TRUE;
440 return FALSE; /* no repeat */
443 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
448 if (!(self = menu_from_name(name)) ||
449 grab_on_keyboard() || grab_on_pointer()) return;
451 /* if the requested menu is already the top visible menu, then don't
453 if (menu_frame_visible) {
454 frame = menu_frame_visible->data;
455 if (frame->menu == self)
459 menu_frame_hide_all();
461 /* clear the pipe menus when showing a new menu */
462 menu_clear_pipe_caches();
464 frame = menu_frame_new(self, 0, client);
465 if (!menu_frame_show_topmenu(frame, x, y, mouse))
466 menu_frame_free(frame);
469 /* select the first entry if it's not a submenu and we opened
470 * the menu with the keyboard, and skip all headers */
471 GList *it = frame->entries;
473 ObMenuEntryFrame *e = it->data;
474 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
475 menu_frame_select(frame, e, FALSE);
477 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
478 it = g_list_next(it);
484 /* reset the hide timer */
486 menu_can_hide = TRUE;
488 menu_can_hide = FALSE;
489 obt_main_loop_timeout_add(ob_main_loop,
490 config_menu_hide_delay * 1000,
491 menu_hide_delay_func,
492 NULL, g_direct_equal, NULL);
497 gboolean menu_hide_delay_reached(void)
499 return menu_can_hide;
502 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
508 self = g_slice_new0(ObMenuEntry);
515 case OB_MENU_ENTRY_TYPE_NORMAL:
516 self->data.normal.enabled = TRUE;
518 case OB_MENU_ENTRY_TYPE_SUBMENU:
519 case OB_MENU_ENTRY_TYPE_SEPARATOR:
526 void menu_entry_ref(ObMenuEntry *self)
531 void menu_entry_unref(ObMenuEntry *self)
533 if (self && --self->ref == 0) {
534 switch (self->type) {
535 case OB_MENU_ENTRY_TYPE_NORMAL:
536 RrImageUnref(self->data.normal.icon);
537 g_free(self->data.normal.label);
538 while (self->data.normal.actions) {
539 actions_act_unref(self->data.normal.actions->data);
540 self->data.normal.actions =
541 g_slist_delete_link(self->data.normal.actions,
542 self->data.normal.actions);
545 case OB_MENU_ENTRY_TYPE_SUBMENU:
546 g_free(self->data.submenu.name);
548 case OB_MENU_ENTRY_TYPE_SEPARATOR:
549 g_free(self->data.separator.label);
553 g_slice_free(ObMenuEntry, self);
557 void menu_clear_entries(ObMenu *self)
560 /* assert that the menu isn't visible */
565 for (it = menu_frame_visible; it; it = g_list_next(it)) {
567 g_assert(f->menu != self);
572 while (self->entries) {
573 menu_entry_unref(self->entries->data);
574 self->entries = g_list_delete_link(self->entries, self->entries);
576 self->more_menu->entries = self->entries; /* keep it in sync */
579 void menu_entry_remove(ObMenuEntry *self)
581 self->menu->entries = g_list_remove(self->menu->entries, self);
582 menu_entry_unref(self);
585 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
586 GSList *actions, gboolean allow_shortcut)
590 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
591 e->data.normal.actions = actions;
593 menu_entry_set_label(e, label, allow_shortcut);
595 self->entries = g_list_append(self->entries, e);
596 self->more_menu->entries = self->entries; /* keep it in sync */
600 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
603 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
604 /* points to itself */
605 e->data.submenu.name = g_strdup(self->name);
606 e->data.submenu.submenu = self;
607 e->data.submenu.show_from = show_from;
611 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
615 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
616 e->data.submenu.name = g_strdup(submenu);
618 self->entries = g_list_append(self->entries, e);
619 self->more_menu->entries = self->entries; /* keep it in sync */
623 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
627 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
629 menu_entry_set_label(e, label, FALSE);
631 self->entries = g_list_append(self->entries, e);
632 self->more_menu->entries = self->entries; /* keep it in sync */
636 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
638 self->show_func = func;
641 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
643 self->hide_func = func;
646 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
648 self->update_func = func;
651 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
653 self->execute_func = func;
654 self->more_menu->execute_func = func; /* keep it in sync */
657 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
659 self->destroy_func = func;
662 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
664 self->place_func = func;
667 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
669 ObMenuEntry *ret = NULL;
672 for (it = self->entries; it; it = g_list_next(it)) {
673 ObMenuEntry *e = it->data;
683 void menu_find_submenus(ObMenu *self)
687 for (it = self->entries; it; it = g_list_next(it)) {
688 ObMenuEntry *e = it->data;
690 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
691 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
695 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
696 gboolean allow_shortcut)
698 switch (self->type) {
699 case OB_MENU_ENTRY_TYPE_SEPARATOR:
700 g_free(self->data.separator.label);
701 self->data.separator.label = g_strdup(label);
703 case OB_MENU_ENTRY_TYPE_NORMAL:
704 g_free(self->data.normal.label);
705 self->data.normal.shortcut =
706 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
707 &self->data.normal.shortcut_position,
708 &self->data.normal.shortcut_always_show);
711 g_assert_not_reached();
715 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
717 self->show_all_shortcuts = show;