Activate callback with gtk_application_add_window()

I would like to know what difference is if I use activate_02 callback instead of activate callback.
I am asking strictly to this example code, more over why should I not use activate_2.

#include <gtk/gtk.h>

void activate( GApplication *app );
void activate( GApplication *app )
    GtkWidget *widget;
    widget = gtk_application_window_new( GTK_APPLICATION ( app ) );
    gtk_widget_show ( widget );

void activate_2( GApplication *app );
void activate_2( GApplication *app )
    GtkWidget *window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_application_add_window( GTK_APPLICATION( app ), GTK_WINDOW( window ) );
    gtk_widget_show ( window );

gint main ( void )
    GtkApplication *app;
    int status;
    const char *const application_id = "";

    app = gtk_application_new ( application_id, G_APPLICATION_FLAGS_NONE );
    g_signal_connect_swapped ( app, "activate", G_CALLBACK ( activate_2 ), app );
    status = g_application_run ( G_APPLICATION ( app ), 0, NULL );
    g_object_unref ( app );

    return status;

The difference is that activate() will give you a GtkApplicationWindow, while activate_2() will give you a GtkWindow :slight_smile:

GtkApplicationWindow is basically a subclass of GtkWindow with some added functionality to integrate better with GtkApplication. You can read read the rest of those differences in the GtkApplicationWindow documentation.

1 Like

Thank you for your reply. I do know the differences :slight_smile:)
If so, than that code becomes somehow equivalent with this?

#include <gtk/gtk.h>

gint main ( void )
    GtkWidget *window;
    gtk_init ( NULL, NULL );

    window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    g_signal_connect ( window, "destroy", gtk_main_quit, NULL);

    gtk_widget_show_all ( window );

I mean if “gives” me the GtkWindow…

I honestly don’t understand where are you trying to go with this.

Could you please try to give us a bit more context as to what are you trying to achieve?

I do not really have a reason, sorry about that.
I just created that activate_02 callback where I used instead of gtk_application_window_new() the gtk_application_add_window() function and noticed that worked fine.
I was not sure why it did, but I think everything is clear now.

GtkApplicationWindow is just a GtkWindow that automatically sets the GtkWindow:application property at construction time.

The g_application_run() method is going to spin the default main context, just like gtk_main() does.

You can achieve the same result through different ways, though the idiomatic way to write GTK applications is to use a GtkApplication instance and create GtkApplicationWindow instances for the top-level application windows—especially because gtk_main() and gtk_main_quit() have been removed from GTK4.


Like always strait to the point.
Thank you both for you time, that helped.

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