C and GTK3 - Application menu not showing when using Glade

Viewed 901

I am having trouble getting the correct application menu showing when using Glade (GtkBuilder). This is the code I am currently using:

main.c:

void app_init(GtkApplication* app, void* userdata)
{
    (void)userdata;
    GActionEntry appactions[] = {
        {"about", app_aboutaction, NULL, NULL, NULL, {0}},
        {"quit", app_quitaction, NULL, NULL, NULL, {0}},
    };

    g_action_map_add_action_entries(
        G_ACTION_MAP(app),
        appactions,
        sizeof appactions / sizeof *appactions,
        app
    );

    GMenu* menu = g_menu_new();
    g_menu_append(menu, "About", "app.about");
    g_menu_append(menu, "Quit", "app.quit");

    gtk_application_set_app_menu(app, G_MENU_MODEL(menu));
}

void app_ctor(GtkApplication* app, void* userdata)
{
    (void)userdata;

    GtkBuilder* builder = gtk_builder_new_from_file("./builder.ui");
    GtkWindow* appwin = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
    gtk_window_set_application(appwin, app);
}

int main(int argc, char* argv[])
{
    GtkApplication* app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "startup", G_CALLBACK(app_init), NULL);
    g_signal_connect(app, "activate", G_CALLBACK(app_ctor), NULL);

    return g_application_run(G_APPLICATION(app), argc, argv);
}

builder.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkApplicationWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

Resulting application menu:

result

Expected application menu:

expected result

I believe that it has something to do with me not calling the application window constructor, since it is created with GtkBuilder or something like that.

Any ideas to what I can do?

1 Answers
Related