Previous Next Contents

5. Widget Overview

The general steps to creating a widget in GTK are:

  1. gtk_*_new - one of various functions to create a new widget. These are all detailed in this section.
  2. Connect all signals we wish to use to the appropriate handlers.
  3. Set the attributes of the widget.
  4. Pack the widget into a container using the appropriate call such as gtk_container_add() or gtk_box_pack_start().
  5. 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.

5.1 Casting

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:

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 GtkObject base class. This means you can use an widget in any place the function asks for an object - simply use the GTK_OBJECT() macro.

For example:

gtk_signal_connect(GTK_OBJECT(button), "clicked",
                   GTK_SIGNAL_FUNC(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 drive from the GtkContainer class. Any one of those widgets may use 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 recomend taking a look through the GTK header files. It can be very educational. In fact, it's not difficult to learn how a widget works just by looking at the function declarations.

5.2 Widget Hierarchy

For your reference, here is the class hierarchy tree used to implement widgets.

    GtkObject
    +-- GtkData
    |   \-- GtkAdjustment
    |
    \-- GtkWidget
        +-- GtkContainer
        |   +-- GtkBin
        |   |   +-- GtkAlignment
        |   |   +-- GtkFrame
        |   |   |   *-- GtkAspectFrame
        |   |   |
        |   |   +-- GtkItem
        |   |   |   +-- GtkListItem
        |   |   |   +-- GtkMenuItem
        |   |   |   |   +-- GtkCheckMenuItem
        |   |   |   |       *-- GtkRadioMenuItem
        |   |   |   |
        |   |   |   *-- GtkTreeItem
        |   |   |
        |   |   +-- GtkViewport
        |   |   \-- GtkWindow
        |   |       +-- GtkDialog
        |   |       \-- GtkFileSelection
        |   |
        |   +-- GtkBox
        |   |   +-- GtkHBox
        |   |   \-- GtkVBox
        |   |       +-- GtkColorSelection
        |   |       \-- GtkCurve
        |   |
        |   +-- GtkButton
        |   |   +-- GtkOptionMenu
        |   |   \-- GtkToggleButton
        |   |       \-- GtkCheckButton
        |   |           \-- GtkRadioButton
        |   |
        |   +-- GtkList
        |   +-- GtkMenuShell
        |   |   +-- GtkMenu
        |   |   \-- GtkMenuBar
        |   |
        |   +-- GtkNotebook
        |   +-- GtkScrolledWindow
        |   +-- GtkTable
        |   \-- GtkTree
        |
        +-- GtkDrawingArea
        +-- GtkEntry
        +-- GtkMisc
        |   +-- GtkArrow
        |   +-- GtkImage
        |   +-- GtkLabel
        |   \-- GtkPixmap
        |
        +-- GtkPreview
        +-- GtkProgressBar
        +-- GtkRange
        |   +-- GtkScale
        |   |   +-- GtkHScale
        |   |   \-- GtkVScale
        |   |
        |   \-- GtkScrollbar
        |       +-- GtkHScrollbar
        |       \-- GtkVScrollbar
        |
        +-- GtkRuler
        |   +-- GtkHRuler
        |   \-- GtkVRuler
        |
        \-- GtkSeparator
            +-- GtkHSeparator
            \-- GtkVSeparator

5.3 Widgets Without Windows

The following widgets do not have an associated window. If you want to capture events, you'll have to use the GtkEventBox. See the section on The EventBox Widget

GtkAlignment
GtkArrow
GtkBin
GtkBox
GtkImage
GtkItem
GtkLabel
GtkPaned
GtkPixmap
GtkScrolledWindow
GtkSeparator
GtkTable
GtkViewport
GtkAspectFrame
GtkFrame
GtkVPaned
GtkHPaned
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 testgtk.c program that comes with GTK. It can be found in gtk/testgtk.c.


Previous Next Contents