GTK Widget Hierarchy Tutorial

This article explains GTK widget hierarchy and object hierarchy. GTK is a graphical toolkit for GUI design that is implemented as a set of libraries. GTK widgets have a hierarchy because they have an object oriented design. Understanding GTK widget hierarchy is a necessary part of learning to program with GTK.

Part 20 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

GTK Object Oriented Design in C

GTK libraries are written in the C language, but have an object oriented design. The following quote from the Gnome developer GTK tutorial elaborates.

GTK is essentially an object oriented application programmers interface (API). Although written completely in C, it is implemented using the idea of classes and callback functions (pointers to functions).

Reference: GTK tutorial introduction

The GTK object oriented framework is found in the GObject library which is a part of the GLib library. It is GObject that allows object oriented programming using the C programming language.

Object Hierarchy with GObject

Every GTK object is derived from GObject. In the following image it can be seen that GObject is the parent object. GInitiallyUnowned is the child of GObject and inherits all of its properties. GtkWidget is then derived from GInitiallyUnowned and inherits from its parent object.

GTK Object Hierarchy
GTK Object Hierarchy

The image also shows all of the objects that are derived from or are children of GtkWidget. This is classical object oriented inheritance.

GTK Widget Hierarchy

When writing GTK applications, we are mostly concerned with widgets and therefore child widgets that are derived from GtkWidget. Examples of two widgets follow, showing their GTK hierarchy.

GTK Widget Hierarchy of GtkLabel

The image below shows a familiar widget, GtkLabel. GtkLabel is derived from GtkMisc which is derived from GtkWidget. GtkLabel therefore inherits all properties further up the chain.

GTK Widget Hierarchy of GtkLabel
GTK Widget Hierarchy of GtkLabel

The image also shows that GtkAccelLabel is derived from or is the child of GtkLabel.

GTK Widget Hierarchy GtkButton

A second example is shown in the image below. GtkButton is derived from GtkBin which is derived from GtkContainer which is derived from GtkWidget.

GTK Widget Hierarchy for GtkButton
GTK Widget Hierarchy for GtkButton

Several other widgets are children of GtkButton, such as GtkToggleButton and others that can be seen below GtkButton in the image.

Why Object Hierarchy or Widget Hierarchy is Important

Finding Properties, Signals and Functions

When creating applications in GTK, it is important to know about object hierarchy or the hierarchy of widgets. The main reason for this is that some properties, signals and functions are inherited by widgets. This means that some properties, signals and functions will not be found directly in a widget, but may be inherited.

GTK Widget Hierarchy with Glade

When working with Glade, some widget settings in Glade may be inherited. This means that not all of the settings for a widget in Glade apply directly to the widget itself, but may apply to one of the widgets ancestors.

As an example, the signals for a GtkButton in Glade are shown below. Signals for GtkContainer, GtkWidget and GObject also appear in the signals pane. This is because of inheritance.

GTK Hierarchy in the Signals Pane of Glade
GTK Hierarchy in the Signals Pane of Glade

Leave a Reply

Your email address will not be published. Required fields are marked *