gtkbuilderparser.c 40.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* gtkbuilderparser.c
 * Copyright (C) 2006-2007 Async Open Source,
 *                         Johan Dahlin <jdahlin@async.com.br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20

21 22 23
#include <string.h>
#include <gmodule.h>

24
#include <gio/gio.h>
25 26 27 28
#include "gtkbuilderprivate.h"
#include "gtkbuilder.h"
#include "gtkbuildable.h"
#include "gtkdebug.h"
29
#include "gtkversion.h"
30
#include "gtktypebuiltins.h"
31
#include "gtkintl.h"
32

33

34 35
static void free_property_info (PropertyInfo *info);
static void free_object_info (ObjectInfo *info);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

static inline void
state_push (ParserData *data, gpointer info)
{
  data->stack = g_slist_prepend (data->stack, info);
}

static inline gpointer
state_peek (ParserData *data)
{
  if (!data->stack)
    return NULL;

  return data->stack->data;
}

static inline gpointer
state_pop (ParserData *data)
{
  gpointer old = NULL;

  if (!data->stack)
    return NULL;

  old = data->stack->data;
  data->stack = g_slist_delete_link (data->stack, data->stack);
  return old;
}
#define state_peek_info(data, st) ((st*)state_peek(data))
#define state_pop_info(data, st) ((st*)state_pop(data))

static void
Matthias Clasen's avatar
Matthias Clasen committed
68 69 70 71
error_missing_attribute (ParserData   *data,
                         const gchar  *tag,
                         const gchar  *attribute,
                         GError      **error)
72
{
Matthias Clasen's avatar
Matthias Clasen committed
73
  gint line, col;
74

Matthias Clasen's avatar
Matthias Clasen committed
75
  g_markup_parse_context_get_position (data->ctx, &line, &col);
76 77 78 79

  g_set_error (error,
               GTK_BUILDER_ERROR,
               GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
80
               "%s:%d:%d <%s> requires attribute '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
81
               data->filename, line, col, tag, attribute);
82 83 84
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
85 86 87 88
error_invalid_tag (ParserData   *data,
                   const gchar  *tag,
                   const gchar  *expected,
                   GError      **error)
89
{
Matthias Clasen's avatar
Matthias Clasen committed
90
  gint line, col;
91

Matthias Clasen's avatar
Matthias Clasen committed
92
  g_markup_parse_context_get_position (data->ctx, &line, &col);
93 94 95

  if (expected)
    g_set_error (error,
Matthias Clasen's avatar
Matthias Clasen committed
96 97
                 GTK_BUILDER_ERROR,
                 GTK_BUILDER_ERROR_INVALID_TAG,
98
                 "%s:%d:%d <%s> is not a valid tag here, expected a <%s> tag",
Matthias Clasen's avatar
Matthias Clasen committed
99
                 data->filename, line, col, tag, expected);
100 101
  else
    g_set_error (error,
Matthias Clasen's avatar
Matthias Clasen committed
102 103
                 GTK_BUILDER_ERROR,
                 GTK_BUILDER_ERROR_INVALID_TAG,
104
                 "%s:%d:%d <%s> is not a valid tag here",
Matthias Clasen's avatar
Matthias Clasen committed
105
                 data->filename, line, col, tag);
106 107
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static void
error_unhandled_tag (ParserData   *data,
                     const gchar  *tag,
                     GError      **error)
{
  gint line, col;

  g_markup_parse_context_get_position (data->ctx, &line, &col);
  g_set_error (error,
               GTK_BUILDER_ERROR,
               GTK_BUILDER_ERROR_UNHANDLED_TAG,
               "%s:%d:%d Unhandled tag: <%s>",
               data->filename, line, col, tag);
}

123
static GObject *
124 125
builder_construct (ParserData  *data,
                   ObjectInfo  *object_info,
Matthias Clasen's avatar
Matthias Clasen committed
126
                   GError     **error)
127 128 129 130 131
{
  GObject *object;

  g_assert (object_info != NULL);

132
  if (object_info->object && object_info->applied_properties)
133 134 135 136
    return object_info->object;

  object_info->properties = g_slist_reverse (object_info->properties);

137 138 139 140
  if (object_info->object == NULL)
    {
      object = _gtk_builder_construct (data->builder, object_info, error);
      if (!object)
Matthias Clasen's avatar
Matthias Clasen committed
141
        return NULL;
142 143 144 145 146 147 148 149 150 151 152
    }
  else
    {
      /* We're building a template, the object is already set and
       * we just want to resolve the properties at the right time
       */
      object = object_info->object;
      _gtk_builder_apply_properties (data->builder, object_info, error);
    }

  object_info->applied_properties = TRUE;
153

154 155 156 157 158 159 160
  g_assert (G_IS_OBJECT (object));

  object_info->object = object;

  return object;
}

161
static GType
Matthias Clasen's avatar
Matthias Clasen committed
162
_get_type_by_symbol (const gchar *symbol)
163 164 165
{
  static GModule *module = NULL;
  GTypeGetFunc func;
Matthias Clasen's avatar
Matthias Clasen committed
166

167 168 169 170
  if (!module)
    module = g_module_open (NULL, 0);

  if (!g_module_symbol (module, symbol, (gpointer)&func))
171
    return G_TYPE_INVALID;
Matthias Clasen's avatar
Matthias Clasen committed
172

173
  return func ();
174 175
}

176 177
static void
parse_requires (ParserData   *data,
Matthias Clasen's avatar
Matthias Clasen committed
178 179 180 181
                const gchar  *element_name,
                const gchar **names,
                const gchar **values,
                GError      **error)
182 183 184 185
{
  RequiresInfo *req_info;
  const gchar  *library = NULL;
  const gchar  *version = NULL;
186 187 188 189 190 191 192 193
  gchar **split;
  gint version_major = 0;
  gint version_minor = 0;

  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING, "lib", &library,
                                    G_MARKUP_COLLECT_STRING, "version", &version,
                                    G_MARKUP_COLLECT_INVALID))
194
    {
195
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
196 197 198 199 200 201
      return;
    }

  if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
    {
      g_set_error (error,
Matthias Clasen's avatar
Matthias Clasen committed
202 203
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_INVALID_VALUE,
204 205
                   "'version' attribute has malformed value '%s'", version);
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
206 207 208 209 210 211 212 213
      return;
    }
  version_major = g_ascii_strtoll (split[0], NULL, 10);
  version_minor = g_ascii_strtoll (split[1], NULL, 10);
  g_strfreev (split);

  req_info = g_slice_new0 (RequiresInfo);
  req_info->library = g_strdup (library);
214 215
  req_info->major = version_major;
  req_info->minor = version_minor;
216 217 218 219
  state_push (data, req_info);
  req_info->tag.name = element_name;
}

