GTK+ By Example/Beginning

In this chapter we will look at some very simple GTK+ examples; Theory and detailed will be at the bottom each chapter, it should be much easier to digest once you have written some code. Lets start off with some classic hello world examples, feel free to work in the way you learn best, but, let us make some recommendations to you.

  1. Make sure you understand what you are doing before you move on to the next chapter, feel free to finish the one you are on already chapters in this book are designed to be completed as individual units that will rely on some previous chapters.
  2. If you want to copy and paste feel free (so long as you are making changes to the code), but, until you get used to the GTK+ syntax maybe you should type things out by hand.
  3. Take it slow and tell yourself how wonderful you are for getting every line of code working
  4. Make mistakes changing the code, get things wrong and have fun.

An empty windowEdit

In this example we create a single window, set its title and size and connect an event that allows the application to close.

#include <gtk/gtk.h>
 
int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;
 
  /* Initialise GTK+ passing to it all command line arguments  */
  gtk_init (&argc, &argv);
 
  /* create a new window, set values */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello GTK+ World");
  /* set the size of the window */
  gtk_widget_set_size_request (GTK_WIDGET (window), 200, 200);
  /* connect the windows "destroy" event */
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);  
 
  /* set the window as visible */
  gtk_widget_show (window);
 
  /* run the GTK+ main loop */
  gtk_main ();
  return 0;
}


Congratulations, you have created your first GTK+ window. You may not completely understand the code you have written yet. That's alright; by the end of this chapter creating windows will be easy. GTK+ is object oriented. As C is not designed as an object oriented language you must explicitly cast objects- this will occur most often with the paramater passed to a function. You first cast window from a GtkWidget to a GtkWindow when you wrote GTK_WINDOW (window). If you don't understand this example feel free to move on after you complete two small challenges. You can probably guess how to do them easily, but don't let yourself be tricked. The act of doing even small tasks will help you focus yourself on solving large problems.

challenge: Change the name of the window to your first name.
challenge: Change the window size to 400 * 400 pixels, currently it is 200 * 200 pixels.


We will have explained everything in this example by the end of the chapter, but for now let us focus on two lines of code.

gtk_init (&argc, &argv);
gtk_init () is called at the start of all GTK+ applications, it initialises gtk+ library. &argc and &argv are passed to gtk_init (), these are command line arguments that have been passed to the application gtk_init () takes these becase it has a set of command line arguments it can accept.
gtk_main ();
gtk_main () is called once your application has been set up, once this has been called you will have to wait until gtk_main_quit () is called before the application continues. We have already connected to that event and will explain it after further excercises.

Window with a buttonEdit

This example is slightly different to the last example, we have added a button also notice we have not called gtk_widget_set_size_request ().

#include <gtk/gtk.h>
 
int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;
  GtkWidget *button;
 
  /* Initialise GTK+ passing to it all command line arguments  */
  gtk_init (&argc, &argv);
 
  /* create a new window, set values */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello Button");
  /* connect the windows "destroy" event */
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);  
 
  button = gtk_button_new_with_label ("Hello Window");
  gtk_container_add (GTK_CONTAINER (window), button);
  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (gtk_main_quit), NULL);
 
  /* set the window as visible */
  gtk_widget_show_all (window);
 
  /* run the GTK+ main loop */
  gtk_main ();
  return 0;
}


Good work, another one bites the dust, don't move on just yet, same format as before my friend. When you click the button it closes the window, what kind of annoying application is this? One that is trying to get you to pay attention to how it is done that's what. Notice this time we did not call gtk_widget_set_size_request (), that is because GTK+ automatically sizes windows and widgets for you, for the most part you should not have to set the size of widgets or windows your self; the last example was different because we had nothing inside the window. As the name size_request suggest the size you ask for may not be the size allocated to the widget, this will be explained more further on in the book, for now understanding that it is a request should be enough.

challenge: make the button do nothing when you click it, that's right nothing.
challenge: remove the line starting with 'button =', and run your program from the command line and see what happens.


Once again we will not be explaining everything just yet, that would take the fun out of this for you. Anyhow, how did you go with the challenges? I bet you did well. Let's go through a two more lines of code:

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_new () creates a new window of one of two types, GTK_WINDOW_TOPLEVEL and GTK_WINDOW_POPUP. Don't let the name GTK_WINDOW_POPUP confuse you it is not for popup dialogs, popup dialogs and GTK_WINDOW_POPUP both will be explained in future chapters.
button = gtk_button_new_with_label ("Hello Window");
button_new_with_label () is a convenience function, in this case it means it creates a button and a label and inserts the label in the button for you. A button in GTK+ is a container that when clicked may cause a piece of code to be run. The button may hold anything you insert into it, but putting somethin in a button just for fun will really confuse people, so we suggest you insert labels or images, and if you are a great person you will learn to insert images and labels at the rate you are going you will rule the world soon.

TheoryEdit

TODO

Last modified on 1 November 2013, at 08:59