Stepping Through Hello World

Now that we know the theory behind this, let's clarify by walking through the example helloworld program.

Here is the callback function that will be called when the button is "clicked". We ignore both the widget and the data in this example, but it is not hard to do things with them. The next example will use the data argument to tell us which button was pressed.

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

The next callback is a bit special. The "delete-event" occurs when the window manager sends this event to the application. We have a choice here as to what to do about these events. We can ignore them, make some sort of response, or simply quit the application.

The value you return in this callback lets GTK know what action to take. By returning TRUE, we let it know that we don't want to have the "destroy" signal emitted, keeping our application running. By returning FALSE, we ask that "destroy" be emitted, which in turn will call our "destroy" signal handler.

static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
    g_print ("delete event occurred\n");

    return TRUE; 

Here is another callback function which causes the program to quit by calling gtk_main_quit(). This function tells GTK that it is to exit from gtk_main when control is returned to it.

static void destroy( GtkWidget *widget,
                     gpointer   data )
    gtk_main_quit ();

I assume you know about the main() function... yes, as with other applications, all GTK applications will also have one of these.

int main( int   argc,
          char *argv[] )

This next part declares pointers to a structure of type GtkWidget. These are used below to create a window and a button.

    GtkWidget *window;
    GtkWidget *button;

Here is our gtk_init() again. As before, this initializes the toolkit, and parses the arguments found on the command line. Any argument it recognizes from the command line, it removes from the list, and modifies argc and argv to make it look like they never existed, allowing your application to parse the remaining arguments.

    gtk_init (&argc, &argv);

Create a new window. This is fairly straightforward. Memory is allocated for the GtkWidget *window structure so it now points to a valid structure. It sets up a new window, but it is not displayed until we call gtk_widget_show(window) near the end of our program.

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

Here are two examples of connecting a signal handler to an object, in this case, the window. Here, the "delete-event" and "destroy" signals are caught. The first is emitted when we use the window manager to kill the window. The second is emitted when we use the gtk_widget_destroy() call passing in the window widget as the object to destroy, or when, in the "delete-event" handler, we return FALSE. The G_CALLBACK is a macro that performs type casting and checking for us, as well as aid the readability of the code.

    g_signal_connect (window, "delete-event",
                      G_CALLBACK (delete_event), NULL);
    g_signal_connect (window, "destroy",
                      G_CALLBACK (destroy), NULL);

This next function is used to set an attribute of a container object. This just sets the window so it has a blank area along the inside of it 10 pixels wide where no widgets will go. There are other similar functions which we will look at in the section on Setting Widget Attributes

And again, GTK_CONTAINER is a macro to perform type casting.

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

This call creates a new button. It allocates space for a new GtkWidget structure in memory, initializes it, and makes the button pointer point to it. It will have the label "Hello World" on it when displayed.

    button = gtk_button_new_with_label ("Hello World");

Here, we take this button, and make it do something useful. We attach a signal handler to it so when it emits the "clicked" signal, our hello() function is called. The data is ignored, so we simply pass in NULL to the hello() callback function. Obviously, the "clicked" signal is emitted when we click the button with our mouse pointer.

    g_signal_connect (button, "clicked",
                      G_CALLBACK (hello), NULL);

We are also going to use this button to exit our program. This will illustrate how the "destroy" signal may come from either the window manager, or our program. When the button is "clicked", same as above, it calls the first hello() callback function, and then this one in the order they are set up. You may have as many callback functions as you need, and all will be executed in the order you connected them. Because the gtk_widget_destroy() function accepts only a GtkWidget *widget as an argument, we use the g_signal_connect_swapped() function here instead of straight g_signal_connect().

    g_signal_connect_swapped (button, "clicked",
                              G_CALLBACK (gtk_widget_destroy),

This is a packing call, which will be explained in depth later on in Packing Widgets. But it is fairly easy to understand. It simply tells GTK that the button is to be placed in the window where it will be displayed. Note that a GTK container can only contain one widget. There are other widgets, that are described later, which are designed to layout multiple widgets in various ways.

    gtk_container_add (GTK_CONTAINER (window), button);

Now we have everything set up the way we want it to be. With all the signal handlers in place, and the button placed in the window where it should be, we ask GTK to "show" the widgets on the screen. The window widget is shown last so the whole window will pop up at once rather than seeing the window pop up, and then the button form inside of it. Although with such a simple example, you'd never notice.

    gtk_widget_show (button);

    gtk_widget_show (window);

And of course, we call gtk_main() which waits for events to come from the X server and will call on the widgets to emit signals when these events come.

    gtk_main ();

And the final return. Control returns here after gtk_main_quit() is called.

    return 0;

Now, when we click the mouse button on a GTK button, the widget emits a "clicked" signal. In order for us to use this information, our program sets up a signal handler to catch that signal, which dispatches the function of our choice. In our example, when the button we created is "clicked", the hello() function is called with a NULL argument, and then the next handler for this signal is called. This calls the gtk_widget_destroy() function, passing it the window widget as its argument, destroying the window widget. This causes the window to emit the "destroy" signal, which is caught, and calls our destroy() callback function, which simply exits GTK.

Another course of events is to use the window manager to kill the window, which will cause the "delete-event" to be emitted. This will call our "delete-event" handler. If we return TRUE here, the window will be left as is and nothing will happen. Returning FALSE will cause GTK to emit the "destroy" signal which of course calls the "destroy" callback, exiting GTK.