220 221 222 223 224 225 226 227
static gboolean
is_requested_object (const gchar *object,
                     ParserData  *data)
{
  GSList *l;

  for (l = data->requested_objects; l; l = l->next)
    {
228
      if (g_strcmp0 (l->data, object) == 0)
229 230 231 232 233 234
        return TRUE;
    }

  return FALSE;
}

235
static void
236 237 238 239 240 241
parse_object (GMarkupParseContext  *context,
              ParserData           *data,
              const gchar          *element_name,
              const gchar         **names,
              const gchar         **values,
              GError              **error)
242 243 244
{
  ObjectInfo *object_info;
  ChildInfo* child_info;
245
  GType object_type = G_TYPE_INVALID;
246
  const gchar *object_class = NULL;
247
  const gchar *constructor = NULL;
248 249
  const gchar *type_func = NULL;
  const gchar *object_id = NULL;
250
  gchar *internal_id = NULL;
251
  gint line;
252 253 254 255 256 257 258 259

  child_info = state_peek_info (data, ChildInfo);
  if (child_info && strcmp (child_info->tag.name, "object") == 0)
    {
      error_invalid_tag (data, element_name, NULL, error);
      return;
    }

260 261 262 263 264 265
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "class", &object_class,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "constructor", &constructor,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type-func", &type_func,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "id", &object_id,
                                    G_MARKUP_COLLECT_INVALID))
266
    {
267 268 269
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
    }
270

271
  if (type_func)
272
    {
273 274 275 276
      /* Call the GType function, and return the GType, it's guaranteed afterwards
       * that g_type_from_name on the name will return our GType
       */
      object_type = _get_type_by_symbol (type_func);
277
      if (object_type == G_TYPE_INVALID)
278
        {
279 280
          g_set_error (error,
                       GTK_BUILDER_ERROR,
281 282
                       GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
                       "Invalid type function '%s'", type_func);
283 284
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
285
        }
286
    }
287
  else if (object_class)
288
    {
289
      object_type = gtk_builder_get_type_from_name (data->builder, object_class);
290
      if (object_type == G_TYPE_INVALID)
Matthias Clasen's avatar
Matthias Clasen committed
291
        {
292 293
          g_set_error (error,
                       GTK_BUILDER_ERROR,
294 295
                       GTK_BUILDER_ERROR_INVALID_VALUE,
                       "Invalid object type '%s'", object_class);
296
          _gtk_builder_prefix_error (data->builder, context, error);
Matthias Clasen's avatar
Matthias Clasen committed
297
          return;
298
       }
299
    }
Matthias Clasen's avatar
Matthias Clasen committed
300
  else
301 302 303 304 305 306 307
    {
      error_missing_attribute (data, element_name, "class", error);
      return;
    }

  if (!object_id)
    {
308 309
      internal_id = g_strdup_printf ("___object_%d___", ++data->object_counter);
      object_id = internal_id;
310 311
    }

312 313 314 315 316 317 318 319 320
  ++data->cur_object_level;

  /* check if we reached a requested object (if it is specified) */
  if (data->requested_objects && !data->inside_requested_object)
    {
      if (is_requested_object (object_id, data))
        {
          data->requested_object_level = data->cur_object_level;

321 322 323
          GTK_NOTE (BUILDER,
                    g_message ("requested object \"%s\" found at level %d",
                               object_id, data->requested_object_level));
324 325 326 327

          data->inside_requested_object = TRUE;
        }
      else
328
        {
329
          g_free (internal_id);
330 331
          return;
        }
332 333
    }

334
  object_info = g_slice_new0 (ObjectInfo);
Matthias Clasen's avatar
Matthias Clasen committed
335
  object_info->tag.name = element_name;
336
  object_info->type = object_type;
337
  object_info->oclass = g_type_class_ref (object_type);
338 339
  object_info->id = (internal_id) ? internal_id : g_strdup (object_id);
  object_info->constructor = g_strdup (constructor);
Matthias Clasen's avatar
Matthias Clasen committed
340
  object_info->parent = (CommonInfo*)child_info;
341
  state_push (data, object_info);
342

343 344
  line = GPOINTER_TO_INT (g_hash_table_lookup (data->object_ids, object_id));
  if (line != 0)
