Re: <menu> under <toolitem> in ui



On Wed, 29 Jul 2009, Allin Cottrell wrote:

I'm trying to get a button on a toolbar to produce a menu.
The GtkUIManager docs indicate this can be done but I haven't got
the details right yet and I wonder if someone can put me on the
right track...

I've constructed a minimal test case (appended below).  Hopefully
someone who's familiar with the GtkUIManager API will see what I'm
doing wrong. I'm trying to comply with this in the docs: "Note
that toolitem elements may contain a menu element, but only if
their associated action specifies a GtkMenuToolButton as proxy."

Allin Cottrell

minimal GTK+ program:

#include <gtk/gtk.h>

const gchar *ui =
    "<ui>"
    " <toolbar>"
    "  <toolitem action='IndexButton'>"
    "   <menu action='Menu'>"
    "    <menuitem action='Dummy'/>"
    "    <menuitem action='Quit'/>"
    "   </menu>"
    "  </toolitem>"
    "  <toolitem action='Close'/>"
    " </toolbar>"
    "</ui>";

GtkActionEntry toolbar_items[] = {
    { "IndexButton", GTK_STOCK_INDEX, NULL, NULL, NULL, NULL },
    { "Menu", NULL, NULL, NULL, NULL, NULL },
    { "Dummy", NULL, NULL, NULL, "Nothing", NULL },
    { "Quit", GTK_STOCK_DELETE, NULL, NULL, NULL, gtk_main_quit },
    { "Close", GTK_STOCK_CLOSE, NULL, NULL, NULL, gtk_main_quit }
};

/* I expect: the first button on the toolbar will pop down a
   menu with two items.

   I get: the first button does nothing, plus an error message:

   Gtk-CRITICAL **: gtk_menu_tool_button_set_menu: assertion
   `GTK_IS_MENU_TOOL_BUTTON (button)' failed
*/

GtkWidget *make_toolbar (void)
{
    GtkActionGroup *actions;
    GtkAction *action;
    GtkToolItem *proxy;
    GtkUIManager *mgr;
    GError *err = NULL;

    actions = gtk_action_group_new("Actions");
    gtk_action_group_add_actions(actions, toolbar_items,
                                 G_N_ELEMENTS(toolbar_items),
                                 NULL);

    action = gtk_action_group_get_action(actions, "IndexButton");
    proxy = gtk_menu_tool_button_new(NULL, NULL);
    gtk_action_connect_proxy(action, GTK_WIDGET(proxy));

    mgr = gtk_ui_manager_new();
    gtk_ui_manager_insert_action_group(mgr, actions, 0);
    g_object_unref(actions);

    gtk_ui_manager_add_ui_from_string(mgr, ui, -1, &err);
    gtk_ui_manager_ensure_update(mgr);

    if (err != NULL) {
        /* this is not triggered */
        fprintf(stderr, "building toolbar failed: %s",
                err->message);
        g_error_free(err);
    }

    return gtk_ui_manager_get_widget(mgr, "/toolbar");
}

int main (int argc, char **argv)
{
    GtkWidget *w;

    gtk_init(&argc, &argv);

    w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(w), 80, -1);

    g_signal_connect(G_OBJECT(w), "destroy",
                     gtk_main_quit, NULL);

    gtk_container_add(GTK_CONTAINER(w), make_toolbar());
    gtk_widget_show_all(w);

    gtk_main();

    return 0;
}




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