gtkbuilder.c 43.7 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Johan Dahlin's avatar
Johan Dahlin committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (C) 1998-2002 James Henstridge <james@daa.com.au>
 * Copyright (C) 2006-2007 Async Open Source,
 *                         Johan Dahlin <jdahlin@async.com.br>,
 *                         Henrique Romano <henrique@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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
#include "config.h"
Johan Dahlin's avatar
Johan Dahlin committed
24 25 26 27 28 29 30 31 32 33 34
#include <errno.h> /* errno */
#include <stdlib.h> /* strtol, strtoul */
#include <string.h> /* strlen */

#include "gtkbuilder.h"
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtkmain.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
35
#include "gtkwindow.h"
Matthias Clasen's avatar
Matthias Clasen committed
36 37
#include "gtkicontheme.h"
#include "gtkstock.h"
Johan Dahlin's avatar
Johan Dahlin committed
38 39 40 41 42 43 44 45 46 47 48 49 50
#include "gtkalias.h"

static void gtk_builder_class_init     (GtkBuilderClass *klass);
static void gtk_builder_init           (GtkBuilder      *builder);
static void gtk_builder_finalize       (GObject         *object);
static void gtk_builder_set_property   (GObject         *object,
                                        guint            prop_id,
                                        const GValue    *value,
                                        GParamSpec      *pspec);
static void gtk_builder_get_property   (GObject         *object,
                                        guint            prop_id,
                                        GValue          *value,
                                        GParamSpec      *pspec);
Matthias Clasen's avatar
Matthias Clasen committed
51 52
static GType gtk_builder_real_get_type_from_name (GtkBuilder  *builder,
                                                  const gchar *type_name);
Johan Dahlin's avatar
Johan Dahlin committed
53 54 55 56 57 58 59 60 61 62

enum {
  PROP_0,
  PROP_TRANSLATION_DOMAIN,
};

struct _GtkBuilderPrivate
{
  gchar *domain;
  GHashTable *objects;
63
  GSList *delayed_properties;
Johan Dahlin's avatar
Johan Dahlin committed
64
  GSList *signals;
65
  gchar *filename;
Johan Dahlin's avatar
Johan Dahlin committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
};

G_DEFINE_TYPE (GtkBuilder, gtk_builder, G_TYPE_OBJECT)

static void
gtk_builder_class_init (GtkBuilderClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gtk_builder_finalize;
  gobject_class->set_property = gtk_builder_set_property;
  gobject_class->get_property = gtk_builder_get_property;

  klass->get_type_from_name = gtk_builder_real_get_type_from_name;

Matthias Clasen's avatar
Matthias Clasen committed
83 84 85 86 87 88
 /** 
  * GtkBuilder:translation-domain:
  *
  * The translation domain used when translating property values that
  * have been marked as translatable in interface descriptions.
  * If the translation domain is %NULL, #GtkBuilder uses gettext(),
89
  * otherwise g_dgettext().
Matthias Clasen's avatar
Matthias Clasen committed
90 91 92
  *
  * Since: 2.12
  */
Johan Dahlin's avatar
Johan Dahlin committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  g_object_class_install_property (gobject_class,
                                   PROP_TRANSLATION_DOMAIN,
                                   g_param_spec_string ("translation-domain",
                                                        P_("Translation Domain"),
                                                        P_("The translation domain used by gettext"),
                                                        NULL,
                                                        GTK_PARAM_READWRITE));

  g_type_class_add_private (gobject_class, sizeof (GtkBuilderPrivate));
}

static void
gtk_builder_init (GtkBuilder *builder)
{
  builder->priv = G_TYPE_INSTANCE_GET_PRIVATE (builder, GTK_TYPE_BUILDER,
                                               GtkBuilderPrivate);
  builder->priv->domain = NULL;
  builder->priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal,
111
                                                  g_free, g_object_unref);
Johan Dahlin's avatar
Johan Dahlin committed
112 113 114 115 116 117 118 119 120 121
}


/*
 * GObject virtual methods
 */

static void
gtk_builder_finalize (GObject *object)
{
122
  GtkBuilderPrivate *priv = GTK_BUILDER (object)->priv;
Johan Dahlin's avatar
Johan Dahlin committed
123
  
124 125 126 127 128 129 130 131 132
  g_free (priv->domain);
  g_free (priv->filename);
  
  g_hash_table_destroy (priv->objects);

  g_slist_foreach (priv->signals, (GFunc) _free_signal_info, NULL);
  g_slist_free (priv->signals);
  
  G_OBJECT_CLASS (gtk_builder_parent_class)->finalize (object);
Johan Dahlin's avatar
Johan Dahlin committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
}

