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.
27 #include "menuframe.h"
31 #include "client_menu.h"
32 #include "client_list_menu.h"
33 #include "client_list_combined_menu.h"
35 #include "parser/parse.h"
37 typedef struct _ObMenuParseState ObMenuParseState
;
39 struct _ObMenuParseState
45 static GHashTable
*menu_hash
= NULL
;
46 static ObParseInst
*menu_parse_inst
;
47 static ObMenuParseState menu_parse_state
;
49 static void menu_destroy_hash_value(ObMenu
*self
);
50 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
52 static void parse_menu_separator(ObParseInst
*i
,
53 xmlDocPtr doc
, xmlNodePtr node
,
55 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
57 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
58 gchar
**strippedlabel
, guint
*position
);
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
)
72 gboolean loaded
= FALSE
;
75 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
76 (GDestroyNotify
)menu_destroy_hash_value
);
78 client_list_menu_startup(reconfig
);
79 client_list_combined_menu_startup(reconfig
);
80 client_menu_startup();
82 menu_parse_inst
= parse_startup();
84 menu_parse_state
.parent
= NULL
;
85 menu_parse_state
.pipe_creator
= NULL
;
86 parse_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
87 parse_register(menu_parse_inst
, "item", parse_menu_item
,
89 parse_register(menu_parse_inst
, "separator",
90 parse_menu_separator
, &menu_parse_state
);
92 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
93 if (parse_load_menu(it
->data
, &doc
, &node
)) {
95 parse_tree(menu_parse_inst
, doc
, node
->children
);
98 g_message(_("Unable to find a valid menu file '%s'"),
99 (const gchar
*)it
->data
);
102 if (parse_load_menu("menu.xml", &doc
, &node
)) {
103 parse_tree(menu_parse_inst
, doc
, node
->children
);
106 g_message(_("Unable to find a valid menu file '%s'"),
110 g_assert(menu_parse_state
.parent
== NULL
);
113 client_add_destroy_notify(client_dest
, NULL
);
116 void menu_shutdown(gboolean reconfig
)
119 client_remove_destroy_notify(client_dest
);
121 parse_shutdown(menu_parse_inst
);
122 menu_parse_inst
= NULL
;
124 client_list_menu_shutdown(reconfig
);
125 client_list_combined_menu_shutdown(reconfig
);
127 menu_frame_hide_all();
128 g_hash_table_destroy(menu_hash
);
132 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
135 return menu
->pipe_creator
== data
;
138 void menu_pipe_execute(ObMenu
*self
)
148 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
149 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
150 self
->execute
, err
->message
);
155 if (parse_load_mem(output
, strlen(output
),
156 "openbox_pipe_menu", &doc
, &node
))
158 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, self
);
159 menu_clear_entries(self
);
161 menu_parse_state
.pipe_creator
= self
;
162 menu_parse_state
.parent
= self
;
163 parse_tree(menu_parse_inst
, doc
, node
->children
);
166 g_message(_("Invalid output from pipe-menu '%s'"), self
->execute
);
172 static ObMenu
* menu_from_name(gchar
*name
)
176 g_assert(name
!= NULL
);
178 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
179 g_message(_("Attempted to access menu '%s' but it does not exist"),
184 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
185 ((c) >= 'A' && (c) <= 'Z') || \
186 ((c) >= 'a' && (c) <= 'z'))
188 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
189 gchar
**strippedlabel
, guint
*position
)
191 gunichar shortcut
= 0;
195 g_assert(strippedlabel
!= NULL
);
198 *strippedlabel
= NULL
;
202 *strippedlabel
= g_strdup(label
);
204 /* if allow_shortcut is false, then you can't use the &, instead you
205 have to just use the first valid character
208 i
= strchr(*strippedlabel
, '&');
209 if (allow_shortcut
&& i
!= NULL
) {
210 /* there is an ampersand in the string */
212 /* you have to use a printable ascii character for shortcuts
213 don't allow space either, so you can have like "a & b"
215 if (VALID_SHORTCUT(*(i
+1))) {
216 shortcut
= g_unichar_tolower(g_utf8_get_char(i
+1));
217 *position
= i
- *strippedlabel
;
219 /* remove the & from the string */
220 for (; *i
!= '\0'; ++i
)
224 /* there is no ampersand, so find the first valid character to use
227 for (i
= *strippedlabel
; *i
!= '\0'; ++i
)
228 if (VALID_SHORTCUT(*i
)) {
229 *position
= i
- *strippedlabel
;
230 shortcut
= g_unichar_tolower(g_utf8_get_char(i
));
238 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
241 ObMenuParseState
*state
= data
;
245 if (parse_attr_string("label", node
, &label
)) {
248 for (node
= node
->children
; node
; node
= node
->next
)
249 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action")) {
250 ObAction
*a
= action_parse
251 (i
, doc
, node
, OB_USER_ACTION_MENU_SELECTION
);
253 acts
= g_slist_append(acts
, a
);
255 menu_add_normal(state
->parent
, -1, label
, acts
, FALSE
);
261 static void parse_menu_separator(ObParseInst
*i
,
262 xmlDocPtr doc
, xmlNodePtr node
,
265 ObMenuParseState
*state
= data
;
270 if (!parse_attr_string("label", node
, &label
))
273 menu_add_separator(state
->parent
, -1, label
);
278 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
281 ObMenuParseState
*state
= data
;
282 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
285 if (!parse_attr_string("id", node
, &name
))
286 goto parse_menu_fail
;
288 if (!g_hash_table_lookup(menu_hash
, name
)) {
289 if (!parse_attr_string("label", node
, &title
))
290 goto parse_menu_fail
;
292 if ((menu
= menu_new(name
, title
, FALSE
, NULL
))) {
293 menu
->pipe_creator
= state
->pipe_creator
;
294 if (parse_attr_string("execute", node
, &script
)) {
295 menu
->execute
= parse_expand_tilde(script
);
300 state
->parent
= menu
;
301 parse_tree(i
, doc
, node
->children
);
308 menu_add_submenu(state
->parent
, -1, name
);
316 ObMenu
* menu_new(const gchar
*name
, const gchar
*title
,
317 gboolean allow_shortcut_selection
, gpointer data
)
321 self
= g_new0(ObMenu
, 1);
322 self
->name
= g_strdup(name
);
325 self
->shortcut
= parse_shortcut(title
, allow_shortcut_selection
,
326 &self
->title
, &self
->shortcut_position
);
328 g_hash_table_replace(menu_hash
, self
->name
, self
);
330 /* Each menu has a single more_menu. When the menu spills past what
331 can fit on the screen, a new menu frame entry is created from this
332 more_menu, and a new menu frame for the submenu is created for this
333 menu, also pointing to the more_menu.
335 This can be done multiple times using the same more_menu.
337 more_menu->more_menu will always be NULL, since there is only 1 for
339 self
->more_menu
= g_new0(ObMenu
, 1);
340 self
->more_menu
->name
= _("More...");
341 self
->more_menu
->title
= _("More...");
342 self
->more_menu
->data
= data
;
343 self
->more_menu
->shortcut
= g_unichar_tolower(g_utf8_get_char("M"));
345 self
->more_menu
->show_func
= self
->show_func
;
346 self
->more_menu
->hide_func
= self
->hide_func
;
347 self
->more_menu
->update_func
= self
->update_func
;
348 self
->more_menu
->execute_func
= self
->execute_func
;
349 self
->more_menu
->destroy_func
= self
->destroy_func
;
350 self
->more_menu
->place_func
= self
->place_func
;
355 static void menu_destroy_hash_value(ObMenu
*self
)
357 /* make sure its not visible */
362 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
365 menu_frame_hide_all();
369 if (self
->destroy_func
)
370 self
->destroy_func(self
, self
->data
);
372 menu_clear_entries(self
);
375 g_free(self
->execute
);
376 g_free(self
->more_menu
);
381 void menu_free(ObMenu
*menu
)
384 g_hash_table_remove(menu_hash
, menu
->name
);
387 void menu_show(gchar
*name
, gint x
, gint y
, gint button
, ObClient
*client
)
392 if (!(self
= menu_from_name(name
))
393 || keyboard_interactively_grabbed()) return;
395 /* if the requested menu is already the top visible menu, then don't
397 if (menu_frame_visible
) {
398 frame
= menu_frame_visible
->data
;
399 if (frame
->menu
== self
)
403 menu_frame_hide_all();
405 frame
= menu_frame_new(self
, 0, client
);
406 if (!menu_frame_show_topmenu(frame
, x
, y
, button
))
407 menu_frame_free(frame
);
409 /* select the first entry if it's not a submenu and we opened
410 * the menu with the keyboard, and skip all headers */
411 GList
*it
= frame
->entries
;
413 ObMenuEntryFrame
*e
= it
->data
;
414 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
415 menu_frame_select(frame
, e
, FALSE
);
417 } else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
418 it
= g_list_next(it
);
425 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
431 self
= g_new0(ObMenuEntry
, 1);
438 case OB_MENU_ENTRY_TYPE_NORMAL
:
439 self
->data
.normal
.enabled
= TRUE
;
441 case OB_MENU_ENTRY_TYPE_SUBMENU
:
442 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
449 void menu_entry_ref(ObMenuEntry
*self
)
454 void menu_entry_unref(ObMenuEntry
*self
)
456 if (self
&& --self
->ref
== 0) {
457 switch (self
->type
) {
458 case OB_MENU_ENTRY_TYPE_NORMAL
:
459 g_free(self
->data
.normal
.label
);
460 while (self
->data
.normal
.actions
) {
461 action_unref(self
->data
.normal
.actions
->data
);
462 self
->data
.normal
.actions
=
463 g_slist_delete_link(self
->data
.normal
.actions
,
464 self
->data
.normal
.actions
);
467 case OB_MENU_ENTRY_TYPE_SUBMENU
:
468 g_free(self
->data
.submenu
.name
);
470 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
478 void menu_clear_entries(ObMenu
*self
)
481 /* assert that the menu isn't visible */
486 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
488 g_assert(f
->menu
!= self
);
493 while (self
->entries
) {
494 menu_entry_unref(self
->entries
->data
);
495 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
497 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
500 void menu_entry_remove(ObMenuEntry
*self
)
502 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
503 menu_entry_unref(self
);
506 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, const gchar
*label
,
507 GSList
*actions
, gboolean allow_shortcut
)
511 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
512 e
->data
.normal
.actions
= actions
;
514 menu_entry_set_label(e
, label
, allow_shortcut
);
516 self
->entries
= g_list_append(self
->entries
, e
);
517 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
521 ObMenuEntry
* menu_get_more(ObMenu
*self
, guint show_from
)
524 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, -1);
525 /* points to itself */
526 e
->data
.submenu
.name
= g_strdup(self
->name
);
527 e
->data
.submenu
.submenu
= self
;
528 e
->data
.submenu
.show_from
= show_from
;
532 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, const gchar
*submenu
)
536 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
537 e
->data
.submenu
.name
= g_strdup(submenu
);
539 self
->entries
= g_list_append(self
->entries
, e
);
540 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
544 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
, const gchar
*label
)
548 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
550 menu_entry_set_label(e
, label
, FALSE
);
552 self
->entries
= g_list_append(self
->entries
, e
);
553 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
557 void menu_set_show_func(ObMenu
*self
, ObMenuShowFunc func
)
559 self
->show_func
= func
;
560 self
->more_menu
->show_func
= func
; /* keep it in sync */
563 void menu_set_hide_func(ObMenu
*self
, ObMenuHideFunc func
)
565 self
->hide_func
= func
;
566 self
->more_menu
->hide_func
= func
; /* keep it in sync */
569 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
571 self
->update_func
= func
;
572 self
->more_menu
->update_func
= func
; /* keep it in sync */
575 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
577 self
->execute_func
= func
;
578 self
->more_menu
->execute_func
= func
; /* keep it in sync */
581 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
583 self
->destroy_func
= func
;
584 self
->more_menu
->destroy_func
= func
; /* keep it in sync */
587 void menu_set_place_func(ObMenu
*self
, ObMenuPlaceFunc func
)
589 self
->place_func
= func
;
590 self
->more_menu
->place_func
= func
; /* keep it in sync */
593 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
595 ObMenuEntry
*ret
= NULL
;
598 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
599 ObMenuEntry
*e
= it
->data
;
609 void menu_find_submenus(ObMenu
*self
)
613 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
614 ObMenuEntry
*e
= it
->data
;
616 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
617 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);
621 void menu_entry_set_label(ObMenuEntry
*self
, const gchar
*label
,
622 gboolean allow_shortcut
)
624 switch (self
->type
) {
625 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
626 g_free(self
->data
.separator
.label
);
627 self
->data
.separator
.label
= g_strdup(label
);
629 case OB_MENU_ENTRY_TYPE_NORMAL
:
630 g_free(self
->data
.normal
.label
);
631 self
->data
.normal
.shortcut
=
632 parse_shortcut(label
, allow_shortcut
, &self
->data
.normal
.label
,
633 &self
->data
.normal
.shortcut_position
);
636 g_assert_not_reached();
640 void menu_show_all_shortcuts(ObMenu
*self
, gboolean show
)
642 self
->show_all_shortcuts
= show
;