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>
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.
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.
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.
25 * @Short_description: Build an interface from an XML UI definition
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.
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.
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.
52 * The function gtk_builder_connect_signals() and variants thereof can be
53 * used to connect handlers to the named signals in the description.
55 * <refsect2 id="BUILDER-UI">
56 * <title>GtkBuilder UI Definitions</title>
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
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*) >
74 * <!ATTLIST interface domain #IMPLIED >
75 * <!ATTLIST object id #REQUIRED
78 * constructor #IMPLIED >
79 * <!ATTLIST requires lib #REQUIRED
81 * <!ATTLIST property name #REQUIRED
82 * translatable #IMPLIED
85 * <!ATTLIST signal name #REQUIRED
90 * last_modification_time #IMPLIED >
91 * <!ATTLIST child type #IMPLIED
92 * internal-child #IMPLIED >
93 * ]]></programlisting>
95 * The toplevel element is <interface>. 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 * <object> elements, which can contain <property> elements to set
100 * properties, <signal> elements which connect signals to handlers, and
101 * <child> 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 <child> element contains an <object> element which
104 * describes the child object. The target toolkit version(s) are described by
105 * <requires> 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 * "<major>.<minor>". The builder will error out if the version
109 * requirements are not met.
111 * Typically, the specific kind of object represented by an <object>
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.
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
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
134 * Setting properties of objects is pretty straightforward with the
135 * <property> 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.
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 — 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.
158 * Signal handlers are set up with the <signal> 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.
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 <child> element
172 * to a true value. Note that GtkBuilder still requires an <object>
173 * element for the internal child, even if it has already been constructed.
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 <child>. The possible values
178 * for the "type" attribute are described in the sections describing the
179 * widget-specific portions of UI definitions.
182 * <title>A GtkBuilder UI Definition</title>
183 * <programlisting><![CDATA[
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>
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"/>
205 * ]]></programlisting>
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 <child> element gets parsed by the custom tag
211 * handler of the parent object, while a custom element in an <object>
212 * element gets parsed by the custom tag handler of the object.
214 * These XML fragments are explained in the documentation of the respective
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>.
246 #include <errno.h> /* errno */
248 #include <string.h> /* strlen */
250 #include "gtkbuilder.h"
251 #include "gtkbuildable.h"
252 #include "gtkbuilderprivate.h"
253 #include "gtkdebug.h"
256 #include "gtkprivate.h"
257 #include "gtktypebuiltins.h"
258 #include "gtkwindow.h"
259 #include "gtkicontheme.h"
260 #include "gtkstock.h"
263 static void gtk_builder_class_init (GtkBuilderClass *klass);
264 static void gtk_builder_init (GtkBuilder *builder);
265 static void gtk_builder_finalize (GObject *object);
266 static void gtk_builder_set_property (GObject *object,
270 static void gtk_builder_get_property (GObject *object,
274 static GType gtk_builder_real_get_type_from_name (GtkBuilder *builder,
275 const gchar *type_name);
279 PROP_TRANSLATION_DOMAIN,
282 struct _GtkBuilderPrivate
286 GSList *delayed_properties;
291 G_DEFINE_TYPE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)
294 gtk_builder_class_init (GtkBuilderClass *klass)
296 GObjectClass *gobject_class;
298 gobject_class = G_OBJECT_CLASS (klass);
300 gobject_class->finalize = gtk_builder_finalize;
301 gobject_class->set_property = gtk_builder_set_property;
302 gobject_class->get_property = gtk_builder_get_property;
304 klass->get_type_from_name = gtk_builder_real_get_type_from_name;
307 * GtkBuilder:translation-domain:
309 * The translation domain used when translating property values that
310 * have been marked as translatable in interface descriptions.
311 * If the translation domain is %NULL, #GtkBuilder uses gettext(),
312 * otherwise g_dgettext().
316 g_object_class_install_property (gobject_class,
317 PROP_TRANSLATION_DOMAIN,
318 g_param_spec_string ("translation-domain",
319 P_("Translation Domain"),
320 P_("The translation domain used by gettext"),
322 GTK_PARAM_READWRITE));
324 g_type_class_add_private (gobject_class, sizeof (GtkBuilderPrivate));
328 gtk_builder_init (GtkBuilder *builder)
330 builder->priv = G_TYPE_INSTANCE_GET_PRIVATE (builder, GTK_TYPE_BUILDER,
332 builder->priv->domain = NULL;
333 builder->priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal,
334 g_free, g_object_unref);
339 * GObject virtual methods
343 gtk_builder_finalize (GObject *object)
345 GtkBuilderPrivate *priv = GTK_BUILDER (object)->priv;
347 g_free (priv->domain);
348 g_free (priv->filename);
350 g_hash_table_destroy (priv->objects);
352 g_slist_foreach (priv->signals, (GFunc) _free_signal_info, NULL);
353 g_slist_free (priv->signals);
355 G_OBJECT_CLASS (gtk_builder_parent_class)->finalize (object);
359 gtk_builder_set_property (GObject *object,
364 GtkBuilder *builder = GTK_BUILDER (object);
368 case PROP_TRANSLATION_DOMAIN:
369 gtk_builder_set_translation_domain (builder, g_value_get_string (value));
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 gtk_builder_get_property (GObject *object,
383 GtkBuilder *builder = GTK_BUILDER (object);
387 case PROP_TRANSLATION_DOMAIN:
388 g_value_set_string (value, builder->priv->domain);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 * Try to map a type name to a _get_type function
401 * GtkWindow -> gtk_window_get_type
402 * GtkHBox -> gtk_hbox_get_type
403 * GtkUIManager -> gtk_ui_manager_get_type
407 _gtk_builder_resolve_type_lazily (const gchar *name)
409 static GModule *module = NULL;
411 GString *symbol_name = g_string_new ("");
414 GType gtype = G_TYPE_INVALID;
417 module = g_module_open (NULL, 0);
419 for (i = 0; name[i] != '\0'; i++)
422 /* skip if uppercase, first or previous is uppercase */
423 if ((c == g_ascii_toupper (c) &&
424 i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
425 (i > 2 && name[i] == g_ascii_toupper (name[i]) &&
426 name[i-1] == g_ascii_toupper (name[i-1]) &&
427 name[i-2] == g_ascii_toupper (name[i-2])))
428 g_string_append_c (symbol_name, '_');
429 g_string_append_c (symbol_name, g_ascii_tolower (c));
431 g_string_append (symbol_name, "_get_type");
433 symbol = g_string_free (symbol_name, FALSE);
435 if (g_module_symbol (module, symbol, (gpointer)&func))
444 * GtkBuilder virtual methods
448 gtk_builder_real_get_type_from_name (GtkBuilder *builder,
449 const gchar *type_name)
453 gtype = g_type_from_name (type_name);
454 if (gtype != G_TYPE_INVALID)
457 return _gtk_builder_resolve_type_lazily (type_name);
468 gtk_builder_get_parameters (GtkBuilder *builder,
470 const gchar *object_name,
473 GArray **construct_parameters)
477 GObjectClass *oclass;
478 DelayedProperty *property;
479 GError *error = NULL;
481 oclass = g_type_class_ref (object_type);
482 g_assert (oclass != NULL);
484 *parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
485 *construct_parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
487 for (l = properties; l; l = l->next)
489 PropertyInfo *prop = (PropertyInfo*)l->data;
490 GParameter parameter = { NULL };
492 pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
496 g_warning ("Unknown property: %s.%s",
497 g_type_name (object_type), prop->name);
501 parameter.name = prop->name;
503 if (G_IS_PARAM_SPEC_OBJECT (pspec) &&
504 (G_PARAM_SPEC_VALUE_TYPE (pspec) != GDK_TYPE_PIXBUF))
506 GObject *object = gtk_builder_get_object (builder, prop->data);
510 g_value_init (¶meter.value, G_OBJECT_TYPE (object));
511 g_value_set_object (¶meter.value, object);
515 if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
517 g_warning ("Failed to get constuct only property "
518 "%s of %s with value `%s'",
519 prop->name, object_name, prop->data);
522 /* Delay setting property */
523 property = g_slice_new (DelayedProperty);
524 property->object = g_strdup (object_name);
525 property->name = g_strdup (prop->name);
526 property->value = g_strdup (prop->data);
527 builder->priv->delayed_properties =
528 g_slist_prepend (builder->priv->delayed_properties, property);
532 else if (!gtk_builder_value_from_string (builder, pspec,
533 prop->data, ¶meter.value, &error))
535 g_warning ("Failed to set property %s.%s to %s: %s",
536 g_type_name (object_type), prop->name, prop->data,
538 g_error_free (error);
543 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
544 g_array_append_val (*construct_parameters, parameter);
546 g_array_append_val (*parameters, parameter);
549 g_type_class_unref (oclass);
553 gtk_builder_get_internal_child (GtkBuilder *builder,
555 const gchar *childname,
565 info = (ObjectInfo*)((ChildInfo*)info->parent)->parent;
570 g_print ("Trying to get internal child %s from %s\n",
572 gtk_buildable_get_name (GTK_BUILDABLE (info->object))));
574 if (GTK_IS_BUILDABLE (info->object))
575 obj = gtk_buildable_get_internal_child (GTK_BUILDABLE (info->object),
584 GTK_BUILDER_ERROR_INVALID_VALUE,
585 "Unknown internal child: %s", childname);
591 _gtk_builder_construct (GtkBuilder *builder,
595 GArray *parameters, *construct_parameters;
599 GtkBuildableIface *iface;
600 gboolean custom_set_property;
601 GtkBuildable *buildable;
603 g_assert (info->class_name != NULL);
604 object_type = gtk_builder_get_type_from_name (builder, info->class_name);
605 if (object_type == G_TYPE_INVALID)
609 GTK_BUILDER_ERROR_INVALID_VALUE,
610 "Invalid object type `%s'",
615 gtk_builder_get_parameters (builder, object_type,
619 &construct_parameters);
621 if (info->constructor)
623 GObject *constructor;
625 constructor = gtk_builder_get_object (builder, info->constructor);
626 if (constructor == NULL)
630 GTK_BUILDER_ERROR_INVALID_VALUE,
631 "Unknown object constructor for %s: %s",
634 g_array_free (parameters, TRUE);
635 g_array_free (construct_parameters, TRUE);
638 obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
641 g_assert (obj != NULL);
642 if (construct_parameters->len)
643 g_warning ("Can't pass in construct-only parameters to %s", info->id);
645 else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
647 gchar *childname = ((ChildInfo*)info->parent)->internal_child;
648 obj = gtk_builder_get_internal_child (builder, info, childname, error);
651 g_array_free (parameters, TRUE);
652 g_array_free (construct_parameters, TRUE);
655 if (construct_parameters->len)
656 g_warning ("Can't pass in construct-only parameters to %s", childname);
661 obj = g_object_newv (object_type,
662 construct_parameters->len,
663 (GParameter *)construct_parameters->data);
665 /* No matter what, make sure we have a reference.
667 * If it's an initially unowned object, sink it.
668 * If it's not initially unowned then we have the reference already.
670 * In the case that this is a window it will be sunk already and
671 * this is effectively a call to g_object_ref(). That's what
674 if (G_IS_INITIALLY_UNOWNED (obj))
675 g_object_ref_sink (obj);
678 g_print ("created %s of type %s\n", info->id, info->class_name));
680 for (i = 0; i < construct_parameters->len; i++)
682 GParameter *param = &g_array_index (construct_parameters,
684 g_value_unset (¶m->value);
687 g_array_free (construct_parameters, TRUE);
689 custom_set_property = FALSE;
692 if (GTK_IS_BUILDABLE (obj))
694 buildable = GTK_BUILDABLE (obj);
695 iface = GTK_BUILDABLE_GET_IFACE (obj);
696 if (iface->set_buildable_property)
697 custom_set_property = TRUE;
700 for (i = 0; i < parameters->len; i++)
702 GParameter *param = &g_array_index (parameters, GParameter, i);
703 if (custom_set_property)
704 iface->set_buildable_property (buildable, builder, param->name, ¶m->value);
706 g_object_set_property (obj, param->name, ¶m->value);
709 if (gtk_get_debug_flags () & GTK_DEBUG_BUILDER)
711 gchar *str = g_strdup_value_contents ((const GValue*)¶m->value);
712 g_print ("set %s: %s = %s\n", info->id, param->name, str);
716 g_value_unset (¶m->value);
718 g_array_free (parameters, TRUE);
720 if (GTK_IS_BUILDABLE (obj))
721 gtk_buildable_set_name (buildable, info->id);
723 g_object_set_data_full (obj,
728 /* we already own a reference to obj. put it in the hash table. */
729 g_hash_table_insert (builder->priv->objects, g_strdup (info->id), obj);
736 _gtk_builder_add (GtkBuilder *builder,
737 ChildInfo *child_info)
742 /* Internal children are already added
743 * Also prevent us from being called twice.
746 child_info->internal_child ||
750 object = child_info->object;
754 if (!child_info->parent)
756 g_warning ("%s: Not adding, No parent",
757 gtk_buildable_get_name (GTK_BUILDABLE (object)));
761 g_assert (object != NULL);
763 parent = ((ObjectInfo*)child_info->parent)->object;
764 g_assert (GTK_IS_BUILDABLE (parent));
767 g_print ("adding %s to %s\n",
768 gtk_buildable_get_name (GTK_BUILDABLE (object)),
769 gtk_buildable_get_name (GTK_BUILDABLE (parent))));
771 gtk_buildable_add_child (GTK_BUILDABLE (parent), builder, object,
774 child_info->added = TRUE;
778 _gtk_builder_add_signals (GtkBuilder *builder,
781 builder->priv->signals = g_slist_concat (builder->priv->signals,
782 g_slist_copy (signals));
786 gtk_builder_apply_delayed_properties (GtkBuilder *builder)
789 DelayedProperty *property;
792 GObjectClass *oclass;
795 /* take the list over from the builder->priv.
797 * g_slist_reverse does not copy the list, so the list now
798 * belongs to us (and we free it at the end of this function).
800 props = g_slist_reverse (builder->priv->delayed_properties);
801 builder->priv->delayed_properties = NULL;
803 for (l = props; l; l = l->next)
805 property = (DelayedProperty*)l->data;
806 object = g_hash_table_lookup (builder->priv->objects, property->object);
807 g_assert (object != NULL);
809 object_type = G_OBJECT_TYPE (object);
810 g_assert (object_type != G_TYPE_INVALID);
812 oclass = g_type_class_ref (object_type);
813 g_assert (oclass != NULL);
815 pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
818 g_warning ("Unknown property: %s.%s", g_type_name (object_type),
824 obj = g_hash_table_lookup (builder->priv->objects, property->value);
826 g_warning ("No object called: %s", property->value);
828 g_object_set (object, property->name, obj, NULL);
830 g_free (property->value);
831 g_free (property->object);
832 g_free (property->name);
833 g_slice_free (DelayedProperty, property);
834 g_type_class_unref (oclass);
836 g_slist_free (props);
840 _gtk_builder_finish (GtkBuilder *builder)
842 gtk_builder_apply_delayed_properties (builder);
848 * Creates a new builder object.
850 * Return value: a new #GtkBuilder object
855 gtk_builder_new (void)
857 return g_object_new (GTK_TYPE_BUILDER, NULL);
861 * gtk_builder_add_from_file:
862 * @builder: a #GtkBuilder
863 * @filename: the name of the file to parse
864 * @error: (allow-none): return location for an error, or %NULL
866 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder
867 * UI definition</link> and merges it with the current contents of @builder.
869 * Upon errors 0 will be returned and @error will be assigned a
870 * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR
873 * Returns: A positive value on success, 0 if an error occurred
878 gtk_builder_add_from_file (GtkBuilder *builder,
879 const gchar *filename,
886 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
887 g_return_val_if_fail (filename != NULL, 0);
888 g_return_val_if_fail (error == NULL || *error == NULL, 0);
892 if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
894 g_propagate_error (error, tmp_error);
898 g_free (builder->priv->filename);
899 builder->priv->filename = g_strdup (filename);
901 _gtk_builder_parser_parse_buffer (builder, filename,
908 if (tmp_error != NULL)
910 g_propagate_error (error, tmp_error);
918 * gtk_builder_add_objects_from_file:
919 * @builder: a #GtkBuilder
920 * @filename: the name of the file to parse
921 * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
922 * @error: (allow-none): return location for an error, or %NULL
924 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder
925 * UI definition</link> building only the requested objects and merges
926 * them with the current contents of @builder.
928 * Upon errors 0 will be returned and @error will be assigned a
929 * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR
933 * If you are adding an object that depends on an object that is not
934 * its child (for instance a #GtkTreeView that depends on its
935 * #GtkTreeModel), you have to explicitely list all of them in @object_ids.
938 * Returns: A positive value on success, 0 if an error occurred
943 gtk_builder_add_objects_from_file (GtkBuilder *builder,
944 const gchar *filename,
952 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
953 g_return_val_if_fail (filename != NULL, 0);
954 g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
955 g_return_val_if_fail (error == NULL || *error == NULL, 0);
959 if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
961 g_propagate_error (error, tmp_error);
965 g_free (builder->priv->filename);
966 builder->priv->filename = g_strdup (filename);
968 _gtk_builder_parser_parse_buffer (builder, filename,
975 if (tmp_error != NULL)
977 g_propagate_error (error, tmp_error);
985 * gtk_builder_add_from_string:
986 * @builder: a #GtkBuilder
987 * @buffer: the string to parse
988 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
989 * @error: (allow-none): return location for an error, or %NULL
991 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder
992 * UI definition</link> and merges it with the current contents of @builder.
994 * Upon errors 0 will be returned and @error will be assigned a
995 * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
997 * Returns: A positive value on success, 0 if an error occurred
1002 gtk_builder_add_from_string (GtkBuilder *builder,
1003 const gchar *buffer,
1009 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1010 g_return_val_if_fail (buffer != NULL, 0);
1011 g_return_val_if_fail (error == NULL || *error == NULL, 0);
1015 g_free (builder->priv->filename);
1016 builder->priv->filename = g_strdup (".");
1018 _gtk_builder_parser_parse_buffer (builder, "<input>",
1022 if (tmp_error != NULL)
1024 g_propagate_error (error, tmp_error);
1032 * gtk_builder_add_objects_from_string:
1033 * @builder: a #GtkBuilder
1034 * @buffer: the string to parse
1035 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1036 * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
1037 * @error: (allow-none): return location for an error, or %NULL
1039 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder
1040 * UI definition</link> building only the requested objects and merges
1041 * them with the current contents of @builder.
1043 * Upon errors 0 will be returned and @error will be assigned a
1044 * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
1047 * If you are adding an object that depends on an object that is not
1048 * its child (for instance a #GtkTreeView that depends on its
1049 * #GtkTreeModel), you have to explicitely list all of them in @object_ids.
1052 * Returns: A positive value on success, 0 if an error occurred
1057 gtk_builder_add_objects_from_string (GtkBuilder *builder,
1058 const gchar *buffer,
1065 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1066 g_return_val_if_fail (buffer != NULL, 0);
1067 g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
1068 g_return_val_if_fail (error == NULL || *error == NULL, 0);
1072 g_free (builder->priv->filename);
1073 builder->priv->filename = g_strdup (".");
1075 _gtk_builder_parser_parse_buffer (builder, "<input>",
1080 if (tmp_error != NULL)
1082 g_propagate_error (error, tmp_error);
1090 * gtk_builder_get_object:
1091 * @builder: a #GtkBuilder
1092 * @name: name of object to get
1094 * Gets the object named @name. Note that this function does not
1095 * increment the reference count of the returned object.
1097 * Return value: (transfer none): the object named @name or %NULL if
1098 * it could not be found in the object tree.
1103 gtk_builder_get_object (GtkBuilder *builder,
1106 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1107 g_return_val_if_fail (name != NULL, NULL);
1109 return g_hash_table_lookup (builder->priv->objects, name);
1113 object_add_to_list (gchar *object_id,
1117 *list = g_slist_prepend (*list, object);
1121 * gtk_builder_get_objects:
1122 * @builder: a #GtkBuilder
1124 * Gets all objects that have been constructed by @builder. Note that
1125 * this function does not increment the reference counts of the returned
1128 * Return value: (element-type GObject) (transfer container): a newly-allocated #GSList containing all the objects
1129 * constructed by the #GtkBuilder instance. It should be freed by
1135 gtk_builder_get_objects (GtkBuilder *builder)
1137 GSList *objects = NULL;
1139 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1141 g_hash_table_foreach (builder->priv->objects, (GHFunc)object_add_to_list, &objects);
1143 return g_slist_reverse (objects);
1147 * gtk_builder_set_translation_domain:
1148 * @builder: a #GtkBuilder
1149 * @domain: (allow-none): the translation domain or %NULL
1151 * Sets the translation domain of @builder.
1152 * See #GtkBuilder:translation-domain.
1157 gtk_builder_set_translation_domain (GtkBuilder *builder,
1158 const gchar *domain)
1162 g_return_if_fail (GTK_IS_BUILDER (builder));
1164 new_domain = g_strdup (domain);
1165 g_free (builder->priv->domain);
1166 builder->priv->domain = new_domain;
1168 g_object_notify (G_OBJECT (builder), "translation-domain");
1172 * gtk_builder_get_translation_domain:
1173 * @builder: a #GtkBuilder
1175 * Gets the translation domain of @builder.
1177 * Return value: the translation domain. This string is owned
1178 * by the builder object and must not be modified or freed.
1183 gtk_builder_get_translation_domain (GtkBuilder *builder)
1185 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1187 return builder->priv->domain;
1196 gtk_builder_connect_signals_default (GtkBuilder *builder,
1198 const gchar *signal_name,
1199 const gchar *handler_name,
1200 GObject *connect_object,
1201 GConnectFlags flags,
1205 connect_args *args = (connect_args*)user_data;
1207 if (!g_module_symbol (args->module, handler_name, (gpointer)&func))
1209 g_warning ("Could not find signal handler '%s'", handler_name);
1214 g_signal_connect_object (object, signal_name, func, connect_object, flags);
1216 g_signal_connect_data (object, signal_name, func, args->data, NULL, flags);
1221 * gtk_builder_connect_signals:
1222 * @builder: a #GtkBuilder
1223 * @user_data: a pointer to a structure sent in as user data to all signals
1225 * This method is a simpler variation of gtk_builder_connect_signals_full().
1226 * It uses #GModule's introspective features (by opening the module %NULL)
1227 * to look at the application's symbol table. From here it tries to match
1228 * the signal handler names given in the interface description with
1229 * symbols in the application and connects the signals. Note that this
1230 * function can only be called once, subsequent calls will do nothing.
1232 * Note that this function will not work correctly if #GModule is not
1233 * supported on the platform.
1235 * When compiling applications for Windows, you must declare signal callbacks
1236 * with #G_MODULE_EXPORT, or they will not be put in the symbol table.
1237 * On Linux and Unices, this is not necessary; applications should instead
1238 * be compiled with the -Wl,--export-dynamic CFLAGS, and linked against
1239 * gmodule-export-2.0.
1244 gtk_builder_connect_signals (GtkBuilder *builder,
1249 g_return_if_fail (GTK_IS_BUILDER (builder));
1251 if (!g_module_supported ())
1252 g_error ("gtk_builder_connect_signals() requires working GModule");
1254 args = g_slice_new0 (connect_args);
1255 args->module = g_module_open (NULL, G_MODULE_BIND_LAZY);
1256 args->data = user_data;
1258 gtk_builder_connect_signals_full (builder,
1259 gtk_builder_connect_signals_default,
1261 g_module_close (args->module);
1263 g_slice_free (connect_args, args);
1267 * GtkBuilderConnectFunc:
1268 * @builder: a #GtkBuilder
1269 * @object: object to connect a signal to
1270 * @signal_name: name of the signal
1271 * @handler_name: name of the handler
1272 * @connect_object: a #GObject, if non-%NULL, use g_signal_connect_object()
1273 * @flags: #GConnectFlags to use
1274 * @user_data: user data
1276 * This is the signature of a function used to connect signals. It is used
1277 * by the gtk_builder_connect_signals() and gtk_builder_connect_signals_full()
1278 * methods. It is mainly intended for interpreted language bindings, but
1279 * could be useful where the programmer wants more control over the signal
1280 * connection process. Note that this function can only be called once,
1281 * subsequent calls will do nothing.
1287 * gtk_builder_connect_signals_full:
1288 * @builder: a #GtkBuilder
1289 * @func: (scope call): the function used to connect the signals
1290 * @user_data: arbitrary data that will be passed to the connection function
1292 * This function can be thought of the interpreted language binding
1293 * version of gtk_builder_connect_signals(), except that it does not
1294 * require GModule to function correctly.
1299 gtk_builder_connect_signals_full (GtkBuilder *builder,
1300 GtkBuilderConnectFunc func,
1305 GObject *connect_object;
1307 g_return_if_fail (GTK_IS_BUILDER (builder));
1308 g_return_if_fail (func != NULL);
1310 if (!builder->priv->signals)
1313 builder->priv->signals = g_slist_reverse (builder->priv->signals);
1314 for (l = builder->priv->signals; l; l = l->next)
1316 SignalInfo *signal = (SignalInfo*)l->data;
1318 g_assert (signal != NULL);
1319 g_assert (signal->name != NULL);
1321 object = g_hash_table_lookup (builder->priv->objects,
1322 signal->object_name);
1323 g_assert (object != NULL);
1325 connect_object = NULL;
1327 if (signal->connect_object_name)
1329 connect_object = g_hash_table_lookup (builder->priv->objects,
1330 signal->connect_object_name);
1331 if (!connect_object)
1332 g_warning ("Could not lookup object %s on signal %s of object %s",
1333 signal->connect_object_name, signal->name,
1334 signal->object_name);
1337 func (builder, object, signal->name, signal->handler,
1338 connect_object, signal->flags, user_data);
1341 g_slist_foreach (builder->priv->signals, (GFunc)_free_signal_info, NULL);
1342 g_slist_free (builder->priv->signals);
1343 builder->priv->signals = NULL;
1347 * gtk_builder_value_from_string:
1348 * @builder: a #GtkBuilder
1349 * @pspec: the #GParamSpec for the property
1350 * @string: the string representation of the value
1351 * @value: (out): the #GValue to store the result in
1352 * @error: (allow-none): return location for an error, or %NULL
1354 * This function demarshals a value from a string. This function
1355 * calls g_value_init() on the @value argument, so it need not be
1356 * initialised beforehand.
1358 * This function can handle char, uchar, boolean, int, uint, long,
1359 * ulong, enum, flags, float, double, string, #GdkColor, #GdkRGBA and
1360 * #GtkAdjustment type values. Support for #GtkWidget type values is
1363 * Upon errors %FALSE will be returned and @error will be assigned a
1364 * #GError from the #GTK_BUILDER_ERROR domain.
1366 * Returns: %TRUE on success
1371 gtk_builder_value_from_string (GtkBuilder *builder,
1373 const gchar *string,
1377 g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
1378 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1379 g_return_val_if_fail (string != NULL, FALSE);
1380 g_return_val_if_fail (value != NULL, FALSE);
1381 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1384 * GParamSpecUnichar has the internal type G_TYPE_UINT,
1385 * so we cannot handle this in the switch, do it separately
1387 if (G_IS_PARAM_SPEC_UNICHAR (pspec))
1390 g_value_init (value, G_TYPE_UINT);
1391 c = g_utf8_get_char_validated (string, strlen (string));
1393 g_value_set_uint (value, c);
1397 return gtk_builder_value_from_string_type (builder,
1398 G_PARAM_SPEC_VALUE_TYPE (pspec),
1399 string, value, error);
1403 * gtk_builder_value_from_string_type:
1404 * @builder: a #GtkBuilder
1405 * @type: the #GType of the value
1406 * @string: the string representation of the value
1407 * @value: (out): the #GValue to store the result in
1408 * @error: (allow-none): return location for an error, or %NULL
1410 * Like gtk_builder_value_from_string(), this function demarshals
1411 * a value from a string, but takes a #GType instead of #GParamSpec.
1412 * This function calls g_value_init() on the @value argument, so it
1413 * need not be initialised beforehand.
1415 * Upon errors %FALSE will be returned and @error will be assigned a
1416 * #GError from the #GTK_BUILDER_ERROR domain.
1418 * Returns: %TRUE on success
1423 gtk_builder_value_from_string_type (GtkBuilder *builder,
1425 const gchar *string,
1429 gboolean ret = TRUE;
1431 g_return_val_if_fail (type != G_TYPE_INVALID, FALSE);
1432 g_return_val_if_fail (string != NULL, FALSE);
1433 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1435 g_value_init (value, type);
1437 switch (G_TYPE_FUNDAMENTAL (type))
1440 g_value_set_char (value, string[0]);
1443 g_value_set_uchar (value, (guchar)string[0]);
1445 case G_TYPE_BOOLEAN:
1449 if (!_gtk_builder_boolean_from_string (string, &b, error))
1454 g_value_set_boolean (value, b);
1461 gchar *endptr = NULL;
1463 l = g_ascii_strtoll (string, &endptr, 0);
1464 if (errno || endptr == string)
1468 GTK_BUILDER_ERROR_INVALID_VALUE,
1469 "Could not parse integer `%s'",
1474 if (G_VALUE_HOLDS_INT (value))
1475 g_value_set_int (value, l);
1477 g_value_set_long (value, l);
1484 gchar *endptr = NULL;
1486 ul = g_ascii_strtoull (string, &endptr, 0);
1487 if (errno || endptr == string)
1491 GTK_BUILDER_ERROR_INVALID_VALUE,
1492 "Could not parse unsigned integer `%s'",
1497 if (G_VALUE_HOLDS_UINT (value))
1498 g_value_set_uint (value, ul);
1500 g_value_set_ulong (value, ul);
1506 if (!_gtk_builder_enum_from_string (type, string, &enum_value, error))
1511 g_value_set_enum (value, enum_value);
1518 if (!_gtk_builder_flags_from_string (type, string, &flags_value, error))
1523 g_value_set_flags (value, flags_value);
1530 gchar *endptr = NULL;
1532 d = g_ascii_strtod (string, &endptr);
1533 if (errno || endptr == string)
1537 GTK_BUILDER_ERROR_INVALID_VALUE,
1538 "Could not parse double `%s'",
1543 if (G_VALUE_HOLDS_FLOAT (value))
1544 g_value_set_float (value, d);
1546 g_value_set_double (value, d);
1550 g_value_set_string (value, string);
1553 if (G_VALUE_HOLDS (value, GDK_TYPE_COLOR))
1555 GdkColor color = { 0, };
1557 if (gdk_color_parse (string, &color))
1558 g_value_set_boxed (value, &color);
1563 GTK_BUILDER_ERROR_INVALID_VALUE,
1564 "Could not parse color `%s'",
1569 else if (G_VALUE_HOLDS (value, GDK_TYPE_RGBA))
1571 GdkRGBA rgba = { 0 };
1573 if (gdk_rgba_parse (&rgba, string))
1574 g_value_set_boxed (value, &rgba);
1579 GTK_BUILDER_ERROR_INVALID_VALUE,
1580 "Could not parse RGBA color '%s'",
1585 else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
1587 gchar **vector = g_strsplit (string, "\n", 0);
1588 g_value_take_boxed (value, vector);
1594 GTK_BUILDER_ERROR_INVALID_VALUE,
1595 "Could not parse '%s' as a %s",
1596 string, G_VALUE_TYPE_NAME (value));
1601 if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
1604 GError *tmp_error = NULL;
1607 if (gtk_builder_get_object (builder, string))
1611 GTK_BUILDER_ERROR_INVALID_VALUE,
1612 "Could not load image '%s': "
1613 " '%s' is already used as object id",
1618 filename = _gtk_builder_get_absolute_filename (builder, string);
1619 pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);
1623 GtkIconTheme *theme;
1625 g_warning ("Could not load image '%s': %s",
1626 string, tmp_error->message);
1627 g_error_free (tmp_error);
1629 /* fall back to a missing image */
1630 theme = gtk_icon_theme_get_default ();
1631 pixbuf = gtk_icon_theme_load_icon (theme,
1632 GTK_STOCK_MISSING_IMAGE,
1634 GTK_ICON_LOOKUP_USE_BUILTIN,
1640 g_value_set_object (value, pixbuf);
1641 g_object_unref (G_OBJECT (pixbuf));
1656 /* Catch unassigned error for object types as well as any unsupported types.
1657 * While parsing GtkBuilder; object types are deserialized
1658 * without calling gtk_builder_value_from_string_type().
1660 if (!ret && error && *error == NULL)
1663 GTK_BUILDER_ERROR_INVALID_VALUE,
1664 "Unsupported GType `%s'", g_type_name (type));
1670 _gtk_builder_enum_from_string (GType type,
1671 const gchar *string,
1681 g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
1682 g_return_val_if_fail (string != NULL, FALSE);
1688 value = g_ascii_strtoull (string, &endptr, 0);
1689 if (errno == 0 && endptr != string) /* parsed a number */
1690 *enum_value = value;
1693 eclass = g_type_class_ref (type);
1694 ev = g_enum_get_value_by_name (eclass, string);
1696 ev = g_enum_get_value_by_nick (eclass, string);
1699 *enum_value = ev->value;
1704 GTK_BUILDER_ERROR_INVALID_VALUE,
1705 "Could not parse enum: `%s'",
1710 g_type_class_unref (eclass);
1717 _gtk_builder_flags_from_string (GType type,
1718 const gchar *string,
1722 GFlagsClass *fclass;
1723 gchar *endptr, *prevptr;
1731 g_return_val_if_fail (G_TYPE_IS_FLAGS (type), FALSE);
1732 g_return_val_if_fail (string != 0, FALSE);
1738 value = g_ascii_strtoull (string, &endptr, 0);
1739 if (errno == 0 && endptr != string) /* parsed a number */
1740 *flags_value = value;
1743 fclass = g_type_class_ref (type);
1745 flagstr = g_strdup (string);
1746 for (value = i = j = 0; ; i++)
1749 eos = flagstr[i] == '\0';
1751 if (!eos && flagstr[i] != '|')
1755 endptr = &flagstr[i];
1759 flagstr[i++] = '\0';
1766 ch = g_utf8_get_char (flag);
1767 if (!g_unichar_isspace (ch))
1769 flag = g_utf8_next_char (flag);
1772 while (endptr > flag)
1774 prevptr = g_utf8_prev_char (endptr);
1775 ch = g_utf8_get_char (prevptr);
1776 if (!g_unichar_isspace (ch))
1784 fv = g_flags_get_value_by_name (fclass, flag);
1787 fv = g_flags_get_value_by_nick (fclass, flag);
1795 GTK_BUILDER_ERROR_INVALID_VALUE,
1796 "Unknown flag: `%s'",
1805 *flags_value = value;
1812 g_type_class_unref (fclass);
1819 * gtk_builder_get_type_from_name:
1820 * @builder: a #GtkBuilder
1821 * @type_name: type name to lookup
1823 * Looks up a type by name, using the virtual function that
1824 * #GtkBuilder has for that purpose. This is mainly used when
1825 * implementing the #GtkBuildable interface on a type.
1827 * Returns: the #GType found for @type_name or #G_TYPE_INVALID
1828 * if no type was found
1833 gtk_builder_get_type_from_name (GtkBuilder *builder,
1834 const gchar *type_name)
1836 g_return_val_if_fail (GTK_IS_BUILDER (builder), G_TYPE_INVALID);
1837 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1839 return GTK_BUILDER_GET_CLASS (builder)->get_type_from_name (builder, type_name);
1843 gtk_builder_error_quark (void)
1845 return g_quark_from_static_string ("gtk-builder-error-quark");
1849 _gtk_builder_get_absolute_filename (GtkBuilder *builder, const gchar *string)
1852 gchar *dirname = NULL;
1854 if (g_path_is_absolute (string))
1855 return g_strdup (string);
1857 if (builder->priv->filename &&
1858 strcmp (builder->priv->filename, ".") != 0)
1860 dirname = g_path_get_dirname (builder->priv->filename);
1862 if (strcmp (dirname, ".") == 0)
1865 dirname = g_get_current_dir ();
1869 dirname = g_get_current_dir ();
1871 filename = g_build_filename (dirname, string, NULL);