13 #include "parser/parse.h"
15 static GHashTable
*menu_hash
= NULL
;
17 ObParseInst
*menu_parse_inst
;
19 typedef struct _ObMenuParseState ObMenuParseState
;
21 struct _ObMenuParseState
26 static void menu_clear_entries_internal(ObMenu
*self
);
28 static ObMenu
* menu_from_name(gchar
*name
)
32 g_assert(name
!= NULL
);
34 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
35 g_warning("Attempted to access menu '%s' but it does not exist.",
40 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
43 ObMenuParseState
*state
= data
;
47 if (parse_attr_string("label", node
, &label
)) {
50 for (node
= node
->xmlChildrenNode
; node
; node
= node
->next
)
51 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action"))
52 acts
= g_slist_append(acts
, action_parse(i
, doc
, node
));
53 menu_add_normal(state
->menus
->data
, 0, label
, acts
);
59 static void parse_menu_separator(ObParseInst
*i
,
60 xmlDocPtr doc
, xmlNodePtr node
,
63 ObMenuParseState
*state
= data
;
66 menu_add_separator(state
->menus
->data
, 0);
69 gboolean
menu_open_plugin(ObParseInst
*i
, gchar
*name
, gchar
*plugin
)
73 if (plugin_open(plugin
, i
)) {
75 if (g_hash_table_lookup(menu_hash
, name
))
78 g_warning("Specified plugin '%s' did not provide the "
79 "menu '%s'", plugin
, name
);
84 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
87 ObMenuParseState
*state
= data
;
88 gchar
*name
= NULL
, *title
= NULL
, *plugin
= NULL
;
90 if (!parse_attr_string("id", node
, &name
))
93 if (!g_hash_table_lookup(menu_hash
, name
)) {
94 if (parse_attr_string("plugin", node
, &plugin
)) {
95 menu_open_plugin(i
, name
, plugin
);
97 if (!parse_attr_string("label", node
, &title
))
100 if (menu_new(name
, title
, NULL
)) {
101 state
->menus
= g_slist_prepend(state
->menus
, name
);
102 parse_tree(i
, doc
, node
->xmlChildrenNode
);
103 state
->menus
= g_slist_delete_link(state
->menus
, state
->menus
);
109 menu_add_submenu(state
->menus
->data
, 0, name
);
118 void menu_destroy_hash_value(ObMenu
*self
)
120 /* XXX make sure its not visible */
122 if (self
->destroy_func
)
123 self
->destroy_func(self
, self
->data
);
125 menu_clear_entries_internal(self
);
130 void menu_startup(ObParseInst
*i
)
132 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
133 (GDestroyNotify
)menu_destroy_hash_value
);
134 menu_parse_inst
= parse_startup();
139 parse_shutdown(menu_parse_inst
);
140 menu_parse_inst
= NULL
;
142 menu_frame_hide_all();
143 g_hash_table_destroy(menu_hash
);
147 gboolean
menu_open(gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*node
)
149 gboolean loaded
= TRUE
;
152 p
= g_build_filename(g_get_home_dir(), ".openbox", file
, NULL
);
153 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
155 p
= g_build_filename(RCDIR
, file
, NULL
);
156 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
159 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
160 g_warning("Failed to load menu from '%s'", file
);
171 ObMenuParseState parse_state
;
174 gboolean loaded
= FALSE
;
177 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
178 if (menu_open(it
->data
, &doc
, &node
))
183 loaded
= menu_open("menu", &doc
, &node
);
186 parse_state
.menus
= NULL
;
188 parse_register(menu_parse_inst
, "menu", parse_menu
, &parse_state
);
189 parse_register(menu_parse_inst
, "item", parse_menu_item
, &parse_state
);
190 parse_register(menu_parse_inst
, "separator",
191 parse_menu_separator
, &parse_state
);
192 parse_tree(menu_parse_inst
, doc
, node
->xmlChildrenNode
);
196 ObMenu
* menu_new(gchar
*name
, gchar
*title
, gpointer data
)
200 /*if (g_hash_table_lookup(menu_hash, name)) return FALSE;*/
202 self
= g_new0(ObMenu
, 1);
203 self
->name
= g_strdup(name
);
204 self
->title
= g_strdup(title
);
207 g_hash_table_replace(menu_hash
, self
->name
, self
);
212 void menu_free(gchar
*name
)
216 if (!(self
= menu_from_name(name
))) return;
217 g_hash_table_remove(menu_hash
, self
->name
);
220 void menu_show(gchar
*name
, gint x
, gint y
, ObClient
*client
)
225 if (!(self
= menu_from_name(name
))) return;
227 frame
= menu_frame_new(self
, client
);
228 menu_frame_move(frame
, x
, y
);
229 menu_frame_show(frame
, NULL
);
232 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
238 self
= g_new0(ObMenuEntry
, 1);
244 case OB_MENU_ENTRY_TYPE_NORMAL
:
245 self
->data
.normal
.enabled
= TRUE
;
247 case OB_MENU_ENTRY_TYPE_SUBMENU
:
248 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
255 static void menu_entry_free(ObMenuEntry
*self
)
258 switch (self
->type
) {
259 case OB_MENU_ENTRY_TYPE_NORMAL
:
260 g_free(self
->data
.normal
.label
);
261 while (self
->data
.normal
.actions
) {
262 action_free(self
->data
.normal
.actions
->data
);
263 self
->data
.normal
.actions
=
264 g_slist_delete_link(self
->data
.normal
.actions
,
265 self
->data
.normal
.actions
);
268 case OB_MENU_ENTRY_TYPE_SUBMENU
:
269 g_free(self
->data
.submenu
.name
);
271 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
279 void menu_clear_entries(gchar
*name
)
283 if (!(self
= menu_from_name(name
))) return;
285 menu_clear_entries_internal(self
);
288 static void menu_clear_entries_internal(ObMenu
*self
)
290 /* XXX assert that the menu isn't visible */
292 while (self
->entries
) {
293 menu_entry_free(self
->entries
->data
);
294 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
298 ObMenuEntry
* menu_add_normal(gchar
*name
, gint id
, gchar
*label
,
304 if (!(self
= menu_from_name(name
))) return;
306 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
307 e
->data
.normal
.label
= g_strdup(label
);
308 e
->data
.normal
.actions
= actions
;
310 self
->entries
= g_list_append(self
->entries
, e
);
314 ObMenuEntry
* menu_add_submenu(gchar
*name
, gint id
, gchar
*submenu
)
319 if (!(self
= menu_from_name(name
))) return;
321 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
322 e
->data
.submenu
.name
= g_strdup(submenu
);
324 self
->entries
= g_list_append(self
->entries
, e
);
328 ObMenuEntry
* menu_add_separator(gchar
*name
, gint id
)
333 if (!(self
= menu_from_name(name
))) return;
335 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
337 self
->entries
= g_list_append(self
->entries
, e
);
341 void menu_set_update_func(gchar
*name
, ObMenuUpdateFunc func
)
345 if (!(self
= menu_from_name(name
))) return;
346 self
->update_func
= func
;
349 void menu_set_execute_func(gchar
*name
, ObMenuExecuteFunc func
)
353 if (!(self
= menu_from_name(name
))) return;
354 self
->execute_func
= func
;
357 void menu_set_destroy_func(gchar
*name
, ObMenuDestroyFunc func
)
361 if (!(self
= menu_from_name(name
))) return;
362 self
->destroy_func
= func
;
365 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
367 ObMenuEntry
*ret
= NULL
;
370 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
371 ObMenuEntry
*e
= it
->data
;
381 void menu_find_submenus(ObMenu
*self
)
385 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
386 ObMenuEntry
*e
= it
->data
;
388 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
389 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);