Can anyone figure out why the popup menu items are disabled in this C++
example, in the gmenu branch of gtkmm-documentation:
https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/menus/popup/examplewindow.cc
but not in the attached C example.
--
Murray Cumming
[email protected]
www.murrayc.com
www.openismus.com
#include <gtk/gtk.h>
const char* ui =
"<interface>"
" <menu id='menu-somemenu'>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>SomeThing</attribute>"
" <attribute name='action'>somemenu.something</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>OtherThing</attribute>"
" <attribute name='action'>somemenu.otherthing</attribute>"
" </item>"
" </section>"
" </menu>"
"</interface>";
static GtkWidget *menu = NULL;
static void
on_popup_item (GSimpleAction *action, GVariant* parameter, gpointer user_data)
{
g_warning ("%s called.\n", G_STRFUNC);
}
static GActionEntry action_entries[] = {
{ "something", on_popup_item },
{ "otherthing", on_popup_item }
};
static gboolean
on_window_delete_event (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
return FALSE;
}
static void
on_window_destroy (GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
static gboolean
on_button_press_event (GtkWidget* widget, GdkEventButton* event, gpointer user_data)
{
if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
{
gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
event->button, event->time);
return TRUE; /* Handled. */
}
return FALSE; /* Not handled. */
}
int
main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
g_signal_connect (window, "delete-event",
G_CALLBACK (on_window_delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (on_window_destroy), NULL);
GtkBuilder *builder = gtk_builder_new();
GError* error = NULL;
gtk_builder_add_from_string (builder, ui, -1, &error);
g_assert_no_error (error);
GMenu* gmenu = G_MENU (gtk_builder_get_object (builder, "menu-somemenu"));
g_assert (gmenu);
menu = gtk_menu_new_from_model (G_MENU_MODEL (gmenu));
g_assert (menu);
/* This doesn't work, so we must use the SimpleActionGroup to specify a callback instead: */
/*
GMenuItem* menu_item = G_MENU_ITEM (gtk_builder_get_object (builder,
"something"));
g_assert (menu_item);
*/
GSimpleActionGroup *action_group = g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP (action_group),
action_entries,
G_N_ELEMENTS (action_entries),
NULL);
gtk_widget_insert_action_group(GTK_WIDGET (window), "somemenu", G_ACTION_GROUP (action_group));
gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (window), NULL);
g_signal_connect (window, "button-press-event",
G_CALLBACK (on_button_press_event), NULL);
gtk_main ();
g_object_unref (G_OBJECT (builder));
g_object_unref (G_OBJECT (action_group));
return 0;
}
_______________________________________________
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list