+ g_free(self);
+}
+
+void menu_free(ObMenu *menu)
+{
+ if (menu)
+ g_hash_table_remove(menu_hash, menu->name);
+}
+
+static gboolean menu_hide_delay_func(gpointer data)
+{
+ menu_can_hide = TRUE;
+ return FALSE; /* no repeat */
+}
+
+void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
+{
+ ObMenu *self;
+ ObMenuFrame *frame;
+
+ if (!(self = menu_from_name(name)) ||
+ grab_on_keyboard() || grab_on_pointer()) return;
+
+ /* if the requested menu is already the top visible menu, then don't
+ bother */
+ if (menu_frame_visible) {
+ frame = menu_frame_visible->data;
+ if (frame->menu == self)
+ return;
+ }
+
+ menu_frame_hide_all();
+
+ /* clear the pipe menus when showing a new menu */
+ menu_clear_pipe_caches();
+
+ frame = menu_frame_new(self, 0, client);
+ if (!menu_frame_show_topmenu(frame, x, y, mouse))
+ menu_frame_free(frame);
+ else {
+ if (!mouse) {
+ /* select the first entry if it's not a submenu and we opened
+ * the menu with the keyboard, and skip all headers */
+ GList *it = frame->entries;
+ while (it) {
+ ObMenuEntryFrame *e = it->data;
+ if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
+ menu_frame_select(frame, e, FALSE);
+ break;
+ } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
+ it = g_list_next(it);
+ else
+ break;
+ }
+ }
+
+ /* reset the hide timer */
+ if (!mouse)
+ menu_can_hide = TRUE;
+ else {
+ menu_can_hide = FALSE;
+ ob_main_loop_timeout_add(ob_main_loop,
+ config_menu_hide_delay * 1000,
+ menu_hide_delay_func,
+ NULL, g_direct_equal, NULL);
+ }
+ }
+}
+
+gboolean menu_hide_delay_reached(void)
+{
+ return menu_can_hide;
+}
+
+static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
+{
+ ObMenuEntry *self;
+
+ g_assert(menu);
+
+ self = g_new0(ObMenuEntry, 1);
+ self->ref = 1;
+ self->type = type;
+ self->menu = menu;
+ self->id = id;
+
+ switch (type) {
+ case OB_MENU_ENTRY_TYPE_NORMAL:
+ self->data.normal.enabled = TRUE;
+ break;
+ case OB_MENU_ENTRY_TYPE_SUBMENU:
+ case OB_MENU_ENTRY_TYPE_SEPARATOR:
+ break;
+ }
+
+ return self;
+}
+
+void menu_entry_ref(ObMenuEntry *self)
+{
+ ++self->ref;
+}
+
+void menu_entry_unref(ObMenuEntry *self)
+{
+ if (self && --self->ref == 0) {
+ switch (self->type) {
+ case OB_MENU_ENTRY_TYPE_NORMAL:
+ RrImageUnref(self->data.normal.icon);
+ g_free(self->data.normal.label);
+ while (self->data.normal.actions) {
+ actions_act_unref(self->data.normal.actions->data);
+ self->data.normal.actions =
+ g_slist_delete_link(self->data.normal.actions,
+ self->data.normal.actions);
+ }
+ break;
+ case OB_MENU_ENTRY_TYPE_SUBMENU:
+ g_free(self->data.submenu.name);
+ break;
+ case OB_MENU_ENTRY_TYPE_SEPARATOR:
+ g_free(self->data.separator.label);
+ break;
+ }
+
+ g_free(self);
+ }
+}
+
+void menu_clear_entries(ObMenu *self)
+{
+#ifdef DEBUG
+ /* assert that the menu isn't visible */
+ {
+ GList *it;
+ ObMenuFrame *f;
+
+ for (it = menu_frame_visible; it; it = g_list_next(it)) {
+ f = it->data;
+ g_assert(f->menu != self);
+ }
+ }
+#endif
+
+ while (self->entries) {
+ menu_entry_unref(self->entries->data);
+ self->entries = g_list_delete_link(self->entries, self->entries);
+ }
+ self->more_menu->entries = self->entries; /* keep it in sync */
+}
+
+void menu_entry_remove(ObMenuEntry *self)
+{
+ self->menu->entries = g_list_remove(self->menu->entries, self);
+ menu_entry_unref(self);
+}
+
+ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
+ GSList *actions, gboolean allow_shortcut)
+{
+ ObMenuEntry *e;
+
+ e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
+ e->data.normal.actions = actions;
+
+ menu_entry_set_label(e, label, allow_shortcut);
+
+ self->entries = g_list_append(self->entries, e);
+ self->more_menu->entries = self->entries; /* keep it in sync */
+ return e;
+}
+
+ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
+{
+ ObMenuEntry *e;
+ e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
+ /* points to itself */
+ e->data.submenu.name = g_strdup(self->name);
+ e->data.submenu.submenu = self;
+ e->data.submenu.show_from = show_from;
+ return e;
+}
+
+ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
+{
+ ObMenuEntry *e;
+
+ e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
+ e->data.submenu.name = g_strdup(submenu);
+
+ self->entries = g_list_append(self->entries, e);
+ self->more_menu->entries = self->entries; /* keep it in sync */
+ return e;
+}
+
+ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
+{
+ ObMenuEntry *e;
+
+ e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
+
+ menu_entry_set_label(e, label, FALSE);
+
+ self->entries = g_list_append(self->entries, e);
+ self->more_menu->entries = self->entries; /* keep it in sync */
+ return e;
+}
+
+void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
+{
+ self->show_func = func;
+}
+
+void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
+{
+ self->hide_func = func;
+}
+
+void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
+{
+ self->update_func = func;
+}
+
+void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
+{
+ self->execute_func = func;
+ self->more_menu->execute_func = func; /* keep it in sync */
+}
+
+void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
+{
+ self->destroy_func = func;
+}
+
+void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
+{
+ self->place_func = func;
+}
+
+ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
+{
+ ObMenuEntry *ret = NULL;
+ GList *it;
+
+ for (it = self->entries; it; it = g_list_next(it)) {
+ ObMenuEntry *e = it->data;
+
+ if (e->id == id) {
+ ret = e;
+ break;
+ }
+ }
+ return ret;
+}
+
+void menu_find_submenus(ObMenu *self)
+{
+ GList *it;
+
+ for (it = self->entries; it; it = g_list_next(it)) {
+ ObMenuEntry *e = it->data;
+
+ if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
+ e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
+ }
+}
+
+void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
+ gboolean allow_shortcut)
+{
+ switch (self->type) {
+ case OB_MENU_ENTRY_TYPE_SEPARATOR:
+ g_free(self->data.separator.label);
+ self->data.separator.label = g_strdup(label);
+ break;
+ case OB_MENU_ENTRY_TYPE_NORMAL:
+ g_free(self->data.normal.label);
+ self->data.normal.shortcut =
+ parse_shortcut(label, allow_shortcut, &self->data.normal.label,
+ &self->data.normal.shortcut_position,
+ &self->data.normal.shortcut_always_show);
+ break;
+ default:
+ g_assert_not_reached();
+ }
+}
+
+void menu_show_all_shortcuts(ObMenu *self, gboolean show)
+{
+ self->show_all_shortcuts = show;