Chapter 19. Glade and libglademm

## Chapter 19. Glade and libglademm

Although you can use C++ code to instantiate and arrange widgets, this can soon become tedious and repetitive. And it requires a recompilation to show changes. The `Glade` application allows you to layout widgets on screen and then save an XML description of the arrangement. Your application can then use the `libglademm` API to load that XML file at runtime and obtain a pointer to specifically named widget instances.

This has the following advantages:

1. Less C++ code is required.
2. UI changes can be seen more quickly, so UIs are able to improve.
3. Designers without programming skills can create and edit UIs.

You still need C++ code to deal with User Interface changes triggered by user actions, but using `libglademm` for the basic widget layout allows you to focus on implementing that functionality.

## Loading the .glade file

`Gnome::Glade::Xml` must be used via a `Glib::RefPtr`. Like all such classes, you need to use create() method to instantiate it.

    Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("basic.glade");

This will instantiate the windows defined in the .glade file, though they will not be shown immediately unless you have specified that via the Properties window in Glade. The widgets are ownedg by the `Gnome::Glade::Xml` instance, and will be deleted automatically when it is deleted, when the last copy of the smartpointer goes out of scope.

To instantiate just one window, or just one of the child widgets, you can specify the name of a widget as the second parameter. For instance,

    Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("basic.glade", "treeview_products");

Shared resources |  | Accessing widgets