345
    {
346 347
      g_set_error (error,
                   GTK_BUILDER_ERROR,
348
                   GTK_BUILDER_ERROR_DUPLICATE_ID,
349 350 351
                   "Duplicate object ID '%s' (previously on line %d)",
                   object_id, line);
      _gtk_builder_prefix_error (data->builder, context, error);
352 353 354
      return;
    }

355
  g_markup_parse_context_get_position (context, &line, NULL);
356
  g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
357 358
}

359 360
static void
parse_template (GMarkupParseContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
361 362 363 364 365
                ParserData           *data,
                const gchar          *element_name,
                const gchar         **names,
                const gchar         **values,
                GError              **error)
366 367
{
  ObjectInfo *object_info;
368
  const gchar *object_class = NULL;
369
  const gchar *parent_class = NULL;
370 371
  gint line;
  GType template_type;
372 373
  GType parsed_type;

374 375
  template_type = _gtk_builder_get_template_type (data->builder);

376 377 378 379
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING, "class", &object_class,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "parent", &parent_class,
                                    G_MARKUP_COLLECT_INVALID))
380
    {
381
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
382 383
      return;
    }
384 385

  if (template_type == 0)
386
    {
387 388 389 390 391 392
      g_set_error (error,
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_UNHANDLED_TAG,
                   "Not expecting to handle a template (class '%s', parent '%s')",
                   object_class, parent_class ? parent_class : "GtkWidget");
      _gtk_builder_prefix_error (data->builder, context, error);
393 394
      return;
    }
395
  else if (state_peek (data) != NULL)
396
    {
397
      error_invalid_tag (data, "template", NULL, error);
398 399 400 401 402 403 404
      return;
    }

  parsed_type = g_type_from_name (object_class);
  if (template_type != parsed_type)
    {
      g_set_error (error,
Matthias Clasen's avatar
Matthias Clasen committed
405 406
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
407
                   "Parsed template definition for type '%s', expected type '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
408
                   object_class, g_type_name (template_type));
409
      _gtk_builder_prefix_error (data->builder, context, error);
410 411 412
      return;
    }

413 414 415 416 417 418 419 420 421
  if (parent_class)
    {
      GType parent_type = g_type_from_name (parent_class);
      GType expected_type = g_type_parent (parsed_type);

      if (parent_type == G_TYPE_INVALID)
        {
          g_set_error (error, GTK_BUILDER_ERROR,
                       GTK_BUILDER_ERROR_INVALID_VALUE,
422 423
                       "Invalid template parent type '%s'", parent_class);
          _gtk_builder_prefix_error (data->builder, context, error);
424
          return;
Matthias Clasen's avatar
Matthias Clasen committed
425
        }
426 427 428 429
      if (parent_type != expected_type)
        {
          g_set_error (error, GTK_BUILDER_ERROR,
                       GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
430
                       "Template parent type '%s' does not match instance parent type '%s'.",
431
                       parent_class, g_type_name (expected_type));
432
          _gtk_builder_prefix_error (data->builder, context, error);
433 434 435
          return;
        }
    }
Matthias Clasen's avatar
Matthias Clasen committed
436

437 438 439
  ++data->cur_object_level;

  object_info = g_slice_new0 (ObjectInfo);
Matthias Clasen's avatar
Matthias Clasen committed
440
  object_info->tag.name = element_name;
441
  object_info->type = parsed_type;
442
  object_info->oclass = g_type_class_ref (parsed_type);
443 444 445 446
  object_info->id = g_strdup (object_class);
  object_info->object = gtk_builder_get_object (data->builder, object_class);
  state_push (data, object_info);

447 448
  line = GPOINTER_TO_INT (g_hash_table_lookup (data->object_ids, object_class));
  if (line != 0)
449
    {
450 451
      g_set_error (error,
                   GTK_BUILDER_ERROR,
452
                   GTK_BUILDER_ERROR_DUPLICATE_ID,
453 454 455
                   "Duplicate object ID '%s' (previously on line %d)",
                   object_class, line);
      _gtk_builder_prefix_error (data->builder, context, error);
456 457 458
      return;
    }

459
  g_markup_parse_context_get_position (context, &line, NULL);
460 461 462 463
  g_hash_table_insert (data->object_ids, g_strdup (object_class), GINT_TO_POINTER (line));
}


464 465 466 467
static void
free_object_info (ObjectInfo *info)
{
  /* Do not free the signal items, which GtkBuilder takes ownership of */
468
  g_type_class_unref (info->oclass);
469
  g_slist_free (info->signals);
Matthias Clasen's avatar
Matthias Clasen committed
470
  g_slist_free_full (info->properties, (GDestroyNotify)free_property_info);
471 472 473 474 475
  g_free (info->constructor);
  g_free (info->id);
  g_slice_free (ObjectInfo, info);
}

476 477 478 479 480 481 482 483
static void
free_menu_info (MenuInfo *info)
{
  g_free (info->id);
  g_hash_table_unref (info->objects);
  g_slice_free (MenuInfo, info);
}

484 485 486 487 488 489 490 491 492 493
static void
parse_child (ParserData   *data,
             const gchar  *element_name,
             const gchar **names,
             const gchar **values,
             GError      **error)

