gtkbuilderparser.c 40.4 KB
Newer Older
Johan Dahlin's avatar
Johan Dahlin committed
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/>.
Johan Dahlin's avatar
Johan Dahlin committed
17 18
 */

19
#include "config.h"
20

Johan Dahlin's avatar
Johan Dahlin committed
21 22 23
#include <string.h>
#include <gmodule.h>

24
#include <gio/gio.h>
Johan Dahlin's avatar
Johan Dahlin committed
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

Johan Dahlin's avatar
Johan Dahlin committed
33

34 35
static void free_property_info (PropertyInfo *info);
static void free_object_info (ObjectInfo *info);
Johan Dahlin's avatar
Johan Dahlin committed
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)
Johan Dahlin's avatar
Johan Dahlin committed
72
{
Matthias Clasen's avatar
Matthias Clasen committed
73
  gint line, col;
Johan Dahlin's avatar
Johan Dahlin committed
74

Matthias Clasen's avatar
Matthias Clasen committed
75
  g_markup_parse_context_get_position (data->ctx, &line, &col);
Johan Dahlin's avatar
Johan Dahlin committed
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);
Johan Dahlin's avatar
Johan Dahlin committed
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)
Johan Dahlin's avatar
Johan Dahlin committed
89
{
Matthias Clasen's avatar
Matthias Clasen committed
90
  gint line, col;
Johan Dahlin's avatar
Johan Dahlin committed
91

Matthias Clasen's avatar
Matthias Clasen committed
92
  g_markup_parse_context_get_position (data->ctx, &line, &col);
Johan Dahlin's avatar
Johan Dahlin committed
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);
Johan Dahlin's avatar
Johan Dahlin committed
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);
Johan Dahlin's avatar
Johan Dahlin committed
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);
}

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

  g_assert (object_info != NULL);

132
  if (object_info->object && object_info->applied_properties)
Johan Dahlin's avatar
Johan Dahlin committed
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

Johan Dahlin's avatar
Johan Dahlin committed
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;
}

Johan Dahlin's avatar
Johan Dahlin committed
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)
Johan Dahlin's avatar
Johan Dahlin committed
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;
Johan Dahlin's avatar
Johan Dahlin committed
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))
Johan Dahlin's avatar
Johan Dahlin committed
266
    {
267 268 269
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
    }
270

