Wednesday, October 22, 2008

How to add a GtkUiManager menubar to a hildon window

Some time ago i found a very cool tip from this blog ( a simple and easy function to convert ftom a gtkuimanager menubar to a gtkmenu. We need to do this because hildon_window_set_menu requires a GtkMenu. Let see the source:

static GtkWidget *
menubar_to_menu (GtkUIManager *ui_manager) {
GtkWidget *main_menu;
GtkWidget *menubar;
GList *iter;

/* Create new main menu */
main_menu = gtk_menu_new();

/* Get the menubar from the UI manager */
menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");

iter = gtk_container_get_children (GTK_CONTAINER (menubar));
while (iter) {
GtkWidget *menu;

menu = GTK_WIDGET (iter->data);
gtk_widget_reparent(menu, main_menu);

iter = g_list_next (iter);
return main_menu;

And you can use it this way:

ui = gtk_ui_manager_new ();
hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar_to_menu (ui)));

No comments: