Whole document tree
    

Whole document tree

Embedding Libglade Interfaces

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 the menubar or just a notebook for a dialog or something. Libglade allows this as well.

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" isn't a bad 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");
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 file that contains many dialogs.

One thing to note -- if you don't want a widget to be displayed as soon as it is built with glade_xml_new, you should turn off the visible flag on that widget 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).