]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilderparser.c
svn path=/trunk/; revision=18842
[~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_boolean_from_string (const gchar  *string,
162                                   gboolean     *value,
163                                   GError      **error)
164 {
165   gboolean retval = TRUE;
166   int length;
167   
168   g_assert (string != NULL);
169   length = strlen (string);
170   
171   if (length == 0)
172     retval = FALSE;
173   else if (length == 1)
174     {
175       gchar c = g_ascii_tolower (string[0]);
176       if (c == 'y' || c == 't' || c == '1')
177         *value = TRUE;
178       else if (c == 'n' || c == 'f' || c == '0')
179         *value = FALSE;
180       else
181         retval = FALSE;
182     }
183   else
184     {
185       gchar *lower = g_ascii_strdown (string, length);
186       
187       if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
188         *value = TRUE;
189       else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
190         *value = FALSE;
191       else
192         retval = FALSE;
193       g_free (lower);
194     }
195   
196   if (!retval)
197     g_set_error (error,
198                  GTK_BUILDER_ERROR,
199                  GTK_BUILDER_ERROR_INVALID_VALUE,
200                  "could not parse boolean `%s'",
201                  string);
202
203   return retval;
204 }
205
206 static GObject *
207 builder_construct (ParserData *data,
208                    ObjectInfo *object_info)
209 {
210   GObject *object;
211
212   g_assert (object_info != NULL);
213
214   if (object_info->object)
215     return object_info->object;
216
217   object_info->properties = g_slist_reverse (object_info->properties);
218
219   object = _gtk_builder_construct (data->builder, object_info);
220   g_assert (G_IS_OBJECT (object));
221
222   object_info->object = object;
223
224   return object;
225 }
226
227 static gchar *
228 _get_type_by_symbol (const gchar* symbol)
229 {
230   static GModule *module = NULL;
231   GTypeGetFunc func;
232   GType type;
233   
234   if (!module)
235     module = g_module_open (NULL, 0);
236
237   if (!g_module_symbol (module, symbol, (gpointer)&func))
238     return NULL;
239   
240   type = func ();
241   if (type == G_TYPE_INVALID)
242     return NULL;
243
244   return g_strdup (g_type_name (type));
245 }
246
247 static void
248 parse_object (ParserData   *data,
249               const gchar  *element_name,
250               const gchar **names,
251               const gchar **values,
252               GError      **error)
253 {
254   ObjectInfo *object_info;
255   ChildInfo* child_info;
256   int i;
257   gchar *object_class = NULL;
258   gchar *object_id = NULL;
259   gchar *constructor = NULL;
260
261   child_info = state_peek_info (data, ChildInfo);
262   if (child_info && strcmp (child_info->tag.name, "object") == 0)
263     {
264       error_invalid_tag (data, element_name, NULL, error);
265       if (child_info)
266         free_object_info ((ObjectInfo*)child_info);
267       return;
268     }
269
270   for (i = 0; names[i] != NULL; i++)
271     {
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)
279         {
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
283            */
284           object_class = _get_type_by_symbol (values[i]);
285           if (!object_class)
286             {
287               g_set_error (error, GTK_BUILDER_ERROR, 
288                            GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
289                            _("Invalid type function: `%s'"),
290                            values[i]);
291               return;
292             }
293         }
294       else
295         {
296           error_invalid_attribute (data, element_name, names[i], error);
297           return;
298         }
299     }
300
301   if (!object_class)
302     {
303       error_missing_attribute (data, element_name, "class", error);
304       return;
305     }
306
307   if (!object_id)
308     {
309       error_missing_attribute (data, element_name, "id", error);
310       return;
311     }
312
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;
320
321   if (child_info)
322     object_info->parent = (CommonInfo*)child_info;
323 }
324
325 static void
326 free_object_info (ObjectInfo *info)
327 {
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);
335   g_free (info->id);
336   g_slice_free (ObjectInfo, info);
337 }
338
339 static void
340 parse_child (ParserData   *data,
341              const gchar  *element_name,
342              const gchar **names,
343              const gchar **values,
344              GError      **error)
345
346 {
347   ObjectInfo* object_info;
348   ChildInfo *child_info;
349   guint i;
350
351   object_info = state_peek_info (data, ObjectInfo);
352   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
353     {
354       error_invalid_tag (data, element_name, "object", error);
355       if (object_info)
356         free_object_info (object_info);
357       return;
358     }
359   
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++)
365     {
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]);
370       else
371         error_invalid_attribute (data, element_name, names[i], error);
372     }
373
374   child_info->parent = (CommonInfo*)object_info;
375
376   object_info->object = builder_construct (data, object_info);
377 }
378
379 static void
380 free_child_info (ChildInfo *info)
381 {
382   g_free (info->type);
383   g_free (info->internal_child);
384   g_slice_free (ChildInfo, info);
385 }
386
387 static void
388 parse_property (ParserData   *data,
389                 const gchar  *element_name,
390                 const gchar **names,
391                 const gchar **values,
392                 GError      **error)
393 {
394   PropertyInfo *info;
395   gchar *name = NULL;
396   gchar *context = NULL;
397   gboolean translatable = FALSE;
398   int i;
399
400   g_assert (data->stack != NULL);
401
402   for (i = 0; names[i] != NULL; i++)
403     {
404       if (strcmp (names[i], "name") == 0)
405         name = g_strdelimit (g_strdup (values[i]), "_", '-');
406       else if (strcmp (names[i], "translatable") == 0)
407         {
408           if (!_gtk_builder_boolean_from_string (values[i], &translatable,
409                                                  error))
410             return;
411         }
412       else if (strcmp (names[i], "comments") == 0)
413         {
414           /* do nothing, comments are for translators */
415         }
416       else if (strcmp (names[i], "context") == 0) 
417         {
418           context = g_strdup (values[i]);
419         }
420       else
421         {
422           error_invalid_attribute (data, element_name, names[i], error);
423           return;
424         }
425     }
426
427   if (!name)
428     {
429       error_missing_attribute (data, element_name, "name", error);
430       return;
431     }
432
433   info = g_slice_new0 (PropertyInfo);
434   info->name = name;
435   info->translatable = translatable;
436   info->context = context;
437   state_push (data, info);
438
439   info->tag.name = element_name;
440 }
441
442 static void
443 free_property_info (PropertyInfo *info)
444 {
445   g_free (info->data);
446   g_free (info->name);
447   g_slice_free (PropertyInfo, info);
448 }
449
450 static void
451 parse_signal (ParserData   *data,
452               const gchar  *element_name,
453               const gchar **names,
454               const gchar **values,
455               GError      **error)
456 {
457   SignalInfo *info;
458   gchar *name = NULL;
459   gchar *handler = NULL;
460   gchar *object = NULL;
461   gboolean after = FALSE;
462   gboolean swapped = FALSE;
463   gboolean swapped_set = FALSE;
464   int i;
465
466   g_assert (data->stack != NULL);
467
468   for (i = 0; names[i] != NULL; i++)
469     {
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)
475         {
476           if (!_gtk_builder_boolean_from_string (values[i], &after, error))
477             return;
478         }
479       else if (strcmp (names[i], "swapped") == 0)
480         {
481           if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
482             return;
483           swapped_set = TRUE;
484         }
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 */
489         ;
490       else
491         {
492           error_invalid_attribute (data, element_name, names[i], error);
493           return;
494         }
495     }
496
497   if (!name)
498     {
499       error_missing_attribute (data, element_name, "name", error);
500       return;
501     }
502   else if (!handler)
503     {
504       error_missing_attribute (data, element_name, "handler", error);
505       return;
506     }
507
508   /* Swapped defaults to FALSE except when object is set */
509   if (object && !swapped_set)
510     swapped = TRUE;
511   
512   info = g_slice_new0 (SignalInfo);
513   info->name = name;
514   info->handler = handler;
515   if (after)
516     info->flags |= G_CONNECT_AFTER;
517   if (swapped)
518     info->flags |= G_CONNECT_SWAPPED;
519   info->connect_object_name = object;
520   state_push (data, info);
521
522   info->tag.name = element_name;
523 }
524
525 /* Called by GtkBuilder */
526 void
527 _free_signal_info (SignalInfo *info,
528                    gpointer user_data)
529 {
530   g_free (info->name);
531   g_free (info->handler);
532   g_free (info->connect_object_name);
533   g_free (info->object_name);
534   g_slice_free (SignalInfo, info);
535 }
536
537 static void
538 parse_interface (ParserData   *data,
539                  const gchar  *element_name,
540                  const gchar **names,
541                  const gchar **values,
542                  GError      **error)
543 {
544   int i;
545
546   for (i = 0; names[i] != NULL; i++)
547     {
548       if (strcmp (names[i], "domain") == 0 && !data->domain)
549         {
550           data->domain = g_strdup (values[i]);
551           break;
552         }
553       else
554         error_invalid_attribute (data, "interface", names[i], error);
555     }
556 }
557
558 static SubParser *
559 create_subparser (GObject       *object,
560                   GObject       *child,
561                   const gchar   *element_name,
562                   GMarkupParser *parser,
563                   gpointer       user_data)
564 {
565   SubParser *subparser;
566
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;
574
575   return subparser;
576 }
577
578 static void
579 free_subparser (SubParser *subparser)
580 {
581   g_free (subparser->tagname);
582   g_slice_free (SubParser, subparser);
583 }
584
585 static gboolean
586 subparser_start (GMarkupParseContext *context,
587                  const gchar         *element_name,
588                  const gchar        **names,
589                  const gchar        **values,
590                  ParserData          *data,
591                  GError             **error)
592 {
593   SubParser *subparser = data->subparser;
594
595   if (!subparser->start &&
596       strcmp (element_name, subparser->tagname) == 0)
597     subparser->start = element_name;
598
599   if (subparser->start)
600     {
601       if (subparser->parser->start_element)
602         subparser->parser->start_element (context,
603                                           element_name, names, values,
604                                           subparser->data, error);
605       return FALSE;
606     }
607   return TRUE;
608 }
609
610 static void
611 subparser_end (GMarkupParseContext *context,
612                const gchar         *element_name,
613                ParserData          *data,
614                GError             **error)
615 {
616   if (data->subparser->parser->end_element)
617     data->subparser->parser->end_element (context, element_name,
618                                           data->subparser->data, error);
619   
620   if (!strcmp (data->subparser->start, element_name) == 0)
621     return;
622     
623   gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
624                                 data->builder,
625                                 data->subparser->child,
626                                 element_name,
627                                 data->subparser->data);
628   g_free (data->subparser->parser);
629       
630   if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
631     data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
632                                                data->subparser);
633   else
634     free_subparser (data->subparser);
635   
636   data->subparser = NULL;
637 }
638
639 static gboolean
640 parse_custom (GMarkupParseContext *context,
641               const gchar         *element_name,
642               const gchar        **names,
643               const gchar        **values,
644               ParserData          *data,
645               GError             **error)
646 {
647   CommonInfo* parent_info;
648   GMarkupParser parser;
649   gpointer *subparser_data;
650   GObject *object;
651   GObject *child;
652
653   parent_info = state_peek_info (data, CommonInfo);
654   if (!parent_info)
655     return FALSE;
656
657   if (strcmp (parent_info->tag.name, "object") == 0)
658     {
659       ObjectInfo* object_info = (ObjectInfo*)parent_info;
660       if (!object_info->object)
661         object_info->object = _gtk_builder_construct (data->builder,
662                                                       object_info);
663       g_assert (object_info->object);
664       object = object_info->object;
665       child = NULL;
666     }
667   else if (strcmp (parent_info->tag.name, "child") == 0)
668     {
669       ChildInfo* child_info = (ChildInfo*)parent_info;
670       
671       _gtk_builder_add (data->builder, child_info);
672       
673       object = ((ObjectInfo*)child_info->parent)->object;
674       child  = child_info->object;
675     }
676   else
677     return FALSE;
678
679   if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
680                                        data->builder,
681                                        child,
682                                        element_name,
683                                        &parser,
684                                        (gpointer*)&subparser_data))
685     return FALSE;
686       
687   data->subparser = create_subparser (object, child, element_name,
688                                       &parser, subparser_data);
689   
690   if (parser.start_element)
691     parser.start_element (context,
692                           element_name, names, values,
693                           subparser_data, error);
694   return TRUE;
695 }
696
697 static void
698 start_element (GMarkupParseContext *context,
699                const gchar         *element_name,
700                const gchar        **names,
701                const gchar        **values,
702                gpointer             user_data,
703                GError             **error)
704 {
705   ParserData *data = (ParserData*)user_data;
706
707 #ifdef GTK_ENABLE_DEBUG
708   if (gtk_debug_flags & GTK_DEBUG_BUILDER)
709     {
710       GString *tags = g_string_new ("");
711       int i;
712       for (i = 0; names[i]; i++)
713         g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
714
715       if (i)
716         {
717           g_string_insert_c (tags, 0, ' ');
718           g_string_truncate (tags, tags->len - 1);
719         }
720       g_print ("<%s%s>\n", element_name, tags->str);
721       g_string_free (tags, TRUE);
722     }
723 #endif
724
725   if (!data->last_element && strcmp (element_name, "interface") != 0)
726     {
727       g_set_error (error, GTK_BUILDER_ERROR, 
728                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
729                    _("Invalid root element: '%s'"),
730                    element_name);
731       return;
732     }
733   data->last_element = element_name;
734
735   if (data->subparser)
736     if (!subparser_start (context, element_name, names, values,
737                           data, error))
738       return;
739   
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)
751     {
752       /* placeholder has no special treatmeant, but it needs an
753        * if clause to avoid an error below.
754        */
755     }
756   else
757     if (!parse_custom (context, element_name, names, values,
758                        data, error))
759       g_set_error (error, GTK_BUILDER_ERROR, 
760                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
761                    _("Unhandled tag: '%s'"),
762                    element_name);
763 }
764
765 /* Called for close tags </foo> */
766 static void
767 end_element (GMarkupParseContext *context,
768              const gchar         *element_name,
769              gpointer             user_data,
770              GError             **error)
771 {
772   ParserData *data = (ParserData*)user_data;
773
774   GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
775
776   if (data->subparser && data->subparser->start)
777     {
778       subparser_end (context, element_name, data, error);
779       return;
780     }
781
782   if (strcmp (element_name, "object") == 0)
783     {
784       ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
785       ChildInfo* child_info = state_peek_info (data, ChildInfo);
786
787       object_info->object = builder_construct (data, object_info);
788
789       if (child_info)
790         child_info->object = object_info->object;
791
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);
796     }
797   else if (strcmp (element_name, "property") == 0)
798     {
799       PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
800       CommonInfo *info = state_peek_info (data, CommonInfo);
801
802       if (!prop_info->data)
803         {
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);
808           return;
809         }
810       
811       /* Normal properties */
812       if (strcmp (info->tag.name, "object") == 0)
813         {
814           ObjectInfo *object_info = (ObjectInfo*)info;
815           object_info->properties =
816             g_slist_prepend (object_info->properties, prop_info);
817         }
818       else
819         g_assert_not_reached ();
820     }
821   else if (strcmp (element_name, "child") == 0)
822     {
823       ChildInfo *child_info = state_pop_info (data, ChildInfo);
824
825       _gtk_builder_add (data->builder, child_info);
826
827       free_child_info (child_info);
828     }
829   else if (strcmp (element_name, "signal") == 0)
830     {
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);
836     }
837   else if (strcmp (element_name, "interface") == 0)
838     {
839     }
840   else if (strcmp (element_name, "placeholder") == 0)
841     {
842     }
843   else
844     {
845       g_assert_not_reached ();
846     }
847 }
848
849 /* This function is taken from gettext.h 
850  * GNU gettext uses '\004' to separate context and msgid in .mo files.
851  */
852 static const char *
853 dpgettext (const char *domain,
854            const char *msgctxt,
855            const char *msgid)
856 {
857   size_t msgctxt_len = strlen (msgctxt) + 1;
858   size_t msgid_len = strlen (msgid) + 1;
859   const char *translation;
860   char* msg_ctxt_id;
861
862   msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
863   
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);
867
868   translation = dgettext (domain, msg_ctxt_id);
869
870   if (translation == msg_ctxt_id) 
871     {
872       /* try the old way of doing message contexts, too */
873       msg_ctxt_id[msgctxt_len - 1] = '|';
874       translation = dgettext (domain, msg_ctxt_id);
875   
876       if (translation == msg_ctxt_id)
877         return msgid;
878     }
879  
880   return translation;
881 }
882
883 /* Called for character data */
884 /* text is not nul-terminated */
885 static void
886 text (GMarkupParseContext *context,
887       const gchar         *text,
888       gsize                text_len,
889       gpointer             user_data,
890       GError             **error)
891 {
892   ParserData *data = (ParserData*)user_data;
893   CommonInfo *info;
894
895   if (data->subparser && data->subparser->start)
896     {
897       if (data->subparser->parser->text)
898         data->subparser->parser->text (context, text, text_len,
899                                        data->subparser->data, error);
900       return;
901     }
902
903   if (!data->stack)
904     return;
905
906   info = state_peek_info (data, CommonInfo);
907   g_assert (info != NULL);
908
909   if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
910     {
911       PropertyInfo *prop_info = (PropertyInfo*)info;
912
913       /* text is not guaranteed to be null-terminated */
914       char *string = g_strndup (text, text_len);
915
916       if (prop_info->translatable && text_len)
917         {
918           if (prop_info->context)
919             text = dpgettext (data->domain, prop_info->context, string);
920           else
921             text = dgettext (data->domain, string);
922
923           prop_info->data = g_strdup (text);
924           g_free (string);
925         }
926       else
927         prop_info->data = string;
928     }
929 }
930
931 static const GMarkupParser parser = {
932   start_element,
933   end_element,
934   text,
935   NULL,
936   NULL
937 };
938
939 void
940 _gtk_builder_parser_parse_buffer (GtkBuilder   *builder,
941                                   const gchar  *filename,
942                                   const gchar  *buffer,
943                                   gsize         length,
944                                   GError      **error)
945 {
946   ParserData *data;
947   GSList *l;
948   
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));
953
954   data->ctx = g_markup_parse_context_new (
955                   &parser, G_MARKUP_TREAT_CDATA_AS_TEXT, data, NULL);
956
957   if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
958     goto out;
959   
960   gtk_builder_set_translation_domain (data->builder, data->domain);
961   _gtk_builder_finish (builder);
962
963   /* Custom parser_finished */
964   data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
965   for (l = data->custom_finalizers; l; l = l->next)
966     {
967       SubParser *sub = (SubParser*)l->data;
968       
969       gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
970                                      builder,
971                                      sub->child,
972                                      sub->tagname,
973                                      sub->data);
974       free_subparser (sub);
975     }
976   
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)
980     {
981       GtkBuildable *buildable = (GtkBuildable*)l->data;
982       gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
983     }
984
985  out:
986   g_markup_parse_context_free (data->ctx);
987
988   g_slist_free (data->stack);
989   g_slist_free (data->custom_finalizers);
990   g_slist_free (data->finalizers);
991   g_free (data->domain);
992   g_free (data);
993 }