Previous Next

The Backend: Interacting with the GUI

Libraries and APIS

There are several different libraries and facilities which the programmer should consider when coding Gtk+ or Gnome applications. Below is a listing, with a short introduction for each.

Referencing the Widgets

Interfacing the backend with the GUI is simply a matter of presenting or receiving data from the widgets. The Gtk+ and Gnome library references above provide functions for each widget which allow you to set or grab data. But you'll still need to reference the widget somehow in your application.

One of the easiest ways is to use the lookup_widget() function which Glade provides in support.c. This function takes a widget and the name of the widget desired as parameters, and returns the widget desired (if it exists in the tree of the widget parameter). For example, in the Gtemp interface we created a widget called fahr_entry which was in the main application window tree. If we were dealing with fahr_entry let's say in an activate callback, but wanted to reference cel_entry to put data into that entry field, we'd use GtkWidget *cel_entry = lookup_widget(GTK_WIDGET(fahr_entry), "cel_entry"). This might seem confusing, but basically it's a pointer declared to GtkWidget type (cel_entry) which is named the same thing as the widget itself.

Another way to reference a widget is with gtk_widget_get_toplevel(). The parameter here is the child widget. For example, I could find the topmost container widget for cel_entry (which happens to be the gtemp application window) if I did this... GtkWidget *gtemp_app = gtk_widget_get_toplevel(GTK_WIDGET(cel_entry));.

You can also keep track of widgets by using static or global variables. Oftentimes this is the easiest way to reference the main window from subsidiary windows (ie popups and dialogs). However, too many globals is looked upon as poor coding by some, and limiting their use is encouraged. If we were to use gtemp_app as a global we'd realize it in main.c, then declare it as an extern variable in callbacks.c or any other place we need to reference it. As it stands, Gtemp doesn't need to do this.

Glib provides a way to set data associated with a key string on any g_object, and subsequently on any gtk+ object. Note that in Gtk+ versions prior to 2.0, there was a gtk_object class, this has been subsumed under the g_object class now. Anyway, you can use a string to reference data 'set' on an object. For instance, this snippet of code will set up an reference to cel_entry and attach it to fahr_entry.
static gchar *my_key_string = "my_key_string";

cel_entry = lookup_widget(GTK_WIDGET(fahr_entry), "cel_entry");
g_object_set_data(G_OBJECT(fahr_entry), my_key_string, cel_entry);


then later on in the program we can grab cel_entry straight from fahr_entry:
GtkWidget *cel_entry;

cel_entry = g_object_get_data(G_OBJECT(fahr_entry), my_key_string);


Of course, this isn't too useful when referencing widgets that are in the same tree (ie they are in the same window together). The lookup_widget function works much better for this. However, this technique is good to use when there is a need to reference one window from another, and you don't wish to use globals. You could take data in a popup window and feed it into the main window this way, for example, without having to make the main window global.

Loading or 'Calling Up' New Windows or Menus

Glade writes create functions for each window or popup menu you design and puts them in interface.c. As usual, there is no reason to edit interface.c to use these functions, which are named create_windowname(), have no parameters, and return a pointer to the actual window or popup_menu widget. For example, here's code to create the about window for Gtemp:
GtkWidget *about;

about = create_gtemp_about();


And yes, it's that easy.

Destroying Windows When they are No Longer Needed

If you have created a popup or dialog window that you no longer need, you can destroy it with gtk_widget_destroy(). The parameter is the GtkWidget you wish to destroy. Generally this is called from an 'ok' or 'cancel' button on the window to be destroyed. For example, to destroy a popup window named 'which_file' from the 'ok' button in that window just call the function above in the callback for the ok button:
gtk_widget_destroy(gtk_widget_get_toplevel(GTK_WIDGET(ok_button)));

Notice we are not calling the function on the button itself, this would simply destroy the button. Instead we call it on the button's toplevel parent, which is the window itself.

What Next?

Now that you know where the references to the libraries are located, and how to reference widgets in your application, it's time to turn to coding. The real guts of the application usually consists of what you do with the data after you grab it, or how you manipulate it before you present it. Glade does nothing to deal with this aspect, and the development of the application becomes a coding problem, not an interface problem. To see in more detail how a simple interface can be tied into a very simple backend, we'll return to the Gtemp application.

Previous Next