static void
gtk_builder_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GtkBuilder *builder = GTK_BUILDER (object);

  switch (prop_id)
    {
    case PROP_TRANSLATION_DOMAIN:
      gtk_builder_set_translation_domain (builder, g_value_get_string (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_builder_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GtkBuilder *builder = GTK_BUILDER (object);

  switch (prop_id)
    {
    case PROP_TRANSLATION_DOMAIN:
      g_value_set_string (value, builder->priv->domain);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


/*
 * Try to map a type name to a _get_type function
 * and call it, eg:
 *
 * GtkWindow -> gtk_window_get_type
 * GtkHBox -> gtk_hbox_get_type
 * GtkUIManager -> gtk_ui_manager_get_type
 *
 */
static GType
_gtk_builder_resolve_type_lazily (const gchar *name)
{
  static GModule *module = NULL;
  GTypeGetFunc func;
  GString *symbol_name = g_string_new ("");
  char c, *symbol;
  int i;
  GType gtype = G_TYPE_INVALID;

  if (!module)
    module = g_module_open (NULL, 0);
  
  for (i = 0; name[i] != '\0'; i++)
    {
      c = name[i];
      /* skip if uppercase, first or previous is uppercase */
200 201 202 203 204
      if ((c == g_ascii_toupper (c) &&
           i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
          (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
           name[i-1] == g_ascii_toupper (name[i-1]) &&
           name[i-2] == g_ascii_toupper (name[i-2])))
Johan Dahlin's avatar
Johan Dahlin committed
205
        g_string_append_c (symbol_name, '_');
206
      g_string_append_c (symbol_name, g_ascii_tolower (c));
Johan Dahlin's avatar
Johan Dahlin committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    }
  g_string_append (symbol_name, "_get_type");
  
  symbol = g_string_free (symbol_name, FALSE);

  if (g_module_symbol (module, symbol, (gpointer)&func))
    gtype = func ();
  
  g_free (symbol);

  return gtype;
}

/*
 * GtkBuilder virtual methods
 */

static GType
Matthias Clasen's avatar
Matthias Clasen committed
225 226
gtk_builder_real_get_type_from_name (GtkBuilder  *builder, 
                                     const gchar *type_name)
Johan Dahlin's avatar
Johan Dahlin committed
227 228 229
{
  GType gtype;

230
  gtype = g_type_from_name (type_name);
Johan Dahlin's avatar
Johan Dahlin committed
231 232 233
  if (gtype != G_TYPE_INVALID)
    return gtype;

234
  return _gtk_builder_resolve_type_lazily (type_name);
Johan Dahlin's avatar
Johan Dahlin committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
}

typedef struct
{
  gchar *object;
  gchar *name;
  gchar *value;
} DelayedProperty;

static void
gtk_builder_get_parameters (GtkBuilder  *builder,
                            GType        object_type,
                            const gchar *object_name,
                            GSList      *properties,
                            GArray      **parameters,
                            GArray      **construct_parameters)
{
  GSList *l;
  GParamSpec *pspec;
  GObjectClass *oclass;
  DelayedProperty *property;
256 257
  GError *error = NULL;
  
Johan Dahlin's avatar
Johan Dahlin committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  oclass = g_type_class_ref (object_type);
  g_assert (oclass != NULL);

  *parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));
  *construct_parameters = g_array_new (FALSE, FALSE, sizeof (GParameter));

  for (l = properties; l; l = l->next)
    {
      PropertyInfo *prop = (PropertyInfo*)l->data;
      GParameter parameter = { NULL };

      pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
                                            prop->name);
      if (!pspec)
        {
273
          g_warning ("Unknown property: %s.%s",
Johan Dahlin's avatar
Johan Dahlin committed
274 275 276 277 278 279
                     g_type_name (object_type), prop->name);
          continue;
        }

      parameter.name = prop->name;

Matthias Clasen's avatar
Matthias Clasen committed
280 281
      if (G_IS_PARAM_SPEC_OBJECT (pspec) &&
          (G_PARAM_SPEC_VALUE_TYPE (pspec) != GDK_TYPE_PIXBUF))
Johan Dahlin's avatar
Johan Dahlin committed
282 283 284 285 286 287 288
        {
          if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
            {
              GObject *object;
              object = gtk_builder_get_object (builder, prop->data);
              if (!object)
                {
Matthias Clasen's avatar
Matthias Clasen committed
289 290
                  g_warning ("Failed to get constuct only property "
                             "%s of %s with value `%s'",
Johan Dahlin's avatar
Johan Dahlin committed
291 292 293 294
                             prop->name, object_name, prop->data);
                  continue;
                }
              g_value_init (&parameter.value, G_OBJECT_TYPE (object));
295
              g_value_set_object (&parameter.value, object);
Johan Dahlin's avatar
Johan Dahlin committed
296 297 298 299 300 301 302
            }
          else
            {
              property = g_slice_new (DelayedProperty);
              property->object = g_strdup (object_name);
              property->name = g_strdup (prop->name);
              property->value = g_strdup (prop->data);
303 304 305
              builder->priv->delayed_properties =
                g_slist_prepend (builder->priv->delayed_properties, property);

Johan Dahlin's avatar
Johan Dahlin committed
306 307 308
              continue;
            }
        }
309 310
      else if (!gtk_builder_value_from_string (builder, pspec,
					       prop->data, &parameter.value, &error))
Johan Dahlin's avatar
Johan Dahlin committed
311
        {
Matthias Clasen's avatar
Matthias Clasen committed
312
          g_warning ("Failed to set property %s.%s to %s: %s",
313 314 315 316
                     g_type_name (object_type), prop->name, prop->data,
		     error->message);
	  g_error_free (error);
	  error = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
          continue;
        }

      if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
        g_array_append_val (*construct_parameters, parameter);
      else
        g_array_append_val (*parameters, parameter);
    }

  g_type_class_unref (oclass);
}

static GObject *
gtk_builder_get_internal_child (GtkBuilder  *builder,
                                ObjectInfo  *info,
332 333
                                const gchar *childname,
				GError      **error)
Johan Dahlin's avatar
Johan Dahlin committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
{
  GObject *obj = NULL;

  while (!obj)
    {
      if (!info->parent)
        break;

      info = (ObjectInfo*)((ChildInfo*)info->parent)->parent;
      if (!info)
        break;

      GTK_NOTE (BUILDER,
                g_print ("Trying to get internal child %s from %s\n",
                         childname,
                         gtk_buildable_get_name (GTK_BUILDABLE (info->object))));

      if (GTK_IS_BUILDABLE (info->object))
          obj = gtk_buildable_get_internal_child (GTK_BUILDABLE (info->object),
                                                  builder,
                                                  childname);
    };

  if (!obj)
358 359 360 361 362 363
    {
      g_set_error (error,
		   GTK_BUILDER_ERROR,
		   GTK_BUILDER_ERROR_INVALID_VALUE,
		   "Unknown internal child: %s", childname);
    }
Johan Dahlin's avatar
Johan Dahlin committed
364 365 366 367 368
  return obj;
}

GObject *
_gtk_builder_construct (GtkBuilder *builder,
369 370
                        ObjectInfo *info,
			GError **error)
Johan Dahlin's avatar
Johan Dahlin committed
371 372 373 374 375 376 377 378 379 380 381 382
{
  GArray *parameters, *construct_parameters;
  GType object_type;
  GObject *obj;
  int i;
  GtkBuildableIface *iface;
  gboolean custom_set_property;
  GtkBuildable *buildable;

  g_assert (info->class_name != NULL);
  object_type = gtk_builder_get_type_from_name (builder, info->class_name);
  if (object_type == G_TYPE_INVALID)
383 384 385 386 387 388 389 390
    {
      g_set_error (error,
		   GTK_BUILDER_ERROR,
		   GTK_BUILDER_ERROR_INVALID_VALUE,
		   "Invalid object type `%s'",
		   info->class_name);
      return NULL;
    }
Johan Dahlin's avatar
Johan Dahlin committed
391 392 393 394 395 396 397 398 399 400 401 402 403

  gtk_builder_get_parameters (builder, object_type,
                              info->id,
                              info->properties,
                              &parameters,
                              &construct_parameters);

  if (info->constructor)
    {
      GObject *constructor;

      constructor = gtk_builder_get_object (builder, info->constructor);
      if (constructor == NULL)
404 405 406 407 408 409 410 411 412 413 414
	{
	  g_set_error (error,
		       GTK_BUILDER_ERROR,
		       GTK_BUILDER_ERROR_INVALID_VALUE,
		       "Unknown object constructor for %s: %s",
		       info->id,
		       info->constructor);
	  g_array_free (parameters, TRUE);
	  g_array_free (construct_parameters, TRUE);
	  return NULL;
	}
Johan Dahlin's avatar
Johan Dahlin committed
415 416 417 418 419 420 421 422 423 424
      obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
                                           builder,
                                           info->id);
      g_assert (obj != NULL);
      if (construct_parameters->len)
        g_warning ("Can't pass in construct-only parameters to %s", info->id);
    }
  else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
    {
      gchar *childname = ((ChildInfo*)info->parent)->internal_child;
425 426 427 428 429 430 431
      obj = gtk_builder_get_internal_child (builder, info, childname, error);
      if (!obj)
	{
	  g_array_free (parameters, TRUE);
	  g_array_free (construct_parameters, TRUE);
	  return NULL;
	}
Johan Dahlin's avatar
Johan Dahlin committed
432 433
      if (construct_parameters->len)
        g_warning ("Can't pass in construct-only parameters to %s", childname);
434
      g_object_ref (obj);
Johan Dahlin's avatar
Johan Dahlin committed
435 436 437 438 439 440 441
    }
  else
    {
      obj = g_object_newv (object_type,
                           construct_parameters->len,
                           (GParameter *)construct_parameters->data);

442 443 444 445 446 447 448 449 450 451 452 453
      /* No matter what, make sure we have a reference.
       *
       * If it's an initially unowned object, sink it.
       * If it's not initially unowned then we have the reference already.
       *
       * In the case that this is a window it will be sunk already and
       * this is effectively a call to g_object_ref().  That's what
       * we want.
       */
      if (G_IS_INITIALLY_UNOWNED (obj))
        g_object_ref_sink (obj);

Johan Dahlin's avatar
Johan Dahlin committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      GTK_NOTE (BUILDER,
                g_print ("created %s of type %s\n", info->id, info->class_name));

      for (i = 0; i < construct_parameters->len; i++)
        {
          GParameter *param = &g_array_index (construct_parameters,
                                              GParameter, i);
          g_value_unset (&param->value);
        }
    }
  g_array_free (construct_parameters, TRUE);

  custom_set_property = FALSE;
  buildable = NULL;
  iface = NULL;
  if (GTK_IS_BUILDABLE (obj))
    {
      buildable = GTK_BUILDABLE (obj);
      iface = GTK_BUILDABLE_GET_IFACE (obj);
473
      if (iface->set_buildable_property)
Johan Dahlin's avatar
Johan Dahlin committed
474 475 476 477 478 479 480
        custom_set_property = TRUE;
    }

  for (i = 0; i < parameters->len; i++)
    {
      GParameter *param = &g_array_index (parameters, GParameter, i);
      if (custom_set_property)
481
        iface->set_buildable_property (buildable, builder, param->name, &param->value);
Johan Dahlin's avatar
Johan Dahlin committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      else
        g_object_set_property (obj, param->name, &param->value);

#if G_ENABLE_DEBUG
      if (gtk_debug_flags & GTK_DEBUG_BUILDER)
        {
          gchar *str = g_strdup_value_contents ((const GValue*)&param->value);
          g_print ("set %s: %s = %s\n", info->id, param->name, str);
          g_free (str);
        }
#endif      
      g_value_unset (&param->value);
    }
  g_array_free (parameters, TRUE);
  
  if (GTK_IS_BUILDABLE (obj))
    gtk_buildable_set_name (buildable, info->id);
  else
    g_object_set_data_full (obj,
                            "gtk-builder-name",
                            g_strdup (info->id),
                            g_free);

505
  /* we already own a reference to obj.  put it in the hash table. */
Johan Dahlin's avatar
Johan Dahlin committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  g_hash_table_insert (builder->priv->objects, g_strdup (info->id), obj);
  
  return obj;
}


void
_gtk_builder_add (GtkBuilder *builder,
                  ChildInfo  *child_info)
{
  GObject *object;
  GObject *parent;

  /* Internal children are already added
   * Also prevent us from being called twice.
   */
  if (!child_info ||
      child_info->internal_child ||
      child_info->added)
    return;

  object = child_info->object;
  if (!object)
    return;

  if (!child_info->parent)
    {
533
      g_warning ("%s: Not adding, No parent",
Johan Dahlin's avatar
Johan Dahlin committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547
                 gtk_buildable_get_name (GTK_BUILDABLE (object)));
      return;
    }

  g_assert (object != NULL);

  parent = ((ObjectInfo*)child_info->parent)->object;
  g_assert (GTK_IS_BUILDABLE (parent));

  GTK_NOTE (BUILDER,
            g_print ("adding %s to %s\n",
                     gtk_buildable_get_name (GTK_BUILDABLE (object)),
                     gtk_buildable_get_name (GTK_BUILDABLE (parent))));
  
548 549
  gtk_buildable_add_child (GTK_BUILDABLE (parent), builder, object,
			   child_info->type);
Johan Dahlin's avatar
Johan Dahlin committed
550 551 552 553

  child_info->added = TRUE;
}

554 555 556 557 558 559 560 561
void
_gtk_builder_add_signals (GtkBuilder *builder,
			  GSList     *signals)
{
  builder->priv->signals = g_slist_concat (builder->priv->signals,
                                           g_slist_copy (signals));
}

Johan Dahlin's avatar
Johan Dahlin committed
562
static void
563
gtk_builder_apply_delayed_properties (GtkBuilder *builder)
Johan Dahlin's avatar
Johan Dahlin committed
564
{
565
  GSList *l, *props;
Johan Dahlin's avatar
Johan Dahlin committed
566 567 568 569 570 571
  DelayedProperty *property;
  GObject *object;
  GType object_type;
  GObjectClass *oclass;
  GParamSpec *pspec;

572 573 574 575 576 577 578 579
  /* take the list over from the builder->priv.
   *
   * g_slist_reverse does not copy the list, so the list now
   * belongs to us (and we free it at the end of this function).
   */
  props = g_slist_reverse (builder->priv->delayed_properties);
  builder->priv->delayed_properties = NULL;

Johan Dahlin's avatar
Johan Dahlin committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  for (l = props; l; l = l->next)
    {
      property = (DelayedProperty*)l->data;
      object = g_hash_table_lookup (builder->priv->objects, property->object);
      g_assert (object != NULL);

      object_type = G_OBJECT_TYPE (object);
      g_assert (object_type != G_TYPE_INVALID);

      oclass = g_type_class_ref (object_type);
      g_assert (oclass != NULL);

      pspec = g_object_class_find_property (G_OBJECT_CLASS (oclass),
                                            property->name);
      if (!pspec)
595
        g_warning ("Unknown property: %s.%s", g_type_name (object_type),
Johan Dahlin's avatar
Johan Dahlin committed
596 597 598 599 600 601 602
                   property->name);
      else
        {
          GObject *obj;

          obj = g_hash_table_lookup (builder->priv->objects, property->value);
          if (!obj)
603
            g_warning ("No object called: %s", property->value);
Johan Dahlin's avatar
Johan Dahlin committed
604 605 606 607 608 609 610 611 612 613 614 615 616
          else
            g_object_set (object, property->name, obj, NULL);
        }
      g_free (property->value);
      g_free (property->object);
      g_free (property->name);
      g_slice_free (DelayedProperty, property);
      g_type_class_unref (oclass);
    }
  g_slist_free (props);
}

void
617
_gtk_builder_finish (GtkBuilder *builder)
Johan Dahlin's avatar
Johan Dahlin committed
618
{
619
  gtk_builder_apply_delayed_properties (builder);
Johan Dahlin's avatar
Johan Dahlin committed
620 621 622 623 624 625 626
}

/**
 * gtk_builder_new:
 *
 * Creates a new builder object.
 *
Matthias Clasen's avatar
Matthias Clasen committed
627
 * Return value: a new #GtkBuilder object
Johan Dahlin's avatar
Johan Dahlin committed
628 629 630 631 632 633 634 635 636 637 638 639 640
 *
 * Since: 2.12
 **/
GtkBuilder *
gtk_builder_new (void)
{
  return g_object_new (GTK_TYPE_BUILDER, NULL);
}

/**
 * gtk_builder_add_from_file:
 * @builder: a #GtkBuilder
 * @filename: the name of the file to parse
Matthias Clasen's avatar
Matthias Clasen committed
641
 * @error: return location for an error, or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
642
 *
Matthias Clasen's avatar
Matthias Clasen committed
643
 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
Matthias Clasen's avatar
Matthias Clasen committed
644
 * UI definition</link> and merges it with the current contents of @builder. 
Johan Dahlin's avatar
Johan Dahlin committed
645 646 647 648 649 650 651 652 653 654
 * 
 * Returns: A positive value on success, 0 if an error occurred
 *
 * Since: 2.12
 **/
guint
gtk_builder_add_from_file (GtkBuilder   *builder,
                           const gchar  *filename,
                           GError      **error)
{
Matthias Clasen's avatar
Matthias Clasen committed
655
  gchar *buffer;
656
  gsize length;
Johan Dahlin's avatar
Johan Dahlin committed
657 658 659 660
  GError *tmp_error;

  g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
  g_return_val_if_fail (filename != NULL, 0);
661
  g_return_val_if_fail (error == NULL || *error == NULL, 0);
Johan Dahlin's avatar
Johan Dahlin committed
662 663 664 665 666 667 668 669 670

  tmp_error = NULL;

  if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }
  
671
  g_free (builder->priv->filename);
672
  builder->priv->filename = g_strdup (filename);
Matthias Clasen's avatar
Matthias Clasen committed
673

Johan Dahlin's avatar
Johan Dahlin committed
674 675
  _gtk_builder_parser_parse_buffer (builder, filename,
                                    buffer, length,
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
                                    NULL,
                                    &tmp_error);

  g_free (buffer);

  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }

  return 1;
}

