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_boolean_from_string (const gchar *string,
165 gboolean retval = TRUE;
168 g_assert (string != NULL);
169 length = strlen (string);
173 else if (length == 1)
175 gchar c = g_ascii_tolower (string[0]);
176 if (c == 'y' || c == 't' || c == '1')
178 else if (c == 'n' || c == 'f' || c == '0')
185 gchar *lower = g_ascii_strdown (string, length);
187 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
189 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
199 GTK_BUILDER_ERROR_INVALID_VALUE,
200 "could not parse boolean `%s'",
207 builder_construct (ParserData *data,
208 ObjectInfo *object_info)
212 g_assert (object_info != NULL);
214 if (object_info->object)
215 return object_info->object;
217 object_info->properties = g_slist_reverse (object_info->properties);
219 object = _gtk_builder_construct (data->builder, object_info);
220 g_assert (G_IS_OBJECT (object));
222 object_info->object = object;
228 _get_type_by_symbol (const gchar* symbol)
230 static GModule *module = NULL;
235 module = g_module_open (NULL, 0);
237 if (!g_module_symbol (module, symbol, (gpointer)&func))
241 if (type == G_TYPE_INVALID)
244 return g_strdup (g_type_name (type));
248 parse_object (ParserData *data,
249 const gchar *element_name,
251 const gchar **values,
254 ObjectInfo *object_info;
255 ChildInfo* child_info;
257 gchar *object_class = NULL;
258 gchar *object_id = NULL;
259 gchar *constructor = NULL;
261 child_info = state_peek_info (data, ChildInfo);
262 if (child_info && strcmp (child_info->tag.name, "object") == 0)
264 error_invalid_tag (data, element_name, NULL, error);
266 free_object_info ((ObjectInfo*)child_info);
270 for (i = 0; names[i] != NULL; i++)
272 if (strcmp (names[i], "class") == 0)
273 object_class = g_strdup (values[i]);
274 else if (strcmp (names[i], "id") == 0)
275 object_id = g_strdup (values[i]);
276 else if (strcmp (names[i], "constructor") == 0)
277 constructor = g_strdup (values[i]);
278 else if (strcmp (names[i], "type-func") == 0)
280 /* Call the GType function, and return the name of the GType,
281 * it's guaranteed afterwards that g_type_from_name on the name
282 * will return our GType
284 object_class = _get_type_by_symbol (values[i]);
287 g_set_error (error, GTK_BUILDER_ERROR,
288 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
289 _("Invalid type function: `%s'"),
296 error_invalid_attribute (data, element_name, names[i], error);
303 error_missing_attribute (data, element_name, "class", error);
309 error_missing_attribute (data, element_name, "id", error);
313 object_info = g_slice_new0 (ObjectInfo);
314 object_info->class_name = object_class;
315 object_info->id = object_id;
316 object_info->constructor = constructor;
317 state_push (data, object_info);
318 g_assert (state_peek (data) != NULL);
319 object_info->tag.name = element_name;
322 object_info->parent = (CommonInfo*)child_info;
326 free_object_info (ObjectInfo *info)
328 /* Do not free the signal items, which GtkBuilder takes ownership of */
329 g_slist_free (info->signals);
330 g_slist_foreach (info->properties,
331 (GFunc)free_property_info, NULL);
332 g_slist_free (info->properties);
333 g_free (info->constructor);
334 g_free (info->class_name);
336 g_slice_free (ObjectInfo, info);
340 parse_child (ParserData *data,
341 const gchar *element_name,
343 const gchar **values,
347 ObjectInfo* object_info;
348 ChildInfo *child_info;
351 object_info = state_peek_info (data, ObjectInfo);
352 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
354 error_invalid_tag (data, element_name, "object", error);
356 free_object_info (object_info);
360 child_info = g_slice_new0 (ChildInfo);
361 state_push (data, child_info);
362 g_assert (state_peek (data) != NULL);
363 child_info->tag.name = element_name;
364 for (i = 0; names[i]; i++)
366 if (strcmp (names[i], "type") == 0)
367 child_info->type = g_strdup (values[i]);
368 else if (strcmp (names[i], "internal-child") == 0)
369 child_info->internal_child = g_strdup (values[i]);
371 error_invalid_attribute (data, element_name, names[i], error);
374 child_info->parent = (CommonInfo*)object_info;
376 object_info->object = builder_construct (data, object_info);
380 free_child_info (ChildInfo *info)
383 g_free (info->internal_child);
384 g_slice_free (ChildInfo, info);
388 parse_property (ParserData *data,
389 const gchar *element_name,
391 const gchar **values,
396 gchar *context = NULL;
397 gboolean translatable = FALSE;
400 g_assert (data->stack != NULL);
402 for (i = 0; names[i] != NULL; i++)
404 if (strcmp (names[i], "name") == 0)
405 name = g_strdelimit (g_strdup (values[i]), "_", '-');
406 else if (strcmp (names[i], "translatable") == 0)
408 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
412 else if (strcmp (names[i], "comments") == 0)
414 /* do nothing, comments are for translators */
416 else if (strcmp (names[i], "context") == 0)
418 context = g_strdup (values[i]);
422 error_invalid_attribute (data, element_name, names[i], error);
429 error_missing_attribute (data, element_name, "name", error);
433 info = g_slice_new0 (PropertyInfo);
435 info->translatable = translatable;
436 info->context = context;
437 state_push (data, info);
439 info->tag.name = element_name;
443 free_property_info (PropertyInfo *info)
447 g_slice_free (PropertyInfo, info);
451 parse_signal (ParserData *data,
452 const gchar *element_name,
454 const gchar **values,
459 gchar *handler = NULL;
460 gchar *object = NULL;
461 gboolean after = FALSE;
462 gboolean swapped = FALSE;
463 gboolean swapped_set = FALSE;
466 g_assert (data->stack != NULL);
468 for (i = 0; names[i] != NULL; i++)
470 if (strcmp (names[i], "name") == 0)
471 name = g_strdup (values[i]);
472 else if (strcmp (names[i], "handler") == 0)
473 handler = g_strdup (values[i]);
474 else if (strcmp (names[i], "after") == 0)
476 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
479 else if (strcmp (names[i], "swapped") == 0)
481 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
485 else if (strcmp (names[i], "object") == 0)
486 object = g_strdup (values[i]);
487 else if (strcmp (names[i], "last_modification_time") == 0)
488 /* parse but ignore */
492 error_invalid_attribute (data, element_name, names[i], error);
499 error_missing_attribute (data, element_name, "name", error);
504 error_missing_attribute (data, element_name, "handler", error);
508 /* Swapped defaults to FALSE except when object is set */
509 if (object && !swapped_set)
512 info = g_slice_new0 (SignalInfo);
514 info->handler = handler;
516 info->flags |= G_CONNECT_AFTER;
518 info->flags |= G_CONNECT_SWAPPED;
519 info->connect_object_name = object;
520 state_push (data, info);
522 info->tag.name = element_name;
525 /* Called by GtkBuilder */
527 _free_signal_info (SignalInfo *info,
531 g_free (info->handler);
532 g_free (info->connect_object_name);
533 g_free (info->object_name);
534 g_slice_free (SignalInfo, info);
538 parse_interface (ParserData *data,
539 const gchar *element_name,
541 const gchar **values,
546 for (i = 0; names[i] != NULL; i++)
548 if (strcmp (names[i], "domain") == 0 && !data->domain)
550 data->domain = g_strdup (values[i]);
554 error_invalid_attribute (data, "interface", names[i], error);
559 create_subparser (GObject *object,
561 const gchar *element_name,
562 GMarkupParser *parser,
565 SubParser *subparser;
567 subparser = g_slice_new0 (SubParser);
568 subparser->object = object;
569 subparser->child = child;
570 subparser->tagname = g_strdup (element_name);
571 subparser->start = element_name;
572 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
573 subparser->data = user_data;
579 free_subparser (SubParser *subparser)
581 g_free (subparser->tagname);
582 g_slice_free (SubParser, subparser);
586 subparser_start (GMarkupParseContext *context,
587 const gchar *element_name,
589 const gchar **values,
593 SubParser *subparser = data->subparser;
595 if (!subparser->start &&
596 strcmp (element_name, subparser->tagname) == 0)
597 subparser->start = element_name;
599 if (subparser->start)
601 if (subparser->parser->start_element)
602 subparser->parser->start_element (context,
603 element_name, names, values,
604 subparser->data, error);
611 subparser_end (GMarkupParseContext *context,
612 const gchar *element_name,
616 if (data->subparser->parser->end_element)
617 data->subparser->parser->end_element (context, element_name,
618 data->subparser->data, error);
620 if (!strcmp (data->subparser->start, element_name) == 0)
623 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
625 data->subparser->child,
627 data->subparser->data);
628 g_free (data->subparser->parser);
630 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
631 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
634 free_subparser (data->subparser);
636 data->subparser = NULL;
640 parse_custom (GMarkupParseContext *context,
641 const gchar *element_name,
643 const gchar **values,
647 CommonInfo* parent_info;
648 GMarkupParser parser;
649 gpointer *subparser_data;
653 parent_info = state_peek_info (data, CommonInfo);
657 if (strcmp (parent_info->tag.name, "object") == 0)
659 ObjectInfo* object_info = (ObjectInfo*)parent_info;
660 if (!object_info->object)
661 object_info->object = _gtk_builder_construct (data->builder,
663 g_assert (object_info->object);
664 object = object_info->object;
667 else if (strcmp (parent_info->tag.name, "child") == 0)
669 ChildInfo* child_info = (ChildInfo*)parent_info;
671 _gtk_builder_add (data->builder, child_info);
673 object = ((ObjectInfo*)child_info->parent)->object;
674 child = child_info->object;
679 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
684 (gpointer*)&subparser_data))
687 data->subparser = create_subparser (object, child, element_name,
688 &parser, subparser_data);
690 if (parser.start_element)
691 parser.start_element (context,
692 element_name, names, values,
693 subparser_data, error);
698 start_element (GMarkupParseContext *context,
699 const gchar *element_name,
701 const gchar **values,
705 ParserData *data = (ParserData*)user_data;
707 #ifdef GTK_ENABLE_DEBUG
708 if (gtk_debug_flags & GTK_DEBUG_BUILDER)
710 GString *tags = g_string_new ("");
712 for (i = 0; names[i]; i++)
713 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
717 g_string_insert_c (tags, 0, ' ');
718 g_string_truncate (tags, tags->len - 1);
720 g_print ("<%s%s>\n", element_name, tags->str);
721 g_string_free (tags, TRUE);
725 if (!data->last_element && strcmp (element_name, "interface") != 0)
727 g_set_error (error, GTK_BUILDER_ERROR,
728 GTK_BUILDER_ERROR_UNHANDLED_TAG,
729 _("Invalid root element: '%s'"),
733 data->last_element = element_name;
736 if (!subparser_start (context, element_name, names, values,
740 if (strcmp (element_name, "object") == 0)
741 parse_object (data, element_name, names, values, error);
742 else if (strcmp (element_name, "child") == 0)
743 parse_child (data, element_name, names, values, error);
744 else if (strcmp (element_name, "property") == 0)
745 parse_property (data, element_name, names, values, error);
746 else if (strcmp (element_name, "signal") == 0)
747 parse_signal (data, element_name, names, values, error);
748 else if (strcmp (element_name, "interface") == 0)
749 parse_interface (data, element_name, names, values, error);
750 else if (strcmp (element_name, "placeholder") == 0)
752 /* placeholder has no special treatmeant, but it needs an
753 * if clause to avoid an error below.
757 if (!parse_custom (context, element_name, names, values,
759 g_set_error (error, GTK_BUILDER_ERROR,
760 GTK_BUILDER_ERROR_UNHANDLED_TAG,
761 _("Unhandled tag: '%s'"),
765 /* Called for close tags </foo> */
767 end_element (GMarkupParseContext *context,
768 const gchar *element_name,
772 ParserData *data = (ParserData*)user_data;
774 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
776 if (data->subparser && data->subparser->start)
778 subparser_end (context, element_name, data, error);
782 if (strcmp (element_name, "object") == 0)
784 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
785 ChildInfo* child_info = state_peek_info (data, ChildInfo);
787 object_info->object = builder_construct (data, object_info);
790 child_info->object = object_info->object;
792 if (GTK_IS_BUILDABLE (object_info->object) &&
793 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
794 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
795 free_object_info (object_info);
797 else if (strcmp (element_name, "property") == 0)
799 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
800 CommonInfo *info = state_peek_info (data, CommonInfo);
802 if (!prop_info->data)
804 error_missing_property_value (data, error);
805 free_property_info (prop_info);
806 if (strcmp (info->tag.name, "object") == 0)
807 free_object_info((ObjectInfo*)info);
811 /* Normal properties */
812 if (strcmp (info->tag.name, "object") == 0)
814 ObjectInfo *object_info = (ObjectInfo*)info;
815 object_info->properties =
816 g_slist_prepend (object_info->properties, prop_info);
819 g_assert_not_reached ();
821 else if (strcmp (element_name, "child") == 0)
823 ChildInfo *child_info = state_pop_info (data, ChildInfo);
825 _gtk_builder_add (data->builder, child_info);
827 free_child_info (child_info);
829 else if (strcmp (element_name, "signal") == 0)
831 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
832 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
833 signal_info->object_name = g_strdup (object_info->id);
834 object_info->signals =
835 g_slist_prepend (object_info->signals, signal_info);
837 else if (strcmp (element_name, "interface") == 0)
840 else if (strcmp (element_name, "placeholder") == 0)
845 g_assert_not_reached ();
849 /* This function is taken from gettext.h
850 * GNU gettext uses '\004' to separate context and msgid in .mo files.
853 dpgettext (const char *domain,
857 size_t msgctxt_len = strlen (msgctxt) + 1;
858 size_t msgid_len = strlen (msgid) + 1;
859 const char *translation;
862 msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
864 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
865 msg_ctxt_id[msgctxt_len - 1] = '\004';
866 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
868 translation = dgettext (domain, msg_ctxt_id);
870 if (translation == msg_ctxt_id)
872 /* try the old way of doing message contexts, too */
873 msg_ctxt_id[msgctxt_len - 1] = '|';
874 translation = dgettext (domain, msg_ctxt_id);
876 if (translation == msg_ctxt_id)
883 /* Called for character data */
884 /* text is not nul-terminated */
886 text (GMarkupParseContext *context,
892 ParserData *data = (ParserData*)user_data;
895 if (data->subparser && data->subparser->start)
897 if (data->subparser->parser->text)
898 data->subparser->parser->text (context, text, text_len,
899 data->subparser->data, error);
906 info = state_peek_info (data, CommonInfo);
907 g_assert (info != NULL);
909 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
911 PropertyInfo *prop_info = (PropertyInfo*)info;
913 /* text is not guaranteed to be null-terminated */
914 char *string = g_strndup (text, text_len);
916 if (prop_info->translatable && text_len)
918 if (prop_info->context)
919 text = dpgettext (data->domain, prop_info->context, string);
921 text = dgettext (data->domain, string);
923 prop_info->data = g_strdup (text);
927 prop_info->data = string;
931 static const GMarkupParser parser = {
940 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
941 const gchar *filename,
949 data = g_new0 (ParserData, 1);
950 data->builder = builder;
951 data->filename = filename;
952 data->domain = g_strdup (gtk_builder_get_translation_domain (builder));
954 data->ctx = g_markup_parse_context_new (
955 &parser, G_MARKUP_TREAT_CDATA_AS_TEXT, data, NULL);
957 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
960 gtk_builder_set_translation_domain (data->builder, data->domain);
961 _gtk_builder_finish (builder);
963 /* Custom parser_finished */
964 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
965 for (l = data->custom_finalizers; l; l = l->next)
967 SubParser *sub = (SubParser*)l->data;
969 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
974 free_subparser (sub);
977 /* Common parser_finished, for all created objects */
978 data->finalizers = g_slist_reverse (data->finalizers);
979 for (l = data->finalizers; l; l = l->next)
981 GtkBuildable *buildable = (GtkBuildable*)l->data;
982 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
986 g_markup_parse_context_free (data->ctx);
988 g_slist_free (data->stack);
989 g_slist_free (data->custom_finalizers);
990 g_slist_free (data->finalizers);
991 g_free (data->domain);