Embedding Libglade Interfaces

Sometimes you will only want to use libglade for a small part of your program. If it is just for some dialogs, this is easy -- you just generate the dialogs from the interface files when needed (note that libglade caches the XML parse tree between calls to glade_xml_new, so you will not suffer the performance hit of parsing a particular XML file more than once).

On the other hand, you may want to use libglade to generate just part of the UI, such as the menubar or a notebook or something. Libglade allows you to build only part of the interface if you want to. The second argument to glade_xml_new specifies the name of the base widget to build the interface from. This way we can limit the widgets that are constructed by libglade.

For the menubar example, we would create a dummy window in Glade, and insert a menubar widget into the window. We would then name the menubar in glade ("menubar" would be a good choice for the widget name ), and customise it as much as we want. Now in the program, we can use the following code:

GladeXML *xml;
GtkWidget *menubar;

xml = glade_xml_new("some-interface-file", "menubar", NULL);

glade_xml_signal_autoconnect(xml);

menubar = glade_xml_get_widget(xml, "menubar");
/* do whatever we want to with the menubar */

From here, we can do what ever we want with the menubar widget. The dummy window we created in Glade is never created, so does not affect the program. You can also use similar code to only build a single dialog from a glade file that contains many dialogs.

One thing to note -- if you don't want a widget to be displayed as soon as it is constructed with glade_xml_new, you should set the visible property on that widget to "no" in Glade. This is the correct solution to the problem (putting a hack into libglade so that it never shows the toplevel windows is not The Right Thing).