The C programming language is a fine language that many of us hold dear. But it's not necessarily the right solution for every situation, and it certainly isn't the fastest way to create one of the most common classes of applications today: end-user-oriented graphical user interface (GUI) applications. When you don't care about raw number-crunching power or the ability to directly touch low-level aspects of the machine, suddenly those strengths turn into a hindrance, wasting something very important and expensive -- the time of your developers.
For this reason, many different tools were created to help developers concentrate on writing important code instead of fighting with the unnecessarily verbose and bothersome low-level aspects of the language used. Those tools include higher-level languages, integrated development environments (IDEs), and various utilities for shortening and automating common tasks. This article doesn't try to cover all these subjects in depth; instead, it provides an overview of the different options available for when you're using the GTK+-based toolset.
Recognizing the need to work with differing tools, the GTK+ team set out from the beginning to create a library
that could be easily and rapidly bindable into other languages. By language bindings, I mean a specific kind
of glue that allows one programming language to present a native interface to the programmer while actually
delegating all the work to an underlying library written in another language. Here, the same low-level nature of
C that makes it a bother to work with also makes it a perfect choice as a basis
for those higher-level bindings, because just about everything can talk to C.
While the full array of languages in which you can interface with GTK+ is rather vast -- with each bindings set being an independent project with its own level of support and completeness (see Resources for details) -- there are some that warrant a closer look, either because of their outstanding quality and popularity or just to show how very different languages can be used to program GTK+.
One of the most popular bindings is the Python set, developed by the PyGTK project. Thanks to the exceptional quality of their work as well as the general joy of working with the Python language, PyGTK is one of the most renowned bindings and also the only one so far to be officially included in the core GNOME desktop release. This means that Python has been recognized as good and popular enough to allow applications written in PyGTK (as well as the PyGTK itself) into GNOME proper instead of being treated as a third-party component. Of course, that doesn't mean other bindings aren't good, but if you enjoy working with Python, you're sure to like PyGTK as well.
Beside the basic set of GTK+ libraries, Python has full coverage of the GNOME development platform. So, in writing your applications, you can use all the shared facilities of the GNOME platform, making your development time even more efficient.
The latest offering in the development department from the Redmond giant, Microsoft® .NET is
also popular on platforms other than Microsoft Windows®, thanks to the free implementation by
Mono project. What you might find surprising, however, is just how well adopted it is. In fact, there's a good
chance that the most popular GUI toolkit for .NET won't be Microsoft's own WinForms, but GTK+. Since the
Gtk# bindings set is shipped with the default Mono distribution and because of the quality of GTK+ itself, GTK+
has seen a rapid uptake by programmers wanting to develop for the desktop with the new .NET platform.
Coupled with the great productivity of .NET, the result is many new applications being written in
C# and GTK+, including a few high-impact applications such as the Beagle project,
which allows you to rapidly and effectively search through all sorts of information stored on your computer.
Note that despite the name, Gtk# also includes appropriate glue for many GNOME libraries, allowing you to use the GNOME platform when necessary (provided there are GNOME libraries on your operating system, that is). Gtk# is portable to Windows as well, so you don't have to spend too much time porting.
And now for something completely different. Haskell, the lazy functional language, is unlike any of the other languages presented so far, which are all imperative. Thanks to that radically different approach, Haskell's conciseness and expressiveness is unmatched in many applications, particularly in those requiring mathematical reasoning. For this reason, it's a very useful tool to have in your arsenal, and using the results of Gtk2Hs project, you'll be able to pack your pretty mathematical tidbits into an equally pretty user interface (UI).
Gtk2Hs sports comprehensive coverage of bindings (up to GTK+ V2.8), including important tools such as libglade, decent documentation, and a friendly monadic application programming interface (API) similar to that offered by more traditional languages, because while functional programming is certainly fun, it's just not the right way to express user interaction. In addition, thanks to the reference-counting scheme that GTK+ employs, Gtk2Hs is used in a Haskell-ish way without requiring you to explicitly manage your memory, unlike some other GUI libraries for Haskell (for example, wxHaskell).
Going back to imperative languages, the Java™ programming language -- one of the most popular environments -- also doesn't have anything to be ashamed of. The Java-GNOME project's efforts have produced a robust bindings set that covers both the latest revision of GTK+ and GNOME libraries. And, in the case of the Java language specifically, you have the added option of using GTK+ through the Eclipse Standard Widget Toolkit (SWT). That's good news if you're using the Java language to create your desktop applications, because you can also use GTK+ without difficulty.
General notes on language bindings
When writing GTK+ code with bindings, keep in mind that while all the usual features of the library are available,
there might be (depending on maturity and the exact nature of the language in question) certain areas where
full support for everything you can do from C code isn't available. In particular,
new class derivation is a bit problematic for scripting languages, placing certain restrictions on the exact things
you can do with them. However, the language teams are hard at work to address that, and you should refer
to the documentation of your language bindings to see what (if any) limitations apply.
To facilitate tracking and using the appropriate versions of bindings, the GNOME Language Bindings project
was formed. Currently, four languages (Python, the Java language, C++, and
Perl) have bindings in the project, which assures that these languages will be always ready on a predictable
schedule and cover the latest release of the GNOME development platform (including the latest version of
GTK+). Obviously, other languages are free to cover the release as well, but for languages within the project,
you have a much better guarantee about their completeness and timeliness.
Besides using a proper language for your needs, it's equally important to use the tools that best match your coding efforts. Such tools can significantly cut down on development time and cost, either by supporting the target language directly in a more intelligent way or by automating and simplifying repetitive tasks.
Of the available tools, probably the single most important tool in context of GTK+ programming is libglade. Using libglade (see " GTK+ fundamentals, Part 2: How to use GTK+"), you can get tremendous gains in code legibility, maintainability, modularity, and brevity -- all by separating the inherently visual task of specifying what your UI is going to look like from the process of writing code (that is, the application logic).
Because of its crucial importance, language bindings developers are making a point to support libglade (even though it's not (yet) part of the base GTK+ library) in their languages. Therefore, you can be sure that your language of choice will support libglade, as long as you have the option to use GTK+.
To use libglade, you must create the interface description files for it. You can use several applications to do so, starting with the original Glade2, after which the library takes its name. However, prompted by some limitations of the current stable Glade2 and coupled with long-stalled progress on the Glade3 branch, a couple of alternative UI editors have been created. Of those, the most mature today is Gazpacho (see Resources), written as a Python implementation of Glade3 design ideas. Gazpacho also sports a pure-Python reimplementation of libglade better tailored for PyGTK+ applications' needs.
Yet another category of support tools is various kinds of IDEs, which are designed to provide comprehensive help during all stages of development. Tailored to a specific language, IDEs can often offer significant assistance for programmers. In this area, Mono/.NET enjoy good support, both from the original .NET creator (Microsoft) and from the community gathered around the Mono project, which develops an accompanying IDE called MonoDevelop. As Gtk# is the GUI toolkit of choice for Mono and MonoDevelop creators, it has good support out of the box; however, for those using the Microsoft family of tools, you can easily install custom packages with appropriate definitions in the Microsoft Visual Studio® IDE.
Similarly, Java programmers have an exceptionally good, freely available IDE from the Eclipse project. To get the support for GTK+ code, you need to do little more than import the libraries into your project. You can find detailed instructions on settings on the Java-GNOME project page (see Resources).
You have different options for obtaining increased productivity beyond simply switching languages. One of the most useful techniques comes from a practical application of the old right tool for the right job rule known as embedding.
In this setup, the core application -- often with rather strict performance requirements -- is written in a low-level language, such as C language, to get the most from the hardware. However, after that, the application exposes an extension interface in a different, higher-level language to make it easy and painless to enhance and customize the interface's behavior.
Using embedded scripting, you can usually get the best of both worlds, with a slim and perfomant core and easy extensibility. A language perfectly fit for embedding is Python, which was originally written just for that purpose. For a prime example of how well such a combination can work, see Nautilus and Epiphany (both GNOME projects) exposing their extension API to Python programs. Thanks to Python's brevity, you can successfully create extensions that are useful despite being only a couple of lines long.
Increase quality and do better work
As you could see above, you have a variety of options to consider whenever you want to accelerate your GUI development with GTK+. In this short article, only a quick glance of them was possible. But using this information and the links in the Resources section, you should be able to explore the possibilities with your language and toolset of choice, all to increase quality and do better work in a shorter period of time.
Learn
-
GTK+ bindings page: Explore the official GTK+ bindings page.
-
GNOME Language Bindings: Discover more about
the GNOME Language Bindings project.
- Want more? The developerWorks AIX and UNIX zone hosts hundreds of informative articles and introductory, intermediate, and advanced tutorials on AIX and UNIX.
-
Technical
events and webcasts: Stay current with developerWorks technical events and webcasts.
Get products and technologies
-
Libglade: Visit the libglade home page.
-
Gazpacho: Discover Gazpacho, an editor for Glade UI description files.
-
Epiphany: Epiphany is the GNOME Web browser
extensible in Python.
-
Java-GNOME: The Java-GNOME projects has created a set of Java
bindings for GTK+ and GNOME libraries.
-
PyGTK: PyGTK is a set of language bindings for Python.
-
Gtk#: Discover Gtk#, bindings for the C#
and Microsoft .NET platform.
-
Gtk2Hs: Learn about the Gtk2Hs bindings for the lazy functional language,
Haskell.
-
IBM trial software:
Build your next development project with IBM trial software, available for download directly from developerWorks.
Discuss
-
developerWorks blogs: Participate in developerWorks blogs
and get involved in the developerWorks community.