{
  ObjectInfo* object_info;
  ChildInfo *child_info;
494 495
  const gchar *type = NULL;
  const gchar *internal_child = NULL;
496 497

  object_info = state_peek_info (data, ObjectInfo);
498 499
  if (!object_info ||
      !(strcmp (object_info->tag.name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
500
        strcmp (object_info->tag.name, "template") == 0))
501
    {
502
      error_invalid_tag (data, element_name, NULL, error);
503 504
      return;
    }
Matthias Clasen's avatar
Matthias Clasen committed
505

506 507 508 509
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type", &type,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "internal-child", &internal_child,
                                    G_MARKUP_COLLECT_INVALID))
510
    {
511 512
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
513 514
    }

515 516 517 518
  child_info = g_slice_new0 (ChildInfo);
  child_info->tag.name = element_name;
  child_info->type = g_strdup (type);
  child_info->internal_child = g_strdup (internal_child);
519
  child_info->parent = (CommonInfo*)object_info;
Matthias Clasen's avatar
Matthias Clasen committed
520
  state_push (data, child_info);
521

522
  object_info->object = builder_construct (data, object_info, error);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
}

static void
free_child_info (ChildInfo *info)
{
  g_free (info->type);
  g_free (info->internal_child);
  g_slice_free (ChildInfo, info);
}

static void
parse_property (ParserData   *data,
                const gchar  *element_name,
                const gchar **names,
                const gchar **values,
                GError      **error)
{
  PropertyInfo *info;
541
  const gchar *name = NULL;
542 543 544
  const gchar *context = NULL;
  const gchar *bind_source = NULL;
  const gchar *bind_property = NULL;
545
  const gchar *bind_flags_str = NULL;
546
  GBindingFlags bind_flags = G_BINDING_DEFAULT;
547
  gboolean translatable = FALSE;
548
  ObjectInfo *object_info;
549
  GParamSpec *pspec = NULL;
550
  gint line, col;
551

552
  object_info = state_peek_info (data, ObjectInfo);
Matthias Clasen's avatar
Matthias Clasen committed
553
  if (!object_info ||
554
      !(strcmp (object_info->tag.name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
555
        strcmp (object_info->tag.name, "template") == 0))
556
    {
557
      error_invalid_tag (data, element_name, NULL, error);
558 559
      return;
    }
560

561 562 563 564 565 566 567 568 569
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING, "name", &name,
                                    G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &context,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-source", &bind_source,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-property", &bind_property,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-flags", &bind_flags_str,
                                    G_MARKUP_COLLECT_INVALID))
570
    {
571 572 573
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
    }
574

575
  pspec = g_object_class_find_property (object_info->oclass, name);
576

577
  if (!pspec)
578
    {
579 580 581 582 583 584
      g_set_error (error,
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_INVALID_PROPERTY,
                   "Invalid property: %s.%s",
                   g_type_name (object_info->type), name);
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
585 586 587
      return;
    }

588 589
  if (bind_flags_str)
    {
590
      if (!_gtk_builder_flags_from_string (G_TYPE_BINDING_FLAGS, NULL, bind_flags_str, &bind_flags, error))
591 592 593 594 595 596
        {
          _gtk_builder_prefix_error (data->builder, data->ctx, error);
          return;
        }
    }

Matthias Clasen's avatar
Matthias Clasen committed
597 598
  g_markup_parse_context_get_position (data->ctx, &line, &col);

599 600
  if (bind_source && bind_property)
    {
Matthias Clasen's avatar
Matthias Clasen committed
601
      BindingInfo *binfo;
602

Matthias Clasen's avatar
Matthias Clasen committed
603 604
      binfo = g_slice_new (BindingInfo);
      binfo->target = NULL;
605
      binfo->target_pspec = pspec;
606 607 608
      binfo->source = g_strdup (bind_source);
      binfo->source_property = g_strdup (bind_property);
      binfo->flags = bind_flags;
Matthias Clasen's avatar
Matthias Clasen committed
609 610
      binfo->line = line;
      binfo->col = col;
611 612 613 614 615 616 617 618 619 620 621

      object_info->bindings = g_slist_prepend (object_info->bindings, binfo);
    }
  else if (bind_source || bind_property)
    {
      error_missing_attribute (data, element_name,
                               (bind_source) ? "bind-property" : "bind-source",
                               error);
      return;
    }

622 623
  info = g_slice_new (PropertyInfo);
  info->tag.name = element_name;
624
  info->pspec = pspec;
625
  info->text = g_string_new ("");
626
  info->translatable = translatable;
627
  info->bound = (bind_source && bind_property);
628
  info->context = g_strdup (context);
629 630
  info->line = line;
  info->col = col;
631

632
  state_push (data, info);
633 634 635
}

static void
636
free_property_info (PropertyInfo *info)
637
{
638
  g_string_free (info->text, TRUE);
639
  g_free (info->context);
640 641 642 643 644 645 646 647 648 649 650
  g_slice_free (PropertyInfo, info);
}

static void
parse_signal (ParserData   *data,
              const gchar  *element_name,
              const gchar **names,
              const gchar **values,
              GError      **error)
{
  SignalInfo *info;
651
  const gchar *name;
652 653
  const gchar *handler = NULL;
  const gchar *object = NULL;
654
  gboolean after = FALSE;
655
  gboolean swapped = -1;
656
  ObjectInfo *object_info;
657
  guint id = 0;
658
  GQuark detail = 0;
659

660
  object_info = state_peek_info (data, ObjectInfo);
661 662
  if (!object_info ||
      !(strcmp (object_info->tag.name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
663
        strcmp (object_info->tag.name, "template") == 0))
664
    {
665
      error_invalid_tag (data, element_name, NULL, error);
666 667
      return;
    }
668

669 670 671 672 673 674 675 676
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING, "name", &name,
                                    G_MARKUP_COLLECT_STRING, "handler", &handler,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "object", &object,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "last_modification_time", NULL,
                                    G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "after", &after,
                                    G_MARKUP_COLLECT_TRISTATE|G_MARKUP_COLLECT_OPTIONAL, "swapped", &swapped,
                                    G_MARKUP_COLLECT_INVALID))