/**
 * gtk_builder_add_objects_from_file:
 * @builder: a #GtkBuilder
 * @filename: the name of the file to parse
 * @object_ids: nul-terminated array of objects to build
 * @error: return location for an error, or %NULL
 *
 * Parses a file containing a <link linkend="BUILDER-UI">GtkBuilder 
 * UI definition</link> building only the requested objects and merges
 * them with the current contents of @builder. 
 *
 * <note><para>
 * If you are adding an object that depends on an object that is not 
 * its child (for instance a #GtkTreeView that depends on its
 * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
 * </para></note>
 *
 * Returns: A positive value on success, 0 if an error occurred
 *
 * Since: 2.14
 **/
guint
gtk_builder_add_objects_from_file (GtkBuilder   *builder,
                                   const gchar  *filename,
                                   gchar       **object_ids,
                                   GError      **error)
{
  gchar *buffer;
  gsize length;
  GError *tmp_error;

  g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
  g_return_val_if_fail (filename != NULL, 0);
  g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
  g_return_val_if_fail (error == NULL || *error == NULL, 0);

  tmp_error = NULL;

  if (!g_file_get_contents (filename, &buffer, &length, &tmp_error))
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }
  
  g_free (builder->priv->filename);
  builder->priv->filename = g_strdup (filename);

  _gtk_builder_parser_parse_buffer (builder, filename,
                                    buffer, length,
                                    object_ids,
Johan Dahlin's avatar
Johan Dahlin committed
740 741
                                    &tmp_error);

