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