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.
22 #include <glib/gfileutils.h>
23 #include <glib/gi18n.h>
24 #include <glib/gmacros.h>
25 #include <glib/gmessages.h>
26 #include <glib/gslist.h>
27 #include <glib/gstrfuncs.h>
28 #include <glib-object.h>
31 #include <gdk/gdkenumtypes.h>
32 #include <gdk/gdkkeys.h>
33 #include <gtk/gtktypeutils.h>
34 #include "gtkbuilderprivate.h"
35 #include "gtkbuilder.h"
36 #include "gtkbuildable.h"
38 #include "gtktypeutils.h"
41 static void free_property_info (PropertyInfo *info);
42 static void free_object_info (ObjectInfo *info);
45 state_push (ParserData *data, gpointer info)
47 data->stack = g_slist_prepend (data->stack, info);
50 static inline gpointer
51 state_peek (ParserData *data)
56 return data->stack->data;
59 static inline gpointer
60 state_pop (ParserData *data)
67 old = data->stack->data;
68 data->stack = g_slist_delete_link (data->stack, data->stack);
71 #define state_peek_info(data, st) ((st*)state_peek(data))
72 #define state_pop_info(data, st) ((st*)state_pop(data))
75 error_missing_attribute (ParserData *data,
77 const gchar *attribute,
80 gint line_number, char_number;
82 g_markup_parse_context_get_position (data->ctx,
88 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
89 "%s:%d:%d <%s> requires attribute \"%s\"",
91 line_number, char_number, tag, attribute);
95 error_invalid_attribute (ParserData *data,
97 const gchar *attribute,
100 gint line_number, char_number;
102 g_markup_parse_context_get_position (data->ctx,
108 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
109 "%s:%d:%d '%s' is not a valid attribute of <%s>",
111 line_number, char_number, attribute, tag);
115 error_invalid_tag (ParserData *data,
117 const gchar *expected,
120 gint line_number, char_number;
122 g_markup_parse_context_get_position (data->ctx,
129 GTK_BUILDER_ERROR_INVALID_TAG,
130 "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
132 line_number, char_number, tag, expected);
136 GTK_BUILDER_ERROR_INVALID_TAG,
137 "%s:%d:%d '%s' is not a valid tag here",
139 line_number, char_number, tag);
143 _gtk_builder_boolean_from_string (const gchar *string,
147 gboolean retval = TRUE;
150 g_assert (string != NULL);
151 length = strlen (string);
155 else if (length == 1)
157 gchar c = g_ascii_tolower (string[0]);
158 if (c == 'y' || c == 't' || c == '1')
160 else if (c == 'n' || c == 'f' || c == '0')
167 gchar *lower = g_ascii_strdown (string, length);
169 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
171 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
181 GTK_BUILDER_ERROR_INVALID_VALUE,
182 "could not parse boolean `%s'",
189 builder_construct (ParserData *data,
190 ObjectInfo *object_info)
194 g_assert (object_info != NULL);
196 if (object_info->object)
197 return object_info->object;
199 object_info->properties = g_slist_reverse (object_info->properties);
201 object = _gtk_builder_construct (data->builder, object_info);
202 g_assert (G_IS_OBJECT (object));
204 object_info->object = object;
210 _get_type_by_symbol (const gchar* symbol)
212 static GModule *module = NULL;
217 module = g_module_open (NULL, 0);
219 if (!g_module_symbol (module, symbol, (gpointer)&func))
223 if (type == G_TYPE_INVALID)
226 return g_strdup (g_type_name (type));
230 parse_object (ParserData *data,
231 const gchar *element_name,
233 const gchar **values,
236 ObjectInfo *object_info;
237 ChildInfo* child_info;
239 gchar *object_class = NULL;
240 gchar *object_id = NULL;
241 gchar *constructor = NULL;
243 child_info = state_peek_info (data, ChildInfo);
244 if (child_info && strcmp (child_info->tag.name, "object") == 0)
246 error_invalid_tag (data, element_name, NULL, error);
250 for (i = 0; names[i] != NULL; i++)
252 if (strcmp (names[i], "class") == 0)
253 object_class = g_strdup (values[i]);
254 else if (strcmp (names[i], "id") == 0)
255 object_id = g_strdup (values[i]);
256 else if (strcmp (names[i], "constructor") == 0)
257 constructor = g_strdup (values[i]);
258 else if (strcmp (names[i], "type-func") == 0)
260 /* Call the GType function, and return the name of the GType,
261 * it's guaranteed afterwards that g_type_from_name on the name
262 * will return our GType
264 object_class = _get_type_by_symbol (values[i]);
267 g_set_error (error, GTK_BUILDER_ERROR,
268 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
269 _("Invalid type function: `%s'"),
276 error_invalid_attribute (data, element_name, names[i], error);
283 error_missing_attribute (data, element_name, "class", error);
289 error_missing_attribute (data, element_name, "id", error);
293 object_info = g_slice_new0 (ObjectInfo);
294 object_info->class_name = object_class;
295 object_info->id = object_id;
296 object_info->constructor = constructor;
297 state_push (data, object_info);
298 object_info->tag.name = element_name;
301 object_info->parent = (CommonInfo*)child_info;
305 free_object_info (ObjectInfo *info)
307 /* Do not free the signal items, which GtkBuilder takes ownership of */
308 g_slist_free (info->signals);
309 g_slist_foreach (info->properties,
310 (GFunc)free_property_info, NULL);
311 g_slist_free (info->properties);
312 g_free (info->constructor);
313 g_free (info->class_name);
315 g_slice_free (ObjectInfo, info);
319 parse_child (ParserData *data,
320 const gchar *element_name,
322 const gchar **values,
326 ObjectInfo* object_info;
327 ChildInfo *child_info;
330 object_info = state_peek_info (data, ObjectInfo);
331 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
333 error_invalid_tag (data, element_name, NULL, error);
337 child_info = g_slice_new0 (ChildInfo);
338 state_push (data, child_info);
339 child_info->tag.name = element_name;
340 for (i = 0; names[i]; i++)
342 if (strcmp (names[i], "type") == 0)
343 child_info->type = g_strdup (values[i]);
344 else if (strcmp (names[i], "internal-child") == 0)
345 child_info->internal_child = g_strdup (values[i]);
347 error_invalid_attribute (data, element_name, names[i], error);
350 child_info->parent = (CommonInfo*)object_info;
352 object_info->object = builder_construct (data, object_info);
356 free_child_info (ChildInfo *info)
359 g_free (info->internal_child);
360 g_slice_free (ChildInfo, info);
364 parse_property (ParserData *data,
365 const gchar *element_name,
367 const gchar **values,
372 gchar *context = NULL;
373 gboolean translatable = FALSE;
374 ObjectInfo *object_info;
377 object_info = state_peek_info (data, ObjectInfo);
378 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
380 error_invalid_tag (data, element_name, NULL, error);
384 for (i = 0; names[i] != NULL; i++)
386 if (strcmp (names[i], "name") == 0)
387 name = g_strdelimit (g_strdup (values[i]), "_", '-');
388 else if (strcmp (names[i], "translatable") == 0)
390 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
394 else if (strcmp (names[i], "comments") == 0)
396 /* do nothing, comments are for translators */
398 else if (strcmp (names[i], "context") == 0)
400 context = g_strdup (values[i]);
404 error_invalid_attribute (data, element_name, names[i], error);
411 error_missing_attribute (data, element_name, "name", error);
415 info = g_slice_new0 (PropertyInfo);
417 info->translatable = translatable;
418 info->context = context;
419 info->text = g_string_new ("");
420 state_push (data, info);
422 info->tag.name = element_name;
426 free_property_info (PropertyInfo *info)
430 g_slice_free (PropertyInfo, info);
434 parse_signal (ParserData *data,
435 const gchar *element_name,
437 const gchar **values,
442 gchar *handler = NULL;
443 gchar *object = NULL;
444 gboolean after = FALSE;
445 gboolean swapped = FALSE;
446 gboolean swapped_set = FALSE;
447 ObjectInfo *object_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 for (i = 0; names[i] != NULL; i++)
459 if (strcmp (names[i], "name") == 0)
460 name = g_strdup (values[i]);
461 else if (strcmp (names[i], "handler") == 0)
462 handler = g_strdup (values[i]);
463 else if (strcmp (names[i], "after") == 0)
465 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
468 else if (strcmp (names[i], "swapped") == 0)
470 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
474 else if (strcmp (names[i], "object") == 0)
475 object = g_strdup (values[i]);
476 else if (strcmp (names[i], "last_modification_time") == 0)
477 /* parse but ignore */
481 error_invalid_attribute (data, element_name, names[i], error);
488 error_missing_attribute (data, element_name, "name", error);
493 error_missing_attribute (data, element_name, "handler", error);
497 /* Swapped defaults to FALSE except when object is set */
498 if (object && !swapped_set)
501 info = g_slice_new0 (SignalInfo);
503 info->handler = handler;
505 info->flags |= G_CONNECT_AFTER;
507 info->flags |= G_CONNECT_SWAPPED;
508 info->connect_object_name = object;
509 state_push (data, info);
511 info->tag.name = element_name;
514 /* Called by GtkBuilder */
516 _free_signal_info (SignalInfo *info,
520 g_free (info->handler);
521 g_free (info->connect_object_name);
522 g_free (info->object_name);
523 g_slice_free (SignalInfo, info);
527 parse_interface (ParserData *data,
528 const gchar *element_name,
530 const gchar **values,
535 for (i = 0; names[i] != NULL; i++)
537 if (strcmp (names[i], "domain") == 0)
539 g_free (data->domain);
540 data->domain = g_strdup (values[i]);
544 error_invalid_attribute (data, "interface", names[i], error);
549 create_subparser (GObject *object,
551 const gchar *element_name,
552 GMarkupParser *parser,
555 SubParser *subparser;
557 subparser = g_slice_new0 (SubParser);
558 subparser->object = object;
559 subparser->child = child;
560 subparser->tagname = g_strdup (element_name);
561 subparser->start = element_name;
562 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
563 subparser->data = user_data;
569 free_subparser (SubParser *subparser)
571 g_free (subparser->tagname);
572 g_slice_free (SubParser, subparser);
576 subparser_start (GMarkupParseContext *context,
577 const gchar *element_name,
579 const gchar **values,
583 SubParser *subparser = data->subparser;
585 if (!subparser->start &&
586 strcmp (element_name, subparser->tagname) == 0)
587 subparser->start = element_name;
589 if (subparser->start)
591 if (subparser->parser->start_element)
592 subparser->parser->start_element (context,
593 element_name, names, values,
594 subparser->data, error);
601 subparser_end (GMarkupParseContext *context,
602 const gchar *element_name,
606 if (data->subparser->parser->end_element)
607 data->subparser->parser->end_element (context, element_name,
608 data->subparser->data, error);
610 if (!strcmp (data->subparser->start, element_name) == 0)
613 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
615 data->subparser->child,
617 data->subparser->data);
618 g_free (data->subparser->parser);
620 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
621 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
624 free_subparser (data->subparser);
626 data->subparser = NULL;
630 parse_custom (GMarkupParseContext *context,
631 const gchar *element_name,
633 const gchar **values,
637 CommonInfo* parent_info;
638 GMarkupParser parser;
639 gpointer *subparser_data;
643 parent_info = state_peek_info (data, CommonInfo);
647 if (strcmp (parent_info->tag.name, "object") == 0)
649 ObjectInfo* object_info = (ObjectInfo*)parent_info;
650 if (!object_info->object)
651 object_info->object = _gtk_builder_construct (data->builder,
653 g_assert (object_info->object);
654 object = object_info->object;
657 else if (strcmp (parent_info->tag.name, "child") == 0)
659 ChildInfo* child_info = (ChildInfo*)parent_info;
661 _gtk_builder_add (data->builder, child_info);
663 object = ((ObjectInfo*)child_info->parent)->object;
664 child = child_info->object;
669 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
674 (gpointer*)&subparser_data))
677 data->subparser = create_subparser (object, child, element_name,
678 &parser, subparser_data);
680 if (parser.start_element)
681 parser.start_element (context,
682 element_name, names, values,
683 subparser_data, error);
688 start_element (GMarkupParseContext *context,
689 const gchar *element_name,
691 const gchar **values,
695 ParserData *data = (ParserData*)user_data;
697 #ifdef GTK_ENABLE_DEBUG
698 if (gtk_debug_flags & GTK_DEBUG_BUILDER)
700 GString *tags = g_string_new ("");
702 for (i = 0; names[i]; i++)
703 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
707 g_string_insert_c (tags, 0, ' ');
708 g_string_truncate (tags, tags->len - 1);
710 g_print ("<%s%s>\n", element_name, tags->str);
711 g_string_free (tags, TRUE);
715 if (!data->last_element && strcmp (element_name, "interface") != 0)
717 g_set_error (error, GTK_BUILDER_ERROR,
718 GTK_BUILDER_ERROR_UNHANDLED_TAG,
719 _("Invalid root element: '%s'"),
723 data->last_element = element_name;
726 if (!subparser_start (context, element_name, names, values,
730 if (strcmp (element_name, "object") == 0)
731 parse_object (data, element_name, names, values, error);
732 else if (strcmp (element_name, "child") == 0)
733 parse_child (data, element_name, names, values, error);
734 else if (strcmp (element_name, "property") == 0)
735 parse_property (data, element_name, names, values, error);
736 else if (strcmp (element_name, "signal") == 0)
737 parse_signal (data, element_name, names, values, error);
738 else if (strcmp (element_name, "interface") == 0)
739 parse_interface (data, element_name, names, values, error);
740 else if (strcmp (element_name, "placeholder") == 0)
742 /* placeholder has no special treatmeant, but it needs an
743 * if clause to avoid an error below.
747 if (!parse_custom (context, element_name, names, values,
749 g_set_error (error, GTK_BUILDER_ERROR,
750 GTK_BUILDER_ERROR_UNHANDLED_TAG,
751 _("Unhandled tag: '%s'"),
755 /* This function is taken from gettext.h
756 * GNU gettext uses '\004' to separate context and msgid in .mo files.
759 dpgettext (const char *domain,
763 size_t msgctxt_len = strlen (msgctxt) + 1;
764 size_t msgid_len = strlen (msgid) + 1;
765 const char *translation;
768 msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
770 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
771 msg_ctxt_id[msgctxt_len - 1] = '\004';
772 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
774 translation = dgettext (domain, msg_ctxt_id);
776 if (translation == msg_ctxt_id)
778 /* try the old way of doing message contexts, too */
779 msg_ctxt_id[msgctxt_len - 1] = '|';
780 translation = dgettext (domain, msg_ctxt_id);
782 if (translation == msg_ctxt_id)
789 /* Called for close tags </foo> */
791 end_element (GMarkupParseContext *context,
792 const gchar *element_name,
796 ParserData *data = (ParserData*)user_data;
798 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
800 if (data->subparser && data->subparser->start)
802 subparser_end (context, element_name, data, error);
806 if (strcmp (element_name, "object") == 0)
808 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
809 ChildInfo* child_info = state_peek_info (data, ChildInfo);
811 object_info->object = builder_construct (data, object_info);
814 child_info->object = object_info->object;
816 if (GTK_IS_BUILDABLE (object_info->object) &&
817 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
818 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
819 _gtk_builder_add_signals (data->builder, object_info->signals);
821 free_object_info (object_info);
823 else if (strcmp (element_name, "property") == 0)
825 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
826 CommonInfo *info = state_peek_info (data, CommonInfo);
828 /* Normal properties */
829 if (strcmp (info->tag.name, "object") == 0)
831 ObjectInfo *object_info = (ObjectInfo*)info;
833 if (prop_info->translatable && prop_info->text->len)
837 if (prop_info->context)
838 text = dpgettext (data->domain,
840 prop_info->text->str);
842 text = dgettext (data->domain, prop_info->text->str);
844 prop_info->data = g_strdup (text);
845 g_string_free (prop_info->text, TRUE);
849 prop_info->data = prop_info->text->str;
850 g_string_free (prop_info->text, FALSE);
853 object_info->properties =
854 g_slist_prepend (object_info->properties, prop_info);
857 g_assert_not_reached ();
859 else if (strcmp (element_name, "child") == 0)
861 ChildInfo *child_info = state_pop_info (data, ChildInfo);
863 _gtk_builder_add (data->builder, child_info);
865 free_child_info (child_info);
867 else if (strcmp (element_name, "signal") == 0)
869 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
870 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
871 signal_info->object_name = g_strdup (object_info->id);
872 object_info->signals =
873 g_slist_prepend (object_info->signals, signal_info);
875 else if (strcmp (element_name, "interface") == 0)
878 else if (strcmp (element_name, "placeholder") == 0)
883 g_assert_not_reached ();
887 /* Called for character data */
888 /* text is not nul-terminated */
890 text (GMarkupParseContext *context,
896 ParserData *data = (ParserData*)user_data;
899 if (data->subparser && data->subparser->start)
901 if (data->subparser->parser->text)
902 data->subparser->parser->text (context, text, text_len,
903 data->subparser->data, error);
910 info = state_peek_info (data, CommonInfo);
911 g_assert (info != NULL);
913 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
915 PropertyInfo *prop_info = (PropertyInfo*)info;
917 g_string_append_len (prop_info->text, text, text_len);
922 free_info (CommonInfo *info)
924 if (strcmp (info->tag.name, "object") == 0)
925 free_object_info ((ObjectInfo *)info);
926 else if (strcmp (info->tag.name, "child") == 0)
927 free_child_info ((ChildInfo *)info);
928 else if (strcmp (info->tag.name, "property") == 0)
929 free_property_info ((PropertyInfo *)info);
930 else if (strcmp (info->tag.name, "signal") == 0)
931 _free_signal_info ((SignalInfo *)info, NULL);
933 g_assert_not_reached ();
936 static const GMarkupParser parser = {
945 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
946 const gchar *filename,
954 data = g_new0 (ParserData, 1);
955 data->builder = builder;
956 data->filename = filename;
957 data->domain = g_strdup (gtk_builder_get_translation_domain (builder));
959 data->ctx = g_markup_parse_context_new (&parser,
960 G_MARKUP_TREAT_CDATA_AS_TEXT,
963 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
966 _gtk_builder_finish (builder);
968 /* Custom parser_finished */
969 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
970 for (l = data->custom_finalizers; l; l = l->next)
972 SubParser *sub = (SubParser*)l->data;
974 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
981 /* Common parser_finished, for all created objects */
982 data->finalizers = g_slist_reverse (data->finalizers);
983 for (l = data->finalizers; l; l = l->next)
985 GtkBuildable *buildable = (GtkBuildable*)l->data;
986 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
991 g_slist_foreach (data->stack, (GFunc)free_info, NULL);
992 g_slist_free (data->stack);
993 g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
994 g_slist_free (data->custom_finalizers);
995 g_slist_free (data->finalizers);
996 g_free (data->domain);
997 g_markup_parse_context_free (data->ctx);