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
!= NULL
;
140 static void clear_cache(gpointer key
, gpointer val
, gpointer data
)
144 menu_clear_entries(menu
);
147 void menu_clear_pipe_caches()
149 /* delete any pipe menus' submenus */
150 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, NULL
);
151 /* empty the top level pipe menus */
152 g_hash_table_foreach(menu_hash
, clear_cache
, NULL
);
155 void menu_pipe_execute(ObMenu
*self
)
164 if (self
->entries
) /* the entries are already created and cached */
167 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
168 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
169 self
->execute
, err
->message
);
174 if (parse_load_mem(output
, strlen(output
),
175 "openbox_pipe_menu", &doc
, &node
))
177 menu_parse_state
.pipe_creator
= self
;
178 menu_parse_state
.parent
= self
;
179 parse_tree(menu_parse_inst
, doc
, node
->children
);
182 g_message(_("Invalid output from pipe-menu '%s'"), self
->execute
);
188 static ObMenu
* menu_from_name(gchar
*name
)
192 g_assert(name
!= NULL
);
194 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
195 g_message(_("Attempted to access menu '%s' but it does not exist"),
200 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
201 ((c) >= 'A' && (c) <= 'Z') || \
202 ((c) >= 'a' && (c) <= 'z'))
204 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
205 gchar
**strippedlabel
, guint
*position
)
207 gunichar shortcut
= 0;
211 g_assert(strippedlabel
!= NULL
);
214 *strippedlabel
= NULL
;
218 *strippedlabel
= g_strdup(label
);
220 /* if allow_shortcut is false, then you can't use the &, instead you
221 have to just use the first valid character
224 i
= strchr(*strippedlabel
, '&');
225 if (allow_shortcut
&& i
!= NULL
) {
226 /* there is an ampersand in the string */
228 /* you have to use a printable ascii character for shortcuts
229 don't allow space either, so you can have like "a & b"
231 if (VALID_SHORTCUT(*(i
+1))) {
232 shortcut
= g_unichar_tolower(g_utf8_get_char(i
+1));
233 *position
= i
- *strippedlabel
;
235 /* remove the & from the string */
236 for (; *i
!= '\0'; ++i
)
240 /* there is no ampersand, so find the first valid character to use
243 for (i
= *strippedlabel
; *i
!= '\0'; ++i
)
244 if (VALID_SHORTCUT(*i
)) {
245 *position
= i
- *strippedlabel
;
246 shortcut
= g_unichar_tolower(g_utf8_get_char(i
));
254 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
257 ObMenuParseState
*state
= data
;
261 if (parse_attr_string("label", node
, &label
)) {
264 for (node
= node
->children
; node
; node
= node
->next
)
265 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action")) {
266 ObAction
*a
= action_parse
267 (i
, doc
, node
, OB_USER_ACTION_MENU_SELECTION
);
269 acts
= g_slist_append(acts
, a
);
271 menu_add_normal(state
->parent
, -1, label
, acts
, FALSE
);
277 static void parse_menu_separator(ObParseInst
*i
,
278 xmlDocPtr doc
, xmlNodePtr node
,
281 ObMenuParseState
*state
= data
;
286 if (!parse_attr_string("label", node
, &label
))
289 menu_add_separator(state
->parent
, -1, label
);
294 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
297 ObMenuParseState
*state
= data
;
298 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
301 if (!parse_attr_string("id", node
, &name
))
302 goto parse_menu_fail
;
304 if (!g_hash_table_lookup(menu_hash
, name
)) {
305 if (!parse_attr_string("label", node
, &title
))
306 goto parse_menu_fail
;
308 if ((menu
= menu_new(name
, title
, FALSE
, NULL
))) {
309 menu
->pipe_creator
= state
->pipe_creator
;
310 if (parse_attr_string("execute", node
, &script
)) {
311 menu
->execute
= parse_expand_tilde(script
);
316 state
->parent
= menu
;
317 parse_tree(i
, doc
, node
->children
);
324 menu_add_submenu(state
->parent
, -1, name
);
332 ObMenu
* menu_new(const gchar
*name
, const gchar
*title
,
333 gboolean allow_shortcut_selection
, gpointer data
)
337 self
= g_new0(ObMenu
, 1);
338 self
->name
= g_strdup(name
);
341 self
->shortcut
= parse_shortcut(title
, allow_shortcut_selection
,
342 &self
->title
, &self
->shortcut_position
);
344 g_hash_table_replace(menu_hash
, self
->name
, self
);
346 /* Each menu has a single more_menu. When the menu spills past what
347 can fit on the screen, a new menu frame entry is created from this
348 more_menu, and a new menu frame for the submenu is created for this
349 menu, also pointing to the more_menu.
351 This can be done multiple times using the same more_menu.
353 more_menu->more_menu will always be NULL, since there is only 1 for
355 self
->more_menu
= g_new0(ObMenu
, 1);
356 self
->more_menu
->name
= _("More...");
357 self
->more_menu
->title
= _("More...");
358 self
->more_menu
->data
= data
;
359 self
->more_menu
->shortcut
= g_unichar_tolower(g_utf8_get_char("M"));
361 self
->more_menu
->show_func
= self
->show_func
;
362 self
->more_menu
->hide_func
= self
->hide_func
;
363 self
->more_menu
->update_func
= self
->update_func
;
364 self
->more_menu
->execute_func
= self
->execute_func
;
365 self
->more_menu
->destroy_func
= self
->destroy_func
;
366 self
->more_menu
->place_func
= self
->place_func
;
371 static void menu_destroy_hash_value(ObMenu
*self
)
373 /* make sure its not visible */
378 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
381 menu_frame_hide_all();
385 if (self
->destroy_func
)
386 self
->destroy_func(self
, self
->data
);
388 menu_clear_entries(self
);
391 g_free(self
->execute
);
392 g_free(self
->more_menu
);
397 void menu_free(ObMenu
*menu
)
400 g_hash_table_remove(menu_hash
, menu
->name
);
403 static gboolean
menu_hide_delay_func(gpointer data
)
405 menu_can_hide
= TRUE
;
406 return FALSE
; /* no repeat */
409 void menu_show(gchar
*name
, gint x
, gint y
, gint button
, ObClient
*client
)
414 if (!(self
= menu_from_name(name
))
415 || keyboard_interactively_grabbed()) return;
417 /* if the requested menu is already the top visible menu, then don't
419 if (menu_frame_visible
) {
420 frame
= menu_frame_visible
->data
;
421 if (frame
->menu
== self
)
425 menu_frame_hide_all();
427 /* clear the pipe menus when showing a new menu */
428 menu_clear_pipe_caches();
430 frame
= menu_frame_new(self
, 0, client
);
431 if (!menu_frame_show_topmenu(frame
, x
, y
, button
))
432 menu_frame_free(frame
);
434 /* select the first entry if it's not a submenu and we opened
435 * the menu with the keyboard, and skip all headers */
436 GList
*it
= frame
->entries
;
438 ObMenuEntryFrame
*e
= it
->data
;
439 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
440 menu_frame_select(frame
, e
, FALSE
);
442 } else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
443 it
= g_list_next(it
);
450 menu_can_hide
= TRUE
;
452 menu_can_hide
= FALSE
;
453 ob_main_loop_timeout_add(ob_main_loop
,
454 config_menu_hide_delay
* 1000,
455 menu_hide_delay_func
,
456 NULL
, g_direct_equal
, NULL
);
460 gboolean
menu_hide_delay_reached()
462 return menu_can_hide
;
465 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
471 self
= g_new0(ObMenuEntry
, 1);
478 case OB_MENU_ENTRY_TYPE_NORMAL
:
479 self
->data
.normal
.enabled
= TRUE
;
481 case OB_MENU_ENTRY_TYPE_SUBMENU
:
482 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
489 void menu_entry_ref(ObMenuEntry
*self
)
494 void menu_entry_unref(ObMenuEntry
*self
)
496 if (self
&& --self
->ref
== 0) {
497 switch (self
->type
) {
498 case OB_MENU_ENTRY_TYPE_NORMAL
:
499 g_free(self
->data
.normal
.label
);
500 while (self
->data
.normal
.actions
) {
501 action_unref(self
->data
.normal
.actions
->data
);
502 self
->data
.normal
.actions
=
503 g_slist_delete_link(self
->data
.normal
.actions
,
504 self
->data
.normal
.actions
);
507 case OB_MENU_ENTRY_TYPE_SUBMENU
:
508 g_free(self
->data
.submenu
.name
);
510 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
518 void menu_clear_entries(ObMenu
*self
)
521 /* assert that the menu isn't visible */
526 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
528 g_assert(f
->menu
!= self
);
533 while (self
->entries
) {
534 menu_entry_unref(self
->entries
->data
);
535 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
537 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
540 void menu_entry_remove(ObMenuEntry
*self
)
542 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
543 menu_entry_unref(self
);
546 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, const gchar
*label
,
547 GSList
*actions
, gboolean allow_shortcut
)
551 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
552 e
->data
.normal
.actions
= actions
;
554 menu_entry_set_label(e
, label
, allow_shortcut
);
556 self
->entries
= g_list_append(self
->entries
, e
);
557 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
561 ObMenuEntry
* menu_get_more(ObMenu
*self
, guint show_from
)
564 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, -1);
565 /* points to itself */
566 e
->data
.submenu
.name
= g_strdup(self
->name
);
567 e
->data
.submenu
.submenu
= self
;
568 e
->data
.submenu
.show_from
= show_from
;
572 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, const gchar
*submenu
)
576 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
577 e
->data
.submenu
.name
= g_strdup(submenu
);
579 self
->entries
= g_list_append(self
->entries
, e
);
580 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
584 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
, const gchar
*label
)
588 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
590 menu_entry_set_label(e
, label
, FALSE
);
592 self
->entries
= g_list_append(self
->entries
, e
);
593 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
597 void menu_set_show_func(ObMenu
*self
, ObMenuShowFunc func
)
599 self
->show_func
= func
;
600 self
->more_menu
->show_func
= func
; /* keep it in sync */
603 void menu_set_hide_func(ObMenu
*self
, ObMenuHideFunc func
)
605 self
->hide_func
= func
;
606 self
->more_menu
->hide_func
= func
; /* keep it in sync */
609 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
611 self
->update_func
= func
;
612 self
->more_menu
->update_func
= func
; /* keep it in sync */
615 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
617 self
->execute_func
= func
;
618 self
->more_menu
->execute_func
= func
; /* keep it in sync */
621 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
623 self
->destroy_func
= func
;
624 self
->more_menu
->destroy_func
= func
; /* keep it in sync */
627 void menu_set_place_func(ObMenu
*self
, ObMenuPlaceFunc func
)
629 self
->place_func
= func
;
630 self
->more_menu
->place_func
= func
; /* keep it in sync */
633 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
635 ObMenuEntry
*ret
= NULL
;
638 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
639 ObMenuEntry
*e
= it
->data
;
649 void menu_find_submenus(ObMenu
*self
)
653 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
654 ObMenuEntry
*e
= it
->data
;
656 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
657 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);
661 void menu_entry_set_label(ObMenuEntry
*self
, const gchar
*label
,
662 gboolean allow_shortcut
)
664 switch (self
->type
) {
665 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
666 g_free(self
->data
.separator
.label
);
667 self
->data
.separator
.label
= g_strdup(label
);
669 case OB_MENU_ENTRY_TYPE_NORMAL
:
670 g_free(self
->data
.normal
.label
);
671 self
->data
.normal
.shortcut
=
672 parse_shortcut(label
, allow_shortcut
, &self
->data
.normal
.label
,
673 &self
->data
.normal
.shortcut_position
);
676 g_assert_not_reached();
680 void menu_show_all_shortcuts(ObMenu
*self
, gboolean show
)
682 self
->show_all_shortcuts
= show
;