]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtkbuilder.sgml
More additions
[~andy/gtk] / docs / reference / gtk / tmpl / gtkbuilder.sgml
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 <para>
120 Setting properties of objects is pretty straightforward with
121 the &lt;property&gt; element: the "name" attribute specifies
122 the name of the property, and the content of the element 
123 specifies the value. If the "translatable" attribute is 
124 set to a true value, GTK+ uses gettext() (or dgettext() if
125 the builder has a translation domain set) to find a translation 
126 for the value. This happens before the value is parsed, so
127 it can be used for properties of any type, but it is probably
128 most useful for string properties. It is also possible to
129 specify a context to disambiguate short strings, and comments
130 which may help the translators.
131 </para>
132 <para>
133 GtkBuilder can parse textual representations for the most
134 common property types: characters, strings, integers, floating-point
135 numbers, booleans (strings like "TRUE", "t", "yes", "y", "1" are
136 interpreted as %TRUE, strings like "FALSE, "f", "no", "n", "0" are
137 interpreted as %FALSE), enumerations (can be specified by their 
138 name, nick or integer value), flags (can be specified by their name, 
139 nick, integer value, optionally combined with "|", e.g. 
140 "GTK_VISIBLE|GTK_REALIZED")  and colors (in a format understood by 
141 gdk_color_parse()). Objects can be referred to by their name. 
142 Pixbufs can be specified as a filename of an image file to load.
143 In general, GtkBuilder allows forward references to objects &mdash; 
144 an object doesn't have to constructed before it can be referred to.
145 The exception to this rule is that an object has to be constructed
146 before it can be used as the value of a construct-only property. 
147 </para>
148 <para>
149 Signal handlers are set up with the &lt;signal&gt; element.
150 The "name" attribute specifies the name of the signal, and the
151 "handler" attribute specifies the function to connect to the signal.
152 By default, GTK+ tries to find the handler using g_module_symbol(),
153 but this can be changed by passing a custom #GtkBuilderConnectFunc
154 to gtk_builder_connect_signals_full(). The remaining attributes,
155 "after", "swapped" and "object", have the same meaning as the 
156 corresponding parameters of the g_signal_connect_object() or
157 g_signal_connect_data() functions. A "last_modification_time" attribute
158 is also allowed, but it does not have a meaning to the builder.
159 </para>
160 <para>
161 Sometimes it is necessary to refer to widgets which have implicitly
162 been constructed by GTK+ as part of a composite widget, to set 
163 properties on them or to add further children (e.g. the @vbox 
164 of a #GtkDialog). This can be achieved by setting the "internal-child"
165 propery of the &lt;child&gt; element to a true value. Note that
166 GtkBuilder still requires an &lt;object&gt; element for the internal
167 child, even if it has already been constructed.
168 </para>
169 <para>
170 A number of widgets have different places where a child can be 
171 added (e.g. tabs vs. page content in notebooks). This can be reflected
172 in a UI definition by specifying the "type" attribute on a &lt;child&gt;
173 The possible values for the "type" attribute are described in 
174 the sections describing the widget-specific portions of UI definitions.
175 </para>
176 <example>
177 <title>A GtkBuilder UI Definition</title>
178 <programlisting><![CDATA[
179 <interface>
180   <object class="GtkDialog" id="dialog1">
181     <child internal-child="vbox">
182       <object class="GtkVBox" id="vbox1">
183         <property name="border-width">10</property>
184         <child internal-child="action_area">
185           <object class="GtkHButtonBox" id="hbuttonbox1">
186             <property name="border-width">20</property>
187             <child>
188               <object class="GtkButton" id="ok_button">
189                 <property name="label">gtk-ok</property>
190                 <property name="use-stock">TRUE</property>
191                 <signal name="clicked" handler="ok_button_clicked"/>
192               </object>
193             </child>
194           </object>
195         </child>
196       </object>
197     </child>
198   </object>
199 </interface>
200 ]]></programlisting>
201 </example>
202 <para>
203 Beyond this general structure, several object classes define
204 their own XML DTD fragments for filling in the ANY placeholders
205 in the DTD above. Note that a custom element in a &lt;child&gt;
206 element gets parsed by the custom tag handler of the parent
207 object, while a custom element in an &lt;object&gt; element
208 gets parsed by the custom tag handler of the object. 
209 </para>
210 <para>
211 These XML fragments are explained in the documentation of the
212 respective objects, see 
213 <link linkend="GtkWidget-BUILDER-UI">GtkWidget</link>,
214 <link linkend="GtkLabel-BUILDER-UI">GtkLabel</link>,
215 <link linkend="GtkWindow-BUILDER-UI">GtkWindow</link>,
216 <link linkend="GtkContainer-BUILDER-UI">GtkContainer</link>,
217 <link linkend="GtkDialog-BUILDER-UI">GtkDialog</link>,
218 <link linkend="GtkCellLayout-BUILDER-UI">GtkCellLayout</link>,
219 <link linkend="GtkColorSelectionDialog-BUILDER-UI">GtkColorSelectionDialog</link>,
220 <link linkend="GtkFontSelectionDialog-BUILDER-UI">GtkFontSelectionDialog</link>,
221 <link linkend="GtkComboBoxEntry-BUILDER-UI">GtkComboBoxEntry</link>,
222 <link linkend="GtkExpander-BUILDER-UI">GtkExpander</link>,
223 <link linkend="GtkFrame-BUILDER-UI">GtkFrame</link>,
224 <link linkend="GtkListStore-BUILDER-UI">GtkListStore</link>,
225 <link linkend="GtkTreeStore-BUILDER-UI">GtkTreeStore</link>,
226 <link linkend="GtkNotebook-BUILDER-UI">GtkNotebook</link>,
227 <link linkend="GtkSizeGroup-BUILDER-UI">GtkSizeGroup</link>,
228 <link linkend="GtkTreeView-BUILDER-UI">GtkTreeView</link>,
229 <link linkend="GtkUIManager-BUILDER-UI">GtkUIManager</link>,
230 <link linkend="GtkActionGroup-BUILDER-UI">GtkActionGroup</link>.
231 <link linkend="GtkMenuItem-BUILDER-UI">GtkMenuItem</link>.
232 </para>
233 </refsect2>
234
235 <!-- ##### SECTION See_Also ##### -->
236
237
238 <!-- ##### SECTION Stability_Level ##### -->
239
240
241 <!-- ##### STRUCT GtkBuilder ##### -->
242 <para>
243
244 </para>
245
246
247 <!-- ##### ARG GtkBuilder:translation-domain ##### -->
248 <para>
249
250 </para>
251
252 <!-- ##### USER_FUNCTION GtkBuilderConnectFunc ##### -->
253 <para>
254
255 </para>
256
257 @builder: 
258 @object: 
259 @signal_name: 
260 @handler_name: 
261 @connect_object: 
262 @flags: 
263 @user_data: 
264
265
266 <!-- ##### ENUM GtkBuilderError ##### -->
267 <para>
268 Error codes that identify various error that can occur while
269 using #GtkBuilder.
270 </para>
271
272 @GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION: A type-func attribute didn't name
273     a function that returns a #GType.
274 @GTK_BUILDER_ERROR_UNHANDLED_TAG: The input contained a tag that #GtkBuilder
275     can't handle. 
276 @GTK_BUILDER_ERROR_MISSING_ATTRIBUTE: An attribute that is required by
277     #GtkBuilder was missing.
278 @GTK_BUILDER_ERROR_INVALID_ATTRIBUTE: #GtkBuilder found an attribute that 
279     it doesn't understand.
280 @GTK_BUILDER_ERROR_INVALID_TAG: #GtkBuilder found a tag that
281     it doesn't understand.
282 @GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE: 
283 @GTK_BUILDER_ERROR_INVALID_VALUE: #GtkBuilder couldn't parse
284     some attribute value.
285 @GTK_BUILDER_ERROR_VERSION_MISMATCH: The input file requires a newer version
286     of GTK+.
287
288
289 <!-- ##### FUNCTION gtk_builder_new ##### -->
290 <para>
291
292 </para>
293
294 @Returns: 
295
296
297 <!-- ##### FUNCTION gtk_builder_add_from_file ##### -->
298 <para>
299
300 </para>
301
302 @builder: 
303 @filename: 
304 @error: 
305 @Returns: 
306
307
308 <!-- ##### FUNCTION gtk_builder_add_from_string ##### -->
309 <para>
310
311 </para>
312
313 @builder: 
314 @buffer: 
315 @length: 
316 @error: 
317 @Returns: 
318
319
320 <!-- ##### FUNCTION gtk_builder_add_objects_from_file ##### -->
321 <para>
322
323 </para>
324
325 @builder: 
326 @filename: 
327 @object_ids: 
328 @error: 
329 @Returns: 
330
331
332 <!-- ##### FUNCTION gtk_builder_add_objects_from_string ##### -->
333 <para>
334
335 </para>
336
337 @builder: 
338 @buffer: 
339 @length: 
340 @object_ids: 
341 @error: 
342 @Returns: 
343
344
345 <!-- ##### FUNCTION gtk_builder_get_object ##### -->
346 <para>
347
348 </para>
349
350 @builder: 
351 @name: 
352 @Returns: 
353
354
355 <!-- ##### FUNCTION gtk_builder_get_objects ##### -->
356 <para>
357
358 </para>
359
360 @builder: 
361 @Returns: 
362
363
364 <!-- ##### FUNCTION gtk_builder_connect_signals ##### -->
365 <para>
366
367 </para>
368
369 @builder: 
370 @user_data: 
371
372
373 <!-- ##### FUNCTION gtk_builder_connect_signals_full ##### -->
374 <para>
375
376 </para>
377
378 @builder: 
379 @func: 
380 @user_data: 
381
382
383 <!-- ##### FUNCTION gtk_builder_set_translation_domain ##### -->
384 <para>
385
386 </para>
387
388 @builder: 
389 @domain: 
390
391
392 <!-- ##### FUNCTION gtk_builder_get_translation_domain ##### -->
393 <para>
394
395 </para>
396
397 @builder: 
398 @Returns: 
399
400
401 <!-- ##### FUNCTION gtk_builder_get_type_from_name ##### -->
402 <para>
403
404 </para>
405
406 @builder: 
407 @type_name: 
408 @Returns: 
409
410
411 <!-- ##### FUNCTION gtk_builder_value_from_string ##### -->
412 <para>
413
414 </para>
415
416 @builder: 
417 @pspec: 
418 @string: 
419 @value: 
420 @error: 
421 @Returns: 
422
423
424 <!-- ##### FUNCTION gtk_builder_value_from_string_type ##### -->
425 <para>
426
427 </para>
428
429 @builder: 
430 @type: 
431 @string: 
432 @value: 
433 @error: 
434 @Returns: 
435
436
437 <!-- ##### MACRO GTK_BUILDER_WARN_INVALID_CHILD_TYPE ##### -->
438 <para>
439 This macro should be used to emit a warning about and unexpected 
440 @type value in a #GtkBuildable add_child implementation.
441 </para>
442
443 @object: the #GtkBuildable on which the warning ocurred
444 @type: the unexpected type value
445
446
447 <!-- ##### MACRO GTK_BUILDER_ERROR ##### -->
448 <para>
449 The #GError quark for #GtkBuilder errors
450 </para>
451
452
453