Visual Development with Mono, GTK # and Glade. Part I

This multi-part article describes how to use Glade, a development tool that aids in creating a GTK+ based visual interface for Mono applications. Using Glade is a bit like using the visual designer in Visual Studio or Delphi. One big difference, however, is that in Visual Studio you use Windows.Forms to create a .NET GUI interface, while in Glade you use GTK+ to create a visual interface.

Though Glade has a nice visual designer, it is not a full featured development environment like Delphi or Visual Studio. It does, however, provide tools that allow you to drag, drop, arrange and configure visual elements such as buttons, edit controls and list boxes.

All of the tools discussed in this article, including Gnome, Glade, Mono, GTK+ and the GDK are open source projects. This means that they are distributed with source. More importantly, both Mono and GTK+ are cross platform technologies. This means that they run equally well on Linux or Windows.

This article begins by explaining the technologies used in Glade development. Once this background material is out of the way, the second part of the article outlines the simple steps necessary to create applications with Glade and Mono.

Mono and Gnome

Mono is the Linux implementation of the Microsoft .NET framework. It is based on open standards such as the C# standard and the Common Language Infrastructure defined by the ECMA group.

The C# language was created at Microsoft by Anders Hejlsberg, Scott Wiltamuth, and Peter Golde. The standard was submitted to the ECMA group by Microsoft, Hewlett Packard and Intel.

C# is designed to be simple and safe to use. It supports cornerstones of modern language design such as strong type checking, array bounds checking, detection of uninitialized variables, and garbage collection. Though not intended to be as fast as C or C++, it is designed to reflect much of the syntax found in those languages. This was done to make it easy to attract new developers who are familiar with those languages. C# was also designed to support distributed computing.

Since the Mono group is interested in the Linux operating system, they were attracted by the fact that C# was also designed to promote cross platform development. This is, in fact, one of the goals of C# specification, as laid down in the official ECMA documents.

The Mono project has complete implementations of the Mono specification for C#, and the CLI. It also supports web and database development. That is, it implements ADO.NET and ASP.NET.

Mono has had more trouble, however, implementing a .NET based GUI front end for desktop applications. In Microsoft’s implementation of .NET, the GUI front end is handled by a toolkit called Windows.Forms. The Mono team has had trouble implementing Windows.Forms on Linux. As a result, an alternative to Windows.Forms, called GTK#, has emerged. GTK# is similar to Windows.Forms, runs on both Linux and Windows, and has in Glade a visual development environment similar to Delphi. If you have patience, you will find that the next few sections of this article will, in a methodical way, explain what GTK# is, and how it is structured.

Gnome

The Mono project is headed by Miguel de Icaza. Miguel’s background includes credit for being the chief architect behind Gnome. Gnome is a complex toolkit that consists of several disparate parts including an implementation of CORBA and a toolkit called Bonobo which is similar to Microsoft COM. However, Gnome is known most widely as a desktop environment similar to KDE or Windows. The goal of the Gnome desktop environment is to put an easy to use GUI front end on Unix. The Gnome desktop includes tools like a file manager, a panel for managing applications and windows, and various other components too numerous to mention.

Gnome, however, is more than just a desktop environment. It is also a development environment, a suite of API’s that a developer can use to access and control many features of a computer. In particular, it gives the developer control over the X Window System.

The Gnome API’s are not limited to GUI development. You can also use Gnome to develop command line applications. However, for the purposes of this article, Gnome is important because it provides a GUI development API similar to that provided by Windows.Forms.

Because the Gnome API’s are based on a desktop environment, GUI’s developed with it have a consistent and integrated look and feel. A Gnome based GUI can be run on top of the raw X Window System, on top of Microsoft Windows, or on KDE, but it has the most consistent and unified look and feel when it runs on top of the Gnome desktop.

Glib, GDK and GTK+: Important Libraries

