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