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