+ *position = 0;
+ *always_show = FALSE;
+
+ g_assert(strippedlabel != NULL);
+
+ if (label == NULL) {
+ *strippedlabel = NULL;
+ } else {
+ gchar *i;
+
+ *strippedlabel = g_strdup(label);
+
+ /* if allow_shortcut is false, then you can't use the '_', instead you
+ have to just use the first valid character
+ */
+
+ i = strchr(*strippedlabel, '_');
+ if (allow_shortcut && i != NULL) {
+ /* there is an underscore in the string */
+
+ /* you have to use a printable ascii character for shortcuts
+ don't allow space either, so you can have like "a _ b"
+ */
+ if (VALID_SHORTCUT(*(i+1))) {
+ shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
+ *position = i - *strippedlabel;
+ *always_show = TRUE;
+
+ /* remove the '_' from the string */
+ for (; *i != '\0'; ++i)
+ *i = *(i+1);
+ } else if (*(i+1) == '\0') {
+ /* no default shortcut if the '_' is the last character
+ (eg. "Exit_") for menu entries that you don't want
+ to be executed by mistake
+ */
+ *i = '\0';
+ }
+ } else {
+ /* there is no underscore, so find the first valid character to use
+ instead */
+
+ for (i = *strippedlabel; *i != '\0'; ++i)
+ if (VALID_SHORTCUT(*i)) {
+ *position = i - *strippedlabel;
+ shortcut = g_unichar_tolower(g_utf8_get_char(i));
+ break;
+ }
+ }
+ }
+ return shortcut;
+}
+
+static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
+ gpointer data)
+{
+ ObMenuParseState *state = data;
+ gchar *label;
+
+ if (state->parent) {
+ if (parse_attr_string("label", node, &label)) {
+ GSList *acts = NULL;
+
+ for (node = node->children; node; node = node->next)
+ if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
+ ObActionsAct *a = actions_parse(i, doc, node);
+ if (a)
+ acts = g_slist_append(acts, a);
+ }
+ menu_add_normal(state->parent, -1, label, acts, TRUE);
+ g_free(label);
+ }
+ }
+}
+
+static void parse_menu_separator(ObParseInst *i,
+ xmlDocPtr doc, xmlNodePtr node,
+ gpointer data)
+{
+ ObMenuParseState *state = data;
+
+ if (state->parent) {
+ gchar *label;
+
+ if (!parse_attr_string("label", node, &label))
+ label = NULL;
+
+ menu_add_separator(state->parent, -1, label);
+ g_free(label);
+ }
+}
+
+static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
+ gpointer data)
+{
+ ObMenuParseState *state = data;
+ gchar *name = NULL, *title = NULL, *script = NULL;
+ ObMenu *menu;
+
+ if (!parse_attr_string("id", node, &name))
+ goto parse_menu_fail;
+
+ if (!g_hash_table_lookup(menu_hash, name)) {
+ if (!parse_attr_string("label", node, &title))
+ goto parse_menu_fail;
+
+ if ((menu = menu_new(name, title, TRUE, NULL))) {
+ menu->pipe_creator = state->pipe_creator;
+ if (parse_attr_string("execute", node, &script)) {
+ menu->execute = parse_expand_tilde(script);
+ } else {
+ ObMenu *old;
+
+ old = state->parent;
+ state->parent = menu;
+ parse_tree(i, doc, node->children);
+ state->parent = old;
+ }
+ }
+ }
+
+ if (state->parent)
+ menu_add_submenu(state->parent, -1, name);
+
+parse_menu_fail:
+ g_free(name);
+ g_free(title);
+ g_free(script);
+}
+
+ObMenu* menu_new(const gchar *name, const gchar *title,
+ gboolean allow_shortcut_selection, gpointer data)
+{
+ ObMenu *self;
+
+ self = g_new0(ObMenu, 1);
+ self->name = g_strdup(name);
+ self->data = data;
+
+ self->shortcut = parse_shortcut(title, allow_shortcut_selection,
+ &self->title, &self->shortcut_position,
+ &self->shortcut_always_show);
+
+ g_hash_table_replace(menu_hash, self->name, self);
+
+ /* Each menu has a single more_menu. When the menu spills past what
+ can fit on the screen, a new menu frame entry is created from this
+ more_menu, and a new menu frame for the submenu is created for this
+ menu, also pointing to the more_menu.
+
+ This can be done multiple times using the same more_menu.
+
+ more_menu->more_menu will always be NULL, since there is only 1 for
+ each menu. */
+ self->more_menu = g_new0(ObMenu, 1);
+ self->more_menu->name = _("More...");
+ self->more_menu->title = _("More...");
+ self->more_menu->data = data;
+ self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
+
+ self->more_menu->show_func = self->show_func;
+ self->more_menu->hide_func = self->hide_func;
+ self->more_menu->update_func = self->update_func;
+ self->more_menu->execute_func = self->execute_func;
+ self->more_menu->destroy_func = self->destroy_func;
+ self->more_menu->place_func = self->place_func;
+
+ return self;
+}
+
+static void menu_destroy_hash_value(ObMenu *self)
+{
+ /* make sure its not visible */
+ {
+ GList *it;
+ ObMenuFrame *f;
+
+ for (it = menu_frame_visible; it; it = g_list_next(it)) {
+ f = it->data;
+ if (f->menu == self)
+ menu_frame_hide_all();
+ }
+ }
+
+ if (self->destroy_func)
+ self->destroy_func(self, self->data);
+
+ menu_clear_entries(self);
+ g_free(self->name);
+ g_free(self->title);
+ g_free(self->execute);
+ g_free(self->more_menu);
+
+ g_free(self);
+}