I recently started learning how to create GUI applications using GTK + (3), following the instructions on the gnome website (first link below).
So, I began to study the first few textbooks, and everything was wonderful. Here are some of the early codes I wrote:
#include <gtk/gtk.h> static void activate (GtkApplication* app, gpointer user_data){ GtkWidget *window; GtkWidget *button_box; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "First application"); gtk_window_set_default_size (GTK_WINDOW (window), 640, 480); button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL); gtk_container_add(GTK_CONTAINER(window), button_box); button = gtk_button_new_with_label("Click this!"); g_signal_connect_swapped(button, "clicked", G_CALLBACK(g_print), "Hello, world!\n"); g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window); gtk_container_add(GTK_CONTAINER(button_box), button); gtk_widget_show_all (window); } int main(int argc, char *argv[]){ GtkApplication *app; int status; app = gtk_application_new ("com.example.application", 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; }
Everything worked exactly the way I wanted. Then they introduced Gtk Builder, and I thought it was really cool, so I used this to create the application.
However, all of a sudden, they started writing code that did not look anything like everything they wrote earlier (second link below). So, I was very embarrassed and decided to look at the code from other sources to see how they wrote their GTK applications. I saw how they used things like gtk_init() , gtk_main() and gtk_window_new(GTK_WINDOW_TOPLEVEL) . This is not at all like the code I posted earlier.
So, as I understand it now, there are several ways to write GTK applications using either GtkApplication or gtk_init() . One of them is outdated and no longer prefers? If so, which one is preferred or should be preferred?
A few links to the two ways I found in the official Gtk tutorial: