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, see <http://www.gnu.org/licenses/>.
25 #include "gtkbuilderprivate.h"
26 #include "gtkbuilder.h"
27 #include "gtkbuildable.h"
29 #include "gtkversion.h"
30 #include "gtktypebuiltins.h"
34 static void free_property_info (PropertyInfo *info);
35 static void free_object_info (ObjectInfo *info);
38 state_push (ParserData *data, gpointer info)
40 data->stack = g_slist_prepend (data->stack, info);
43 static inline gpointer
44 state_peek (ParserData *data)
49 return data->stack->data;
52 static inline gpointer
53 state_pop (ParserData *data)
60 old = data->stack->data;
61 data->stack = g_slist_delete_link (data->stack, data->stack);
64 #define state_peek_info(data, st) ((st*)state_peek(data))
65 #define state_pop_info(data, st) ((st*)state_pop(data))
68 error_missing_attribute (ParserData *data,
70 const gchar *attribute,
73 gint line_number, char_number;
75 g_markup_parse_context_get_position (data->ctx,
81 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
82 "%s:%d:%d <%s> requires attribute \"%s\"",
84 line_number, char_number, tag, attribute);
88 error_invalid_attribute (ParserData *data,
90 const gchar *attribute,
93 gint line_number, char_number;
95 g_markup_parse_context_get_position (data->ctx,
101 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
102 "%s:%d:%d '%s' is not a valid attribute of <%s>",
104 line_number, char_number, attribute, tag);
108 error_invalid_tag (ParserData *data,
110 const gchar *expected,
113 gint line_number, char_number;
115 g_markup_parse_context_get_position (data->ctx,
122 GTK_BUILDER_ERROR_INVALID_TAG,
123 "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
125 line_number, char_number, tag, expected);
129 GTK_BUILDER_ERROR_INVALID_TAG,
130 "%s:%d:%d '%s' is not a valid tag here",
132 line_number, char_number, tag);
136 _gtk_builder_boolean_from_string (const gchar *string,
140 gboolean retval = TRUE;
143 g_assert (string != NULL);
144 length = strlen (string);
148 else if (length == 1)
150 gchar c = g_ascii_tolower (string[0]);
151 if (c == 'y' || c == 't' || c == '1')
153 else if (c == 'n' || c == 'f' || c == '0')
160 gchar *lower = g_ascii_strdown (string, length);
162 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
164 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
174 GTK_BUILDER_ERROR_INVALID_VALUE,
175 "could not parse boolean `%s'",
182 builder_construct (ParserData *data,
183 ObjectInfo *object_info,
188 g_assert (object_info != NULL);
190 if (object_info->object)
191 return object_info->object;
193 object_info->properties = g_slist_reverse (object_info->properties);
195 object = _gtk_builder_construct (data->builder, object_info, error);
199 g_assert (G_IS_OBJECT (object));
201 object_info->object = object;
207 _get_type_by_symbol (const gchar* symbol)
209 static GModule *module = NULL;
214 module = g_module_open (NULL, 0);
216 if (!g_module_symbol (module, symbol, (gpointer)&func))
220 if (type == G_TYPE_INVALID)
223 return g_strdup (g_type_name (type));
227 parse_requires (ParserData *data,
228 const gchar *element_name,
230 const gchar **values,
233 RequiresInfo *req_info;
234 const gchar *library = NULL;
235 const gchar *version = NULL;
237 gint i, version_major = 0, version_minor = 0;
238 gint line_number, char_number;
240 g_markup_parse_context_get_position (data->ctx,
244 for (i = 0; names[i] != NULL; i++)
246 if (strcmp (names[i], "lib") == 0)
248 else if (strcmp (names[i], "version") == 0)
251 error_invalid_attribute (data, element_name, names[i], error);
254 if (!library || !version)
256 error_missing_attribute (data, element_name,
257 version ? "lib" : "version", error);
261 if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
265 GTK_BUILDER_ERROR_INVALID_VALUE,
266 "%s:%d:%d <%s> attribute has malformed value \"%s\"",
268 line_number, char_number, "version", version);
271 version_major = g_ascii_strtoll (split[0], NULL, 10);
272 version_minor = g_ascii_strtoll (split[1], NULL, 10);
275 req_info = g_slice_new0 (RequiresInfo);
276 req_info->library = g_strdup (library);
277 req_info->major = version_major;
278 req_info->minor = version_minor;
279 state_push (data, req_info);
280 req_info->tag.name = element_name;
284 is_requested_object (const gchar *object,
289 for (l = data->requested_objects; l; l = l->next)
291 if (strcmp (l->data, object) == 0)
299 parse_object (GMarkupParseContext *context,
301 const gchar *element_name,
303 const gchar **values,
306 ObjectInfo *object_info;
307 ChildInfo* child_info;
309 gchar *object_class = NULL;
310 gchar *object_id = NULL;
311 gchar *constructor = NULL;
314 child_info = state_peek_info (data, ChildInfo);
315 if (child_info && strcmp (child_info->tag.name, "object") == 0)
317 error_invalid_tag (data, element_name, NULL, error);
321 for (i = 0; names[i] != NULL; i++)
323 if (strcmp (names[i], "class") == 0)
324 object_class = g_strdup (values[i]);
325 else if (strcmp (names[i], "id") == 0)
326 object_id = g_strdup (values[i]);
327 else if (strcmp (names[i], "constructor") == 0)
328 constructor = g_strdup (values[i]);
329 else if (strcmp (names[i], "type-func") == 0)
331 /* Call the GType function, and return the name of the GType,
332 * it's guaranteed afterwards that g_type_from_name on the name
333 * will return our GType
335 object_class = _get_type_by_symbol (values[i]);
338 g_markup_parse_context_get_position (context, &line, NULL);
339 g_set_error (error, GTK_BUILDER_ERROR,
340 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
341 _("Invalid type function on line %d: '%s'"),
348 error_invalid_attribute (data, element_name, names[i], error);
355 error_missing_attribute (data, element_name, "class", error);
361 error_missing_attribute (data, element_name, "id", error);
365 ++data->cur_object_level;
367 /* check if we reached a requested object (if it is specified) */
368 if (data->requested_objects && !data->inside_requested_object)
370 if (is_requested_object (object_id, data))
372 data->requested_object_level = data->cur_object_level;
374 GTK_NOTE (BUILDER, g_print ("requested object \"%s\" found at level %d\n",
376 data->requested_object_level));
378 data->inside_requested_object = TRUE;
382 g_free (object_class);
384 g_free (constructor);
389 object_info = g_slice_new0 (ObjectInfo);
390 object_info->class_name = object_class;
391 object_info->id = object_id;
392 object_info->constructor = constructor;
393 state_push (data, object_info);
394 object_info->tag.name = element_name;
397 object_info->parent = (CommonInfo*)child_info;
399 g_markup_parse_context_get_position (context, &line, NULL);
400 line2 = GPOINTER_TO_INT (g_hash_table_lookup (data->object_ids, object_id));
403 g_set_error (error, GTK_BUILDER_ERROR,
404 GTK_BUILDER_ERROR_DUPLICATE_ID,
405 _("Duplicate object ID '%s' on line %d (previously on line %d)"),
406 object_id, line, line2);
411 g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
415 free_object_info (ObjectInfo *info)
417 /* Do not free the signal items, which GtkBuilder takes ownership of */
418 g_slist_free (info->signals);
419 g_slist_foreach (info->properties,
420 (GFunc)free_property_info, NULL);
421 g_slist_free (info->properties);
422 g_free (info->constructor);
423 g_free (info->class_name);
425 g_slice_free (ObjectInfo, info);
429 free_menu_info (MenuInfo *info)
432 g_hash_table_unref (info->objects);
433 g_slice_free (MenuInfo, info);
437 parse_child (ParserData *data,
438 const gchar *element_name,
440 const gchar **values,
444 ObjectInfo* object_info;
445 ChildInfo *child_info;
448 object_info = state_peek_info (data, ObjectInfo);
449 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
451 error_invalid_tag (data, element_name, NULL, error);
455 child_info = g_slice_new0 (ChildInfo);
456 state_push (data, child_info);
457 child_info->tag.name = element_name;
458 for (i = 0; names[i]; i++)
460 if (strcmp (names[i], "type") == 0)
461 child_info->type = g_strdup (values[i]);
462 else if (strcmp (names[i], "internal-child") == 0)
463 child_info->internal_child = g_strdup (values[i]);
465 error_invalid_attribute (data, element_name, names[i], error);
468 child_info->parent = (CommonInfo*)object_info;
470 object_info->object = builder_construct (data, object_info, error);
474 free_child_info (ChildInfo *info)
477 g_free (info->internal_child);
478 g_slice_free (ChildInfo, info);
482 parse_property (ParserData *data,
483 const gchar *element_name,
485 const gchar **values,
490 gchar *context = NULL;
491 gboolean translatable = FALSE;
492 ObjectInfo *object_info;
495 object_info = state_peek_info (data, ObjectInfo);
496 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
498 error_invalid_tag (data, element_name, NULL, error);
502 for (i = 0; names[i] != NULL; i++)
504 if (strcmp (names[i], "name") == 0)
505 name = g_strdelimit (g_strdup (values[i]), "_", '-');
506 else if (strcmp (names[i], "translatable") == 0)
508 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
512 else if (strcmp (names[i], "comments") == 0)
514 /* do nothing, comments are for translators */
516 else if (strcmp (names[i], "context") == 0)
518 context = g_strdup (values[i]);
522 error_invalid_attribute (data, element_name, names[i], error);
529 error_missing_attribute (data, element_name, "name", error);
533 info = g_slice_new0 (PropertyInfo);
535 info->translatable = translatable;
536 info->context = context;
537 info->text = g_string_new ("");
538 state_push (data, info);
540 info->tag.name = element_name;
544 free_property_info (PropertyInfo *info)
548 g_slice_free (PropertyInfo, info);
552 parse_signal (ParserData *data,
553 const gchar *element_name,
555 const gchar **values,
560 gchar *handler = NULL;
561 gchar *object = NULL;
562 gboolean after = FALSE;
563 gboolean swapped = FALSE;
564 gboolean swapped_set = FALSE;
565 ObjectInfo *object_info;
568 object_info = state_peek_info (data, ObjectInfo);
569 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
571 error_invalid_tag (data, element_name, NULL, error);
575 for (i = 0; names[i] != NULL; i++)
577 if (strcmp (names[i], "name") == 0)
578 name = g_strdup (values[i]);
579 else if (strcmp (names[i], "handler") == 0)
580 handler = g_strdup (values[i]);
581 else if (strcmp (names[i], "after") == 0)
583 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
586 else if (strcmp (names[i], "swapped") == 0)
588 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
592 else if (strcmp (names[i], "object") == 0)
593 object = g_strdup (values[i]);
594 else if (strcmp (names[i], "last_modification_time") == 0)
595 /* parse but ignore */
599 error_invalid_attribute (data, element_name, names[i], error);
606 error_missing_attribute (data, element_name, "name", error);
611 error_missing_attribute (data, element_name, "handler", error);
615 /* Swapped defaults to FALSE except when object is set */
616 if (object && !swapped_set)
619 info = g_slice_new0 (SignalInfo);
621 info->handler = handler;
623 info->flags |= G_CONNECT_AFTER;
625 info->flags |= G_CONNECT_SWAPPED;
626 info->connect_object_name = object;
627 state_push (data, info);
629 info->tag.name = element_name;
632 /* Called by GtkBuilder */
634 _free_signal_info (SignalInfo *info,
638 g_free (info->handler);
639 g_free (info->connect_object_name);
640 g_free (info->object_name);
641 g_slice_free (SignalInfo, info);
645 free_requires_info (RequiresInfo *info,
648 g_free (info->library);
649 g_slice_free (RequiresInfo, info);
653 parse_interface (ParserData *data,
654 const gchar *element_name,
656 const gchar **values,
661 for (i = 0; names[i] != NULL; i++)
663 if (strcmp (names[i], "domain") == 0)
667 if (strcmp (data->domain, values[i]) == 0)
670 g_warning ("%s: interface domain '%s' overrides "
671 "programically set domain '%s'",
677 g_free (data->domain);
680 data->domain = g_strdup (values[i]);
681 gtk_builder_set_translation_domain (data->builder, data->domain);
684 error_invalid_attribute (data, "interface", names[i], error);
689 create_subparser (GObject *object,
691 const gchar *element_name,
692 GMarkupParser *parser,
695 SubParser *subparser;
697 subparser = g_slice_new0 (SubParser);
698 subparser->object = object;
699 subparser->child = child;
700 subparser->tagname = g_strdup (element_name);
701 subparser->start = element_name;
702 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
703 subparser->data = user_data;
709 free_subparser (SubParser *subparser)
711 g_free (subparser->tagname);
712 g_slice_free (SubParser, subparser);
716 subparser_start (GMarkupParseContext *context,
717 const gchar *element_name,
719 const gchar **values,
723 SubParser *subparser = data->subparser;
725 if (!subparser->start &&
726 strcmp (element_name, subparser->tagname) == 0)
727 subparser->start = element_name;
729 if (subparser->start)
731 if (subparser->parser->start_element)
732 subparser->parser->start_element (context,
733 element_name, names, values,
734 subparser->data, error);
741 subparser_end (GMarkupParseContext *context,
742 const gchar *element_name,
746 if (data->subparser->parser->end_element)
747 data->subparser->parser->end_element (context, element_name,
748 data->subparser->data, error);
750 if (!strcmp (data->subparser->start, element_name) == 0)
753 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
755 data->subparser->child,
757 data->subparser->data);
758 g_free (data->subparser->parser);
760 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
761 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
764 free_subparser (data->subparser);
766 data->subparser = NULL;
770 parse_custom (GMarkupParseContext *context,
771 const gchar *element_name,
773 const gchar **values,
777 CommonInfo* parent_info;
778 GMarkupParser parser;
779 gpointer subparser_data;
783 parent_info = state_peek_info (data, CommonInfo);
787 if (strcmp (parent_info->tag.name, "object") == 0)
789 ObjectInfo* object_info = (ObjectInfo*)parent_info;
790 if (!object_info->object)
792 object_info->properties = g_slist_reverse (object_info->properties);
793 object_info->object = _gtk_builder_construct (data->builder,
796 if (!object_info->object)
797 return TRUE; /* A GError is already set */
799 g_assert (object_info->object);
800 object = object_info->object;
803 else if (strcmp (parent_info->tag.name, "child") == 0)
805 ChildInfo* child_info = (ChildInfo*)parent_info;
807 _gtk_builder_add (data->builder, child_info);
809 object = ((ObjectInfo*)child_info->parent)->object;
810 child = child_info->object;
815 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
823 data->subparser = create_subparser (object, child, element_name,
824 &parser, subparser_data);
826 if (parser.start_element)
827 parser.start_element (context,
828 element_name, names, values,
829 subparser_data, error);
834 start_element (GMarkupParseContext *context,
835 const gchar *element_name,
837 const gchar **values,
841 ParserData *data = (ParserData*)user_data;
843 #ifdef GTK_ENABLE_DEBUG
844 if (gtk_get_debug_flags () & GTK_DEBUG_BUILDER)
846 GString *tags = g_string_new ("");
848 for (i = 0; names[i]; i++)
849 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
853 g_string_insert_c (tags, 0, ' ');
854 g_string_truncate (tags, tags->len - 1);
856 g_print ("<%s%s>\n", element_name, tags->str);
857 g_string_free (tags, TRUE);
861 if (!data->last_element && strcmp (element_name, "interface") != 0)
863 g_set_error (error, GTK_BUILDER_ERROR,
864 GTK_BUILDER_ERROR_UNHANDLED_TAG,
865 _("Invalid root element: '%s'"),
869 data->last_element = element_name;
872 if (!subparser_start (context, element_name, names, values,
876 if (strcmp (element_name, "requires") == 0)
877 parse_requires (data, element_name, names, values, error);
878 else if (strcmp (element_name, "object") == 0)
879 parse_object (context, data, element_name, names, values, error);
880 else if (data->requested_objects && !data->inside_requested_object)
882 /* If outside a requested object, simply ignore this tag */
885 else if (strcmp (element_name, "child") == 0)
886 parse_child (data, element_name, names, values, error);
887 else if (strcmp (element_name, "property") == 0)
888 parse_property (data, element_name, names, values, error);
889 else if (strcmp (element_name, "signal") == 0)
890 parse_signal (data, element_name, names, values, error);
891 else if (strcmp (element_name, "interface") == 0)
892 parse_interface (data, element_name, names, values, error);
893 else if (strcmp (element_name, "menu") == 0)
894 _gtk_builder_menu_start (data, element_name, names, values, error);
895 else if (strcmp (element_name, "placeholder") == 0)
897 /* placeholder has no special treatmeant, but it needs an
898 * if clause to avoid an error below.
902 if (!parse_custom (context, element_name, names, values,
904 g_set_error (error, GTK_BUILDER_ERROR,
905 GTK_BUILDER_ERROR_UNHANDLED_TAG,
906 _("Unhandled tag: '%s'"),
911 _gtk_builder_parser_translate (const gchar *domain,
912 const gchar *context,
918 s = g_dpgettext2 (domain, context, text);
920 s = g_dgettext (domain, text);
925 /* Called for close tags </foo> */
927 end_element (GMarkupParseContext *context,
928 const gchar *element_name,
932 ParserData *data = (ParserData*)user_data;
934 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
936 if (data->subparser && data->subparser->start)
938 subparser_end (context, element_name, data, error);
942 if (strcmp (element_name, "requires") == 0)
944 RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
946 /* TODO: Allow third party widget developers to check thier
947 * required versions, possibly throw a signal allowing them
948 * to check thier library versions here.
950 if (!strcmp (req_info->library, "gtk+"))
952 if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
955 GTK_BUILDER_ERROR_VERSION_MISMATCH,
956 "%s: required %s version %d.%d, current version is %d.%d",
957 data->filename, req_info->library,
958 req_info->major, req_info->minor,
959 GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
961 free_requires_info (req_info, NULL);
963 else if (strcmp (element_name, "interface") == 0)
966 else if (data->requested_objects && !data->inside_requested_object)
968 /* If outside a requested object, simply ignore this tag */
971 else if (strcmp (element_name, "menu") == 0)
973 _gtk_builder_menu_end (data);
975 else if (strcmp (element_name, "object") == 0)
977 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
978 ChildInfo* child_info = state_peek_info (data, ChildInfo);
980 if (data->requested_objects && data->inside_requested_object &&
981 (data->cur_object_level == data->requested_object_level))
983 GTK_NOTE (BUILDER, g_print ("requested object end found at level %d\n",
984 data->requested_object_level));
986 data->inside_requested_object = FALSE;
989 --data->cur_object_level;
991 g_assert (data->cur_object_level >= 0);
993 object_info->object = builder_construct (data, object_info, error);
994 if (!object_info->object)
996 free_object_info (object_info);
1000 child_info->object = object_info->object;
1002 if (GTK_IS_BUILDABLE (object_info->object) &&
1003 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
1004 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
1005 _gtk_builder_add_signals (data->builder, object_info->signals);
1007 free_object_info (object_info);
1009 else if (strcmp (element_name, "property") == 0)
1011 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
1012 CommonInfo *info = state_peek_info (data, CommonInfo);
1014 /* Normal properties */
1015 if (strcmp (info->tag.name, "object") == 0)
1017 ObjectInfo *object_info = (ObjectInfo*)info;
1019 if (prop_info->translatable && prop_info->text->len)
1021 prop_info->data = _gtk_builder_parser_translate (data->domain,
1023 prop_info->text->str);
1024 g_string_free (prop_info->text, TRUE);
1028 prop_info->data = g_string_free (prop_info->text, FALSE);
1032 object_info->properties =
1033 g_slist_prepend (object_info->properties, prop_info);
1036 g_assert_not_reached ();
1038 else if (strcmp (element_name, "child") == 0)
1040 ChildInfo *child_info = state_pop_info (data, ChildInfo);
1042 _gtk_builder_add (data->builder, child_info);
1044 free_child_info (child_info);
1046 else if (strcmp (element_name, "signal") == 0)
1048 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
1049 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
1050 signal_info->object_name = g_strdup (object_info->id);
1051 object_info->signals =
1052 g_slist_prepend (object_info->signals, signal_info);
1054 else if (strcmp (element_name, "placeholder") == 0)
1059 g_assert_not_reached ();
1063 /* Called for character data */
1064 /* text is not nul-terminated */
1066 text (GMarkupParseContext *context,
1072 ParserData *data = (ParserData*)user_data;
1075 if (data->subparser && data->subparser->start)
1077 GError *tmp_error = NULL;
1079 if (data->subparser->parser->text)
1080 data->subparser->parser->text (context, text, text_len,
1081 data->subparser->data, &tmp_error);
1083 g_propagate_error (error, tmp_error);
1090 info = state_peek_info (data, CommonInfo);
1091 g_assert (info != NULL);
1093 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
1095 PropertyInfo *prop_info = (PropertyInfo*)info;
1097 g_string_append_len (prop_info->text, text, text_len);
1102 free_info (CommonInfo *info)
1104 if (strcmp (info->tag.name, "object") == 0)
1105 free_object_info ((ObjectInfo *)info);
1106 else if (strcmp (info->tag.name, "child") == 0)
1107 free_child_info ((ChildInfo *)info);
1108 else if (strcmp (info->tag.name, "property") == 0)
1109 free_property_info ((PropertyInfo *)info);
1110 else if (strcmp (info->tag.name, "signal") == 0)
1111 _free_signal_info ((SignalInfo *)info, NULL);
1112 else if (strcmp (info->tag.name, "requires") == 0)
1113 free_requires_info ((RequiresInfo *)info, NULL);
1114 else if (strcmp (info->tag.name, "menu") == 0)
1115 free_menu_info ((MenuInfo *)info);
1117 g_assert_not_reached ();
1120 static const GMarkupParser parser = {
1128 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
1129 const gchar *filename,
1130 const gchar *buffer,
1132 gchar **requested_objs,
1135 const gchar* domain;
1139 /* Store the original domain so that interface domain attribute can be
1140 * applied for the builder and the original domain can be restored after
1141 * parsing has finished. This allows subparsers to translate elements with
1142 * gtk_builder_get_translation_domain() without breaking the ABI or API
1144 domain = gtk_builder_get_translation_domain (builder);
1146 data = g_new0 (ParserData, 1);
1147 data->builder = builder;
1148 data->filename = filename;
1149 data->domain = g_strdup (domain);
1150 data->object_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
1151 (GDestroyNotify)g_free, NULL);
1153 data->requested_objects = NULL;
1158 data->inside_requested_object = FALSE;
1159 for (i = 0; requested_objs[i]; ++i)
1161 data->requested_objects = g_slist_prepend (data->requested_objects,
1162 g_strdup (requested_objs[i]));
1167 /* get all the objects */
1168 data->inside_requested_object = TRUE;
1171 data->ctx = g_markup_parse_context_new (&parser,
1172 G_MARKUP_TREAT_CDATA_AS_TEXT,
1175 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
1178 _gtk_builder_finish (builder);
1180 /* Custom parser_finished */
1181 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
1182 for (l = data->custom_finalizers; l; l = l->next)
1184 SubParser *sub = (SubParser*)l->data;
1186 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
1193 /* Common parser_finished, for all created objects */
1194 data->finalizers = g_slist_reverse (data->finalizers);
1195 for (l = data->finalizers; l; l = l->next)
1197 GtkBuildable *buildable = (GtkBuildable*)l->data;
1198 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1203 g_slist_foreach (data->stack, (GFunc)free_info, NULL);
1204 g_slist_free (data->stack);
1205 g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
1206 g_slist_free (data->custom_finalizers);
1207 g_slist_free (data->finalizers);
1208 g_slist_foreach (data->requested_objects, (GFunc) g_free, NULL);
1209 g_slist_free (data->requested_objects);
1210 g_free (data->domain);
1211 g_hash_table_destroy (data->object_ids);
1212 g_markup_parse_context_free (data->ctx);
1215 /* restore the original domain */
1216 gtk_builder_set_translation_domain (builder, domain);