Skip to main content

Getting along with libglade...

(hassle free GUI) This weekend, we were experimenting a bit with libglade & SQLite. So, here's this write-up on libglade.... read on... *** All the time we prepared GUI for our semester projects, we had that tinge of dissatisfaction that we hadn't had a way to keep the GUI stuff outside our code and that way keep the code clean. The neccessity was felt even more when there was a requirement to redesign the GUI. Once we started hacking Evolution, we knew the best way out. The Solution was to use libglade. Much has been talked about libglade, but very less written. Hand coding GUI can be fun when it is a semester mini project you're doing and when there's lot of time in hand. Usually, there also isn't a need to revamp the GUI as nobody even looks back. But not so when time is a constraint and when the GUI needs frequent upgradation/change. What libglade does for you: At runtime, libglade reads an XML description of the interface and (dynamically) loads the widgets. The GUI can be changed any time afterwards without having to touch the source code. Using libglade results in two very useful things for you: 1) Keeps aside the source from GUI. 2) Lets you manipulate GUI with ease. All you need is to get the .glade file from the project folder generated by Glade. You might've guessed by now, libglade makes use of the same XML format that Glade generates. Starting off... You need to know how to get around with glade, which shouldn't be tough at all. Use the pallette, drag and drop all that you want, name them, and assign them the signal handlers. Set all the properties required and get an interface ready. Now, save the project. Make sure you have proper settings in Project Options (File->Project Options) shot1.png Here's an Example... shot2.png I've made use of a Clist in a scrolled window, which is within a GnomeDialog. One thing you've to make sure is to have the right container on which you plan to put the other widgets (I've used Vertical Box) Now, you're done with glade. Next Steps... Glade would've created an XML file for you in the Projects folder (or wherever you saved the project) Copy the file to a different folder. Get your main.c to the same folder. Now, all that is left to do, is to make a few changes to your main.c and the Makefile. Let us see what changes are required to the main function. (you can pick the main.c created by glade if you feel lazy to write one again. All you need to do is reflect the following changes) The list of changes is as below: /* handler for the xml file */ GladeXML *xml; /* needed to initialise glade for gnome */ glade_gnome_init(); /* to load the interface */ xml = glade_xml_new("simpuwonder.glade", select); /* if not loaded, intimate.. */ if(!xml) { g_warning("We could not load the interface!"); return 1; } /* connect the signals in the interface */ glade_xml_signal_autoconnect(xml); /* To unreference the objects and to free the memory */ gtk_object_unref(GTK_OBJECT(xml)); and yeah, don't forget to include the header file 'glade/glade.h'. Now, a widget can be called by using something like: select = glade_xml_get_widget(xml, "select"); (The names are the same as you specify in glade) Thats it! You can either compile directly by providing the proper flags, or just write a new Makefile as the one below: CC = gcc CFLAGS=-g -Wall `gnome-config --cflags gnome gnomeui libglade` LDFLAGS= `gnome-config --libs gnome gnomeui libglade` all: simpuwonder clean: rm -f *.o core simpuwonder You're done! You should be able to include few more dialogs, callbacks to suit your needs. Add your code in callbacks and connect the widgets to your requirements. The Next time you want to change the GUI, all you need to do is to open Glade, and just edit the .glade file!