]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilder.c
Updated Norwegian bokmål translation
[~andy/gtk] / gtk / gtkbuilder.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1998-2002 James Henstridge <james@daa.com.au>
3  * Copyright (C) 2006-2007 Async Open Source,
4  *                         Johan Dahlin <jdahlin@async.com.br>,
5  *                         Henrique Romano <henrique@async.com.br>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gtkbuilder
25  * @Short_description: Build an interface from an XML UI definition
26  * @Title: GtkBuilder
27  *
28  * A GtkBuilder is an auxiliary object that reads textual descriptions
29  * of a user interface and instantiates the described objects. To pass a
30  * description to a GtkBuilder, call gtk_builder_add_from_file() or
31  * gtk_builder_add_from_string(). These functions can be called multiple
32  * times; the builder merges the content of all descriptions.
33  *
34  * A GtkBuilder holds a reference to all objects that it has constructed
35  * and drops these references when it is finalized. This finalization can
36  * cause the destruction of non-widget objects or widgets which are not
37  * contained in a toplevel window. For toplevel windows constructed by a
38  * builder, it is the responsibility of the user to call gtk_widget_destroy()
39  * to get rid of them and all the widgets they contain.
40  *
41  * The functions gtk_builder_get_object() and gtk_builder_get_objects()
42  * can be used to access the widgets in the interface by the names assigned
43  * to them inside the UI description. Toplevel windows returned by these
44  * functions will stay around until the user explicitly destroys them
45  * with gtk_widget_destroy(). Other widgets will either be part of a
46  * larger hierarchy constructed by the builder (in which case you should
47  * not have to worry about their lifecycle), or without a parent, in which
48  * case they have to be added to some container to make use of them.
49  * Non-widget objects need to be reffed with g_object_ref() to keep them
50  * beyond the lifespan of the builder.
51  *
52  * The function gtk_builder_connect_signals() and variants thereof can be
53  * used to connect handlers to the named signals in the description.
54  *
55  * <refsect2 id="BUILDER-UI">
56  * <title>GtkBuilder UI Definitions</title>
57  * <para>
58  * GtkBuilder parses textual descriptions of user interfaces which are specified
59  * in an XML format which can be roughly described by the DTD below. We refer to
60  * these descriptions as <firstterm>GtkBuilder UI definitions</firstterm> or
61  * just <firstterm>UI definitions</firstterm> if the context is clear. Do not
62  * confuse GtkBuilder UI Definitions with
63  * <link linkend="XML-UI">GtkUIManager UI Definitions</link>, which are more
64  * limited in scope.
65  * </para>
66  * <programlisting><![CDATA[
67  * <!ELEMENT interface (requires|object)* >
68  * <!ELEMENT object    (property|signal|child|ANY)* >
69  * <!ELEMENT property  PCDATA >
70  * <!ELEMENT signal    EMPTY >
71  * <!ELEMENT requires  EMPTY >
72  * <!ELEMENT child     (object|ANY*) >
73  *
74  * <!ATTLIST interface  domain              #IMPLIED >
75  * <!ATTLIST object     id                  #REQUIRED
76  *                      class               #REQUIRED
77  *                      type-func           #IMPLIED
78  *                      constructor         #IMPLIED >
79  * <!ATTLIST requires   lib                         #REQUIRED
80  *                      version                     #REQUIRED >
81  * <!ATTLIST property   name                #REQUIRED
82  *                      translatable        #IMPLIED
83  *                      comments               #IMPLIED
84  *                      context                #IMPLIED >
85  * <!ATTLIST signal     name                #REQUIRED
86  *                      handler             #REQUIRED
87  *                      after               #IMPLIED
88  *                      swapped             #IMPLIED
89  *                      object              #IMPLIED
90  *                      last_modification_time #IMPLIED >
91  * <!ATTLIST child      type                #IMPLIED
92  *                      internal-child      #IMPLIED >
93  * ]]></programlisting>
94  * <para>
95  * The toplevel element is &lt;interface&gt;. It optionally takes a "domain"
96  * attribute, which will make the builder look for translated strings using
97  * dgettext() in the domain specified. This can also be done by calling
98  * gtk_builder_set_translation_domain() on the builder. Objects are described by
99  * &lt;object&gt; elements, which can contain &lt;property&gt; elements to set
100  * properties, &lt;signal&gt; elements which connect signals to handlers, and
101  * &lt;child&gt; elements, which describe child objects (most often widgets
102  * inside a container, but also e.g. actions in an action group, or columns in a
103  * tree model). A &lt;child&gt; element contains an &lt;object&gt; element which
104  * describes the child object. The target toolkit version(s) are described by
105  * &lt;requires&gt; elements, the "lib" attribute specifies the widget library
106  * in question (currently the only supported value is "gtk+") and the "version"
107  * attribute specifies the target version in the form
108  * "&lt;major&gt;.&lt;minor&gt;". The builder will error out if the version
109  * requirements are not met.
110  *
111  * Typically, the specific kind of object represented by an &lt;object&gt;
112  * element is specified by the "class" attribute. If the type has not been
113  * loaded yet, GTK+ tries to find the <function>_get_type()</function> from the
114  * class name by applying heuristics. This works in most cases, but if
115  * necessary, it is possible to specify the name of the
116  * <function>_get_type()</function> explictly with the "type-func" attribute.
117  * As a special case, GtkBuilder allows to use an object that has been
118  * constructed by a #GtkUIManager in another part of the UI definition by
119  * specifying the id of the #GtkUIManager in the "constructor" attribute and the
120  * name of the object in the "id" attribute.
121  *
122  * Objects must be given a name with the "id" attribute, which allows the
123  * application to retrieve them from the builder with gtk_builder_get_object().
124  * An id is also necessary to use the object as property value in other parts of
125  * the UI definition.
126  * </para>
127  * <note><para>
128  * Prior to 2.20, GtkBuilder was setting the "name" property of constructed widgets to the
129  * "id" attribute. In GTK+ 2.20 or newer, you have to use gtk_buildable_get_name() instead
130  * of gtk_widget_get_name() to obtain the "id", or set the "name" property in your UI
131  * definition.
132  * </para></note>
133  * <para>
134  * Setting properties of objects is pretty straightforward with the
135  * &lt;property&gt; element: the "name" attribute specifies the name of the
136  * property, and the content of the element specifies the value. If the
137  * "translatable" attribute is set to a true value, GTK+ uses gettext() (or
138  * dgettext() if the builder has a translation domain set) to find a translation
139  * for the value. This happens before the value is parsed, so it can be used for
140  * properties of any type, but it is probably most useful for string properties.
141  * It is also possible to specify a context to disambiguate short strings, and
142  * comments which may help the translators.
143  *
144  * GtkBuilder can parse textual representations for the most common property
145  * types: characters, strings, integers, floating-point numbers, booleans
146  * (strings like "TRUE", "t", "yes", "y", "1" are interpreted as %TRUE, strings
147  * like "FALSE, "f", "no", "n", "0" are interpreted as %FALSE), enumerations
148  * (can be specified by their name, nick or integer value), flags (can be
149  * specified by their name, nick, integer value, optionally combined with "|",
150  * e.g. "GTK_VISIBLE|GTK_REALIZED")  and colors (in a format understood by
151  * gdk_color_parse()). Objects can be referred to by their name. Pixbufs can be
152  * specified as a filename of an image file to load. In general, GtkBuilder
153  * allows forward references to objects &mdash; an object doesn't have to be
154  * constructed before it can be referred to. The exception to this rule is that
155  * an object has to be constructed before it can be used as the value of a
156  * construct-only property.
157  *
158  * Signal handlers are set up with the &lt;signal&gt; element. The "name"
159  * attribute specifies the name of the signal, and the "handler" attribute
160  * specifies the function to connect to the signal. By default, GTK+ tries to
161  * find the handler using g_module_symbol(), but this can be changed by passing
162  * a custom #GtkBuilderConnectFunc to gtk_builder_connect_signals_full(). The
163  * remaining attributes, "after", "swapped" and "object", have the same meaning
164  * as the corresponding parameters of the g_signal_connect_object() or
165  * g_signal_connect_data() functions. A "last_modification_time" attribute
166  * is also allowed, but it does not have a meaning to the builder.
167  *
168  * Sometimes it is necessary to refer to widgets which have implicitly been
169  * constructed by GTK+ as part of a composite widget, to set properties on them
170  * or to add further children (e.g. the @vbox of a #GtkDialog). This can be
171  * achieved by setting the "internal-child" propery of the &lt;child&gt; element
172  * to a true value. Note that GtkBuilder still requires an &lt;object&gt;
173  * element for the internal child, even if it has already been constructed.
174  *
175  * A number of widgets have different places where a child can be added (e.g.
176  * tabs vs. page content in notebooks). This can be reflected in a UI definition
177  * by specifying the "type" attribute on a &lt;child&gt;. The possible values
178  * for the "type" attribute are described in the sections describing the
179  * widget-specific portions of UI definitions.
180  * </para>
181  * <example>
182  * <title>A GtkBuilder UI Definition</title>
183  * <programlisting><![CDATA[
184  * <interface>
185  *   <object class="GtkDialog" id="dialog1">
186  *     <child internal-child="vbox">
187  *       <object class="GtkVBox" id="vbox1">
188  *         <property name="border-width">10</property>
189  *         <child internal-child="action_area">
190  *           <object class="GtkHButtonBox" id="hbuttonbox1">
191  *             <property name="border-width">20</property>
192  *             <child>
193  *               <object class="GtkButton" id="ok_button">
194  *                 <property name="label">gtk-ok</property>
195  *                 <property name="use-stock">TRUE</property>
196  *                 <signal name="clicked" handler="ok_button_clicked"/>
197  *               </object>
198  *             </child>
199  *           </object>
200  *         </child>
201  *       </object>
202  *     </child>
203  *   </object>
204  * </interface>
205  * ]]></programlisting>
206  * </example>
207  * <para>
208  * Beyond this general structure, several object classes define their own XML
209  * DTD fragments for filling in the ANY placeholders in the DTD above. Note that
210  * a custom element in a &lt;child&gt; element gets parsed by the custom tag
211  * handler of the parent object, while a custom element in an &lt;object&gt;
212  * element gets parsed by the custom tag handler of the object.
213  *
214  * These XML fragments are explained in the documentation of the respective
215  * objects, see
216  * <link linkend="GtkWidget-BUILDER-UI">GtkWidget</link>,
217  * <link linkend="GtkLabel-BUILDER-UI">GtkLabel</link>,
218  * <link linkend="GtkWindow-BUILDER-UI">GtkWindow</link>,
219  * <link linkend="GtkContainer-BUILDER-UI">GtkContainer</link>,
220  * <link linkend="GtkDialog-BUILDER-UI">GtkDialog</link>,
221  * <link linkend="GtkCellLayout-BUILDER-UI">GtkCellLayout</link>,
222  * <link linkend="GtkColorSelectionDialog-BUILDER-UI">GtkColorSelectionDialog</link>,
223  * <link linkend="GtkFontSelectionDialog-BUILDER-UI">GtkFontSelectionDialog</link>,
224  * <link linkend="GtkComboBoxEntry-BUILDER-UI">GtkComboBoxEntry</link>,
225  * <link linkend="GtkExpander-BUILDER-UI">GtkExpander</link>,
226  * <link linkend="GtkFrame-BUILDER-UI">GtkFrame</link>,
227  * <link linkend="GtkListStore-BUILDER-UI">GtkListStore</link>,
228  * <link linkend="GtkTreeStore-BUILDER-UI">GtkTreeStore</link>,
229  * <link linkend="GtkNotebook-BUILDER-UI">GtkNotebook</link>,
230  * <link linkend="GtkSizeGroup-BUILDER-UI">GtkSizeGroup</link>,
231  * <link linkend="GtkTreeView-BUILDER-UI">GtkTreeView</link>,
232  * <link linkend="GtkUIManager-BUILDER-UI">GtkUIManager</link>,
233  * <link linkend="GtkActionGroup-BUILDER-UI">GtkActionGroup</link>.
234  * <link linkend="GtkMenuItem-BUILDER-UI">GtkMenuItem</link>,
235  * <link linkend="GtkAssistant-BUILDER-UI">GtkAssistant</link>,
236  * <link linkend="GtkScale-BUILDER-UI">GtkScale</link>.
237  * </para>
238  * </refsect2>
239  */
240
241 #include "config.h"
242 #include <errno.h> /* errno */
243 #include <stdlib.h> /* strtol, strtoul */
244 #include <string.h> /* strlen */
245
246 #include "gtkbuilder.h"
247 #include "gtkbuildable.h"
248 #include "gtkbuilderprivate.h"
249 #include "gtkmain.h"
250 #include "gtkintl.h"
251 #include "gtkprivate.h"
252 #include "gtktypebuiltins.h"
253 #include "gtkwindow.h"
254 #include "gtkicontheme.h"
255 #include "gtkstock.h"
256
257
258 static void gtk_builder_class_init     (GtkBuilderClass *klass);
259 static void gtk_builder_init           (GtkBuilder      *builder);
260 static void gtk_builder_finalize       (GObject         *object);
261 static void gtk_builder_set_property   (GObject         *object,
262                                         guint            prop_id,
263                                         const GValue    *value,
264                                         GParamSpec      *pspec);
265 static void gtk_builder_get_property   (GObject         *object,
266                                         guint            prop_id,
267                                         GValue          *value,
268                                         GParamSpec      *pspec);
269 static GType gtk_builder_real_get_type_from_name (GtkBuilder  *builder,
270                                                   const gchar *type_name);
271
272 enum {
273   PROP_0,
274   PROP_TRANSLATION_DOMAIN,
275 };
276
277 struct _GtkBuilderPrivate
278 {
279   gchar *domain;
280   GHashTable *objects;
281   GSList *delayed_properties;
282   GSList *signals;
283   gchar *filename;
284 };
285
286 G_DEFINE_TYPE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)
287
288 static void
289 gtk_builder_class_init (GtkBuilderClass *klass)
290 {
291   GObjectClass *gobject_class;
292
293   gobject_class = G_OBJECT_CLASS (klass);
294
295   gobject_class->finalize = gtk_builder_finalize;
296   gobject_class->set_property = gtk_builder_set_property;
297   gobject_class->get_property = gtk_builder_get_property;
298
299   klass->get_type_from_name = gtk_builder_real_get_type_from_name;
300
301  /** 
302   * GtkBuilder:translation-domain:
303   *
304   * The translation domain used when translating property values that
305   * have been marked as translatable in interface descriptions.
306   * If the translation domain is %NULL, #GtkBuilder uses gettext(),
307   * otherwise g_dgettext().
308   *
309   * Since: 2.12
310   */
311   g_object_class_install_property (gobject_class,
312                                    PROP_TRANSLATION_DOMAIN,
313                                    g_param_spec_string ("translation-domain",
314                                                         P_("Translation Domain"),
315                                                         P_("The translation domain used by gettext"),
316                                                         NULL,
317                                                         GTK_PARAM_READWRITE));
318
319   g_type_class_add_private (gobject_class, sizeof (GtkBuilderPrivate));
320 }
321
322 static void
323 gtk_builder_init (GtkBuilder *builder)
324 {
325   builder->priv = G_TYPE_INSTANCE_GET_PRIVATE (builder, GTK_TYPE_BUILDER,
326                                                GtkBuilderPrivate);
327   builder->priv->domain = NULL;
328   builder->priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal,
329                                                   g_free, g_object_unref);
330 }
331
332
333 /*
334  * GObject virtual methods
335  */
336
337 static void
338 gtk_builder_finalize (GObject *object)
339 {
340   GtkBuilderPrivate *priv = GTK_BUILDER (object)->priv;
341   
342   g_free (priv->domain);
343   g_free (priv->filename);
344   
345   g_hash_table_destroy (priv->objects);
346
347   g_slist_foreach (priv->signals, (GFunc) _free_signal_info, NULL);
348   g_slist_free (priv->signals);
349   
350   G_OBJECT_CLASS (gtk_builder_parent_class)->finalize (object);
351 }
352
353 static void
354 gtk_builder_set_property (GObject      *object,
355                           guint         prop_id,
356                           const GValue *value,
357                           GParamSpec   *pspec)
358 {
359   GtkBuilder *builder = GTK_BUILDER (object);
360
361   switch (prop_id)
362     {
363     case PROP_TRANSLATION_DOMAIN:
364       gtk_builder_set_translation_domain (builder, g_value_get_string (value));
365       break;
366     default:
367       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368       break;
369     }
370 }
371
372 static void
373 gtk_builder_get_property (GObject    *object,
374                           guint       prop_id,
375                           GValue     *value,
376                           GParamSpec *pspec)
377 {
378   GtkBuilder *builder = GTK_BUILDER (object);
379
380   switch (prop_id)
381     {
382     case PROP_TRANSLATION_DOMAIN:
383       g_value_set_string (value, builder->priv->domain);
384       break;
385     default:
386       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387       break;
388     }
389 }
390
391
392 /*
393  * Try to map a type name to a _get_type function
394  * and call it, eg:
395  *
396  * GtkWindow -> gtk_window_get_type
397  * GtkHBox -> gtk_hbox_get_type
398  * GtkUIManager -> gtk_ui_manager_get_type
399  *
400  */
401 static GType
402 _gtk_builder_resolve_type_lazily (const gchar *name)
403 {
404   static GModule *module = NULL;
405   GTypeGetFunc func;
406   GString *symbol_name = g_string_new ("");
407   char c, *symbol;
408   int i;
409   GType gtype = G_TYPE_INVALID;
410
411   if (!module)
412     module = g_module_open (NULL, 0);
413   
414   for (i = 0; name[i] != '\0'; i++)
415     {
416       c = name[i];
417       /* skip if uppercase, first or previous is uppercase */
418       if ((c == g_ascii_toupper (c) &&
419            i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
420           (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
421            name[i-1] == g_ascii_toupper (name[i-1]) &&
422            name[i-2] == g_ascii_toupper (name[i-2])))
423         g_string_append_c (symbol_name, '_');
424       g_string_append_c (symbol_name, g_ascii_tolower (c));
425     }
426   g_string_append (symbol_name, "_get_type");
427   
428   symbol = g_string_free (symbol_name, FALSE);
429
430   if (g_module_symbol (module, symbol, (gpointer)&func))
431     gtype = func ();
432   
433   g_free (symbol);
434
435   return gtype;
436 }
437
438 /*
439  * GtkBuilder virtual methods
440  */
441
442 static GType
443 gtk_builder_real_get_type_from_name (GtkBuilder  *builder, 
444                                      const gchar *type_name)
445 {
446   GType gtype;
447
448   gtype = g_type_from_name (type_name);
449   if (gtype != G_TYPE_INVALID)
450     return gtype;
451
452   return _gtk_builder_resolve_type_lazily (type_name);
453 }
454
455 typedef struct
456 {
457   gchar *object;
458   gchar *name;
459   gchar *value;
460 } DelayedProperty;
461
462 static void
463 gtk_builder_get_parameters (GtkBuilder  *builder,
464                             GType        object_type,
465                             const gchar *object_name,
466                             GSList      *properties,
467                             GArray      **parameters,
468                             GArray      **construct_parameters)
469 {
470   GSList *l;
471   GParamSpec *pspec;
472   GObjectClass *oclass;
473   DelayedProperty *property;
474   GError *error = NULL;
475   
476   oclass = g_type_class_ref (object_type);
477   g_assert (oclass != NULL);
478
479   *parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
480   *construct_parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
481
482   for (l = properties; l; l = l->next)
483     {
484       PropertyInfo *prop = (PropertyInfo*)l->data;
485       GParameter parameter = { NULL };
486
487       pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
488                                             prop->name);
489       if (!pspec)
490         {
491           g_warning ("Unknown property: %s.%s",
492                      g_type_name (object_type), prop->name);
493           continue;
494         }
495
496       parameter.name = prop->name;
497
498       if (G_IS_PARAM_SPEC_OBJECT (pspec) &&
499           (G_PARAM_SPEC_VALUE_TYPE (pspec) != GDK_TYPE_PIXBUF))
500         {
501           if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
502             {
503               GObject *object;
504               object = gtk_builder_get_object (builder, prop->data);
505               if (!object)
506                 {
507                   g_warning ("Failed to get constuct only property "
508                              "%s of %s with value `%s'",
509                              prop->name, object_name, prop->data);
510                   continue;
511                 }
512               g_value_init (&parameter.value, G_OBJECT_TYPE (object));
513               g_value_set_object (&parameter.value, object);
514             }
515           else
516             {
517               property = g_slice_new (DelayedProperty);
518               property->object = g_strdup (object_name);
519               property->name = g_strdup (prop->name);
520               property->value = g_strdup (prop->data);
521               builder->priv->delayed_properties =
522                 g_slist_prepend (builder->priv->delayed_properties, property);
523
524               continue;
525             }
526         }
527       else if (!gtk_builder_value_from_string (builder, pspec,
528                                                prop->data, &parameter.value, &error))
529         {
530           g_warning ("Failed to set property %s.%s to %s: %s",
531                      g_type_name (object_type), prop->name, prop->data,
532                      error->message);
533           g_error_free (error);
534           error = NULL;
535           continue;
536         }
537
538       if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
539         g_array_append_val (*construct_parameters, parameter);
540       else
541         g_array_append_val (*parameters, parameter);
542     }
543
544   g_type_class_unref (oclass);
545 }
546
547 static GObject *
548 gtk_builder_get_internal_child (GtkBuilder  *builder,
549                                 ObjectInfo  *info,
550                                 const gchar *childname,
551                                 GError      **error)
552 {
553   GObject *obj = NULL;
554
555   while (!obj)
556     {
557       if (!info->parent)
558         break;
559
560       info = (ObjectInfo*)((ChildInfo*)info->parent)->parent;
561       if (!info)
562         break;
563
564       GTK_NOTE (BUILDER,
565                 g_print ("Trying to get internal child %s from %s\n",
566                          childname,
567                          gtk_buildable_get_name (GTK_BUILDABLE (info->object))));
568
569       if (GTK_IS_BUILDABLE (info->object))
570           obj = gtk_buildable_get_internal_child (GTK_BUILDABLE (info->object),
571                                                   builder,
572                                                   childname);
573     };
574
575   if (!obj)
576     {
577       g_set_error (error,
578                    GTK_BUILDER_ERROR,
579                    GTK_BUILDER_ERROR_INVALID_VALUE,
580                    "Unknown internal child: %s", childname);
581     }
582   return obj;
583 }
584
585 GObject *
586 _gtk_builder_construct (GtkBuilder *builder,
587                         ObjectInfo *info,
588                         GError **error)
589 {
590   GArray *parameters, *construct_parameters;
591   GType object_type;
592   GObject *obj;
593   int i;
594   GtkBuildableIface *iface;
595   gboolean custom_set_property;
596   GtkBuildable *buildable;
597
598   g_assert (info->class_name != NULL);
599   object_type = gtk_builder_get_type_from_name (builder, info->class_name);
600   if (object_type == G_TYPE_INVALID)
601     {
602       g_set_error (error,
603                    GTK_BUILDER_ERROR,
604                    GTK_BUILDER_ERROR_INVALID_VALUE,
605                    "Invalid object type `%s'",
606                    info->class_name);
607       return NULL;
608     }
609
610   gtk_builder_get_parameters (builder, object_type,
611                               info->id,
612                               info->properties,
613                               &parameters,
614                               &construct_parameters);
615
616   if (info->constructor)
617     {
618       GObject *constructor;
619
620       constructor = gtk_builder_get_object (builder, info->constructor);
621       if (constructor == NULL)
622         {
623           g_set_error (error,
624                        GTK_BUILDER_ERROR,
625                        GTK_BUILDER_ERROR_INVALID_VALUE,
626                        "Unknown object constructor for %s: %s",
627                        info->id,
628                        info->constructor);
629           g_array_free (parameters, TRUE);
630           g_array_free (construct_parameters, TRUE);
631           return NULL;
632         }
633       obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
634                                            builder,
635                                            info->id);
636       g_assert (obj != NULL);
637       if (construct_parameters->len)
638         g_warning ("Can't pass in construct-only parameters to %s", info->id);
639     }
640   else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
641     {
642       gchar *childname = ((ChildInfo*)info->parent)->internal_child;
643       obj = gtk_builder_get_internal_child (builder, info, childname, error);
644       if (!obj)
645         {
646           g_array_free (parameters, TRUE);
647           g_array_free (construct_parameters, TRUE);
648           return NULL;
649         }
650       if (construct_parameters->len)
651         g_warning ("Can't pass in construct-only parameters to %s", childname);
652       g_object_ref (obj);
653     }
654   else
655     {
656       obj = g_object_newv (object_type,
657                            construct_parameters->len,
658                            (GParameter *)construct_parameters->data);
659
660       /* No matter what, make sure we have a reference.
661        *
662        * If it's an initially unowned object, sink it.
663        * If it's not initially unowned then we have the reference already.
664        *
665        * In the case that this is a window it will be sunk already and
666        * this is effectively a call to g_object_ref().  That's what
667        * we want.
668        */
669       if (G_IS_INITIALLY_UNOWNED (obj))
670         g_object_ref_sink (obj);
671
672       GTK_NOTE (BUILDER,
673                 g_print ("created %s of type %s\n", info->id, info->class_name));
674
675       for (i = 0; i < construct_parameters->len; i++)
676         {
677           GParameter *param = &g_array_index (construct_parameters,
678                                               GParameter, i);
679           g_value_unset (&param->value);
680         }
681     }
682   g_array_free (construct_parameters, TRUE);
683
684   custom_set_property = FALSE;
685   buildable = NULL;
686   iface = NULL;
687   if (GTK_IS_BUILDABLE (obj))
688     {
689       buildable = GTK_BUILDABLE (obj);
690       iface = GTK_BUILDABLE_GET_IFACE (obj);
691       if (iface->set_buildable_property)
692         custom_set_property = TRUE;
693     }
694
695   for (i = 0; i < parameters->len; i++)
696     {
697       GParameter *param = &g_array_index (parameters, GParameter, i);
698       if (custom_set_property)
699         iface->set_buildable_property (buildable, builder, param->name, &param->value);
700       else
701         g_object_set_property (obj, param->name, &param->value);
702
703 #if G_ENABLE_DEBUG
704       if (gtk_debug_flags & GTK_DEBUG_BUILDER)
705         {
706           gchar *str = g_strdup_value_contents ((const GValue*)&param->value);
707           g_print ("set %s: %s = %s\n", info->id, param->name, str);
708           g_free (str);
709         }
710 #endif      
711       g_value_unset (&param->value);
712     }
713   g_array_free (parameters, TRUE);
714   
715   if (GTK_IS_BUILDABLE (obj))
716     gtk_buildable_set_name (buildable, info->id);
717   else
718     g_object_set_data_full (obj,
719                             "gtk-builder-name",
720                             g_strdup (info->id),
721                             g_free);
722
723   /* we already own a reference to obj.  put it in the hash table. */
724   g_hash_table_insert (builder->priv->objects, g_strdup (info->id), obj);
725   
726   return obj;
727 }
728
729
730 void
731 _gtk_builder_add (GtkBuilder *builder,
732                   ChildInfo  *child_info)
733 {
734   GObject *object;
735   GObject *parent;
736
737   /* Internal children are already added
738    * Also prevent us from being called twice.
739    */
740   if (!child_info ||
741       child_info->internal_child ||
742       child_info->added)
743     return;
744
745   object = child_info->object;
746   if (!object)
747     return;
748
749   if (!child_info->parent)
750     {
751       g_warning ("%s: Not adding, No parent",
752                  gtk_buildable_get_name (GTK_BUILDABLE (object)));
753       return;
754     }
755
756   g_assert (object != NULL);
757
758   parent = ((ObjectInfo*)child_info->parent)->object;
759   g_assert (GTK_IS_BUILDABLE (parent));
760
761   GTK_NOTE (BUILDER,
762             g_print ("adding %s to %s\n",
763                      gtk_buildable_get_name (GTK_BUILDABLE (object)),
764                      gtk_buildable_get_name (GTK_BUILDABLE (parent))));
765   
766   gtk_buildable_add_child (GTK_BUILDABLE (parent), builder, object,
767                            child_info->type);
768
769   child_info->added = TRUE;
770 }
771
772 void
773 _gtk_builder_add_signals (GtkBuilder *builder,
774                           GSList     *signals)
775 {
776   builder->priv->signals = g_slist_concat (builder->priv->signals,
777                                            g_slist_copy (signals));
778 }
779
780 static void
781 gtk_builder_apply_delayed_properties (GtkBuilder *builder)
782 {
783   GSList *l, *props;
784   DelayedProperty *property;
785   GObject *object;
786   GType object_type;
787   GObjectClass *oclass;
788   GParamSpec *pspec;
789
790   /* take the list over from the builder->priv.
791    *
792    * g_slist_reverse does not copy the list, so the list now
793    * belongs to us (and we free it at the end of this function).
794    */
795   props = g_slist_reverse (builder->priv->delayed_properties);
796   builder->priv->delayed_properties = NULL;
797
798   for (l = props; l; l = l->next)
799     {
800       property = (DelayedProperty*)l->data;
801       object = g_hash_table_lookup (builder->priv->objects, property->object);
802       g_assert (object != NULL);
803
804       object_type = G_OBJECT_TYPE (object);
805       g_assert (object_type != G_TYPE_INVALID);
806
807       oclass = g_type_class_ref (object_type);
808       g_assert (oclass != NULL);
809
810       pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
811                                             property->name);
812       if (!pspec)
813         g_warning ("Unknown property: %s.%s", g_type_name (object_type),
814                    property->name);
815       else
816         {
817           GObject *obj;
818
819           obj = g_hash_table_lookup (builder->priv->objects, property->value);
820           if (!obj)
821             g_warning ("No object called: %s", property->value);
822           else
823             g_object_set (object, property->name, obj, NULL);
824         }
825       g_free (property->value);
826       g_free (property->object);
827       g_free (property->name);
828       g_slice_free (DelayedProperty, property);
829       g_type_class_unref (oclass);
830     }
831   g_slist_free (props);
832 }
833
834 void
835 _gtk_builder_finish (GtkBuilder *builder)
836 {
837   gtk_builder_apply_delayed_properties (builder);
838 }
839
840 /**
841  * gtk_builder_new:
842  *
843  * Creates a new builder object.
844  *
845  * Return value: a new #GtkBuilder object
846  *
847  * Since: 2.12
848  **/
849 GtkBuilder *
850 gtk_builder_new (void)
851 {
852   return g_object_new (GTK_TYPE_BUILDER, NULL);
853 }
854
855 /**
856  * gtk_builder_add_from_file:
857  * @builder: a #GtkBuilder
858  * @filename: the name of the file to parse
859  * @error: (allow-none): return location for an error, or %NULL
860  *
861  * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
862  * UI definition</link> and merges it with the current contents of @builder. 
863  * 
864  * Upon errors 0 will be returned and @error will be assigned a
865  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR 
866  * domain.
867  *
868  * Returns: A positive value on success, 0 if an error occurred
869  *
870  * Since: 2.12
871  **/
872 guint
873 gtk_builder_add_from_file (GtkBuilder   *builder,
874                            const gchar  *filename,
875                            GError      **error)
876 {
877   gchar *buffer;
878   gsize length;
879   GError *tmp_error;
880
881   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
882   g_return_val_if_fail (filename != NULL, 0);
883   g_return_val_if_fail (error == NULL || *error == NULL, 0);
884
885   tmp_error = NULL;
886
887   if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
888     {
889       g_propagate_error (error, tmp_error);
890       return 0;
891     }
892   
893   g_free (builder->priv->filename);
894   builder->priv->filename = g_strdup (filename);
895
896   _gtk_builder_parser_parse_buffer (builder, filename,
897                                     buffer, length,
898                                     NULL,
899                                     &tmp_error);
900
901   g_free (buffer);
902
903   if (tmp_error != NULL)
904     {
905       g_propagate_error (error, tmp_error);
906       return 0;
907     }
908
909   return 1;
910 }
911
912 /**
913  * gtk_builder_add_objects_from_file:
914  * @builder: a #GtkBuilder
915  * @filename: the name of the file to parse
916  * @object_ids: nul-terminated array of objects to build
917  * @error: (allow-none): return location for an error, or %NULL
918  *
919  * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
920  * UI definition</link> building only the requested objects and merges
921  * them with the current contents of @builder. 
922  *
923  * Upon errors 0 will be returned and @error will be assigned a
924  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR 
925  * domain.
926  *
927  * <note><para>
928  * If you are adding an object that depends on an object that is not 
929  * its child (for instance a #GtkTreeView that depends on its
930  * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
931  * </para></note>
932  *
933  * Returns: A positive value on success, 0 if an error occurred
934  *
935  * Since: 2.14
936  **/
937 guint
938 gtk_builder_add_objects_from_file (GtkBuilder   *builder,
939                                    const gchar  *filename,
940                                    gchar       **object_ids,
941                                    GError      **error)
942 {
943   gchar *buffer;
944   gsize length;
945   GError *tmp_error;
946
947   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
948   g_return_val_if_fail (filename != NULL, 0);
949   g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
950   g_return_val_if_fail (error == NULL || *error == NULL, 0);
951
952   tmp_error = NULL;
953
954   if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
955     {
956       g_propagate_error (error, tmp_error);
957       return 0;
958     }
959   
960   g_free (builder->priv->filename);
961   builder->priv->filename = g_strdup (filename);
962
963   _gtk_builder_parser_parse_buffer (builder, filename,
964                                     buffer, length,
965                                     object_ids,
966                                     &tmp_error);
967
968   g_free (buffer);
969
970   if (tmp_error != NULL)
971     {
972       g_propagate_error (error, tmp_error);
973       return 0;
974     }
975
976   return 1;
977 }
978
979 /**
980  * gtk_builder_add_from_string:
981  * @builder: a #GtkBuilder
982  * @buffer: the string to parse
983  * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
984  * @error: (allow-none): return location for an error, or %NULL
985  *
986  * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
987  * UI definition</link> and merges it with the current contents of @builder. 
988  *
989  * Upon errors 0 will be returned and @error will be assigned a
990  * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
991  *
992  * Returns: A positive value on success, 0 if an error occurred
993  *
994  * Since: 2.12
995  **/
996 guint
997 gtk_builder_add_from_string (GtkBuilder   *builder,
998                              const gchar  *buffer,
999                              gsize         length,
1000                              GError      **error)
1001 {
1002   GError *tmp_error;
1003
1004   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1005   g_return_val_if_fail (buffer != NULL, 0);
1006   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1007
1008   tmp_error = NULL;
1009
1010   g_free (builder->priv->filename);
1011   builder->priv->filename = g_strdup (".");
1012
1013   _gtk_builder_parser_parse_buffer (builder, "<input>",
1014                                     buffer, length,
1015                                     NULL,
1016                                     &tmp_error);
1017   if (tmp_error != NULL)
1018     {
1019       g_propagate_error (error, tmp_error);
1020       return 0;
1021     }
1022
1023   return 1;
1024 }
1025
1026 /**
1027  * gtk_builder_add_objects_from_string:
1028  * @builder: a #GtkBuilder
1029  * @buffer: the string to parse
1030  * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1031  * @object_ids: nul-terminated array of objects to build
1032  * @error: (allow-none): return location for an error, or %NULL
1033  *
1034  * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
1035  * UI definition</link> building only the requested objects and merges
1036  * them with the current contents of @builder. 
1037  *
1038  * Upon errors 0 will be returned and @error will be assigned a
1039  * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
1040  * 
1041  * <note><para>
1042  * If you are adding an object that depends on an object that is not 
1043  * its child (for instance a #GtkTreeView that depends on its
1044  * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
1045  * </para></note>
1046  *
1047  * Returns: A positive value on success, 0 if an error occurred
1048  *
1049  * Since: 2.14
1050  **/
1051 guint
1052 gtk_builder_add_objects_from_string (GtkBuilder   *builder,
1053                                      const gchar  *buffer,
1054                                      gsize         length,
1055                                      gchar       **object_ids,
1056                                      GError      **error)
1057 {
1058   GError *tmp_error;
1059
1060   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1061   g_return_val_if_fail (buffer != NULL, 0);
1062   g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
1063   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1064
1065   tmp_error = NULL;
1066
1067   g_free (builder->priv->filename);
1068   builder->priv->filename = g_strdup (".");
1069
1070   _gtk_builder_parser_parse_buffer (builder, "<input>",
1071                                     buffer, length,
1072                                     object_ids,
1073                                     &tmp_error);
1074
1075   if (tmp_error != NULL)
1076     {
1077       g_propagate_error (error, tmp_error);
1078       return 0;
1079     }
1080
1081   return 1;
1082 }
1083
1084 /**
1085  * gtk_builder_get_object:
1086  * @builder: a #GtkBuilder
1087  * @name: name of object to get
1088  *
1089  * Gets the object named @name. Note that this function does not
1090  * increment the reference count of the returned object. 
1091  *
1092  * Return value: (transfer none): the object named @name or %NULL if
1093  *    it could not be found in the object tree.
1094  *
1095  * Since: 2.12
1096  **/
1097 GObject *
1098 gtk_builder_get_object (GtkBuilder  *builder,
1099                         const gchar *name)
1100 {
1101   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1102   g_return_val_if_fail (name != NULL, NULL);
1103
1104   return g_hash_table_lookup (builder->priv->objects, name);
1105 }
1106
1107 static void
1108 object_add_to_list (gchar    *object_id,
1109                     GObject  *object,
1110                     GSList  **list)
1111 {
1112   *list = g_slist_prepend (*list, object);
1113 }
1114
1115 /**
1116  * gtk_builder_get_objects:
1117  * @builder: a #GtkBuilder
1118  *
1119  * Gets all objects that have been constructed by @builder. Note that 
1120  * this function does not increment the reference counts of the returned
1121  * objects.
1122  *
1123  * Return value: (element-type GObject) (transfer container): a newly-allocated #GSList containing all the objects
1124  *   constructed by the #GtkBuilder instance. It should be freed by
1125  *   g_slist_free()
1126  *
1127  * Since: 2.12
1128  **/
1129 GSList *
1130 gtk_builder_get_objects (GtkBuilder *builder)
1131 {
1132   GSList *objects = NULL;
1133
1134   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1135
1136   g_hash_table_foreach (builder->priv->objects, (GHFunc)object_add_to_list, &objects);
1137
1138   return g_slist_reverse (objects);
1139 }
1140
1141 /**
1142  * gtk_builder_set_translation_domain:
1143  * @builder: a #GtkBuilder
1144  * @domain: (allow-none): the translation domain or %NULL
1145  *
1146  * Sets the translation domain of @builder. 
1147  * See #GtkBuilder:translation-domain.
1148  *
1149  * Since: 2.12
1150  **/
1151 void
1152 gtk_builder_set_translation_domain (GtkBuilder  *builder,
1153                                     const gchar *domain)
1154 {
1155   gchar *new_domain;
1156     
1157   g_return_if_fail (GTK_IS_BUILDER (builder));
1158
1159   new_domain = g_strdup (domain);
1160   g_free (builder->priv->domain);
1161   builder->priv->domain = new_domain;
1162
1163   g_object_notify (G_OBJECT (builder), "translation-domain");
1164 }
1165
1166 /**
1167  * gtk_builder_get_translation_domain:
1168  * @builder: a #GtkBuilder
1169  *
1170  * Gets the translation domain of @builder.
1171  *
1172  * Return value: the translation domain. This string is owned
1173  * by the builder object and must not be modified or freed.
1174  *
1175  * Since: 2.12
1176  **/
1177 const gchar *
1178 gtk_builder_get_translation_domain (GtkBuilder *builder)
1179 {
1180   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1181
1182   return builder->priv->domain;
1183 }
1184
1185 typedef struct {
1186   GModule *module;
1187   gpointer data;
1188 } connect_args;
1189
1190 static void
1191 gtk_builder_connect_signals_default (GtkBuilder    *builder,
1192                                      GObject       *object,
1193                                      const gchar   *signal_name,
1194                                      const gchar   *handler_name,
1195                                      GObject       *connect_object,
1196                                      GConnectFlags  flags,
1197                                      gpointer       user_data)
1198 {
1199   GCallback func;
1200   connect_args *args = (connect_args*)user_data;
1201   
1202   if (!g_module_symbol (args->module, handler_name, (gpointer)&func))
1203     {
1204       g_warning ("Could not find signal handler '%s'", handler_name);
1205       return;
1206     }
1207
1208   if (connect_object)
1209     g_signal_connect_object (object, signal_name, func, connect_object, flags);
1210   else
1211     g_signal_connect_data (object, signal_name, func, args->data, NULL, flags);
1212 }
1213
1214
1215 /**
1216  * gtk_builder_connect_signals:
1217  * @builder: a #GtkBuilder
1218  * @user_data: a pointer to a structure sent in as user data to all signals
1219  *
1220  * This method is a simpler variation of gtk_builder_connect_signals_full().
1221  * It uses #GModule's introspective features (by opening the module %NULL) 
1222  * to look at the application's symbol table. From here it tries to match
1223  * the signal handler names given in the interface description with
1224  * symbols in the application and connects the signals.
1225  * 
1226  * Note that this function will not work correctly if #GModule is not
1227  * supported on the platform.
1228  *
1229  * When compiling applications for Windows, you must declare signal callbacks
1230  * with #G_MODULE_EXPORT, or they will not be put in the symbol table.
1231  * On Linux and Unices, this is not necessary; applications should instead
1232  * be compiled with the -Wl,--export-dynamic CFLAGS, and linked against
1233  * gmodule-export-2.0.
1234  *
1235  * Since: 2.12
1236  **/
1237 void
1238 gtk_builder_connect_signals (GtkBuilder *builder,
1239                              gpointer    user_data)
1240 {
1241   connect_args *args;
1242   
1243   g_return_if_fail (GTK_IS_BUILDER (builder));
1244   
1245   if (!g_module_supported ())
1246     g_error ("gtk_builder_connect_signals() requires working GModule");
1247
1248   args = g_slice_new0 (connect_args);
1249   args->module = g_module_open (NULL, G_MODULE_BIND_LAZY);
1250   args->data = user_data;
1251   
1252   gtk_builder_connect_signals_full (builder,
1253                                     gtk_builder_connect_signals_default,
1254                                     args);
1255   g_module_close (args->module);
1256
1257   g_slice_free (connect_args, args);
1258 }
1259
1260 /**
1261  * GtkBuilderConnectFunc:
1262  * @builder: a #GtkBuilder
1263  * @object: object to connect a signal to
1264  * @signal_name: name of the signal
1265  * @handler_name: name of the handler
1266  * @connect_object: a #GObject, if non-%NULL, use g_signal_connect_object()
1267  * @flags: #GConnectFlags to use
1268  * @user_data: user data
1269  *
1270  * This is the signature of a function used to connect signals.  It is used
1271  * by the gtk_builder_connect_signals() and gtk_builder_connect_signals_full()
1272  * methods.  It is mainly intended for interpreted language bindings, but
1273  * could be useful where the programmer wants more control over the signal
1274  * connection process.
1275  *
1276  * Since: 2.12
1277  */
1278
1279 /**
1280  * gtk_builder_connect_signals_full:
1281  * @builder: a #GtkBuilder
1282  * @func: the function used to connect the signals
1283  * @user_data: arbitrary data that will be passed to the connection function
1284  *
1285  * This function can be thought of the interpreted language binding
1286  * version of gtk_builder_connect_signals(), except that it does not
1287  * require GModule to function correctly.
1288  *
1289  * Since: 2.12
1290  */
1291 void
1292 gtk_builder_connect_signals_full (GtkBuilder            *builder,
1293                                   GtkBuilderConnectFunc  func,
1294                                   gpointer               user_data)
1295 {
1296   GSList *l;
1297   GObject *object;
1298   GObject *connect_object;
1299   
1300   g_return_if_fail (GTK_IS_BUILDER (builder));
1301   g_return_if_fail (func != NULL);
1302   
1303   if (!builder->priv->signals)
1304     return;
1305
1306   builder->priv->signals = g_slist_reverse (builder->priv->signals);
1307   for (l = builder->priv->signals; l; l = l->next)
1308     {
1309       SignalInfo *signal = (SignalInfo*)l->data;
1310
1311       g_assert (signal != NULL);
1312       g_assert (signal->name != NULL);
1313
1314       object = g_hash_table_lookup (builder->priv->objects,
1315                                     signal->object_name);
1316       g_assert (object != NULL);
1317
1318       connect_object = NULL;
1319       
1320       if (signal->connect_object_name)
1321         {
1322           connect_object = g_hash_table_lookup (builder->priv->objects,
1323                                                 signal->connect_object_name);
1324           if (!connect_object)
1325               g_warning ("Could not lookup object %s on signal %s of object %s",
1326                          signal->connect_object_name, signal->name,
1327                          signal->object_name);
1328         }
1329                                                   
1330       func (builder, object, signal->name, signal->handler, 
1331             connect_object, signal->flags, user_data);
1332     }
1333
1334   g_slist_foreach (builder->priv->signals, (GFunc)_free_signal_info, NULL);
1335   g_slist_free (builder->priv->signals);
1336   builder->priv->signals = NULL;
1337 }
1338
1339 /**
1340  * gtk_builder_value_from_string:
1341  * @builder: a #GtkBuilder
1342  * @pspec: the #GParamSpec for the property
1343  * @string: the string representation of the value
1344  * @value: the #GValue to store the result in
1345  * @error: (allow-none): return location for an error, or %NULL
1346  *
1347  * This function demarshals a value from a string. This function
1348  * calls g_value_init() on the @value argument, so it need not be
1349  * initialised beforehand.
1350  *
1351  * This function can handle char, uchar, boolean, int, uint, long,
1352  * ulong, enum, flags, float, double, string, #GdkColor and
1353  * #GtkAdjustment type values. Support for #GtkWidget type values is
1354  * still to come.
1355  *
1356  * Upon errors %FALSE will be returned and @error will be assigned a
1357  * #GError from the #GTK_BUILDER_ERROR domain.
1358  *
1359  * Returns: %TRUE on success
1360  *
1361  * Since: 2.12
1362  */
1363 gboolean
1364 gtk_builder_value_from_string (GtkBuilder   *builder,
1365                                GParamSpec   *pspec,
1366                                const gchar  *string,
1367                                GValue       *value,
1368                                GError      **error)
1369 {
1370   g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
1371   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1372   g_return_val_if_fail (string != NULL, FALSE);
1373   g_return_val_if_fail (value != NULL, FALSE);
1374   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1375
1376   /*
1377    * GParamSpecUnichar has the internal type G_TYPE_UINT,
1378    * so we cannot handle this in the switch, do it separately
1379    */
1380   if (G_IS_PARAM_SPEC_UNICHAR (pspec))
1381     {
1382       gunichar c;
1383       g_value_init (value, G_TYPE_UINT);
1384       c = g_utf8_get_char_validated (string, strlen (string));
1385       if (c > 0)
1386         g_value_set_uint (value, c);
1387       return TRUE;
1388     }
1389
1390   return gtk_builder_value_from_string_type (builder,
1391                                              G_PARAM_SPEC_VALUE_TYPE (pspec),
1392                                              string, value, error);
1393 }
1394
1395 /**
1396  * gtk_builder_value_from_string_type:
1397  * @builder: a #GtkBuilder
1398  * @type: the #GType of the value
1399  * @string: the string representation of the value
1400  * @value: the #GValue to store the result in
1401  * @error: (allow-none): return location for an error, or %NULL
1402  *
1403  * Like gtk_builder_value_from_string(), this function demarshals 
1404  * a value from a string, but takes a #GType instead of #GParamSpec.
1405  * This function calls g_value_init() on the @value argument, so it 
1406  * need not be initialised beforehand.
1407  *
1408  * Upon errors %FALSE will be returned and @error will be assigned a
1409  * #GError from the #GTK_BUILDER_ERROR domain.
1410  *
1411  * Returns: %TRUE on success
1412  *
1413  * Since: 2.12
1414  */
1415 gboolean
1416 gtk_builder_value_from_string_type (GtkBuilder   *builder,
1417                                     GType         type,
1418                                     const gchar  *string,
1419                                     GValue       *value,
1420                                     GError      **error)
1421 {
1422   gboolean ret = TRUE;
1423
1424   g_return_val_if_fail (type != G_TYPE_INVALID, FALSE);
1425   g_return_val_if_fail (string != NULL, FALSE);
1426   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1427
1428   g_value_init (value, type);
1429
1430   switch (G_TYPE_FUNDAMENTAL (type))
1431     {
1432     case G_TYPE_CHAR:
1433       g_value_set_char (value, string[0]);
1434       break;
1435     case G_TYPE_UCHAR:
1436       g_value_set_uchar (value, (guchar)string[0]);
1437       break;
1438     case G_TYPE_BOOLEAN:
1439       {
1440         gboolean b;
1441
1442         if (!_gtk_builder_boolean_from_string (string, &b, error))
1443           {
1444             ret = FALSE;
1445             break;
1446           }
1447         g_value_set_boolean (value, b);
1448         break;
1449       }
1450     case G_TYPE_INT:
1451     case G_TYPE_LONG:
1452       {
1453         long l;
1454         gchar *endptr;
1455         errno = 0;
1456         l = strtol (string, &endptr, 0);
1457         if (errno || endptr == string)
1458           {
1459             g_set_error (error,
1460                          GTK_BUILDER_ERROR,
1461                          GTK_BUILDER_ERROR_INVALID_VALUE,
1462                          "Could not parse integer `%s'",
1463                          string);
1464             ret = FALSE;
1465             break;
1466           }
1467         if (G_VALUE_HOLDS_INT (value))
1468           g_value_set_int (value, l);
1469         else
1470           g_value_set_long (value, l);
1471         break;
1472       }
1473     case G_TYPE_UINT:
1474     case G_TYPE_ULONG:
1475       {
1476         gulong ul;
1477         gchar *endptr;
1478         errno = 0;
1479         ul = strtoul (string, &endptr, 0);
1480         if (errno || endptr == string)
1481           {
1482             g_set_error (error,
1483                          GTK_BUILDER_ERROR,
1484                          GTK_BUILDER_ERROR_INVALID_VALUE,
1485                          "Could not parse unsigned integer `%s'",
1486                          string);
1487             ret = FALSE;
1488             break;
1489           }
1490         if (G_VALUE_HOLDS_UINT (value))
1491           g_value_set_uint (value, ul);
1492         else 
1493           g_value_set_ulong (value, ul);
1494         break;
1495       }
1496     case G_TYPE_ENUM:
1497       {
1498         gint enum_value;
1499         if (!_gtk_builder_enum_from_string (type, string, &enum_value, error))
1500           {
1501             ret = FALSE;
1502             break;
1503           }
1504         g_value_set_enum (value, enum_value);
1505         break;
1506       }
1507     case G_TYPE_FLAGS:
1508       {
1509         guint flags_value;
1510
1511         if (!_gtk_builder_flags_from_string (type, string, &flags_value, error))
1512           {
1513             ret = FALSE;
1514             break;
1515           }
1516         g_value_set_flags (value, flags_value);
1517         break;
1518       }
1519     case G_TYPE_FLOAT:
1520     case G_TYPE_DOUBLE:
1521       {
1522         gdouble d;
1523         gchar *endptr;
1524         errno = 0;
1525         d = g_ascii_strtod (string, &endptr);
1526         if (errno || endptr == string)
1527           {
1528             g_set_error (error,
1529                          GTK_BUILDER_ERROR,
1530                          GTK_BUILDER_ERROR_INVALID_VALUE,
1531                          "Could not parse double `%s'",
1532                          string);
1533             ret = FALSE;
1534             break;
1535           }
1536         if (G_VALUE_HOLDS_FLOAT (value))
1537           g_value_set_float (value, d);
1538         else
1539           g_value_set_double (value, d);
1540         break;
1541       }
1542     case G_TYPE_STRING:
1543       g_value_set_string (value, string);
1544       break;
1545     case G_TYPE_BOXED:
1546       if (G_VALUE_HOLDS (value, GDK_TYPE_COLOR))
1547         {
1548           GdkColor colour = { 0, };
1549
1550           if (gdk_color_parse (string, &colour) &&
1551               gdk_colormap_alloc_color (gtk_widget_get_default_colormap (),
1552                                         &colour, FALSE, TRUE))
1553             g_value_set_boxed (value, &colour);
1554           else
1555             {
1556               g_set_error (error,
1557                            GTK_BUILDER_ERROR,
1558                            GTK_BUILDER_ERROR_INVALID_VALUE,
1559                            "Could not parse color `%s'",
1560                            string);
1561               ret = FALSE;
1562             }
1563         }
1564       else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
1565         {
1566           gchar **vector = g_strsplit (string, "\n", 0);
1567           g_value_take_boxed (value, vector);
1568         }
1569       else
1570         {
1571           g_set_error (error,
1572                        GTK_BUILDER_ERROR,
1573                        GTK_BUILDER_ERROR_INVALID_VALUE,
1574                        "Could not parse '%s' as a %s",
1575                        string, G_VALUE_TYPE_NAME (value));
1576           ret = FALSE;
1577         }
1578       break;
1579     case G_TYPE_OBJECT:
1580       if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
1581         {
1582           gchar *filename;
1583           GError *tmp_error = NULL;
1584           GdkPixbuf *pixbuf;
1585        
1586           if (gtk_builder_get_object (builder, string))
1587             {
1588               g_set_error (error,
1589                            GTK_BUILDER_ERROR,
1590                            GTK_BUILDER_ERROR_INVALID_VALUE,
1591                            "Could not load image '%s': "
1592                            " '%s' is already used as object id",
1593                            string, string);
1594               return FALSE;
1595             }
1596
1597           filename = _gtk_builder_get_absolute_filename (builder, string);
1598           pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);
1599
1600           if (pixbuf == NULL)
1601             {
1602               GtkIconTheme *theme;
1603
1604               g_warning ("Could not load image '%s': %s", 
1605                          string, tmp_error->message);
1606               g_error_free (tmp_error);
1607
1608               /* fall back to a missing image */
1609               theme = gtk_icon_theme_get_default ();
1610               pixbuf = gtk_icon_theme_load_icon (theme, 
1611                                                  GTK_STOCK_MISSING_IMAGE,
1612                                                  16,
1613                                                  GTK_ICON_LOOKUP_USE_BUILTIN,
1614                                                  NULL);
1615             }
1616  
1617           if (pixbuf)
1618             {
1619               g_value_set_object (value, pixbuf);
1620               g_object_unref (G_OBJECT (pixbuf));
1621             }
1622
1623           g_free (filename);
1624
1625           ret = TRUE;
1626         }
1627       else
1628         ret = FALSE;
1629       break;
1630     default:
1631       ret = FALSE;
1632       break;
1633     }
1634  
1635   /* Catch unassigned error for object types as well as any unsupported types.
1636    * While parsing GtkBuilder; object types are deserialized
1637    * without calling gtk_builder_value_from_string_type().
1638    */
1639   if (!ret && error && *error == NULL) 
1640     g_set_error (error,
1641                  GTK_BUILDER_ERROR,
1642                  GTK_BUILDER_ERROR_INVALID_VALUE,
1643                  "Unsupported GType `%s'", g_type_name (type));
1644
1645   return ret;
1646 }
1647
1648 gboolean
1649 _gtk_builder_enum_from_string (GType         type, 
1650                                const gchar  *string,
1651                                gint         *enum_value,
1652                                GError      **error)
1653 {
1654   GEnumClass *eclass;
1655   GEnumValue *ev;
1656   gchar *endptr;
1657   gint value;
1658   gboolean ret;
1659   
1660   g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
1661   g_return_val_if_fail (string != NULL, FALSE);
1662   
1663   ret = TRUE;
1664
1665   value = strtoul (string, &endptr, 0);
1666   if (endptr != string) /* parsed a number */
1667     *enum_value = value;
1668   else
1669     {
1670       eclass = g_type_class_ref (type);
1671       ev = g_enum_get_value_by_name (eclass, string);
1672       if (!ev)
1673         ev = g_enum_get_value_by_nick (eclass, string);
1674
1675       if (ev)
1676         *enum_value = ev->value;
1677       else
1678         {
1679           g_set_error (error,
1680                        GTK_BUILDER_ERROR,
1681                        GTK_BUILDER_ERROR_INVALID_VALUE,
1682                        "Could not parse enum: `%s'",
1683                        string);
1684           ret = FALSE;
1685         }
1686       
1687       g_type_class_unref (eclass);
1688     }
1689   
1690   return ret;
1691 }
1692
1693 gboolean
1694 _gtk_builder_flags_from_string (GType         type, 
1695                                 const gchar  *string,
1696                                 guint        *flags_value,
1697                                 GError      **error)
1698 {
1699   GFlagsClass *fclass;
1700   gchar *endptr, *prevptr;
1701   guint i, j, value;
1702   gchar *flagstr;
1703   GFlagsValue *fv;
1704   const gchar *flag;
1705   gunichar ch;
1706   gboolean eos, ret;
1707
1708   g_return_val_if_fail (G_TYPE_IS_FLAGS (type), FALSE);
1709   g_return_val_if_fail (string != 0, FALSE);
1710
1711   ret = TRUE;
1712   
1713   value = strtoul (string, &endptr, 0);
1714   if (endptr != string) /* parsed a number */
1715     *flags_value = value;
1716   else
1717     {
1718       fclass = g_type_class_ref (type);
1719
1720       flagstr = g_strdup (string);
1721       for (value = i = j = 0; ; i++)
1722         {
1723           
1724           eos = flagstr[i] == '\0';
1725           
1726           if (!eos && flagstr[i] != '|')
1727             continue;
1728           
1729           flag = &flagstr[j];
1730           endptr = &flagstr[i];
1731           
1732           if (!eos)
1733             {
1734               flagstr[i++] = '\0';
1735               j = i;
1736             }
1737           
1738           /* trim spaces */
1739           for (;;)
1740             {
1741               ch = g_utf8_get_char (flag);
1742               if (!g_unichar_isspace (ch))
1743                 break;
1744               flag = g_utf8_next_char (flag);
1745             }
1746           
1747           while (endptr > flag)
1748             {
1749               prevptr = g_utf8_prev_char (endptr);
1750               ch = g_utf8_get_char (prevptr);
1751               if (!g_unichar_isspace (ch))
1752                 break;
1753               endptr = prevptr;
1754             }
1755           
1756           if (endptr > flag)
1757             {
1758               *endptr = '\0';
1759               fv = g_flags_get_value_by_name (fclass, flag);
1760               
1761               if (!fv)
1762                 fv = g_flags_get_value_by_nick (fclass, flag);
1763               
1764               if (fv)
1765                 value |= fv->value;
1766               else
1767                 {
1768                   g_set_error (error,
1769                                GTK_BUILDER_ERROR,
1770                                GTK_BUILDER_ERROR_INVALID_VALUE,
1771                                "Unknown flag: `%s'",
1772                                flag);
1773                   ret = FALSE;
1774                   break;
1775                 }
1776             }
1777           
1778           if (eos)
1779             {
1780               *flags_value = value;
1781               break;
1782             }
1783         }
1784       
1785       g_free (flagstr);
1786       
1787       g_type_class_unref (fclass);
1788     }
1789
1790   return ret;
1791 }
1792
1793 /**
1794  * gtk_builder_get_type_from_name:
1795  * @builder: a #GtkBuilder
1796  * @type_name: type name to lookup
1797  *
1798  * Looks up a type by name, using the virtual function that 
1799  * #GtkBuilder has for that purpose. This is mainly used when
1800  * implementing the #GtkBuildable interface on a type.
1801  *
1802  * Returns: the #GType found for @type_name or #G_TYPE_INVALID 
1803  *   if no type was found
1804  *
1805  * Since: 2.12
1806  */
1807 GType
1808 gtk_builder_get_type_from_name (GtkBuilder  *builder, 
1809                                 const gchar *type_name)
1810 {
1811   g_return_val_if_fail (GTK_IS_BUILDER (builder), G_TYPE_INVALID);
1812   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1813
1814   return GTK_BUILDER_GET_CLASS (builder)->get_type_from_name (builder, type_name);
1815 }
1816
1817 GQuark
1818 gtk_builder_error_quark (void)
1819 {
1820   return g_quark_from_static_string ("gtk-builder-error-quark");
1821 }
1822
1823 gchar *
1824 _gtk_builder_get_absolute_filename (GtkBuilder *builder, const gchar *string)
1825 {
1826   gchar *filename;
1827   gchar *dirname = NULL;
1828   
1829   if (g_path_is_absolute (string))
1830     return g_strdup (string);
1831
1832   if (builder->priv->filename &&
1833       strcmp (builder->priv->filename, ".") != 0) 
1834     {
1835       dirname = g_path_get_dirname (builder->priv->filename);
1836
1837       if (strcmp (dirname, ".") == 0)
1838         {
1839           g_free (dirname);
1840           dirname = g_get_current_dir ();
1841         }
1842     }
1843   else
1844     dirname = g_get_current_dir ();
1845     
1846   filename = g_build_filename (dirname, string, NULL);
1847   g_free (dirname);
1848   
1849   return filename;
1850 }