I have an Demo code with an Item using the a gboolean
state used for a check menu.
The code works fine, but in this context ( the way the code is ):
#include <gtk/gtk.h>
static void check_clbk ( GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, G_GNUC_UNUSED gpointer user_data )
{
GVariant *action_state;
action_state = g_action_get_state ( G_ACTION ( action ) );
const gboolean flag = g_variant_get_boolean ( action_state );
if ( flag )
{
g_print ( "OFF\n" );
g_simple_action_set_state ( action, g_variant_new_boolean ( FALSE ) );
}
else
{
g_print ( "ON\n" );
g_simple_action_set_state ( action, g_variant_new_boolean ( TRUE ) );
}
}
static void create_actions ( GtkApplication *const application )
{
static const GActionEntry actions[] =
{
{ "checkMenu", check_clbk, NULL, "true", NULL, { 0, 0, 0 } },
};
g_action_map_add_action_entries ( G_ACTION_MAP ( application ), actions, G_N_ELEMENTS ( actions ), application );
}
static void create_menu ( GMenu *const main_menu, const gchar *const menu_name )
{
GMenu *edit_menu;
GMenuItem *item;
edit_menu = g_menu_new();
item = g_menu_item_new ( "Check", "app.checkMenu" );
g_menu_item_set_action_and_target ( item, "app.checkMenu", NULL, "true", NULL );
g_menu_append_item ( edit_menu, item );
g_object_unref ( item );
g_menu_append_submenu ( main_menu, menu_name, G_MENU_MODEL ( edit_menu ) );
g_object_unref ( edit_menu );
}
static void startup ( GtkApplication *const application )
{
GMenu *menu;
menu = g_menu_new();
/// *** Create the Menu
create_menu ( menu, "Edit" );
/// ***
gtk_application_set_menubar ( application, G_MENU_MODEL ( menu ) );
/// ***
create_actions ( application );
g_object_unref ( menu );
}
static void activate ( GtkApplication *const application )
{
GtkWidget *window;
window = gtk_application_window_new ( application );
gtk_window_set_application ( GTK_WINDOW ( window ), application );
gtk_window_set_title ( GTK_WINDOW ( window ), "GMenu" );
gtk_widget_show_all ( window );
}
gint main ( gint argc, gchar **argv )
{
GtkApplication *application;
gint status;
application = gtk_application_new ( "GMenu", G_APPLICATION_FLAGS_NONE );
g_signal_connect_swapped ( application, "startup", G_CALLBACK ( startup ), application );
g_signal_connect_swapped ( application, "activate", G_CALLBACK ( activate ), application );
status = g_application_run ( G_APPLICATION ( application ), argc, argv );
g_object_unref ( application );
return status;
}
It works fine if the action was created with change_state
set to NULL and activate
pointed to check_clbk:
{ "checkMenu", check_clbk, NULL, "true", NULL, { 0, 0, 0 } },
and also works fine, if the action was created with activate
set to NULL and change_state
pointed to check_clbk
:
{ "checkMenu", NULL, NULL, "true", check_clbk, { 0, 0, 0 } }
How should I treat/understand this situation ?