Gnome is based on several external libraries. In particular, it relies on two libraries called Glib and GTK+. Before you can fully understand GTK#, it is best that you read at least this brief introduction to GTK+ and Glib.

Glib consists of a number of utility functions, many involving solutions to portability related problems. The developers of Glib were interested in cross platform development, and in particular, they wanted a consistent API on which to develop cross platform applications. A problem arose in cross platform development when a fundamental routine was needed on two different platforms, but was only available natively on one platform. Glib was created in large part to solve such problems. In particular, any routines that were needed on multiple platforms was implemented as part of Glib. Developers could then install the Glib library on a platform, and then map into existing Glib routines to get the functionality they needed.

It is interesting to note that in many cases problems of this kind were resolved by adding a routine to a particular language. For instance, when a routine for string manipulation was needed in the C language, sprintf was developed. When C was ported to different platforms, the sprintf routine came along as a matter of course. But the creators of Glib took a different approach. Instead of embedding a particular routine in a language like C, they imbedded it in the Glib library itself. From the beginning, Glib was designed to be run on multiple platforms and to support multiple languages. So rather than building the routine into any particular language, the routine was built into Glib, and then mappings to Glib were created for a wide range of languages, including the Mono implementation of C#.

It is interesting to note that GTK was not originally created for use in Gnome. Instead, GTK was designed to be one of the tool kits used by a sophisticated drawing program called the Gimp. In fact, the letters GTK stand for the Gimp Tool Kit. Another important library, called the GDK, is the Gimp Drawing Kit. Both the GTK and GDK depend on Glib. Gnome, in its turn, relies on GTK+, GDK and Glib. In other words, the architects of Gnome choose to rely on the same graphics libraries as the Gimp. This has proved to be a wise decision, for the GTK has been a good base on which to build.

GTK+ includes a widget library for creating controls such as buttons and list boxes. Though these controls were originally meant only to be part of the Gimp, they have now become part of Gnome, and serve as the widget set for the whole Gnome library. GTK+ also includes a type system, and a set of routines for implementing events. In GTK+, events are called signals. As you can see, GTK+ is an sophisticated library that plays a big role in Gnome development.

Because GTK+ calls into the GDK and not into the X Windows System itself, it has been possible to rewrite the GDK to hook into windowing systems on other platforms such as Windows. As a result, GTK+ and GDK run on multiple platforms, including Windows. This means you can write a single GTK+ based application and have it run unchanged on Windows and Linux.

Mono, GTK+, GTK# and Gnome

GTK# is a C# wrapper around GTK+. It therefore provides a cross platform way for Mono based applications to have access to a powerful, and well thought out, visual library of widgets and drawing routines. Mono developers using C# can call the routines in GTK# to manually create a visual interface for an application. In terms of complexity, such an operation is similar to creating a window in the raw QT API. That is to say, it is simpler than having to write a raw Win32 API application by hand, but it is more difficult than using a tool like Delphi or Visual Studio to draw the interface for an application.

Glade was designed to bring visual development to GTK+ and wrapper languages such as GTK#. Using Glade, you can create an XML file that defines a visual interface for an application. Using GTK#, you can automatically load that interface and display it to the user with a single, easy to use call.

An example of an application developed with GLADE and the GTK# library is shown in Figure 1. Applications such as this one can be quickly assembled using Glade and a very few lines of GTK#. An exact explanation of the process will be shown in part 2 of this article.

Figure 1: A simple Mono application written using the GTK library.

Summary

In this article you have learned that GTK# is a wrapper around a visual library called GTK+. This library forms the basis for the desktop environment called Gnome. Because Gnome is a complete, and sophisticated tool, it demonstrates that one can use GTK+ to create sophisticated visual applications. By creating a mapping from C# into GTK+, the Mono team has a found a good way for their developers to create sophisticated graphical user interfaces for their applications. Such applications are built on standards based, open source code that can be run on either Windows or Linux.

No comments yet

Leave a Reply

You must be logged in to post a comment.