677
    {
678
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
679 680
      return;
    }
681 682

  if (!g_signal_parse_name (name, object_info->type, &id, &detail, FALSE))
683
    {
684 685 686 687 688 689
      g_set_error (error,
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_INVALID_SIGNAL,
                   "Invalid signal '%s' for type '%s'",
                   name, g_type_name (object_info->type));
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
690 691 692 693
      return;
    }

  /* Swapped defaults to FALSE except when object is set */
694 695 696 697 698 699 700
  if (swapped == -1)
    {
      if (object)
        swapped = TRUE;
      else
        swapped = FALSE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
701

702
  info = g_slice_new0 (SignalInfo);
703 704
  info->id = id;
  info->detail = detail;
705
  info->handler = g_strdup (handler);
706 707 708 709
  if (after)
    info->flags |= G_CONNECT_AFTER;
  if (swapped)
    info->flags |= G_CONNECT_SWAPPED;
710
  info->connect_object_name = g_strdup (object);
711 712 713 714 715 716 717 718
  state_push (data, info);

  info->tag.name = element_name;
}

/* Called by GtkBuilder */
void
_free_signal_info (SignalInfo *info,
Matthias Clasen's avatar
Matthias Clasen committed
719
                   gpointer    user_data)
720 721 722 723 724 725 726
{
  g_free (info->handler);
  g_free (info->connect_object_name);
  g_free (info->object_name);
  g_slice_free (SignalInfo, info);
}

727 728
static void
free_requires_info (RequiresInfo *info,
Matthias Clasen's avatar
Matthias Clasen committed
729
                    gpointer      user_data)
730 731 732 733 734
{
  g_free (info->library);
  g_slice_free (RequiresInfo, info);
}

735 736
static void
parse_interface (ParserData   *data,
Matthias Clasen's avatar
Matthias Clasen committed
737 738 739 740
                 const gchar  *element_name,
                 const gchar **names,
                 const gchar **values,
                 GError      **error)
741
{
742
  const gchar *domain = NULL;
743

744 745 746
  if (!g_markup_collect_attributes (element_name, names, values, error,
                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "domain", &domain,
                                    G_MARKUP_COLLECT_INVALID))
747
    {
748 749 750
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
    }
Matthias Clasen's avatar
Matthias Clasen committed
751

752 753 754 755 756 757 758
  if (domain)
    {
      if (data->domain && strcmp (data->domain, domain) != 0)
        {
          g_warning ("%s: interface domain '%s' overrides programmatic value '%s'",
                     data->filename, domain, data->domain);
          g_free (data->domain);
Matthias Clasen's avatar
Matthias Clasen committed
759
        }
760 761 762

      data->domain = g_strdup (domain);
      gtk_builder_set_translation_domain (data->builder, data->domain);
763 764 765 766 767
    }
}

static SubParser *
create_subparser (GObject       *object,
Matthias Clasen's avatar
Matthias Clasen committed
768 769 770 771
                  GObject       *child,
                  const gchar   *element_name,
                  GMarkupParser *parser,
                  gpointer       user_data)
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
{
  SubParser *subparser;

  subparser = g_slice_new0 (SubParser);
  subparser->object = object;
  subparser->child = child;
  subparser->tagname = g_strdup (element_name);
  subparser->start = element_name;
  subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
  subparser->data = user_data;

  return subparser;
}

