home " subscribe " advertise " customer service " back issues " " contacts

Sections
  Newbies
  Reviews
  How To
    Best Defense
    Guru Guidance
    On The Desktop
  Developer's Den
    Gearheads Only
    Compile Time
    Perl of Wisdom
  Who's Who
 
Indexes
  Issue Archive
  Author Index
 
Linux Magazine
  Subscribe
  Advertise
  Customer Service
  Back Issues
  
  Contacts
 
On Stands Now Click to view Table of Contents for Linux Magazine March 2000 Issue
 
Subscribe to Linux Magazine

Linux Magazine / July 1999 / COMPILE TIME
GUI Building With GTK+
 
       page 01 02 03   next >>

COMPILE TIME
GUI Building With GTK+
by Eric Harlow

While developing the GIMP (GNU Image Manipulation Program) a few years back, Spencer Kimball and Peter Mattis decided that it might be fun to write a user interface toolkit to go along with it. And so they created GTK+ (the GIMP Toolkit) -- a library of "widgets" that makes it easier for developers to build GUI-based application. A widget is one of the various components of a graphical application; a toolbar, for example. The widgets provide programmers with pre-built dialog boxes, windows, menu bars and other GUI components. Having a pre-built foundation for these user interface components greatly speeds development of GUI applications and also enables applications built with those widgets to share a common "look and feel".

For those of us seeking a toolkit that does not restrict us to any one operating system or language, GTK+ is worth checking out.

While the GTK+ toolkit is written in C, there are language bindings available for a variety of languages including C++, Pascal, Guile, Perl, and Python. And the fact that the toolkit is released under an Open Source license makes de-bugging applications and learning about GTK+ easier than it would be with a proprietary toolkit.

GTK+ has been ported to a variety of platforms, including Solaris, HP-UX, MS Windows, and Irix, as well as Linux. It includes a library of routines (called GLIB) to support non-graphical elements required in an event-driven application such as linked lists, trees,sockets, and memory allocation to help make applications more portable.

GTK+ comes with a large collection of widgets, from simple components like the standard button and text widget, to more complicated components like the color selection and file selection widgets. GTK+ also makes it easy to extend existing widgets and create new ones.

While it would be impossible to teach GTK+ development in a single article, the basics of writing and implementing a GTK+ application are simple. Spencer, Peter, and the literally hundreds of contributors to the project have provided a wide range of widgets to make even the most complex graphical application easier to manage. As mentioned earlier, a widget is a GUI component. Text boxes, buttons, labels, and windows are all widgets. By organizing these widgets together and writing a few functions to process the messages the widgets send to each other (called "signals"), applications can be easily created. (Okay, it's not quite that simple, but let's not go there just yet.) Each widget also has at least one widget creation function and usually several properties that can be modified to change the characteristics of that widget.

Container Widgets

Some widgets can act as containers for other widgets. The standard button widget, for example, (GtkButton) usually has a label in it. This label is, in fact, a label widget (GtkLabel). The label widget can be replaced with a graphic image instead of a text label to create an image button.Container widgets can only have one widget placed within them, but this limitation can be circumvented by using packing widgets.

Packing Widgets

Packing widgets can hold more than one widget. They are not visible, so they're usually placed within a container widget, giving the container widget the appearance of holding many widgets. Packing widgets come in three varieties: The vertical packing box allows widgets to be stacked vertically; the horizontal packing widget allows widgets to lie next to each other from left to right; finally, the packing tableprovides a grid to place widgets on in any direction.

Packing widgets can be placed within other packing widgets for more elaborate effects than the basic top/down or left/right displays. The packing widgetseliminate the use of absolute x and y coordinates when placing widgets. Most applications don't care about the position of the widget as long as one widget is on top of another or next to another. Manually keeping track of the height of a widget and the position of the one above it as the screen resizes can be an unnecessary pain.

Inheritance

Using inheritance, the simple widgets in GTK+ can be combined to make more complex widgets. Since GTK+ is written in C, which is not a language that has inheritance built in to it, a fair bit of work was done in the toolkit to make inheritance to work. All widgets inherit from the base widget class (GtkWidget) and many widgets have multiple levels of inheritance.

The Radio Button in GTK+ (GtkRadioButton) is inherited from the Check Button (GtkCheckButton) which is in turn based on the Toggle Button (GtkToggleButton) which inherits some of it's behavior from the Button Widget (GtkButton). This inheritance goes all the way back to the basic widget. This allows widgets to reuse code instead of duplicating the same code for several identical widgets.

Compile GTK 1
Button: The result of the Simple Button Example.

Properties

Widgets employ a number of properties. Frequently used properties include the visible property which can be used to display or hide a widget. The gtk_widget_show function and the gtk_widget_hide function are used to manipulate the visible property.

Other properties are less frequently used or are more specific to particular widgets. For example, the toggle button widget has a property that indicates the state of the button (up or down) and the state can only be set or queried by the toggle button or any widgets that inherit from the toggle button.


       page 01 02 03   next >>
 
Linux Magazine / July 1999 / COMPILE TIME
GUI Building With GTK+

home " subscribe " advertise " customer service " back issues " " contacts