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 error_missing_property_value (ParserData *data,
146 gint line_number, char_number;
148 g_markup_parse_context_get_position (data->ctx,
154 GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE,
155 "%s:%d:%d <property> must have a value set",
157 line_number, char_number);
161 _gtk_builder_parse_boolean (const gchar *string,
165 gboolean retval = TRUE;
169 g_assert (string != NULL);
170 length = strlen (string);
174 else if (length == 1)
176 gchar c = g_ascii_tolower (string[0]);
177 if (c == 'y' || c == 't' || c == '1')
179 else if (c == 'n' || c == 'f' || c == '0')
186 gchar *lower = g_strdup (string);
187 for (i = 0; i < strlen (string); i++)
188 lower[i] = g_ascii_tolower (string[i]);
190 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
192 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
202 GTK_BUILDER_ERROR_INVALID_VALUE,
203 "could not parse boolean `%s'",
210 builder_construct (ParserData *data,
211 ObjectInfo *object_info)
215 g_assert (object_info != NULL);
217 if (object_info->object)
218 return object_info->object;
220 object_info->properties = g_slist_reverse (object_info->properties);
222 object = _gtk_builder_construct (data->builder, object_info);
223 g_assert (G_IS_OBJECT (object));
225 object_info->object = object;
231 _get_type_by_symbol (const gchar* symbol)
233 static GModule *module = NULL;
238 module = g_module_open (NULL, 0);
240 if (!g_module_symbol (module, symbol, (gpointer)&func))
244 if (type == G_TYPE_INVALID)
247 return g_strdup (g_type_name (type));
251 parse_object (ParserData *data,
252 const gchar *element_name,
254 const gchar **values,
257 ObjectInfo *object_info;
258 ChildInfo* child_info;
260 gchar *object_class = NULL;
261 gchar *object_id = NULL;
262 gchar *constructor = NULL;
264 child_info = state_peek_info (data, ChildInfo);
265 if (child_info && strcmp (child_info->tag.name, "object") == 0)
267 error_invalid_tag (data, element_name, NULL, error);
269 free_object_info ((ObjectInfo*)child_info);
273 for (i = 0; names[i] != NULL; i++)
275 if (strcmp (names[i], "class") == 0)
276 object_class = g_strdup (values[i]);
277 else if (strcmp (names[i], "id") == 0)
278 object_id = g_strdup (values[i]);
279 else if (strcmp (names[i], "constructor") == 0)
280 constructor = g_strdup (values[i]);
281 else if (strcmp (names[i], "type-func") == 0)
283 /* Call the GType function, and return the name of the GType,
284 * it's guaranteed afterwards that g_type_from_name on the name
285 * will return our GType
287 object_class = _get_type_by_symbol (values[i]);
290 g_set_error (error, GTK_BUILDER_ERROR,
291 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
292 _("Invalid type function: `%s'"),
299 error_invalid_attribute (data, element_name, names[i], error);
306 error_missing_attribute (data, element_name, "class", error);
312 error_missing_attribute (data, element_name, "id", error);
316 object_info = g_slice_new0 (ObjectInfo);
317 object_info->class_name = object_class;
318 object_info->id = object_id;
319 object_info->constructor = constructor;
320 state_push (data, object_info);
321 g_assert (state_peek (data) != NULL);
322 object_info->tag.name = element_name;
325 object_info->parent = (CommonInfo*)child_info;
329 free_object_info (ObjectInfo *info)
331 /* Do not free the signal items, which GtkBuilder takes ownership of */
332 g_slist_free (info->signals);
333 g_slist_foreach (info->properties,
334 (GFunc)free_property_info, NULL);
335 g_slist_free (info->properties);
336 g_free (info->constructor);
337 g_free (info->class_name);
339 g_slice_free (ObjectInfo, info);
343 parse_child (ParserData *data,
344 const gchar *element_name,
346 const gchar **values,
350 ObjectInfo* object_info;
351 ChildInfo *child_info;
354 object_info = state_peek_info (data, ObjectInfo);
355 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
357 error_invalid_tag (data, element_name, "object", error);
359 free_object_info (object_info);
363 child_info = g_slice_new0 (ChildInfo);
364 state_push (data, child_info);
365 g_assert (state_peek (data) != NULL);
366 child_info->tag.name = element_name;
367 for (i = 0; names[i]; i++)
369 if (strcmp (names[i], "type") == 0)
370 child_info->type = g_strdup (values[i]);
371 else if (strcmp (names[i], "internal-child") == 0)
372 child_info->internal_child = g_strdup (values[i]);
374 error_invalid_attribute (data, element_name, names[i], error);
377 child_info->parent = (CommonInfo*)object_info;
379 object_info->object = builder_construct (data, object_info);
383 free_child_info (ChildInfo *info)
386 g_free (info->internal_child);
387 g_slice_free (ChildInfo, info);
391 parse_property (ParserData *data,
392 const gchar *element_name,
394 const gchar **values,
399 gboolean translatable = FALSE;
402 g_assert (data->stack != NULL);
404 for (i = 0; names[i] != NULL; i++)
406 if (strcmp (names[i], "name") == 0)
407 name = g_strdelimit (g_strdup (values[i]), "_", '-');
408 else if (strcmp (names[i], "translatable") == 0)
410 if (!_gtk_builder_parse_boolean (values[i], &translatable, error))
415 error_invalid_attribute (data, element_name, names[i], error);
422 error_missing_attribute (data, element_name, "name", error);
426 info = g_slice_new0 (PropertyInfo);
428 info->translatable = translatable;
429 state_push (data, info);
431 info->tag.name = element_name;
435 free_property_info (PropertyInfo *info)
439 g_slice_free (PropertyInfo, info);
443 parse_signal (ParserData *data,
444 const gchar *element_name,
446 const gchar **values,
451 gchar *handler = NULL;
452 gchar *object = NULL;
453 gboolean after = FALSE;
454 gboolean swapped = FALSE;
455 gboolean swapped_set = FALSE;
458 g_assert (data->stack != NULL);
460 for (i = 0; names[i] != NULL; i++)
462 if (strcmp (names[i], "name") == 0)
463 name = g_strdup (values[i]);
464 else if (strcmp (names[i], "handler") == 0)
465 handler = g_strdup (values[i]);
466 else if (strcmp (names[i], "after") == 0)
468 if (!_gtk_builder_parse_boolean (values[i], &after, error))
471 else if (strcmp (names[i], "swapped") == 0)
473 if (!_gtk_builder_parse_boolean (values[i], &swapped, error))
477 else if (strcmp (names[i], "object") == 0)
478 object = g_strdup (values[i]);
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 && !data->domain)
539 data->domain = g_strdup (values[i]);
543 error_invalid_attribute (data, "interface", names[i], error);
548 create_subparser (GObject *object,
550 const gchar *element_name,
551 GMarkupParser *parser,
554 SubParser *subparser;
556 subparser = g_slice_new0 (SubParser);
557 subparser->object = object;
558 subparser->child = child;
559 subparser->tagname = g_strdup (element_name);
560 subparser->start = element_name;
561 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
562 subparser->data = user_data;
568 free_subparser (SubParser *subparser)
570 g_free (subparser->tagname);
571 g_slice_free (SubParser, subparser);
575 subparser_start (GMarkupParseContext *context,
576 const gchar *element_name,
578 const gchar **values,
582 SubParser *subparser = data->subparser;
584 if (!subparser->start &&
585 strcmp (element_name, subparser->tagname) == 0)
586 subparser->start = element_name;
588 if (subparser->start)
590 if (subparser->parser->start_element)
591 subparser->parser->start_element (context,
592 element_name, names, values,
593 subparser->data, error);
600 subparser_end (GMarkupParseContext *context,
601 const gchar *element_name,
605 if (data->subparser->parser->end_element)
606 data->subparser->parser->end_element (context, element_name,
607 data->subparser->data, error);
609 if (!strcmp (data->subparser->start, element_name) == 0)
612 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
614 data->subparser->child,
616 data->subparser->data);
617 g_free (data->subparser->parser);
619 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
620 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
623 free_subparser (data->subparser);
625 data->subparser = NULL;
629 parse_custom (GMarkupParseContext *context,
630 const gchar *element_name,
632 const gchar **values,
636 CommonInfo* parent_info;
637 GMarkupParser parser;
638 gpointer *subparser_data;
642 parent_info = state_peek_info (data, CommonInfo);
646 if (strcmp (parent_info->tag.name, "object") == 0)
648 ObjectInfo* object_info = (ObjectInfo*)parent_info;
649 if (!object_info->object)
650 object_info->object = _gtk_builder_construct (data->builder,
652 g_assert (object_info->object);
653 object = object_info->object;
656 else if (strcmp (parent_info->tag.name, "child") == 0)
658 ChildInfo* child_info = (ChildInfo*)parent_info;
660 _gtk_builder_add (data->builder, child_info);
662 object = ((ObjectInfo*)child_info->parent)->object;
663 child = child_info->object;
668 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
673 (gpointer*)&subparser_data))
676 data->subparser = create_subparser (object, child, element_name,
677 &parser, subparser_data);
679 if (parser.start_element)
680 parser.start_element (context,
681 element_name, names, values,
682 subparser_data, error);
687 start_element (GMarkupParseContext *context,
688 const gchar *element_name,
690 const gchar **values,
694 ParserData *data = (ParserData*)user_data;
696 #ifdef GTK_ENABLE_DEBUG
697 if (gtk_debug_flags & GTK_DEBUG_BUILDER)
699 GString *tags = g_string_new ("");
701 for (i = 0; names[i]; i++)
702 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
706 g_string_insert_c (tags, 0, ' ');
707 g_string_truncate (tags, tags->len - 1);
709 g_print ("<%s%s>\n", element_name, tags->str);
710 g_string_free (tags, TRUE);
714 if (!data->last_element && strcmp (element_name, "interface") != 0)
716 g_set_error (error, GTK_BUILDER_ERROR,
717 GTK_BUILDER_ERROR_UNHANDLED_TAG,
718 _("Invalid root element: '%s'"),
722 data->last_element = element_name;
725 if (!subparser_start (context, element_name, names, values,
729 if (strcmp (element_name, "object") == 0)
730 parse_object (data, element_name, names, values, error);
731 else if (strcmp (element_name, "child") == 0)
732 parse_child (data, element_name, names, values, error);
733 else if (strcmp (element_name, "property") == 0)
734 parse_property (data, element_name, names, values, error);
735 else if (strcmp (element_name, "signal") == 0)
736 parse_signal (data, element_name, names, values, error);
737 else if (strcmp (element_name, "interface") == 0)
738 parse_interface (data, element_name, names, values, error);
739 else if (strcmp (element_name, "placeholder") == 0)
741 /* placeholder has no special treatmeant, but it needs an
742 * if clause to avoid an error below.
746 if (!parse_custom (context, element_name, names, values,
748 g_set_error (error, GTK_BUILDER_ERROR,
749 GTK_BUILDER_ERROR_UNHANDLED_TAG,
750 _("Unhandled tag: '%s'"),
754 /* Called for close tags </foo> */
756 end_element (GMarkupParseContext *context,
757 const gchar *element_name,
761 ParserData *data = (ParserData*)user_data;
763 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
765 if (data->subparser && data->subparser->start)
767 subparser_end (context, element_name, data, error);
771 if (strcmp (element_name, "object") == 0)
773 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
774 ChildInfo* child_info = state_peek_info (data, ChildInfo);
776 object_info->object = builder_construct (data, object_info);
779 child_info->object = object_info->object;
781 if (GTK_IS_BUILDABLE (object_info->object) &&
782 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
783 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
784 free_object_info (object_info);
786 else if (strcmp (element_name, "property") == 0)
788 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
789 CommonInfo *info = state_peek_info (data, CommonInfo);
791 if (!prop_info->data)
793 error_missing_property_value (data, error);
794 free_property_info (prop_info);
795 if (strcmp (info->tag.name, "object") == 0)
796 free_object_info((ObjectInfo*)info);
800 /* Normal properties */
801 if (strcmp (info->tag.name, "object") == 0)
803 ObjectInfo *object_info = (ObjectInfo*)info;
804 object_info->properties =
805 g_slist_prepend (object_info->properties, prop_info);
808 g_assert_not_reached ();
810 else if (strcmp (element_name, "child") == 0)
812 ChildInfo *child_info = state_pop_info (data, ChildInfo);
814 _gtk_builder_add (data->builder, child_info);
816 free_child_info (child_info);
818 else if (strcmp (element_name, "signal") == 0)
820 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
821 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
822 signal_info->object_name = g_strdup (object_info->id);
823 object_info->signals =
824 g_slist_prepend (object_info->signals, signal_info);
826 else if (strcmp (element_name, "interface") == 0)
829 else if (strcmp (element_name, "placeholder") == 0)
834 g_assert_not_reached ();
838 /* Called for character data */
839 /* text is not nul-terminated */
841 text (GMarkupParseContext *context,
847 ParserData *data = (ParserData*)user_data;
850 if (data->subparser && data->subparser->start)
852 if (data->subparser->parser->text)
853 data->subparser->parser->text (context, text, text_len,
854 data->subparser->data, error);
861 info = state_peek_info (data, CommonInfo);
862 g_assert (info != NULL);
864 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
866 PropertyInfo *prop_info = (PropertyInfo*)info;
868 if (prop_info->translatable && text_len)
871 text = dgettext (data->domain, text);
873 text = gettext (text);
875 prop_info->data = g_strndup (text, text_len);
879 static const GMarkupParser parser = {
888 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
889 const gchar *filename,
897 data = g_new0 (ParserData, 1);
898 data->builder = builder;
899 data->filename = filename;
900 data->domain = g_strdup (gtk_builder_get_translation_domain (builder));
902 data->ctx = g_markup_parse_context_new (
903 &parser, G_MARKUP_TREAT_CDATA_AS_TEXT, data, NULL);
905 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
908 gtk_builder_set_translation_domain (data->builder, data->domain);
909 _gtk_builder_finish (builder);
911 /* Custom parser_finished */
912 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
913 for (l = data->custom_finalizers; l; l = l->next)
915 SubParser *sub = (SubParser*)l->data;
917 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
922 free_subparser (sub);
925 /* Common parser_finished, for all created objects */
926 data->finalizers = g_slist_reverse (data->finalizers);
927 for (l = data->finalizers; l; l = l->next)
929 GtkBuildable *buildable = (GtkBuildable*)l->data;
930 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
934 g_markup_parse_context_free (data->ctx);
936 g_slist_free (data->stack);
937 g_slist_free (data->custom_finalizers);
938 g_slist_free (data->finalizers);
939 g_free (data->domain);