static void
free_subparser (SubParser *subparser)
{
  g_free (subparser->tagname);
  g_slice_free (SubParser, subparser);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
794 795 796 797 798 799
subparser_start (GMarkupParseContext  *context,
                 const gchar          *element_name,
                 const gchar         **names,
                 const gchar         **values,
                 ParserData           *data,
                 GError              **error)
800 801 802 803 804 805 806 807 808 809
{
  SubParser *subparser = data->subparser;

  if (!subparser->start &&
      strcmp (element_name, subparser->tagname) == 0)
    subparser->start = element_name;

  if (subparser->start)
    {
      if (subparser->parser->start_element)
Matthias Clasen's avatar
Matthias Clasen committed
810 811 812
        subparser->parser->start_element (context,
                                          element_name, names, values,
                                          subparser->data, error);
813 814 815 816 817 818
      return FALSE;
    }
  return TRUE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
819 820 821 822
subparser_end (GMarkupParseContext  *context,
               const gchar          *element_name,
               ParserData           *data,
               GError              **error)
823 824 825
{
  if (data->subparser->parser->end_element)
    data->subparser->parser->end_element (context, element_name,
Matthias Clasen's avatar
Matthias Clasen committed
826
                                          data->subparser->data, error);
827

828 829 830
  if (*error)
    return;

831
  if (strcmp (data->subparser->start, element_name) != 0)
832
    return;
833

834
  gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
Matthias Clasen's avatar
Matthias Clasen committed
835 836 837 838
                                data->builder,
                                data->subparser->child,
                                element_name,
                                data->subparser->data);
839
  g_free (data->subparser->parser);
840

841 842
  if (_gtk_builder_lookup_failed (data->builder, error))
    return;
843

844 845
  if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
    data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
Matthias Clasen's avatar
Matthias Clasen committed
846
                                               data->subparser);
847 848
  else
    free_subparser (data->subparser);
849

850 851 852 853
  data->subparser = NULL;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
854 855 856 857 858 859
parse_custom (GMarkupParseContext  *context,
              const gchar          *element_name,
              const gchar         **names,
              const gchar         **values,
              ParserData           *data,
              GError              **error)
860 861 862
{
  CommonInfo* parent_info;
  GMarkupParser parser;
863
  gpointer subparser_data;
864 865 866 867 868 869 870
  GObject *object;
  GObject *child;

  parent_info = state_peek_info (data, CommonInfo);
  if (!parent_info)
    return FALSE;

871 872
  if (strcmp (parent_info->tag.name, "object") == 0 ||
      strcmp (parent_info->tag.name, "template") == 0)
873 874 875
    {
      ObjectInfo* object_info = (ObjectInfo*)parent_info;
      if (!object_info->object)
Matthias Clasen's avatar
Matthias Clasen committed
876 877 878 879 880 881 882 883
        {
          object_info->properties = g_slist_reverse (object_info->properties);
          object_info->object = _gtk_builder_construct (data->builder,
                                                        object_info,
                                                        error);
          if (!object_info->object)
            return TRUE; /* A GError is already set */
        }
884 885 886 887 888 889 890
      g_assert (object_info->object);
      object = object_info->object;
      child = NULL;
    }
  else if (strcmp (parent_info->tag.name, "child") == 0)
    {
      ChildInfo* child_info = (ChildInfo*)parent_info;
Matthias Clasen's avatar
Matthias Clasen committed
891

892
      _gtk_builder_add (data->builder, child_info);
Matthias Clasen's avatar
Matthias Clasen committed
893

894 895 896 897 898 899 900
      object = ((ObjectInfo*)child_info->parent)->object;
      child  = child_info->object;
    }
  else
    return FALSE;

  if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
Matthias Clasen's avatar
Matthias Clasen committed
901 902 903 904 905
                                       data->builder,
                                       child,
                                       element_name,
                                       &parser,
                                       &subparser_data))
906
    return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
907

908
  data->subparser = create_subparser (object, child, element_name,
Matthias Clasen's avatar
Matthias Clasen committed
909 910
                                      &parser, subparser_data);

911 912
  if (parser.start_element)
    parser.start_element (context,
Matthias Clasen's avatar
Matthias Clasen committed
913 914
                          element_name, names, values,
                          subparser_data, error);
915 916 917 918
  return TRUE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
919 920 921 922 923 924
start_element (GMarkupParseContext  *context,
               const gchar          *element_name,
               const gchar         **names,
               const gchar         **values,
               gpointer              user_data,
               GError              **error)
925 926 927
{
  ParserData *data = (ParserData*)user_data;

928
#ifdef G_ENABLE_DEBUG
929
  if (GTK_DEBUG_CHECK (BUILDER))
930 931 932 933 934 935 936 937 938 939 940
    {
      GString *tags = g_string_new ("");
      int i;
      for (i = 0; names[i]; i++)
        g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);

      if (i)
        {
          g_string_insert_c (tags, 0, ' ');
          g_string_truncate (tags, tags->len - 1);
        }
941
      g_message ("<%s%s>", element_name, tags->str);
942 943 944 945 946 947
      g_string_free (tags, TRUE);
    }
#endif

  if (!data->last_element && strcmp (element_name, "interface") != 0)
    {
948
      error_unhandled_tag (data, element_name, error);
949 950 951 952 953
      return;
    }
  data->last_element = element_name;

  if (data->subparser)
Matthias Clasen's avatar
Matthias Clasen committed
954 955 956 957
    {
      if (!subparser_start (context, element_name, names, values, data, error))
        return;
    }
958 959 960 961

  if (strcmp (element_name, "requires") == 0)
    parse_requires (data, element_name, names, values, error);
  else if (strcmp (element_name, "object") == 0)
962
    parse_object (context, data, element_name, names, values, error);
963 964
  else if (strcmp (element_name, "template") == 0)
    parse_template (context, data, element_name, names, values, error);
965 966 967 968
  else if (data->requested_objects && !data->inside_requested_object)
    {
      /* If outside a requested object, simply ignore this tag */
    }
969 970 971 972 973 974 975 976
  else if (strcmp (element_name, "child") == 0)
    parse_child (data, element_name, names, values, error);
  else if (strcmp (element_name, "property") == 0)
    parse_property (data, element_name, names, values, error);
  else if (strcmp (element_name, "signal") == 0)
    parse_signal (data, element_name, names, values, error);
  else if (strcmp (element_name, "interface") == 0)
    parse_interface (data, element_name, names, values, error);
977
  else if (strcmp (element_name, "menu") == 0)
978
    _gtk_builder_menu_start (data, element_name, names, values, error);
979 980 981 982 983 984
  else if (strcmp (element_name, "placeholder") == 0)
    {
      /* placeholder has no special treatmeant, but it needs an
       * if clause to avoid an error below.
       */
    }
Matthias Clasen's avatar
Matthias Clasen committed
985
  else if (!parse_custom (context, element_name, names, values, data, error))
986
    error_unhandled_tag (data, element_name, error);
987 988
}

989
const gchar *
990
_gtk_builder_parser_translate (const gchar *domain,
Matthias Clasen's avatar
Matthias Clasen committed
991 992
                               const gchar *context,
                               const gchar *text)
993
{
994
  const gchar *s;
995 996

  if (context)
997
    s = g_dpgettext2 (domain, context, text);
998
  else
999
    s = g_dgettext (domain, text);
1000

1001
  return s;
1002 1003
}

