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