~ubuntu-branches/ubuntu/precise/gtk+2.0/precise-updates

« back to all changes in this revision

Viewing changes to docs/reference/gtk/tmpl/gtkbuilder.sgml

  • Committer: Package Import Robot
  • Author(s): Ken VanDine
  • Date: 2011-12-01 11:40:06 UTC
  • mfrom: (1.14.11)
  • Revision ID: package-import@ubuntu.com-20111201114006-nrmf6qu3pg512veo
Tags: 2.24.8-0ubuntu1
* New upstream release 
  - gtkfilechooser should be more robust to malformed URIs
    in .gtk-bookmarks (LP: #189494)
* debian/patches/010_make_bg_changes_queue_repaint.patch
  - dropped it introduces performance regressions in some gtk2 
    apps (LP: #889019)
* 101_filechooser.patch, 000git_file_chooser.patch: dropped, upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!-- ##### SECTION Title ##### -->
2
 
GtkBuilder
3
 
 
4
 
<!-- ##### SECTION Short_Description ##### -->
5
 
Build an interface from an XML UI definition
6
 
 
7
 
<!-- ##### SECTION Long_Description ##### -->
8
 
<para>
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.
14
 
</para>
15
 
<para>
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.
22
 
</para>
23
 
<para>
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.
34
 
</para>
35
 
<para>
36
 
The function gtk_builder_connect_signals() and variants thereof can be
37
 
used to connect handlers to the named signals in the description.
38
 
</para>
39
 
 
40
 
<refsect2 id="BUILDER-UI"><title>GtkBuilder UI Definitions</title>
41
 
<para>
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.
50
 
</para>
51
 
<para>
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*) >
59
 
 
60
 
<!ATTLIST interface  domain                 #IMPLIED >
61
 
<!ATTLIST object     id                     #REQUIRED
62
 
                     class                  #REQUIRED
63
 
                     type-func              #IMPLIED
64
 
                     constructor            #IMPLIED >
65
 
<!ATTLIST requires   lib                    #REQUIRED
66
 
                     version                #REQUIRED >
67
 
<!ATTLIST property   name                   #REQUIRED
68
 
                     translatable           #IMPLIED
69
 
                     comments               #IMPLIED
70
 
                     context                #IMPLIED >
71
 
<!ATTLIST signal     name                   #REQUIRED
72
 
                     handler                #REQUIRED
73
 
                     after                  #IMPLIED
74
 
                     swapped                #IMPLIED
75
 
                     object                 #IMPLIED
76
 
                     last_modification_time #IMPLIED >
77
 
<!ATTLIST child      type                   #IMPLIED
78
 
                     internal-child         #IMPLIED >
79
 
]]></programlisting>
80
 
</para>
81
 
<para>
82
 
The toplevel element is &lt;interface&gt;.
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 &lt;object&gt; elements, which can
88
 
contain &lt;property&gt; elements to set properties, &lt;signal&gt;
89
 
elements which connect signals to handlers, and &lt;child&gt;
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 &lt;child&gt; element contains
93
 
an &lt;object&gt; element which describes the child object.
94
 
The target toolkit version(s) are described by &lt;requires&gt;
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 "&lt;major&gt;.&lt;minor&gt;".
98
 
The builder will error out if the version requirements are not met.
99
 
</para>
100
 
<para>
101
 
Typically, the specific kind of object represented by an
102
 
&lt;object&gt; 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.
112
 
</para>
113
 
<para>
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.
118
 
</para>
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.
124
 
</para></note>
125
 
<para>
126
 
Setting properties of objects is pretty straightforward with
127
 
the &lt;property&gt; 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.
137
 
</para>
138
 
<para>
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 &mdash;
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.
153
 
</para>
154
 
<para>
155
 
Signal handlers are set up with the &lt;signal&gt; 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.
165
 
</para>
166
 
<para>
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 &lt;child&gt; element to a true value. Note that
172
 
GtkBuilder still requires an &lt;object&gt; element for the internal
173
 
child, even if it has already been constructed.
174
 
</para>
175
 
<para>
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 &lt;child&gt;
179
 
The possible values for the "type" attribute are described in
180
 
the sections describing the widget-specific portions of UI definitions.
181
 
</para>
182
 
<example>
183
 
<title>A GtkBuilder UI Definition</title>
184
 
<programlisting><![CDATA[
185
 
<interface>
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>
193
 
            <child>
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"/>
198
 
              </object>
199
 
            </child>
200
 
          </object>
201
 
        </child>
202
 
      </object>
203
 
    </child>
204
 
  </object>
205
 
</interface>
206
 
]]></programlisting>
207
 
</example>
208
 
<para>
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 &lt;child&gt;
212
 
element gets parsed by the custom tag handler of the parent
213
 
object, while a custom element in an &lt;object&gt; element
214
 
gets parsed by the custom tag handler of the object.
215
 
</para>
216
 
<para>
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>.
240
 
</para>
241
 
</refsect2>
242
 
 
243
 
<!-- ##### SECTION See_Also ##### -->
244
 
 
245
 
 
246
 
<!-- ##### SECTION Stability_Level ##### -->
247
 
 
248
 
 
249
 
<!-- ##### SECTION Image ##### -->
250
 
 
251
 
 
252
 
<!-- ##### STRUCT GtkBuilder ##### -->
253
 
<para>
254
 
 
255
 
</para>
256
 
 
257
 
 
258
 
<!-- ##### ARG GtkBuilder:translation-domain ##### -->
259
 
