Writing Applications with GTK+ 1 Introduction
Total Page:16
File Type:pdf, Size:1020Kb
+ Writing Applications with GTK Owen Taylor April Introduction Graphical user interfaces have b ecome almost universally familiar However it may b e worth saying a few words ab out how graphical user interfaces work in Linux and X from the p oint of view of the programmer The X server is resp onsible for only the simplest op erations of drawing graphics and text on the screen and for keeping track of the users mouse and keyboard actions Pro grams communicate with the server via the Xlib library However programming applications in straight Xlib would b e a tremendous chore Since Xlib provides only basic drawing commands each application would have to provide their own co de to user interface elements such as buttons or menus Such user interface elemenets are called widgets To avoid such a lab orious job and to provide consistancy b etween dierent applications the normal practice is to use a to olkit a library that builds on top of Xlib and handles the details of the user interface The traditional choices for such a to olkit have b een two libraries built up on X Intrinsics libXt library distributed with X the Athena Widgets which are distributed with X and Mo 1 tif However Xt is complicated to learn and use the Athena Widgets havent lo oked stylish since and Motif while somewhat more up to date is large slow has an app earance disliked by many p eople and most imp ortantly is a proprietary pro duct without freely available source co de For these reasons and others much recent development has fo cused on to olk its that build directly on Xlib There are dozens of such to olkits available Per haps the most prominent are John Ousterhouts Tk to olkit tightly integrated 2 with the Tcl language and the Qt library by Troll Tech which is written in C 1 Motif is a registered trademark of The Op en Group 2 Qt is a trademark of Troll Tech AS History + GTK was created as part of the GNU Image Manipulation Package GIMP Early revisions of the GIMP used Motif but when a ma jor revision was b egun in Peter Mattis decided that b ecause of its proprietary status Motif was not suitable and b egan work a new to olkit called the GIMP Toolkit GTK In early additional ob ject oriented features were added including inheritance and a exible signal system for callbacks and the to olkit was renamed from GTK to + + GTK Because the original version was never released GTK is often referred + to simply as GTK and even when written as GTK is typically pronounced jeeteekay not jeeteekay plus + Why GTK + GTK oers a number of advantages over other avaqilable to olkits It is written entirely in C Not only are high quality C compilers universally available but the Conly makes it very easy to integrate with existing systems as well + Interfaces for GTK exist for many dierent languages Well developed bindings exist for in addition to C C guile Perl and Ob jective C Bindings also exist or are in development for Python Java Eiel Mo dula OCaml and TOM As has long b een known by users of TclTk GUI development is much more ecient when done in a highlevel scripting language Although p orts of Tk have b een done to a number of languages other than Tcl they have b een hobbled b ecause Tcl is heavily integrated into the Tk core so each p ort has required pro ducing a mo died version of Tk or has required interfacing to Tk + through Tcl The ob jectoriented design and straightC approach of GTK + mean that all the ab ove mentioned bindings use unmo died versions of GTK + GTK is licensed under the GNU Library General Public License LGPL + his ensures the continued availability of the GTK source co de and mo dica tions to it and means allows it can b e used without charge in all applications This contrasts sharply with Motif for which the source co de is not available without paying large amounts of money and Qt which is free only for non commercial use As you might exp ect for a to olkit that originated as part of a graphics pro + gram GTK has a very attractive default app earance The user interface el ements are crisply dened and well spaced The colors fonts and background pixmaps of GTK widgets can b e conveniently customized by the user via a con guration le system If that isnt enough work is currently in progress to add + Themeability to GTK Themeability b est known from its implementation the Enlightenment window manager will allow complete customization of the user interface without recompiling applications + GTK also provides transparently provides supp ort for a number of X exten sions With at most trivial mo dications a GTK program will allow the input of Chinese and Japanese text via the XIM extension and use shared memory for + images via the XShm extension A more unusual feature of GTK is supp ort for + the XInput extension Programs using the GTK can again with only minor mo dications take advantage of pressure and tilt information from a graphics tablet Design Principles + There are a number basic design decisions that distinguish GTK from other + to olkits The most basic one was mentioned ab ove GTK is written in pure C to allow the maximum p ortability exibility and eciency + Another basic principle used in GTK is Evenrything is a widget In + most to olkits a button or menu item simply has a xed text In GTK buttons and menuitems are simply Container widgets The could hold a Lab el widget to display a text string a Pixmap widget to display a picture and Arrow widget a DrawingArea widget where the application draws its own graphics and so forth General Ideas Structure + The GTK package consists of three libraries The rst libglib provides two things systemindep endent replacements for nonp ortable routines and routines for manipulation of generic data structures The extensive use of these + + data structures in the rest of GTK and in GTK applications simplies the co de and improves p erformance by allowing sophisticated data structures such as hashes and caches to b e conveniently used The second library libgdk the GIMP Drawing Kit provides a wrapp er around the raw Xlib functions This hides much of complications of X from the + rest of the co de and should make p orting GTK to other windowing systems + somewhat easier Finally libgtk includes the co de for the GTK ob ject system and for the widgets itself GtkOb ject GtkFixed GtkData GtkList GtkAdjustment GtkMenuShell GtkTooltips GtkMenuBar GtkWidget GtkMenu GtkContainer GtkNoteb o ok GtkBin GtkPaned GtkAlignment GtkHPaned GtkEventBox GtkVPaned GtkFrame GtkScrolledWindow GtkAsp ectFrame GtkTable GtkHandleBox GtkToolbar GtkItem GtkTree GtkListItem GtkDrawingArea GtkMenuItem GtkCurve GtkCheckMenuItem GtkEditable GtkRadioMenuItem GtkEntry GtkTreeItem GtkSpinButton GtkViewp ort GtkText GtkWindow GtkMisc GtkColorSelectionDialog GtkArrow GtkDialog GtkImage GtkInputDialog GtkLab el GtkFileSelection GtkTipsQuery GtkBox GtkPixmap GtkButtonBox GtkPreview GtkHButtonBox GtkProgressBar GtkVButtonBox GtkRange GtkHBox GtkScale GtkCombo GtkHScale GtkStatusbar GtkVScale GtkVBox GtkScrollbar GtkColorSelection GtkHScrollbar GtkGammaCurve GtkVScrollbar GtkButton GtkRuler GtkOptionMenu GtkHRuler GtkToggleButton GtkVRuler GtkCheckButton GtkSeparator GtkRadioButton GtkHSeparator GtkCList GtkVSeparator + Figure The GTK class heirarchy GtkEntry GtkEntryClass GtkEditable GtkEditableClass GtkWidget GtkWidgetClass GtkObject GtkObjectClass klass "destroy" : gtk_entry_destroy "show" : gtk_entry_show "changed":NULL Figure Memory layout of GtkEntry and GtkEntryClass structures Inheritance + Despite b eing written in C GTK is fully ob ject oriented A metho d is invoked on an ob ject by calling a function that takes the ob ject as its rst parameter For instance to show a widget on the screen one calls the function void gtkwidgetshow GtkWidget widget As an another example to set the text in an Entry widget one calls void gtkentrysettext GtkEntry entry const gchar text A co de fragment that uses hese functions lo oks like GtkWidget entry gtkentrynew gtkentrysettext GTKENTRY entry Edit Me gtkwidgetshow entry The interesting thing here to note is the use of the GTKENTRY macro A GtkWidget was returned by gtkentrynew but gtkentrysettext exp ects GtkEntry To get from one to the other we use the GTKENTRY macro Actually we could have written just as well gtkentrysettext GtkEntry entry Edit Me + When debugging checks in GTK are disabled the GTKENTRY macro just a convenient way to write the cast When debugging is enabled the macro also do es some checks to make sure the p ointer we are converting into a GtkEntry actually p oints to an Entry widget So how can the same p ointer p oint b oth to a GtkWidget and a GtkEntry Do esnt the ob ject in memory have to to b e either one or the other For the answer to that see Figure What is going on is that the we have nested structures Inside the GtkEntry structure is a GtkEditable struc ture Inside the GtkEditable structure is a GtkWidget structure and inside the GtkWidget structure is a GtkObject structure All of these structures b egin at the same place in memory so the the same p ointer can b e used as a GtkEntry a GtkEditable a GtkWidget or a GtkObject + In this manner GTK implements a heirarchy of widgets along with a few nonwidget ob jects See Figure At the base of the heirarchy is the GtkOb ject type which provides various functionality common to all Ob jects such as memory management There is actual a second set of structures nested in the same Russiandoll fashion This is illustrated by our other call gtkwidgetshow entry As you might imagine dierent things are involved