Window perserverance under gtk+

I am not an expert on GTK+. I have never taken any courses or systematically studied the subject. All my knowledge is self-taught from Internet sources. I relate my experience in the hope it will be useful to others.

GTK+ is billed as a multi-platform toolkit for creating graphical user interfaces. It's libraries may be called from many languages, including C, C++, perl, and python. All the examples I will give access the GTK+ libraries from C.

GTK+ is widget-based, a widget being defined as a component of an interface that enables a user to perform a function or access a service.

Arguably, the simplest widget is a window, which can itself contain other widgets. A typical window will have several "entry" or "selection" widgets for entering information, and several buttons for selecting actions. When you request an action, the program will typically pop up a new window, where again you may enter information and select various options.

I will not go into great detail here about creating windows. Those desiring more information should check the excellent tutorials. The only point I wish to comment upon in this article, is that most of the tutorials I found on the Internet, terminate a window that is no longer needed with the command gtk_main_quit(). In my hands, this caused the window to lose focus, but not disappear. I think the logic behind doing things this way is that the window manager, which runs in backgound, should be responsible for destroying windows when they are no longer needed. While it is true that when my programs exit, the window manager kills all the windows, I found all those windows hanging around while the program is running distracting. This tutorial will explain how one can prevent the windows from persisting until the application is completely finished.

The tutorial consists of three programs, with increasing degree of sophistication. The first program asks the user to enter their favorite name and then pops up a window summarizing the choice. The "enter-choice window" loses focus but persists after the "summary window" is launched. In the second program, the user is asked to enter the name of his favorite person. Again the "enter-choice window" persists until the program exits. The crucial difference between the programs is that the second requires two text-entry widgets rather than one. The third program is like the second, but has a few additional modifications so that the "enter-choice window" is killed before the "summary window" is launched.

Source files for all three C programs, along with a makefile for compiling them may be downloaded. To uncompress the tutorial, issue the command "tar -xvzf tutorial.tar.gz", which creates the directory "tutorial", having all the necessary files. To compile the programs, simple enter the directory "tutorial" and type "make". To run program "prog" after compilation, simply type "./prog".

Emmes Technologies
Updated 1 May, 2013

valid html 4.01!