Libglade Programming Notes

Table of Contents
Libglade Programming Basics
Adding GNOME Support
Internationalisation with Libglade
Extending Libglade
Embedding Libglade Interfaces

Libglade is an alternative to using Glade's code generation. Instead of generating code from the XML interface description, libglade loads and parses the description at runtime. It also provides functions that can be used to connect signal handlers to parts of the interface.

In this way, it allows you to separate your program code from the interface code. In fact, if you use the glade_xml_signal_autoconnect() function, the GUI code could be as simple as the test-libglade.c example that comes with libglade. Of course, you would also add your own signal handlers to the code. Note that the signals are connected the same way as if you had hand coded the interface. There is no extra overhead to interfaces generated by libglade (after the initial generating of course, and this is not much of an overhead) when compared to a hand crafted interface.


Libglade Programming Basics

Your basic libglade program will look something like this:

#include <gtk/gtk.h>
#include <glade/glade.h>

void some_signal_handler_func(GtkWidget *widget, gpointer user_data) {
  /* do something useful here */
}

int main(int argc, char *argv[]) {
    GladeXML *xml;

    gtk_init(&argc, &argv);
    glade_init();

    /* load the interface */
    xml = glade_xml_new("filename-for-interface", NULL);
    /* connect the signals in the interface */
    glade_xml_signal_autoconnect(xml);
    /* start the event loop */
    gtk_main();
    return 0;
}

This will create the interface from the file filename-for-interface, then connect all the signals in the interface. The automatic signal connection is done by looking up function names in the symbol table using gmodule. This means that the interface file can use standard GTK functions such as gtk_widget_show, or gtk_main_quit, or others in the interface and not have to write any code to connect the signals.

The some_signal_handler_func function is not referenced anywhere in the program explicitely, but if any signals are defined in the interface description that use "some_signal_handler_func" as the handler name, then this function will automatically be connected. Note that the function can not be static, since we require it to apear in the symbol table. Here is an example of the XML that would cause some_signal_handler_func to be connected:

<widget>
  <class>GtkWindow</class>
  <name>MainWindow</name>
  <signal>
    <name>destroy</name>
    <handler>some_signal_handler_func</handler>
  </signal>
  ...

To compile the program, we would use the following:

cc -o testprogram testprogram.c `libglade-config --cflags --libs`

The libglade-config script will give the correct parameters used to compile the program. If you are using automake or autoconf, you may want to investigate the AM_PATH_LIBGLADE macro that is installed as $(datadir)/aclocal/libglade.m4.