Hello, World

In the long tradition of programming languages and libraries, this example is called Hello, World.

Figure 2. Hello, world

Hello, world

Hello World in C

Create a new file with the following content named example-1.c.

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
  g_print ("Hello World\n");

static void
activate (GtkApplication *app,
          gpointer        user_data)
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_window_set_child (GTK_WINDOW (window), box);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_destroy), window);
  gtk_box_append (GTK_BOX (box), button);

  gtk_widget_show (window);

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;

You can compile the program above with GCC using:

gcc `pkg-config --cflags gtk4` -o example-1 example-1.c `pkg-config --libs gtk4`

As seen above, example-1.c builds further upon example-0.c by adding a button to our window, with the label Hello World. Two new GtkWidget pointers are declared to accomplish this, button and box. The box variable is created to store a GtkBox, which is GTK’s way of controlling the size and layout of buttons.

The GtkBox is created with gtk_box_new() which takes a GtkOrientation enum as parameter. The buttons which this box will contain can either be layed out horizontally or vertically. This does not matter in this particular case, as we are dealing with only one button. After initializing box with the newly created GtkBox, the code adds the box widget to the window widget using gtk_window_set_child().

Next the button variable is initialized in similar manner. gtk_button_new_with_label() is called which returns a GtkButton to be stored in button. Afterwards button is added to our box.

Using g_signal_connect(), the button is connected to a function in our app called print_hello(), so that when the button is clicked, GTK will call this function. As the print_hello() function does not use any data as input, NULL is passed to it. print_hello() calls g_print() with the string Hello World which will print Hello World in a terminal if the GTK application was started from one.

After connecting print_hello(), another signal is connected to the clicked state of the button using g_signal_connect_swapped(). This functions is similar to a g_signal_connect() with the difference lying in how the callback function is treated. g_signal_connect_swapped() allows you to specify what the callback function should take as parameter by letting you pass it as data. In this case the function being called back is gtk_window_destroy() and the window pointer is passed to it. This has the effect that when the button is clicked, the whole GTK window is destroyed. In contrast if a normal g_signal_connect() were used to connect the clicked signal with gtk_window_destroy(), then the function would be called on button (which would not go well, since the function expects a GtkWindow as argument).

More information about creating buttons can be found here.

The rest of the code in example-1.c is identical to example-0.c. The next section will elaborate further on how to add several GtkWidgets to your GTK application.