De-/activate g_menu_item with visible checkmark

Hi!
I am struggling with a g_menu. I got it to work but I still cannot solve an issue:
I build a simple g_menu, with stateful actions to get checkmarks, but then, depending on application state I need to deactivate some entries - the equivalent of gtk_widget_set_sensitive. I can do that by using the function g_action_map_remove_action, but the problem is, the checkmarks are not visible anymore.
How could I do this the right way?

Here is part of the code I use:

	GtkWidget		*widget, *popover;
	GMenu			*menu, *section;
	GMenuItem		*item;
	GVariant		*variant;
	
	variant = g_variant_new_boolean (0);
	menu = g_menu_new ();
	
	action_pattern = g_simple_action_new_stateful ("test_pattern", NULL, variant);
	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action_pattern));
	g_signal_connect (action_pattern, "activate", G_CALLBACK (Menu_Test_Pattern_Action), 
			G_ACTION (action_pattern));
	item = g_menu_item_new ("Test Pattern", "app.test_pattern");
	g_menu_append_item (menu, item);
	g_object_unref (item);
	
	section = g_menu_new ();
	g_menu_append_section (menu, "Image Flip", G_MENU_MODEL (section) );
	
	action_flip_h = g_simple_action_new_stateful ("flip_h", NULL, variant);
	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action_flip_h));
	g_signal_connect (action_flip_h, "activate", G_CALLBACK (Menu_Flip_H_Action), 
			G_ACTION (action_flip_h));
	item = g_menu_item_new ("Flip Image H", "app.flip_h");
	g_menu_append_item (section, item);
	g_object_unref (item);

	action_flip_v = g_simple_action_new_stateful ("flip_v", NULL, variant);
	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action_flip_v));
	g_signal_connect (action_flip_v, "activate", G_CALLBACK (Menu_Flip_V_Action), 
			G_ACTION (action_flip_v));
	item = g_menu_item_new ("Flip Image V", "app.flip_v");
	g_menu_append_item (section, item);
	g_object_unref (item);

	g_menu_freeze (section);
	g_object_unref (section);
	
	g_menu_freeze (menu);
	g_object_unref (menu);

And then, when I need to de-/activate the entries, I use:

	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action_pattern));
	g_action_map_remove_action (G_ACTION_MAP (app), "flip_h");
	g_action_map_remove_action (G_ACTION_MAP (app), "flip_v");

Thank you!

Use g_simple_action_set_enabled().

Removing the action results in a broken menu item, and while some of the handling matches what you want (namely: making the item insensitive), it’s still broken (as you noticed by the missing state).

Thank you for the answer. Yes, now it’s not broken, but while an action is disabled the checkmark is not visible anymore. So, the user cannot tell the status of it - checked or unchecked.
Is there a solution for this or do I have to build the menu popover with labels and buttons like in GTK3?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.