GTK+ Object System

Although GTK+ is written in C, a language without explicit support for object-oriented program, the design of GTK+ is heavily object oriented. The basis of this is a layer known as the GTK+ Object System. In addition to such traditional features as inheritance, polymorphism and reference counting, the GTK+ Object System also adds a number of features particularly adapted for a widget toolkit, including a signal system for notification and an object attribute system.

Inheritance in the GTK+ Object System is achieved by nesting structures within each other. For instance, the GtkButton class inherits from GtkWidget, so the first part of a GtkButton structure is a GtkWidget structure. This means that a pointer to a GtkButton can be cast into a pointer to a GtkWidget. Each class also has an associated class structure, which is essentially a table of pointers to the class's implementation of functions that it overrides from the parent class. (For example, the GtkWidgetClass structure includes a pointer to a draw() function; GtkButtonClass provides an implementation that draws buttons.)

Each GTK+ object class can have an associated set of signals. Each signal represents a certain type of event or other occurence that an application would want to attach a callback to. For instance, the GtkButton class provides the "clicked" signal that is emitted when the user clicks the button widget. Any number of callbacks can be connected to a signal and when the signal is emitted all callback will be called in order. Signals can also be used to change the behavior of widgets - for instance, by connecting to the "insert_text" signal for a GtkEntry widget, an application can filter the text that the user enters into the Entry to allow only numbers.

Each class also has an associated set of arguments; each argument represents some characteristic of the widget that can be queried or set. For instance, the GtkLabel widget provides a "label" argument to allow setting the text of the label and a "justify" argument to allow setting the justification of the label. The powerful feature of the argument system is that the set of arguments for a class can be dynamically queried at run time. This allows a graphical builder application to provide interfaces for setting the arguments of widgets it didn't know about in advance.

Reference (GTK+ Reference Documentation Project)

Users · Developers · ViewCVS · FTP · Bugzilla · Software Map · Contact