2 * Copyright (C) 2006-2007 Async Open Source,
3 * Johan Dahlin <jdahlin@async.com.br>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include "gtkbuilderprivate.h"
28 #include "gtkbuilder.h"
29 #include "gtkbuildable.h"
31 #include "gtkversion.h"
32 #include "gtktypebuiltins.h"
36 static void free_property_info (PropertyInfo *info);
37 static void free_object_info (ObjectInfo *info);
40 state_push (ParserData *data, gpointer info)
42 data->stack = g_slist_prepend (data->stack, info);
45 static inline gpointer
46 state_peek (ParserData *data)
51 return data->stack->data;
54 static inline gpointer
55 state_pop (ParserData *data)
62 old = data->stack->data;
63 data->stack = g_slist_delete_link (data->stack, data->stack);
66 #define state_peek_info(data, st) ((st*)state_peek(data))
67 #define state_pop_info(data, st) ((st*)state_pop(data))
70 error_missing_attribute (ParserData *data,
72 const gchar *attribute,
75 gint line_number, char_number;
77 g_markup_parse_context_get_position (data->ctx,
83 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
84 "%s:%d:%d <%s> requires attribute \"%s\"",
86 line_number, char_number, tag, attribute);
90 error_invalid_attribute (ParserData *data,
92 const gchar *attribute,
95 gint line_number, char_number;
97 g_markup_parse_context_get_position (data->ctx,
103 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
104 "%s:%d:%d '%s' is not a valid attribute of <%s>",
106 line_number, char_number, attribute, tag);
110 error_invalid_tag (ParserData *data,
112 const gchar *expected,
115 gint line_number, char_number;
117 g_markup_parse_context_get_position (data->ctx,
124 GTK_BUILDER_ERROR_INVALID_TAG,
125 "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
127 line_number, char_number, tag, expected);
131 GTK_BUILDER_ERROR_INVALID_TAG,
132 "%s:%d:%d '%s' is not a valid tag here",
134 line_number, char_number, tag);
138 _gtk_builder_boolean_from_string (const gchar *string,
142 gboolean retval = TRUE;
145 g_assert (string != NULL);
146 length = strlen (string);
150 else if (length == 1)
152 gchar c = g_ascii_tolower (string[0]);
153 if (c == 'y' || c == 't' || c == '1')
155 else if (c == 'n' || c == 'f' || c == '0')
162 gchar *lower = g_ascii_strdown (string, length);
164 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
166 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
176 GTK_BUILDER_ERROR_INVALID_VALUE,
177 "could not parse boolean `%s'",
184 builder_construct (ParserData *data,
185 ObjectInfo *object_info,
190 g_assert (object_info != NULL);
192 if (object_info->object)
193 return object_info->object;
195 object_info->properties = g_slist_reverse (object_info->properties);
197 object = _gtk_builder_construct (data->builder, object_info, error);
201 g_assert (G_IS_OBJECT (object));
203 object_info->object = object;
209 _get_type_by_symbol (const gchar* symbol)
211 static GModule *module = NULL;
216 module = g_module_open (NULL, 0);
218 if (!g_module_symbol (module, symbol, (gpointer)&func))
222 if (type == G_TYPE_INVALID)
225 return g_strdup (g_type_name (type));
229 parse_requires (ParserData *data,
230 const gchar *element_name,
232 const gchar **values,
235 RequiresInfo *req_info;
236 const gchar *library = NULL;
237 const gchar *version = NULL;
239 gint i, version_major = 0, version_minor = 0;
240 gint line_number, char_number;
242 g_markup_parse_context_get_position (data->ctx,
246 for (i = 0; names[i] != NULL; i++)
248 if (strcmp (names[i], "lib") == 0)
250 else if (strcmp (names[i], "version") == 0)
253 error_invalid_attribute (data, element_name, names[i], error);
256 if (!library || !version)
258 error_missing_attribute (data, element_name,
259 version ? "lib" : "version", error);
263 if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
267 GTK_BUILDER_ERROR_INVALID_VALUE,
268 "%s:%d:%d <%s> attribute has malformed value \"%s\"",
270 line_number, char_number, "version", version);
273 version_major = g_ascii_strtoll (split[0], NULL, 10);
274 version_minor = g_ascii_strtoll (split[1], NULL, 10);
277 req_info = g_slice_new0 (RequiresInfo);
278 req_info->library = g_strdup (library);
279 req_info->major = version_major;
280 req_info->minor = version_minor;
281 state_push (data, req_info);
282 req_info->tag.name = element_name;
286 is_requested_object (const gchar *object,
291 for (l = data->requested_objects; l; l = l->next)
293 if (strcmp (l->data, object) == 0)
301 parse_object (GMarkupParseContext *context,
303 const gchar *element_name,
305 const gchar **values,
308 ObjectInfo *object_info;
309 ChildInfo* child_info;
311 gchar *object_class = NULL;
312 gchar *object_id = NULL;
313 gchar *constructor = NULL;
316 child_info = state_peek_info (data, ChildInfo);
317 if (child_info && strcmp (child_info->tag.name, "object") == 0)
319 error_invalid_tag (data, element_name, NULL, error);
323 for (i = 0; names[i] != NULL; i++)
325 if (strcmp (names[i], "class") == 0)
326 object_class = g_strdup (values[i]);
327 else if (strcmp (names[i], "id") == 0)
328 object_id = g_strdup (values[i]);
329 else if (strcmp (names[i], "constructor") == 0)
330 constructor = g_strdup (values[i]);
331 else if (strcmp (names[i], "type-func") == 0)
333 /* Call the GType function, and return the name of the GType,
334 * it's guaranteed afterwards that g_type_from_name on the name
335 * will return our GType
337 object_class = _get_type_by_symbol (values[i]);
340 g_markup_parse_context_get_position (context, &line, NULL);
341 g_set_error (error, GTK_BUILDER_ERROR,
342 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
343 _("Invalid type function on line %d: '%s'"),
350 error_invalid_attribute (data, element_name, names[i], error);
357 error_missing_attribute (data, element_name, "class", error);
363 error_missing_attribute (data, element_name, "id", error);
367 ++data->cur_object_level;
369 /* check if we reached a requested object (if it is specified) */
370 if (data->requested_objects && !data->inside_requested_object)
372 if (is_requested_object (object_id, data))
374 data->requested_object_level = data->cur_object_level;
376 GTK_NOTE (BUILDER, g_print ("requested object \"%s\" found at level %d\n",
378 data->requested_object_level));
380 data->inside_requested_object = TRUE;
384 g_free (object_class);
386 g_free (constructor);
391 object_info = g_slice_new0 (ObjectInfo);
392 object_info->class_name = object_class;
393 object_info->id = object_id;
394 object_info->constructor = constructor;
395 state_push (data, object_info);
396 object_info->tag.name = element_name;
399 object_info->parent = (CommonInfo*)child_info;
401 g_markup_parse_context_get_position (context, &line, NULL);
402 line2 = GPOINTER_TO_INT (g_hash_table_lookup (data->object_ids, object_id));
405 g_set_error (error, GTK_BUILDER_ERROR,
406 GTK_BUILDER_ERROR_DUPLICATE_ID,
407 _("Duplicate object ID '%s' on line %d (previously on line %d)"),
408 object_id, line, line2);
413 g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
417 free_object_info (ObjectInfo *info)
419 /* Do not free the signal items, which GtkBuilder takes ownership of */
420 g_slist_free (info->signals);
421 g_slist_foreach (info->properties,
422 (GFunc)free_property_info, NULL);
423 g_slist_free (info->properties);
424 g_free (info->constructor);
425 g_free (info->class_name);
427 g_slice_free (ObjectInfo, info);
431 free_menu_info (MenuInfo *info)
434 g_hash_table_unref (info->objects);
435 g_slice_free (MenuInfo, info);
439 parse_child (ParserData *data,
440 const gchar *element_name,
442 const gchar **values,
446 ObjectInfo* object_info;
447 ChildInfo *child_info;
450 object_info = state_peek_info (data, ObjectInfo);
451 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
453 error_invalid_tag (data, element_name, NULL, error);
457 child_info = g_slice_new0 (ChildInfo);
458 state_push (data, child_info);
459 child_info->tag.name = element_name;
460 for (i = 0; names[i]; i++)
462 if (strcmp (names[i], "type") == 0)
463 child_info->type = g_strdup (values[i]);
464 else if (strcmp (names[i], "internal-child") == 0)
465 child_info->internal_child = g_strdup (values[i]);
467 error_invalid_attribute (data, element_name, names[i], error);
470 child_info->parent = (CommonInfo*)object_info;
472 object_info->object = builder_construct (data, object_info, error);
476 free_child_info (ChildInfo *info)
479 g_free (info->internal_child);
480 g_slice_free (ChildInfo, info);
484 parse_property (ParserData *data,
485 const gchar *element_name,
487 const gchar **values,
492 gchar *context = NULL;
493 gboolean translatable = FALSE;
494 ObjectInfo *object_info;
497 object_info = state_peek_info (data, ObjectInfo);
498 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
500 error_invalid_tag (data, element_name, NULL, error);
504 for (i = 0; names[i] != NULL; i++)
506 if (strcmp (names[i], "name") == 0)
507 name = g_strdelimit (g_strdup (values[i]), "_", '-');
508 else if (strcmp (names[i], "translatable") == 0)
510 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
514 else if (strcmp (names[i], "comments") == 0)
516 /* do nothing, comments are for translators */
518 else if (strcmp (names[i], "context") == 0)
520 context = g_strdup (values[i]);
524 error_invalid_attribute (data, element_name, names[i], error);
531 error_missing_attribute (data, element_name, "name", error);
535 info = g_slice_new0 (PropertyInfo);
537 info->translatable = translatable;
538 info->context = context;
539 info->text = g_string_new ("");
540 state_push (data, info);
542 info->tag.name = element_name;
546 free_property_info (PropertyInfo *info)
550 g_slice_free (PropertyInfo, info);
554 parse_signal (ParserData *data,
555 const gchar *element_name,
557 const gchar **values,
562 gchar *handler = NULL;
563 gchar *object = NULL;
564 gboolean after = FALSE;
565 gboolean swapped = FALSE;
566 gboolean swapped_set = FALSE;
567 ObjectInfo *object_info;
570 object_info = state_peek_info (data, ObjectInfo);
571 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
573 error_invalid_tag (data, element_name, NULL, error);
577 for (i = 0; names[i] != NULL; i++)
579 if (strcmp (names[i], "name") == 0)
580 name = g_strdup (values[i]);
581 else if (strcmp (names[i], "handler") == 0)
582 handler = g_strdup (values[i]);
583 else if (strcmp (names[i], "after") == 0)
585 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
588 else if (strcmp (names[i], "swapped") == 0)
590 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
594 else if (strcmp (names[i], "object") == 0)
595 object = g_strdup (values[i]);
596 else if (strcmp (names[i], "last_modification_time") == 0)
597 /* parse but ignore */
601 error_invalid_attribute (data, element_name, names[i], error);
608 error_missing_attribute (data, element_name, "name", error);
613 error_missing_attribute (data, element_name, "handler", error);
617 /* Swapped defaults to FALSE except when object is set */
618 if (object && !swapped_set)
621 info = g_slice_new0 (SignalInfo);
623 info->handler = handler;
625 info->flags |= G_CONNECT_AFTER;
627 info->flags |= G_CONNECT_SWAPPED;
628 info->connect_object_name = object;
629 state_push (data, info);
631 info->tag.name = element_name;
634 /* Called by GtkBuilder */
636 _free_signal_info (SignalInfo *info,
640 g_free (info->handler);
641 g_free (info->connect_object_name);
642 g_free (info->object_name);
643 g_slice_free (SignalInfo, info);
647 _free_requires_info (RequiresInfo *info,
650 g_free (info->library);
651 g_slice_free (RequiresInfo, info);
655 parse_interface (ParserData *data,
656 const gchar *element_name,
658 const gchar **values,
663 for (i = 0; names[i] != NULL; i++)
665 if (strcmp (names[i], "domain") == 0)
669 if (strcmp (data->domain, values[i]) == 0)
672 g_warning ("%s: interface domain '%s' overrides "
673 "programically set domain '%s'",
679 g_free (data->domain);
682 data->domain = g_strdup (values[i]);
683 gtk_builder_set_translation_domain (data->builder, data->domain);
686 error_invalid_attribute (data, "interface", names[i], error);
691 create_subparser (GObject *object,
693 const gchar *element_name,
694 GMarkupParser *parser,
697 SubParser *subparser;
699 subparser = g_slice_new0 (SubParser);
700 subparser->object = object;
701 subparser->child = child;
702 subparser->tagname = g_strdup (element_name);
703 subparser->start = element_name;
704 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
705 subparser->data = user_data;
711 free_subparser (SubParser *subparser)
713 g_free (subparser->tagname);
714 g_slice_free (SubParser, subparser);
718 subparser_start (GMarkupParseContext *context,
719 const gchar *element_name,
721 const gchar **values,
725 SubParser *subparser = data->subparser;
727 if (!subparser->start &&
728 strcmp (element_name, subparser->tagname) == 0)
729 subparser->start = element_name;
731 if (subparser->start)
733 if (subparser->parser->start_element)
734 subparser->parser->start_element (context,
735 element_name, names, values,
736 subparser->data, error);
743 subparser_end (GMarkupParseContext *context,
744 const gchar *element_name,
748 if (data->subparser->parser->end_element)
749 data->subparser->parser->end_element (context, element_name,
750 data->subparser->data, error);
752 if (!strcmp (data->subparser->start, element_name) == 0)
755 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
757 data->subparser->child,
759 data->subparser->data);
760 g_free (data->subparser->parser);
762 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
763 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
766 free_subparser (data->subparser);
768 data->subparser = NULL;
772 parse_custom (GMarkupParseContext *context,
773 const gchar *element_name,
775 const gchar **values,
779 CommonInfo* parent_info;
780 GMarkupParser parser;
781 gpointer subparser_data;
785 parent_info = state_peek_info (data, CommonInfo);
789 if (strcmp (parent_info->tag.name, "object") == 0)
791 ObjectInfo* object_info = (ObjectInfo*)parent_info;
792 if (!object_info->object)
794 object_info->properties = g_slist_reverse (object_info->properties);
795 object_info->object = _gtk_builder_construct (data->builder,
798 if (!object_info->object)
799 return TRUE; /* A GError is already set */
801 g_assert (object_info->object);
802 object = object_info->object;
805 else if (strcmp (parent_info->tag.name, "child") == 0)
807 ChildInfo* child_info = (ChildInfo*)parent_info;
809 _gtk_builder_add (data->builder, child_info);
811 object = ((ObjectInfo*)child_info->parent)->object;
812 child = child_info->object;
817 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
825 data->subparser = create_subparser (object, child, element_name,
826 &parser, subparser_data);
828 if (parser.start_element)
829 parser.start_element (context,
830 element_name, names, values,
831 subparser_data, error);
836 start_element (GMarkupParseContext *context,
837 const gchar *element_name,
839 const gchar **values,
843 ParserData *data = (ParserData*)user_data;
845 #ifdef GTK_ENABLE_DEBUG
846 if (gtk_get_debug_flags () & GTK_DEBUG_BUILDER)
848 GString *tags = g_string_new ("");
850 for (i = 0; names[i]; i++)
851 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
855 g_string_insert_c (tags, 0, ' ');
856 g_string_truncate (tags, tags->len - 1);
858 g_print ("<%s%s>\n", element_name, tags->str);
859 g_string_free (tags, TRUE);
863 if (!data->last_element && strcmp (element_name, "interface") != 0)
865 g_set_error (error, GTK_BUILDER_ERROR,
866 GTK_BUILDER_ERROR_UNHANDLED_TAG,
867 _("Invalid root element: '%s'"),
871 data->last_element = element_name;
874 if (!subparser_start (context, element_name, names, values,
878 if (strcmp (element_name, "requires") == 0)
879 parse_requires (data, element_name, names, values, error);
880 else if (strcmp (element_name, "object") == 0)
881 parse_object (context, data, element_name, names, values, error);
882 else if (data->requested_objects && !data->inside_requested_object)
884 /* If outside a requested object, simply ignore this tag */
887 else if (strcmp (element_name, "child") == 0)
888 parse_child (data, element_name, names, values, error);
889 else if (strcmp (element_name, "property") == 0)
890 parse_property (data, element_name, names, values, error);
891 else if (strcmp (element_name, "signal") == 0)
892 parse_signal (data, element_name, names, values, error);
893 else if (strcmp (element_name, "interface") == 0)
894 parse_interface (data, element_name, names, values, error);
895 else if (strcmp (element_name, "menu") == 0)
896 _gtk_builder_menu_start (data, element_name, names, values, error);
897 else if (strcmp (element_name, "placeholder") == 0)
899 /* placeholder has no special treatmeant, but it needs an
900 * if clause to avoid an error below.
904 if (!parse_custom (context, element_name, names, values,
906 g_set_error (error, GTK_BUILDER_ERROR,
907 GTK_BUILDER_ERROR_UNHANDLED_TAG,
908 _("Unhandled tag: '%s'"),
913 _gtk_builder_parser_translate (const gchar *domain,
914 const gchar *context,
920 s = g_dpgettext2 (domain, context, text);
922 s = g_dgettext (domain, text);
927 /* Called for close tags </foo> */
929 end_element (GMarkupParseContext *context,
930 const gchar *element_name,
934 ParserData *data = (ParserData*)user_data;
936 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
938 if (data->subparser && data->subparser->start)
940 subparser_end (context, element_name, data, error);
944 if (strcmp (element_name, "requires") == 0)
946 RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
948 /* TODO: Allow third party widget developers to check thier
949 * required versions, possibly throw a signal allowing them
950 * to check thier library versions here.
952 if (!strcmp (req_info->library, "gtk+"))
954 if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
957 GTK_BUILDER_ERROR_VERSION_MISMATCH,
958 "%s: required %s version %d.%d, current version is %d.%d",
959 data->filename, req_info->library,
960 req_info->major, req_info->minor,
961 GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
963 _free_requires_info (req_info, NULL);
965 else if (strcmp (element_name, "interface") == 0)
968 else if (strcmp (element_name, "menu") == 0)
970 _gtk_builder_menu_end (data);
972 else if (data->requested_objects && !data->inside_requested_object)
974 /* If outside a requested object, simply ignore this tag */
977 else if (strcmp (element_name, "object") == 0)
979 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
980 ChildInfo* child_info = state_peek_info (data, ChildInfo);
982 if (data->requested_objects && data->inside_requested_object &&
983 (data->cur_object_level == data->requested_object_level))
985 GTK_NOTE (BUILDER, g_print ("requested object end found at level %d\n",
986 data->requested_object_level));
988 data->inside_requested_object = FALSE;
991 --data->cur_object_level;
993 g_assert (data->cur_object_level >= 0);
995 object_info->object = builder_construct (data, object_info, error);
996 if (!object_info->object)
998 free_object_info (object_info);
1002 child_info->object = object_info->object;
1004 if (GTK_IS_BUILDABLE (object_info->object) &&
1005 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
1006 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
1007 _gtk_builder_add_signals (data->builder, object_info->signals);
1009 free_object_info (object_info);
1011 else if (strcmp (element_name, "property") == 0)
1013 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
1014 CommonInfo *info = state_peek_info (data, CommonInfo);
1016 /* Normal properties */
1017 if (strcmp (info->tag.name, "object") == 0)
1019 ObjectInfo *object_info = (ObjectInfo*)info;
1021 if (prop_info->translatable && prop_info->text->len)
1023 prop_info->data = _gtk_builder_parser_translate (data->domain,
1025 prop_info->text->str);
1026 g_string_free (prop_info->text, TRUE);
1030 prop_info->data = g_string_free (prop_info->text, FALSE);
1034 object_info->properties =
1035 g_slist_prepend (object_info->properties, prop_info);
1038 g_assert_not_reached ();
1040 else if (strcmp (element_name, "child") == 0)
1042 ChildInfo *child_info = state_pop_info (data, ChildInfo);
1044 _gtk_builder_add (data->builder, child_info);
1046 free_child_info (child_info);
1048 else if (strcmp (element_name, "signal") == 0)
1050 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
1051 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
1052 signal_info->object_name = g_strdup (object_info->id);
1053 object_info->signals =
1054 g_slist_prepend (object_info->signals, signal_info);
1056 else if (strcmp (element_name, "placeholder") == 0)
1061 g_assert_not_reached ();
1065 /* Called for character data */
1066 /* text is not nul-terminated */
1068 text (GMarkupParseContext *context,
1074 ParserData *data = (ParserData*)user_data;
1077 if (data->subparser && data->subparser->start)
1079 GError *tmp_error = NULL;
1081 if (data->subparser->parser->text)
1082 data->subparser->parser->text (context, text, text_len,
1083 data->subparser->data, &tmp_error);
1085 g_propagate_error (error, tmp_error);
1092 info = state_peek_info (data, CommonInfo);
1093 g_assert (info != NULL);
1095 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
1097 PropertyInfo *prop_info = (PropertyInfo*)info;
1099 g_string_append_len (prop_info->text, text, text_len);
1104 free_info (CommonInfo *info)
1106 if (strcmp (info->tag.name, "object") == 0)
1107 free_object_info ((ObjectInfo *)info);
1108 else if (strcmp (info->tag.name, "child") == 0)
1109 free_child_info ((ChildInfo *)info);
1110 else if (strcmp (info->tag.name, "property") == 0)
1111 free_property_info ((PropertyInfo *)info);
1112 else if (strcmp (info->tag.name, "signal") == 0)
1113 _free_signal_info ((SignalInfo *)info, NULL);
1114 else if (strcmp (info->tag.name, "requires") == 0)
1115 _free_requires_info ((RequiresInfo *)info, NULL);
1116 else if (strcmp (info->tag.name, "menu") == 0)
1117 free_menu_info ((MenuInfo *)info);
1119 g_assert_not_reached ();
1122 static const GMarkupParser parser = {
1130 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
1131 const gchar *filename,
1132 const gchar *buffer,
1134 gchar **requested_objs,
1137 const gchar* domain;
1141 /* Store the original domain so that interface domain attribute can be
1142 * applied for the builder and the original domain can be restored after
1143 * parsing has finished. This allows subparsers to translate elements with
1144 * gtk_builder_get_translation_domain() without breaking the ABI or API
1146 domain = gtk_builder_get_translation_domain (builder);
1148 data = g_new0 (ParserData, 1);
1149 data->builder = builder;
1150 data->filename = filename;
1151 data->domain = g_strdup (domain);
1152 data->object_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
1153 (GDestroyNotify)g_free, NULL);
1155 data->requested_objects = NULL;
1160 data->inside_requested_object = FALSE;
1161 for (i = 0; requested_objs[i]; ++i)
1163 data->requested_objects = g_slist_prepend (data->requested_objects,
1164 g_strdup (requested_objs[i]));
1169 /* get all the objects */
1170 data->inside_requested_object = TRUE;
1173 data->ctx = g_markup_parse_context_new (&parser,
1174 G_MARKUP_TREAT_CDATA_AS_TEXT,
1177 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
1180 _gtk_builder_finish (builder);
1182 /* Custom parser_finished */
1183 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
1184 for (l = data->custom_finalizers; l; l = l->next)
1186 SubParser *sub = (SubParser*)l->data;
1188 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
1195 /* Common parser_finished, for all created objects */
1196 data->finalizers = g_slist_reverse (data->finalizers);
1197 for (l = data->finalizers; l; l = l->next)
1199 GtkBuildable *buildable = (GtkBuildable*)l->data;
1200 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1205 g_slist_foreach (data->stack, (GFunc)free_info, NULL);
1206 g_slist_free (data->stack);
1207 g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
1208 g_slist_free (data->custom_finalizers);
1209 g_slist_free (data->finalizers);
1210 g_slist_foreach (data->requested_objects, (GFunc) g_free, NULL);
1211 g_slist_free (data->requested_objects);
1212 g_free (data->domain);
1213 g_hash_table_destroy (data->object_ids);
1214 g_markup_parse_context_free (data->ctx);
1217 /* restore the original domain */
1218 gtk_builder_set_translation_domain (builder, domain);