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.
26 #include "gtktypeutils.h"
27 #include "gtkbuilderprivate.h"
28 #include "gtkbuilder.h"
29 #include "gtkbuildable.h"
31 #include "gtkversion.h"
32 #include "gtktypeutils.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 parse_object (ParserData *data,
287 const gchar *element_name,
289 const gchar **values,
292 ObjectInfo *object_info;
293 ChildInfo* child_info;
295 gchar *object_class = NULL;
296 gchar *object_id = NULL;
297 gchar *constructor = NULL;
299 child_info = state_peek_info (data, ChildInfo);
300 if (child_info && strcmp (child_info->tag.name, "object") == 0)
302 error_invalid_tag (data, element_name, NULL, error);
306 for (i = 0; names[i] != NULL; i++)
308 if (strcmp (names[i], "class") == 0)
309 object_class = g_strdup (values[i]);
310 else if (strcmp (names[i], "id") == 0)
311 object_id = g_strdup (values[i]);
312 else if (strcmp (names[i], "constructor") == 0)
313 constructor = g_strdup (values[i]);
314 else if (strcmp (names[i], "type-func") == 0)
316 /* Call the GType function, and return the name of the GType,
317 * it's guaranteed afterwards that g_type_from_name on the name
318 * will return our GType
320 object_class = _get_type_by_symbol (values[i]);
323 g_set_error (error, GTK_BUILDER_ERROR,
324 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
325 _("Invalid type function: `%s'"),
332 error_invalid_attribute (data, element_name, names[i], error);
339 error_missing_attribute (data, element_name, "class", error);
345 error_missing_attribute (data, element_name, "id", error);
349 object_info = g_slice_new0 (ObjectInfo);
350 object_info->class_name = object_class;
351 object_info->id = object_id;
352 object_info->constructor = constructor;
353 state_push (data, object_info);
354 object_info->tag.name = element_name;
357 object_info->parent = (CommonInfo*)child_info;
361 free_object_info (ObjectInfo *info)
363 /* Do not free the signal items, which GtkBuilder takes ownership of */
364 g_slist_free (info->signals);
365 g_slist_foreach (info->properties,
366 (GFunc)free_property_info, NULL);
367 g_slist_free (info->properties);
368 g_free (info->constructor);
369 g_free (info->class_name);
371 g_slice_free (ObjectInfo, info);
375 parse_child (ParserData *data,
376 const gchar *element_name,
378 const gchar **values,
382 ObjectInfo* object_info;
383 ChildInfo *child_info;
386 object_info = state_peek_info (data, ObjectInfo);
387 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
389 error_invalid_tag (data, element_name, NULL, error);
393 child_info = g_slice_new0 (ChildInfo);
394 state_push (data, child_info);
395 child_info->tag.name = element_name;
396 for (i = 0; names[i]; i++)
398 if (strcmp (names[i], "type") == 0)
399 child_info->type = g_strdup (values[i]);
400 else if (strcmp (names[i], "internal-child") == 0)
401 child_info->internal_child = g_strdup (values[i]);
403 error_invalid_attribute (data, element_name, names[i], error);
406 child_info->parent = (CommonInfo*)object_info;
408 object_info->object = builder_construct (data, object_info, error);
412 free_child_info (ChildInfo *info)
415 g_free (info->internal_child);
416 g_slice_free (ChildInfo, info);
420 parse_property (ParserData *data,
421 const gchar *element_name,
423 const gchar **values,
428 gchar *context = NULL;
429 gboolean translatable = FALSE;
430 ObjectInfo *object_info;
433 object_info = state_peek_info (data, ObjectInfo);
434 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
436 error_invalid_tag (data, element_name, NULL, error);
440 for (i = 0; names[i] != NULL; i++)
442 if (strcmp (names[i], "name") == 0)
443 name = g_strdelimit (g_strdup (values[i]), "_", '-');
444 else if (strcmp (names[i], "translatable") == 0)
446 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
450 else if (strcmp (names[i], "comments") == 0)
452 /* do nothing, comments are for translators */
454 else if (strcmp (names[i], "context") == 0)
456 context = g_strdup (values[i]);
460 error_invalid_attribute (data, element_name, names[i], error);
467 error_missing_attribute (data, element_name, "name", error);
471 info = g_slice_new0 (PropertyInfo);
473 info->translatable = translatable;
474 info->context = context;
475 info->text = g_string_new ("");
476 state_push (data, info);
478 info->tag.name = element_name;
482 free_property_info (PropertyInfo *info)
486 g_slice_free (PropertyInfo, info);
490 parse_signal (ParserData *data,
491 const gchar *element_name,
493 const gchar **values,
498 gchar *handler = NULL;
499 gchar *object = NULL;
500 gboolean after = FALSE;
501 gboolean swapped = FALSE;
502 gboolean swapped_set = FALSE;
503 ObjectInfo *object_info;
506 object_info = state_peek_info (data, ObjectInfo);
507 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
509 error_invalid_tag (data, element_name, NULL, error);
513 for (i = 0; names[i] != NULL; i++)
515 if (strcmp (names[i], "name") == 0)
516 name = g_strdup (values[i]);
517 else if (strcmp (names[i], "handler") == 0)
518 handler = g_strdup (values[i]);
519 else if (strcmp (names[i], "after") == 0)
521 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
524 else if (strcmp (names[i], "swapped") == 0)
526 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
530 else if (strcmp (names[i], "object") == 0)
531 object = g_strdup (values[i]);
532 else if (strcmp (names[i], "last_modification_time") == 0)
533 /* parse but ignore */
537 error_invalid_attribute (data, element_name, names[i], error);
544 error_missing_attribute (data, element_name, "name", error);
549 error_missing_attribute (data, element_name, "handler", error);
553 /* Swapped defaults to FALSE except when object is set */
554 if (object && !swapped_set)
557 info = g_slice_new0 (SignalInfo);
559 info->handler = handler;
561 info->flags |= G_CONNECT_AFTER;
563 info->flags |= G_CONNECT_SWAPPED;
564 info->connect_object_name = object;
565 state_push (data, info);
567 info->tag.name = element_name;
570 /* Called by GtkBuilder */
572 _free_signal_info (SignalInfo *info,
576 g_free (info->handler);
577 g_free (info->connect_object_name);
578 g_free (info->object_name);
579 g_slice_free (SignalInfo, info);
583 _free_requires_info (RequiresInfo *info,
586 g_free (info->library);
587 g_slice_free (RequiresInfo, info);
591 parse_interface (ParserData *data,
592 const gchar *element_name,
594 const gchar **values,
599 for (i = 0; names[i] != NULL; i++)
601 if (strcmp (names[i], "domain") == 0)
603 g_free (data->domain);
604 data->domain = g_strdup (values[i]);
608 error_invalid_attribute (data, "interface", names[i], error);
613 create_subparser (GObject *object,
615 const gchar *element_name,
616 GMarkupParser *parser,
619 SubParser *subparser;
621 subparser = g_slice_new0 (SubParser);
622 subparser->object = object;
623 subparser->child = child;
624 subparser->tagname = g_strdup (element_name);
625 subparser->start = element_name;
626 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
627 subparser->data = user_data;
633 free_subparser (SubParser *subparser)
635 g_free (subparser->tagname);
636 g_slice_free (SubParser, subparser);
640 subparser_start (GMarkupParseContext *context,
641 const gchar *element_name,
643 const gchar **values,
647 SubParser *subparser = data->subparser;
649 if (!subparser->start &&
650 strcmp (element_name, subparser->tagname) == 0)
651 subparser->start = element_name;
653 if (subparser->start)
655 if (subparser->parser->start_element)
656 subparser->parser->start_element (context,
657 element_name, names, values,
658 subparser->data, error);
665 subparser_end (GMarkupParseContext *context,
666 const gchar *element_name,
670 if (data->subparser->parser->end_element)
671 data->subparser->parser->end_element (context, element_name,
672 data->subparser->data, error);
674 if (!strcmp (data->subparser->start, element_name) == 0)
677 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
679 data->subparser->child,
681 data->subparser->data);
682 g_free (data->subparser->parser);
684 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
685 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
688 free_subparser (data->subparser);
690 data->subparser = NULL;
694 parse_custom (GMarkupParseContext *context,
695 const gchar *element_name,
697 const gchar **values,
701 CommonInfo* parent_info;
702 GMarkupParser parser;
703 gpointer subparser_data;
707 parent_info = state_peek_info (data, CommonInfo);
711 if (strcmp (parent_info->tag.name, "object") == 0)
713 ObjectInfo* object_info = (ObjectInfo*)parent_info;
714 if (!object_info->object)
716 object_info->properties = g_slist_reverse (object_info->properties);
717 object_info->object = _gtk_builder_construct (data->builder,
720 if (!object_info->object)
721 return TRUE; /* A GError is already set */
723 g_assert (object_info->object);
724 object = object_info->object;
727 else if (strcmp (parent_info->tag.name, "child") == 0)
729 ChildInfo* child_info = (ChildInfo*)parent_info;
731 _gtk_builder_add (data->builder, child_info);
733 object = ((ObjectInfo*)child_info->parent)->object;
734 child = child_info->object;
739 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
747 data->subparser = create_subparser (object, child, element_name,
748 &parser, subparser_data);
750 if (parser.start_element)
751 parser.start_element (context,
752 element_name, names, values,
753 subparser_data, error);
758 start_element (GMarkupParseContext *context,
759 const gchar *element_name,
761 const gchar **values,
765 ParserData *data = (ParserData*)user_data;
767 #ifdef GTK_ENABLE_DEBUG
768 if (gtk_debug_flags & GTK_DEBUG_BUILDER)
770 GString *tags = g_string_new ("");
772 for (i = 0; names[i]; i++)
773 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
777 g_string_insert_c (tags, 0, ' ');
778 g_string_truncate (tags, tags->len - 1);
780 g_print ("<%s%s>\n", element_name, tags->str);
781 g_string_free (tags, TRUE);
785 if (!data->last_element && strcmp (element_name, "interface") != 0)
787 g_set_error (error, GTK_BUILDER_ERROR,
788 GTK_BUILDER_ERROR_UNHANDLED_TAG,
789 _("Invalid root element: '%s'"),
793 data->last_element = element_name;
796 if (!subparser_start (context, element_name, names, values,
800 if (strcmp (element_name, "requires") == 0)
801 parse_requires (data, element_name, names, values, error);
802 else if (strcmp (element_name, "object") == 0)
803 parse_object (data, element_name, names, values, error);
804 else if (strcmp (element_name, "child") == 0)
805 parse_child (data, element_name, names, values, error);
806 else if (strcmp (element_name, "property") == 0)
807 parse_property (data, element_name, names, values, error);
808 else if (strcmp (element_name, "signal") == 0)
809 parse_signal (data, element_name, names, values, error);
810 else if (strcmp (element_name, "interface") == 0)
811 parse_interface (data, element_name, names, values, error);
812 else if (strcmp (element_name, "placeholder") == 0)
814 /* placeholder has no special treatmeant, but it needs an
815 * if clause to avoid an error below.
819 if (!parse_custom (context, element_name, names, values,
821 g_set_error (error, GTK_BUILDER_ERROR,
822 GTK_BUILDER_ERROR_UNHANDLED_TAG,
823 _("Unhandled tag: '%s'"),
827 /* This function is taken from gettext.h
828 * GNU gettext uses '\004' to separate context and msgid in .mo files.
831 _dpgettext (const char *domain,
835 size_t msgctxt_len = strlen (msgctxt) + 1;
836 size_t msgid_len = strlen (msgid) + 1;
837 const char *translation;
840 msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
842 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
843 msg_ctxt_id[msgctxt_len - 1] = '\004';
844 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
846 translation = g_dgettext (domain, msg_ctxt_id);
848 if (translation == msg_ctxt_id)
850 /* try the old way of doing message contexts, too */
851 msg_ctxt_id[msgctxt_len - 1] = '|';
852 translation = g_dgettext (domain, msg_ctxt_id);
854 if (translation == msg_ctxt_id)
862 _gtk_builder_parser_translate (const gchar *domain,
863 const gchar *context,
869 s = _dpgettext (domain, context, text);
871 s = g_dgettext (domain, text);
876 /* Called for close tags </foo> */
878 end_element (GMarkupParseContext *context,
879 const gchar *element_name,
883 ParserData *data = (ParserData*)user_data;
885 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
887 if (data->subparser && data->subparser->start)
889 subparser_end (context, element_name, data, error);
893 if (strcmp (element_name, "requires") == 0)
895 RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
897 /* TODO: Allow third party widget developers to check thier
898 * required versions, possibly throw a signal allowing them
899 * to check thier library versions here.
901 if (!strcmp (req_info->library, "gtk+"))
903 if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
906 GTK_BUILDER_ERROR_VERSION_MISMATCH,
907 "%s: required %s version %d.%d, current version is %d.%d",
908 data->filename, req_info->library,
909 req_info->major, req_info->minor,
910 GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
913 else if (strcmp (element_name, "object") == 0)
915 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
916 ChildInfo* child_info = state_peek_info (data, ChildInfo);
918 object_info->object = builder_construct (data, object_info, error);
919 if (!object_info->object)
921 free_object_info (object_info);
925 child_info->object = object_info->object;
927 if (GTK_IS_BUILDABLE (object_info->object) &&
928 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
929 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
930 _gtk_builder_add_signals (data->builder, object_info->signals);
932 free_object_info (object_info);
934 else if (strcmp (element_name, "property") == 0)
936 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
937 CommonInfo *info = state_peek_info (data, CommonInfo);
939 /* Normal properties */
940 if (strcmp (info->tag.name, "object") == 0)
942 ObjectInfo *object_info = (ObjectInfo*)info;
944 if (prop_info->translatable && prop_info->text->len)
946 prop_info->data = _gtk_builder_parser_translate (data->domain,
948 prop_info->text->str);
949 g_string_free (prop_info->text, TRUE);
953 prop_info->data = g_string_free (prop_info->text, FALSE);
957 object_info->properties =
958 g_slist_prepend (object_info->properties, prop_info);
961 g_assert_not_reached ();
963 else if (strcmp (element_name, "child") == 0)
965 ChildInfo *child_info = state_pop_info (data, ChildInfo);
967 _gtk_builder_add (data->builder, child_info);
969 free_child_info (child_info);
971 else if (strcmp (element_name, "signal") == 0)
973 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
974 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
975 signal_info->object_name = g_strdup (object_info->id);
976 object_info->signals =
977 g_slist_prepend (object_info->signals, signal_info);
979 else if (strcmp (element_name, "interface") == 0)
982 else if (strcmp (element_name, "placeholder") == 0)
987 g_assert_not_reached ();
991 /* Called for character data */
992 /* text is not nul-terminated */
994 text (GMarkupParseContext *context,
1000 ParserData *data = (ParserData*)user_data;
1003 if (data->subparser && data->subparser->start)
1005 GError *tmp_error = NULL;
1007 if (data->subparser->parser->text)
1008 data->subparser->parser->text (context, text, text_len,
1009 data->subparser->data, &tmp_error);
1011 g_propagate_error (error, tmp_error);
1018 info = state_peek_info (data, CommonInfo);
1019 g_assert (info != NULL);
1021 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
1023 PropertyInfo *prop_info = (PropertyInfo*)info;
1025 g_string_append_len (prop_info->text, text, text_len);
1030 free_info (CommonInfo *info)
1032 if (strcmp (info->tag.name, "object") == 0)
1033 free_object_info ((ObjectInfo *)info);
1034 else if (strcmp (info->tag.name, "child") == 0)
1035 free_child_info ((ChildInfo *)info);
1036 else if (strcmp (info->tag.name, "property") == 0)
1037 free_property_info ((PropertyInfo *)info);
1038 else if (strcmp (info->tag.name, "signal") == 0)
1039 _free_signal_info ((SignalInfo *)info, NULL);
1040 else if (strcmp (info->tag.name, "requires") == 0)
1041 _free_requires_info ((RequiresInfo *)info, NULL);
1043 g_assert_not_reached ();
1046 static const GMarkupParser parser = {
1055 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
1056 const gchar *filename,
1057 const gchar *buffer,
1064 data = g_new0 (ParserData, 1);
1065 data->builder = builder;
1066 data->filename = filename;
1067 data->domain = g_strdup (gtk_builder_get_translation_domain (builder));
1069 data->ctx = g_markup_parse_context_new (&parser,
1070 G_MARKUP_TREAT_CDATA_AS_TEXT,
1073 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
1076 _gtk_builder_finish (builder);
1078 /* Custom parser_finished */
1079 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
1080 for (l = data->custom_finalizers; l; l = l->next)
1082 SubParser *sub = (SubParser*)l->data;
1084 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
1091 /* Common parser_finished, for all created objects */
1092 data->finalizers = g_slist_reverse (data->finalizers);
1093 for (l = data->finalizers; l; l = l->next)
1095 GtkBuildable *buildable = (GtkBuildable*)l->data;
1096 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1101 g_slist_foreach (data->stack, (GFunc)free_info, NULL);
1102 g_slist_free (data->stack);
1103 g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
1104 g_slist_free (data->custom_finalizers);
1105 g_slist_free (data->finalizers);
1106 g_free (data->domain);
1107 g_markup_parse_context_free (data->ctx);