GUI toolkits

GUIs form the core computing experience for the majority of users - even many Linux people today find themselves using KDE or Gnome for tasks that only a few years ago would have been done from a shell. The key reason for this is that GUIs are, generally speaking, designed to be easy to use, with the goal of allowing users to spend more time thinking about what they wish to achieve and less about how it needs to be achieved.

In order to minimise the learning curve required for new users to get to grips with an environment, GUIs use shared code to implement the graphical components that make it up; for example, the code to generate a toolbar would be the same across all programs written using a given GUI toolkit in order that all the programs share the same look and feel.

Several GUI toolkits, including GTK and Trolltech's Qt (used in KDE) are written using C++ classes and objects, which means each graphical element of a program, known in the Unix world as a widget, has its own class which inherits properties and methods from various ancestor classes.

For example, the GtkButton classes inherits from the GtkBin class, which in turn inherits from the GtkContainer class, then the GtkWidget class, which inherits from the GtkObject class. Each sub-class adds new methods and properties to do a specific task for that widget, and if a programmer wished to create a specific kind of GtkButton - for example, a button that automatically played a sound when clicked -- they would probably find it easiest to inherit from the GtkButton class.

After you have been using GTK for some time, you will likely come to appreciate its fine-grained class inheritance structure, as it allows you to create your own objects at all levels, and also provides you with some very powerful objects, like the GtkCalendar widget, which provides fairly good calendar functionality just by instantiating the class.

Beyond inheriting class variables ("properties") and class functions ("methods"), GTK widgets also inherit signals, which is a core topic in GTK. Simply put, signals are emitted when things happen in your GUI; usually this is the user interacting with your widgets. GtkButtons, to take the preceding example, emit a signal when the mouse moves over them, a signal when the mouse clicks them, a signal when the mouse is released from clicking them, and a signal when the mouse moves away from them, amongst other signals.

So each signal, as you can see, is sent out when a particular occurrence happens to a widget, and each widget has its own set of signals that it will emit as a result of user interaction. The magic comes when you tie a given signal, e.g. "clicked" for a GtkButton, to a function you have written; this function is then called every time your user clicks the button you chose.

All clear on the theory aspect? Excellent; time to demonstrate the first piece of code...

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: A Basic GUI >>

Previous chapter: Getting started

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.