Hello world..... in gtk

I'm tired and bored so I thought I would add to the brilliant introduction/tutorial posts that have been on this site lately. I'm going to be going through programming in gtk2.

I'll start with something very simple: an explanation.

GTK is a tool kit for gui programming. It was written for the gimp. It's full name is Gimp Toolkit. Gnome uses GTK of course and it has bindings for loads of languages:

Python,
C,
C++,
Perl,
PHP,
etc etc etc!
n
I'll be doing it in C because that's available everywhere.

Before you can program gtk2 apps, you will need to install the development files for gtk2. Under fedora you can do this either from the cd or by either of the following:

yum install gtk2-devel
apt-get install gtk2-devel

Firstly - GTK programs are not like normal programs. You use gtk to setup your windows etc.. connect 'signals' to callbacks(Function called when events happen) and then let gtk do the rest. You actually pass control of your program to gtk. Gui programming is actually not difficult at all once you get the hang of some stuff. I'll start with something really simple. A window.
#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
GtkWidget *window;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);

gtk_main();

return 0;
}This can be compiled by typing this:
gcc -o test1 test1.c `pkg-config!
--cflags --libs gtk+-2.0`The pkg-config part just lets pkg-config specify extra needed flags to gcc.

This is simple!.. this is what is happening:

#include <gtk/gtk.h>
This line includes all the gtk functions

int main(int argc, char *argv[]) {
This line starts the main() function. main() is where your program starts executing.

GtkWidget *window
This line declares window as a pointer to a GtkWidget. In this case we will make it a window. Widgets can be anything like buttons etc.

gtk_init (&argc, &argv);
This gives gtk the chance to set it self up. It takes argv and argc as parameters. This lets you pass options to gtk on the command line.

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
This asks gtk to make a window and points 'window' to it. We can now refer to 'window' whenever we want to manipulate it.

gtk_widget_show (window);
This tells gtk it can show the window because we are finished modifying it.

gtk_main();
This is where we !
let gtk take over. Not much will happen yet because we have only got a very simple window that can't even exit the program when it is closed.. we'll hook up some signals soon.

return 0;
Return control to the operating system and let it know everything went ok..

Very simple.. Now lets hook up the close button..

I'll put coments in the code from now on..
#include <gtk/gtk.h>

// This next line defines a function. We will ask gtk to call this
// function when the close button is pressed. We need to add the
// parameters to the function because gtk will pass the parameters
// The paramets let you refer to things like - the widget that fired
// the event.
static gboolean delete_event(GtkWidget *widget,
GdkEvent *event, gpointer data) {
return FALSE;
}

// This function is used to actually close the appliction. If the delete
// event returns FALSE then this function is called. We can return
// TRUE from the delete !
even to stop it from closing. Eg: Do you
// really want to close this window?
static void destroy(GtkWidget *widget, gpointer data) {
gtk_main_quit();
}

int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

// here we connect the functions to the signals that gtk will generate
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);

gtk_widget_show (window);

gtk_main();

return 0;
}Again..
gcc -o test1 test1.c `pkg-config --cflags --libs gtk+-2.0`Next I'll show you how to add a button and how to popup alert boxes.