742 743
  g_free (buffer);

Johan Dahlin's avatar
Johan Dahlin committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }

  return 1;
}

/**
 * gtk_builder_add_from_string:
 * @builder: a #GtkBuilder
 * @buffer: the string to parse
 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
Matthias Clasen's avatar
Matthias Clasen committed
758
 * @error: return location for an error, or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
759
 *
Matthias Clasen's avatar
Matthias Clasen committed
760 761
 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
 * UI definition</link> and merges it with the current contents of @builder. 
762
 *
Johan Dahlin's avatar
Johan Dahlin committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776
 * Returns: A positive value on success, 0 if an error occurred
 *
 * Since: 2.12
 **/
guint
gtk_builder_add_from_string (GtkBuilder   *builder,
                             const gchar  *buffer,
                             gsize         length,
                             GError      **error)
{
  GError *tmp_error;

  g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
  g_return_val_if_fail (buffer != NULL, 0);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
  g_return_val_if_fail (error == NULL || *error == NULL, 0);

  tmp_error = NULL;

  g_free (builder->priv->filename);
  builder->priv->filename = g_strdup (".");

  _gtk_builder_parser_parse_buffer (builder, "<input>",
                                    buffer, length,
                                    NULL,
                                    &tmp_error);
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }

  return 1;
}

/**
 * gtk_builder_add_objects_from_string:
 * @builder: a #GtkBuilder
 * @buffer: the string to parse
 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
 * @object_ids: nul-terminated array of objects to build
 * @error: return location for an error, or %NULL
 *
 * Parses a string containing a <link linkend="BUILDER-UI">GtkBuilder 
 * UI definition</link> building only the requested objects and merges
 * them with the current contents of @builder. 
 * 
 * <note><para>
 * If you are adding an object that depends on an object that is not 
 * its child (for instance a #GtkTreeView that depends on its
 * #GtkTreeModel), you have to explicitely list all of them in @object_ids. 
 * </para></note>
 *
 * Returns: A positive value on success, 0 if an error occurred
 *
 * Since: 2.14
 **/
guint
gtk_builder_add_objects_from_string (GtkBuilder   *builder,
                                     const gchar  *buffer,
                                     gsize         length,
                                     gchar       **object_ids,
                                     GError      **error)
{
  GError *tmp_error;

  g_return_val_if_fail (GTK_IS_BUILDER (builder), 0);
  g_return_val_if_fail (buffer != NULL, 0);
  g_return_val_if_fail (object_ids != NULL && object_ids[0] != NULL, 0);
  g_return_val_if_fail (error == NULL || *error == NULL, 0);
Johan Dahlin's avatar
Johan Dahlin committed
832 833 834

  tmp_error = NULL;

835
  g_free (builder->priv->filename);
Matthias Clasen's avatar
Matthias Clasen committed
836
  builder->priv->filename = g_strdup (".");
Matthias Clasen's avatar
Matthias Clasen committed
837

Johan Dahlin's avatar
Johan Dahlin committed
838 839
  _gtk_builder_parser_parse_buffer (builder, "<input>",
                                    buffer, length,
840
                                    object_ids,
Johan Dahlin's avatar
Johan Dahlin committed
841
                                    &tmp_error);
842

Johan Dahlin's avatar
Johan Dahlin committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
      return 0;
    }

  return 1;
}

/**
 * gtk_builder_get_object:
 * @builder: a #GtkBuilder
 * @name: name of object to get
 *
Matthias Clasen's avatar
Matthias Clasen committed
857 858
 * Gets the object named @name. Note that this function does not
 * increment the reference count of the returned object. 
Matthias Clasen's avatar
Matthias Clasen committed
859 860
 *
 * Return value: the object named @name or %NULL if it could not be 
Matthias Clasen's avatar
Matthias Clasen committed
861
 *    found in the object tree. 
Johan Dahlin's avatar
Johan Dahlin committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875
 *
 * Since: 2.12
 **/
