]> Pileus Git - ~andy/gtk/blob - gtk/gtkbuilderparser.c
Set properties in the order in which they are specified in the xml file.
[~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 "config.h"
22
23 #include <string.h>
24 #include <gmodule.h>
25
26 #include "gtktypeutils.h"
27 #include "gtkbuilderprivate.h"
28 #include "gtkbuilder.h"
29 #include "gtkbuildable.h"
30 #include "gtkdebug.h"
31 #include "gtkversion.h"
32 #include "gtktypeutils.h"
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36 static void free_property_info (PropertyInfo *info);
37 static void free_object_info (ObjectInfo *info);
38
39 static inline void
40 state_push (ParserData *data, gpointer info)
41 {
42   data->stack = g_slist_prepend (data->stack, info);
43 }
44
45 static inline gpointer
46 state_peek (ParserData *data)
47 {
48   if (!data->stack)
49     return NULL;
50
51   return data->stack->data;
52 }
53
54 static inline gpointer
55 state_pop (ParserData *data)
56 {
57   gpointer old = NULL;
58
59   if (!data->stack)
60     return NULL;
61
62   old = data->stack->data;
63   data->stack = g_slist_delete_link (data->stack, data->stack);
64   return old;
65 }
66 #define state_peek_info(data, st) ((st*)state_peek(data))
67 #define state_pop_info(data, st) ((st*)state_pop(data))
68
69 static void
70 error_missing_attribute (ParserData *data,
71                          const gchar *tag,
72                          const gchar *attribute,
73                          GError **error)
74 {
75   gint line_number, char_number;
76
77   g_markup_parse_context_get_position (data->ctx,
78                                        &line_number,
79                                        &char_number);
80
81   g_set_error (error,
82                GTK_BUILDER_ERROR,
83                GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
84                "%s:%d:%d <%s> requires attribute \"%s\"",
85                data->filename,
86                line_number, char_number, tag, attribute);
87 }
88
89 static void
90 error_invalid_attribute (ParserData *data,
91                          const gchar *tag,
92                          const gchar *attribute,
93                          GError **error)
94 {
95   gint line_number, char_number;
96
97   g_markup_parse_context_get_position (data->ctx,
98                                        &line_number,
99                                        &char_number);
100
101   g_set_error (error,
102                GTK_BUILDER_ERROR,
103                GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
104                "%s:%d:%d '%s' is not a valid attribute of <%s>",
105                data->filename,
106                line_number, char_number, attribute, tag);
107 }
108
109 static void
110 error_invalid_tag (ParserData *data,
111                    const gchar *tag,
112                    const gchar *expected,
113                    GError **error)
114 {
115   gint line_number, char_number;
116
117   g_markup_parse_context_get_position (data->ctx,
118                                        &line_number,
119                                        &char_number);
120
121   if (expected)
122     g_set_error (error,
123                  GTK_BUILDER_ERROR,
124                  GTK_BUILDER_ERROR_INVALID_TAG,
125                  "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
126                  data->filename,
127                  line_number, char_number, tag, expected);
128   else
129     g_set_error (error,
130                  GTK_BUILDER_ERROR,
131                  GTK_BUILDER_ERROR_INVALID_TAG,
132                  "%s:%d:%d '%s' is not a valid tag here",
133                  data->filename,
134                  line_number, char_number, tag);
135 }
136
137 gboolean
138 _gtk_builder_boolean_from_string (const gchar  *string,
139                                   gboolean     *value,
140                                   GError      **error)
141 {
142   gboolean retval = TRUE;
143   int length;
144   
145   g_assert (string != NULL);
146   length = strlen (string);
147   
148   if (length == 0)
149     retval = FALSE;
150   else if (length == 1)
151     {
152       gchar c = g_ascii_tolower (string[0]);
153       if (c == 'y' || c == 't' || c == '1')
154         *value = TRUE;
155       else if (c == 'n' || c == 'f' || c == '0')
156         *value = FALSE;
157       else
158         retval = FALSE;
159     }
160   else
161     {
162       gchar *lower = g_ascii_strdown (string, length);
163       
164       if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
165         *value = TRUE;
166       else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
167         *value = FALSE;
168       else
169         retval = FALSE;
170       g_free (lower);
171     }
172   
173   if (!retval)
174     g_set_error (error,
175                  GTK_BUILDER_ERROR,
176                  GTK_BUILDER_ERROR_INVALID_VALUE,
177                  "could not parse boolean `%s'",
178                  string);
179
180   return retval;
181 }
182
183 static GObject *
184 builder_construct (ParserData  *data,
185                    ObjectInfo  *object_info,
186                    GError     **error)
187 {
188   GObject *object;
189
190   g_assert (object_info != NULL);
191
192   if (object_info->object)
193     return object_info->object;
194
195   object_info->properties = g_slist_reverse (object_info->properties);
196
197   object = _gtk_builder_construct (data->builder, object_info, error);
198   if (!object)
199     return NULL;
200
201   g_assert (G_IS_OBJECT (object));
202
203   object_info->object = object;
204
205   return object;
206 }
207
208 static gchar *
209 _get_type_by_symbol (const gchar* symbol)
210 {
211   static GModule *module = NULL;
212   GTypeGetFunc func;
213   GType type;
214   
215   if (!module)
216     module = g_module_open (NULL, 0);
217
218   if (!g_module_symbol (module, symbol, (gpointer)&func))
219     return NULL;
220   
221   type = func ();
222   if (type == G_TYPE_INVALID)
223     return NULL;
224
225   return g_strdup (g_type_name (type));
226 }
227
228 static void
229 parse_requires (ParserData   *data,
230                 const gchar  *element_name,
231                 const gchar **names,
232                 const gchar **values,
233                 GError      **error)
234 {
235   RequiresInfo *req_info;
236   const gchar  *library = NULL;
237   const gchar  *version = NULL;
238   gchar       **split;
239   gint          i, version_major = 0, version_minor = 0;
240   gint          line_number, char_number;
241
242   g_markup_parse_context_get_position (data->ctx,
243                                        &line_number,
244                                        &char_number);
245
246   for (i = 0; names[i] != NULL; i++)
247     {
248       if (strcmp (names[i], "lib") == 0)
249         library = values[i];
250       else if (strcmp (names[i], "version") == 0)
251         version = values[i];
252       else
253         error_invalid_attribute (data, element_name, names[i], error);
254     }
255
256   if (!library || !version)
257     {
258       error_missing_attribute (data, element_name, 
259                                version ? "lib" : "version", error);
260       return;
261     }
262
263   if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
264     {
265       g_set_error (error,
266                    GTK_BUILDER_ERROR,
267                    GTK_BUILDER_ERROR_INVALID_VALUE,
268                    "%s:%d:%d <%s> attribute has malformed value \"%s\"",
269                    data->filename,
270                    line_number, char_number, "version", version);
271       return;
272     }
273   version_major = g_ascii_strtoll (split[0], NULL, 10);
274   version_minor = g_ascii_strtoll (split[1], NULL, 10);
275   g_strfreev (split);
276
277   req_info = g_slice_new0 (RequiresInfo);
278   req_info->library = g_strdup (library);
279   req_info->major   = version_major;
280   req_info->minor   = version_minor;
281   state_push (data, req_info);
282   req_info->tag.name = element_name;
283 }
284
285 static void
286 parse_object (ParserData   *data,
287               const gchar  *element_name,
288               const gchar **names,
289               const gchar **values,
290               GError      **error)
291 {
292   ObjectInfo *object_info;
293   ChildInfo* child_info;
294   int i;
295   gchar *object_class = NULL;
296   gchar *object_id = NULL;
297   gchar *constructor = NULL;
298
299   child_info = state_peek_info (data, ChildInfo);
300   if (child_info && strcmp (child_info->tag.name, "object") == 0)
301     {
302       error_invalid_tag (data, element_name, NULL, error);
303       return;
304     }
305
306   for (i = 0; names[i] != NULL; i++)
307     {
308       if (strcmp (names[i], "class") == 0)
309         object_class = g_strdup (values[i]);
310       else if (strcmp (names[i], "id") == 0)
311         object_id = g_strdup (values[i]);
312       else if (strcmp (names[i], "constructor") == 0)
313         constructor = g_strdup (values[i]);
314       else if (strcmp (names[i], "type-func") == 0)
315         {
316           /* Call the GType function, and return the name of the GType,
317            * it's guaranteed afterwards that g_type_from_name on the name
318            * will return our GType
319            */
320           object_class = _get_type_by_symbol (values[i]);
321           if (!object_class)
322             {
323               g_set_error (error, GTK_BUILDER_ERROR, 
324                            GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
325                            _("Invalid type function: `%s'"),
326                            values[i]);
327               return;
328             }
329         }
330       else
331         {
332           error_invalid_attribute (data, element_name, names[i], error);
333           return;
334         }
335     }
336
337   if (!object_class)
338     {
339       error_missing_attribute (data, element_name, "class", error);
340       return;
341     }
342
343   if (!object_id)
344     {
345       error_missing_attribute (data, element_name, "id", error);
346       return;
347     }
348
349   object_info = g_slice_new0 (ObjectInfo);
350   object_info->class_name = object_class;
351   object_info->id = object_id;
352   object_info->constructor = constructor;
353   state_push (data, object_info);
354   object_info->tag.name = element_name;
355
356   if (child_info)
357     object_info->parent = (CommonInfo*)child_info;
358 }
359
360 static void
361 free_object_info (ObjectInfo *info)
362 {
363   /* Do not free the signal items, which GtkBuilder takes ownership of */
364   g_slist_free (info->signals);
365   g_slist_foreach (info->properties,
366                    (GFunc)free_property_info, NULL);
367   g_slist_free (info->properties);
368   g_free (info->constructor);
369   g_free (info->class_name);
370   g_free (info->id);
371   g_slice_free (ObjectInfo, info);
372 }
373
374 static void
375 parse_child (ParserData   *data,
376              const gchar  *element_name,
377              const gchar **names,
378              const gchar **values,
379              GError      **error)
380
381 {
382   ObjectInfo* object_info;
383   ChildInfo *child_info;
384   guint i;
385
386   object_info = state_peek_info (data, ObjectInfo);
387   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
388     {
389       error_invalid_tag (data, element_name, NULL, error);
390       return;
391     }
392   
393   child_info = g_slice_new0 (ChildInfo);
394   state_push (data, child_info);
395   child_info->tag.name = element_name;
396   for (i = 0; names[i]; i++)
397     {
398       if (strcmp (names[i], "type") == 0)
399         child_info->type = g_strdup (values[i]);
400       else if (strcmp (names[i], "internal-child") == 0)
401         child_info->internal_child = g_strdup (values[i]);
402       else
403         error_invalid_attribute (data, element_name, names[i], error);
404     }
405
406   child_info->parent = (CommonInfo*)object_info;
407
408   object_info->object = builder_construct (data, object_info, error);
409 }
410
411 static void
412 free_child_info (ChildInfo *info)
413 {
414   g_free (info->type);
415   g_free (info->internal_child);
416   g_slice_free (ChildInfo, info);
417 }
418
419 static void
420 parse_property (ParserData   *data,
421                 const gchar  *element_name,
422                 const gchar **names,
423                 const gchar **values,
424                 GError      **error)
425 {
426   PropertyInfo *info;
427   gchar *name = NULL;
428   gchar *context = NULL;
429   gboolean translatable = FALSE;
430   ObjectInfo *object_info;
431   int i;
432
433   object_info = state_peek_info (data, ObjectInfo);
434   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
435     {
436       error_invalid_tag (data, element_name, NULL, error);
437       return;
438     }
439
440   for (i = 0; names[i] != NULL; i++)
441     {
442       if (strcmp (names[i], "name") == 0)
443         name = g_strdelimit (g_strdup (values[i]), "_", '-');
444       else if (strcmp (names[i], "translatable") == 0)
445         {
446           if (!_gtk_builder_boolean_from_string (values[i], &translatable,
447                                                  error))
448             return;
449         }
450       else if (strcmp (names[i], "comments") == 0)
451         {
452           /* do nothing, comments are for translators */
453         }
454       else if (strcmp (names[i], "context") == 0) 
455         {
456           context = g_strdup (values[i]);
457         }
458       else
459         {
460           error_invalid_attribute (data, element_name, names[i], error);
461           return;
462         }
463     }
464
465   if (!name)
466     {
467       error_missing_attribute (data, element_name, "name", error);
468       return;
469     }
470
471   info = g_slice_new0 (PropertyInfo);
472   info->name = name;
473   info->translatable = translatable;
474   info->context = context;
475   info->text = g_string_new ("");
476   state_push (data, info);
477
478   info->tag.name = element_name;
479 }
480
481 static void
482 free_property_info (PropertyInfo *info)
483 {
484   g_free (info->data);
485   g_free (info->name);
486   g_slice_free (PropertyInfo, info);
487 }
488
489 static void
490 parse_signal (ParserData   *data,
491               const gchar  *element_name,
492               const gchar **names,
493               const gchar **values,
494               GError      **error)
495 {
496   SignalInfo *info;
497   gchar *name = NULL;
498   gchar *handler = NULL;
499   gchar *object = NULL;
500   gboolean after = FALSE;
501   gboolean swapped = FALSE;
502   gboolean swapped_set = FALSE;
503   ObjectInfo *object_info;
504   int i;
505
506   object_info = state_peek_info (data, ObjectInfo);
507   if (!object_info || strcmp (object_info->tag.name, "object") != 0)
508     {
509       error_invalid_tag (data, element_name, NULL, error);
510       return;
511     }
512
513   for (i = 0; names[i] != NULL; i++)
514     {
515       if (strcmp (names[i], "name") == 0)
516         name = g_strdup (values[i]);
517       else if (strcmp (names[i], "handler") == 0)
518         handler = g_strdup (values[i]);
519       else if (strcmp (names[i], "after") == 0)
520         {
521           if (!_gtk_builder_boolean_from_string (values[i], &after, error))
522             return;
523         }
524       else if (strcmp (names[i], "swapped") == 0)
525         {
526           if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
527             return;
528           swapped_set = TRUE;
529         }
530       else if (strcmp (names[i], "object") == 0)
531         object = g_strdup (values[i]);
532       else if (strcmp (names[i], "last_modification_time") == 0)
533         /* parse but ignore */
534         ;
535       else
536         {
537           error_invalid_attribute (data, element_name, names[i], error);
538           return;
539         }
540     }
541
542   if (!name)
543     {
544       error_missing_attribute (data, element_name, "name", error);
545       return;
546     }
547   else if (!handler)
548     {
549       error_missing_attribute (data, element_name, "handler", error);
550       return;
551     }
552
553   /* Swapped defaults to FALSE except when object is set */
554   if (object && !swapped_set)
555     swapped = TRUE;
556   
557   info = g_slice_new0 (SignalInfo);
558   info->name = name;
559   info->handler = handler;
560   if (after)
561     info->flags |= G_CONNECT_AFTER;
562   if (swapped)
563     info->flags |= G_CONNECT_SWAPPED;
564   info->connect_object_name = object;
565   state_push (data, info);
566
567   info->tag.name = element_name;
568 }
569
570 /* Called by GtkBuilder */
571 void
572 _free_signal_info (SignalInfo *info,
573                    gpointer user_data)
574 {
575   g_free (info->name);
576   g_free (info->handler);
577   g_free (info->connect_object_name);
578   g_free (info->object_name);
579   g_slice_free (SignalInfo, info);
580 }
581
582 void
583 _free_requires_info (RequiresInfo *info,
584                      gpointer user_data)
585 {
586   g_free (info->library);
587   g_slice_free (RequiresInfo, info);
588 }
589
590 static void
591 parse_interface (ParserData   *data,
592                  const gchar  *element_name,
593                  const gchar **names,
594                  const gchar **values,
595                  GError      **error)
596 {
597   int i;
598
599   for (i = 0; names[i] != NULL; i++)
600     {
601       if (strcmp (names[i], "domain") == 0)
602         {
603           g_free (data->domain);
604           data->domain = g_strdup (values[i]);
605           break;
606         }
607       else
608         error_invalid_attribute (data, "interface", names[i], error);
609     }
610 }
611
612 static SubParser *
613 create_subparser (GObject       *object,
614                   GObject       *child,
615                   const gchar   *element_name,
616                   GMarkupParser *parser,
617                   gpointer       user_data)
618 {
619   SubParser *subparser;
620
621   subparser = g_slice_new0 (SubParser);
622   subparser->object = object;
623   subparser->child = child;
624   subparser->tagname = g_strdup (element_name);
625   subparser->start = element_name;
626   subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
627   subparser->data = user_data;
628
629   return subparser;
630 }
631
632 static void
633 free_subparser (SubParser *subparser)
634 {
635   g_free (subparser->tagname);
636   g_slice_free (SubParser, subparser);
637 }
638
639 static gboolean
640 subparser_start (GMarkupParseContext *context,
641                  const gchar         *element_name,
642                  const gchar        **names,
643                  const gchar        **values,
644                  ParserData          *data,
645                  GError             **error)
646 {
647   SubParser *subparser = data->subparser;
648
649   if (!subparser->start &&
650       strcmp (element_name, subparser->tagname) == 0)
651     subparser->start = element_name;
652
653   if (subparser->start)
654     {
655       if (subparser->parser->start_element)
656         subparser->parser->start_element (context,
657                                           element_name, names, values,
658                                           subparser->data, error);
659       return FALSE;
660     }
661   return TRUE;
662 }
663
664 static void
665 subparser_end (GMarkupParseContext *context,
666                const gchar         *element_name,
667                ParserData          *data,
668                GError             **error)
669 {
670   if (data->subparser->parser->end_element)
671     data->subparser->parser->end_element (context, element_name,
672                                           data->subparser->data, error);
673   
674   if (!strcmp (data->subparser->start, element_name) == 0)
675     return;
676     
677   gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
678                                 data->builder,
679                                 data->subparser->child,
680                                 element_name,
681                                 data->subparser->data);
682   g_free (data->subparser->parser);
683       
684   if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
685     data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
686                                                data->subparser);
687   else
688     free_subparser (data->subparser);
689   
690   data->subparser = NULL;
691 }
692
693 static gboolean
694 parse_custom (GMarkupParseContext *context,
695               const gchar         *element_name,
696               const gchar        **names,
697               const gchar        **values,
698               ParserData          *data,
699               GError             **error)
700 {
701   CommonInfo* parent_info;
702   GMarkupParser parser;
703   gpointer subparser_data;
704   GObject *object;
705   GObject *child;
706
707   parent_info = state_peek_info (data, CommonInfo);
708   if (!parent_info)
709     return FALSE;
710
711   if (strcmp (parent_info->tag.name, "object") == 0)
712     {
713       ObjectInfo* object_info = (ObjectInfo*)parent_info;
714       if (!object_info->object)
715         {
716           object_info->properties = g_slist_reverse (object_info->properties);
717           object_info->object = _gtk_builder_construct (data->builder,
718                                                         object_info,
719                                                         error);
720           if (!object_info->object)
721             return TRUE; /* A GError is already set */
722         }
723       g_assert (object_info->object);
724       object = object_info->object;
725       child = NULL;
726     }
727   else if (strcmp (parent_info->tag.name, "child") == 0)
728     {
729       ChildInfo* child_info = (ChildInfo*)parent_info;
730       
731       _gtk_builder_add (data->builder, child_info);
732       
733       object = ((ObjectInfo*)child_info->parent)->object;
734       child  = child_info->object;
735     }
736   else
737     return FALSE;
738
739   if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
740                                        data->builder,
741                                        child,
742                                        element_name,
743                                        &parser,
744                                        &subparser_data))
745     return FALSE;
746       
747   data->subparser = create_subparser (object, child, element_name,
748                                       &parser, subparser_data);
749   
750   if (parser.start_element)
751     parser.start_element (context,
752                           element_name, names, values,
753                           subparser_data, error);
754   return TRUE;
755 }
756
757 static void
758 start_element (GMarkupParseContext *context,
759                const gchar         *element_name,
760                const gchar        **names,
761                const gchar        **values,
762                gpointer             user_data,
763                GError             **error)
764 {
765   ParserData *data = (ParserData*)user_data;
766
767 #ifdef GTK_ENABLE_DEBUG
768   if (gtk_debug_flags & GTK_DEBUG_BUILDER)
769     {
770       GString *tags = g_string_new ("");
771       int i;
772       for (i = 0; names[i]; i++)
773         g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
774
775       if (i)
776         {
777           g_string_insert_c (tags, 0, ' ');
778           g_string_truncate (tags, tags->len - 1);
779         }
780       g_print ("<%s%s>\n", element_name, tags->str);
781       g_string_free (tags, TRUE);
782     }
783 #endif
784
785   if (!data->last_element && strcmp (element_name, "interface") != 0)
786     {
787       g_set_error (error, GTK_BUILDER_ERROR, 
788                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
789                    _("Invalid root element: '%s'"),
790                    element_name);
791       return;
792     }
793   data->last_element = element_name;
794
795   if (data->subparser)
796     if (!subparser_start (context, element_name, names, values,
797                           data, error))
798       return;
799
800   if (strcmp (element_name, "requires") == 0)
801     parse_requires (data, element_name, names, values, error);
802   else if (strcmp (element_name, "object") == 0)
803     parse_object (data, element_name, names, values, error);
804   else if (strcmp (element_name, "child") == 0)
805     parse_child (data, element_name, names, values, error);
806   else if (strcmp (element_name, "property") == 0)
807     parse_property (data, element_name, names, values, error);
808   else if (strcmp (element_name, "signal") == 0)
809     parse_signal (data, element_name, names, values, error);
810   else if (strcmp (element_name, "interface") == 0)
811     parse_interface (data, element_name, names, values, error);
812   else if (strcmp (element_name, "placeholder") == 0)
813     {
814       /* placeholder has no special treatmeant, but it needs an
815        * if clause to avoid an error below.
816        */
817     }
818   else
819     if (!parse_custom (context, element_name, names, values,
820                        data, error))
821       g_set_error (error, GTK_BUILDER_ERROR, 
822                    GTK_BUILDER_ERROR_UNHANDLED_TAG,
823                    _("Unhandled tag: '%s'"),
824                    element_name);
825 }
826
827 /* This function is taken from gettext.h 
828  * GNU gettext uses '\004' to separate context and msgid in .mo files.
829  */
830 static const char *
831 _dpgettext (const char *domain,
832             const char *msgctxt,
833             const char *msgid)
834 {
835   size_t msgctxt_len = strlen (msgctxt) + 1;
836   size_t msgid_len = strlen (msgid) + 1;
837   const char *translation;
838   char* msg_ctxt_id;
839
840   msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
841   
842   memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
843   msg_ctxt_id[msgctxt_len - 1] = '\004';
844   memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
845
846   translation = g_dgettext (domain, msg_ctxt_id);
847
848   if (translation == msg_ctxt_id) 
849     {
850       /* try the old way of doing message contexts, too */
851       msg_ctxt_id[msgctxt_len - 1] = '|';
852       translation = g_dgettext (domain, msg_ctxt_id);
853   
854       if (translation == msg_ctxt_id)
855         return msgid;
856     }
857  
858   return translation;
859 }
860
861 gchar *
862 _gtk_builder_parser_translate (const gchar *domain,
863                                const gchar *context,
864                                const gchar *text)
865 {
866   const char *s;
867
868   if (context)
869     s = _dpgettext (domain, context, text);
870   else
871     s = g_dgettext (domain, text);
872
873   return g_strdup (s);
874 }
875
876 /* Called for close tags </foo> */
877 static void
878 end_element (GMarkupParseContext *context,
879              const gchar         *element_name,
880              gpointer             user_data,
881              GError             **error)
882 {
883   ParserData *data = (ParserData*)user_data;
884
885   GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
886
887   if (data->subparser && data->subparser->start)
888     {
889       subparser_end (context, element_name, data, error);
890       return;
891     }
892
893   if (strcmp (element_name, "requires") == 0)
894     {
895       RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
896
897       /* TODO: Allow third party widget developers to check thier
898        * required versions, possibly throw a signal allowing them
899        * to check thier library versions here.
900        */
901       if (!strcmp (req_info->library, "gtk+"))
902         {
903           if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
904             g_set_error (error,
905                          GTK_BUILDER_ERROR,
906                          GTK_BUILDER_ERROR_VERSION_MISMATCH,
907                          "%s: required %s version %d.%d, current version is %d.%d",
908                          data->filename, req_info->library, 
909                          req_info->major, req_info->minor,
910                          GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
911         }
912     }
913   else if (strcmp (element_name, "object") == 0)
914     {
915       ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
916       ChildInfo* child_info = state_peek_info (data, ChildInfo);
917
918       object_info->object = builder_construct (data, object_info, error);
919       if (!object_info->object)
920         {
921           free_object_info (object_info);
922           return;
923         }
924       if (child_info)
925         child_info->object = object_info->object;
926
927       if (GTK_IS_BUILDABLE (object_info->object) &&
928           GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
929         data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
930       _gtk_builder_add_signals (data->builder, object_info->signals);
931
932       free_object_info (object_info);
933     }
934   else if (strcmp (element_name, "property") == 0)
935     {
936       PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
937       CommonInfo *info = state_peek_info (data, CommonInfo);
938
939       /* Normal properties */
940       if (strcmp (info->tag.name, "object") == 0)
941         {
942           ObjectInfo *object_info = (ObjectInfo*)info;
943
944           if (prop_info->translatable && prop_info->text->len)
945             {
946               prop_info->data = _gtk_builder_parser_translate (data->domain,
947                                                                prop_info->context,
948                                                                prop_info->text->str);
949               g_string_free (prop_info->text, TRUE);
950             }
951           else
952             {
953               prop_info->data = g_string_free (prop_info->text, FALSE);
954               
955             }
956
957           object_info->properties =
958             g_slist_prepend (object_info->properties, prop_info);
959         }
960       else
961         g_assert_not_reached ();
962     }
963   else if (strcmp (element_name, "child") == 0)
964     {
965       ChildInfo *child_info = state_pop_info (data, ChildInfo);
966
967       _gtk_builder_add (data->builder, child_info);
968
969       free_child_info (child_info);
970     }
971   else if (strcmp (element_name, "signal") == 0)
972     {
973       SignalInfo *signal_info = state_pop_info (data, SignalInfo);
974       ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
975       signal_info->object_name = g_strdup (object_info->id);
976       object_info->signals =
977         g_slist_prepend (object_info->signals, signal_info);
978     }
979   else if (strcmp (element_name, "interface") == 0)
980     {
981     }
982   else if (strcmp (element_name, "placeholder") == 0)
983     {
984     }
985   else
986     {
987       g_assert_not_reached ();
988     }
989 }
990
991 /* Called for character data */
992 /* text is not nul-terminated */
993 static void
994 text (GMarkupParseContext *context,
995       const gchar         *text,
996       gsize                text_len,
997       gpointer             user_data,
998       GError             **error)
999 {
1000   ParserData *data = (ParserData*)user_data;
1001   CommonInfo *info;
1002
1003   if (data->subparser && data->subparser->start)
1004     {
1005       GError *tmp_error = NULL;
1006       
1007       if (data->subparser->parser->text)
1008         data->subparser->parser->text (context, text, text_len,
1009                                        data->subparser->data, &tmp_error);
1010       if (tmp_error)
1011         g_propagate_error (error, tmp_error);
1012       return;
1013     }
1014
1015   if (!data->stack)
1016     return;
1017
1018   info = state_peek_info (data, CommonInfo);
1019   g_assert (info != NULL);
1020
1021   if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
1022     {
1023       PropertyInfo *prop_info = (PropertyInfo*)info;
1024
1025       g_string_append_len (prop_info->text, text, text_len);
1026     }
1027 }
1028
1029 static void
1030 free_info (CommonInfo *info)
1031 {
1032   if (strcmp (info->tag.name, "object") == 0) 
1033     free_object_info ((ObjectInfo *)info);
1034   else if (strcmp (info->tag.name, "child") == 0) 
1035     free_child_info ((ChildInfo *)info);
1036   else if (strcmp (info->tag.name, "property") == 0) 
1037     free_property_info ((PropertyInfo *)info);
1038   else if (strcmp (info->tag.name, "signal") == 0) 
1039     _free_signal_info ((SignalInfo *)info, NULL);
1040   else if (strcmp (info->tag.name, "requires") == 0) 
1041     _free_requires_info ((RequiresInfo *)info, NULL);
1042   else 
1043     g_assert_not_reached ();
1044 }
1045
1046 static const GMarkupParser parser = {
1047   start_element,
1048   end_element,
1049   text,
1050   NULL,
1051   NULL
1052 };
1053
1054 void
1055 _gtk_builder_parser_parse_buffer (GtkBuilder   *builder,
1056                                   const gchar  *filename,
1057                                   const gchar  *buffer,
1058                                   gsize         length,
1059                                   GError      **error)
1060 {
1061   ParserData *data;
1062   GSList *l;
1063   
1064   data = g_new0 (ParserData, 1);
1065   data->builder = builder;
1066   data->filename = filename;
1067   data->domain = g_strdup (gtk_builder_get_translation_domain (builder));
1068
1069   data->ctx = g_markup_parse_context_new (&parser, 
1070                                           G_MARKUP_TREAT_CDATA_AS_TEXT, 
1071                                           data, NULL);
1072
1073   if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
1074     goto out;
1075   
1076   _gtk_builder_finish (builder);
1077
1078   /* Custom parser_finished */
1079   data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
1080   for (l = data->custom_finalizers; l; l = l->next)
1081     {
1082       SubParser *sub = (SubParser*)l->data;
1083       
1084       gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
1085                                      builder,
1086                                      sub->child,
1087                                      sub->tagname,
1088                                      sub->data);
1089     }
1090   
1091   /* Common parser_finished, for all created objects */
1092   data->finalizers = g_slist_reverse (data->finalizers);
1093   for (l = data->finalizers; l; l = l->next)
1094     {
1095       GtkBuildable *buildable = (GtkBuildable*)l->data;
1096       gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1097     }
1098
1099  out:
1100
1101   g_slist_foreach (data->stack, (GFunc)free_info, NULL);
1102   g_slist_free (data->stack);
1103   g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
1104   g_slist_free (data->custom_finalizers);
1105   g_slist_free (data->finalizers);
1106   g_free (data->domain);
1107   g_markup_parse_context_free (data->ctx);
1108   g_free (data);
1109 }