I was going to provide some more GTK4 examples.
While the headerbar example was working already well, I thought to start with an old gtk2 non app style example and used /tests/simple.c.
The interesting observation is that the program keeps running when we click the x close symbol of the window.
The reason for this is that the Nim bindings ref the widgets and unref them when they go out of scope. So I tested with plain C examples applying g_object_ref_sink(), as that is what the Nim bindings do.
First I took a plain gtk3 non app style example and added g_object_ref_sink():
// http://zetcode.com/gui/gtk2/firstprograms/
// gcc -o zsimple zsimple.c `pkg-config --libs --cflags gtk+-3.0`
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_object_ref_sink(window); // added by salewski for testing
gtk_widget_show(window);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Seems to work fine still.
Now with GTK4 test/simple.c and g_object_ref_sink() added:
// https://gitlab.gnome.org/GNOME/gtk/-/blob/master/tests/simple.c
// gcc -Wall simple.c -o simple `pkg-config --cflags --libs gtk4`
#include <gtk/gtk.h>
static void
hello (void)
{
g_print ("hello world\n");
}
static void
quit_cb (GtkWidget *widget,
gpointer data)
{
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
}
int
main (int argc, char *argv[])
{
GtkWidget *window, *button;
gboolean done = FALSE;
gtk_init ();
window = gtk_window_new ();
g_object_ref_sink(window); // added by salewski for testing
gtk_window_set_title (GTK_WINDOW (window), "hello world");
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
button = gtk_button_new ();
gtk_button_set_label (GTK_BUTTON (button), "hello world");
gtk_widget_set_margin_top (button, 10);
gtk_widget_set_margin_bottom (button, 10);
gtk_widget_set_margin_start (button, 10);
gtk_widget_set_margin_end (button, 10);
g_signal_connect (button, "clicked", G_CALLBACK (hello), NULL);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_widget_show (window);
while (!done)
g_main_context_iteration (NULL, TRUE);
return 0;
}
For me program does not terminate when I close the window. It seems that the quit_cb() function is not executed at all. And when we think about it, it is not too surprising, as we have ref the window. First question is why it works for the zcode example. Next question for myself is how I can fix it for the Nim bindings. Maybe never ref toplevel GTKWindows? Maybe this issue is not that serious, as we generally use app style and not the old gtk2 style.