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="GtkAssistant-BUILDER-UI">GtkAssistant</link>,
235 * <link linkend="GtkScale-BUILDER-UI">GtkScale</link>.
241 #include <errno.h> /* errno */
242 #include <stdlib.h> /* strtol, strtoul */
243 #include <string.h> /* strlen */
245 #include "gtkbuilder.h"
246 #include "gtkbuildable.h"
247 #include "gtkbuilderprivate.h"
248 #include "gtkdebug.h"
251 #include "gtkprivate.h"
252 #include "gtktypebuiltins.h"
253 #include "gtkwindow.h"
254 #include "gtkicontheme.h"
255 #include "gtkstock.h"
258 static void gtk_builder_class_init (GtkBuilderClass *klass);
259 static void gtk_builder_init (GtkBuilder *builder);
260 static void gtk_builder_finalize (GObject *object);
261 static void gtk_builder_set_property (GObject *object,
265 static void gtk_builder_get_property (GObject *object,
269 static GType gtk_builder_real_get_type_from_name (GtkBuilder *builder,
270 const gchar *type_name);
274 PROP_TRANSLATION_DOMAIN,
277 struct _GtkBuilderPrivate
281 GSList *delayed_properties;
286 G_DEFINE_TYPE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)
289 gtk_builder_class_init (GtkBuilderClass *klass)
291 GObjectClass *gobject_class;
293 gobject_class = G_OBJECT_CLASS (klass);
295 gobject_class->finalize = gtk_builder_finalize;
296 gobject_class->set_property = gtk_builder_set_property;
297 gobject_class->get_property = gtk_builder_get_property;
299 klass->get_type_from_name = gtk_builder_real_get_type_from_name;
302 * GtkBuilder:translation-domain:
304 * The translation domain used when translating property values that
305 * have been marked as translatable in interface descriptions.
306 * If the translation domain is %NULL, #GtkBuilder uses gettext(),
307 * otherwise g_dgettext().
311 g_object_class_install_property (gobject_class,
312 PROP_TRANSLATION_DOMAIN,
313 g_param_spec_string ("translation-domain",
314 P_("Translation Domain"),
315 P_("The translation domain used by gettext"),
317 GTK_PARAM_READWRITE));
319 g_type_class_add_private (gobject_class, sizeof (GtkBuilderPrivate));
323 gtk_builder_init (GtkBuilder *builder)
325 builder->priv = G_TYPE_INSTANCE_GET_PRIVATE (builder, GTK_TYPE_BUILDER,
327 builder->priv->domain = NULL;
328 builder->priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal,
329 g_free, g_object_unref);
334 * GObject virtual methods
338 gtk_builder_finalize (GObject *object)
340 GtkBuilderPrivate *priv = GTK_BUILDER (object)->priv;
342 g_free (priv->domain);
343 g_free (priv->filename);
345 g_hash_table_destroy (priv->objects);
347 g_slist_foreach (priv->signals, (GFunc) _free_signal_info, NULL);
348 g_slist_free (priv->signals);
350 G_OBJECT_CLASS (gtk_builder_parent_class)->finalize (object);
354 gtk_builder_set_property (GObject *object,
359 GtkBuilder *builder = GTK_BUILDER (object);
363 case PROP_TRANSLATION_DOMAIN:
364 gtk_builder_set_translation_domain (builder, g_value_get_string (value));
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gtk_builder_get_property (GObject *object,
378 GtkBuilder *builder = GTK_BUILDER (object);
382 case PROP_TRANSLATION_DOMAIN:
383 g_value_set_string (value, builder->priv->domain);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 * Try to map a type name to a _get_type function
396 * GtkWindow -> gtk_window_get_type
397 * GtkHBox -> gtk_hbox_get_type
398 * GtkUIManager -> gtk_ui_manager_get_type
402 _gtk_builder_resolve_type_lazily (const gchar *name)
404 static GModule *module = NULL;
406 GString *symbol_name = g_string_new ("");
409 GType gtype = G_TYPE_INVALID;
412 module = g_module_open (NULL, 0);
414 for (i = 0; name[i] != '\0'; i++)
417 /* skip if uppercase, first or previous is uppercase */
418 if ((c == g_ascii_toupper (c) &&
419 i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
420 (i > 2 && name[i] == g_ascii_toupper (name[i]) &&
421 name[i-1] == g_ascii_toupper (name[i-1]) &&
422 name[i-2] == g_ascii_toupper (name[i-2])))
423 g_string_append_c (symbol_name, '_');
424 g_string_append_c (symbol_name, g_ascii_tolower (c));
426 g_string_append (symbol_name, "_get_type");
428 symbol = g_string_free (symbol_name, FALSE);
430 if (g_module_symbol (module, symbol, (gpointer)&func))
439 * GtkBuilder virtual methods
443 gtk_builder_real_get_type_from_name (GtkBuilder *builder,
444 const gchar *type_name)
448 gtype = g_type_from_name (type_name);
449 if (gtype != G_TYPE_INVALID)
452 return _gtk_builder_resolve_type_lazily (type_name);
463 gtk_builder_get_parameters (GtkBuilder *builder,
465 const gchar *object_name,
468 GArray **construct_parameters)
472 GObjectClass *oclass;
473 DelayedProperty *property;
474 GError *error = NULL;
476 oclass = g_type_class_ref (object_type);
477 g_assert (oclass != NULL);
479 *parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
480 *construct_parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
482 for (l = properties; l; l = l->next)
484 PropertyInfo *prop = (PropertyInfo*)l->data;
485 GParameter parameter = { NULL };
487 pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
491 g_warning ("Unknown property: %s.%s",
492 g_type_name (object_type), prop->name);
496 parameter.name = prop->name;
498 if (G_IS_PARAM_SPEC_OBJECT (pspec) &&
499 (G_PARAM_SPEC_VALUE_TYPE (pspec) != GDK_TYPE_PIXBUF))
501 if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
504 object = gtk_builder_get_object (builder, prop->data);
507 g_warning ("Failed to get constuct only property "
508 "%s of %s with value `%s'",
509 prop->name, object_name, prop->data);
512 g_value_init (¶meter.value, G_OBJECT_TYPE (object));
513 g_value_set_object (¶meter.value, object);
517 property = g_slice_new (DelayedProperty);
518 property->object = g_strdup (object_name);
519 property->name = g_strdup (prop->name);
520 property->value = g_strdup (prop->data);
521 builder->priv->delayed_properties =
522 g_slist_prepend (builder->priv->delayed_properties, property);
527 else if (!gtk_builder_value_from_string (builder, pspec,
528 prop->data, ¶meter.value, &error))
530 g_warning ("Failed to set property %s.%s to %s: %s",
531 g_type_name (object_type), prop->name, prop->data,
533 g_error_free (error);
538 if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
539 g_array_append_val (*construct_parameters, parameter);
541 g_array_append_val (*parameters, parameter);
544 g_type_class_unref (oclass);
548 gtk_builder_get_internal_child (GtkBuilder *builder,
550 const gchar *childname,
560 info = (ObjectInfo*)((ChildInfo*)info->parent)->parent;
565 g_print ("Trying to get internal child %s from %s\n",
567 gtk_buildable_get_name (GTK_BUILDABLE (info->object))));
569 if (GTK_IS_BUILDABLE (info->object))
570 obj = gtk_buildable_get_internal_child (GTK_BUILDABLE (info->object),
579 GTK_BUILDER_ERROR_INVALID_VALUE,
580 "Unknown internal child: %s", childname);
586 _gtk_builder_construct (GtkBuilder *builder,
590 GArray *parameters, *construct_parameters;
594 GtkBuildableIface *iface;
595 gboolean custom_set_property;
596 GtkBuildable *buildable;
598 g_assert (info->class_name != NULL);
599 object_type = gtk_builder_get_type_from_name (builder, info->class_name);
600 if (object_type == G_TYPE_INVALID)
604 GTK_BUILDER_ERROR_INVALID_VALUE,
605 "Invalid object type `%s'",
610 gtk_builder_get_parameters (builder, object_type,
614 &construct_parameters);
616 if (info->constructor)
618 GObject *constructor;
620 constructor = gtk_builder_get_object (builder, info->constructor);
621 if (constructor == NULL)
625 GTK_BUILDER_ERROR_INVALID_VALUE,
626 "Unknown object constructor for %s: %s",
629 g_array_free (parameters, TRUE);
630 g_array_free (construct_parameters, TRUE);
633 obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
636 g_assert (obj != NULL);
637 if (construct_parameters->len)
638 g_warning ("Can't pass in construct-only parameters to %s", info->id);
640 else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
642 gchar *childname = ((ChildInfo*)info->parent)->internal_child;
643 obj = gtk_builder_get_internal_child (builder, info, childname, error);
646 g_array_free (parameters, TRUE);
647 g_array_free (construct_parameters, TRUE);
650 if (construct_parameters->len)
651 g_warning ("Can't pass in construct-only parameters to %s", childname);
656 obj = g_object_newv (object_type,
657 construct_parameters->len,
658 (GParameter *)construct_parameters->data);
660 /* No matter what, make sure we have a reference.
662 * If it's an initially unowned object, sink it.
663 * If it's not initially unowned then we have the reference already.
665 * In the case that this is a window it will be sunk already and
666 * this is effectively a call to g_object_ref(). That's what
669 if (G_IS_INITIALLY_UNOWNED (obj))
670 g_object_ref_sink (obj);
673 g_print ("created %s of type %s\n", info->id, info->class_name));
675 for (i = 0; i < construct_parameters->len; i++)
677 GParameter *param = &g_array_index (construct_parameters,
679 g_value_unset (¶m->value);
682 g_array_free (construct_parameters, TRUE);
684 custom_set_property = FALSE;
687 if (GTK_IS_BUILDABLE (obj))
689 buildable = GTK_BUILDABLE (obj);
690 iface = GTK_BUILDABLE_GET_IFACE (obj);
691 if (iface->set_buildable_property)
692 custom_set_property = TRUE;
695 for (i = 0; i < parameters->len; i++)
697 GParameter *param = &g_array_index (parameters, GParameter, i);
698 if (custom_set_property)
699 iface->set_buildable_property (buildable, builder, param->name, ¶m->value);
701 g_object_set_property (obj, param->name, ¶m->value);
704 if (gtk_get_debug_flags () & GTK_DEBUG_BUILDER)
706 gchar *str = g_strdup_value_contents ((const GValue*)¶m->value);
707 g_print ("set %s: %s = %s\n", info->id, param->name, str);
711 g_value_unset (¶m->value);
713 g_array_free (parameters, TRUE);
715 if (GTK_IS_BUILDABLE (obj))
716 gtk_buildable_set_name (buildable, info->id);
718 g_object_set_data_full (obj,
723 /* we already own a reference to obj. put it in the hash table. */
724 g_hash_table_insert (builder->priv->objects, g_strdup (info->id), obj);
731 _gtk_builder_add (GtkBuilder *builder,
732 ChildInfo *child_info)
737 /* Internal children are already added
738 * Also prevent us from being called twice.
741 child_info->internal_child ||
745 object = child_info->object;
749 if (!child_info->parent)
751 g_warning ("%s: Not adding, No parent",
752 gtk_buildable_get_name (GTK_BUILDABLE (object)));
756 g_assert (object != NULL);
758 parent = ((ObjectInfo*)child_info->parent)->object;
759 g_assert (GTK_IS_BUILDABLE (parent));
762 g_print ("adding %s to %s\n",
763 gtk_buildable_get_name (GTK_BUILDABLE (object)),
764 gtk_buildable_get_name (GTK_BUILDABLE (parent))));
766 gtk_buildable_add_child (GTK_BUILDABLE (parent), builder, object,
769 child_info->added = TRUE;
773 _gtk_builder_add_signals (GtkBuilder *builder,
776 builder->priv->signals = g_slist_concat (builder->priv->signals,
777 g_slist_copy (signals));
781 gtk_builder_apply_delayed_properties (GtkBuilder *builder)
784 DelayedProperty *property;
787 GObjectClass *oclass;
790 /* take the list over from the builder->priv.
792 * g_slist_reverse does not copy the list, so the list now
793 * belongs to us (and we free it at the end of this function).
795 props = g_slist_reverse (builder->priv->delayed_properties);
796 builder->priv->delayed_properties = NULL;
798 for (l = props; l; l = l->next)
800 property = (DelayedProperty*)l->data;
801 object = g_hash_table_lookup (builder->priv->objects, property->object);
802 g_assert (object != NULL);
804 object_type = G_OBJECT_TYPE (object);
805 g_assert (object_type != G_TYPE_INVALID);
807 oclass = g_type_class_ref (object_type);
808 g_assert (oclass != NULL);
810 pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
813 g_warning ("Unknown property: %s.%s", g_type_name (object_type),
819 obj = g_hash_table_lookup (builder->priv->objects, property->value);
821 g_warning ("No object called: %s", property->value);
823 g_object_set (object, property->name, obj, NULL);
825 g_free (property->value);
826 g_free (property->object);
827 g_free (property->name);
828 g_slice_free (DelayedProperty, property);
829 g_type_class_unref (oclass);
831 g_slist_free (props);
835 _gtk_builder_finish (GtkBuilder *builder)
837 gtk_builder_apply_delayed_properties (builder);
843 * Creates a new builder object.
845 * Return value: a new #GtkBuilder object
850 gtk_builder_new (void)
852 return g_object_new (GTK_TYPE_BUILDER, NULL);
856 * gtk_builder_add_from_file:
857 * @builder: a #GtkBuilder
858 * @filename: the name of the file to parse
859 * @error: (allow-none): return location for an error, or %NULL
861 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder
862 * UI definition</link> and merges it with the current contents of @builder.
864 * Upon errors 0 will be returned and @error will be assigned a
865 * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR
868 * Returns: A positive value on success, 0 if an error occurred
873 gtk_builder_add_from_file (GtkBuilder *builder,
874 const gchar *filename,
881 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
882 g_return_val_if_fail (filename != NULL, 0);
883 g_return_val_if_fail (error == NULL || *error == NULL, 0);
887 if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
889 g_propagate_error (error, tmp_error);
893 g_free (builder->priv->filename);
894 builder->priv->filename = g_strdup (filename);
896 _gtk_builder_parser_parse_buffer (builder, filename,
903 if (tmp_error != NULL)
905 g_propagate_error (error, tmp_error);
913 * gtk_builder_add_objects_from_file:
914 * @builder: a #GtkBuilder
915 * @filename: the name of the file to parse
916 * @object_ids: nul-terminated array of objects to build
917 * @error: (allow-none): return location for an error, or %NULL
919 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder
920 * UI definition</link> building only the requested objects and merges
921 * them with the current contents of @builder.
923 * Upon errors 0 will be returned and @error will be assigned a
924 * #GError from the #GTK_BUILDER_ERROR, #G_MARKUP_ERROR or #G_FILE_ERROR
928 * If you are adding an object that depends on an object that is not
929 * its child (for instance a #GtkTreeView that depends on its
930 * #GtkTreeModel), you have to explicitely list all of them in @object_ids.
933 * Returns: A positive value on success, 0 if an error occurred
938 gtk_builder_add_objects_from_file (GtkBuilder *builder,
939 const gchar *filename,
947 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
948 g_return_val_if_fail (filename != NULL, 0);
949 g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
950 g_return_val_if_fail (error == NULL || *error == NULL, 0);
954 if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
956 g_propagate_error (error, tmp_error);
960 g_free (builder->priv->filename);
961 builder->priv->filename = g_strdup (filename);
963 _gtk_builder_parser_parse_buffer (builder, filename,
970 if (tmp_error != NULL)
972 g_propagate_error (error, tmp_error);
980 * gtk_builder_add_from_string:
981 * @builder: a #GtkBuilder
982 * @buffer: the string to parse
983 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
984 * @error: (allow-none): return location for an error, or %NULL
986 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder
987 * UI definition</link> and merges it with the current contents of @builder.
989 * Upon errors 0 will be returned and @error will be assigned a
990 * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
992 * Returns: A positive value on success, 0 if an error occurred
997 gtk_builder_add_from_string (GtkBuilder *builder,
1004 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1005 g_return_val_if_fail (buffer != NULL, 0);
1006 g_return_val_if_fail (error == NULL || *error == NULL, 0);
1010 g_free (builder->priv->filename);
1011 builder->priv->filename = g_strdup (".");
1013 _gtk_builder_parser_parse_buffer (builder, "<input>",
1017 if (tmp_error != NULL)
1019 g_propagate_error (error, tmp_error);
1027 * gtk_builder_add_objects_from_string:
1028 * @builder: a #GtkBuilder
1029 * @buffer: the string to parse
1030 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
1031 * @object_ids: (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build
1032 * @error: (allow-none): return location for an error, or %NULL
1034 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder
1035 * UI definition</link> building only the requested objects and merges
1036 * them with the current contents of @builder.
1038 * Upon errors 0 will be returned and @error will be assigned a
1039 * #GError from the #GTK_BUILDER_ERROR or #G_MARKUP_ERROR domain.
1042 * If you are adding an object that depends on an object that is not
1043 * its child (for instance a #GtkTreeView that depends on its
1044 * #GtkTreeModel), you have to explicitely list all of them in @object_ids.
1047 * Returns: A positive value on success, 0 if an error occurred
1052 gtk_builder_add_objects_from_string (GtkBuilder *builder,
1053 const gchar *buffer,
1060 g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
1061 g_return_val_if_fail (buffer != NULL, 0);
1062 g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
1063 g_return_val_if_fail (error == NULL || *error == NULL, 0);
1067 g_free (builder->priv->filename);
1068 builder->priv->filename = g_strdup (".");
1070 _gtk_builder_parser_parse_buffer (builder, "<input>",
1075 if (tmp_error != NULL)
1077 g_propagate_error (error, tmp_error);
1085 * gtk_builder_get_object:
1086 * @builder: a #GtkBuilder
1087 * @name: name of object to get
1089 * Gets the object named @name. Note that this function does not
1090 * increment the reference count of the returned object.
1092 * Return value: (transfer none): the object named @name or %NULL if
1093 * it could not be found in the object tree.
1098 gtk_builder_get_object (GtkBuilder *builder,
1101 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1102 g_return_val_if_fail (name != NULL, NULL);
1104 return g_hash_table_lookup (builder->priv->objects, name);
1108 object_add_to_list (gchar *object_id,
1112 *list = g_slist_prepend (*list, object);
1116 * gtk_builder_get_objects:
1117 * @builder: a #GtkBuilder
1119 * Gets all objects that have been constructed by @builder. Note that
1120 * this function does not increment the reference counts of the returned
1123 * Return value: (element-type GObject) (transfer container): a newly-allocated #GSList containing all the objects
1124 * constructed by the #GtkBuilder instance. It should be freed by
1130 gtk_builder_get_objects (GtkBuilder *builder)
1132 GSList *objects = NULL;
1134 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1136 g_hash_table_foreach (builder->priv->objects, (GHFunc)object_add_to_list, &objects);
1138 return g_slist_reverse (objects);
1142 * gtk_builder_set_translation_domain:
1143 * @builder: a #GtkBuilder
1144 * @domain: (allow-none): the translation domain or %NULL
1146 * Sets the translation domain of @builder.
1147 * See #GtkBuilder:translation-domain.
1152 gtk_builder_set_translation_domain (GtkBuilder *builder,
1153 const gchar *domain)
1157 g_return_if_fail (GTK_IS_BUILDER (builder));
1159 new_domain = g_strdup (domain);
1160 g_free (builder->priv->domain);
1161 builder->priv->domain = new_domain;
1163 g_object_notify (G_OBJECT (builder), "translation-domain");
1167 * gtk_builder_get_translation_domain:
1168 * @builder: a #GtkBuilder
1170 * Gets the translation domain of @builder.
1172 * Return value: the translation domain. This string is owned
1173 * by the builder object and must not be modified or freed.
1178 gtk_builder_get_translation_domain (GtkBuilder *builder)
1180 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
1182 return builder->priv->domain;
1191 gtk_builder_connect_signals_default (GtkBuilder *builder,
1193 const gchar *signal_name,
1194 const gchar *handler_name,
1195 GObject *connect_object,
1196 GConnectFlags flags,
1200 connect_args *args = (connect_args*)user_data;
1202 if (!g_module_symbol (args->module, handler_name, (gpointer)&func))
1204 g_warning ("Could not find signal handler '%s'", handler_name);
1209 g_signal_connect_object (object, signal_name, func, connect_object, flags);
1211 g_signal_connect_data (object, signal_name, func, args->data, NULL, flags);
1216 * gtk_builder_connect_signals:
1217 * @builder: a #GtkBuilder
1218 * @user_data: a pointer to a structure sent in as user data to all signals
1220 * This method is a simpler variation of gtk_builder_connect_signals_full().
1221 * It uses #GModule's introspective features (by opening the module %NULL)
1222 * to look at the application's symbol table. From here it tries to match
1223 * the signal handler names given in the interface description with
1224 * symbols in the application and connects the signals.
1226 * Note that this function will not work correctly if #GModule is not
1227 * supported on the platform.
1229 * When compiling applications for Windows, you must declare signal callbacks
1230 * with #G_MODULE_EXPORT, or they will not be put in the symbol table.
1231 * On Linux and Unices, this is not necessary; applications should instead
1232 * be compiled with the -Wl,--export-dynamic CFLAGS, and linked against
1233 * gmodule-export-2.0.
1238 gtk_builder_connect_signals (GtkBuilder *builder,
1243 g_return_if_fail (GTK_IS_BUILDER (builder));
1245 if (!g_module_supported ())
1246 g_error ("gtk_builder_connect_signals() requires working GModule");
1248 args = g_slice_new0 (connect_args);
1249 args->module = g_module_open (NULL, G_MODULE_BIND_LAZY);
1250 args->data = user_data;
1252 gtk_builder_connect_signals_full (builder,
1253 gtk_builder_connect_signals_default,
1255 g_module_close (args->module);
1257 g_slice_free (connect_args, args);
1261 * GtkBuilderConnectFunc:
1262 * @builder: a #GtkBuilder
1263 * @object: object to connect a signal to
1264 * @signal_name: name of the signal
1265 * @handler_name: name of the handler
1266 * @connect_object: a #GObject, if non-%NULL, use g_signal_connect_object()
1267 * @flags: #GConnectFlags to use
1268 * @user_data: user data
1270 * This is the signature of a function used to connect signals. It is used
1271 * by the gtk_builder_connect_signals() and gtk_builder_connect_signals_full()
1272 * methods. It is mainly intended for interpreted language bindings, but
1273 * could be useful where the programmer wants more control over the signal
1274 * connection process.
1280 * gtk_builder_connect_signals_full:
1281 * @builder: a #GtkBuilder
1282 * @func: (scope call): the function used to connect the signals
1283 * @user_data: arbitrary data that will be passed to the connection function
1285 * This function can be thought of the interpreted language binding
1286 * version of gtk_builder_connect_signals(), except that it does not
1287 * require GModule to function correctly.
1292 gtk_builder_connect_signals_full (GtkBuilder *builder,
1293 GtkBuilderConnectFunc func,
1298 GObject *connect_object;
1300 g_return_if_fail (GTK_IS_BUILDER (builder));
1301 g_return_if_fail (func != NULL);
1303 if (!builder->priv->signals)
1306 builder->priv->signals = g_slist_reverse (builder->priv->signals);
1307 for (l = builder->priv->signals; l; l = l->next)
1309 SignalInfo *signal = (SignalInfo*)l->data;
1311 g_assert (signal != NULL);
1312 g_assert (signal->name != NULL);
1314 object = g_hash_table_lookup (builder->priv->objects,
1315 signal->object_name);
1316 g_assert (object != NULL);
1318 connect_object = NULL;
1320 if (signal->connect_object_name)
1322 connect_object = g_hash_table_lookup (builder->priv->objects,
1323 signal->connect_object_name);
1324 if (!connect_object)
1325 g_warning ("Could not lookup object %s on signal %s of object %s",
1326 signal->connect_object_name, signal->name,
1327 signal->object_name);
1330 func (builder, object, signal->name, signal->handler,
1331 connect_object, signal->flags, user_data);
1334 g_slist_foreach (builder->priv->signals, (GFunc)_free_signal_info, NULL);
1335 g_slist_free (builder->priv->signals);
1336 builder->priv->signals = NULL;
1340 * gtk_builder_value_from_string:
1341 * @builder: a #GtkBuilder
1342 * @pspec: the #GParamSpec for the property
1343 * @string: the string representation of the value
1344 * @value: the #GValue to store the result in
1345 * @error: (allow-none): return location for an error, or %NULL
1347 * This function demarshals a value from a string. This function
1348 * calls g_value_init() on the @value argument, so it need not be
1349 * initialised beforehand.
1351 * This function can handle char, uchar, boolean, int, uint, long,
1352 * ulong, enum, flags, float, double, string, #GdkColor and
1353 * #GtkAdjustment type values. Support for #GtkWidget type values is
1356 * Upon errors %FALSE will be returned and @error will be assigned a
1357 * #GError from the #GTK_BUILDER_ERROR domain.
1359 * Returns: %TRUE on success
1364 gtk_builder_value_from_string (GtkBuilder *builder,
1366 const gchar *string,
1370 g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
1371 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1372 g_return_val_if_fail (string != NULL, FALSE);
1373 g_return_val_if_fail (value != NULL, FALSE);
1374 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1377 * GParamSpecUnichar has the internal type G_TYPE_UINT,
1378 * so we cannot handle this in the switch, do it separately
1380 if (G_IS_PARAM_SPEC_UNICHAR (pspec))
1383 g_value_init (value, G_TYPE_UINT);
1384 c = g_utf8_get_char_validated (string, strlen (string));
1386 g_value_set_uint (value, c);
1390 return gtk_builder_value_from_string_type (builder,
1391 G_PARAM_SPEC_VALUE_TYPE (pspec),
1392 string, value, error);
1396 * gtk_builder_value_from_string_type:
1397 * @builder: a #GtkBuilder
1398 * @type: the #GType of the value
1399 * @string: the string representation of the value
1400 * @value: the #GValue to store the result in
1401 * @error: (allow-none): return location for an error, or %NULL
1403 * Like gtk_builder_value_from_string(), this function demarshals
1404 * a value from a string, but takes a #GType instead of #GParamSpec.
1405 * This function calls g_value_init() on the @value argument, so it
1406 * need not be initialised beforehand.
1408 * Upon errors %FALSE will be returned and @error will be assigned a
1409 * #GError from the #GTK_BUILDER_ERROR domain.
1411 * Returns: %TRUE on success
1416 gtk_builder_value_from_string_type (GtkBuilder *builder,
1418 const gchar *string,
1422 gboolean ret = TRUE;
1424 g_return_val_if_fail (type != G_TYPE_INVALID, FALSE);
1425 g_return_val_if_fail (string != NULL, FALSE);
1426 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1428 g_value_init (value, type);
1430 switch (G_TYPE_FUNDAMENTAL (type))
1433 g_value_set_char (value, string[0]);
1436 g_value_set_uchar (value, (guchar)string[0]);
1438 case G_TYPE_BOOLEAN:
1442 if (!_gtk_builder_boolean_from_string (string, &b, error))
1447 g_value_set_boolean (value, b);
1456 l = strtol (string, &endptr, 0);
1457 if (errno || endptr == string)
1461 GTK_BUILDER_ERROR_INVALID_VALUE,
1462 "Could not parse integer `%s'",
1467 if (G_VALUE_HOLDS_INT (value))
1468 g_value_set_int (value, l);
1470 g_value_set_long (value, l);
1479 ul = strtoul (string, &endptr, 0);
1480 if (errno || endptr == string)
1484 GTK_BUILDER_ERROR_INVALID_VALUE,
1485 "Could not parse unsigned integer `%s'",
1490 if (G_VALUE_HOLDS_UINT (value))
1491 g_value_set_uint (value, ul);
1493 g_value_set_ulong (value, ul);
1499 if (!_gtk_builder_enum_from_string (type, string, &enum_value, error))
1504 g_value_set_enum (value, enum_value);
1511 if (!_gtk_builder_flags_from_string (type, string, &flags_value, error))
1516 g_value_set_flags (value, flags_value);
1525 d = g_ascii_strtod (string, &endptr);
1526 if (errno || endptr == string)
1530 GTK_BUILDER_ERROR_INVALID_VALUE,
1531 "Could not parse double `%s'",
1536 if (G_VALUE_HOLDS_FLOAT (value))
1537 g_value_set_float (value, d);
1539 g_value_set_double (value, d);
1543 g_value_set_string (value, string);
1546 if (G_VALUE_HOLDS (value, GDK_TYPE_COLOR))
1548 GdkColor color = { 0, };
1550 if (gdk_color_parse (string, &color))
1551 g_value_set_boxed (value, &color);
1556 GTK_BUILDER_ERROR_INVALID_VALUE,
1557 "Could not parse color `%s'",
1562 else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
1564 gchar **vector = g_strsplit (string, "\n", 0);
1565 g_value_take_boxed (value, vector);
1571 GTK_BUILDER_ERROR_INVALID_VALUE,
1572 "Could not parse '%s' as a %s",
1573 string, G_VALUE_TYPE_NAME (value));
1578 if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
1581 GError *tmp_error = NULL;
1584 if (gtk_builder_get_object (builder, string))
1588 GTK_BUILDER_ERROR_INVALID_VALUE,
1589 "Could not load image '%s': "
1590 " '%s' is already used as object id",
1595 filename = _gtk_builder_get_absolute_filename (builder, string);
1596 pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);
1600 GtkIconTheme *theme;
1602 g_warning ("Could not load image '%s': %s",
1603 string, tmp_error->message);
1604 g_error_free (tmp_error);
1606 /* fall back to a missing image */
1607 theme = gtk_icon_theme_get_default ();
1608 pixbuf = gtk_icon_theme_load_icon (theme,
1609 GTK_STOCK_MISSING_IMAGE,
1611 GTK_ICON_LOOKUP_USE_BUILTIN,
1617 g_value_set_object (value, pixbuf);
1618 g_object_unref (G_OBJECT (pixbuf));
1633 /* Catch unassigned error for object types as well as any unsupported types.
1634 * While parsing GtkBuilder; object types are deserialized
1635 * without calling gtk_builder_value_from_string_type().
1637 if (!ret && error && *error == NULL)
1640 GTK_BUILDER_ERROR_INVALID_VALUE,
1641 "Unsupported GType `%s'", g_type_name (type));
1647 _gtk_builder_enum_from_string (GType type,
1648 const gchar *string,
1658 g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
1659 g_return_val_if_fail (string != NULL, FALSE);
1663 value = strtoul (string, &endptr, 0);
1664 if (endptr != string) /* parsed a number */
1665 *enum_value = value;
1668 eclass = g_type_class_ref (type);
1669 ev = g_enum_get_value_by_name (eclass, string);
1671 ev = g_enum_get_value_by_nick (eclass, string);
1674 *enum_value = ev->value;
1679 GTK_BUILDER_ERROR_INVALID_VALUE,
1680 "Could not parse enum: `%s'",
1685 g_type_class_unref (eclass);
1692 _gtk_builder_flags_from_string (GType type,
1693 const gchar *string,
1697 GFlagsClass *fclass;
1698 gchar *endptr, *prevptr;
1706 g_return_val_if_fail (G_TYPE_IS_FLAGS (type), FALSE);
1707 g_return_val_if_fail (string != 0, FALSE);
1711 value = strtoul (string, &endptr, 0);
1712 if (endptr != string) /* parsed a number */
1713 *flags_value = value;
1716 fclass = g_type_class_ref (type);
1718 flagstr = g_strdup (string);
1719 for (value = i = j = 0; ; i++)
1722 eos = flagstr[i] == '\0';
1724 if (!eos && flagstr[i] != '|')
1728 endptr = &flagstr[i];
1732 flagstr[i++] = '\0';
1739 ch = g_utf8_get_char (flag);
1740 if (!g_unichar_isspace (ch))
1742 flag = g_utf8_next_char (flag);
1745 while (endptr > flag)
1747 prevptr = g_utf8_prev_char (endptr);
1748 ch = g_utf8_get_char (prevptr);
1749 if (!g_unichar_isspace (ch))
1757 fv = g_flags_get_value_by_name (fclass, flag);
1760 fv = g_flags_get_value_by_nick (fclass, flag);
1768 GTK_BUILDER_ERROR_INVALID_VALUE,
1769 "Unknown flag: `%s'",
1778 *flags_value = value;
1785 g_type_class_unref (fclass);
1792 * gtk_builder_get_type_from_name:
1793 * @builder: a #GtkBuilder
1794 * @type_name: type name to lookup
1796 * Looks up a type by name, using the virtual function that
1797 * #GtkBuilder has for that purpose. This is mainly used when
1798 * implementing the #GtkBuildable interface on a type.
1800 * Returns: the #GType found for @type_name or #G_TYPE_INVALID
1801 * if no type was found
1806 gtk_builder_get_type_from_name (GtkBuilder *builder,
1807 const gchar *type_name)
1809 g_return_val_if_fail (GTK_IS_BUILDER (builder), G_TYPE_INVALID);
1810 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1812 return GTK_BUILDER_GET_CLASS (builder)->get_type_from_name (builder, type_name);
1816 gtk_builder_error_quark (void)
1818 return g_quark_from_static_string ("gtk-builder-error-quark");
1822 _gtk_builder_get_absolute_filename (GtkBuilder *builder, const gchar *string)
1825 gchar *dirname = NULL;
1827 if (g_path_is_absolute (string))
1828 return g_strdup (string);
1830 if (builder->priv->filename &&
1831 strcmp (builder->priv->filename, ".") != 0)
1833 dirname = g_path_get_dirname (builder->priv->filename);
1835 if (strcmp (dirname, ".") == 0)
1838 dirname = g_get_current_dir ();
1842 dirname = g_get_current_dir ();
1844 filename = g_build_filename (dirname, string, NULL);