I am now learning to use GTK+3.0 with C in Linux. After reading some tutorials and sample code, I have some questions regarding how to initialize an application.
Here are two versions of code I have seen.
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
This code used gtk_application_new()
to init a GtkApplication
and g_application_run()
to start it.
This is the second one.
#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
GtkWidget *window;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"helloworld");
gtk_widget_show(window);
gtk_main();
return 0;
}
This code used gtk_init()
to init the application and gtk_main()
to run it.
However, I can't figure out the difference between them as the running result seems the same.
Linux commandos 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 GNU/Linux
on a Macbook Air 5,2. The first example loads instantly, where the second example loads after about 30 seconds. – Ediva