271 272 273 274
  if (object_class)
    {
      object_type = gtk_builder_get_type_from_name (data->builder, object_class);
      if (object_type == G_TYPE_INVALID)
275
        {
276 277 278 279 280 281
          g_set_error (error,
                       GTK_BUILDER_ERROR,
                       GTK_BUILDER_ERROR_INVALID_VALUE,
                       "Invalid object type '%s'", object_class);
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
Matthias Clasen's avatar
Matthias Clasen committed
282
       }
283 284 285 286 287 288 289 290
    }
  else if (type_func)
    {
      /* 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);
      if (object_type == G_TYPE_INVALID)
Matthias Clasen's avatar
Matthias Clasen committed
291
        {
292 293 294 295 296
          g_set_error (error,
                       GTK_BUILDER_ERROR,
                       GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
                       "Invalid type function '%s'", type_func);
          _gtk_builder_prefix_error (data->builder, context, error);
Matthias Clasen's avatar
Matthias Clasen committed
297 298
          return;
        }
Johan Dahlin's avatar
Johan Dahlin committed
299
    }
Matthias Clasen's avatar
Matthias Clasen committed
300
  else
Johan Dahlin's avatar
Johan Dahlin committed
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;
Johan Dahlin's avatar
Johan Dahlin committed
310 311
    }

312 313 314 315 316 317 318 319 320 321
  ++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;

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

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

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

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

354
  g_markup_parse_context_get_position (context, &line, NULL);
355
  g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
Johan Dahlin's avatar
Johan Dahlin committed
356 357
}

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

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

375 376 377 378
  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))
379
    {
380
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
381 382
      return;
    }
383 384

  if (template_type == 0)
385
    {
386 387 388 389 390 391
      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);
392 393
      return;
    }
394
  else if (state_peek (data) != NULL)
395
    {
396
      error_invalid_tag (data, "template", NULL, error);
397 398 399 400 401 402 403
      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
404 405
                   GTK_BUILDER_ERROR,
                   GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
406
                   "Parsed template definition for type '%s', expected type '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
407
                   object_class, g_type_name (template_type));
408
      _gtk_builder_prefix_error (data->builder, context, error);
409 410 411
      return;
    }

412 413 414 415 416 417 418 419 420
  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,
421 422
                       "Invalid template parent type '%s'", parent_class);
          _gtk_builder_prefix_error (data->builder, context, error);
423
          return;
Matthias Clasen's avatar
Matthias Clasen committed
424
        }
425 426 427 428
      if (parent_type != expected_type)
        {
          g_set_error (error, GTK_BUILDER_ERROR,
                       GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
429
                       "Template parent type '%s' does not match instance parent type '%s'.",
430
                       parent_class, g_type_name (expected_type));
431
          _gtk_builder_prefix_error (data->builder, context, error);
432 433 434
          return;
        }
    }
Matthias Clasen's avatar
Matthias Clasen committed
435

436 437 438
  ++data->cur_object_level;

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

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

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


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

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

Johan Dahlin's avatar
Johan Dahlin committed
483 484 485 486 487 488 489 490 491 492
static void
parse_child (ParserData   *data,
             const gchar  *element_name,
             const gchar **names,
             const gchar **values,
             GError      **error)

{
  ObjectInfo* object_info;
  ChildInfo *child_info;
493 494
  const gchar *type = NULL;
  const gchar *internal_child = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
495 496

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

505 506 507 508
  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))
Johan Dahlin's avatar
Johan Dahlin committed
509
    {
510 511
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
Johan Dahlin's avatar
Johan Dahlin committed
512 513
    }

514 515 516 517
  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);
Johan Dahlin's avatar
Johan Dahlin committed
518
  child_info->parent = (CommonInfo*)object_info;
Matthias Clasen's avatar
Matthias Clasen committed
519
  state_push (data, child_info);
Johan Dahlin's avatar
Johan Dahlin committed
520

521
  object_info->object = builder_construct (data, object_info, error);
Johan Dahlin's avatar
Johan Dahlin committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
}

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;
540
  const gchar *name = NULL;
541 542 543
  const gchar *context = NULL;
  const gchar *bind_source = NULL;
  const gchar *bind_property = NULL;
544
  const gchar *bind_flags_str = NULL;
545
  GBindingFlags bind_flags = G_BINDING_DEFAULT;
Johan Dahlin's avatar
Johan Dahlin committed
546
  gboolean translatable = FALSE;
547
  ObjectInfo *object_info;
548
  GParamSpec *pspec = NULL;
549
  gint line, col;
Johan Dahlin's avatar
Johan Dahlin committed
550

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

560 561 562 563 564 565 566 567 568
  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))
Johan Dahlin's avatar
Johan Dahlin committed
569
    {
570 571 572
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
      return;
    }
573

574
  pspec = g_object_class_find_property (object_info->oclass, name);
Johan Dahlin's avatar
Johan Dahlin committed
575

576
  if (!pspec)
Johan Dahlin's avatar
Johan Dahlin committed
577
    {
578 579 580 581 582 583
      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);
Johan Dahlin's avatar
Johan Dahlin committed
584 585 586
      return;
    }

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

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

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

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

      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;
    }

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

631
  state_push (data, info);
Johan Dahlin's avatar
Johan Dahlin committed
632 633 634
}

static void
635
free_property_info (PropertyInfo *info)
Johan Dahlin's avatar
Johan Dahlin committed
636
{
Matthias Clasen's avatar
Matthias Clasen committed
637
  g_string_free (info->text, TRUE);
638
  g_free (info->context);
Johan Dahlin's avatar
Johan Dahlin committed
639 640 641 642 643 644 645 646 647 648 649
  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;
650
  const gchar *name;
651 652
  const gchar *handler = NULL;
  const gchar *object = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
653
  gboolean after = FALSE;
654
  gboolean swapped = -1;
655
  ObjectInfo *object_info;
656
  guint id = 0;
657
  GQuark detail = 0;
Johan Dahlin's avatar
Johan Dahlin committed
658

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

668 669 670 671 672 673 674 675
  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))
Johan Dahlin's avatar
Johan Dahlin committed
676
    {
677
      _gtk_builder_prefix_error (data->builder, data->ctx, error);
Johan Dahlin's avatar
Johan Dahlin committed
678 679
      return;
    }
680 681

  if (!g_signal_parse_name (name, object_info->type, &id, &detail, FALSE))
Johan Dahlin's avatar
Johan Dahlin committed
682
    {
683 684 685 686 687 688
      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);
Johan Dahlin's avatar
Johan Dahlin committed
689 690 691 692
      return;
    }

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

Johan Dahlin's avatar
Johan Dahlin committed
701
  info = g_slice_new0 (SignalInfo);
702 703
  info->id = id;
  info->detail = detail;
704
  info->handler = g_strdup (handler);
Johan Dahlin's avatar
Johan Dahlin committed
705 706 707 708
  if (after)
    info->flags |= G_CONNECT_AFTER;
  if (swapped)
    info->flags |= G_CONNECT_SWAPPED;
709
  info->connect_object_name = g_strdup (object);
Johan Dahlin's avatar
Johan Dahlin committed
710 711 712 713 714 715 716 717
  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
718
                   gpointer    user_data)
Johan Dahlin's avatar
Johan Dahlin committed
719 720 721 722 723 724 725
{
  g_free (info->handler);
  g_free (info->connect_object_name);
  g_free (info->object_name);
  g_slice_free (SignalInfo, info);
}

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

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

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

751 752 753 754 755 756 757
  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
758
        }
759 760 761

      data->domain = g_strdup (domain);
      gtk_builder_set_translation_domain (data->builder, data->domain);
Johan Dahlin's avatar
Johan Dahlin committed
762 763 764 765 766
    }
}

static SubParser *
create_subparser (GObject       *object,
Matthias Clasen's avatar
Matthias Clasen committed
767 768 769 770
                  GObject       *child,
                  const gchar   *element_name,
                  GMarkupParser *parser,
                  gpointer       user_data)
Johan Dahlin's avatar
Johan Dahlin committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
{
  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
793 794 795 796 797 798
subparser_start (GMarkupParseContext  *context,
                 const gchar          *element_name,
                 const gchar         **names,
                 const gchar         **values,
                 ParserData           *data,
                 GError              **error)
Johan Dahlin's avatar
Johan Dahlin committed
799 800 801 802 803 804 805 806 807 808
{
  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
809 810 811
        subparser->parser->start_element (context,
                                          element_name, names, values,
                                          subparser->data, error);
Johan Dahlin's avatar
Johan Dahlin committed
812 813 814 815 816 817
      return FALSE;
    }
  return TRUE;
}

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

827 828 829
  if (*error)
    return;

Matthias Clasen's avatar
Matthias Clasen committed
830
  if (strcmp (data->subparser->start, element_name) != 0)
Johan Dahlin's avatar
Johan Dahlin committed
831
    return;
Matthias Clasen's avatar
Matthias Clasen committed
832

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

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

Johan Dahlin's avatar
Johan Dahlin committed
843 844
  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
845
                                               data->subparser);
Johan Dahlin's avatar
Johan Dahlin committed
846 847
  else
    free_subparser (data->subparser);
Matthias Clasen's avatar
Matthias Clasen committed
848

Johan Dahlin's avatar
Johan Dahlin committed
849 850 851 852
  data->subparser = NULL;
}

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

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

870 871
  if (strcmp (parent_info->tag.name, "object") == 0 ||
      strcmp (parent_info->tag.name, "template") == 0)
Johan Dahlin's avatar
Johan Dahlin committed
872 873 874
    {
      ObjectInfo* object_info = (ObjectInfo*)parent_info;
      if (!object_info->object)
Matthias Clasen's avatar
Matthias Clasen committed
875 876 877 878 879 880 881 882
        {
          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 */
        }
