General Structure of Gtk+ Applications

A Gtk+ program has a structure that is defined by the way the toolkit interacts with the system. A graphical program is defined by a continuous loop, where it receives messages from the environment (such as mouse clicks, key presses, for example) and translate them into actions visible in the UI.

Most graphical application toolkits have a proper way to handle the event loop mechanism. Gtk+ is not different, and you will will see that the event loop must be defined in some way in the main function.

The major difference between Gtk+ and other graphical UIs is that it provides simple abstractions of the event loop. So, instead of creating a real loop using something like a while in the C language, the programmer can opt to use a single function such as gtk_main, which is responsible for handling all messages and doing the correct dispatch.

Another difference in favor of Gtk+ is the use of the signal/event paradigm. Thanks to signals, Gtk+ applications don't need to check for events as they arrive at the message loop. Application writers can simply define the events handlers that will be connected to a signal, such as a button click. Once this connection is made, the code responsible for handling the message will be automatically called. We will explore the signal mechanism later, but it is important to realize that it is a major part of the simplicity of Gtk+.

The simplest possible application in Gtk+ is one that just initializes the system and calls gtk_main in order to handle the events.

Initialization is necessary because there must be some way to tell the Gtk+ toolkit that it will take control of the program. Also, internal data structures need to be created in order to handle specific functionality.

Once initialization is performed, the application executes code to declare and create user interface elements, such as the main window, menus, dialogs, and other elements that might be necessary.

Finally, when the application controls have been setup, it is time to start the normal operation of the application. Like all graphical applications, this entails to run the main event loop, which will continuously look for new events and execute the necessary actions in response.

Tags: Gtk, programming, Linux, events, signals, main, loop, structure
Article created on 2010-08-19 15:27:43

Post a comment