<para>
260
 
 
261
 
</para>
262
 
 
263
 
<!-- ##### USER_FUNCTION GtkBuilderConnectFunc ##### -->
264
 
<para>
265
 
 
266
 
</para>
267
 
 
268
 
@builder: 
269
 
@object: 
270
 
@signal_name: 
271
 
@handler_name: 
272
 
@connect_object: 
273
 
@flags: 
274
 
@user_data: 
275
 
 
276
 
 
277
 
<!-- ##### ENUM GtkBuilderError ##### -->
278
 
<para>
279
 
Error codes that identify various errors that can occur while
280
 
using #GtkBuilder.
281
 
</para>
282
 
 
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
286
 
    can't handle.
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 
294
 
    missing.
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
298
 
    of GTK+.
299
 
@GTK_BUILDER_ERROR_DUPLICATE_ID: An object id occurred twice.
300
 
 
301
 
<!-- ##### FUNCTION gtk_builder_new ##### -->
302
 
<para>
303
 
 
304
 
</para>
305
 
 
306
 
@void: 
307
 
@Returns: 
308
 
 
309
 
 
310
 
<!-- ##### FUNCTION gtk_builder_add_from_file ##### -->
311
 
<para>
312
 
 
313
 
</para>
314
 
 
315
 
@builder: 
316
 
@filename: 
317
 
@error: 
318
 
@Returns: 
319
 
 
320
 
 
321
 
<!-- ##### FUNCTION gtk_builder_add_from_string ##### -->
322
 
<para>
323
 
 
324
 
</para>
325
 
 
326
 
@builder: 
327
 
@buffer: 
328
 
@length: 
329
 
@error: 
330
 
@Returns: 
331
 
 
332
 
 
333
 
<!-- ##### FUNCTION gtk_builder_add_objects_from_file ##### -->
334
 
<para>
335
 
 
336
 
</para>
337
 
 
338
 
@builder: 
339
 
@filename: 
340
 
@object_ids: 
341
 
@error: 
342
 
@Returns: 
343
 
 
344
 
 
345
 
<!-- ##### FUNCTION gtk_builder_add_objects_from_string ##### -->
346
 
<para>
347
 
 
348
 
</para>
349
 
 
350
 
@builder: 
351
 
@buffer: 
352
 
@length: 
353
 
@object_ids: 
354
 
@error: 
355
 
@Returns: 
356
 
 
357
 
 
358
 
<!-- ##### FUNCTION gtk_builder_get_object ##### -->
359
 
<para>
360
 
 
361
 
</para>
362
 
 
363
 
@builder: 
364
 
@name: 
365
 
@Returns: 
366
 
 
367
 
 
368
 
<!-- ##### FUNCTION gtk_builder_get_objects ##### -->
369
 
<para>
370
 
 
371
 
</para>
372
 
 
373
 
@builder: 
374
 
@Returns: 
375
 
 
376
 
 
377
 
<!-- ##### FUNCTION gtk_builder_connect_signals ##### -->
378
 
<para>
379
 
 
380
 
</para>
381
 
 
382
 
@builder: 
383
 
@user_data: 
384
 
 
385
 
 
386
 
<!-- ##### FUNCTION gtk_builder_connect_signals_full ##### -->
387
 
<para>
388
 
 
389
 
</para>
390
 
 
391
 
@builder: 
392
 
@func: 
393
 
@user_data: 
394
 
 
395
 
 
396
 
<!-- ##### FUNCTION gtk_builder_set_translation_domain ##### -->
397
 
<para>
398
 
 
399
 
</para>
400
 
 
401
 
@builder: 
402
 
@domain: 
403
 
 
404
 
 
405
 
<!-- ##### FUNCTION gtk_builder_get_translation_domain ##### -->
406
 
<para>
407
 
 
408
 
</para>
409
 
 
410
 
@builder: 
411
 
@Returns: 
412
 
 
413
 
 
414
 
<!-- ##### FUNCTION gtk_builder_get_type_from_name ##### -->
415
 
<para>
416
 
 
417
 
</para>
418
 
 
419
 
@builder: 
420
 
@type_name: 
421
 
@Returns: 
422
 
 
423
 
 
424
 
<!-- ##### FUNCTION gtk_builder_value_from_string ##### -->
425
 
<para>
426
 
 
427
 
</para>
428
 
 
429
 
@builder: 
430
 
@pspec: 
431
 
@string: 
432
 
@value: 
433
 
@error: 
434
 
@Returns: 
435
 
 
436
 
 
437
 
<!-- ##### FUNCTION gtk_builder_value_from_string_type ##### -->
438
 
<para>
439
 
 
440
 
</para>
441
 
 
442
 
@builder: 
443
 
@type: 
444
 
@string: 
445
 
@value: 
446
 
@error: 
447
 
@Returns: 
448
 
 
449
 
 
450
 
<!-- ##### MACRO GTK_BUILDER_WARN_INVALID_CHILD_TYPE ##### -->
451
 
<para>
452
 
This macro should be used to emit a warning about and unexpected 
453
 
@type value in a #GtkBuildable add_child implementation.
454
 
</para>
455
 
 
456
 
@object: the #GtkBuildable on which the warning ocurred
457
 
@type: the unexpected type value
458
 
 
459
 
 
460
 
<!-- ##### MACRO GTK_BUILDER_ERROR ##### -->
461
 
<para>
462
 
The #GError quark for #GtkBuilder errors
463
 
</para>
464
 
 
465
 
 
466