3
votes

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
Check this example. It seems you have to subclass GtkApplicationWindow and GtkApplication.José Fonte
@JoséFonte The thing is that it will work as expected if I use gtk_application_window_new(app) instead of GtkBuilderErik W
@libforce for coherency i suggest editing the same code on the question. ThanksJosé Fonte

1 Answers

6
votes

The problem resides on the ui builder file with the visible flag set as true:

a (application) window has to have a GtkApplication instance assigned before it's shown

So, set the builder.ui Visible property to False:

<property name="visible">False</property>

As the window is not being shown, you must do it manually after setting the application, e.g. gtk_window_present:

void app_ctor(GtkApplication* app, G_GNUC_UNUSED gpointer user_data)
{
    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);
    gtk_window_present(appwin);
}