Writing Language Bindings for GTK+ 2.0

James Henstridge

james@daa.com.au

GTK+ 2.0 is a versatile toolkit for creating graphical user interfaces, and is available for the X Window System, the Linux Framebuffer console and Microsoft Windows, among others. Unlike many other toolkits, GTK+ was designed so that it is fairly easy to write bindings to other languages.

This document is designed to help language binding authors understand how to bind GTK+ 2.0


Table of Contents
Overview
GTypes and GValues
GClosure
GObject
GSignal

Overview

GTK+ 2.0 is an object oriented graphical toolkit. It's object model is based on top of the GObject library, which is part of glib. As the object system is implemented in C code, it is not constrained by what is provided by the language.

One of the aims of the GObject system was to make it easy to use from a language binding. Some of the benefits are:

The GObject object model

As you might expect, to write a good GTK language binding, a good understanding of GObject is necessary. There are a number of parts of GObject that you will need to look at:

  • GType: the type registry. Every type used in GTK has (or should have) a type registered with the type system. From integers to enumerations to GObjects. This registry is used for runtime introspection at various parts of the binding.

  • GValue: used for marshalling arbitrary data types to a function. A GValue is similar to a CORBA::Any (XXXX - is this true?). It consists of a GType typecode, and type specific data. The actual value can be read with type specific accessor functions.

    A language binding will probably need some code to marshal.

  • GClosure: a data structure representing a function. It consists of a function and a marshaller that will marshal an array of GValues to a form that can be passed to the function.

    For a language binding, you will need to implement a new closure type and an appropriate marshaller that can call functions written in the scripting language.

    GClosures are used to implement signals for GObjects, and are used for various other callbacks in GTK+ (XXXX - at the moment, there aren't any other uses).

  • GSignal: a notification mechanism. XXXX - put in description of signals here

  • GObject: object and class system. These are the objects provided by the gobject library. A GObject class can be subclassed, can have a number of signals and may implement a number of interfaces. The programmer can store arbitrary data with a GObject instance, which can be accessed with a string key.

    All widgets in the GTK+ library are GObjects. From the point of view of a language binding author, understanding GObjects is vital to writing a good binding.

  • GInterface: interfaces for GObjects. These are similar to C++ signatures (XXXX - well, that is what timj said).

Each of these things will be explained in later sections.