2. Einfache Beispiele
Zunächst sollte man wohl einen kurzen Blick auf die Klassenhierachie im GTK werfen. Alle Klassen stammen von "GtkObject" ab und es gibt keine Mehrfachvererbung:
GtkObject
|
+---GtkData
| |
| +---GtkAdjustment
| |
| +---GtkTooltips
|
+---GtkItemFactory
|
+---GtkWidget
|
+---GtkCalendar
|
+---GtkContainer
| |
| +---GtkBin
| | |
| | +---GtkAlignment
| | |
| | +---GtkAspectFrame
| | |
| | +---GtkButton
| | | |
| | | +---GtkOptionMenu
| | | |
| | | +---GtkToggleButton
| | | |
| | | +---GtkCheckButton
| | | |
| | | +---GtkRadioButton
| | |
| | +---GtkEventBox
| | |
| | +---GtkFrame
| | |
| | +---GtkHandleBox
| | |
| | +---GtkInvisible
| | |
| | +---GtkItem
| | | |
| | | +---GtkListItem
| | | |
| | | +---GtkMenuItem
| | | | |
| | | | +---GtkCheckMenuItem
| | | | | |
| | | | | +---GtkRadioMenuItem
| | | | |
| | | | +---GtkTearoffMenuItem
| | | |
| | | +---GtkTreeItem
| | |
| | +---GtkScrolledWindow
| | |
| | +---GtkViewport
| | |
| | +---GtkWindow
| | |
| | +---GtkColorSelectionDialog
| | |
| | +---GtkDialog
| | |
| | +---GtkFileSelection
| | |
| | +---GtkFontSelectionDialog
| | |
| | +---GtkInputDialog
| | |
| | +---GtkPlug
| |
| +---GtkBox
| | |
| | +---GtkButtonBox
| | | |
| | | +---GtkHButtonBox
| | | |
| | | +---GtkVButtonBox
| | |
| | +---GtkHBox
| | | |
| | | +---GtkCombo
| | | |
| | | +---GtkStatusbar
| | |
| | +---GtkVBox
| | |
| | +---GtkColorSelection
| | |
| | +---GtkGammaCurve
| |
| +---GtkCList
| | |
| | +---GtkCTree
| |
| +---GtkFixed
| |
| +---GtkLayout
| |
| +---GtkList
| |
| +---GtkMenuShell
| | |
| | +---GtkMenu
| | |
| | +---GtkMenuBar
| |
| +---GtkNotebook
| | |
| | +---GtkFontSelection
| |
| +---GtkPacker
| |
| +---GtkPaned
| | |
| | +---GtkHPaned
| | |
| | +---GtkVPaned
| |
| +---GtkSocket
| |
| +---GtkTable
| |
| +---GtkToolbar
| |
| +---GtkTree
|
+---GtkDrawingArea
| |
| +---GtkCurve
|
+---GtkEditable
| |
| +---GtkEntry
| | |
| | +---GtkSpinButton
| |
| +---GtkText
|
+---GtkMisc
| |
| +---GtkArrow
| |
| +---GtkImage
| |
| +---GtkLabel
| | |
| | +---GtkAccelLabel
| | |
| | +---GtkTipsQuery
| |
| +---GtkPixmap
|
+---GtkPreview
|
+---GtkProgress
| |
| +---GtkProgressBar
|
+---GtkRange
| |
| +---GtkScale
| | |
| | +---GtkHScale
| | |
| | +---GtkVScale
| |
| +---GtkScrollbar
| |
| +---GtkHScrollbar
| |
| +---GtkVScrollbar
|
+---GtkRuler
| |
| +---GtkHRuler
| |
| +---GtkVRuler
|
+---GtkSeparator
|
+---GtkHSeparator
|
+---GtkVSeparator
Das erste Beispiel: "Hello, World!" in GTK
Eigentlich kann man die obligatorische "Hello, World!"-Applikation fast komplett entwickeln, ohne auch nur eine Zeile (problemspezifischen) Code zu schreiben:

Nach der Erstellung im Interface-Builder benötigt man noch etwa folgendes Hauptprogramm (Einzelheiten dazu später):
#include <gtk/gtk.h>
#include <glade/glade.h>
int main (int argc, char *argv[])
{
GladeXML *xml;
gtk_init(&argc, &argv);
glade_init();
/* load the interface */
xml = glade_xml_new("hello1.glade", NULL);
glade_xml_signal_autoconnect(xml);
gtk_main();
return 0;
}
"Hello, World!", Version 2
Natürlich kann man das auch direkt in C - also ohne einen Interface-Builder - aufschreiben. Das sieht dann etwa so aus:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWindow *window;
GtkLabel *label;
gtk_init(&argc, &argv);
window = (GtkWindow *) gtk_window_new(GTK_WINDOW_TOPLEVEL);
label = (GtkLabel *) gtk_label_new("Hello, World!");
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(label));
gtk_widget_show_all(GTK_WIDGET(window));
gtk_signal_connect(GTK_OBJECT(window), "destroy", gtk_main_quit, NULL);
gtk_main();
return 0;
}
"Hello, World!", Version 3
Und nun noch einmal zum Vergleich dasselbe Programm in Objective C:
#include <Foundation/NSAutoreleasePool.h>
#include <GToolKit/GToolKit.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
GTKWindow *window;
[[GTKApplication alloc] initWithArgc:&argc argv:&argv];
window = [GTKWindow windowWithType:GTK_WINDOW_TOPLEVEL];
[window add:[GTKLabel labelWithStr:@"Hello, World!"]];
[window showAll];
[GTKApp run];
[pool release];
return 0;
}
"Hello, World!", Version 4
Und schließlich - weil's so schön ist - dasselbe Programm in Perl:
#!/usr/bin/perl -w
use Gtk;
init Gtk;
$window = new Gtk::Window;
$button = new Gtk::Label "Hello World!";
$window->add($button);
$window->show_all();
$window->signal_connect("destroy", sub { exit; });
main Gtk;