Buy Foundations of GTK+ Development now for only $31.49!
Cover Foundations of GTK+ Development

Build sophisticated graphical applications using one of the world's most powerful cross-platform toolkits, learning from practical examples, full applications, and in-depth reference appendices!
GTK+ Book Released on April 23rd

Foundations of GTK+ Development is the only book completely dedicated to GTK+ 2 development available, available at for only $32.99. It uses numerous examples to introduce almost every widget available in the library. In this book, you will learn how to:
  • Use basic widgets such as windows, buttons, and labels, in addition to many types of container widgets.
  • Create custom dialogs and use built-in dialogs for file, font, and color selection. The book also covers GtkAssistant, which was introduced in GTK+ 2.10.
  • Use many features provided by GLib including data structures, dynamic modules, memory management, the main loop, and pipes.
  • Develop with advanced widgets like GtkTreeView and GtkTextView.
  • Dynamically load menus with GtkUIManager, and user interfaces with Libglade.
  • Create your own custom widgets, objects, and interfaces.

It is important that you not only learn how to use the widgets in GTK+, but also become proficient at developing with them in full applications. In the last chapter of Foundations of GTK+ Development, five full applications are created that illustrate what you have learned throughout the rest of the book. Below you can view excerpts for two of these applications, and download their source code!

Calculator Application [Download Sources]

This calculator application was designed in Glade, so the user interface was already completed with absolutely no code. Next, glade_xml_signal_autoconnect() was used to connect all of the buttons to their signals. Since most of the widgets in this example are GtkButton widgets, the clicked and destroy signals were the only two needed.

The calculator allows the user to enter numbers with an optional decimal point, perform the four basic operations (add, subtract, multiply, and divide), negate numbers, and calculate square roots and exponents. In order to cut down on the number of callback functions needed, all of the numbers and the decimal place were connected to a single callback function called num_clicked(), and the four basic operations and the power operations were connected to another. This allows you to take advantage of the fact that these groups of operations need a lot of similar code to work.

When a number or the decimal point button is clicked, the character is appended to the end of the current value, although the length of the number is restricted to ten digits. When an operation button is clicked, the operation is performed, and the new value is stored. It also sets a flag called clear_flag that tells the calculator that a new number should be started when the user presses a number or decimal place.

The square root operation is not grouped with the other operations, because it is immediately performed on the displayed value in the sqrt_clicked() callback function. In order to do this, it simply takes the current value displayed in the GtkEntry widget, calculates the square root of the number, and stores the result back in the GtkEntry widget. Likewise, the negate operation also takes effect immediately. The exponential operation, on the other hand, asks for the power after it is clicked.

Ping Utility [Download Sources]

In Chapter 6, you learned how to use GIOChannel to communicate with applications through pipes. A ping utility application is displayed in [the figure on the right]; it allows the user to ping an address a specific number of times or continually until the application is stopped.

In this application, g_spawn_async_with_pipes() is used to fork an instance of the ping application with the specified address. The shell command received by this function was parsed with g_shell_parse_argv() so that it was in the correct format. The Ping button is also disabled, which prevents the user from running multiple instances of the child process.

After spawning the child process, the output pipe is used to create a new GIOChannel object that watches the pipe for read data. When data is ready to be read, it is parsed so that statistics for each ping can be displayed in a GtkTreeView widget. This will continue the specified number of times or until the user stops the child.

When a child process is running, a Stop button is enabled, which allows the user to kill the child process before it completes. This function simply calls the following instance of the kill() function, which forces the child process to close:

kill (pid, SIGINT);

When the process is killed, the pipe will be destroyed, which will cause the GIOChannel to be shut down in the watch function. This ensures that we will be able to reuse the same GIOChannel object for the next child process.

Copyright © 2007 Andrew Krause