Menu items are getting disabled when i supply an action

Hi all
i don’t know even if this is the right way to use menus, but when i associate an action with menu item it turns disabled, is it the default behavior or i did something wrong?
menus.ui:
https://0bin.net/paste/+fg5rdeJq-zJ819Y#OBYe-JY9z98XMD+GZb8lnRVjwqAZP2TsG9jopghIE/w
(because can’t paste xml here)
source file:

GtkWidget *
seafpad_app_window_new (SeafpadApp *app)
{
g_assert (SEAFPAD_IS_APP (app));
GtkWidget *window = g_object_new (SEAFPAD_TYPE_APP_WINDOW,
“application”, GTK_APPLICATION (app),
NULL);

const GActionEntry entries[] = {{ “win.new”, window_new_file },
{ “win.open”, window_new_file }};

GtkBuilder *builder = gtk_builder_new_from_resource ("/org/abuashraf/seafpad/gtk/menus.ui");

g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), app);

gtk_application_set_menubar (GTK_APPLICATION (app),
G_MENU_MODEL (gtk_builder_get_object (builder,
“menubar”)));
return GTK_WIDGET (window);
}

(using 3.28 on ubuntu 18.04)

You are adding the actions to the application, therefore, the actions will be app.win.new. You need to remove win. prefixes from entries and call g_action_map_add_action_entries on GtkApplicationWindow.

i’ve modified the code as following :

GtkWidget *
seafpad_app_window_new (SeafpadApp *app)
{
  g_assert (SEAFPAD_IS_APP (app));
  GtkWidget *window = g_object_new (SEAFPAD_TYPE_APP_WINDOW,
				    "application", GTK_APPLICATION (app),
				    NULL);
  
  const GActionEntry entries[] = {{ "new", window_new_file },
  				  { "open", window_new_file }};
 
  GtkBuilder *builder = gtk_builder_new_from_resource ("/org/abuashraf/seafpad/gtk/menus.ui");

  
  
  g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), app);

  gtk_application_set_menubar (GTK_APPLICATION (app),
			       G_MENU_MODEL (gtk_builder_get_object (builder,
								     "menubar")));
  return GTK_WIDGET (window);
}

ui file:
https://0bin.net/paste/+jkyI2mQ35qbMWck#20HbVGUpyogwV-9/eDaBdDwoqr+Cxn63Ur+LIwBmldJ

it also doesn’t work, is that what did you mean, jtojnar?

You missed adding it to window: g_action_map_add_action_entries (G_ACTION_MAP (window), …)

thanks jtojnar, it works pretty !
BTW, i wonder where is the usage of Application based action-map, could i use it also with menus or it has another use?

Yes, you can use the actions from applicationʼs action map just like the ones from windowʼs – just use the app. prefix.

Generally, you will want to have application-wide actions like quitting, opening a new window, opening preferences associated with your application, and window specific ones like navigation or opening a new tab (if your window has tabs). The actions are kind of indirection that roughly corresponds to methods in OOP so criteria similar to ones for adding methods to classes should be used for attaching actions to maps.

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