I'm trying to write a small program using gtk+-2.4 but I'm having some problems with gtk_ui_manager. I can get the initial menus to display, but I'm having trouble changing the menus while the program is running. Here's some sample code:
Code:
#include <gtk/gtk.h>
int main(int argc, char **argv)
{
GtkUIManager *ui;
GtkActionGroup *menuactions;
GtkAction *action;
GtkWidget *main_window;
GtkWidget *menubar;
GError *error = NULL;
gtk_init(&argc, &argv);
ui = gtk_ui_manager_new();
menuactions = gtk_action_group_new("MenuActions");
action = gtk_action_new("MenuAction", "Menu", NULL, NULL);
gtk_action_group_add_action(menuactions, action);
action = gtk_action_new("SubMenuAction", "Submenu", NULL, NULL);
gtk_action_group_add_action(menuactions, action);
action = gtk_action_new("ItemAction", "Item", NULL, NULL);
gtk_action_group_add_action(menuactions, action);
action = gtk_action_new("ItemAction2", "Item2", NULL, NULL);
gtk_action_group_add_action(menuactions, action);
gtk_ui_manager_insert_action_group(ui, menuactions, 1);
gtk_ui_manager_add_ui_from_file(ui, "menus.xml", &error);
gtk_ui_manager_add_ui(ui, gtk_ui_manager_new_merge_id(ui),
"/ui/menubar/File/SubMenu", "Item2", "ItemAction2",
GTK_UI_MANAGER_MENUITEM, TRUE);
//gtk_ui_manager_add_ui_from_file(ui, "moremenus.xml", &error);
menubar = gtk_ui_manager_get_widget(ui, "/menubar");
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(main_window), menubar);
gtk_widget_show_all(main_window);
gtk_main();
}
And here is the menus.xml file:
Code:
<ui>
<menubar>
<menu name="Menu" action="MenuAction">
<menu name="SubMenu" action="SubMenuAction">
<menuitem name="MenuItem" action="ItemAction" />
</menu>
</menu>
</menubar>
</ui>
And the moremenus.xml file:
Code:
<ui>
<menubar>
<menu name="Menu" action="MenuAction">
<menu name="SubMenu" action="SubMenuAction">
<menuitem name="Item2" action="ItemAction2" />
</menu>
</menu>
</menubar>
</ui>
The problem is that Item2 doesn't appear in the Submenu. If I delete the gtk_ui_manager_add_ui() function call and uncomment the gtk_ui_manager_add_ui_from_file() call, it works. But its really inconvenient for me to have to specify all the menus in a file - I want to be able to insert menu items using a pathname. Does anyone know how to use gtk_ui_manager_add_ui() properly?