]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilderparser.c
ce4b7dbc912bfcce1f869577b207247c99bc489b
[~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 gboolean
143 _gtk_builder_boolean_from_string (const gchar  *string,
144                                   gboolean     *value,
145                                   GError      **error)
146 {
147   gboolean retval = TRUE;
148   int length;
149   
150   g_assert (string != NULL);
151   length = strlen (string);
152   
153   if (length == 0)
154     retval = FALSE;
155   else if (length == 1)
156     {
157       gchar c = g_ascii_tolower (string[0]);
158       if (c == 'y' || c == 't' || c == '1')
159         *value = TRUE;
160       else if (c == 'n' || c == 'f' || c == '0')
161         *value = FALSE;
162       else
163         retval = FALSE;
164     }
165   else
166     {
167       gchar *lower = g_ascii_strdown (string, length);
168       
169       if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
170         *value = TRUE;
171       else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
172         *value = FALSE;
173       else
174         retval = FALSE;
175       g_free (lower);
176     }
177   
178   if (!retval)
179     g_set_error (error,
180                  GTK_BUILDER_ERROR,
181                  GTK_BUILDER_ERROR_INVALID_VALUE,
182                  "could not parse boolean `%s'",
183                  string);
184
185   return retval;
186 }
187
188 static GObject *
189 builder_construct (ParserData *data,
190                    ObjectInfo *object_info)
191 {
192   GObject *object;
193
194   g_assert (object_info != NULL);
195
196   if (object_info->object)
197     return object_info->object;
198
199   object_info->properties = g_slist_reverse (object_info->properties);
200
201   object = _gtk_builder_construct (data->builder, object_info);
202   g_assert (G_IS_OBJECT (object));
203
204   object_info->object = object;
205
206   return object;
207 }
208
209 static gchar *
210 _get_type_by_symbol (const gchar* symbol)
211 {
212   static GModule *module = NULL;
213   GTypeGetFunc func;
214   GType type;
215   
216   if (!module)
217     module = g_module_open (NULL, 0);
218
219   if (!g_module_symbol (module, symbol, (gpointer)&func))
220     return NULL;
221   
222   type = func ();
223   if (type == G_TYPE_INVALID)
224     return NULL;
225
226   return g_strdup (g_type_name (type));
227 }
228
229 static void
230 parse_object (ParserData   *data,
231               const gchar  *element_name,
232               const gchar **names,
233               const gchar **values,
234               GError      **error)
235 {
236   ObjectInfo *object_info;
237   ChildInfo* child_info;
238   int i;
239   gchar *object_class = NULL;
240   gchar *object_id = NULL;
241   gchar *constructor = NULL;
242
243   child_info = state_peek_info (data, ChildInfo);
244   if (child_info && strcmp (child_info->tag.name, "object") == 0)
245     {
246       error_invalid_tag (data, element_name, NULL, error);
247       return;
248     }
249
250   for (i = 0; names[i] != NULL; i++)
251     {
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)
259         {
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
263            */
264           object_class = _get_type_by_symbol (values[i]);
265           if (!object_class)
266             {
267               g_set_error (error, GTK_BUILDER_ERROR, 
268                            GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
269                            _("Invalid type function: `%s'"),
270                            values[i]);
271               return;
272             }
273         }
274       else
275         {
276           error_invalid_attribute (data, element_name, names[i], error);
277           return;
278         }
279     }
280
281   if (!object_class)
282     {
283       error_missing_attribute (data, element_name, "class", error);
284       return;
285     }
286
287   if (!object_id)
288     {
289       error_missing_attribute (data, element_name, "id", error);
290       return;
291     }
292
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;
299
300   if (child_info)
301     object_info->parent = (CommonInfo*)child_info;
302 }
303
304 static void
305 free_object_info (ObjectInfo *info)
306 {
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);
314   g_free (info->id);
315   g_slice_free (ObjectInfo, info);
316 }
317
318 static void
319 parse_child (ParserData   *data,
320              const gchar  *element_name,
321              const gchar **names,
322              const gchar **values,
323              GError      **error)
324
325 {
326   ObjectInfo* object_info;
327   ChildInfo *child_info;
328   guint i;
329
330   object_info = state_peek_info (data, ObjectInfo);
331   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
332     {
333       error_invalid_tag (data, element_name, NULL, error);
334       return;
335     }
336   
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++)
341     {
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]);
346       else
347         error_invalid_attribute (data, element_name, names[i], error);
348     }
349
350   child_info->parent = (CommonInfo*)object_info;
351
352   object_info->object = builder_construct (data, object_info);
353 }
354
355 static void
356 free_child_info (ChildInfo *info)
357 {
358   g_free (info->type);
359   g_free (info->internal_child);
360   g_slice_free (ChildInfo, info);
361 }
362
363 static void
364 parse_property (ParserData   *data,
365                 const gchar  *element_name,
366                 const gchar **names,
367                 const gchar **values,
368                 GError      **error)
369 {
370   PropertyInfo *info;
371   gchar *name = NULL;
372   gchar *context = NULL;
373   gboolean translatable = FALSE;
374   ObjectInfo *object_info;
375   int i;
376
377   object_info = state_peek_info (data, ObjectInfo);
378   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
379     {
380       error_invalid_tag (data, element_name, NULL, error);
381       return;
382     }
383
384   for (i = 0; names[i] != NULL; i++)
385     {
386       if (strcmp (names[i], "name") == 0)
387         name = g_strdelimit (g_strdup (values[i]), "_", '-');
388       else if (strcmp (names[i], "translatable") == 0)
389         {
390           if (!_gtk_builder_boolean_from_string (values[i], &translatable,
391                                                  error))
392             return;
393         }
394       else if (strcmp (names[i], "comments") == 0)
395         {
396           /* do nothing, comments are for translators */
397         }
398       else if (strcmp (names[i], "context") == 0) 
399         {
400           context = g_strdup (values[i]);
401         }
402       else
403         {
404           error_invalid_attribute (data, element_name, names[i], error);
405           return;
406         }
407     }
408
409   if (!name)
410     {
411       error_missing_attribute (data, element_name, "name", error);
412       return;
413     }
414
415   info = g_slice_new0 (PropertyInfo);
416   info->name = name;
417   info->translatable = translatable;
418   info->context = context;
419   info->text = g_string_new ("");
420   state_push (data, info);
421
422   info->tag.name = element_name;
423 }
424
425 static void
426 free_property_info (PropertyInfo *info)
427 {
428   g_free (info->data);
429   g_free (info->name);
430   g_slice_free (PropertyInfo, info);
431 }
432
433 static void
434 parse_signal (ParserData   *data,
435               const gchar  *element_name,
436               const gchar **names,
437               const gchar **values,
438               GError      **error)
439 {
440   SignalInfo *info;
441   gchar *name = NULL;
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;
448   int i;
449
450   object_info = state_peek_info (data, ObjectInfo);
451   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
452     {
453       error_invalid_tag (data, element_name, NULL, error);
454       return;
455     }
456
457   for (i = 0; names[i] != NULL; i++)
458     {
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)
464         {
465           if (!_gtk_builder_boolean_from_string (values[i], &after, error))
466             return;
467         }
468       else if (strcmp (names[i], "swapped") == 0)
469         {
470           if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
471             return;
472           swapped_set = TRUE;
473         }
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 */
478         ;
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)
538         {
539           g_free (data->domain);
540           data->domain = g_strdup (values[i]);
541           break;
542         }
543       else
544         error_invalid_attribute (data, "interface", names[i], error);
545     }
546 }
547
548 static SubParser *
549 create_subparser (GObject       *object,
550                   GObject       *child,
551                   const gchar   *element_name,
552                   GMarkupParser *parser,
553                   gpointer       user_data)
554 {
555   SubParser *subparser;
556
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;
564
565   return subparser;
566 }
567
568 static void
569 free_subparser (SubParser *subparser)
570 {
571   g_free (subparser->tagname);
572   g_slice_free (SubParser, subparser);
573 }
574
575 static gboolean
576 subparser_start (GMarkupParseContext *context,
577                  const gchar         *element_name,
578                  const gchar        **names,
579                  const gchar        **values,
580                  ParserData          *data,
581                  GError             **error)
582 {
583   SubParser *subparser = data->subparser;
584
585   if (!subparser->start &&
586       strcmp (element_name, subparser->tagname) == 0)
587     subparser->start = element_name;
588
589   if (subparser->start)
590     {
591       if (subparser->parser->start_element)
592         subparser->parser->start_element (context,
593                                           element_name, names, values,
594                                           subparser->data, error);
595       return FALSE;
596     }
597   return TRUE;
598 }
599
600 static void
601 subparser_end (GMarkupParseContext *context,
602                const gchar         *element_name,
603                ParserData          *data,
604                GError             **error)
605 {
606   if (data->subparser->parser->end_element)
607     data->subparser->parser->end_element (context, element_name,
608                                           data->subparser->data, error);
609   
610   if (!strcmp (data->subparser->start, element_name) == 0)
611     return;
612     
613   gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
614                                 data->builder,
615                                 data->subparser->child,
616                                 element_name,
617                                 data->subparser->data);
618   g_free (data->subparser->parser);
619       
620   if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
621     data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
622                                                data->subparser);
623   else
624     free_subparser (data->subparser);
625   
626   data->subparser = NULL;
627 }
628
629 static gboolean
630 parse_custom (GMarkupParseContext *context,
631               const gchar         *element_name,
632               const gchar        **names,
633               const gchar        **values,
634               ParserData          *data,
635               GError             **error)
636 {
637   CommonInfo* parent_info;
638   GMarkupParser parser;
639   gpointer *subparser_data;
640   GObject *object;
641   GObject *child;
642
643   parent_info = state_peek_info (data, CommonInfo);
644   if (!parent_info)
645     return FALSE;
646
647   if (strcmp (parent_info->tag.name, "object") == 0)
648     {
649       ObjectInfo* object_info = (ObjectInfo*)parent_info;
650       if (!object_info->object)
651         object_info->object = _gtk_builder_construct (data->builder,
652                                                       object_info);
653       g_assert (object_info->object);
654       object = object_info->object;
655       child = NULL;
656     }
657   else if (strcmp (parent_info->tag.name, "child") == 0)
658     {
659       ChildInfo* child_info = (ChildInfo*)parent_info;
660       
661       _gtk_builder_add (data->builder, child_info);
662       
663       object = ((ObjectInfo*)child_info->parent)->object;
664       child  = child_info->object;
665     }
666   else
667     return FALSE;
668
669   if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
670                                        data->builder,
671                                        child,
672                                        element_name,
673                                        &parser,
674                                        (gpointer*)&subparser_data))
675     return FALSE;
676       
677   data->subparser = create_subparser (object, child, element_name,
678                                       &parser, subparser_data);
679   
680   if (parser.start_element)
681     parser.start_element (context,
682                           element_name, names, values,
683                           subparser_data, error);
684   return TRUE;
685 }
686
687 static void
688 start_element (GMarkupParseContext *context,
689                const gchar         *element_name,
690                const gchar        **names,
691                const gchar        **values,
692                gpointer             user_data,
693                GError             **error)
694 {
695   ParserData *data = (ParserData*)user_data;
696
697 #ifdef GTK_ENABLE_DEBUG
698   if (gtk_debug_flags & GTK_DEBUG_BUILDER)
699     {
700       GString *tags = g_string_new ("");
701       int i;
702       for (i = 0; names[i]; i++)
703         g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
704
705       if (i)
706         {
707           g_string_insert_c (tags, 0, ' ');
708           g_string_truncate (tags, tags->len - 1);
709         }
710       g_print ("<%s%s>\n", element_name, tags->str);
711       g_string_free (tags, TRUE);
712     }
713 #endif
714
715   if (!data->last_element && strcmp (element_name, "interface") != 0)
716     {
717       g_set_error (error, GTK_BUILDER_ERROR, 
718                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
719                    _("Invalid root element: '%s'"),
720                    element_name);
721       return;
722     }
723   data->last_element = element_name;
724
725   if (data->subparser)
726     if (!subparser_start (context, element_name, names, values,
727                           data, error))
728       return;
729   
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)
741     {
742       /* placeholder has no special treatmeant, but it needs an
743        * if clause to avoid an error below.
744        */
745     }
746   else
747     if (!parse_custom (context, element_name, names, values,
748                        data, error))
749       g_set_error (error, GTK_BUILDER_ERROR, 
750                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
751                    _("Unhandled tag: '%s'"),
752                    element_name);
753 }
754
755 /* This function is taken from gettext.h 
756  * GNU gettext uses '\004' to separate context and msgid in .mo files.
757  */
758 static const char *
759 dpgettext (const char *domain,
760            const char *msgctxt,
761            const char *msgid)
762 {
763   size_t msgctxt_len = strlen (msgctxt) + 1;
764   size_t msgid_len = strlen (msgid) + 1;
765   const char *translation;
766   char* msg_ctxt_id;
767
768   msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
769   
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);
773
774   translation = dgettext (domain, msg_ctxt_id);
775
776   if (translation == msg_ctxt_id) 
777     {
778       /* try the old way of doing message contexts, too */
779       msg_ctxt_id[msgctxt_len - 1] = '|';
780       translation = dgettext (domain, msg_ctxt_id);
781   
782       if (translation == msg_ctxt_id)
783         return msgid;
784     }
785  
786   return translation;
787 }
788
789 /* Called for close tags </foo> */
790 static void
791 end_element (GMarkupParseContext *context,
792              const gchar         *element_name,
793              gpointer             user_data,
794              GError             **error)
795 {
796   ParserData *data = (ParserData*)user_data;
797
798   GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
799
800   if (data->subparser && data->subparser->start)
801     {
802       subparser_end (context, element_name, data, error);
803       return;
804     }
805
806   if (strcmp (element_name, "object") == 0)
807     {
808       ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
809       ChildInfo* child_info = state_peek_info (data, ChildInfo);
810
811       object_info->object = builder_construct (data, object_info);
812
813       if (child_info)
814         child_info->object = object_info->object;
815
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);
820
821       free_object_info (object_info);
822     }
823   else if (strcmp (element_name, "property") == 0)
824     {
825       PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
826       CommonInfo *info = state_peek_info (data, CommonInfo);
827
828       /* Normal properties */
829       if (strcmp (info->tag.name, "object") == 0)
830         {
831           ObjectInfo *object_info = (ObjectInfo*)info;
832
833           if (prop_info->translatable && prop_info->text->len)
834             {
835               const char *text;
836
837               if (prop_info->context)
838                 text = dpgettext (data->domain,
839                                   prop_info->context,
840                                   prop_info->text->str);
841               else
842                 text = dgettext (data->domain, prop_info->text->str);
843
844               prop_info->data = g_strdup (text);
845               g_string_free (prop_info->text, TRUE);
846             }
847           else
848             {
849               prop_info->data = prop_info->text->str;
850               g_string_free (prop_info->text, FALSE);
851             }
852
853           object_info->properties =
854             g_slist_prepend (object_info->properties, prop_info);
855         }
856       else
857         g_assert_not_reached ();
858     }
859   else if (strcmp (element_name, "child") == 0)
860     {
861       ChildInfo *child_info = state_pop_info (data, ChildInfo);
862
863       _gtk_builder_add (data->builder, child_info);
864
865       free_child_info (child_info);
866     }
867   else if (strcmp (element_name, "signal") == 0)
868     {
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);
874     }
875   else if (strcmp (element_name, "interface") == 0)
876     {
877     }
878   else if (strcmp (element_name, "placeholder") == 0)
879     {
880     }
881   else
882     {
883       g_assert_not_reached ();
884     }
885 }
886
887 /* Called for character data */
888 /* text is not nul-terminated */
889 static void
890 text (GMarkupParseContext *context,
891       const gchar         *text,
892       gsize                text_len,
893       gpointer             user_data,
894       GError             **error)
895 {
896   ParserData *data = (ParserData*)user_data;
897   CommonInfo *info;
898
899   if (data->subparser && data->subparser->start)
900     {
901       if (data->subparser->parser->text)
902         data->subparser->parser->text (context, text, text_len,
903                                        data->subparser->data, error);
904       return;
905     }
906
907   if (!data->stack)
908     return;
909
910   info = state_peek_info (data, CommonInfo);
911   g_assert (info != NULL);
912
913   if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
914     {
915       PropertyInfo *prop_info = (PropertyInfo*)info;
916
917       g_string_append_len (prop_info->text, text, text_len);
918     }
919 }
920
921 static void
922 free_info (CommonInfo *info)
923 {
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);
932   else 
933     g_assert_not_reached ();
934 }
935
936 static const GMarkupParser parser = {
937   start_element,
938   end_element,
939   text,
940   NULL,
941   NULL
942 };
943
944 void
945 _gtk_builder_parser_parse_buffer (GtkBuilder   *builder,
946                                   const gchar  *filename,
947                                   const gchar  *buffer,
948                                   gsize         length,
949                                   GError      **error)
950 {
951   ParserData *data;
952   GSList *l;
953   
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));
958
959   data->ctx = g_markup_parse_context_new (&parser, 
960                                           G_MARKUP_TREAT_CDATA_AS_TEXT, 
961                                           data, NULL);
962
963   if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
964     goto out;
965   
966   _gtk_builder_finish (builder);
967
968   /* Custom parser_finished */
969   data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
970   for (l = data->custom_finalizers; l; l = l->next)
971     {
972       SubParser *sub = (SubParser*)l->data;
973       
974       gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
975                                      builder,
976                                      sub->child,
977                                      sub->tagname,
978                                      sub->data);
979     }
980   
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)
984     {
985       GtkBuildable *buildable = (GtkBuildable*)l->data;
986       gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
987     }
988
989  out:
990
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);
998   g_free (data);
999 }