Chapter 1. Background

If you are totally new to Gtk+ and Gtk2-Perl, you are welcome to read this part in order to gain background knowledge. It is always better to see the forest before looking at each of the trees.

Background on Gtk+

Gtk+ was created for the Gimp Image Manipulation Program (GIMP). It is a widget toolkit, written in C, but has bindings for various languages including Perl and Python. The Gtk+ toolkit, including Gtk2-Perl is released under the LGPL. The LGPL allows you to use the Gtk+ toolkit when creating commercial applications.

Background on Gtk2-Perl

Gtk2-Perl is the perl bindings to the Gtk+ toolkit, so now you can use Perl, instead of C to write your programs. When we talk about Gtk2-Perl, we actually speak of three parts.

Gtk2: "Perl bindings to the 2.x series of the Gtk+ widget set. This is a series of widgets such as labels, buttons, and scrollbars, which are organized in an OOP structure. This module allows you to write graphical user interfaces in a perlish and object-oriented way, freeing you from the casting and memory management in C, yet remaining very close in spirit to original API."

Glib: "This module provides perl access to Glib and Glib's GObject libraries. Glib is a portability and utility library. GObject provides a generic type system with inheritance and a powerful signal system. Together these libraries are used as the foundation for many of the libraries that make up the Gnome environment, and are used in many unrelated projects." When you need to acces certain constants for instance, you will use this module. EG:use Glib qw(TRUE FALSE);

Gtk2::Gdk: "This module encapsulates the Windowing system (X or Windows) underlying Gtk+. It contains routines to draw on screen, and react to various mouse or keyboard events. It is automatically included when you specify: use Gtk2;". [1]

Why use Gtk2-Perl

  1. Perl is a very mature language. It has hundreds of modules which are very easy to use in any Perl program, the Gtk2-Perl modules are no different from the rest.

    Example: It is very easy to use any of the CPAN modules in the same script that houses the Gtk2 GUI. To add the Perl DBI (DataBase Independent) module for instance, simply add the DBI module in the use clause at the beginning of your program, to inform the Perl interpreter that you will also use it. The same goes for LDAP, SNMP, or any of the CPAN modules.

  2. Perl has a very gradual learning curve. This allows novice programmers to start of easy, and as time goes by, they can progress to more advanced techniques.

  3. The Gtk+ toolkit is used in almost all the modern GNU/Linux distributions, for example the GNOME desktop. Starting with Gtk2-Perl will expose you to the toolkit, using a easy to understand programming language.

  4. Simple applications are quick to construct in Perl.

  5. Perl's modularity allows for creation of "pluggable" modules, this eases scaling.

  6. With Gtk2-Perl there is no compiling, type casting, or any of the complexities that keeps people away from languages like C.

  7. Gtk2-Perl is Open Source.

Modules and classes and Perl

There are two concepts that may confuse a first time user. Perl modules VS the Gtk+ C API object classes.

Remember that Gtk2-Perl was written as a binding to Gtk+, which is in C. Gtk+ is designed in a OOP manner. This will allow certain class inheritance. We may have that the Gtk2::Window inherits methods and properties from Gtk2::Widget. They BOTH may be under the Gtk2 namespace in Perl, but their inheritance are according to the C API. [2]

Components of a Gtk2-Perl application

The building blocks of your program are the Gtk2::Widget. Every Gtk2::Widget inherits from Glib::Object. All methods that creates an instance of a widget, will return a blessed reference to Glib::Object. Widgets are created by the new or new (something) methods. These methods can contain arguments. Widgets are destroyed by the Gtk2::Widget class's destroy method. You can add your own data to a Glib::Object instance. This is very handy to carry specific data with a widget.



Another definition: Gtk2::Gdk (GTK+ Drawing Kit) is an intermediate library between the X server and Gtk+. It handles basic rendering such as drawing primitives, bitmaps, cursors, fonts, as well as handing window events and drag-and-drop functionality. Gtk2::Gdk is an important port of Gtk+'s portability. Since low-level cross-platform functionality is already provided by GLib, all that is needed to make Gtk+ run on other platforms is to port Gtk2::Gdk to the underlying operating system's graphics layer. Hence, the Gtk2::Gdk ports to Win32 and Quartz are what makes Gtk+ applications run on Windows and Mac OS X, respectively.


This avoids having something like Gtk2::Object::Widget::Container::Bin::Window->new() just to create a new window!