GObject *
gtk_builder_get_object (GtkBuilder  *builder,
                        const gchar *name)
{
  g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return g_hash_table_lookup (builder->priv->objects, name);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
876 877 878
object_add_to_list (gchar    *object_id,
                    GObject  *object,
                    GSList  **list)
Johan Dahlin's avatar
Johan Dahlin committed
879 880 881 882 883 884 885 886
{
  *list = g_slist_prepend (*list, object);
}

/**
 * gtk_builder_get_objects:
 * @builder: a #GtkBuilder
 *
Matthias Clasen's avatar
Matthias Clasen committed
887 888 889
 * Gets all objects that have been constructed by @builder. Note that 
 * this function does not increment the reference counts of the returned
 * objects.
Matthias Clasen's avatar
Matthias Clasen committed
890
 *
Johan Dahlin's avatar
Johan Dahlin committed
891
 * Return value: a newly-allocated #GSList containing all the objects
Matthias Clasen's avatar
Matthias Clasen committed
892 893
 *   constructed by the #GtkBuilder instance. It should be freed by
 *   g_slist_free()
Johan Dahlin's avatar
Johan Dahlin committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
 *
 * Since: 2.12
 **/
GSList *
gtk_builder_get_objects (GtkBuilder *builder)
{
  GSList *objects = NULL;

  g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);

  g_hash_table_foreach (builder->priv->objects, (GHFunc)object_add_to_list, &objects);

  return g_slist_reverse (objects);
}

/**
 * gtk_builder_set_translation_domain:
 * @builder: a #GtkBuilder
 * @domain: the translation domain or %NULL
 *
Matthias Clasen's avatar
Matthias Clasen committed
914 915
 * Sets the translation domain of @builder. 
 * See #GtkBuilder:translation-domain.
Johan Dahlin's avatar
Johan Dahlin committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
 *
 * Since: 2.12
 **/
void
gtk_builder_set_translation_domain (GtkBuilder  *builder,
                                    const gchar *domain)
{
  gchar *new_domain;
    
  g_return_if_fail (GTK_IS_BUILDER (builder));

  new_domain = g_strdup (domain);
  g_free (builder->priv->domain);
  builder->priv->domain = new_domain;

  g_object_notify (G_OBJECT (builder), "translation-domain");
}

/**
 * gtk_builder_get_translation_domain:
 * @builder: a #GtkBuilder
 *
Matthias Clasen's avatar
Matthias Clasen committed
938
 * Gets the translation domain of @builder.
Matthias Clasen's avatar
Matthias Clasen committed
939 940
 *
 * Return value: the translation domain. This string is owned
Johan Dahlin's avatar
Johan Dahlin committed
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
 * by the builder object and must not be modified or freed.
 *
 * Since: 2.12
 **/
const gchar *
gtk_builder_get_translation_domain (GtkBuilder *builder)
{
  g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);

  return builder->priv->domain;
}

typedef struct {
  GModule *module;
  gpointer data;
} connect_args;

static void
gtk_builder_connect_signals_default (GtkBuilder    *builder,
				     GObject       *object,
				     const gchar   *signal_name,
				     const gchar   *handler_name,
				     GObject       *connect_object,
				     GConnectFlags  flags,
				     gpointer       user_data)
{
  GCallback func;
  connect_args *args = (connect_args*)user_data;
  
  if (!g_module_symbol (args->module, handler_name, (gpointer)&func))
    {
Matthias Clasen's avatar
Matthias Clasen committed
972
      g_warning ("Could not find signal handler '%s'", handler_name);
Johan Dahlin's avatar
Johan Dahlin committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
      return;
    }

  if (connect_object)
    g_signal_connect_object (object, signal_name, func, connect_object, flags);
  else
    g_signal_connect_data (object, signal_name, func, args->data, NULL, flags);
}


/**
 * gtk_builder_connect_signals:
 * @builder: a #GtkBuilder
 * @user_data: a pointer to a structure sent in as user data to all signals
 *
 * This method is a simpler variation of gtk_builder_connect_signals_full().
Matthias Clasen's avatar
Matthias Clasen committed
989 990
 * It uses #GModule's introspective features (by opening the module %NULL) 
 * to look at the application's symbol table. From here it tries to match
Johan Dahlin's avatar
Johan Dahlin committed
991 992 993 994 995 996
 * the signal handler names given in the interface description with
 * symbols in the application and connects the signals.
 * 
 * Note that this function will not work correctly if #GModule is not
 * supported on the platform.
 *
997 998 999 1000 1001 1002
 * When compiling applications for Windows, you must declare signal callbacks
 * with #G_MODULE_EXPORT, or they will not be put in the symbol table.
 * On Linux and Unices, this is not necessary; applications should instead
 * be compiled with the -Wl,--export-dynamic CFLAGS, and linked against
 * gmodule-export-2.0.
 *
Johan Dahlin's avatar
Johan Dahlin committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
 * Since: 2.12
 **/
void
gtk_builder_connect_signals (GtkBuilder *builder,
			     gpointer    user_data)
{
  connect_args *args;
  
  g_return_if_fail (GTK_IS_BUILDER (builder));
  
  if (!g_module_supported ())
Matthias Clasen's avatar
Matthias Clasen committed
1014
    g_error ("gtk_builder_connect_signals() requires working GModule");
Johan Dahlin's avatar
Johan Dahlin committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

  args = g_slice_new0 (connect_args);
  args->module = g_module_open (NULL, G_MODULE_BIND_LAZY);
  args->data = user_data;
  
  gtk_builder_connect_signals_full (builder,
                                    gtk_builder_connect_signals_default,
                                    args);
  g_module_close (args->module);

  g_slice_free (connect_args, args);
}

/**
 * GtkBuilderConnectFunc:
 * @builder: a #GtkBuilder
Matthias Clasen's avatar
Matthias Clasen committed
1031
 * @object: object to connect a signal to
Johan Dahlin's avatar
Johan Dahlin committed
1032 1033
 * @signal_name: name of the signal
 * @handler_name: name of the handler
Matthias Clasen's avatar
Matthias Clasen committed
1034
 * @connect_object: a #GObject, if non-%NULL, use g_signal_connect_object()
Johan Dahlin's avatar
Johan Dahlin committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
 * @flags: #GConnectFlags to use
 * @user_data: user data
 *
 * This is the signature of a function used to connect signals.  It is used
 * by the gtk_builder_connect_signals() and gtk_builder_connect_signals_full()
 * methods.  It is mainly intended for interpreted language bindings, but
 * could be useful where the programmer wants more control over the signal
 * connection process.
 *
 * Since: 2.12
 */