1004
static void
Matthias Clasen's avatar
Matthias Clasen committed
1005 1006 1007 1008
end_element (GMarkupParseContext  *context,
             const gchar          *element_name,
             gpointer              user_data,
             GError              **error)
1009 1010 1011
{
  ParserData *data = (ParserData*)user_data;

1012
  GTK_NOTE (BUILDER, g_message ("</%s>", element_name));
1013 1014 1015 1016 1017 1018 1019

  if (data->subparser && data->subparser->start)
    {
      subparser_end (context, element_name, data, error);
      return;
    }

1020 1021 1022 1023 1024 1025 1026 1027 1028
  if (strcmp (element_name, "requires") == 0)
    {
      RequiresInfo *req_info = state_pop_info (data, RequiresInfo);

      /* TODO: Allow third party widget developers to check thier
       * required versions, possibly throw a signal allowing them
       * to check thier library versions here.
       */
      if (!strcmp (req_info->library, "gtk+"))
Matthias Clasen's avatar
Matthias Clasen committed
1029 1030
        {
          if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
            {
              g_set_error (error,
                           GTK_BUILDER_ERROR,
                           GTK_BUILDER_ERROR_VERSION_MISMATCH,
                           "Required %s version %d.%d, current version is %d.%d",
                           req_info->library,
                           req_info->major, req_info->minor,
                           GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
              _gtk_builder_prefix_error (data->builder, context, error);
           }
Matthias Clasen's avatar
Matthias Clasen committed
1041
        }
1042
      free_requires_info (req_info, NULL);
1043
    }
1044 1045 1046 1047 1048 1049 1050
  else if (strcmp (element_name, "interface") == 0)
    {
    }
  else if (data->requested_objects && !data->inside_requested_object)
    {
      /* If outside a requested object, simply ignore this tag */
    }
1051 1052 1053 1054
  else if (strcmp (element_name, "menu") == 0)
    {
      _gtk_builder_menu_end (data);
    }
1055
  else if (strcmp (element_name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
1056
           strcmp (element_name, "template") == 0)
1057 1058 1059 1060
    {
      ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
      ChildInfo* child_info = state_peek_info (data, ChildInfo);

1061 1062 1063
      if (data->requested_objects && data->inside_requested_object &&
          (data->cur_object_level == data->requested_object_level))
        {
1064 1065 1066
          GTK_NOTE (BUILDER,
                    g_message ("requested object end found at level %d",
                               data->requested_object_level));
1067 1068 1069 1070 1071 1072 1073 1074

          data->inside_requested_object = FALSE;
        }

      --data->cur_object_level;

      g_assert (data->cur_object_level >= 0);

1075 1076
      object_info->object = builder_construct (data, object_info, error);
      if (!object_info->object)
Matthias Clasen's avatar
Matthias Clasen committed
1077 1078 1079 1080
        {
          free_object_info (object_info);
          return;
        }
1081 1082 1083 1084 1085 1086
      if (child_info)
        child_info->object = object_info->object;

      if (GTK_IS_BUILDABLE (object_info->object) &&
          GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
        data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
1087 1088
      _gtk_builder_add_signals (data->builder, object_info->signals);

1089 1090 1091 1092 1093 1094 1095
      free_object_info (object_info);
    }
  else if (strcmp (element_name, "property") == 0)
    {
      PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
      CommonInfo *info = state_peek_info (data, CommonInfo);

1096 1097
      g_assert (info != NULL);

1098
      /* Normal properties */
1099
      if (strcmp (info->tag.name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
1100
          strcmp (info->tag.name, "template") == 0)
1101 1102
        {
          ObjectInfo *object_info = (ObjectInfo*)info;
1103 1104 1105

          if (prop_info->translatable && prop_info->text->len)
            {
1106 1107 1108 1109 1110 1111
              const gchar *translated;

              translated = _gtk_builder_parser_translate (data->domain,
                                                          prop_info->context,
                                                          prop_info->text->str);
              g_string_assign (prop_info->text, translated);
1112 1113
            }

1114
          object_info->properties = g_slist_prepend (object_info->properties, prop_info);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
        }
      else
        g_assert_not_reached ();
    }
  else if (strcmp (element_name, "child") == 0)
    {
      ChildInfo *child_info = state_pop_info (data, ChildInfo);

      _gtk_builder_add (data->builder, child_info);

      free_child_info (child_info);
    }
  else if (strcmp (element_name, "signal") == 0)
    {
      SignalInfo *signal_info = state_pop_info (data, SignalInfo);
      ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
1131
      g_assert (object_info != NULL);
1132
      signal_info->object_name = g_strdup (object_info->id);
1133
      object_info->signals = g_slist_prepend (object_info->signals, signal_info);
1134 1135 1136 1137 1138 1139
    }
  else if (strcmp (element_name, "placeholder") == 0)
    {
    }
  else
    {
1140 1141 1142 1143 1144
      g_set_error (error,
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_UNHANDLED_TAG,
                   "Unhandled tag: <%s>", element_name);
      _gtk_builder_prefix_error (data->builder, context, error);
1145 1146 1147 1148 1149 1150
    }
}

/* Called for character data */
/* text is not nul-terminated */
static void
1151 1152 1153 1154 1155
text (GMarkupParseContext  *context,
      const gchar          *text,
      gsize                 text_len,
      gpointer              user_data,
      GError              **error)
1156 1157 1158 1159 1160 1161
{
  ParserData *data = (ParserData*)user_data;
  CommonInfo *info;

  if (data->subparser && data->subparser->start)
    {
1162
      GError *tmp_error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1163

1164 1165
      if (data->subparser->parser->text)
        data->subparser->parser->text (context, text, text_len,
1166 1167
                                       data->subparser->data, &tmp_error);
      if (tmp_error)
Matthias Clasen's avatar
Matthias Clasen committed
1168
        g_propagate_error (error, tmp_error);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
      return;
    }

  if (!data->stack)
    return;

  info = state_peek_info (data, CommonInfo);
  g_assert (info != NULL);

  if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
    {
      PropertyInfo *prop_info = (PropertyInfo*)info;

1182
      g_string_append_len (prop_info->text, text, text_len);
1183 1184 1185
    }
}

1186 1187 1188
static void
free_info (CommonInfo *info)
{
1189 1190
  if (strcmp (info->tag.name, "object") == 0 ||
      strcmp (info->tag.name, "template") == 0)
1191
    free_object_info ((ObjectInfo *)info);
1192
  else if (strcmp (info->tag.name, "child") == 0)
1193
    free_child_info ((ChildInfo *)info);
1194
  else if (strcmp (info->tag.name, "property") == 0)
1195
    free_property_info ((PropertyInfo *)info);
1196
  else if (strcmp (info->tag.name, "signal") == 0)
1197
    _free_signal_info ((SignalInfo *)info, NULL);
1198
  else if (strcmp (info->tag.name, "requires") == 0)
1199
    free_requires_info ((RequiresInfo *)info, NULL);
1200
  else if (strcmp (info->tag.name, "menu") == 0)
1201
    free_menu_info ((MenuInfo *)info);
1202
  else
1203 1204 1205
    g_assert_not_reached ();
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
static const GMarkupParser parser = {
  start_element,
  end_element,
  text,
  NULL,
};

void
_gtk_builder_parser_parse_buffer (GtkBuilder   *builder,
                                  const gchar  *filename,
                                  const gchar  *buffer,
                                  gsize         length,
1218
                                  gchar       **requested_objs,
1219 1220
                                  GError      **error)
{
1221
  const gchar* domain;
1222 1223
  ParserData *data;
  GSList *l;
1224

1225 1226 1227 1228 1229 1230 1231
  /* Store the original domain so that interface domain attribute can be
   * applied for the builder and the original domain can be restored after
   * parsing has finished. This allows subparsers to translate elements with
   * gtk_builder_get_translation_domain() without breaking the ABI or API
   */
  domain = gtk_builder_get_translation_domain (builder);

1232 1233 1234
  data = g_new0 (ParserData, 1);
  data->builder = builder;
  data->filename = filename;
1235
  data->domain = g_strdup (domain);
1236
  data->object_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
Matthias Clasen's avatar
Matthias Clasen committed
1237
                                            (GDestroyNotify)g_free, NULL);
1238

1239 1240 1241 1242 1243 1244 1245 1246 1247
  data->requested_objects = NULL;
  if (requested_objs)
    {
      gint i;

      data->inside_requested_object = FALSE;
      for (i = 0; requested_objs[i]; ++i)
        {
          data->requested_objects = g_slist_prepend (data->requested_objects,
Matthias Clasen's avatar
Matthias Clasen committed
1248
                                                     g_strdup (requested_objs[i]));
1249 1250 1251 1252 1253 1254 1255 1256
        }
    }
  else
    {
      /* get all the objects */
      data->inside_requested_object = TRUE;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1257 1258
  data->ctx = g_markup_parse_context_new (&parser,
                                          G_MARKUP_TREAT_CDATA_AS_TEXT,
1259
                                          data, NULL);
1260 1261 1262

  if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
    goto out;
1263

1264
  _gtk_builder_finish (builder);
1265 1266
  if (_gtk_builder_lookup_failed (builder, error))
    goto out;
1267 1268 1269 1270 1271 1272

  /* Custom parser_finished */
  data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
  for (l = data->custom_finalizers; l; l = l->next)
    {
      SubParser *sub = (SubParser*)l->data;
1273

1274 1275 1276 1277 1278
      gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
                                     builder,
                                     sub->child,
                                     sub->tagname,
                                     sub->data);
1279 1280
      if (_gtk_builder_lookup_failed (builder, error))
        goto out;
1281
    }
1282

1283 1284 1285 1286 1287
  /* Common parser_finished, for all created objects */
  data->finalizers = g_slist_reverse (data->finalizers);
  for (l = data->finalizers; l; l = l->next)
    {
      GtkBuildable *buildable = (GtkBuildable*)l->data;
1288

1289
      gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1290 1291
      if (_gtk_builder_lookup_failed (builder, error))
        goto out;
1292 1293 1294 1295
    }

 out:

Matthias Clasen's avatar
Matthias Clasen committed
1296 1297
  g_slist_free_full (data->stack, (GDestroyNotify)free_info);
  g_slist_free_full (data->custom_finalizers, (GDestroyNotify)free_subparser);
1298
  g_slist_free (data->finalizers);
Matthias Clasen's avatar
Matthias Clasen committed
1299
  g_slist_free_full (data->requested_objects, g_free);
1300
  g_free (data->domain);
1301
  g_hash_table_destroy (data->object_ids);
1302
  g_markup_parse_context_free (data->ctx);
1303
  g_free (data);
1304 1305 1306

  /* restore the original domain */
  gtk_builder_set_translation_domain (builder, domain);
1307
}