Stub

(2012-03-07)

Using dynamic menu and toolbar actions

There are several ways to design and build the graphical interface for your GTK program. Glade 3 is by far the best if your interface has any degree of complication at all. However, the menu editor in Glade does not take advantage of the advanced features for menus and toolbars added in GTK 2.4, such as action groups and accelerator maps. These features are explained in Foundations, but not how to use them in combination with Glade. A serious application, big enough to require Glade for its design, will often want to do things like:

  • Change the "sensitive" status of some actions like Cut and Copy, depending on whether there is text selected in the application;

  • Programmatically change the contents of menus;

  • Have customizable toolbars and keyboard shortcuts.

The latter facility, for example, is easily enabled in Mac OS X applications designed with Apple's Interface Builder for Cocoa. The infrastructure for it is there in GTK, just not as easily accessible. Glade's menu and toolbar editor doesn't build its menus using GtkActions, even though GtkBuilder is capable of constructing them from XML files. This functionality will appear in the future, when Glade is able to write GtkBuilder XML files.

Until then, some trickery is required, and some extra dialogues must be constructed. We will build an example application with ridiculous do-nothing actions inspired by the Beatles' Ballad of Rocky Raccoon, about a young man who fights a duel to win back his lady love. Our program will take special note of the fact that

Her name was Magill / and she called herself Lil / but everyone knew her as Nancy;

GtkActions can also be known by more than one name.

Building the interface

First we will build the interface using Glade 3, and call it rocky.ui. Here is a screenshot of the various windows we built in Glade.

(Screenshots of the Rocky Raccoon user interface)

There is a main application window and an about dialog. For details, see the file itself, included in the code distribution, or load the file in Glade.

You will notice that the main application window does not contain a menu bar or a toolbar. That is because we will automatically generate these from an XML file with the menu structure, which we will write in the next section. It is also important to notice that the text view (representing the actual application widget, whatever it may be) and the status bar are end-packed into their containing GtkVBox; that is, they appear below any start-packed widgets. This is convenient because we can then start-pack the menu bar and toolbar, and not have to worry about where to insert them.