Johan Dahlin's avatar
Johan Dahlin committed
883 884 885 886 887 888 889
      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
890

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

Johan Dahlin's avatar
Johan Dahlin committed
893 894 895 896 897 898 899
      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
900 901 902 903 904
                                       data->builder,
                                       child,
                                       element_name,
                                       &parser,
                                       &subparser_data))
Johan Dahlin's avatar
Johan Dahlin committed
905
    return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
906

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

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

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

927
#ifdef G_ENABLE_DEBUG
Matthias Clasen's avatar
Matthias Clasen committed
928
  if (GTK_DEBUG_CHECK (BUILDER))
Johan Dahlin's avatar
Johan Dahlin committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    {
      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);
        }
      g_print ("<%s%s>\n", element_name, tags->str);
      g_string_free (tags, TRUE);
    }
#endif

  if (!data->last_element && strcmp (element_name, "interface") != 0)
    {
947
      error_unhandled_tag (data, element_name, error);
Johan Dahlin's avatar
Johan Dahlin committed
948 949 950 951 952
      return;
    }
  data->last_element = element_name;

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

  if (strcmp (element_name, "requires") == 0)
    parse_requires (data, element_name, names, values, error);
  else if (strcmp (element_name, "object") == 0)
961
    parse_object (context, data, element_name, names, values, error);
