Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Gtk+/Gnome Application Development
Prev Home Next

User Communication: Dialogs

Dialogs are a continuous annoyance in plain GTK+; every time you want to tell the user anything, you have to create a window, create some buttons, create a label, pack the buttons and label into the window, set up callbacks, remember to capture "delete_event", and so on. It's a pain. Gnome saves you from this pain, with an easy-to-use general-purpose dialog widget, and several subclasses of it that implement common dialog types. Gnome also has easy functions for using modal dialogs.

The GnomeDialog Widget

Since dialogs in plain GTK+ are painstakingly constructed from scratch, there are at least as many ways to write a dialog as there are programmers. The programmer must decide where to place the dialog on the screen, how much padding to have, whether to put a separator above the buttons, what container to put the buttons in, what the keyboard shortcuts are, and so on. The premise of GnomeDialog is that the programmer should not have to care about these things; if they're variable at all, the user should configure them the way they want. From the programmer's perspective, dialogs "just work."

Creating a Dialog

A GnomeDialog is easy to create. Here's a summary of the basic steps, more detail follows:

  1. Read the section called Special Dialog Types and decide whether one of the special dialog subclasses is appropriate. If so, skip the below steps and create that subclass instead.

  2. Create the widget with gnome_dialog_new(). Pass this function the title of the dialog (displayed by the window manager) and the name of each button you'd like to have.

  3. Populate GNOME_DIALOG(dialog)->vbox with the contents of your dialog.

  4. Plan how your dialog will work. You can connect to the "close" or "clicked" signals, as appropriate. You can have the dialog hide or destroy itself when closed. You can also have the dialog automatically close when clicked, or handle this yourself. There are a number of ways the user can interact with a dialog, so it's important to be sure the combination of settings you choose will work no matter what the user does.

To create a dialog, use gnome_dialog_new(), shown in Figure 1. The argument list is a NULL-terminated list of buttons to insert in the dialog. For example, you might say:


GtkWidget* dialog;
dialog = gnome_dialog_new(_("My Dialog Title"),
                          _("OK"),
                          _("Cancel"),
                          NULL);

      

This creates a dialog titled "My Dialog Title" with an OK and a Cancel button; the strings are marked for translation with the _() macro. The OK button will be the leftmost button in the dialog.

       #include <libgnomeui/gnome-dialog.h>
      

GtkWidget* gnome_dialog_new(const gchar* title, ...);

Figure 1. GnomeDialog Constructor

The GnomeDialog API numbers the buttons you add starting with 0; you use these numbers to refer to the buttons later, since you don't have a pointer to the automatically-created button widgets. In this case, the OK button is button 0, and the Cancel button is button 1. (Note that this is standard Gnome practice---OK or Yes goes first, then Cancel or No. In fact libgnomeui/gnome-uidefs.h contains the macros GNOME_YES, GNOME_OK, GNOME_NO, and GNOME_CANCEL which represent the dialog button numbers for these items in a two-button dialog.)

The above example, which specifies buttons called "OK" and "Cancel," is not quite correct for production code. Gnome provides a set of "stock buttons" for common button names. These ensure everyone uses "OK" instead of "Ok" or "OK!"; they allow translators to translate common strings only once; and they often insert icons in the buttons, making them more attractive and recognizable to users. You should always use stock buttons if possible.

You can use stock buttons in gnome_dialog_new(). Simply substitute the stock button macros for the button names:


dialog = gnome_dialog_new(_("My Dialog Title"),
                          GNOME_STOCK_BUTTON_OK,
                          GNOME_STOCK_BUTTON_CANCEL,
                          NULL);

      

Gnome includes many stock buttons, stock menu items, and stock pixmaps---it's a good idea to check these out so you don't reinvent the wheel. There's a complete list in libgnomeui/gnome-stock.h.

Gtk+/Gnome Application Development
Prev Home Next

 
 
  Published under free license. Design by Interspire