/**
 * gtk_builder_connect_signals_full:
 * @builder: a #GtkBuilder
Matthias Clasen's avatar
Matthias Clasen committed
1050 1051
 * @func: the function used to connect the signals
 * @user_data: arbitrary data that will be passed to the connection function
Johan Dahlin's avatar
Johan Dahlin committed
1052 1053
 *
 * This function can be thought of the interpreted language binding
Matthias Clasen's avatar
Matthias Clasen committed
1054 1055
 * version of gtk_builder_connect_signals(), except that it does not
 * require GModule to function correctly.
Johan Dahlin's avatar
Johan Dahlin committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
 *
 * Since: 2.12
 */
void
gtk_builder_connect_signals_full (GtkBuilder            *builder,
                                  GtkBuilderConnectFunc  func,
                                  gpointer               user_data)
{
  GSList *l;
  GObject *object;
  GObject *connect_object;
  
  g_return_if_fail (GTK_IS_BUILDER (builder));
  g_return_if_fail (func != NULL);
  
  if (!builder->priv->signals)
    return;

  builder->priv->signals = g_slist_reverse (builder->priv->signals);
  for (l = builder->priv->signals; l; l = l->next)
    {
      SignalInfo *signal = (SignalInfo*)l->data;

      g_assert (signal != NULL);
      g_assert (signal->name != NULL);

      object = g_hash_table_lookup (builder->priv->objects,
				    signal->object_name);
      g_assert (object != NULL);

      connect_object = NULL;
      
      if (signal->connect_object_name)
	{
	  connect_object = g_hash_table_lookup (builder->priv->objects,
						signal->connect_object_name);
	  if (!connect_object)
Matthias Clasen's avatar
Matthias Clasen committed
1093
	      g_warning ("Could not lookup object %s on signal %s of object %s",
Johan Dahlin's avatar
Johan Dahlin committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
			 signal->connect_object_name, signal->name,
			 signal->object_name);
	}
						  
      func (builder, object, signal->name, signal->handler, 
	    connect_object, signal->flags, user_data);
    }

  g_slist_foreach (builder->priv->signals, (GFunc)_free_signal_info, NULL);
  g_slist_free (builder->priv->signals);
  builder->priv->signals = NULL;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
1108
 * gtk_builder_value_from_string:
1109
 * @builder: a #GtkBuilder
Matthias Clasen's avatar
Matthias Clasen committed
1110 1111 1112 1113
 * @pspec: the #GParamSpec for the property
 * @string: the string representation of the value
 * @value: the #GValue to store the result in
 * @error: return location for an error, or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
1114
 *
Matthias Clasen's avatar
Matthias Clasen committed
1115
 * This function demarshals a value from a string. This function
Johan Dahlin's avatar
Johan Dahlin committed
1116 1117 1118 1119
 * calls g_value_init() on the @value argument, so it need not be
 * initialised beforehand.
 *
 * This function can handle char, uchar, boolean, int, uint, long,
Matthias Clasen's avatar
Matthias Clasen committed
1120 1121
 * ulong, enum, flags, float, double, string, #GdkColor and
 * #GtkAdjustment type values. Support for #GtkWidget type values is
Johan Dahlin's avatar
Johan Dahlin committed
1122 1123
 * still to come.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1124
 * Returns: %TRUE on success
Johan Dahlin's avatar
Johan Dahlin committed
1125 1126 1127 1128
 *
 * Since: 2.12
 */
gboolean
1129 1130 1131 1132 1133
gtk_builder_value_from_string (GtkBuilder   *builder,
			       GParamSpec   *pspec,
                               const gchar  *string,
                               GValue       *value,
			       GError      **error)
Johan Dahlin's avatar
Johan Dahlin committed
1134
{
1135 1136 1137 1138 1139 1140
  g_return_val_if_fail (GTK_IS_BUILDER (builder), FALSE);
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
  g_return_val_if_fail (string != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

Johan Dahlin's avatar
Johan Dahlin committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  /*
   * GParamSpecUnichar has the internal type G_TYPE_UINT,
   * so we cannot handle this in the switch, do it separately
   */
  if (G_IS_PARAM_SPEC_UNICHAR (pspec))
    {
      gunichar c;
      g_value_init (value, G_TYPE_UINT);
      c = g_utf8_get_char_validated (string, strlen (string));
      if (c > 0)
        g_value_set_uint (value, c);
      return TRUE;
    }

1155 1156 1157
  return gtk_builder_value_from_string_type (builder,
					     G_PARAM_SPEC_VALUE_TYPE (pspec),
                                             string, value, error);
Johan Dahlin's avatar
Johan Dahlin committed
1158 1159 1160
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
1161
 * gtk_builder_value_from_string_type:
1162
 * @builder: a #GtkBuilder
Matthias Clasen's avatar
Matthias Clasen committed
1163 1164 1165 1166
 * @type: the #GType of the value
 * @string: the string representation of the value
 * @value: the #GValue to store the result in
 * @error: return location for an error, or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
1167
 *
Matthias Clasen's avatar
Matthias Clasen committed
1168 1169 1170 1171
 * Like gtk_builder_value_from_string(), this function demarshals 
 * a value from a string, but takes a #GType instead of #GParamSpec.
 * This function calls g_value_init() on the @value argument, so it 
 * need not be initialised beforehand.
Johan Dahlin's avatar
Johan Dahlin committed
1172
 *
Matthias Clasen's avatar
Matthias Clasen committed
1173
 * Returns: %TRUE on success
Johan Dahlin's avatar
Johan Dahlin committed
1174 1175 1176 1177
 *
 * Since: 2.12
 */
gboolean
1178 1179 1180 1181 1182
gtk_builder_value_from_string_type (GtkBuilder   *builder,
				    GType         type,
                                    const gchar  *string,
                                    GValue       *value,
				    GError      **error)
Johan Dahlin's avatar
Johan Dahlin committed
1183 1184 1185 1186 1187
{
  gboolean ret = TRUE;

  g_return_val_if_fail (type != G_TYPE_INVALID, FALSE);
  g_return_val_if_fail (string != NULL, FALSE);
1188
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Johan Dahlin's avatar
Johan Dahlin committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203

  g_value_init (value, type);

  switch (G_TYPE_FUNDAMENTAL (type))
    {
    case G_TYPE_CHAR:
      g_value_set_char (value, string[0]);
      break;
    case G_TYPE_UCHAR:
      g_value_set_uchar (value, (guchar)string[0]);
      break;
    case G_TYPE_BOOLEAN:
      {
        gboolean b;

1204
	if (!_gtk_builder_boolean_from_string (string, &b, error))
1205 1206 1207
	  {
	    ret = FALSE;
	    break;
Johan Dahlin's avatar
Johan Dahlin committed
1208 1209 1210 1211 1212 1213 1214 1215
          }
        g_value_set_boolean (value, b);
        break;
      }
    case G_TYPE_INT:
    case G_TYPE_LONG:
      {
        long l;
1216
        gchar *endptr;
Johan Dahlin's avatar
Johan Dahlin committed
1217
        errno = 0;
1218 1219 1220
        l = strtol (string, &endptr, 0);
        if (errno || endptr == string)
          {
1221 1222 1223
	    g_set_error (error,
			 GTK_BUILDER_ERROR,
			 GTK_BUILDER_ERROR_INVALID_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
1224
			 "Could not parse integer `%s'",
1225
			 string);
1226 1227 1228
            ret = FALSE;
            break;
          }
Johan Dahlin's avatar
Johan Dahlin committed
1229 1230 1231 1232 1233 1234 1235 1236 1237
        if (G_VALUE_HOLDS_INT (value))
          g_value_set_int (value, l);
        else
          g_value_set_long (value, l);
        break;
      }
    case G_TYPE_UINT:
    case G_TYPE_ULONG:
      {
1238
        gulong ul;
1239
        gchar *endptr;
Johan Dahlin's avatar
Johan Dahlin committed
1240
        errno = 0;
1241 1242
        ul = strtoul (string, &endptr, 0);
        if (errno || endptr == string)
Johan Dahlin's avatar
Johan Dahlin committed
1243
          {
1244 1245 1246
	    g_set_error (error,
			 GTK_BUILDER_ERROR,
			 GTK_BUILDER_ERROR_INVALID_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
1247
			 "Could not parse unsigned integer `%s'",
1248
			 string);
1249
            ret = FALSE;
Johan Dahlin's avatar
Johan Dahlin committed
1250 1251 1252
            break;
          }
        if (G_VALUE_HOLDS_UINT (value))
1253
          g_value_set_uint (value, ul);
Johan Dahlin's avatar
Johan Dahlin committed
1254
        else 
1255
          g_value_set_ulong (value, ul);
Johan Dahlin's avatar
Johan Dahlin committed
1256 1257 1258
        break;
      }
    case G_TYPE_ENUM:
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
      {
	gint enum_value;
	if (!_gtk_builder_enum_from_string (type, string, &enum_value, error))
	  {
	    ret = FALSE;
	    break;
          }
	g_value_set_enum (value, enum_value);
	break;
      }
Johan Dahlin's avatar
Johan Dahlin committed
1269
    case G_TYPE_FLAGS:
1270
      {
1271 1272
	guint flags_value;

1273 1274 1275 1276 1277 1278 1279 1280
	if (!_gtk_builder_flags_from_string (type, string, &flags_value, error))
	  {
	    ret = FALSE;
	    break;
          }
	g_value_set_flags (value, flags_value);
	break;
      }
Johan Dahlin's avatar
Johan Dahlin committed
1281 1282 1283
    case G_TYPE_FLOAT:
    case G_TYPE_DOUBLE:
      {
Matthias Clasen's avatar
Matthias Clasen committed
1284
        gdouble d;
1285
        gchar *endptr;
Johan Dahlin's avatar
Johan Dahlin committed
1286
        errno = 0;
1287 1288
        d = g_ascii_strtod (string, &endptr);
        if (errno || endptr == string)
Johan Dahlin's avatar
Johan Dahlin committed
1289
          {
1290 1291 1292
	    g_set_error (error,
			 GTK_BUILDER_ERROR,
			 GTK_BUILDER_ERROR_INVALID_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
1293
			 "Could not parse double `%s'",
1294
			 string);
1295
            ret = FALSE;
Johan Dahlin's avatar
Johan Dahlin committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
            break;
          }
        if (G_VALUE_HOLDS_FLOAT (value))
          g_value_set_float (value, d);
        else
          g_value_set_double (value, d);
        break;
      }
    case G_TYPE_STRING:
      g_value_set_string (value, string);
      break;
    case G_TYPE_BOXED:
      if (G_VALUE_HOLDS (value, GDK_TYPE_COLOR))
        {
          GdkColor colour = { 0, };

          if (gdk_color_parse (string, &colour) &&
              gdk_colormap_alloc_color (gtk_widget_get_default_colormap (),
                                        &colour, FALSE, TRUE))
            g_value_set_boxed (value, &colour);
          else
            {
1318 1319 1320
	      g_set_error (error,
			   GTK_BUILDER_ERROR,
			   GTK_BUILDER_ERROR_INVALID_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
1321
			   "Could not parse color `%s'",
1322
			   string);
Johan Dahlin's avatar
Johan Dahlin committed
1323 1324 1325 1326 1327
              ret = FALSE;
            }
        }
      else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
        {
Matthias Clasen's avatar
Matthias Clasen committed
1328
          gchar **vector = g_strsplit (string, "\n", 0);
Johan Dahlin's avatar
Johan Dahlin committed
1329 1330 1331 1332 1333 1334
          g_value_take_boxed (value, vector);
        }
      else
        ret = FALSE;
      break;
    case G_TYPE_OBJECT:
Matthias Clasen's avatar
Matthias Clasen committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
      if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
        {
          gchar *filename;
          GError *tmp_error = NULL;
          GdkPixbuf *pixbuf;
       
          if (gtk_builder_get_object (builder, string))
            {
              g_set_error (error,
                           GTK_BUILDER_ERROR,
                           GTK_BUILDER_ERROR_INVALID_VALUE,
                           "Could not load image '%s': "
                           " '%s' is already used as object id",
                           string, string);
              return FALSE;
            }
Johan Dahlin's avatar
Johan Dahlin committed
1351

1352
	  filename = _gtk_builder_get_absolute_filename (builder, string);
Matthias Clasen's avatar
Matthias Clasen committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
          pixbuf = gdk_pixbuf_new_from_file (filename, &tmp_error);

          if (pixbuf == NULL)
            {
              GtkIconTheme *theme;

              g_warning ("Could not load image '%s': %s", 
                         string, tmp_error->message);
              g_error_free (tmp_error);

              /* fall back to a missing image */
              theme = gtk_icon_theme_get_default ();
              pixbuf = gtk_icon_theme_load_icon (theme, 
                                                 GTK_STOCK_MISSING_IMAGE,
                                                 16,
                                                 GTK_ICON_LOOKUP_USE_BUILTIN,
                                                 NULL);
            }
 
          if (pixbuf)
            {
              g_value_set_object (value, pixbuf);
              g_object_unref (G_OBJECT (pixbuf));
            }

          g_free (filename);

          ret = TRUE;
        }
      else
Matthias Clasen's avatar
Matthias Clasen committed
1383
        ret = FALSE;
Johan Dahlin's avatar
Johan Dahlin committed
1384 1385
      break;
    default:
1386 1387 1388
      g_set_error (error,
		   GTK_BUILDER_ERROR,
		   GTK_BUILDER_ERROR_INVALID_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
1389
		   "Unsupported GType `%s'",
1390
		   g_type_name (type));
Johan Dahlin's avatar
Johan Dahlin committed
1391 1392 1393 1394 1395 1396 1397
      ret = FALSE;
      break;
    }

  return ret;
}

1398
gboolean
1399 1400 1401 1402
_gtk_builder_enum_from_string (GType         type, 
                               const gchar  *string,
			       gint         *enum_value,
			       GError      **error)
Johan Dahlin's avatar
Johan Dahlin committed
1403 1404 1405 1406
{
  GEnumClass *eclass;
  GEnumValue *ev;
  gchar *endptr;
1407
  gint value;
1408
  gboolean ret;
1409
  
1410 1411
  g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
  g_return_val_if_fail (string != NULL, FALSE);
Johan Dahlin's avatar
Johan Dahlin committed
1412
  
1413 1414
  ret = TRUE;

1415
  value = strtoul (string, &endptr, 0);
Johan Dahlin's avatar
Johan Dahlin committed
1416
  if (endptr != string) /* parsed a number */
1417 1418 1419 1420 1421 1422 1423
    *enum_value = value;
  else
    {
      eclass = g_type_class_ref (type);
      ev = g_enum_get_value_by_name (eclass, string);
      if (!ev)
	ev = g_enum_get_value_by_nick (eclass, string);
Johan Dahlin's avatar
Johan Dahlin committed
1424

1425 1426 1427 1428 1429 1430 1431 1432 1433
      if (ev)
	*enum_value = ev->value;
      else
	{
	  g_set_error (error,
		       GTK_BUILDER_ERROR,
		       GTK_BUILDER_ERROR_INVALID_VALUE,
		       "Could not parse enum: `%s'",
		       string);
1434
	  ret = FALSE;
1435 1436 1437 1438 1439
	}
      
      g_type_class_unref (eclass);
    }
  
1440
  return ret;
Johan Dahlin's avatar
Johan Dahlin committed
1441 1442
}

1443 1444 1445
gboolean
_gtk_builder_flags_from_string (GType         type, 
                                const gchar  *string,
1446
				guint        *flags_value,
1447
				GError      **error)
Johan Dahlin's avatar
Johan Dahlin committed
1448 1449 1450
{
  GFlagsClass *fclass;
  gchar *endptr, *prevptr;
1451
  guint i, j, value;
Matthias Clasen's avatar
Matthias Clasen committed
1452
  gchar *flagstr;
Johan Dahlin's avatar
Johan Dahlin committed
1453
  GFlagsValue *fv;
Matthias Clasen's avatar
Matthias Clasen committed
1454
  const gchar *flag;
Johan Dahlin's avatar
Johan Dahlin committed
1455
  gunichar ch;
1456
  gboolean eos, ret;
Johan Dahlin's avatar
Johan Dahlin committed
1457

1458 1459
  g_return_val_if_fail (G_TYPE_IS_FLAGS (type), FALSE);
  g_return_val_if_fail (string != 0, FALSE);
Johan Dahlin's avatar
Johan Dahlin committed
1460

1461 1462 1463
  ret = TRUE;
  
  value = strtoul (string, &endptr, 0);
Johan Dahlin's avatar
Johan Dahlin committed
1464
  if (endptr != string) /* parsed a number */
1465 1466
    *flags_value = value;
  else
Johan Dahlin's avatar
Johan Dahlin committed
1467
    {
1468
      fclass = g_type_class_ref (type);
Johan Dahlin's avatar
Johan Dahlin committed
1469

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
      flagstr = g_strdup (string);
      for (value = i = j = 0; ; i++)
	{
	  
	  eos = flagstr[i] == '\0';
	  
	  if (!eos && flagstr[i] != '|')
	    continue;
	  
	  flag = &flagstr[j];
	  endptr = &flagstr[i];
	  
	  if (!eos)
	    {
	      flagstr[i++] = '\0';
	      j = i;
	    }
	  
	  /* trim spaces */
	  for (;;)
	    {
	      ch = g_utf8_get_char (flag);
	      if (!g_unichar_isspace (ch))
		break;
	      flag = g_utf8_next_char (flag);
	    }
	  
	  while (endptr > flag)
	    {
	      prevptr = g_utf8_prev_char (endptr);
	      ch = g_utf8_get_char (prevptr);
	      if (!g_unichar_isspace (ch))
		break;
	      endptr = prevptr;
	    }
	  
	  if (endptr > flag)
	    {
	      *endptr = '\0';
	      fv = g_flags_get_value_by_name (fclass, flag);
	      
	      if (!fv)
		fv = g_flags_get_value_by_nick (fclass, flag);
	      
	      if (fv)
		value |= fv->value;
	      else
		{
		  g_set_error (error,
			       GTK_BUILDER_ERROR,
			       GTK_BUILDER_ERROR_INVALID_VALUE,
			       "Unknown flag: `%s'",
			       flag);
		  ret = FALSE;
		  break;
		}
	    }
	  
	  if (eos)
	    {
	      *flags_value = value;
	      break;
	    }
	}
      
      g_free (flagstr);
      
      g_type_class_unref (fclass);
Johan Dahlin's avatar
Johan Dahlin committed
1538 1539 1540 1541 1542 1543 1544 1545
    }

  return ret;
}

/**
 * gtk_builder_get_type_from_name:
 * @builder: a #GtkBuilder
Matthias Clasen's avatar
Matthias Clasen committed
1546
 * @type_name: type name to lookup
Matthias Clasen's avatar
Matthias Clasen committed
1547
 *
Matthias Clasen's avatar
Matthias Clasen committed
1548
 * Looks up a type by name, using the virtual function that 
1549 1550
 * #GtkBuilder has for that purpose. This is mainly used when
 * implementing the #GtkBuildable interface on a type.
Johan Dahlin's avatar
Johan Dahlin committed
1551
 *
Matthias Clasen's avatar
Matthias Clasen committed
1552 1553
 * Returns: the #GType found for @type_name or #G_TYPE_INVALID 
 *   if no type was found
Johan Dahlin's avatar
Johan Dahlin committed
1554
 *
1555
 * Since: 2.12
Johan Dahlin's avatar
Johan Dahlin committed
1556 1557
 */
GType
Matthias Clasen's avatar
Matthias Clasen committed
1558 1559
gtk_builder_get_type_from_name (GtkBuilder  *builder, 
                                const gchar *type_name)
Johan Dahlin's avatar
Johan Dahlin committed
1560 1561
{
  g_return_val_if_fail (GTK_IS_BUILDER (builder), G_TYPE_INVALID);
1562
  g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
Johan Dahlin's avatar
Johan Dahlin committed
1563

1564
  return GTK_BUILDER_GET_CLASS (builder)->get_type_from_name (builder, type_name);
Johan Dahlin's avatar
Johan Dahlin committed
1565 1566 1567 1568 1569 1570 1571 1572
}

GQuark
gtk_builder_error_quark (void)
{
  return g_quark_from_static_string ("gtk-builder-error-quark");
}

1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
gchar *
_gtk_builder_get_absolute_filename (GtkBuilder *builder, const gchar *string)
{
  gchar *filename;
  gchar *dirname = NULL;
  
  if (g_path_is_absolute (string))
    return g_strdup (string);

  if (builder->priv->filename &&
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
      strcmp (builder->priv->filename, ".") != 0) 
    {
      dirname = g_path_get_dirname (builder->priv->filename);

      if (strcmp (dirname, ".") == 0)
	{
	  g_free (dirname);
	  dirname = g_get_current_dir ();
	}
    }
1593 1594 1595 1596 1597 1598 1599 1600
  else
    dirname = g_get_current_dir ();
    
  filename = g_build_filename (dirname, string, NULL);
  g_free (dirname);
  
  return filename;
}
Johan Dahlin's avatar
Johan Dahlin committed
1601 1602 1603

#define __GTK_BUILDER_C__
#include "gtkaliasdef.c"