The general steps to creating a widget in GTK are:
gtk_*_new
() - one of various functions
to create a new widget. These are all detailed in this section.gtk_container_add
()
or gtk_box_pack_start
().gtk_widget_show
() the widget.gtk_widget_show
() lets GTK know that
we are done setting the attributes of the widget, and it is ready
to be displayed. You may also use
gtk_widget_hide
() to make it disappear again.
The order in which you show the widgets is not important, but I suggest
showing the window last so the whole window pops up at once rather than
seeing the individual widgets come up on the screen as they're formed.
The children of a widget (a window is a widget too) will not be displayed
until the window itself is shown using the
gtk_widget_show
() function.
You'll notice as you go on that GTK uses a type casting system. This is always done using macros that both test the ability to cast the given item, and perform the cast. Some common ones you will see are:
G_OBJECT (object) GTK_WIDGET (widget) GTK_OBJECT (object) G_CALLBACK (function) GTK_CONTAINER (container) GTK_WINDOW (window) GTK_BOX (box)
These are all used to cast arguments in functions. You'll see them in the examples, and can usually tell when to use them simply by looking at the function's declaration.
As you can see below in the class hierarchy, all GtkWidgets are derived from the GObject base class. This means you can use a widget in any place the function asks for an object. Note that many functions accept a gpointer so there is no need to cast the type.
For example:
g_signal_connect(button, 'clicked', G_CALLBACK(@callback_function), callback_data);
This casts the button into an object, and provides a cast for the function pointer to the callback.
Many widgets are also containers. If you look in the class hierarchy below, you'll notice that many widgets derive from the Container class. Any one of these widgets may be used with the GTK_CONTAINER macro to pass them to functions that ask for containers.
Unfortunately, these macros are not extensively covered in the tutorial, but I recommend taking a look through the GTK header files or the GTK API reference manual. It can be very educational. In fact, it's not difficult to learn how a widget works just by looking at the function declarations.
For your reference, here is the class hierarchy tree used to implement widgets. (Deprecated widgets and auxilliary classes have been omitted.)
GObject | GtkObject +GtkWidget | +GtkMisc | | +GtkLabel | | | `GtkAccelLabel | | +GtkArrow | | `GtkImage | +GtkContainer | | +GtkBin | | | +GtkAlignment | | | +GtkFrame | | | | `GtkAspectFrame | | | +GtkButton | | | | +GtkToggleButton | | | | | `GtkCheckButton | | | | | `GtkRadioButton | | | | `GtkOptionMenu | | | +GtkItem | | | | +GtkMenuItem | | | | | +GtkCheckMenuItem | | | | | | `GtkRadioMenuItem | | | | | +GtkImageMenuItem | | | | | +GtkSeparatorMenuItem | | | | | `GtkTearoffMenuItem | | | +GtkWindow | | | | +GtkDialog | | | | | +GtkColorSelectionDialog | | | | | +GtkFileSelection | | | | | +GtkFontSelectionDialog | | | | | +GtkInputDialog | | | | | `GtkMessageDialog | | | | `GtkPlug | | | +GtkEventBox | | | +GtkHandleBox | | | +GtkScrolledWindow | | | `GtkViewport | | +GtkBox | | | +GtkButtonBox | | | | +GtkHButtonBox | | | | `GtkVButtonBox | | | +GtkVBox | | | | +GtkColorSelection | | | | +GtkFontSelection | | | | `GtkGammaCurve | | | `GtkHBox | | | +GtkCombo | | | `GtkStatusbar | | +GtkFixed | | +GtkPaned | | | +GtkHPaned | | | `GtkVPaned | | +GtkLayout | | +GtkMenuShell | | | +GtkMenuBar | | | `GtkMenu | | +GtkNotebook | | +GtkSocket | | +GtkTable | | +GtkTextView | | +GtkToolbar | | +GtkTreeView | +GtkCalendar | +GtkDrawingArea | | `GtkCurve | +GtkEditable | | +GtkEntry | | | `GtkSpinButton | +GtkRuler | | +GtkHRuler | | `GtkVRuler | +GtkRange | | +GtkScale | | | +GtkHScale | | | `GtkVScale | | `GtkScrollbar | | +GtkHScrollbar | | `GtkVScrollbar | +GtkSeparator | | +GtkHSeparator | | `GtkVSeparator | +GtkInvisible | +GtkPreview | `GtkProgressBar +GtkAdjustment +GtkCellRenderer | +GtkCellRendererPixbuf | +GtkCellRendererText | +GtkCellRendererToggle +GtkItemFactory +GtkTooltips `GtkTreeViewColumn
The following widgets do not have an associated window. If you want to capture events, you'll have to use the EventBox. See the section on the Event Box widget.
GtkAlignment GtkArrow GtkBin GtkBox GtkButton GtkCheckButton GtkFixed GtkImage GtkLabel GtkMenuItem GtkNotebook GtkPaned GtkRadioButton GtkRange GtkScrolledWindow GtkSeparator GtkTable GtkToolbar GtkAspectFrame GtkFrame GtkVBox GtkHBox GtkVSeparator GtkHSeparator
We'll further our exploration of GTK by examining each widget in turn,
creating a few simple functions to display them. Another good source
is the gtk examples directory. If you installed the documentation files
this directory should be:
/usr/doc/fpc-1.0.6/examples/gtk/
Both are available from sourceforge:
SourceForge