1
<!-- ##### SECTION Title ##### -->
4
<!-- ##### SECTION Short_Description ##### -->
5
Build an interface from an XML UI definition
7
<!-- ##### SECTION Long_Description ##### -->
9
A GtkBuilder is an auxiliary object that reads textual descriptions
10
of a user interface and instantiates the described objects. To pass a
11
description to a GtkBuilder, call gtk_builder_add_from_file() or
12
gtk_builder_add_from_string(). These functions can be called multiple
13
times; the builder merges the content of all descriptions.
16
A GtkBuilder holds a reference to all objects that it has constructed
17
and drops these references when it is finalized. This finalization can
18
cause the destruction of non-widget objects or widgets which are not
19
contained in a toplevel window. For toplevel windows constructed by a
20
builder, it is the responsibility of the user to call gtk_widget_destroy()
21
to get rid of them and all the widgets they contain.
24
The functions gtk_builder_get_object() and gtk_builder_get_objects()
25
can be used to access the widgets in the interface by the names assigned
26
to them inside the UI description. Toplevel windows returned by these
27
functions will stay around until the user explicitly destroys them
28
with gtk_widget_destroy(). Other widgets will either be part of a
29
larger hierarchy constructed by the builder (in which case you should
30
not have to worry about their lifecycle), or without a parent, in which
31
case they have to be added to some container to make use of them.
32
Non-widget objects need to be reffed with g_object_ref() to keep them
33
beyond the lifespan of the builder.
36
The function gtk_builder_connect_signals() and variants thereof can be
37
used to connect handlers to the named signals in the description.
40
<refsect2 id="BUILDER-UI"><title>GtkBuilder UI Definitions</title>
42
GtkBuilder parses textual descriptions of user interfaces which
43
are specified in an XML format which can be roughly described
44
by the DTD below. We refer to these descriptions as
45
<firstterm>GtkBuilder UI definitions</firstterm> or just
46
<firstterm>UI definitions</firstterm> if the context is clear.
47
Do not confuse GtkBuilder UI Definitions with
48
<link linkend="XML-UI">GtkUIManager UI Definitions</link>,
49
which are more limited in scope.
52
<programlisting><![CDATA[
53
<!ELEMENT interface (requires|object)* >
54
<!ELEMENT object (property|signal|child|ANY)* >
55
<!ELEMENT property PCDATA >
56
<!ELEMENT signal EMPTY >
57
<!ELEMENT requires EMPTY >
58
<!ELEMENT child (object|ANY*) >
60
<!ATTLIST interface domain #IMPLIED >
61
<!ATTLIST object id #REQUIRED
64
constructor #IMPLIED >
65
<!ATTLIST requires lib #REQUIRED
67
<!ATTLIST property name #REQUIRED
71
<!ATTLIST signal name #REQUIRED
76
last_modification_time #IMPLIED >
77
<!ATTLIST child type #IMPLIED
78
internal-child #IMPLIED >
82
The toplevel element is <interface>.
83
It optionally takes a "domain" attribute, which will make
84
the builder look for translated strings using dgettext() in the
85
domain specified. This can also be done by calling
86
gtk_builder_set_translation_domain() on the builder.
87
Objects are described by <object> elements, which can
88
contain <property> elements to set properties, <signal>
89
elements which connect signals to handlers, and <child>
90
elements, which describe child objects (most often widgets
91
inside a container, but also e.g. actions in an action group,
92
or columns in a tree model). A <child> element contains
93
an <object> element which describes the child object.
94
The target toolkit version(s) are described by <requires>
95
elements, the "lib" attribute specifies the widget library in
96
question (currently the only supported value is "gtk+") and the "version"
97
attribute specifies the target version in the form "<major>.<minor>".
98
The builder will error out if the version requirements are not met.
101
Typically, the specific kind of object represented by an
102
<object> element is specified by the "class" attribute.
103
If the type has not been loaded yet, GTK+ tries to find the
104
<function>_get_type()</function> from the class name by applying
105
heuristics. This works in most cases, but if necessary, it is
106
possible to specify the name of the <function>_get_type()</function>
107
explictly with the "type-func" attribute. As a special case,
108
GtkBuilder allows to use an object that has been constructed
109
by a #GtkUIManager in another part of the UI definition by
110
specifying the id of the #GtkUIManager in the "constructor"
111
attribute and the name of the object in the "id" attribute.
114
Objects must be given a name with the "id" attribute, which
115
allows the application to retrieve them from the builder with
116
gtk_builder_get_object(). An id is also necessary to use the
117
object as property value in other parts of the UI definition.
119
<note><para>Prior to 2.20, GtkBuilder was setting the "name"
120
property of constructed widgets to the "id" attribute. In GTK+
121
2.20 or newer, you have to use gtk_buildable_get_name() instead
122
of gtk_widget_get_name() to obtain the "id", or set the "name"
123
property in your UI definition.
126
Setting properties of objects is pretty straightforward with
127
the <property> element: the "name" attribute specifies
128
the name of the property, and the content of the element
129
specifies the value. If the "translatable" attribute is
130
set to a true value, GTK+ uses gettext() (or dgettext() if
131
the builder has a translation domain set) to find a translation
132
for the value. This happens before the value is parsed, so
133
it can be used for properties of any type, but it is probably
134
most useful for string properties. It is also possible to
135
specify a context to disambiguate short strings, and comments
136
which may help the translators.
139
GtkBuilder can parse textual representations for the most
140
common property types: characters, strings, integers, floating-point
141
numbers, booleans (strings like "TRUE", "t", "yes", "y", "1" are
142
interpreted as %TRUE, strings like "FALSE, "f", "no", "n", "0" are
143
interpreted as %FALSE), enumerations (can be specified by their
144
name, nick or integer value), flags (can be specified by their name,
145
nick, integer value, optionally combined with "|", e.g.
146
"GTK_VISIBLE|GTK_REALIZED") and colors (in a format understood by
147
gdk_color_parse()). Objects can be referred to by their name.
148
Pixbufs can be specified as a filename of an image file to load.
149
In general, GtkBuilder allows forward references to objects —
150
an object doesn't have to constructed before it can be referred to.
151
The exception to this rule is that an object has to be constructed
152
before it can be used as the value of a construct-only property.
155
Signal handlers are set up with the <signal> element.
156
The "name" attribute specifies the name of the signal, and the
157
"handler" attribute specifies the function to connect to the signal.
158
By default, GTK+ tries to find the handler using g_module_symbol(),
159
but this can be changed by passing a custom #GtkBuilderConnectFunc
160
to gtk_builder_connect_signals_full(). The remaining attributes,
161
"after", "swapped" and "object", have the same meaning as the
162
corresponding parameters of the g_signal_connect_object() or
163
g_signal_connect_data() functions. A "last_modification_time" attribute
164
is also allowed, but it does not have a meaning to the builder.
167
Sometimes it is necessary to refer to widgets which have implicitly
168
been constructed by GTK+ as part of a composite widget, to set
169
properties on them or to add further children (e.g. the @vbox
170
of a #GtkDialog). This can be achieved by setting the "internal-child"
171
propery of the <child> element to a true value. Note that
172
GtkBuilder still requires an <object> element for the internal
173
child, even if it has already been constructed.
176
A number of widgets have different places where a child can be
177
added (e.g. tabs vs. page content in notebooks). This can be reflected
178
in a UI definition by specifying the "type" attribute on a <child>
179
The possible values for the "type" attribute are described in
180
the sections describing the widget-specific portions of UI definitions.
183
<title>A GtkBuilder UI Definition</title>
184
<programlisting><![CDATA[
186
<object class="GtkDialog" id="dialog1">
187
<child internal-child="vbox">
188
<object class="GtkVBox" id="vbox1">
189
<property name="border-width">10</property>
190
<child internal-child="action_area">
191
<object class="GtkHButtonBox" id="hbuttonbox1">
192
<property name="border-width">20</property>
194
<object class="GtkButton" id="ok_button">
195
<property name="label">gtk-ok</property>
196
<property name="use-stock">TRUE</property>
197
<signal name="clicked" handler="ok_button_clicked"/>
209
Beyond this general structure, several object classes define
210
their own XML DTD fragments for filling in the ANY placeholders
211
in the DTD above. Note that a custom element in a <child>
212
element gets parsed by the custom tag handler of the parent
213
object, while a custom element in an <object> element
214
gets parsed by the custom tag handler of the object.
217
These XML fragments are explained in the documentation of the
218
respective objects, see
219
<link linkend="GtkWidget-BUILDER-UI">GtkWidget</link>,
220
<link linkend="GtkLabel-BUILDER-UI">GtkLabel</link>,
221
<link linkend="GtkWindow-BUILDER-UI">GtkWindow</link>,
222
<link linkend="GtkContainer-BUILDER-UI">GtkContainer</link>,
223
<link linkend="GtkDialog-BUILDER-UI">GtkDialog</link>,
224
<link linkend="GtkCellLayout-BUILDER-UI">GtkCellLayout</link>,
225
<link linkend="GtkColorSelectionDialog-BUILDER-UI">GtkColorSelectionDialog</link>,
226
<link linkend="GtkFontSelectionDialog-BUILDER-UI">GtkFontSelectionDialog</link>,
227
<link linkend="GtkComboBoxEntry-BUILDER-UI">GtkComboBoxEntry</link>,
228
<link linkend="GtkExpander-BUILDER-UI">GtkExpander</link>,
229
<link linkend="GtkFrame-BUILDER-UI">GtkFrame</link>,
230
<link linkend="GtkListStore-BUILDER-UI">GtkListStore</link>,
231
<link linkend="GtkTreeStore-BUILDER-UI">GtkTreeStore</link>,
232
<link linkend="GtkNotebook-BUILDER-UI">GtkNotebook</link>,
233
<link linkend="GtkSizeGroup-BUILDER-UI">GtkSizeGroup</link>,
234
<link linkend="GtkTreeView-BUILDER-UI">GtkTreeView</link>,
235
<link linkend="GtkUIManager-BUILDER-UI">GtkUIManager</link>,
236
<link linkend="GtkActionGroup-BUILDER-UI">GtkActionGroup</link>.
237
<link linkend="GtkMenuItem-BUILDER-UI">GtkMenuItem</link>,
238
<link linkend="GtkAssistant-BUILDER-UI">GtkAssistant</link>,
239
<link linkend="GtkScale-BUILDER-UI">GtkScale</link>.
243
<!-- ##### SECTION See_Also ##### -->
246
<!-- ##### SECTION Stability_Level ##### -->
249
<!-- ##### SECTION Image ##### -->
252
<!-- ##### STRUCT GtkBuilder ##### -->
258
<!-- ##### ARG GtkBuilder:translation-domain ##### -->
263
<!-- ##### USER_FUNCTION GtkBuilderConnectFunc ##### -->
277
<!-- ##### ENUM GtkBuilderError ##### -->
279
Error codes that identify various errors that can occur while
283
@GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION: A type-func attribute didn't name
284
a function that returns a #GType.
285
@GTK_BUILDER_ERROR_UNHANDLED_TAG: The input contained a tag that #GtkBuilder
287
@GTK_BUILDER_ERROR_MISSING_ATTRIBUTE: An attribute that is required by
288
#GtkBuilder was missing.
289
@GTK_BUILDER_ERROR_INVALID_ATTRIBUTE: #GtkBuilder found an attribute that
290
it doesn't understand.
291
@GTK_BUILDER_ERROR_INVALID_TAG: #GtkBuilder found a tag that
292
it doesn't understand.
293
@GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE: A required property value was
295
@GTK_BUILDER_ERROR_INVALID_VALUE: #GtkBuilder couldn't parse
296
some attribute value.
297
@GTK_BUILDER_ERROR_VERSION_MISMATCH: The input file requires a newer version
299
@GTK_BUILDER_ERROR_DUPLICATE_ID: An object id occurred twice.
301
<!-- ##### FUNCTION gtk_builder_new ##### -->
310
<!-- ##### FUNCTION gtk_builder_add_from_file ##### -->
321
<!-- ##### FUNCTION gtk_builder_add_from_string ##### -->
333
<!-- ##### FUNCTION gtk_builder_add_objects_from_file ##### -->
345
<!-- ##### FUNCTION gtk_builder_add_objects_from_string ##### -->
358
<!-- ##### FUNCTION gtk_builder_get_object ##### -->
368
<!-- ##### FUNCTION gtk_builder_get_objects ##### -->
377
<!-- ##### FUNCTION gtk_builder_connect_signals ##### -->
386
<!-- ##### FUNCTION gtk_builder_connect_signals_full ##### -->
396
<!-- ##### FUNCTION gtk_builder_set_translation_domain ##### -->
405
<!-- ##### FUNCTION gtk_builder_get_translation_domain ##### -->
414
<!-- ##### FUNCTION gtk_builder_get_type_from_name ##### -->
424
<!-- ##### FUNCTION gtk_builder_value_from_string ##### -->
437
<!-- ##### FUNCTION gtk_builder_value_from_string_type ##### -->
450
<!-- ##### MACRO GTK_BUILDER_WARN_INVALID_CHILD_TYPE ##### -->
452
This macro should be used to emit a warning about and unexpected
453
@type value in a #GtkBuildable add_child implementation.
456
@object: the #GtkBuildable on which the warning ocurred
457
@type: the unexpected type value
460
<!-- ##### MACRO GTK_BUILDER_ERROR ##### -->
462
The #GError quark for #GtkBuilder errors