Many programming languages support GUI development as one of the core parts of its language features. C/C++ has no such library attached to it; something like the string library,IO library, and so forth, that we frequently use. This shortcoming opened the horizon for developers to pick from a wide variety of GUI library toolkit available in C/C++. GTK+ is one of them. It stands for GIMP (GNU Image Manipulation Program) Toolkit and can be used to program modern GUI interfaces.
Here, we shall adhere to the basic form of GTK+, which is its C avatar on the Linux platform. The official site to download GTK+ is https://www.gtk.org. The site contains API documentation, tutorials, and other Gnome libraries that are often used along with GTK. In fact, GTK is built on top of libraries, such as:
- Glib: It is a general purpose utility library that provides support for threads, dynamic loading, event loops, low-level data structures, and so on.
- GObject: This library provides full featured object-oriented support in C, without using C++. This library facilitates the language binding created for other languages to give you easy access to C APIs.
- Pango: This library supports text and layout rendering.
- ATK: This library provides support to create accessibility tools such as sticky keys, screen readers, and the like.
- GDK (GIMP Drawing Toolkit): This is the graphics library that provides low-level drawing functions on top of Xlib.
- GdkPixBuf: This library provides image manipulation functions.
- Xlib: This library provides low-level graphics support for Linux systems.
When writing code with GTK, we often find that many of the primitive data types are prefixed with ‘g,’ as in gint, gchar, gshort, gpointer, and so forth. These data types ensure that the code can be recompiled on any platform without making any changes. These data types are defined in these libraries to aid cross-platform development.
The main problem of GUI programming is that it is inherently object-oriented. Therefore, a procedural paradigm does not fit perfectly in the scheme. For this reason, in spite of GTK being written in C, it provides object-oriented support through GObject. Note that this object-oriented support has nothing to do with C++. C++ has its own GTK library, called gtkmm. GObject facilitates some of the object-oriented principles, like polymorphism and inheritance with the help of macros. The following hierarchical relation will clarify the idea further.
tkWindow is a child of or inherits GtkBin, which itself is a child of GtkContainer; therefore, an object of GtkWindow can call the function defined in GtkBin or GtkContainer. This is an example of object-oriented behavior enforced in C by GTK.
GUI with GTK.
Let us understand a few things from our first GTK code in C. First, we include the header file <gtk/gtk.h>. This includes all the files one needs to create a GUI, including the Glib library.