By John
Looney
After a few weeks of playing with GTK, the hot new widget set from the
people that brought you the GIMP, I've learned plenty, that I'd like to
share with you all.
What is GTK ?
To save programmers much of their valuable time, the people that
developed X thought it would be a cunning plan to have a set of convenience
functions that would allow programmers to draw buttons, menus and the like.
The result was "Athena". For it's time, it was very cool. Now, it sucks so
badly, that if you name a really cruddy looking application, chances are,
it uses Athena to do it's buttons. It was only meant as "An example
toolkit", so we can let them away with it. Straight away, other programmers
sought fame and fortune with their own toolkits. Motif was a commerical
one written by Hewlett Packard, and more widget sets, free and commerical,
have followed - openlook, qt, aw3D, lesstif, xforms, to name a few.
What's a widget?
As Longword said to me many years ago, "It's all done with widgets".
Look at netscape. Everything from a button to the menu, to the HTML window
is a widget. A widget is a component of a GUI that has it's own class (in
the object oriented sense) that can represent an atom that you see on the
screen. Widgets can be composite - a file selection box widget is a widget
that is made of a few button widgets, two list widgets, two text entry
widgets and a label widget or two. Widgets are often made to look like
'objects', and object heirarchys can be built, to allow programmers
understand how they work. For instance:
Radiobutton is a type of Button
Button is a type of Label
Label is a type of Box
Box is a type of Widget
File selector is a type of Composite
Composite is a type of Manager
Manager is a type of Widget
Any operation that is relevant to a widget is relevant to the widgets that
are types of it. So, if you can put loads of widgets in a "Manager" widget,
then you can put loads of widgets in a composite widget too. If you can
create a widget, then you can create a Box widget too. If you can put a
pixmap on a button, you can put a pixmap on a radiobutton.
Why is GTK here?
So, the boys that were doing the gimp took a long hard look at them,
and
decided that there wasn't a decent free toolkit, and set about making one.
GTK stands for GNU Toolkit or GIMP Toolkit, whatever you want.
More specifically, GTK is now a set of libraries that enable
programmers to make a decent front end to their programs, that can be
ported to everything from X, Win32 to AmigaDOS. They get this portability
via two extra libraries, GDK and GLIB. GLIB is a simple library that is
fairly platform generic, that has "types" - so a 'gpointer' is the same on
every platform, no matter the OS/hardware. It also has some things like
effienct linked list code, to save you doing your own. Some nice
non-graphical convience routines. GDK is there to make a win32 window the
same as a X11 window, etc. A sort of graphical abstraction layer. It
actually does an awful lot of abstraction work, but the end result is still
very X-like. The GTK library then sits on this, and as a result, there
isn't a line of X specific code in GTK.
Do I want it?
Well, if you want to develop XWindows software for linux owners, and
want it to look nice, and lean, it's definitly worth a look. For
non-programmers, new software like Electric Eyes, Enlightenment and GNOME
are going to use GTK, so you are going to have to install it, if you intend
to migrate away from KDE/fvwm/WindowMaker in the next year or so, or the
newer applications that will come out and use these libraries catch your
eye.
What's it like to program?
It's not easy to jump into. I'll give it that. It's a good idea to have
previous knowledge of how X11 works, and general GUI/Widget design, but I'm
sure people could blunder along, and learn how it works.
The way it works is:
- You "create" a widget
- You set some properties [1]
- You pack the widget
- You set "signal" handlers for the widgets events
- You "show" the widget
This looks, and is, pretty straight forward. When you create a widget,
the structures that hold it's information are allocated and initialised.
It's now a para-widget - it doesn't exist on the screen, but your code can
interact with it. You set properties - like it's title, size, colour etc.
The footnote [1] is because not all properties are settable yet. Things
like a label, with a pixmap in it. Because it doesn't exist on screen, you
can't do things that would require a real window, or a font. They have to
wait until after the widget is shown. This was very confusing, until I
copped it. You can force the windows to be created, and the fonts loaded,
but not display them, by "realizing" the widget, so it's not a problem,
really.
Packing is a bit odd, for people that never used it before. Packing
basically takes a widget, and assigns it a logical parent. Motif allowed
you to say "Attach this widget to the left and right sides of it's parent".
Then, if you resized the window, the widget would stretch in the x
directions, but not in the y directions. It meant a lot of control, but a
lot of code. GTK allows you to put a widget in a container widget, and it
takes care of placement. You can specify some policies - like keep things
left justified and the like, but in the main, the programmer proposes, and
GTK disposes. It means that you have less power on where widgets go, but
like OpenLook, applications look better, and more alike.
Signals are what Xt called "callbacks". The idea is that a widget can
be sent a callback. Buttons would be sent callbacks like "activate" and a
text editor widget would have a "keypressed" and "close" callbacks. GTK
really gives you loads of different signals to trap, not just the obvious
ones. You can also make up your own signals to widgets, and then have other
threads initiate them. No idea why, but it could be used by someone.
Xt based widget sets have a "XtSetBackground()" and the like. The gtk
widgets set had it's own methods, and in general, each property has it's
own function for setting the variable in the widget. Quite nice. Because of
the device abstraction though, it has some quirks that a '-Wall' option is
needed to remind you of. For instance, in an event window, mouse positions
are marked as doubles not integers, like you would expect. This is just
in case GTK is ported to a system that has variable size pixels,
apparently!
How does it compare to competing widget sets?
This is a toughy. I've not used that many - Motif, OpenLook, Athena,
and QT are the only ones I've used. GTK syntax is nicer (once you get used
to it) than everything but Motif. The big drawback is, that it doesn't use
"Xt". Xt is a library designed for building toolkits, and is very good at
it's job. The GTK people decided that they would not use Xt, because Xt
isn't availible on any other platforms.
On the plus side, because it doesn't use Xt, it's a lot more flexable. You
can make widgets when ever you want. Motif and OpenLook required you to
make a widget before any of it's children. GTK lets you make them all the
same time, then you "pack" them into their parents later, just like Tk/Tcl.
How do I learn how to use it?
Go to http://www.gtk.org and download the library. Install it. Then, go
through the excellent (if incomplete) tutorial, and have a look through the
good (but extremely incomplete) reference manuals. I can provide some
example source code, if you want to play around.
Related: Beginning GTK /GNOME Programming
About the author, John Looney.
USERS COMMENTS
|