<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 23. Glade and Gtk::Builder</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Programming with gtkmm"> <link rel="up" href="index.html" title="Programming with gtkmm"> <link rel="prev" href="sec-memory-shared-resources.html" title="Shared resources"> <link rel="next" href="sec-builder-accessing-widgets.html" title="Accessing widgets"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Chapter 23. Glade and Gtk::Builder</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-memory-shared-resources.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center"> </th> <td width="20%" align="right"> <a accesskey="n" href="sec-builder-accessing-widgets.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="chapter" title="Chapter 23. Glade and Gtk::Builder"> <div class="titlepage"><div><div><h2 class="title"> <a name="chapter-builder"></a>Chapter 23. Glade and Gtk::Builder</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul> <li><span class="sect1"><a href="chapter-builder.html#sec-builder-loading-glade-file">Loading the .glade file</a></span></li> <li><span class="sect1"><a href="sec-builder-accessing-widgets.html">Accessing widgets</a></span></li> <li><span class="sect1"><a href="sec-builder-using-derived-widgets.html">Using derived widgets</a></span></li> </ul> </div> <p> 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 <span class="application">Glade</span> application allows you to layout widgets on screen and then save an XML description of the arrangement. Your application can then use the <span class="application">Gtk::Builder</span> API to load that XML file at runtime and obtain a pointer to specifically named widget instances. </p> <p> This has the following advantages: </p> <div class="orderedlist"><ol class="orderedlist" type="1"> <li class="listitem">Less C++ code is required.</li> <li class="listitem">UI changes can be seen more quickly, so UIs are able to improve.</li> <li class="listitem">Designers without programming skills can create and edit UIs.</li> </ol></div> <p> </p> <p> You still need C++ code to deal with User Interface changes triggered by user actions, but using <span class="application">Gtk::Builder</span> for the widget layout allows you to focus on implementing that functionality. </p> <div class="sect1" title="Loading the .glade file"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-builder-loading-glade-file"></a>Loading the .glade file</h2></div></div></div> <p> <code class="classname">Gtk::Builder</code> must be used via a <code class="classname">Glib::RefPtr</code>. Like all such classes, you need to use a <code class="methodname">create()</code> method to instantiate it. For instance, </p> <pre class="programlisting"> Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create("basic.glade"); </pre> <p> This will instantiate the windows defined in the .glade file, though they will not be shown immediately unless you have specified that via the <span class="guilabel">Properties</span> window in <span class="application">Glade</span>. </p> <p>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, </p> <pre class="programlisting"> Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create("basic.glade", "treeview_products"); </pre> <p> </p> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-memory-shared-resources.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"> </td> <td width="40%" align="right"> <a accesskey="n" href="sec-builder-accessing-widgets.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Shared resources </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Accessing widgets</td> </tr> </table> </div> </body> </html>