GTK+2 Programming Part I
Label, FileChooser, Drag & Drop


The code example-label.c creates a window with label widgets.  The User can drag files from a File Browser onto the window.  Information about the file is obtained using the C function stat.
Wherever you see "look this up", refer to the GTK+2 Reference Manual.

First, we create the Main Window.  GtkWindow is a container derived from GtkBin, which can only hold one child widget, but we want to put 3 labels in the window... so we add a box widget which can have multiple children (not sure how many, but it's more than 3). In our case it's a GtkVBox and we pack the label widgets vertically (a GtkHBox would pack the widgets horizontally, really, it's true!).

Compile and link:

gcc -Wall `pkg-config --cflags --libs gtk+-2.0` -o label-demo example-label.c

Notice the strict type-casting.  These are used because we have created a widget (like GtkLabel) which inherits from another widget (in this case GtkMisc), and in turn from a GtkWidget. The compiler will check any API call using this object to see that it is properly cast to the appropriate type.

Finished "noticing"?  Good.  Let's add some buttons to call a File Selector and an About Dialog.  We will also add some bling, a program icon and a vertical label.
We can easily create our own dialogs, but GTK already implements several dialog classes including GtkFileChooserDialog and GtkAboutDialog which saves typing.  You could, of course, copy and paste the code or download it example-label2.c since I have already typed it for you.

Compile and link:

gcc -Wall `pkg-config --cflags --libs gtk+-2.0` -o label2-demo example-label2.c

Since the vbox is now packed into a GtkHBox, we use a new initial GtkVBox so that the buttons appear at the bottom of the window.