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 static void client_dest(ObClient *client, gpointer data)
63 /* menus can be associated with a client, so close any that are since
64 we are disappearing now */
65 menu_frame_hide_all_client(client);
68 void menu_startup(gboolean reconfig)
70 gboolean loaded = FALSE;
73 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
74 (GDestroyNotify)menu_destroy_hash_value);
76 client_list_menu_startup(reconfig);
77 client_list_combined_menu_startup(reconfig);
78 client_menu_startup();
80 menu_parse_inst = obt_xml_instance_new();
82 menu_parse_state.parent = NULL;
83 menu_parse_state.pipe_creator = NULL;
84 obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
85 obt_xml_register(menu_parse_inst, "item", parse_menu_item,
87 obt_xml_register(menu_parse_inst, "separator",
88 parse_menu_separator, &menu_parse_state);
90 for (it = config_menu_files; it; it = g_slist_next(it)) {
91 if (obt_xml_load_config_file(menu_parse_inst,
97 obt_xml_tree_from_root(menu_parse_inst);
98 obt_xml_close(menu_parse_inst);
100 g_message(_("Unable to find a valid menu file \"%s\""),
101 (const gchar*)it->data);
104 if (obt_xml_load_config_file(menu_parse_inst,
109 obt_xml_tree_from_root(menu_parse_inst);
110 obt_xml_close(menu_parse_inst);
112 g_message(_("Unable to find a valid menu file \"%s\""),
116 g_assert(menu_parse_state.parent == NULL);
119 client_add_destroy_notify(client_dest, NULL);
122 void menu_shutdown(gboolean reconfig)
125 client_remove_destroy_notify(client_dest);
127 obt_xml_instance_unref(menu_parse_inst);
128 menu_parse_inst = NULL;
130 client_list_menu_shutdown(reconfig);
131 client_list_combined_menu_shutdown(reconfig);
133 menu_frame_hide_all();
134 g_hash_table_destroy(menu_hash);
138 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
141 return menu->pipe_creator != NULL;
144 static void clear_cache(gpointer key, gpointer val, gpointer data)
148 menu_clear_entries(menu);
151 void menu_clear_pipe_caches(void)
153 /* delete any pipe menus' submenus */
154 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
155 /* empty the top level pipe menus */
156 g_hash_table_foreach(menu_hash, clear_cache, NULL);
159 void menu_pipe_execute(ObMenu *self)
166 if (self->entries) /* the entries are already created and cached */
169 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
170 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
171 self->execute, err->message);
176 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
177 "openbox_pipe_menu"))
179 menu_parse_state.pipe_creator = self;
180 menu_parse_state.parent = self;
181 obt_xml_tree_from_root(menu_parse_inst);
182 obt_xml_close(menu_parse_inst);
184 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
190 static ObMenu* menu_from_name(gchar *name)
194 g_assert(name != NULL);
196 if (!(self = g_hash_table_lookup(menu_hash, name)))
197 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
202 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
203 ((c) >= 'A' && (c) <= 'Z') || \
204 ((c) >= 'a' && (c) <= 'z'))
206 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
207 gchar **strippedlabel, guint *position,
208 gboolean *always_show)
210 gunichar shortcut = 0;
213 *always_show = FALSE;
215 g_assert(strippedlabel != NULL);
218 *strippedlabel = NULL;
222 *strippedlabel = g_strdup(label);
224 /* if allow_shortcut is false, then you can't use the '_', instead you
225 have to just use the first valid character
228 i = strchr(*strippedlabel, '_');
229 if (allow_shortcut && i != NULL) {
230 /* there is an underscore in the string */
232 /* you have to use a printable ascii character for shortcuts
233 don't allow space either, so you can have like "a _ b"
235 if (VALID_SHORTCUT(*(i+1)) || *(i+1) == '_') {
236 /* Allow you to escape the first _ by putting __ */
238 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
239 *position = i - *strippedlabel;
243 /* remove the '_' from the string */
244 for (; *i != '\0'; ++i)
246 } else if (*(i+1) == '\0') {
247 /* no default shortcut if the '_' is the last character
248 (eg. "Exit_") for menu entries that you don't want
249 to be executed by mistake
254 /* there is no underscore, so find the first valid character to use
257 for (i = *strippedlabel; *i != '\0'; ++i)
258 if (VALID_SHORTCUT(*i)) {
259 *position = i - *strippedlabel;
260 shortcut = g_unichar_tolower(g_utf8_get_char(i));
268 static void parse_menu_item(xmlNodePtr node, gpointer data)
270 ObMenuParseState *state = data;
276 /* Don't try to extract "icon" attribute if icons in user-defined
277 menus are not enabled. */
279 if (obt_xml_attr_string(node, "label", &label)) {
283 c = obt_xml_find_node(node->children, "action");
285 ObActionsAct *action = actions_parse(c);
287 acts = g_slist_append(acts, action);
288 c = obt_xml_find_node(node->next, "action");
290 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
292 if (config_menu_show_icons &&
293 obt_xml_attr_string(node, "icon", &icon))
297 ic = RrImageCacheFindName(ob_rr_icons, icon);
301 ic = RrImageNew(ob_rr_icons);
302 if (!RrImageAddPictureName(ic, icon))
303 RrImageUnref(ic); /* no need to keep it around */
305 e->data.normal.icon = ic;
307 if (e->data.normal.icon)
308 e->data.normal.icon_alpha = 0xff;
317 static void parse_menu_separator(xmlNodePtr node, gpointer data)
319 ObMenuParseState *state = data;
324 if (!obt_xml_attr_string(node, "label", &label))
327 menu_add_separator(state->parent, -1, label);
332 static void parse_menu(xmlNodePtr node, gpointer data)
334 ObMenuParseState *state = data;
335 gchar *name = NULL, *title = NULL, *script = NULL;
338 if (!obt_xml_attr_string(node, "id", &name))
339 goto parse_menu_fail;
341 if (!g_hash_table_lookup(menu_hash, name)) {
342 if (!obt_xml_attr_string(node, "label", &title))
343 goto parse_menu_fail;
345 if ((menu = menu_new(name, title, TRUE, NULL))) {
346 menu->pipe_creator = state->pipe_creator;
347 if (obt_xml_attr_string(node, "execute", &script)) {
348 menu->execute = obt_paths_expand_tilde(script);
353 state->parent = menu;
354 obt_xml_tree(menu_parse_inst, node->children);
361 menu_add_submenu(state->parent, -1, name);
369 ObMenu* menu_new(const gchar *name, const gchar *title,
370 gboolean allow_shortcut_selection, gpointer data)
374 self = g_new0(ObMenu, 1);
375 self->name = g_strdup(name);
378 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
379 &self->title, &self->shortcut_position,
380 &self->shortcut_always_show);
382 g_hash_table_replace(menu_hash, self->name, self);
384 /* Each menu has a single more_menu. When the menu spills past what
385 can fit on the screen, a new menu frame entry is created from this
386 more_menu, and a new menu frame for the submenu is created for this
387 menu, also pointing to the more_menu.
389 This can be done multiple times using the same more_menu.
391 more_menu->more_menu will always be NULL, since there is only 1 for
393 self->more_menu = g_new0(ObMenu, 1);
394 self->more_menu->name = _("More...");
395 self->more_menu->title = _("More...");
396 self->more_menu->data = data;
397 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
402 static void menu_destroy_hash_value(ObMenu *self)
404 /* make sure its not visible */
409 for (it = menu_frame_visible; it; it = g_list_next(it)) {
412 menu_frame_hide_all();
416 if (self->destroy_func)
417 self->destroy_func(self, self->data);
419 menu_clear_entries(self);
422 g_free(self->execute);
423 g_free(self->more_menu);
428 void menu_free(ObMenu *menu)
431 g_hash_table_remove(menu_hash, menu->name);
434 static gboolean menu_hide_delay_func(gpointer data)
436 menu_can_hide = TRUE;
437 return FALSE; /* no repeat */
440 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
445 if (!(self = menu_from_name(name)) ||
446 grab_on_keyboard() || grab_on_pointer()) return;
448 /* if the requested menu is already the top visible menu, then don't
450 if (menu_frame_visible) {
451 frame = menu_frame_visible->data;
452 if (frame->menu == self)
456 menu_frame_hide_all();
458 /* clear the pipe menus when showing a new menu */
459 menu_clear_pipe_caches();
461 frame = menu_frame_new(self, 0, client);
462 if (!menu_frame_show_topmenu(frame, x, y, mouse))
463 menu_frame_free(frame);
466 /* select the first entry if it's not a submenu and we opened
467 * the menu with the keyboard, and skip all headers */
468 GList *it = frame->entries;
470 ObMenuEntryFrame *e = it->data;
471 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
472 menu_frame_select(frame, e, FALSE);
474 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
475 it = g_list_next(it);
481 /* reset the hide timer */
483 menu_can_hide = TRUE;
485 menu_can_hide = FALSE;
486 obt_main_loop_timeout_add(ob_main_loop,
487 config_menu_hide_delay * 1000,
488 menu_hide_delay_func,
489 NULL, g_direct_equal, NULL);
494 gboolean menu_hide_delay_reached(void)
496 return menu_can_hide;
499 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
505 self = g_new0(ObMenuEntry, 1);
512 case OB_MENU_ENTRY_TYPE_NORMAL:
513 self->data.normal.enabled = TRUE;
515 case OB_MENU_ENTRY_TYPE_SUBMENU:
516 case OB_MENU_ENTRY_TYPE_SEPARATOR:
523 void menu_entry_ref(ObMenuEntry *self)
528 void menu_entry_unref(ObMenuEntry *self)
530 if (self && --self->ref == 0) {
531 switch (self->type) {
532 case OB_MENU_ENTRY_TYPE_NORMAL:
533 RrImageUnref(self->data.normal.icon);
534 g_free(self->data.normal.label);
535 while (self->data.normal.actions) {
536 actions_act_unref(self->data.normal.actions->data);
537 self->data.normal.actions =
538 g_slist_delete_link(self->data.normal.actions,
539 self->data.normal.actions);
542 case OB_MENU_ENTRY_TYPE_SUBMENU:
543 g_free(self->data.submenu.name);
545 case OB_MENU_ENTRY_TYPE_SEPARATOR:
546 g_free(self->data.separator.label);
554 void menu_clear_entries(ObMenu *self)
557 /* assert that the menu isn't visible */
562 for (it = menu_frame_visible; it; it = g_list_next(it)) {
564 g_assert(f->menu != self);
569 while (self->entries) {
570 menu_entry_unref(self->entries->data);
571 self->entries = g_list_delete_link(self->entries, self->entries);
573 self->more_menu->entries = self->entries; /* keep it in sync */
576 void menu_entry_remove(ObMenuEntry *self)
578 self->menu->entries = g_list_remove(self->menu->entries, self);
579 menu_entry_unref(self);
582 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
583 GSList *actions, gboolean allow_shortcut)
587 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
588 e->data.normal.actions = actions;
590 menu_entry_set_label(e, label, allow_shortcut);
592 self->entries = g_list_append(self->entries, e);
593 self->more_menu->entries = self->entries; /* keep it in sync */
597 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
600 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
601 /* points to itself */
602 e->data.submenu.name = g_strdup(self->name);
603 e->data.submenu.submenu = self;
604 e->data.submenu.show_from = show_from;
608 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
612 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
613 e->data.submenu.name = g_strdup(submenu);
615 self->entries = g_list_append(self->entries, e);
616 self->more_menu->entries = self->entries; /* keep it in sync */
620 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
624 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
626 menu_entry_set_label(e, label, FALSE);
628 self->entries = g_list_append(self->entries, e);
629 self->more_menu->entries = self->entries; /* keep it in sync */
633 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
635 self->show_func = func;
638 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
640 self->hide_func = func;
643 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
645 self->update_func = func;
648 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
650 self->execute_func = func;
651 self->more_menu->execute_func = func; /* keep it in sync */
654 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
656 self->destroy_func = func;
659 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
661 self->place_func = func;
664 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
666 ObMenuEntry *ret = NULL;
669 for (it = self->entries; it; it = g_list_next(it)) {
670 ObMenuEntry *e = it->data;
680 void menu_find_submenus(ObMenu *self)
684 for (it = self->entries; it; it = g_list_next(it)) {
685 ObMenuEntry *e = it->data;
687 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
688 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
692 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
693 gboolean allow_shortcut)
695 switch (self->type) {
696 case OB_MENU_ENTRY_TYPE_SEPARATOR:
697 g_free(self->data.separator.label);
698 self->data.separator.label = g_strdup(label);
700 case OB_MENU_ENTRY_TYPE_NORMAL:
701 g_free(self->data.normal.label);
702 self->data.normal.shortcut =
703 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
704 &self->data.normal.shortcut_position,
705 &self->data.normal.shortcut_always_show);
708 g_assert_not_reached();
712 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
714 self->show_all_shortcuts = show;