Dear All,
thanks in advance for reading me, I am migrating a Gtk3 app to Gtk4 and I am facing some issues,
first I had troubles to switch from the GtkMenu
to the new menu format, but I got lot of help here , thanks !
So I managed to re-create my the menus of my application, and attach actions, so far like this:
The main.ui
XML file:
<interface>
<menu id="menu_bar">
<item>
<attribute name="label" translatable="yes">Open</attribute>
<attribute name="action">app.open</attribute>
<attribute name="accel"><Control>O</attribute>
</item>
</menu>
</interface>
And the corresponding code:
void do_something_with_my_struct (gpointer data)
{
my_struct * user_data = (my_struct *)data;
// Then do what ever I want to do
}
static void atomes_menu_bar_action (GSimpleAction * action, GVariant * parameter, gpointer data)
{
gchar * name = g_strdup_printf ("%s", g_action_get_name(G_ACTION(action)));
if (g_strcmp0 (name, "connect") == 0)
{
do_something_with_my_struct (data);
}
}
GtkWidget * create_main_window (GApplication * my_app)
{
GSimpleAction * act_connect = g_simple_action_new ("open", NULL);
g_action_map_add_action (G_ACTION_MAP(my_app), G_ACTION(act_connect));
my_struct * user_data = create_struct (); // to prepare my data structure
g_signal_connect (act_connect, "activate", G_CALLBACK(do_simple_action), (gpointer)user_data);
GtkWidget * window = gtk_application_window_new (GTK_APPLICATION(my_app));
GtkBuilder * builder = gtk_builder_new_from_file ("menus/main.ui");
GMenuModel * model = G_MENU_MODEL (gtk_builder_get_object (builder, "menu_bar"));
gtk_application_set_menubar (GTK_APPLICATION(my_app), model);
gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW(window), TRUE);
return window;
}
This works for the main application window, what about other windows generated within my application.
In particular if each of these windows do have menu bar / menu, generated using a similar method, and most of all generated using the same XML file … the action name in this XML file being the same then no matter what I do in the code, like changing the data in my_struct
, then I am guessing that each new command that I add to the G_ACTION_MAP will already exist with the same name, and if it works, will only replace the previous one … am I correct ?
If yes what can I do to ensure that for each window the activation of the menu item responds with the proper action ?
Thanks in advance for your lights !
Best regards
Sébastien