]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilder.c
testgtk: Use symbolic names for button numbers
[~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="GtkExpander-BUILDER-UI">GtkExpander</link>,
225  * <link linkend="GtkFrame-BUILDER-UI">GtkFrame</link>,
226  * <link linkend="GtkListStore-BUILDER-UI">GtkListStore</link>,
227  * <link linkend="GtkTreeStore-BUILDER-UI">GtkTreeStore</link>,
228  * <link linkend="GtkNotebook-BUILDER-UI">GtkNotebook</link>,
229  * <link linkend="GtkSizeGroup-BUILDER-UI">GtkSizeGroup</link>,
230  * <link linkend="GtkTreeView-BUILDER-UI">GtkTreeView</link>,
231  * <link linkend="GtkUIManager-BUILDER-UI">GtkUIManager</link>,
232  * <link linkend="GtkActionGroup-BUILDER-UI">GtkActionGroup</link>.
233  * <link linkend="GtkMenuItem-BUILDER-UI">GtkMenuItem</link>,
234  * <link linkend="GtkMenuToolButton-BUILDER-UI">GtkMenuToolButton</link>,
235  * <link linkend="GtkAssistant-BUILDER-UI">GtkAssistant</link>,
236  * <link linkend="GtkScale-BUILDER-UI">GtkScale</link>,
237  * <link linkend="GtkComboBoxText-BUILDER-UI">GtkComboBoxText</link>,
238  * <link linkend="GtkRecentFilter-BUILDER-UI">GtkRecentFilter</link>,
239  * <link linkend="GtkFileFilter-BUILDER-UI">GtkFileFilter</link>,
240  * <link linkend="GtkTextTagTable-BUILDER-UI">GtkTextTagTable</link>.
241  * </para>
242  * </refsect2>
243  * <refsect2>
244  * <title>Embedding other XML</title>
245  * <para>
246  * Apart from the language for UI descriptions that has been explained
247  * in the previous section, GtkBuilder can also parse XML fragments
248  * of <link linkend="gio-GMenu-Markup">GMenu markup</link>. The resulting
249  * #GMenu object and its named submenus are available via
250  * gtk_builder_get_object() like other constructed objects.
251  * </para>
252  * </refsect2>
253  */
254
255 #include "config.h"
256 #include <errno.h> /* errno */
257 #include <stdlib.h>
258 #include <string.h> /* strlen */
259
260 #include "gtkbuilder.h"
261 #include "gtkbuildable.h"
262 #include "gtkbuilderprivate.h"
263 #include "gtkdebug.h"
264 #include "gtkmain.h"
265 #include "gtkintl.h"
266 #include "gtkprivate.h"
267 #include "gtktypebuiltins.h"
268 #include "gtkwindow.h"
269 #include "gtkicontheme.h"
270 #include "gtkstock.h"
271
272
273 static void gtk_builder_class_init     (GtkBuilderClass *klass);
274 static void gtk_builder_init           (GtkBuilder      *builder);
275 static void gtk_builder_finalize       (GObject         *object);
276 static void gtk_builder_set_property   (GObject         *object,
277                                         guint            prop_id,
278                                         const GValue    *value,
279                                         GParamSpec      *pspec);
280 static void gtk_builder_get_property   (GObject         *object,
281                                         guint            prop_id,
282                                         GValue          *value,
283                                         GParamSpec      *pspec);
284 static GType gtk_builder_real_get_type_from_name (GtkBuilder  *builder,
285                                                   const gchar *type_name);
286
287 enum {
288   PROP_0,
289   PROP_TRANSLATION_DOMAIN,
290 };
291
292 struct _GtkBuilderPrivate
293 {
294   gchar *domain;
295   GHashTable *objects;
296   GSList *delayed_properties;
297   GSList *signals;
298   gchar *filename;
299   gchar *resource_prefix;
300 };
301
302 G_DEFINE_TYPE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)
303
304 static void
305 gtk_builder_class_init (GtkBuilderClass *klass)
306 {
307   GObjectClass *gobject_class;
308
309   gobject_class = G_OBJECT_CLASS (klass);
310
311   gobject_class->finalize = gtk_builder_finalize;
312   gobject_class->set_property = gtk_builder_set_property;
313   gobject_class->get_property = gtk_builder_get_property;
314
315   klass->get_type_from_name = gtk_builder_real_get_type_from_name;
316
317  /** 
318   * GtkBuilder:translation-domain:
319   *
320   * The translation domain used when translating property values that
321   * have been marked as translatable in interface descriptions.
322   * If the translation domain is %NULL, #GtkBuilder uses gettext(),
323   * otherwise g_dgettext().
324   *
325   * Since: 2.12
326   */
327   g_object_class_install_property (gobject_class,
328                                    PROP_TRANSLATION_DOMAIN,
329                                    g_param_spec_string ("translation-domain",
330                                                         P_("Translation Domain"),
331                                                         P_("The translation domain used by gettext"),
332                                                         NULL,
333                                                         GTK_PARAM_READWRITE));
334
335   g_type_class_add_private (gobject_class, sizeof (GtkBuilderPrivate));
336 }
337
338 static void
339 gtk_builder_init (GtkBuilder *builder)
340 {
341   builder->priv = G_TYPE_INSTANCE_GET_PRIVATE (builder, GTK_TYPE_BUILDER,
342                                                GtkBuilderPrivate);
343   builder->priv->domain = NULL;
344   builder->priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal,
345                                                   g_free, g_object_unref);
346 }
347
348
349 /*
350  * GObject virtual methods
351  */
352
353 static void
354 gtk_builder_finalize (GObject *object)
355 {
356   GtkBuilderPrivate *priv = GTK_BUILDER (object)->priv;
357   
358   g_free (priv->domain);
359   g_free (priv->filename);
360   g_free (priv->resource_prefix);
361   
362   g_hash_table_destroy (priv->objects);
363
364   g_slist_foreach (priv->signals, (GFunc) _free_signal_info, NULL);
365   g_slist_free (priv->signals);
366   
367   G_OBJECT_CLASS (gtk_builder_parent_class)->finalize (object);
368 }
369
370 static void
371 gtk_builder_set_property (GObject      *object,
372                           guint         prop_id,
373                           const GValue *value,
374                           GParamSpec   *pspec)
375 {
376   GtkBuilder *builder = GTK_BUILDER (object);
377
378   switch (prop_id)
379     {
380     case PROP_TRANSLATION_DOMAIN:
381       gtk_builder_set_translation_domain (builder, g_value_get_string (value));
382       break;
383     default:
384       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385       break;
386     }
387 }
388
389 static void
390 gtk_builder_get_property (GObject    *object,
391                           guint       prop_id,
392                           GValue     *value,
393                           GParamSpec *pspec)
394 {
395   GtkBuilder *builder = GTK_BUILDER (object);
396
397   switch (prop_id)
398     {
399     case PROP_TRANSLATION_DOMAIN:
400       g_value_set_string (value, builder->priv->domain);
401       break;
402     default:
403       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404       break;
405     }
406 }
407
408
409 /*
410  * Try to map a type name to a _get_type function
411  * and call it, eg:
412  *
413  * GtkWindow -> gtk_window_get_type
414  * GtkHBox -> gtk_hbox_get_type
415  * GtkUIManager -> gtk_ui_manager_get_type
416  *
417  */
418 static GType
419 _gtk_builder_resolve_type_lazily (const gchar *name)
420 {
421   static GModule *module = NULL;
422   GTypeGetFunc func;
423   GString *symbol_name = g_string_new ("");
424   char c, *symbol;
425   int i;
426   GType gtype = G_TYPE_INVALID;
427
428   if (!module)
429     module = g_module_open (NULL, 0);
430   
431   for (i = 0; name[i] != '\0'; i++)
432     {
433       c = name[i];
434       /* skip if uppercase, first or previous is uppercase */
435       if ((c == g_ascii_toupper (c) &&
436            i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
437           (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
438            name[i-1] == g_ascii_toupper (name[i-1]) &&
439            name[i-2] == g_ascii_toupper (name[i-2])))
440         g_string_append_c (symbol_name, '_');
441       g_string_append_c (symbol_name, g_ascii_tolower (c));
442     }
443   g_string_append (symbol_name, "_get_type");
444   
445   symbol = g_string_free (symbol_name, FALSE);
446
447   if (g_module_symbol (module, symbol, (gpointer)&func))
448     gtype = func ();
449   
450   g_free (symbol);
451
452   return gtype;
453 }
454
455 /*
456  * GtkBuilder virtual methods
457  */
458
459 static GType
460 gtk_builder_real_get_type_from_name (GtkBuilder  *builder, 
461                                      const gchar *type_name)
462 {
463   GType gtype;
464
465   gtype = g_type_from_name (type_name);
466   if (gtype != G_TYPE_INVALID)
467     return gtype;
468
469   return _gtk_builder_resolve_type_lazily (type_name);
470 }
471
472 typedef struct
473 {
474   gchar *object;
475   gchar *name;
476   gchar *value;
477 } DelayedProperty;
478
479 static void
480 gtk_builder_get_parameters (GtkBuilder  *builder,
481                             GType        object_type,
482                             const gchar *object_name,
483                             GSList      *properties,
484                             GArray      **parameters,
485                             GArray      **construct_parameters)
486 {
487   GSList *l;
488   GParamSpec *pspec;
489   GObjectClass *oclass;
490   DelayedProperty *property;
491   GError *error = NULL;
492   
493   oclass = g_type_class_ref (object_type);
494   g_assert (oclass != NULL);
495
496   *parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
497   *construct_parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
498
499   for (l = properties; l; l = l->next)
500     {
501       PropertyInfo *prop = (PropertyInfo*)l->data;
502       GParameter parameter = { NULL };
503
504       pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
505                                             prop->name);
506       if (!pspec)
507         {
508           g_warning ("Unknown property: %s.%s",
509                      g_type_name (object_type), prop->name);
510           continue;
511         }
512
513       parameter.name = prop->name;
514
515       if (G_IS_PARAM_SPEC_OBJECT (pspec) &&
516           (G_PARAM_SPEC_VALUE_TYPE (pspec) != GDK_TYPE_PIXBUF))
517         {
518           GObject *object = gtk_builder_get_object (builder, prop->data);
519
520           if (object)
521             {
522               g_value_init (&parameter.value, G_OBJECT_TYPE (object));
523               g_value_set_object (&parameter.value, object);
524             }
525           else 
526             {
527               if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
528                 {
529                   g_warning ("Failed to get constuct only property "
530                              "%s of %s with value `%s'",
531                              prop->name, object_name, prop->data);
532                   continue;
533                 }
534               /* Delay setting property */
535               property = g_slice_new (DelayedProperty);
536               property->object = g_strdup (object_name);
537               property->name = g_strdup (prop->name);
538               property->value = g_strdup (prop->data);
539               builder->priv->delayed_properties =
540                 g_slist_prepend (builder->priv->delayed_properties, property);
541               continue;
542             }
543         }
544       else if (!gtk_builder_value_from_string (builder, pspec,
545                                                prop->data, &parameter.value, &error))
546         {
547           g_warning ("Failed to set property %s.%s to %s: %s",
548                      g_type_name (object_type), prop->name, prop->data,
549                      error->message);
550           g_error_free (error);
551           error = NULL;
552           continue;
553         }
554
555       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
556         g_array_append_val (*construct_parameters, parameter);
557       else
558         g_array_append_val (*parameters, parameter);
559     }
560
561   g_type_class_unref (oclass);
562 }
563
564 static GObject *
565 gtk_builder_get_internal_child (GtkBuilder  *builder,
566                                 ObjectInfo  *info,
567                                 const gchar *childname,
568                                 GError      **error)
569 {
570   GObject *obj = NULL;
571
572   while (!obj)
573     {
574       if (!info->parent)
575         break;
576
577       info = (ObjectInfo*)((ChildInfo*)info->parent)->parent;
578       if (!info)
579         break;
580
581       GTK_NOTE (BUILDER,
582                 g_print ("Trying to get internal child %s from %s\n",
583                          childname,
584                          gtk_buildable_get_name (GTK_BUILDABLE (info->object))));
585
586       if (GTK_IS_BUILDABLE (info->object))
587           obj = gtk_buildable_get_internal_child (GTK_BUILDABLE (info->object),
588                                                   builder,
589                                                   childname);
590     };
591
592   if (!obj)
593     {
594       g_set_error (error,
595                    GTK_BUILDER_ERROR,
596                    GTK_BUILDER_ERROR_INVALID_VALUE,
597                    "Unknown internal child: %s", childname);
598     }
599   return obj;
600 }
601
602 GObject *
603 _gtk_builder_construct (GtkBuilder *builder,
604                         ObjectInfo *info,
605                         GError **error)
606 {
607   GArray *parameters, *construct_parameters;
608   GType object_type;
609   GObject *obj;
610   int i;
611   GtkBuildableIface *iface;
612   gboolean custom_set_property;
613   GtkBuildable *buildable;
614
615   g_assert (info->class_name != NULL);
616   object_type = gtk_builder_get_type_from_name (builder, info->class_name);
617   if (object_type == G_TYPE_INVALID)
618     {
619       g_set_error (error,
620                    GTK_BUILDER_ERROR,
621                    GTK_BUILDER_ERROR_INVALID_VALUE,
622                    "Invalid object type `%s'",
623                    info->class_name);
624       return NULL;
625     }
626
627   gtk_builder_get_parameters (builder, object_type,
628                               info->id,
629                               info->properties,
630                               &parameters,
631                               &construct_parameters);
632
633   if (info->constructor)
634     {
635       GObject *constructor;
636
637       constructor = gtk_builder_get_object (builder, info->constructor);
638       if (constructor == NULL)
639         {
640           g_set_error (error,
641                        GTK_BUILDER_ERROR,
642                        GTK_BUILDER_ERROR_INVALID_VALUE,
643                        "Unknown object constructor for %s: %s",
644                        info->id,
645                        info->constructor);
646           g_array_free (parameters, TRUE);
647           g_array_free (construct_parameters, TRUE);
648           return NULL;
649         }
650       obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
651                                            builder,
652                                            info->id);
653       g_assert (obj != NULL);
654       if (construct_parameters->len)
655         g_warning ("Can't pass in construct-only parameters to %s", info->id);
656     }
657   else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
658     {
659       gchar *childname = ((ChildInfo*)info->parent)->internal_child;
660       obj = gtk_builder_get_internal_child (builder, info, childname, error);
661       if (!obj)
662         {
663           g_array_free (parameters, TRUE);
664           g_array_free (construct_parameters, TRUE);
665           return NULL;
666         }
667       if (construct_parameters->len)
668         g_warning ("Can't pass in construct-only parameters to %s", childname);
669       g_object_ref (obj);
670     }
671   else
672     {
673       obj = g_object_newv (object_type,
674                            construct_parameters->len,
675                            (GParameter *)construct_parameters->data);
676
677       /* No matter what, make sure we have a reference.
678        *
679        * If it's an initially unowned object, sink it.
680        * If it's not initially unowned then we have the reference already.
681        *
682        * In the case that this is a window it will be sunk already and
683        * this is effectively a call to g_object_ref().  That's what
684        * we want.
685        */
686       if (G_IS_INITIALLY_UNOWNED (obj))
687         g_object_ref_sink (obj);
688
689       GTK_NOTE (BUILDER,
690                 g_print ("created %s of type %s\n", info->id, info->class_name));
691
692       for (i = 0; i < construct_parameters->len; i++)
693         {
694           GParameter *param = &g_array_index (construct_parameters,
695                                               GParameter, i);
696           g_value_unset (&param->value);
697         }
698     }
699   g_array_free (construct_parameters, TRUE);
700
701   custom_set_property = FALSE;
702   buildable = NULL;
703   iface = NULL;
704   if (GTK_IS_BUILDABLE (obj))
705     {
706       buildable = GTK_BUILDABLE (obj);
707       iface = GTK_BUILDABLE_GET_IFACE (obj);
708       if (iface->set_buildable_property)
709         custom_set_property = TRUE;
710     }
711
712   for (i = 0; i < parameters->len; i++)
713     {
714       GParameter *param = &g_array_index (parameters, GParameter, i);
715       if (custom_set_property)
716         iface->set_buildable_property (buildable, builder, param->name, &param->value);
717       else
718         g_object_set_property (obj, param->name, &param->value);
719
720 #if G_ENABLE_DEBUG
721       if (gtk_get_debug_flags () & GTK_DEBUG_BUILDER)
722         {
723           gchar *str = g_strdup_value_contents ((const GValue*)&param->value);
724           g_print ("set %s: %s = %s\n", info->id, param->name, str);
725           g_free (str);
726         }
727 #endif      
728       g_value_unset (&param->value);
729     }
730   g_array_free (parameters, TRUE);
731   
732   if (GTK_IS_BUILDABLE (obj))
733     gtk_buildable_set_name (buildable, info->id);
734   else
735     g_object_set_data_full (obj,
736                             "gtk-builder-name",
737                             g_strdup (info->id),
738                             g_free);
739
740   /* we already own a reference to obj.  put it in the hash table. */
741   g_hash_table_insert (builder->priv->objects, g_strdup (info->id), obj);
742   
743   return obj;
744 }
745
746 void
747 _gtk_builder_add_object (GtkBuilder  *builder,
748                          const gchar *id,
749                          GObject     *object)
750 {
751   g_hash_table_insert (builder->priv->objects, g_strdup (id), g_object_ref (object));
752 }
753
754 void
755 _gtk_builder_add (GtkBuilder *builder,
756                   ChildInfo  *child_info)
757 {
758   GObject *object;
759   GObject *parent;
760
761   /* Internal children are already added
762    * Also prevent us from being called twice.
763    */
764   if (!child_info ||
765       child_info->internal_child ||
766       child_info->added)
767     return;
768
769   object = child_info->object;
770   if (!object)
771     return;
772
773   if (!child_info->parent)
774     {
775       g_warning ("%s: Not adding, No parent",
776                  gtk_buildable_get_name (GTK_BUILDABLE (object)));
777       return;
778     }
779
780   g_assert (object != NULL);
781
782   parent = ((ObjectInfo*)child_info->parent)->object;
783   g_assert (GTK_IS_BUILDABLE (parent));
784
785   GTK_NOTE (BUILDER,
786             g_print ("adding %s to %s\n",
787                      gtk_buildable_get_name (GTK_BUILDABLE (object)),
788                      gtk_buildable_get_name (GTK_BUILDABLE (parent))));
789   
790   gtk_buildable_add_child (GTK_BUILDABLE (parent), builder, object,
791                            child_info->type);
792
793   child_info->added = TRUE;
794 }
795
796 void
797 _gtk_builder_add_signals (GtkBuilder *builder,
798                           GSList     *signals)
799 {
800   builder->priv->signals = g_slist_concat (builder->priv->signals,
801                                            g_slist_copy (signals));
802 }
803
804 static void
805 gtk_builder_apply_delayed_properties (GtkBuilder *builder)
806 {
807   GSList *l, *props;
808   DelayedProperty *property;
809   GObject *object;
810   GType object_type;
811   GObjectClass *oclass;
812   GParamSpec *pspec;
813
814   /* take the list over from the builder->priv.
815    *
816    * g_slist_reverse does not copy the list, so the list now
817    * belongs to us (and we free it at the end of this function).
818    */
819   props = g_slist_reverse (builder->priv->delayed_properties);
820   builder->priv->delayed_properties = NULL;
821
822   for (l = props; l; l = l->next)
823     {
824       property = (DelayedProperty*)l->data;
825       object = g_hash_table_lookup (builder->priv->objects, property->object);
826       g_assert (object != NULL);
827
828       object_type = G_OBJECT_TYPE (object);
829       g_assert (object_type != G_TYPE_INVALID);
830
831       oclass = g_type_class_ref (object_type);
832       g_assert (oclass != NULL);
833
834       pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
835                                             property->name);
836       if (!pspec)
837         g_warning ("Unknown property: %s.%s", g_type_name (object_type),
838                    property->name);
839       else
840         {
841           GObject *obj;
842
843           obj = g_hash_table_lookup (builder->priv->objects, property->value);
844           if (!obj)
845             g_warning ("No object called: %s", property->value);
846           else
847             g_object_set (object, property->name, obj, NULL);
848         }
849       g_free (property->value);
850       g_free (property->object);
851       g_free (property->name);
852       g_slice_free (DelayedProperty, property);
853       g_type_class_unref (oclass);
854     }
855   g_slist_free (props);
856 }
857
858 void
859 _gtk_builder_finish (GtkBuilder *builder)
860 {
861   gtk_builder_apply_delayed_properties (builder);
862 }
863
864 /**
865  * gtk_builder_new:
866  *
867  * Creates a new builder object.
868  *
869  * Return value: a new #GtkBuilder object
870  *
871  * Since: 2.12
872  **/
873 GtkBuilder *
874 gtk_builder_new (void)
875 {
876   return g_object_new (GTK_TYPE_BUILDER, NULL);
877 }
878
879 /**
880  * gtk_builder_add_from_file:
881  * @builder: a #GtkBuilder
882  * @filename: the name of the file to parse
883  * @error: (allow-none): return location for an error, or %NULL
884  *
885  * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
886  * UI definition</link> and merges it with the current contents of @builder. 
887  * 
888  * Upon errors 0 will be returned and @error will be assigned a
889  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR 
890  * domain.
891  *
892  * Returns: A positive value on success, 0 if an error occurred
893  *
894  * Since: 2.12
895  **/
896 guint
897 gtk_builder_add_from_file (GtkBuilder   *builder,
898                            const gchar  *filename,
899                            GError      **error)
900 {
901   gchar *buffer;
902   gsize length;
903   GError *tmp_error;
904
905   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
906   g_return_val_if_fail (filename != NULL, 0);
907   g_return_val_if_fail (error == NULL || *error == NULL, 0);
908
909   tmp_error = NULL;
910
911   if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
912     {
913       g_propagate_error (error, tmp_error);
914       return 0;
915     }
916   
917   g_free (builder->priv->filename);
918   g_free (builder->priv->resource_prefix);
919   builder->priv->filename = g_strdup (filename);
920   builder->priv->resource_prefix = NULL;
921
922   _gtk_builder_parser_parse_buffer (builder, filename,
923                                     buffer, length,
924                                     NULL,
925                                     &tmp_error);
926
927   g_free (buffer);
928
929   if (tmp_error != NULL)
930     {
931       g_propagate_error (error, tmp_error);
932       return 0;
933     }
934
935   return 1;
936 }
937
938 /**
939  * gtk_builder_add_objects_from_file:
940  * @builder: a #GtkBuilder
941  * @filename: the name of the file to parse
942  * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
943  * @error: (allow-none): return location for an error, or %NULL
944  *
945  * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
946  * UI definition</link> building only the requested objects and merges
947  * them with the current contents of @builder. 
948  *
949  * Upon errors 0 will be returned and @error will be assigned a
950  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR 
951  * domain.
952  *
953  * <note><para>
954  * If you are adding an object that depends on an object that is not 
955  * its child (for instance a #GtkTreeView that depends on its
956  * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
957  * </para></note>
958  *
959  * Returns: A positive value on success, 0 if an error occurred
960  *
961  * Since: 2.14
962  **/
963 guint
964 gtk_builder_add_objects_from_file (GtkBuilder   *builder,
965                                    const gchar  *filename,
966                                    gchar       **object_ids,
967                                    GError      **error)
968 {
969   gchar *buffer;
970   gsize length;
971   GError *tmp_error;
972
973   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
974   g_return_val_if_fail (filename != NULL, 0);
975   g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
976   g_return_val_if_fail (error == NULL || *error == NULL, 0);
977
978   tmp_error = NULL;
979
980   if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
981     {
982       g_propagate_error (error, tmp_error);
983       return 0;
984     }
985   
986   g_free (builder->priv->filename);
987   g_free (builder->priv->resource_prefix);
988   builder->priv->filename = g_strdup (filename);
989   builder->priv->resource_prefix = NULL;
990
991   _gtk_builder_parser_parse_buffer (builder, filename,
992                                     buffer, length,
993                                     object_ids,
994                                     &tmp_error);
995
996   g_free (buffer);
997
998   if (tmp_error != NULL)
999     {
1000       g_propagate_error (error, tmp_error);
1001       return 0;
1002     }
1003
1004   return 1;
1005 }
1006
1007 /**
1008  * gtk_builder_add_from_resource:
1009  * @builder: a #GtkBuilder
1010  * @resource_path: the path of the resource file to parse
1011  * @error: (allow-none): return location for an error, or %NULL
1012  *
1013  * Parses a resource file containing a <link linkend="BUILDER-UI">GtkBuilder
1014  * UI definition</link> and merges it with the current contents of @builder.
1015  *
1016  * Upon errors 0 will be returned and @error will be assigned a
1017  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_RESOURCE_ERROR
1018  * domain.
1019  *
1020  * Returns: A positive value on success, 0 if an error occurred
1021  *
1022  * Since: 3.4
1023  **/
1024 guint
1025 gtk_builder_add_from_resource (GtkBuilder   *builder,
1026                                const gchar  *resource_path,
1027                                GError      **error)
1028 {
1029   GError *tmp_error;
1030   GBytes *data;
1031   char *filename_for_errors;
1032   char *slash;
1033
1034   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1035   g_return_val_if_fail (resource_path != NULL, 0);
1036   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1037
1038   tmp_error = NULL;
1039
1040   data = g_resources_lookup_data (resource_path, 0, &tmp_error);
1041   if (data == NULL)
1042     {
1043       g_propagate_error (error, tmp_error);
1044       return 0;
1045     }
1046
1047   g_free (builder->priv->filename);
1048   g_free (builder->priv->resource_prefix);
1049   builder->priv->filename = g_strdup (".");
1050
1051   slash = strrchr (resource_path, '/');
1052   if (slash != NULL)
1053     builder->priv->resource_prefix =
1054       g_strndup (resource_path, slash - resource_path + 1);
1055   else
1056     builder->priv->resource_prefix =
1057       g_strdup ("/");
1058
1059   filename_for_errors = g_strconcat ("<resource>", resource_path, NULL);
1060
1061   _gtk_builder_parser_parse_buffer (builder, filename_for_errors,
1062                                     g_bytes_get_data (data, NULL), g_bytes_get_size (data),
1063                                     NULL,
1064                                     &tmp_error);
1065
1066   g_free (filename_for_errors);
1067   g_bytes_unref (data);
1068
1069   if (tmp_error != NULL)
1070     {
1071       g_propagate_error (error, tmp_error);
1072       return 0;
1073     }
1074
1075   return 1;
1076 }
1077
1078 /**
1079  * gtk_builder_add_objects_from_resource:
1080  * @builder: a #GtkBuilder
1081  * @resource_path: the path of the resource file to parse
1082  * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
1083  * @error: (allow-none): return location for an error, or %NULL
1084  *
1085  * Parses a resource file containing a <link linkend="BUILDER-UI">GtkBuilder
1086  * UI definition</link> building only the requested objects and merges
1087  * them with the current contents of @builder.
1088  *
1089  * Upon errors 0 will be returned and @error will be assigned a
1090  * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_RESOURCE_ERROR
1091  * domain.
1092  *
1093  * <note><para>
1094  * If you are adding an object that depends on an object that is not
1095  * its child (for instance a #GtkTreeView that depends on its
1096  * #GtkTreeModel), you have to explicitely list all of them in @object_ids.
1097  * </para></note>
1098  *
1099  * Returns: A positive value on success, 0 if an error occurred
1100  *
1101  * Since: 3.4
1102  **/
1103 guint
1104 gtk_builder_add_objects_from_resource (GtkBuilder   *builder,
1105                                        const gchar  *resource_path,
1106                                        gchar       **object_ids,
1107                                        GError      **error)
1108 {
1109   GError *tmp_error;
1110   GBytes *data;
1111   char *filename_for_errors;
1112   char *slash;
1113
1114   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1115   g_return_val_if_fail (resource_path != NULL, 0);
1116   g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
1117   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1118
1119   tmp_error = NULL;
1120
1121   data = g_resources_lookup_data (resource_path, 0, &tmp_error);
1122   if (data == NULL)
1123     {
1124       g_propagate_error (error, tmp_error);
1125       return 0;
1126     }
1127
1128   g_free (builder->priv->filename);
1129   g_free (builder->priv->resource_prefix);
1130   builder->priv->filename = g_strdup (".");
1131
1132   slash = strrchr (resource_path, '/');
1133   if (slash != NULL)
1134     builder->priv->resource_prefix =
1135       g_strndup (resource_path, slash - resource_path + 1);
1136   else
1137     builder->priv->resource_prefix =
1138       g_strdup ("/");
1139
1140   filename_for_errors = g_strconcat ("<resource>", resource_path, NULL);
1141
1142   _gtk_builder_parser_parse_buffer (builder, filename_for_errors,
1143                                     g_bytes_get_data (data, NULL), g_bytes_get_size (data),
1144                                     object_ids,
1145                                     &tmp_error);
1146   g_free (filename_for_errors);
1147   g_bytes_unref (data);
1148
1149   if (tmp_error != NULL)
1150     {
1151       g_propagate_error (error, tmp_error);
1152       return 0;
1153     }
1154
1155   return 1;
1156 }
1157
1158 /**
1159  * gtk_builder_add_from_string:
1160  * @builder: a #GtkBuilder
1161  * @buffer: the string to parse
1162  * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1163  * @error: (allow-none): return location for an error, or %NULL
1164  *
1165  * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
1166  * UI definition</link> and merges it with the current contents of @builder. 
1167  *
1168  * Upon errors 0 will be returned and @error will be assigned a
1169  * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
1170  *
1171  * Returns: A positive value on success, 0 if an error occurred
1172  *
1173  * Since: 2.12
1174  **/
1175 guint
1176 gtk_builder_add_from_string (GtkBuilder   *builder,
1177                              const gchar  *buffer,
1178                              gsize         length,
1179                              GError      **error)
1180 {
1181   GError *tmp_error;
1182
1183   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1184   g_return_val_if_fail (buffer != NULL, 0);
1185   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1186
1187   tmp_error = NULL;
1188
1189   g_free (builder->priv->filename);
1190   g_free (builder->priv->resource_prefix);
1191   builder->priv->filename = g_strdup (".");
1192   builder->priv->resource_prefix = NULL;
1193
1194   _gtk_builder_parser_parse_buffer (builder, "<input>",
1195                                     buffer, length,
1196                                     NULL,
1197                                     &tmp_error);
1198   if (tmp_error != NULL)
1199     {
1200       g_propagate_error (error, tmp_error);
1201       return 0;
1202     }
1203
1204   return 1;
1205 }
1206
1207 /**
1208  * gtk_builder_add_objects_from_string:
1209  * @builder: a #GtkBuilder
1210  * @buffer: the string to parse
1211  * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1212  * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
1213  * @error: (allow-none): return location for an error, or %NULL
1214  *
1215  * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
1216  * UI definition</link> building only the requested objects and merges
1217  * them with the current contents of @builder. 
1218  *
1219  * Upon errors 0 will be returned and @error will be assigned a
1220  * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
1221  * 
1222  * <note><para>
1223  * If you are adding an object that depends on an object that is not 
1224  * its child (for instance a #GtkTreeView that depends on its
1225  * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
1226  * </para></note>
1227  *
1228  * Returns: A positive value on success, 0 if an error occurred
1229  *
1230  * Since: 2.14
1231  **/
1232 guint
1233 gtk_builder_add_objects_from_string (GtkBuilder   *builder,
1234                                      const gchar  *buffer,
1235                                      gsize         length,
1236                                      gchar       **object_ids,
1237                                      GError      **error)
1238 {
1239   GError *tmp_error;
1240
1241   g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1242   g_return_val_if_fail (buffer != NULL, 0);
1243   g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
1244   g_return_val_if_fail (error == NULL || *error == NULL, 0);
1245
1246   tmp_error = NULL;
1247
1248   g_free (builder->priv->filename);
1249   g_free (builder->priv->resource_prefix);
1250   builder->priv->filename = g_strdup (".");
1251   builder->priv->resource_prefix = NULL;
1252
1253   _gtk_builder_parser_parse_buffer (builder, "<input>",
1254                                     buffer, length,
1255                                     object_ids,
1256                                     &tmp_error);
1257
1258   if (tmp_error != NULL)
1259     {
1260       g_propagate_error (error, tmp_error);
1261       return 0;
1262     }
1263
1264   return 1;
1265 }
1266
1267 /**
1268  * gtk_builder_get_object:
1269  * @builder: a #GtkBuilder
1270  * @name: name of object to get
1271  *
1272  * Gets the object named @name. Note that this function does not
1273  * increment the reference count of the returned object. 
1274  *
1275  * Return value: (transfer none): the object named @name or %NULL if
1276  *    it could not be found in the object tree.
1277  *
1278  * Since: 2.12
1279  **/
1280 GObject *
1281 gtk_builder_get_object (GtkBuilder  *builder,
1282                         const gchar *name)
1283 {
1284   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1285   g_return_val_if_fail (name != NULL, NULL);
1286
1287   return g_hash_table_lookup (builder->priv->objects, name);
1288 }
1289
1290 static void
1291 object_add_to_list (gchar    *object_id,
1292                     GObject  *object,
1293                     GSList  **list)
1294 {
1295   *list = g_slist_prepend (*list, object);
1296 }
1297
1298 /**
1299  * gtk_builder_get_objects:
1300  * @builder: a #GtkBuilder
1301  *
1302  * Gets all objects that have been constructed by @builder. Note that 
1303  * this function does not increment the reference counts of the returned
1304  * objects.
1305  *
1306  * Return value: (element-type GObject) (transfer container): a newly-allocated #GSList containing all the objects
1307  *   constructed by the #GtkBuilder instance. It should be freed by
1308  *   g_slist_free()
1309  *
1310  * Since: 2.12
1311  **/
1312 GSList *
1313 gtk_builder_get_objects (GtkBuilder *builder)
1314 {
1315   GSList *objects = NULL;
1316
1317   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1318
1319   g_hash_table_foreach (builder->priv->objects, (GHFunc)object_add_to_list, &objects);
1320
1321   return g_slist_reverse (objects);
1322 }
1323
1324 /**
1325  * gtk_builder_set_translation_domain:
1326  * @builder: a #GtkBuilder
1327  * @domain: (allow-none): the translation domain or %NULL
1328  *
1329  * Sets the translation domain of @builder. 
1330  * See #GtkBuilder:translation-domain.
1331  *
1332  * Since: 2.12
1333  **/
1334 void
1335 gtk_builder_set_translation_domain (GtkBuilder  *builder,
1336                                     const gchar *domain)
1337 {
1338   gchar *new_domain;
1339     
1340   g_return_if_fail (GTK_IS_BUILDER (builder));
1341
1342   new_domain = g_strdup (domain);
1343   g_free (builder->priv->domain);
1344   builder->priv->domain = new_domain;
1345
1346   g_object_notify (G_OBJECT (builder), "translation-domain");
1347 }
1348
1349 /**
1350  * gtk_builder_get_translation_domain:
1351  * @builder: a #GtkBuilder
1352  *
1353  * Gets the translation domain of @builder.
1354  *
1355  * Return value: the translation domain. This string is owned
1356  * by the builder object and must not be modified or freed.
1357  *
1358  * Since: 2.12
1359  **/
1360 const gchar *
1361 gtk_builder_get_translation_domain (GtkBuilder *builder)
1362 {
1363   g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1364
1365   return builder->priv->domain;
1366 }
1367
1368 typedef struct {
1369   GModule *module;
1370   gpointer data;
1371 } connect_args;
1372
1373 static void
1374 gtk_builder_connect_signals_default (GtkBuilder    *builder,
1375                                      GObject       *object,
1376                                      const gchar   *signal_name,
1377                                      const gchar   *handler_name,
1378                                      GObject       *connect_object,
1379                                      GConnectFlags  flags,
1380                                      gpointer       user_data)
1381 {
1382   GCallback func;
1383   connect_args *args = (connect_args*)user_data;
1384   
1385   if (!g_module_symbol (args->module, handler_name, (gpointer)&func))
1386     {
1387       g_warning ("Could not find signal handler '%s'", handler_name);
1388       return;
1389     }
1390
1391   if (connect_object)
1392     g_signal_connect_object (object, signal_name, func, connect_object, flags);
1393   else
1394     g_signal_connect_data (object, signal_name, func, args->data, NULL, flags);
1395 }
1396
1397
1398 /**
1399  * gtk_builder_connect_signals:
1400  * @builder: a #GtkBuilder
1401  * @user_data: a pointer to a structure sent in as user data to all signals
1402  *
1403  * This method is a simpler variation of gtk_builder_connect_signals_full().
1404  * It uses #GModule's introspective features (by opening the module %NULL) 
1405  * to look at the application's symbol table. From here it tries to match
1406  * the signal handler names given in the interface description with
1407  * symbols in the application and connects the signals. Note that this
1408  * function can only be called once, subsequent calls will do nothing.
1409  * 
1410  * Note that this function will not work correctly if #GModule is not
1411  * supported on the platform.
1412  *
1413  * When compiling applications for Windows, you must declare signal callbacks
1414  * with #G_MODULE_EXPORT, or they will not be put in the symbol table.
1415  * On Linux and Unices, this is not necessary; applications should instead
1416  * be compiled with the -Wl,--export-dynamic CFLAGS, and linked against
1417  * gmodule-export-2.0.
1418  *
1419  * Since: 2.12
1420  **/
1421 void
1422 gtk_builder_connect_signals (GtkBuilder *builder,
1423                              gpointer    user_data)
1424 {
1425   connect_args *args;
1426   
1427   g_return_if_fail (GTK_IS_BUILDER (builder));
1428   
1429   if (!g_module_supported ())
1430     g_error ("gtk_builder_connect_signals() requires working GModule");
1431
1432   args = g_slice_new0 (connect_args);
1433   args->module = g_module_open (NULL, G_MODULE_BIND_LAZY);
1434   args->data = user_data;
1435   
1436   gtk_builder_connect_signals_full (builder,
1437                                     gtk_builder_connect_signals_default,
1438                                     args);
1439   g_module_close (args->module);
1440
1441   g_slice_free (connect_args, args);
1442 }
1443
1444 /**
1445  * GtkBuilderConnectFunc:
1446  * @builder: a #GtkBuilder
1447  * @object: object to connect a signal to
1448  * @signal_name: name of the signal
1449  * @handler_name: name of the handler
1450  * @connect_object: a #GObject, if non-%NULL, use g_signal_connect_object()
1451  * @flags: #GConnectFlags to use
1452  * @user_data: user data
1453  *
1454  * This is the signature of a function used to connect signals.  It is used
1455  * by the gtk_builder_connect_signals() and gtk_builder_connect_signals_full()
1456  * methods.  It is mainly intended for interpreted language bindings, but
1457  * could be useful where the programmer wants more control over the signal
1458  * connection process. Note that this function can only be called once,
1459  * subsequent calls will do nothing.
1460  *
1461  * Since: 2.12
1462  */
1463
1464 /**
1465  * gtk_builder_connect_signals_full:
1466  * @builder: a #GtkBuilder
1467  * @func: (scope call): the function used to connect the signals
1468  * @user_data: arbitrary data that will be passed to the connection function
1469  *
1470  * This function can be thought of the interpreted language binding
1471  * version of gtk_builder_connect_signals(), except that it does not
1472  * require GModule to function correctly.
1473  *
1474  * Since: 2.12
1475  */
1476 void
1477 gtk_builder_connect_signals_full (GtkBuilder            *builder,
1478                                   GtkBuilderConnectFunc  func,
1479                                   gpointer               user_data)
1480 {
1481   GSList *l;
1482   GObject *object;
1483   GObject *connect_object;
1484   
1485   g_return_if_fail (GTK_IS_BUILDER (builder));
1486   g_return_if_fail (func != NULL);
1487   
1488   if (!builder->priv->signals)
1489     return;
1490
1491   builder->priv->signals = g_slist_reverse (builder->priv->signals);
1492   for (l = builder->priv->signals; l; l = l->next)
1493     {
1494       SignalInfo *signal = (SignalInfo*)l->data;
1495
1496       g_assert (signal != NULL);
1497       g_assert (signal->name != NULL);
1498
1499       object = g_hash_table_lookup (builder->priv->objects,
1500                                     signal->object_name);
1501       g_assert (object != NULL);
1502
1503       connect_object = NULL;
1504       
1505       if (signal->connect_object_name)
1506         {
1507           connect_object = g_hash_table_lookup (builder->priv->objects,
1508                                                 signal->connect_object_name);
1509           if (!connect_object)
1510               g_warning ("Could not lookup object %s on signal %s of object %s",
1511                          signal->connect_object_name, signal->name,
1512                          signal->object_name);
1513         }
1514                                                   
1515       func (builder, object, signal->name, signal->handler, 
1516             connect_object, signal->flags, user_data);
1517     }
1518
1519   g_slist_foreach (builder->priv->signals, (GFunc)_free_signal_info, NULL);
1520   g_slist_free (builder->priv->signals);
1521   builder->priv->signals = NULL;
1522 }
1523
1524 /**
1525  * gtk_builder_value_from_string:
1526  * @builder: a #GtkBuilder
1527  * @pspec: the #GParamSpec for the property
1528  * @string: the string representation of the value
1529  * @value: (out): the #GValue to store the result in
1530  * @error: (allow-none): return location for an error, or %NULL
1531  *
1532  * This function demarshals a value from a string. This function
1533  * calls g_value_init() on the @value argument, so it need not be
1534  * initialised beforehand.
1535  *
1536  * This function can handle char, uchar, boolean, int, uint, long,
1537  * ulong, enum, flags, float, double, string, #GdkColor, #GdkRGBA and
1538  * #GtkAdjustment type values. Support for #GtkWidget type values is
1539  * still to come.
1540  *
1541  * Upon errors %FALSE will be returned and @error will be assigned a
1542  * #GError from the #GTK_BUILDER_ERROR domain.
1543  *
1544  * Returns: %TRUE on success
1545  *
1546  * Since: 2.12
1547  */
1548 gboolean
1549 gtk_builder_value_from_string (GtkBuilder   *builder,
1550                                GParamSpec   *pspec,
1551                                const gchar  *string,
1552                                GValue       *value,
1553                                GError      **error)
1554 {
1555   g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
1556   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1557   g_return_val_if_fail (string != NULL, FALSE);
1558   g_return_val_if_fail (value != NULL, FALSE);
1559   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1560
1561   /*
1562    * GParamSpecUnichar has the internal type G_TYPE_UINT,
1563    * so we cannot handle this in the switch, do it separately
1564    */
1565   if (G_IS_PARAM_SPEC_UNICHAR (pspec))
1566     {
1567       gunichar c;
1568       g_value_init (value, G_TYPE_UINT);
1569       c = g_utf8_get_char_validated (string, strlen (string));
1570       if (c > 0)
1571         g_value_set_uint (value, c);
1572       return TRUE;
1573     }
1574
1575   /*
1576    * GParamSpecVariant can specify a GVariantType which can help with
1577    * parsing, so we need to take care of that here.
1578    */
1579   if (G_IS_PARAM_SPEC_VARIANT (pspec))
1580     {
1581       GParamSpecVariant *variant_pspec = G_PARAM_SPEC_VARIANT (pspec);
1582       const GVariantType *type;
1583       GVariant *variant;
1584
1585       g_value_init (value, G_TYPE_VARIANT);
1586
1587       /* The GVariant parser doesn't deal with indefinite types */
1588       if (g_variant_type_is_definite (variant_pspec->type))
1589         type = variant_pspec->type;
1590       else
1591         type = NULL;
1592
1593       variant = g_variant_parse (type, string, NULL, NULL, error);
1594       if (variant == NULL)
1595         return FALSE;
1596       g_value_take_variant (value, variant);
1597       return TRUE;
1598     }
1599
1600   return gtk_builder_value_from_string_type (builder,
1601                                              G_PARAM_SPEC_VALUE_TYPE (pspec),
1602                                              string, value, error);
1603 }
1604
1605 /**
1606  * gtk_builder_value_from_string_type:
1607  * @builder: a #GtkBuilder
1608  * @type: the #GType of the value
1609  * @string: the string representation of the value
1610  * @value: (out): the #GValue to store the result in
1611  * @error: (allow-none): return location for an error, or %NULL
1612  *
1613  * Like gtk_builder_value_from_string(), this function demarshals 
1614  * a value from a string, but takes a #GType instead of #GParamSpec.
1615  * This function calls g_value_init() on the @value argument, so it 
1616  * need not be initialised beforehand.
1617  *
1618  * Upon errors %FALSE will be returned and @error will be assigned a
1619  * #GError from the #GTK_BUILDER_ERROR domain.
1620  *
1621  * Returns: %TRUE on success
1622  *
1623  * Since: 2.12
1624  */
1625 gboolean
1626 gtk_builder_value_from_string_type (GtkBuilder   *builder,
1627                                     GType         type,
1628                                     const gchar  *string,
1629                                     GValue       *value,
1630                                     GError      **error)
1631 {
1632   gboolean ret = TRUE;
1633
1634   g_return_val_if_fail (type != G_TYPE_INVALID, FALSE);
1635   g_return_val_if_fail (string != NULL, FALSE);
1636   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1637
1638   g_value_init (value, type);
1639
1640   switch (G_TYPE_FUNDAMENTAL (type))
1641     {
1642     case G_TYPE_CHAR:
1643       g_value_set_schar (value, string[0]);
1644       break;
1645     case G_TYPE_UCHAR:
1646       g_value_set_uchar (value, (guchar)string[0]);
1647       break;
1648     case G_TYPE_BOOLEAN:
1649       {
1650         gboolean b;
1651
1652         if (!_gtk_builder_boolean_from_string (string, &b, error))
1653           {
1654             ret = FALSE;
1655             break;
1656           }
1657         g_value_set_boolean (value, b);
1658         break;
1659       }
1660     case G_TYPE_INT:
1661     case G_TYPE_LONG:
1662       {
1663         long l;
1664         gchar *endptr = NULL;
1665         errno = 0;
1666         l = g_ascii_strtoll (string, &endptr, 0);
1667         if (errno || endptr == string)
1668           {
1669             g_set_error (error,
1670                          GTK_BUILDER_ERROR,
1671                          GTK_BUILDER_ERROR_INVALID_VALUE,
1672                          "Could not parse integer `%s'",
1673                          string);
1674             ret = FALSE;
1675             break;
1676           }
1677         if (G_VALUE_HOLDS_INT (value))
1678           g_value_set_int (value, l);
1679         else
1680           g_value_set_long (value, l);
1681         break;
1682       }
1683     case G_TYPE_UINT:
1684     case G_TYPE_ULONG:
1685       {
1686         gulong ul;
1687         gchar *endptr = NULL;
1688         errno = 0;
1689         ul = g_ascii_strtoull (string, &endptr, 0);
1690         if (errno || endptr == string)
1691           {
1692             g_set_error (error,
1693                          GTK_BUILDER_ERROR,
1694                          GTK_BUILDER_ERROR_INVALID_VALUE,
1695                          "Could not parse unsigned integer `%s'",
1696                          string);
1697             ret = FALSE;
1698             break;
1699           }
1700         if (G_VALUE_HOLDS_UINT (value))
1701           g_value_set_uint (value, ul);
1702         else 
1703           g_value_set_ulong (value, ul);
1704         break;
1705       }
1706     case G_TYPE_ENUM:
1707       {
1708         gint enum_value;
1709         if (!_gtk_builder_enum_from_string (type, string, &enum_value, error))
1710           {
1711             ret = FALSE;
1712             break;
1713           }
1714         g_value_set_enum (value, enum_value);
1715         break;
1716       }
1717     case G_TYPE_FLAGS:
1718       {
1719         guint flags_value;
1720
1721         if (!_gtk_builder_flags_from_string (type, string, &flags_value, error))
1722           {
1723             ret = FALSE;
1724             break;
1725           }
1726         g_value_set_flags (value, flags_value);
1727         break;
1728       }
1729     case G_TYPE_FLOAT:
1730     case G_TYPE_DOUBLE:
1731       {
1732         gdouble d;
1733         gchar *endptr = NULL;
1734         errno = 0;
1735         d = g_ascii_strtod (string, &endptr);
1736         if (errno || endptr == string)
1737           {
1738             g_set_error (error,
1739                          GTK_BUILDER_ERROR,
1740                          GTK_BUILDER_ERROR_INVALID_VALUE,
1741                          "Could not parse double `%s'",
1742                          string);
1743             ret = FALSE;
1744             break;
1745           }
1746         if (G_VALUE_HOLDS_FLOAT (value))
1747           g_value_set_float (value, d);
1748         else
1749           g_value_set_double (value, d);
1750         break;
1751       }
1752     case G_TYPE_STRING:
1753       g_value_set_string (value, string);
1754       break;
1755     case G_TYPE_VARIANT:
1756       {
1757         GVariant *variant;
1758
1759         variant = g_variant_parse (NULL, string, NULL, NULL, error);
1760         if (value != NULL)
1761           g_value_take_variant (value, variant);
1762         else
1763           ret = FALSE;
1764       }
1765       break;
1766     case G_TYPE_BOXED:
1767       if (G_VALUE_HOLDS (value, GDK_TYPE_COLOR))
1768         {
1769           GdkColor color = { 0, };
1770
1771           if (gdk_color_parse (string, &color))
1772             g_value_set_boxed (value, &color);
1773           else
1774             {
1775               g_set_error (error,
1776                            GTK_BUILDER_ERROR,
1777                            GTK_BUILDER_ERROR_INVALID_VALUE,
1778                            "Could not parse color `%s'",
1779                            string);
1780               ret = FALSE;
1781             }
1782         }
1783       else if (G_VALUE_HOLDS (value, GDK_TYPE_RGBA))
1784         {
1785           GdkRGBA rgba = { 0 };
1786
1787           if (gdk_rgba_parse (&rgba, string))
1788             g_value_set_boxed (value, &rgba);
1789           else
1790             {
1791               g_set_error (error,
1792                            GTK_BUILDER_ERROR,
1793                            GTK_BUILDER_ERROR_INVALID_VALUE,
1794                            "Could not parse RGBA color '%s'",
1795                            string);
1796               ret = FALSE;
1797             }
1798         }
1799       else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
1800         {
1801           gchar **vector = g_strsplit (string, "\n", 0);
1802           g_value_take_boxed (value, vector);
1803         }
1804       else
1805         {
1806           g_set_error (error,
1807                        GTK_BUILDER_ERROR,
1808                        GTK_BUILDER_ERROR_INVALID_VALUE,
1809                        "Could not parse '%s' as a %s",
1810                        string, G_VALUE_TYPE_NAME (value));
1811           ret = FALSE;
1812         }
1813       break;
1814     case G_TYPE_OBJECT:
1815       if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
1816         {
1817           gchar *filename;
1818           GError *tmp_error = NULL;
1819           GdkPixbuf *pixbuf = NULL;
1820        
1821           if (gtk_builder_get_object (builder, string))
1822             {
1823               g_set_error (error,
1824                            GTK_BUILDER_ERROR,
1825                            GTK_BUILDER_ERROR_INVALID_VALUE,
1826                            "Could not load image '%s': "
1827                            " '%s' is already used as object id",
1828                            string, string);
1829               return FALSE;
1830             }
1831
1832           filename = _gtk_builder_get_resource_path (builder, string);
1833           if (filename != NULL)
1834             {
1835               GInputStream *stream = g_resources_open_stream (filename, 0, &tmp_error);
1836               if (stream != NULL)
1837                 {
1838                   pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &tmp_error);
1839                   g_object_unref (stream);
1840                 }
1841             }
1842           else
1843             {
1844               filename = _gtk_builder_get_absolute_filename (builder, string);
1845               pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);
1846             }
1847
1848           if (pixbuf == NULL)
1849             {
1850               GtkIconTheme *theme;
1851
1852               g_warning ("Could not load image '%s': %s", 
1853                          string, tmp_error->message);
1854               g_error_free (tmp_error);
1855
1856               /* fall back to a missing image */
1857               theme = gtk_icon_theme_get_default ();
1858               pixbuf = gtk_icon_theme_load_icon (theme, 
1859                                                  GTK_STOCK_MISSING_IMAGE,
1860                                                  16,
1861                                                  GTK_ICON_LOOKUP_USE_BUILTIN,
1862                                                  NULL);
1863             }
1864  
1865           if (pixbuf)
1866             {
1867               g_value_set_object (value, pixbuf);
1868               g_object_unref (G_OBJECT (pixbuf));
1869             }
1870
1871           g_free (filename);
1872
1873           ret = TRUE;
1874         }
1875       else
1876         ret = FALSE;
1877       break;
1878     default:
1879       ret = FALSE;
1880       break;
1881     }
1882  
1883   /* Catch unassigned error for object types as well as any unsupported types.
1884    * While parsing GtkBuilder; object types are deserialized
1885    * without calling gtk_builder_value_from_string_type().
1886    */
1887   if (!ret && error && *error == NULL) 
1888     g_set_error (error,
1889                  GTK_BUILDER_ERROR,
1890                  GTK_BUILDER_ERROR_INVALID_VALUE,
1891                  "Unsupported GType `%s'", g_type_name (type));
1892
1893   return ret;
1894 }
1895
1896 gboolean
1897 _gtk_builder_enum_from_string (GType         type, 
1898                                const gchar  *string,
1899                                gint         *enum_value,
1900                                GError      **error)
1901 {
1902   GEnumClass *eclass;
1903   GEnumValue *ev;
1904   gchar *endptr;
1905   gint value;
1906   gboolean ret;
1907   
1908   g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
1909   g_return_val_if_fail (string != NULL, FALSE);
1910   
1911   ret = TRUE;
1912
1913   endptr = NULL;
1914   errno = 0;
1915   value = g_ascii_strtoull (string, &endptr, 0);
1916   if (errno == 0 && endptr != string) /* parsed a number */
1917     *enum_value = value;
1918   else
1919     {
1920       eclass = g_type_class_ref (type);
1921       ev = g_enum_get_value_by_name (eclass, string);
1922       if (!ev)
1923         ev = g_enum_get_value_by_nick (eclass, string);
1924
1925       if (ev)
1926         *enum_value = ev->value;
1927       else
1928         {
1929           g_set_error (error,
1930                        GTK_BUILDER_ERROR,
1931                        GTK_BUILDER_ERROR_INVALID_VALUE,
1932                        "Could not parse enum: `%s'",
1933                        string);
1934           ret = FALSE;
1935         }
1936       
1937       g_type_class_unref (eclass);
1938     }
1939   
1940   return ret;
1941 }
1942
1943 gboolean
1944 _gtk_builder_flags_from_string (GType         type, 
1945                                 const gchar  *string,
1946                                 guint        *flags_value,
1947                                 GError      **error)
1948 {
1949   GFlagsClass *fclass;
1950   gchar *endptr, *prevptr;
1951   guint i, j, value;
1952   gchar *flagstr;
1953   GFlagsValue *fv;
1954   const gchar *flag;
1955   gunichar ch;
1956   gboolean eos, ret;
1957
1958   g_return_val_if_fail (G_TYPE_IS_FLAGS (type), FALSE);
1959   g_return_val_if_fail (string != 0, FALSE);
1960
1961   ret = TRUE;
1962
1963   endptr = NULL;
1964   errno = 0;
1965   value = g_ascii_strtoull (string, &endptr, 0);
1966   if (errno == 0 && endptr != string) /* parsed a number */
1967     *flags_value = value;
1968   else
1969     {
1970       fclass = g_type_class_ref (type);
1971
1972       flagstr = g_strdup (string);
1973       for (value = i = j = 0; ; i++)
1974         {
1975           
1976           eos = flagstr[i] == '\0';
1977           
1978           if (!eos && flagstr[i] != '|')
1979             continue;
1980           
1981           flag = &flagstr[j];
1982           endptr = &flagstr[i];
1983           
1984           if (!eos)
1985             {
1986               flagstr[i++] = '\0';
1987               j = i;
1988             }
1989           
1990           /* trim spaces */
1991           for (;;)
1992             {
1993               ch = g_utf8_get_char (flag);
1994               if (!g_unichar_isspace (ch))
1995                 break;
1996               flag = g_utf8_next_char (flag);
1997             }
1998           
1999           while (endptr > flag)
2000             {
2001               prevptr = g_utf8_prev_char (endptr);
2002               ch = g_utf8_get_char (prevptr);
2003               if (!g_unichar_isspace (ch))
2004                 break;
2005               endptr = prevptr;
2006             }
2007           
2008           if (endptr > flag)
2009             {
2010               *endptr = '\0';
2011               fv = g_flags_get_value_by_name (fclass, flag);
2012               
2013               if (!fv)
2014                 fv = g_flags_get_value_by_nick (fclass, flag);
2015               
2016               if (fv)
2017                 value |= fv->value;
2018               else
2019                 {
2020                   g_set_error (error,
2021                                GTK_BUILDER_ERROR,
2022                                GTK_BUILDER_ERROR_INVALID_VALUE,
2023                                "Unknown flag: `%s'",
2024                                flag);
2025                   ret = FALSE;
2026                   break;
2027                 }
2028             }
2029           
2030           if (eos)
2031             {
2032               *flags_value = value;
2033               break;
2034             }
2035         }
2036       
2037       g_free (flagstr);
2038       
2039       g_type_class_unref (fclass);
2040     }
2041
2042   return ret;
2043 }
2044
2045 /**
2046  * gtk_builder_get_type_from_name:
2047  * @builder: a #GtkBuilder
2048  * @type_name: type name to lookup
2049  *
2050  * Looks up a type by name, using the virtual function that 
2051  * #GtkBuilder has for that purpose. This is mainly used when
2052  * implementing the #GtkBuildable interface on a type.
2053  *
2054  * Returns: the #GType found for @type_name or #G_TYPE_INVALID 
2055  *   if no type was found
2056  *
2057  * Since: 2.12
2058  */
2059 GType
2060 gtk_builder_get_type_from_name (GtkBuilder  *builder, 
2061                                 const gchar *type_name)
2062 {
2063   g_return_val_if_fail (GTK_IS_BUILDER (builder), G_TYPE_INVALID);
2064   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
2065
2066   return GTK_BUILDER_GET_CLASS (builder)->get_type_from_name (builder, type_name);
2067 }
2068
2069 GQuark
2070 gtk_builder_error_quark (void)
2071 {
2072   return g_quark_from_static_string ("gtk-builder-error-quark");
2073 }
2074
2075 gchar *
2076 _gtk_builder_get_resource_path (GtkBuilder *builder, const gchar *string)
2077 {
2078   if (g_str_has_prefix (string, "resource:///"))
2079     return g_uri_unescape_string (string + 11, "/");
2080
2081   if (g_path_is_absolute (string) ||
2082       builder->priv->resource_prefix == NULL)
2083     return NULL;
2084
2085   return g_build_path ("/", builder->priv->resource_prefix, string, NULL);
2086 }
2087
2088 gchar *
2089 _gtk_builder_get_absolute_filename (GtkBuilder *builder, const gchar *string)
2090 {
2091   gchar *filename;
2092   gchar *dirname = NULL;
2093   
2094   if (g_path_is_absolute (string))
2095     return g_strdup (string);
2096
2097   if (builder->priv->filename &&
2098       strcmp (builder->priv->filename, ".") != 0) 
2099     {
2100       dirname = g_path_get_dirname (builder->priv->filename);
2101
2102       if (strcmp (dirname, ".") == 0)
2103         {
2104           g_free (dirname);
2105           dirname = g_get_current_dir ();
2106         }
2107     }
2108   else
2109     dirname = g_get_current_dir ();
2110     
2111   filename = g_build_filename (dirname, string, NULL);
2112   g_free (dirname);
2113   
2114   return filename;
2115 }