Freeing menus added at runtime in a GtkBuilder project



Hi all,

I'm building an application using GtkBuilder and I'd like to add some menu options at runtime--for plugins, etc. I'd just like to ensure I'm cleaning everything up properly since this is my first shot at doing any major GTK+ code.

Here's a slim example of what I'm doing (minus some error checking):


GtkActionGroup      *action_group = NULL;
GtkAction                *action = NULL;
GtkUIManager        *ui_manager = NULL;
guint                        *merge_id;

gtk_init(&argc, &argv);

// Get a builder
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "gtk_iface.xml", &err);

// Grab the ui manager
ui_manager = gtk_builder_get_object (builder, "uimanager1");

// Add an action group
action_group = gtk_action_group_new("plugin_actions");
gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);

// Add an action
action = gtk_action_new("plugin_action1", "Plugin Name", NULL, NULL);
gtk_action_group_add_action_with_accel(action_group, action, "");

// Add a callback for the action
g_signal_connect(G_OBJECT(action), "activate", G_CALLBACK(plugin_callback), NULL);

// Add the plugin to the existing plugins menu (defined in the xml file)
merge_id = gtk_ui_manager_new_merge_id(ui_manager);
gtk_ui_manager_add_ui(ui_manager, merge_id, "/ui/menubar1/plugins",
       "plugin1", "plugin_action1", GTK_UI_MANAGER_MENUITEM, FALSE);


This code seems sufficient to get an item into the menu, so presumably I can add some loops and get the plugins loaded. Now that I've merged these menu items into the existing ui_manager, what kind of clean-up is necessary?

Does this need to be explicit...

gtk_ui_manager_remove_ui(ui_manager, merge_id);
gtk_action_group_remove_action(action_group, action);
gtk_ui_manager_remove_action_group(ui_manager, action_group);

... or will destroying the toplevel window take care of that for me?

I'm thinking I need at least this much:

g_object_unref(action);
g_object_unref(action_group);
// and this, of course:
g_object_unref(builder);

... but I'm wondering if unrefing the builder or destroying the window might handle that all ready.

Any enlightenment would be greatly appreciated.

Thanks,
Sean



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]