]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilderparser.c
Move type-func to <object> instead of <child>, add a test to make sure
[~andy/gtk] / gtk / gtkbuilderparser.c
1 /* gtkbuilderparser.c
2  * Copyright (C) 2006-2007 Async Open Source,
3  *                         Johan Dahlin <jdahlin@async.com.br>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <string.h>
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>
29 #include <gmodule.h>
30
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"
37 #include "gtkdebug.h"
38 #include "gtktypeutils.h"
39 #include "gtkalias.h"
40
41 static void free_property_info (PropertyInfo *info);
42 static void free_object_info (ObjectInfo *info);
43
44 static inline void
45 state_push (ParserData *data, gpointer info)
46 {
47   data->stack = g_slist_prepend (data->stack, info);
48 }
49
50 static inline gpointer
51 state_peek (ParserData *data)
52 {
53   if (!data->stack)
54     return NULL;
55
56   return data->stack->data;
57 }
58
59 static inline gpointer
60 state_pop (ParserData *data)
61 {
62   gpointer old = NULL;
63
64   if (!data->stack)
65     return NULL;
66
67   old = data->stack->data;
68   data->stack = g_slist_delete_link (data->stack, data->stack);
69   return old;
70 }
71 #define state_peek_info(data, st) ((st*)state_peek(data))
72 #define state_pop_info(data, st) ((st*)state_pop(data))
73
74 static void
75 error_missing_attribute (ParserData *data,
76                          const gchar *tag,
77                          const gchar *attribute,
78                          GError **error)
79 {
80   gint line_number, char_number;
81
82   g_markup_parse_context_get_position (data->ctx,
83                                        &line_number,
84                                        &char_number);
85
86   g_set_error (error,
87                GTK_BUILDER_ERROR,
88                GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
89                "%s:%d:%d <%s> requires attribute \"%s\"",
90                data->filename,
91                line_number, char_number, tag, attribute);
92 }
93
94 static void
95 error_invalid_attribute (ParserData *data,
96                          const gchar *tag,
97                          const gchar *attribute,
98                          GError **error)
99 {
100   gint line_number, char_number;
101
102   g_markup_parse_context_get_position (data->ctx,
103                                        &line_number,
104                                        &char_number);
105
106   g_set_error (error,
107                GTK_BUILDER_ERROR,
108                GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
109                "%s:%d:%d '%s' is not a valid attribute of <%s>",
110                data->filename,
111                line_number, char_number, attribute, tag);
112 }
113
114 static void
115 error_invalid_tag (ParserData *data,
116                    const gchar *tag,
117                    const gchar *expected,
118                    GError **error)
119 {
120   gint line_number, char_number;
121
122   g_markup_parse_context_get_position (data->ctx,
123                                        &line_number,
124                                        &char_number);
125
126   if (expected)
127     g_set_error (error,
128                  GTK_BUILDER_ERROR,
129                  GTK_BUILDER_ERROR_INVALID_TAG,
130                  "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
131                  data->filename,
132                  line_number, char_number, tag, expected);
133   else
134     g_set_error (error,
135                  GTK_BUILDER_ERROR,
136                  GTK_BUILDER_ERROR_INVALID_TAG,
137                  "%s:%d:%d '%s' is not a valid tag here",
138                  data->filename,
139                  line_number, char_number, tag);
140 }
141
142 static void
143 error_missing_property_value (ParserData *data,
144                               GError **error)
145 {
146   gint line_number, char_number;
147
148   g_markup_parse_context_get_position (data->ctx,
149                                        &line_number,
150                                        &char_number);
151
152   g_set_error (error,
153                GTK_BUILDER_ERROR,
154                GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE,
155                "%s:%d:%d <property> must have a value set",
156                data->filename,
157                line_number, char_number);
158 }
159
160 gboolean
161 _gtk_builder_parse_boolean (const gchar  *string,
162                             gboolean     *value,
163                             GError      **error)
164 {
165   gboolean retval = TRUE;
166   int i;
167   int length;
168   
169   g_assert (string != NULL);
170   length = strlen (string);
171   
172   if (length == 0)
173     retval = FALSE;
174   else if (length == 1)
175     {
176       gchar c = g_ascii_tolower (string[0]);
177       if (c == 'y' || c == 't' || c == '1')
178         *value = TRUE;
179       else if (c == 'n' || c == 'f' || c == '0')
180         *value = FALSE;
181       else
182         retval = FALSE;
183     }
184   else
185     {
186       gchar *lower = g_strdup (string);
187       for (i = 0; i < strlen (string); i++)
188         lower[i] = g_ascii_tolower (string[i]);
189       
190       if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
191         *value = TRUE;
192       else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
193         *value = FALSE;
194       else
195         retval = FALSE;
196       g_free (lower);
197     }
198   
199   if (!retval)
200     g_set_error (error,
201                  GTK_BUILDER_ERROR,
202                  GTK_BUILDER_ERROR_INVALID_VALUE,
203                  "could not parse boolean `%s'",
204                  string);
205
206   return retval;
207 }
208
209 static GObject *
210 builder_construct (ParserData *data,
211                    ObjectInfo *object_info)
212 {
213   GObject *object;
214
215   g_assert (object_info != NULL);
216
217   if (object_info->object)
218     return object_info->object;
219
220   object_info->properties = g_slist_reverse (object_info->properties);
221
222   object = _gtk_builder_construct (data->builder, object_info);
223   g_assert (G_IS_OBJECT (object));
224
225   object_info->object = object;
226
227   return object;
228 }
229
230 static gchar *
231 _get_type_by_symbol (const gchar* symbol)
232 {
233   static GModule *module = NULL;
234   GTypeGetFunc func;
235   GType type;
236   
237   if (!module)
238     module = g_module_open (NULL, 0);
239
240   if (!g_module_symbol (module, symbol, (gpointer)&func))
241     return NULL;
242   
243   type = func ();
244   if (type == G_TYPE_INVALID)
245     return NULL;
246
247   return g_strdup (g_type_name (type));
248 }
249
250 static void
251 parse_object (ParserData   *data,
252               const gchar  *element_name,
253               const gchar **names,
254               const gchar **values,
255               GError      **error)
256 {
257   ObjectInfo *object_info;
258   ChildInfo* child_info;
259   int i;
260   gchar *object_class = NULL;
261   gchar *object_id = NULL;
262   gchar *constructor = NULL;
263
264   child_info = state_peek_info (data, ChildInfo);
265   if (child_info && strcmp (child_info->tag.name, "object") == 0)
266     {
267       error_invalid_tag (data, element_name, NULL, error);
268       if (child_info)
269         free_object_info ((ObjectInfo*)child_info);
270       return;
271     }
272
273   for (i = 0; names[i] != NULL; i++)
274     {
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)
282         {
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
286            */
287           object_class = _get_type_by_symbol (values[i]);
288           if (!object_class)
289             {
290               g_set_error (error, GTK_BUILDER_ERROR, 
291                            GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
292                            _("Invalid type function: `%s'"),
293                            values[i]);
294               return;
295             }
296         }
297       else
298         {
299           error_invalid_attribute (data, element_name, names[i], error);
300           return;
301         }
302     }
303
304   if (!object_class)
305     {
306       error_missing_attribute (data, element_name, "class", error);
307       return;
308     }
309
310   if (!object_id)
311     {
312       error_missing_attribute (data, element_name, "id", error);
313       return;
314     }
315
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;
323
324   if (child_info)
325     object_info->parent = (CommonInfo*)child_info;
326 }
327
328 static void
329 free_object_info (ObjectInfo *info)
330 {
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);
338   g_free (info->id);
339   g_slice_free (ObjectInfo, info);
340 }
341
342 static void
343 parse_child (ParserData   *data,
344              const gchar  *element_name,
345              const gchar **names,
346              const gchar **values,
347              GError      **error)
348
349 {
350   ObjectInfo* object_info;
351   ChildInfo *child_info;
352   guint i;
353
354   object_info = state_peek_info (data, ObjectInfo);
355   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
356     {
357       error_invalid_tag (data, element_name, "object", error);
358       if (object_info)
359         free_object_info (object_info);
360       return;
361     }
362   
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++)
368     {
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]);
373       else
374         error_invalid_attribute (data, element_name, names[i], error);
375     }
376
377   child_info->parent = (CommonInfo*)object_info;
378
379   object_info->object = builder_construct (data, object_info);
380 }
381
382 static void
383 free_child_info (ChildInfo *info)
384 {
385   g_free (info->type);
386   g_free (info->internal_child);
387   g_slice_free (ChildInfo, info);
388 }
389
390 static void
391 parse_property (ParserData   *data,
392                 const gchar  *element_name,
393                 const gchar **names,
394                 const gchar **values,
395                 GError      **error)
396 {
397   PropertyInfo *info;
398   gchar *name = NULL;
399   gboolean translatable = FALSE;
400   int i;
401
402   g_assert (data->stack != NULL);
403
404   for (i = 0; names[i] != NULL; i++)
405     {
406       if (strcmp (names[i], "name") == 0)
407         name = g_strdelimit (g_strdup (values[i]), "_", '-');
408       else if (strcmp (names[i], "translatable") == 0)
409         {
410           if (!_gtk_builder_parse_boolean (values[i], &translatable, error))
411             return;
412         }
413       else
414         {
415           error_invalid_attribute (data, element_name, names[i], error);
416           return;
417         }
418     }
419
420   if (!name)
421     {
422       error_missing_attribute (data, element_name, "name", error);
423       return;
424     }
425
426   info = g_slice_new0 (PropertyInfo);
427   info->name = name;
428   info->translatable = translatable;
429   state_push (data, info);
430
431   info->tag.name = element_name;
432 }
433
434 static void
435 free_property_info (PropertyInfo *info)
436 {
437   g_free (info->data);
438   g_free (info->name);
439   g_slice_free (PropertyInfo, info);
440 }
441
442 static void
443 parse_signal (ParserData   *data,
444               const gchar  *element_name,
445               const gchar **names,
446               const gchar **values,
447               GError      **error)
448 {
449   SignalInfo *info;
450   gchar *name = NULL;
451   gchar *handler = NULL;
452   gchar *object = NULL;
453   gboolean after = FALSE;
454   gboolean swapped = FALSE;
455   gboolean swapped_set = FALSE;
456   int i;
457
458   g_assert (data->stack != NULL);
459
460   for (i = 0; names[i] != NULL; i++)
461     {
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)
467         {
468           if (!_gtk_builder_parse_boolean (values[i], &after, error))
469             return;
470         }
471       else if (strcmp (names[i], "swapped") == 0)
472         {
473           if (!_gtk_builder_parse_boolean (values[i], &swapped, error))
474             return;
475           swapped_set = TRUE;
476         }
477       else if (strcmp (names[i], "object") == 0)
478         object = g_strdup (values[i]);
479       else
480         {
481           error_invalid_attribute (data, element_name, names[i], error);
482           return;
483         }
484     }
485
486   if (!name)
487     {
488       error_missing_attribute (data, element_name, "name", error);
489       return;
490     }
491   else if (!handler)
492     {
493       error_missing_attribute (data, element_name, "handler", error);
494       return;
495     }
496
497   /* Swapped defaults to FALSE except when object is set */
498   if (object && !swapped_set)
499     swapped = TRUE;
500   
501   info = g_slice_new0 (SignalInfo);
502   info->name = name;
503   info->handler = handler;
504   if (after)
505     info->flags |= G_CONNECT_AFTER;
506   if (swapped)
507     info->flags |= G_CONNECT_SWAPPED;
508   info->connect_object_name = object;
509   state_push (data, info);
510
511   info->tag.name = element_name;
512 }
513
514 /* Called by GtkBuilder */
515 void
516 _free_signal_info (SignalInfo *info,
517                    gpointer user_data)
518 {
519   g_free (info->name);
520   g_free (info->handler);
521   g_free (info->connect_object_name);
522   g_free (info->object_name);
523   g_slice_free (SignalInfo, info);
524 }
525
526 static void
527 parse_interface (ParserData   *data,
528                  const gchar  *element_name,
529                  const gchar **names,
530                  const gchar **values,
531                  GError      **error)
532 {
533   int i;
534
535   for (i = 0; names[i] != NULL; i++)
536     {
537       if (strcmp (names[i], "domain") == 0 && !data->domain)
538         {
539           data->domain = g_strdup (values[i]);
540           break;
541         }
542       else
543         error_invalid_attribute (data, "interface", names[i], error);
544     }
545 }
546
547 static SubParser *
548 create_subparser (GObject       *object,
549                   GObject       *child,
550                   const gchar   *element_name,
551                   GMarkupParser *parser,
552                   gpointer       user_data)
553 {
554   SubParser *subparser;
555
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;
563
564   return subparser;
565 }
566
567 static void
568 free_subparser (SubParser *subparser)
569 {
570   g_free (subparser->tagname);
571   g_slice_free (SubParser, subparser);
572 }
573
574 static gboolean
575 subparser_start (GMarkupParseContext *context,
576                  const gchar         *element_name,
577                  const gchar        **names,
578                  const gchar        **values,
579                  ParserData          *data,
580                  GError             **error)
581 {
582   SubParser *subparser = data->subparser;
583
584   if (!subparser->start &&
585       strcmp (element_name, subparser->tagname) == 0)
586     subparser->start = element_name;
587
588   if (subparser->start)
589     {
590       if (subparser->parser->start_element)
591         subparser->parser->start_element (context,
592                                           element_name, names, values,
593                                           subparser->data, error);
594       return FALSE;
595     }
596   return TRUE;
597 }
598
599 static void
600 subparser_end (GMarkupParseContext *context,
601                const gchar         *element_name,
602                ParserData          *data,
603                GError             **error)
604 {
605   if (data->subparser->parser->end_element)
606     data->subparser->parser->end_element (context, element_name,
607                                           data->subparser->data, error);
608   
609   if (!strcmp (data->subparser->start, element_name) == 0)
610     return;
611     
612   gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
613                                 data->builder,
614                                 data->subparser->child,
615                                 element_name,
616                                 data->subparser->data);
617   g_free (data->subparser->parser);
618       
619   if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
620     data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
621                                                data->subparser);
622   else
623     free_subparser (data->subparser);
624   
625   data->subparser = NULL;
626 }
627
628 static gboolean
629 parse_custom (GMarkupParseContext *context,
630               const gchar         *element_name,
631               const gchar        **names,
632               const gchar        **values,
633               ParserData          *data,
634               GError             **error)
635 {
636   CommonInfo* parent_info;
637   GMarkupParser parser;
638   gpointer *subparser_data;
639   GObject *object;
640   GObject *child;
641
642   parent_info = state_peek_info (data, CommonInfo);
643   if (!parent_info)
644     return FALSE;
645
646   if (strcmp (parent_info->tag.name, "object") == 0)
647     {
648       ObjectInfo* object_info = (ObjectInfo*)parent_info;
649       if (!object_info->object)
650         object_info->object = _gtk_builder_construct (data->builder,
651                                                       object_info);
652       g_assert (object_info->object);
653       object = object_info->object;
654       child = NULL;
655     }
656   else if (strcmp (parent_info->tag.name, "child") == 0)
657     {
658       ChildInfo* child_info = (ChildInfo*)parent_info;
659       
660       _gtk_builder_add (data->builder, child_info);
661       
662       object = ((ObjectInfo*)child_info->parent)->object;
663       child  = child_info->object;
664     }
665   else
666     return FALSE;
667
668   if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
669                                        data->builder,
670                                        child,
671                                        element_name,
672                                        &parser,
673                                        (gpointer*)&subparser_data))
674     return FALSE;
675       
676   data->subparser = create_subparser (object, child, element_name,
677                                       &parser, subparser_data);
678   
679   if (parser.start_element)
680     parser.start_element (context,
681                           element_name, names, values,
682                           subparser_data, error);
683   return TRUE;
684 }
685
686 static void
687 start_element (GMarkupParseContext *context,
688                const gchar         *element_name,
689                const gchar        **names,
690                const gchar        **values,
691                gpointer             user_data,
692                GError             **error)
693 {
694   ParserData *data = (ParserData*)user_data;
695
696 #ifdef GTK_ENABLE_DEBUG
697   if (gtk_debug_flags & GTK_DEBUG_BUILDER)
698     {
699       GString *tags = g_string_new ("");
700       int i;
701       for (i = 0; names[i]; i++)
702         g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
703
704       if (i)
705         {
706           g_string_insert_c (tags, 0, ' ');
707           g_string_truncate (tags, tags->len - 1);
708         }
709       g_print ("<%s%s>\n", element_name, tags->str);
710       g_string_free (tags, TRUE);
711     }
712 #endif
713
714   if (!data->last_element && strcmp (element_name, "interface") != 0)
715     {
716       g_set_error (error, GTK_BUILDER_ERROR, 
717                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
718                    _("Invalid root element: '%s'"),
719                    element_name);
720       return;
721     }
722   data->last_element = element_name;
723
724   if (data->subparser)
725     if (!subparser_start (context, element_name, names, values,
726                           data, error))
727       return;
728   
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)
740     {
741       /* placeholder has no special treatmeant, but it needs an
742        * if clause to avoid an error below.
743        */
744     }
745   else
746     if (!parse_custom (context, element_name, names, values,
747                        data, error))
748       g_set_error (error, GTK_BUILDER_ERROR, 
749                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
750                    _("Unhandled tag: '%s'"),
751                    element_name);
752 }
753
754 /* Called for close tags </foo> */
755 static void
756 end_element (GMarkupParseContext *context,
757              const gchar         *element_name,
758              gpointer             user_data,
759              GError             **error)
760 {
761   ParserData *data = (ParserData*)user_data;
762
763   GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
764
765   if (data->subparser && data->subparser->start)
766     {
767       subparser_end (context, element_name, data, error);
768       return;
769     }
770
771   if (strcmp (element_name, "object") == 0)
772     {
773       ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
774       ChildInfo* child_info = state_peek_info (data, ChildInfo);
775
776       object_info->object = builder_construct (data, object_info);
777
778       if (child_info)
779         child_info->object = object_info->object;
780
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);
785     }
786   else if (strcmp (element_name, "property") == 0)
787     {
788       PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
789       CommonInfo *info = state_peek_info (data, CommonInfo);
790
791       if (!prop_info->data)
792         {
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);
797           return;
798         }
799       
800       /* Normal properties */
801       if (strcmp (info->tag.name, "object") == 0)
802         {
803           ObjectInfo *object_info = (ObjectInfo*)info;
804           object_info->properties =
805             g_slist_prepend (object_info->properties, prop_info);
806         }
807       else
808         g_assert_not_reached ();
809     }
810   else if (strcmp (element_name, "child") == 0)
811     {
812       ChildInfo *child_info = state_pop_info (data, ChildInfo);
813
814       _gtk_builder_add (data->builder, child_info);
815
816       free_child_info (child_info);
817     }
818   else if (strcmp (element_name, "signal") == 0)
819     {
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);
825     }
826   else if (strcmp (element_name, "interface") == 0)
827     {
828     }
829   else if (strcmp (element_name, "placeholder") == 0)
830     {
831     }
832   else
833     {
834       g_assert_not_reached ();
835     }
836 }
837
838 /* Called for character data */
839 /* text is not nul-terminated */
840 static void
841 text (GMarkupParseContext *context,
842       const gchar         *text,
843       gsize                text_len,
844       gpointer             user_data,
845       GError             **error)
846 {
847   ParserData *data = (ParserData*)user_data;
848   CommonInfo *info;
849
850   if (data->subparser && data->subparser->start)
851     {
852       if (data->subparser->parser->text)
853         data->subparser->parser->text (context, text, text_len,
854                                        data->subparser->data, error);
855       return;
856     }
857
858   if (!data->stack)
859     return;
860
861   info = state_peek_info (data, CommonInfo);
862   g_assert (info != NULL);
863
864   if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
865     {
866       PropertyInfo *prop_info = (PropertyInfo*)info;
867
868       if (prop_info->translatable && text_len)
869         {
870           if (data->domain)
871             text = dgettext (data->domain, text);
872           else
873             text = gettext (text);
874         }
875       prop_info->data = g_strndup (text, text_len);
876     }
877 }
878
879 static const GMarkupParser parser = {
880   start_element,
881   end_element,
882   text,
883   NULL,
884   NULL
885 };
886
887 void
888 _gtk_builder_parser_parse_buffer (GtkBuilder   *builder,
889                                   const gchar  *filename,
890                                   const gchar  *buffer,
891                                   gsize         length,
892                                   GError      **error)
893 {
894   ParserData *data;
895   GSList *l;
896   
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));
901
902   data->ctx = g_markup_parse_context_new (
903                   &parser, G_MARKUP_TREAT_CDATA_AS_TEXT, data, NULL);
904
905   if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
906     goto out;
907   
908   gtk_builder_set_translation_domain (data->builder, data->domain);
909   _gtk_builder_finish (builder);
910
911   /* Custom parser_finished */
912   data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
913   for (l = data->custom_finalizers; l; l = l->next)
914     {
915       SubParser *sub = (SubParser*)l->data;
916       
917       gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
918                                      builder,
919                                      sub->child,
920                                      sub->tagname,
921                                      sub->data);
922       free_subparser (sub);
923     }
924   
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)
928     {
929       GtkBuildable *buildable = (GtkBuildable*)l->data;
930       gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
931     }
932
933  out:
934   g_markup_parse_context_free (data->ctx);
935
936   g_slist_free (data->stack);
937   g_slist_free (data->custom_finalizers);
938   g_slist_free (data->finalizers);
939   g_free (data->domain);
940   g_free (data);
941 }