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"
39 #include "imageload.h"
41 typedef struct _ObMenuParseState ObMenuParseState
;
43 struct _ObMenuParseState
49 static GHashTable
*menu_hash
= NULL
;
50 static ObtXmlInst
*menu_parse_inst
;
51 static ObMenuParseState menu_parse_state
;
52 static gboolean menu_can_hide
= FALSE
;
54 static void menu_destroy_hash_value(ObMenu
*self
);
55 static void parse_menu_item(xmlNodePtr node
, gpointer data
);
56 static void parse_menu_separator(xmlNodePtr node
, gpointer data
);
57 static void parse_menu(xmlNodePtr node
, gpointer data
);
58 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
59 gchar
**strippedlabel
, guint
*position
,
60 gboolean
*always_show
);
62 static void client_dest(ObClient
*client
, gpointer data
)
64 /* menus can be associated with a client, so close any that are since
65 we are disappearing now */
66 menu_frame_hide_all_client(client
);
69 void menu_startup(gboolean reconfig
)
71 gboolean loaded
= FALSE
;
74 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
75 (GDestroyNotify
)menu_destroy_hash_value
);
77 client_list_menu_startup(reconfig
);
78 client_list_combined_menu_startup(reconfig
);
79 client_menu_startup();
81 menu_parse_inst
= obt_xml_instance_new();
83 menu_parse_state
.parent
= NULL
;
84 menu_parse_state
.pipe_creator
= NULL
;
85 obt_xml_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
86 obt_xml_register(menu_parse_inst
, "item", parse_menu_item
,
88 obt_xml_register(menu_parse_inst
, "separator",
89 parse_menu_separator
, &menu_parse_state
);
91 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
92 if (obt_xml_load_config_file(menu_parse_inst
,
98 obt_xml_tree_from_root(menu_parse_inst
);
99 obt_xml_close(menu_parse_inst
);
101 g_message(_("Unable to find a valid menu file \"%s\""),
102 (const gchar
*)it
->data
);
105 if (obt_xml_load_config_file(menu_parse_inst
,
110 obt_xml_tree_from_root(menu_parse_inst
);
111 obt_xml_close(menu_parse_inst
);
113 g_message(_("Unable to find a valid menu file \"%s\""),
117 g_assert(menu_parse_state
.parent
== NULL
);
120 client_add_destroy_notify(client_dest
, NULL
);
123 void menu_shutdown(gboolean reconfig
)
126 client_remove_destroy_notify(client_dest
);
128 obt_xml_instance_unref(menu_parse_inst
);
129 menu_parse_inst
= NULL
;
131 client_list_menu_shutdown(reconfig
);
132 client_list_combined_menu_shutdown(reconfig
);
134 menu_frame_hide_all();
135 g_hash_table_destroy(menu_hash
);
139 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
142 return menu
->pipe_creator
!= NULL
;
145 static void clear_cache(gpointer key
, gpointer val
, gpointer data
)
149 menu_clear_entries(menu
);
152 void menu_clear_pipe_caches(void)
154 /* delete any pipe menus' submenus */
155 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, NULL
);
156 /* empty the top level pipe menus */
157 g_hash_table_foreach(menu_hash
, clear_cache
, NULL
);
160 void menu_pipe_execute(ObMenu
*self
)
167 if (self
->entries
) /* the entries are already created and cached */
170 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
171 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
172 self
->execute
, err
->message
);
177 if (obt_xml_load_mem(menu_parse_inst
, output
, strlen(output
),
178 "openbox_pipe_menu"))
180 menu_parse_state
.pipe_creator
= self
;
181 menu_parse_state
.parent
= self
;
182 obt_xml_tree_from_root(menu_parse_inst
);
183 obt_xml_close(menu_parse_inst
);
185 g_message(_("Invalid output from pipe-menu \"%s\""), self
->execute
);
191 static ObMenu
* menu_from_name(gchar
*name
)
195 g_assert(name
!= NULL
);
197 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
198 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
203 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
204 ((c) >= 'A' && (c) <= 'Z') || \
205 ((c) >= 'a' && (c) <= 'z'))
207 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
208 gchar
**strippedlabel
, guint
*position
,
209 gboolean
*always_show
)
211 gunichar shortcut
= 0;
214 *always_show
= FALSE
;
216 g_assert(strippedlabel
!= NULL
);
219 *strippedlabel
= NULL
;
223 *strippedlabel
= g_strdup(label
);
225 /* if allow_shortcut is false, then you can't use the '_', instead you
226 have to just use the first valid character
229 i
= strchr(*strippedlabel
, '_');
230 if (allow_shortcut
&& i
!= NULL
) {
231 /* there is an underscore in the string */
233 /* you have to use a printable ascii character for shortcuts
234 don't allow space either, so you can have like "a _ b"
236 if (VALID_SHORTCUT(*(i
+1)) || *(i
+1) == '_') {
237 /* Allow you to escape the first _ by putting __ */
239 shortcut
= g_unichar_tolower(g_utf8_get_char(i
+1));
240 *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. */
279 if (!(config_menu_user_show_icons
&&
280 obt_xml_attr_string(node
, "icon", &icon
)))
285 if (obt_xml_attr_string(node
, "label", &label
)) {
288 node
= obt_xml_find_node(node
->children
, "action");
290 ObActionsAct
*action
= actions_parse(node
);
292 acts
= g_slist_append(acts
, action
);
293 node
= obt_xml_find_node(node
->next
, "action");
295 e
= menu_add_normal(state
->parent
, -1, label
, acts
, TRUE
);
297 if (icon
) { /* Icon will be used. */
298 e
->data
.normal
.icon
= RrImageFetchFromFile(ob_rr_icons
, icon
);
299 if (e
->data
.normal
.icon
)
300 e
->data
.normal
.icon_alpha
= 0xff;
308 static void parse_menu_separator(xmlNodePtr node
, gpointer data
)
310 ObMenuParseState
*state
= data
;
315 if (!obt_xml_attr_string(node
, "label", &label
))
318 menu_add_separator(state
->parent
, -1, label
);
323 static void parse_menu(xmlNodePtr node
, gpointer data
)
325 ObMenuParseState
*state
= data
;
326 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
329 if (!obt_xml_attr_string(node
, "id", &name
))
330 goto parse_menu_fail
;
332 if (!g_hash_table_lookup(menu_hash
, name
)) {
333 if (!obt_xml_attr_string(node
, "label", &title
))
334 goto parse_menu_fail
;
336 if ((menu
= menu_new(name
, title
, TRUE
, NULL
))) {
337 menu
->pipe_creator
= state
->pipe_creator
;
338 if (obt_xml_attr_string(node
, "execute", &script
)) {
339 menu
->execute
= obt_paths_expand_tilde(script
);
344 state
->parent
= menu
;
345 obt_xml_tree(menu_parse_inst
, node
->children
);
352 menu_add_submenu(state
->parent
, -1, name
);
360 ObMenu
* menu_new(const gchar
*name
, const gchar
*title
,
361 gboolean allow_shortcut_selection
, gpointer data
)
365 self
= g_new0(ObMenu
, 1);
366 self
->name
= g_strdup(name
);
369 self
->shortcut
= parse_shortcut(title
, allow_shortcut_selection
,
370 &self
->title
, &self
->shortcut_position
,
371 &self
->shortcut_always_show
);
373 g_hash_table_replace(menu_hash
, self
->name
, self
);
375 /* Each menu has a single more_menu. When the menu spills past what
376 can fit on the screen, a new menu frame entry is created from this
377 more_menu, and a new menu frame for the submenu is created for this
378 menu, also pointing to the more_menu.
380 This can be done multiple times using the same more_menu.
382 more_menu->more_menu will always be NULL, since there is only 1 for
384 self
->more_menu
= g_new0(ObMenu
, 1);
385 self
->more_menu
->name
= _("More...");
386 self
->more_menu
->title
= _("More...");
387 self
->more_menu
->data
= data
;
388 self
->more_menu
->shortcut
= g_unichar_tolower(g_utf8_get_char("M"));
393 static void menu_destroy_hash_value(ObMenu
*self
)
395 /* make sure its not visible */
400 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
403 menu_frame_hide_all();
407 if (self
->destroy_func
)
408 self
->destroy_func(self
, self
->data
);
410 menu_clear_entries(self
);
413 g_free(self
->execute
);
414 g_free(self
->more_menu
);
419 void menu_free(ObMenu
*menu
)
422 g_hash_table_remove(menu_hash
, menu
->name
);
425 static gboolean
menu_hide_delay_func(gpointer data
)
427 menu_can_hide
= TRUE
;
428 return FALSE
; /* no repeat */
431 void menu_show(gchar
*name
, gint x
, gint y
, gboolean mouse
, ObClient
*client
)
436 if (!(self
= menu_from_name(name
)) ||
437 grab_on_keyboard() || grab_on_pointer()) return;
439 /* if the requested menu is already the top visible menu, then don't
441 if (menu_frame_visible
) {
442 frame
= menu_frame_visible
->data
;
443 if (frame
->menu
== self
)
447 menu_frame_hide_all();
449 /* clear the pipe menus when showing a new menu */
450 menu_clear_pipe_caches();
452 frame
= menu_frame_new(self
, 0, client
);
453 if (!menu_frame_show_topmenu(frame
, x
, y
, mouse
))
454 menu_frame_free(frame
);
457 /* select the first entry if it's not a submenu and we opened
458 * the menu with the keyboard, and skip all headers */
459 GList
*it
= frame
->entries
;
461 ObMenuEntryFrame
*e
= it
->data
;
462 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
463 menu_frame_select(frame
, e
, FALSE
);
465 } else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
466 it
= g_list_next(it
);
472 /* reset the hide timer */
474 menu_can_hide
= TRUE
;
476 menu_can_hide
= FALSE
;
477 obt_main_loop_timeout_add(ob_main_loop
,
478 config_menu_hide_delay
* 1000,
479 menu_hide_delay_func
,
480 NULL
, g_direct_equal
, NULL
);
485 gboolean
menu_hide_delay_reached(void)
487 return menu_can_hide
;
490 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
496 self
= g_new0(ObMenuEntry
, 1);
503 case OB_MENU_ENTRY_TYPE_NORMAL
:
504 self
->data
.normal
.enabled
= TRUE
;
506 case OB_MENU_ENTRY_TYPE_SUBMENU
:
507 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
514 void menu_entry_ref(ObMenuEntry
*self
)
519 void menu_entry_unref(ObMenuEntry
*self
)
521 if (self
&& --self
->ref
== 0) {
522 switch (self
->type
) {
523 case OB_MENU_ENTRY_TYPE_NORMAL
:
524 RrImageUnref(self
->data
.normal
.icon
);
525 g_free(self
->data
.normal
.label
);
526 while (self
->data
.normal
.actions
) {
527 actions_act_unref(self
->data
.normal
.actions
->data
);
528 self
->data
.normal
.actions
=
529 g_slist_delete_link(self
->data
.normal
.actions
,
530 self
->data
.normal
.actions
);
533 case OB_MENU_ENTRY_TYPE_SUBMENU
:
534 g_free(self
->data
.submenu
.name
);
536 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
537 g_free(self
->data
.separator
.label
);
545 void menu_clear_entries(ObMenu
*self
)
548 /* assert that the menu isn't visible */
553 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
555 g_assert(f
->menu
!= self
);
560 while (self
->entries
) {
561 menu_entry_unref(self
->entries
->data
);
562 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
564 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
567 void menu_entry_remove(ObMenuEntry
*self
)
569 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
570 menu_entry_unref(self
);
573 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, const gchar
*label
,
574 GSList
*actions
, gboolean allow_shortcut
)
578 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
579 e
->data
.normal
.actions
= actions
;
581 menu_entry_set_label(e
, label
, allow_shortcut
);
583 self
->entries
= g_list_append(self
->entries
, e
);
584 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
588 ObMenuEntry
* menu_get_more(ObMenu
*self
, guint show_from
)
591 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, -1);
592 /* points to itself */
593 e
->data
.submenu
.name
= g_strdup(self
->name
);
594 e
->data
.submenu
.submenu
= self
;
595 e
->data
.submenu
.show_from
= show_from
;
599 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, const gchar
*submenu
)
603 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
604 e
->data
.submenu
.name
= g_strdup(submenu
);
606 self
->entries
= g_list_append(self
->entries
, e
);
607 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
611 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
, const gchar
*label
)
615 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
617 menu_entry_set_label(e
, label
, FALSE
);
619 self
->entries
= g_list_append(self
->entries
, e
);
620 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
624 void menu_set_show_func(ObMenu
*self
, ObMenuShowFunc func
)
626 self
->show_func
= func
;
629 void menu_set_hide_func(ObMenu
*self
, ObMenuHideFunc func
)
631 self
->hide_func
= func
;
634 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
636 self
->update_func
= func
;
639 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
641 self
->execute_func
= func
;
642 self
->more_menu
->execute_func
= func
; /* keep it in sync */
645 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
647 self
->destroy_func
= func
;
650 void menu_set_place_func(ObMenu
*self
, ObMenuPlaceFunc func
)
652 self
->place_func
= func
;
655 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
657 ObMenuEntry
*ret
= NULL
;
660 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
661 ObMenuEntry
*e
= it
->data
;
671 void menu_find_submenus(ObMenu
*self
)
675 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
676 ObMenuEntry
*e
= it
->data
;
678 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
679 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);
683 void menu_entry_set_label(ObMenuEntry
*self
, const gchar
*label
,
684 gboolean allow_shortcut
)
686 switch (self
->type
) {
687 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
688 g_free(self
->data
.separator
.label
);
689 self
->data
.separator
.label
= g_strdup(label
);
691 case OB_MENU_ENTRY_TYPE_NORMAL
:
692 g_free(self
->data
.normal
.label
);
693 self
->data
.normal
.shortcut
=
694 parse_shortcut(label
, allow_shortcut
, &self
->data
.normal
.label
,
695 &self
->data
.normal
.shortcut_position
,
696 &self
->data
.normal
.shortcut_always_show
);
699 g_assert_not_reached();
703 void menu_show_all_shortcuts(ObMenu
*self
, gboolean show
)
705 self
->show_all_shortcuts
= show
;