GUI Programming

The traditional style of programming which most of us who program are probably familiar is a linear style, where step A follows step B follows step C. This model is not appropriate for a graphical user interface, where the user could ask any one of a number of functions to be performed in any order. You might, for instance, want to open a new file, or click on a button, or enter new text. Furthermore there will be various means by which the user will interact with the program, via keyboard, mouse movement or mouse button.

A GUI environment handles the various and random user requests by introducing an "event loop", which is a stack of the various input instructions the user provides, such as keystrokes and mouseclicks, and provides a set of handling functions that define what action to take when that instruction is received. When the program is run, the handling functions are set up and then the loop is started, and keeps running until a "quit" event is processed (e.g. when the "Exit" entry in a menu is pulled down and selected).

You can, therefore see how the important parts of a GUI application are the various elements of the display - the menu bar, buttons, text boxes, etc, each of which has various functions assigned to it. In this way, GUI programming can be think of as naturally "object-oriented", and one would expect an object-oriented programming language would be most useful for developing such applications. Nevertheless, by defining each event-handling function in turn and starting up the event loop, it is quite straightforward to create a GUI program using a traditional procedural language such as C.

GTK+ has in fact been designed for use with plain C, on the one hand because a majority of unix programmers are probably already familiar with that language, and on the other hand because at the time of early development of the library, compiler for C++ were still not as well-matured as those for C. Furthermore, GTK+ itself may be considered written in an object-oriented manner, this being what distinguishes "GTK+" from the original "GTK". Nevertheless, a set of bindings have been produced making the GTK+ library accessible to a number of different languages, including C++, perl, python.

Next: First things in GTK+
Back to index