962 963
  else if (strcmp (element_name, "template") == 0)
    parse_template (context, data, element_name, names, values, error);
964 965 966 967
  else if (data->requested_objects && !data->inside_requested_object)
    {
      /* If outside a requested object, simply ignore this tag */
    }
Johan Dahlin's avatar
Johan Dahlin committed
968 969 970 971 972 973 974 975
  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);
976
  else if (strcmp (element_name, "menu") == 0)
977
    _gtk_builder_menu_start (data, element_name, names, values, error);
Johan Dahlin's avatar
Johan Dahlin committed
978 979 980 981 982 983
  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
984
  else if (!parse_custom (context, element_name, names, values, data, error))
985
    error_unhandled_tag (data, element_name, error);
Johan Dahlin's avatar
Johan Dahlin committed
986 987
}

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

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

1000
  return s;
1001 1002
}

Johan Dahlin's avatar
Johan Dahlin committed
1003
static void
Matthias Clasen's avatar
Matthias Clasen committed
1004 1005 1006 1007
end_element (GMarkupParseContext  *context,
             const gchar          *element_name,
             gpointer              user_data,
             GError              **error)
Johan Dahlin's avatar
Johan Dahlin committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
{
  ParserData *data = (ParserData*)user_data;

  GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));

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

1019 1020 1021 1022 1023 1024 1025 1026 1027
  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
1028 1029
        {
          if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
            {
              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
1040
        }
1041
      free_requires_info (req_info, NULL);
1042
    }
1043 1044 1045 1046 1047 1048 1049
  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 */
    }
1050 1051 1052 1053
  else if (strcmp (element_name, "menu") == 0)
    {
      _gtk_builder_menu_end (data);
    }
1054
  else if (strcmp (element_name, "object") == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
1055
           strcmp (element_name, "template") == 0)
Johan Dahlin's avatar
Johan Dahlin committed
1056 1057 1058 1059
    {
      ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
      ChildInfo* child_info = state_peek_info (data, ChildInfo);

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

          data->inside_requested_object = FALSE;
        }

      --data->cur_object_level;

      g_assert (data->cur_object_level >= 0);

1073 1074
      object_info->object = builder_construct (data, object_info, error);
      if (!object_info->object)
Matthias Clasen's avatar
Matthias Clasen committed
1075 1076 1077 1078
        {
          free_object_info (object_info);
          return;
        }
Johan Dahlin's avatar
Johan Dahlin committed
1079 1080 1081 1082 1083 1084
      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);
1085 1086
      _gtk_builder_add_signals (data->builder, object_info->signals);

Johan Dahlin's avatar
Johan Dahlin committed
1087 1088 1089 1090 1091 1092 1093
      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);

1094 1095
      g_assert (info != NULL);

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

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

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

1112
          object_info->properties = g_slist_prepend (object_info->properties, prop_info);
Johan Dahlin's avatar
Johan Dahlin committed
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
        }
      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);
1129
      g_assert (object_info != NULL);
Johan Dahlin's avatar
Johan Dahlin committed
1130
      signal_info->object_name = g_strdup (object_info->id);
1131
      object_info->signals = g_slist_prepend (object_info->signals, signal_info);
Johan Dahlin's avatar
Johan Dahlin committed
1132 1133 1134 1135 1136 1137
    }
  else if (strcmp (element_name, "placeholder") == 0)
    {
    }
  else
    {
1138 1139 1140 1141 1142
      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);
Johan Dahlin's avatar
Johan Dahlin committed
1143 1144 1145 1146 1147 1148
    }
}

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

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

Johan Dahlin's avatar
Johan Dahlin committed
1162 1163
      if (data->subparser->parser->text)
        data->subparser->parser->text (context, text, text_len,
1164 1165
                                       data->subparser->data, &tmp_error);
      if (tmp_error)
Matthias Clasen's avatar
Matthias Clasen committed
1166
        g_propagate_error (error, tmp_error);
Johan Dahlin's avatar
Johan Dahlin committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
      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;

1180
      g_string_append_len (prop_info->text, text, text_len);
Johan Dahlin's avatar
Johan Dahlin committed
1181 1182 1183
    }
}

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