Libglade

Libglade is a library that performs a similar job to the C source output routines in the GLADE user interface builder. Whereas GLADE's output routines create C source code that must be compiled, libglade builds the interface from an XML file (GLADE's save format) at runtime. This can allow modifying the user interface without recompiling.

Libglade can also automatically connect signal handlers in the user interface. It does this by matching handler names specified in the glade file with symbols in the executable looked up with the gmodule library (this requires that applications be linked with the --export-dynamic flag).

A minimal libglade program in C looks like this:

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

void
some_handler(GtkWidget *widget)
{
    /* a handler referenced by the glade file.  Must not be static
     * so that it appears in the global symbol table. */
}

int
main(int argc, char **argv)
{
    GladeXML *xml;
    GtkWidget *widget;

    gtk_init(&argc, &argv);
    xml = glade_xml_new("filename.glade", NULL, NULL);

    /* get a widget (useful if you want to change something) */
    widget = glade_xml_get_widget(xml, "widgetname");

    /* connect signal handlers */
    glade_xml_signal_autoconnect(xml);

    gtk_main();

    return 0;
}

There are also Python bindings for libglade. This makes a nice rapid application development system. The Python equivalent of the above program is:

import gtk
import gtk.glade

def some_handler(widget):
    pass

xml = gtk.glade.XML('filename.glade')
widget = xml.get_widget('widgetname')
xml.autoconnect({
  'some_handler': some_handler
})
gtk.main()

Downloads

Libglade can be downloaded from ftp.gnome.org or its mirrors:


| Home | Software | Talks | Articles | Fractals | Photos | Diary |
Copyright © 2005 — James Henstridge <james@jamesh.id.au>