gtkcontainer.c 74.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25
#include "config.h"
26

27
#include "gtkcontainerprivate.h"
28

29
#include "gtkadjustment.h"
30
#include "gtkassistant.h"
Johan Dahlin's avatar
Johan Dahlin committed
31
#include "gtkbuildable.h"
32
#include "gtkbuilderprivate.h"
33 34
#include "gtkintl.h"
#include "gtkpopovermenu.h"
35
#include "gtkprivate.h"
36
#include "gtkmarshalers.h"
37 38
#include "gtkshortcutssection.h"
#include "gtkshortcutswindow.h"
39
#include "gtksizerequest.h"
40 41
#include "gtkstylecontextprivate.h"
#include "gtktypebuiltins.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
42
#include "gtkwidgetprivate.h"
43
#include "gtkwindow.h"
44

Benjamin Otte's avatar
Benjamin Otte committed
45
#include "a11y/gtkcontaineraccessibleprivate.h"
46

47 48 49 50 51
#include <gobject/gobjectnotifyqueue.c>
#include <gobject/gvaluecollector.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/**
 * SECTION:gtkcontainer
 * @Short_description: Base class for widgets which contain other widgets
 * @Title: GtkContainer
 *
 * A GTK+ user interface is constructed by nesting widgets inside widgets.
 * Container widgets are the inner nodes in the resulting tree of widgets:
 * they contain other widgets. So, for example, you might have a #GtkWindow
 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
 * of a textual label inside the frame, you might replace the #GtkLabel widget
 * with a #GtkImage widget.
 *
 * There are two major kinds of container widgets in GTK+. Both are subclasses
 * of the abstract GtkContainer base class.
 *
 * The first type of container widget has a single child widget and derives
69
 * from #GtkBin. These containers are decorators, which
70
 * add some kind of functionality to the child. For example, a #GtkButton makes
71 72
 * its child into a clickable button; a #GtkFrame draws a frame around its child
 * and a #GtkWindow places its child widget inside a top-level window.
73
 *
74
 * The second type of container can have more than one child; its purpose is to
75
 * manage layout. This means that these containers assign
Timm Bäder's avatar
Timm Bäder committed
76
 * sizes and positions to their children. For example, a horizontal #GtkBox arranges its
77
 * children in a horizontal row, and a #GtkGrid arranges the widgets it contains
78 79
 * in a two-dimensional grid.
 *
80 81 82 83 84 85 86 87 88 89
 * For implementations of #GtkContainer the virtual method #GtkContainerClass.forall()
 * is always required, since it's used for drawing and other internal operations
 * on the children.
 * If the #GtkContainer implementation expect to have non internal children
 * it's needed to implement both #GtkContainerClass.add() and #GtkContainerClass.remove().
 * If the GtkContainer implementation has internal children, they should be added
 * with gtk_widget_set_parent() on init() and removed with gtk_widget_unparent()
 * in the #GtkWidgetClass.destroy() implementation.
 * See more about implementing custom widgets at https://wiki.gnome.org/HowDoI/CustomWidgets
 *
90
 * # Child properties
Matthias Clasen's avatar
Matthias Clasen committed
91
 *
92
 * GtkContainer introduces child properties.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
 * These are object properties that are not specific
 * to either the container or the contained widget, but rather to their relation.
 * Typical examples of child properties are the position or pack-type of a widget
 * which is contained in a #GtkBox.
 *
 * Use gtk_container_class_install_child_property() to install child properties
 * for a container class and gtk_container_class_find_child_property() or
 * gtk_container_class_list_child_properties() to get information about existing
 * child properties.
 *
 * To set the value of a child property, use gtk_container_child_set_property(),
 * gtk_container_child_set() or gtk_container_child_set_valist().
 * To obtain the value of a child property, use
 * gtk_container_child_get_property(), gtk_container_child_get() or
 * gtk_container_child_get_valist(). To emit notification about child property
 * changes, use gtk_widget_child_notify().
Matthias Clasen's avatar
Matthias Clasen committed
109
 *
110
 * # GtkContainer as GtkBuildable
Matthias Clasen's avatar
Matthias Clasen committed
111
 *
112 113 114
 * The GtkContainer implementation of the GtkBuildable interface supports
 * a <packing> element for children, which can contain multiple <property>
 * elements that specify child properties for the child.
Matthias Clasen's avatar
Matthias Clasen committed
115
 * 
116
 * Child properties can also be marked as translatable using
117 118 119
 * the same “translatable”, “comments” and “context” attributes that are used
 * for regular properties.
 *
120
 * Containers can have a <focus-chain> element containing multiple
121 122 123 124
 * <widget> elements, one for each child that should be added to the focus
 * chain. The ”name” attribute gives the id of the widget.
 *
 * An example of these properties in UI definitions:
125
 * |[
126
 * <object class="GtkBox">
127
 *   <child>
128
 *     <object class="GtkEntry" id="entry1"/>
129 130 131 132
 *     <packing>
 *       <property name="pack-type">start</property>
 *     </packing>
 *   </child>
133 134 135 136 137 138 139
 *   <child>
 *     <object class="GtkEntry" id="entry2"/>
 *   </child>
 *   <focus-chain>
 *     <widget name="entry1"/>
 *     <widget name="entry2"/>
 *   </focus-chain>
140
 * </object>
141
 * ]|
Matthias Clasen's avatar
Matthias Clasen committed
142
 *
143 144
 */

145 146 147 148 149 150 151 152 153

struct _GtkContainerPrivate
{
  guint resize_handler;

  guint has_focus_chain    : 1;
  guint restyle_pending    : 1;
};

Elliot Lee's avatar
Elliot Lee committed
154 155 156
enum {
  ADD,
  REMOVE,
157
  CHECK_RESIZE,
158
  SET_FOCUS_CHILD,
Elliot Lee's avatar
Elliot Lee committed
159 160
  LAST_SIGNAL
};
161

Tim Janik's avatar
Tim Janik committed
162 163
#define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
#define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
164

Tim Janik's avatar
Tim Janik committed
165 166

/* --- prototypes --- */
167
static void     gtk_container_base_class_init      (GtkContainerClass *klass);
Tim Janik's avatar
Tim Janik committed
168
static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
169 170
static void     gtk_container_class_init           (GtkContainerClass *klass);
static void     gtk_container_init                 (GtkContainer      *container);
171
static void     gtk_container_destroy              (GtkWidget         *widget);
172
static void     gtk_container_add_unimplemented    (GtkContainer      *container,
Matthias Clasen's avatar
Matthias Clasen committed
173
                                                    GtkWidget         *widget);
174
static void     gtk_container_remove_unimplemented (GtkContainer      *container,
Matthias Clasen's avatar
Matthias Clasen committed
175
                                                    GtkWidget         *widget);
176
static void     gtk_container_real_check_resize    (GtkContainer      *container);
177 178 179
static void     gtk_container_compute_expand       (GtkWidget         *widget,
                                                    gboolean          *hexpand_p,
                                                    gboolean          *vexpand_p);
180
static gboolean gtk_container_focus                (GtkWidget         *widget,
Matthias Clasen's avatar
Matthias Clasen committed
181
                                                    GtkDirectionType   direction);
182
static void     gtk_container_real_set_focus_child (GtkContainer      *container,
Matthias Clasen's avatar
Matthias Clasen committed
183
                                                    GtkWidget         *widget);
184

185
static void     gtk_container_children_callback    (GtkWidget         *widget,
Matthias Clasen's avatar
Matthias Clasen committed
186
                                                    gpointer           client_data);
187
static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget   *widget);
188

189 190 191
static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
                                                              GtkWidget    *child);

Johan Dahlin's avatar
Johan Dahlin committed
192 193
/* GtkBuildable */
static void gtk_container_buildable_init           (GtkBuildableIface *iface);
194
static void gtk_container_buildable_add_child      (GtkBuildable *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
195 196 197
                                                    GtkBuilder   *builder,
                                                    GObject      *child,
                                                    const gchar  *type);
Johan Dahlin's avatar
Johan Dahlin committed
198
static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
199 200 201 202 203
                                                          GtkBuilder    *builder,
                                                          GObject       *child,
                                                          const gchar   *tagname,
                                                          GMarkupParser *parser,
                                                          gpointer      *data);
Johan Dahlin's avatar
Johan Dahlin committed
204
static void    gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
205 206 207 208
                                                       GtkBuilder   *builder,
                                                       GObject      *child,
                                                       const gchar  *tagname,
                                                       gpointer     *data);
209 210 211 212 213
static void    gtk_container_buildable_custom_finished (GtkBuildable *buildable,
                                                        GtkBuilder   *builder,
                                                        GObject      *child,
                                                        const gchar  *tagname,
                                                        gpointer      data);
Johan Dahlin's avatar
Johan Dahlin committed
214

Tim Janik's avatar
Tim Janik committed
215
/* --- variables --- */
216 217 218
static GQuark                vadjustment_key_id;
static GQuark                hadjustment_key_id;
static GQuark                quark_focus_chain;
Tim Janik's avatar
Tim Janik committed
219
static guint                 container_signals[LAST_SIGNAL] = { 0 };
220
static gint                  GtkContainer_private_offset;
221
static GtkWidgetClass       *gtk_container_parent_class = NULL;
Tim Janik's avatar
Tim Janik committed
222 223
extern GParamSpecPool       *_gtk_widget_child_property_pool;
extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
Johan Dahlin's avatar
Johan Dahlin committed
224
static GtkBuildableIface    *parent_buildable_iface;
Elliot Lee's avatar
Elliot Lee committed
225

226

Tim Janik's avatar
Tim Janik committed
227
/* --- functions --- */
228 229 230 231 232 233
static inline gpointer
gtk_container_get_instance_private (GtkContainer *self)
{
  return G_STRUCT_MEMBER_P (self, GtkContainer_private_offset);
}

Manish Singh's avatar
Manish Singh committed
234
GType
235
gtk_container_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
236
{
Manish Singh's avatar
Manish Singh committed
237
  static GType container_type = 0;
Elliot Lee's avatar
Elliot Lee committed
238 239 240

  if (!container_type)
    {
241
      const GTypeInfo container_info =
Manish Singh's avatar
Manish Singh committed
242
      {
Matthias Clasen's avatar
Matthias Clasen committed
243 244 245 246 247 248 249 250 251 252
        sizeof (GtkContainerClass),
        (GBaseInitFunc) gtk_container_base_class_init,
        (GBaseFinalizeFunc) gtk_container_base_class_finalize,
        (GClassInitFunc) gtk_container_class_init,
        NULL        /* class_finalize */,
        NULL        /* class_data */,
        sizeof (GtkContainer),
        0           /* n_preallocs */,
        (GInstanceInitFunc) gtk_container_init,
        NULL,       /* value_table */
Elliot Lee's avatar
Elliot Lee committed
253 254
      };

255
      const GInterfaceInfo buildable_info =
Johan Dahlin's avatar
Johan Dahlin committed
256
      {
Matthias Clasen's avatar
Matthias Clasen committed
257 258 259
        (GInterfaceInitFunc) gtk_container_buildable_init,
        NULL,
        NULL
Johan Dahlin's avatar
Johan Dahlin committed
260 261
      };

Manish Singh's avatar
Manish Singh committed
262
      container_type =
Matthias Clasen's avatar
Matthias Clasen committed
263 264
        g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
                                &container_info, G_TYPE_FLAG_ABSTRACT);
Johan Dahlin's avatar
Johan Dahlin committed
265

266 267 268
      GtkContainer_private_offset =
        g_type_add_instance_private (container_type, sizeof (GtkContainerPrivate));

Johan Dahlin's avatar
Johan Dahlin committed
269
      g_type_add_interface_static (container_type,
Matthias Clasen's avatar
Matthias Clasen committed
270 271
                                   GTK_TYPE_BUILDABLE,
                                   &buildable_info);
Johan Dahlin's avatar
Johan Dahlin committed
272

Elliot Lee's avatar
Elliot Lee committed
273 274 275 276 277
    }

  return container_type;
}

278 279 280 281
static void
gtk_container_base_class_init (GtkContainerClass *class)
{
  /* reset instance specifc class fields that don't get inherited */
Tim Janik's avatar
Tim Janik committed
282 283
  class->set_child_property = NULL;
  class->get_child_property = NULL;
284 285
}

Elliot Lee's avatar
Elliot Lee committed
286
static void
Tim Janik's avatar
Tim Janik committed
287
gtk_container_base_class_finalize (GtkContainerClass *class)
Elliot Lee's avatar
Elliot Lee committed
288
{
Tim Janik's avatar
Tim Janik committed
289
  GList *list, *node;
Elliot Lee's avatar
Elliot Lee committed
290

291
  list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
Tim Janik's avatar
Tim Janik committed
292 293 294
  for (node = list; node; node = node->next)
    {
      GParamSpec *pspec = node->data;
295

Tim Janik's avatar
Tim Janik committed
296 297 298 299 300 301 302 303 304 305 306 307
      g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
      PARAM_SPEC_SET_PARAM_ID (pspec, 0);
      g_param_spec_unref (pspec);
    }
  g_list_free (list);
}

static void
gtk_container_class_init (GtkContainerClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
308

309
  gtk_container_parent_class = g_type_class_peek_parent (class);
310

311 312 313
  vadjustment_key_id = g_quark_from_static_string ("gtk-vadjustment");
  hadjustment_key_id = g_quark_from_static_string ("gtk-hadjustment");
  quark_focus_chain = g_quark_from_static_string ("gtk-container-focus-chain");
314

315
  widget_class->destroy = gtk_container_destroy;
316
  widget_class->compute_expand = gtk_container_compute_expand;
Tim Janik's avatar
Tim Janik committed
317
  widget_class->focus = gtk_container_focus;
318
  widget_class->get_request_mode = gtk_container_get_request_mode;
319

Tim Janik's avatar
Tim Janik committed
320 321 322 323 324 325
  class->add = gtk_container_add_unimplemented;
  class->remove = gtk_container_remove_unimplemented;
  class->check_resize = gtk_container_real_check_resize;
  class->forall = NULL;
  class->set_focus_child = gtk_container_real_set_focus_child;
  class->child_type = NULL;
326
  class->get_path_for_child = gtk_container_real_get_path_for_child;
Tim Janik's avatar
Tim Janik committed
327

Elliot Lee's avatar
Elliot Lee committed
328
  container_signals[ADD] =
Matthias Clasen's avatar
Matthias Clasen committed
329
    g_signal_new (I_("add"),
Matthias Clasen's avatar
Matthias Clasen committed
330 331 332 333
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkContainerClass, add),
                  NULL, NULL,
334
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
335 336
                  G_TYPE_NONE, 1,
                  GTK_TYPE_WIDGET);
Elliot Lee's avatar
Elliot Lee committed
337
  container_signals[REMOVE] =
Matthias Clasen's avatar
Matthias Clasen committed
338
    g_signal_new (I_("remove"),
Matthias Clasen's avatar
Matthias Clasen committed
339 340 341 342
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkContainerClass, remove),
                  NULL, NULL,
343
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
344 345
                  G_TYPE_NONE, 1,
                  GTK_TYPE_WIDGET);
346
  container_signals[CHECK_RESIZE] =
347
    g_signal_new (I_("check-resize"),
Matthias Clasen's avatar
Matthias Clasen committed
348 349 350 351
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkContainerClass, check_resize),
                  NULL, NULL,
352
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
353
                  G_TYPE_NONE, 0);
354
  container_signals[SET_FOCUS_CHILD] =
Matthias Clasen's avatar
Matthias Clasen committed
355
    g_signal_new (I_("set-focus-child"),
Matthias Clasen's avatar
Matthias Clasen committed
356 357 358 359
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
                  NULL, NULL,
360
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
361 362
                  G_TYPE_NONE, 1,
                  GTK_TYPE_WIDGET);
363

364 365
  if (GtkContainer_private_offset != 0)
    g_type_class_adjust_private_offset (class, &GtkContainer_private_offset);
366 367

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE);
368 369
}

Johan Dahlin's avatar
Johan Dahlin committed
370 371 372 373
static void
gtk_container_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
374
  iface->add_child = gtk_container_buildable_add_child;
Johan Dahlin's avatar
Johan Dahlin committed
375 376
  iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
  iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
377
  iface->custom_finished = gtk_container_buildable_custom_finished;
Johan Dahlin's avatar
Johan Dahlin committed
378 379 380
}

static void
381
gtk_container_buildable_add_child (GtkBuildable  *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
382 383 384
                                   GtkBuilder    *builder,
                                   GObject       *child,
                                   const gchar   *type)
Johan Dahlin's avatar
Johan Dahlin committed
385
{
386 387
  if (GTK_IS_WIDGET (child) &&
      _gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
388
    {
389 390 391 392 393 394 395 396
      if (type)
        {
          GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
        }
      else
        {
          gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
        }
397
    }
398
  else
399
    {
400
      parent_buildable_iface->add_child (buildable, builder, child, type);
401
    }
Johan Dahlin's avatar
Johan Dahlin committed
402 403
}

404 405 406 407 408 409 410 411 412 413 414 415 416
static inline void
container_set_child_property (GtkContainer       *container,
                              GtkWidget          *child,
                              GParamSpec         *pspec,
                              const GValue       *value,
                              GObjectNotifyQueue *nqueue)
{
  GValue tmp_value = G_VALUE_INIT;
  GtkContainerClass *class = g_type_class_peek (pspec->owner_type);

  /* provide a copy to work from, convert (if necessary) and validate */
  g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
  if (!g_value_transform (value, &tmp_value))
417
    g_warning ("unable to set child property '%s' of type '%s' from value of type '%s'",
418 419 420 421 422 423 424
               pspec->name,
               g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
               G_VALUE_TYPE_NAME (value));
  else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
    {
      gchar *contents = g_strdup_value_contents (value);

425
      g_warning ("value \"%s\" of type '%s' is invalid for property '%s' of type '%s'",
426 427 428 429 430 431 432 433 434 435 436 437 438 439
                 contents,
                 G_VALUE_TYPE_NAME (value),
                 pspec->name,
                 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
      g_free (contents);
    }
  else
    {
      class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
      g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
    }
  g_value_unset (&tmp_value);
}

Johan Dahlin's avatar
Johan Dahlin committed
440 441
static void
gtk_container_buildable_set_child_property (GtkContainer *container,
Matthias Clasen's avatar
Matthias Clasen committed
442 443 444 445
                                            GtkBuilder   *builder,
                                            GtkWidget    *child,
                                            gchar        *name,
                                            const gchar  *value)
Johan Dahlin's avatar
Johan Dahlin committed
446 447
{
  GParamSpec *pspec;
Javier Jardón's avatar
Javier Jardón committed
448
  GValue gvalue = G_VALUE_INIT;
449
  GError *error = NULL;
450
  GObjectNotifyQueue *nqueue;
451

452
  pspec = gtk_container_class_find_child_property (G_OBJECT_GET_CLASS (container), name);
Johan Dahlin's avatar
Johan Dahlin committed
453 454
  if (!pspec)
    {
455 456 457 458 459 460
      g_warning ("%s does not have a child property called %s",
                 G_OBJECT_TYPE_NAME (container), name);
      return;
    }
  else if (!(pspec->flags & G_PARAM_WRITABLE))
    {
461
      g_warning ("Child property '%s' of container class '%s' is not writable",
462
                 name, G_OBJECT_TYPE_NAME (container));
Johan Dahlin's avatar
Johan Dahlin committed
463 464 465
      return;
    }

466
  if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
Johan Dahlin's avatar
Johan Dahlin committed
467
    {
468
      g_warning ("Could not read property %s:%s with value %s of type %s: %s",
Matthias Clasen's avatar
Matthias Clasen committed
469 470 471 472 473
                 g_type_name (G_OBJECT_TYPE (container)),
                 name,
                 value,
                 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
                 error->message);
474
      g_error_free (error);
Johan Dahlin's avatar
Johan Dahlin committed
475 476 477
      return;
    }

478 479 480 481 482 483 484
  g_object_ref (container);
  g_object_ref (child);
  nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
  container_set_child_property (container, child, pspec, &gvalue, nqueue);
  g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
  g_object_unref (container);
  g_object_unref (child);
Johan Dahlin's avatar
Johan Dahlin committed
485 486 487 488 489 490 491
  g_value_unset (&gvalue);
}

typedef struct {
  GtkBuilder   *builder;
  GtkContainer *container;
  GtkWidget    *child;
492
  GString      *string;
Johan Dahlin's avatar
Johan Dahlin committed
493
  gchar        *child_prop_name;
494
  gchar        *context;
495
  gboolean      translatable;
496
} PackingData;
Johan Dahlin's avatar
Johan Dahlin committed
497 498

static void
499 500 501 502 503 504
packing_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
505
{
506
  PackingData *data = (PackingData*)user_data;
Johan Dahlin's avatar
Johan Dahlin committed
507 508

  if (strcmp (element_name, "property") == 0)
Matthias Clasen's avatar
Matthias Clasen committed
509
    {
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
      const gchar *name;
      gboolean translatable = FALSE;
      const gchar *ctx = NULL;

      if (!_gtk_builder_check_parent (data->builder, context, "packing", error))
        return;

      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", &ctx,
                                        G_MARKUP_COLLECT_INVALID))
       {
         _gtk_builder_prefix_error (data->builder, context, error);
         return;
       }

     data->child_prop_name = g_strdup (name);
     data->translatable = translatable;
     data->context = g_strdup (ctx);
Matthias Clasen's avatar
Matthias Clasen committed
531
    }
Johan Dahlin's avatar
Johan Dahlin committed
532
  else if (strcmp (element_name, "packing") == 0)
533 534 535 536 537 538 539 540 541
    {
      if (!_gtk_builder_check_parent (data->builder, context, "child", error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_INVALID, NULL, NULL,
                                        G_MARKUP_COLLECT_INVALID))
        _gtk_builder_prefix_error (data->builder, context, error);
    }
Johan Dahlin's avatar
Johan Dahlin committed
542
  else
543 544 545 546 547
    {
      _gtk_builder_error_unhandled_tag (data->builder, context,
                                        "GtkContainer", element_name,
                                        error);
    }
Johan Dahlin's avatar
Johan Dahlin committed
548 549 550
}

static void
551 552 553 554 555
packing_text_element (GMarkupParseContext  *context,
                      const gchar          *text,
                      gsize                 text_len,
                      gpointer              user_data,
                      GError              **error)
Johan Dahlin's avatar
Johan Dahlin committed
556
{
557
  PackingData *data = (PackingData*)user_data;
Johan Dahlin's avatar
Johan Dahlin committed
558

559 560
  if (data->child_prop_name)
    g_string_append_len (data->string, text, text_len);
561
}
562

563
static void
564 565 566 567
packing_end_element (GMarkupParseContext  *context,
                     const gchar          *element_name,
                     gpointer              user_data,
                     GError              **error)
568
{
569
  PackingData *data = (PackingData*)user_data;
570

571
  /* translate the string */
572
  if (data->string->len && data->translatable)
573
    {
574
      const gchar *translated;
575
      const gchar *domain;
576

577
      domain = gtk_builder_get_translation_domain (data->builder);
578

579
      translated = _gtk_builder_parser_translate (domain,
580 581 582
                                                  data->context,
                                                  data->string->str);
      g_string_assign (data->string, translated);
583
    }
Johan Dahlin's avatar
Johan Dahlin committed
584

585 586 587 588 589 590 591 592 593 594 595
  if (data->child_prop_name)
    gtk_container_buildable_set_child_property (data->container,
                                                data->builder,
                                                data->child,
                                                data->child_prop_name,
                                                data->string->str);

  g_string_set_size (data->string, 0);
  g_clear_pointer (&data->child_prop_name, g_free);
  g_clear_pointer (&data->context, g_free);
  data->translatable = FALSE;
Johan Dahlin's avatar
Johan Dahlin committed
596 597
}

598
static const GMarkupParser packing_parser =
Johan Dahlin's avatar
Johan Dahlin committed
599
  {
600 601 602
    packing_start_element,
    packing_end_element,
    packing_text_element,
Johan Dahlin's avatar
Johan Dahlin committed
603 604
  };

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
typedef struct
  {
    gchar *name;
    gint line;
    gint col;
  } FocusChainWidget;

static void
focus_chain_widget_free (gpointer data)
{
  FocusChainWidget *fcw = data;

  g_free (fcw->name);
  g_free (fcw);
}

621 622 623 624
typedef struct
  {
    GSList *items;
    GObject *object;
625
    GtkBuilder *builder;
626 627
    gint line;
    gint col;
628 629 630
  } FocusChainData;

static void
631 632 633 634 635 636
focus_chain_start_element (GMarkupParseContext  *context,
                           const gchar          *element_name,
                           const gchar         **names,
                           const gchar         **values,
                           gpointer              user_data,
                           GError              **error)
637 638 639 640 641
{
  FocusChainData *data = (FocusChainData*)user_data;

  if (strcmp (element_name, "widget") == 0)
    {
642
      const gchar *name;
643
      FocusChainWidget *fcw;
644 645 646 647 648 649 650

      if (!_gtk_builder_check_parent (data->builder, context, "focus-chain", error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        G_MARKUP_COLLECT_INVALID))
651
        {
652 653
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
654
        }
655

656 657 658 659
      fcw = g_new (FocusChainWidget, 1);
      fcw->name = g_strdup (name);
      g_markup_parse_context_get_position (context, &fcw->line, &fcw->col);
      data->items = g_slist_prepend (data->items, fcw);
660 661 662
    }
  else if (strcmp (element_name, "focus-chain") == 0)
    {
663 664 665 666 667 668 669
      if (!_gtk_builder_check_parent (data->builder, context, "object", error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_INVALID, "", NULL,
                                        G_MARKUP_COLLECT_INVALID))
        _gtk_builder_prefix_error (data->builder, context, error);
670 671 672
    }
  else
    {
673 674 675
      _gtk_builder_error_unhandled_tag (data->builder, context,
                                        "GtkContainer", element_name,
                                        error);
676 677 678 679 680 681 682 683
    }
}

static const GMarkupParser focus_chain_parser =
  {
    focus_chain_start_element
  };

Johan Dahlin's avatar
Johan Dahlin committed
684 685
static gboolean
gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
686 687 688 689
                                          GtkBuilder    *builder,
                                          GObject       *child,
                                          const gchar   *tagname,
                                          GMarkupParser *parser,
690
                                          gpointer      *parser_data)
Johan Dahlin's avatar
Johan Dahlin committed
691 692
{
  if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
693
                                                tagname, parser, parser_data))
Johan Dahlin's avatar
Johan Dahlin committed
694 695 696 697
    return TRUE;

  if (child && strcmp (tagname, "packing") == 0)
    {
698
      PackingData *data;
699

700 701 702 703 704 705 706 707 708
      data = g_slice_new0 (PackingData);
      data->string = g_string_new ("");
      data->builder = builder;
      data->container = GTK_CONTAINER (buildable);
      data->child = GTK_WIDGET (child);
      data->child_prop_name = NULL;

      *parser = packing_parser;
      *parser_data = data;
Johan Dahlin's avatar
Johan Dahlin committed
709 710 711

      return TRUE;
    }
712 713
  else if (!child && strcmp (tagname, "focus-chain") == 0)
    {
714 715 716 717 718 719
      FocusChainData *data;

      data = g_slice_new0 (FocusChainData);
      data->items = NULL;
      data->object = G_OBJECT (buildable);
      data->builder = builder;
720 721

      *parser = focus_chain_parser;
722 723
      *parser_data = data;

724 725
      return TRUE;
    }
Johan Dahlin's avatar
Johan Dahlin committed
726 727 728 729 730 731

  return FALSE;
}

static void
gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
Matthias Clasen's avatar
Matthias Clasen committed
732 733 734
                                        GtkBuilder   *builder,
                                        GObject      *child,
                                        const gchar  *tagname,
735
                                        gpointer     *parser_data)
Johan Dahlin's avatar
Johan Dahlin committed
736 737 738
{
  if (strcmp (tagname, "packing") == 0)
    {
739 740 741 742 743
      PackingData *data = (PackingData*)parser_data;

      g_string_free (data->string, TRUE);
      g_slice_free (PackingData, data);

Johan Dahlin's avatar
Johan Dahlin committed
744 745 746 747 748
      return;
    }

  if (parent_buildable_iface->custom_tag_end)
    parent_buildable_iface->custom_tag_end (buildable, builder,
749
                                            child, tagname, parser_data);
Johan Dahlin's avatar
Johan Dahlin committed
750 751
}

752 753 754 755 756
static void
gtk_container_buildable_custom_finished (GtkBuildable *buildable,
                                         GtkBuilder   *builder,
                                         GObject      *child,
                                         const gchar  *tagname,
757
                                         gpointer      parser_data)
758 759 760
{
   if (strcmp (tagname, "focus-chain") == 0)
    {
761
      FocusChainData *data = (FocusChainData*)parser_data;
762
      FocusChainWidget *fcw;
763 764 765 766 767
      GSList *l;
      GList *chain;
      GObject *object;

      chain = NULL;
768
      for (l = data->items; l; l = l->next)
769
        {
770 771
          fcw = l->data;
          object = _gtk_builder_lookup_object (builder, fcw->name, fcw->line, fcw->col);
772
          if (!object)
773
            continue;
774 775 776
          chain = g_list_prepend (chain, object);
        }

777
      gtk_container_set_focus_chain (GTK_CONTAINER (data->object), chain);
778 779
      g_list_free (chain);

780
      g_slist_free_full (data->items, focus_chain_widget_free);
781
      g_slice_free (FocusChainData, data);
782 783 784 785 786 787

      return;
    }

  if (parent_buildable_iface->custom_finished)
    parent_buildable_iface->custom_finished (buildable, builder,
788
                                             child, tagname, parser_data);
789 790
}

Matthias Clasen's avatar
Matthias Clasen committed
791
/**
792
 * gtk_container_child_type:
793
 * @container: a #GtkContainer
Matthias Clasen's avatar
Matthias Clasen committed
794 795 796
 *
 * Returns the type of the children supported by the container.
 *
Manish Singh's avatar
Manish Singh committed
797
 * Note that this may return %G_TYPE_NONE to indicate that no more
798
 * children can be added, e.g. for a #GtkPaned which already has two
Matthias Clasen's avatar
Matthias Clasen committed
799 800
 * children.
 *
801
 * Returns: a #GType.
Matthias Clasen's avatar
Matthias Clasen committed
802
 **/
Manish Singh's avatar
Manish Singh committed
803
GType
Tim Janik's avatar
Tim Janik committed
804
gtk_container_child_type (GtkContainer *container)
805
{
Manish Singh's avatar
Manish Singh committed
806
  GType slot;
807 808 809 810
  GtkContainerClass *class;

  g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);

811
  class = GTK_CONTAINER_GET_CLASS (container);
812 813
  if (class->child_type)
    slot = class->child_type (container);
814
  else
Manish Singh's avatar
Manish Singh committed
815
    slot = G_TYPE_NONE;
816 817 818 819

  return slot;
}

Tim Janik's avatar
Tim Janik committed
820
/* --- GtkContainer child property mechanism --- */
821 822 823 824

/**
 * gtk_container_child_notify:
 * @container: the #GtkContainer
825 826
 * @child: the child widget
 * @child_property: the name of a child property installed on
827 828 829
 *     the class of @container
 *
 * Emits a #GtkWidget::child-notify signal for the
830
 * [child property][child-properties]
831
 * @child_property on the child.
832 833 834 835 836 837 838
 *
 * This is an analogue of g_object_notify() for child properties.
 *
 * Also see gtk_widget_child_notify().
 */
void
gtk_container_child_notify (GtkContainer *container,
839
                            GtkWidget    *child,
840 841 842 843 844 845
                            const gchar  *child_property)
{
  GObject *obj;
  GParamSpec *pspec;

  g_return_if_fail (GTK_IS_CONTAINER (container));
846
  g_return_if_fail (GTK_IS_WIDGET (child));
847 848
  g_return_if_fail (child_property != NULL);

849
  obj = G_OBJECT (child);
850 851 852 853 854 855 856 857 858 859 860 861 862

  if (obj->ref_count == 0)
    return;

  g_object_ref (obj);

  pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
                                    child_property,
                                    G_OBJECT_TYPE (container),
                                    TRUE);

  if (pspec == NULL)
    {
863
      g_warning ("%s: container class '%s' has no child property named '%s'",
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
                 G_STRLOC,
                 G_OBJECT_TYPE_NAME (container),
                 child_property);
    }
  else
    {
      GObjectNotifyQueue *nqueue;

      nqueue = g_object_notify_queue_freeze (obj, _gtk_widget_child_property_notify_context);

      g_object_notify_queue_add (obj, nqueue, pspec);
      g_object_notify_queue_thaw (obj, nqueue);
    }

  g_object_unref (obj);
}

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
/**
 * gtk_container_child_notify_by_pspec:
 * @container: the #GtkContainer
 * @child: the child widget
 * @pspec: the #GParamSpec of a child property instealled on
 *     the class of @container
 *
 * Emits a #GtkWidget::child-notify signal for the
 * [child property][child-properties] specified by
 * @pspec on the child.
 *
 * This is an analogue of g_object_notify_by_pspec() for child properties.
 */
void
gtk_container_child_notify_by_pspec (GtkContainer *container,
                                     GtkWidget    *child,
                                     GParamSpec   *pspec)
{
  GObject *obj = G_OBJECT (child);
  GObjectNotifyQueue *nqueue;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));

  if (obj->ref_count == 0)
    return;

  g_object_ref (obj);

  nqueue = g_object_notify_queue_freeze (obj, _gtk_widget_child_property_notify_context);

  g_object_notify_queue_add (obj, nqueue, pspec);
  g_object_notify_queue_thaw (obj, nqueue);

  g_object_unref (obj);
}

Tim Janik's avatar
Tim Janik committed
919 920
static inline void
container_get_child_property (GtkContainer *container,
Matthias Clasen's avatar
Matthias Clasen committed
921 922 923
                              GtkWidget    *child,
                              GParamSpec   *pspec,
                              GValue       *value)
Tim Janik's avatar
Tim Janik committed
924 925
{
  GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
926

Tim Janik's avatar
Tim Janik committed
927 928 929
  class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
}

Matthias Clasen's avatar
Matthias Clasen committed
930 931 932 933
/**
 * gtk_container_child_get_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
934
 * @first_property_name: the name of the first property to get
935
 * @var_args: return location for the first property, followed
936
 *     optionally by more name/return location pairs, followed by %NULL
937
 *
Matthias Clasen's avatar
Matthias Clasen committed
938 939
 * Gets the values of one or more child properties for @child and @container.
 **/
940
void
Tim Janik's avatar
Tim Janik committed
941
gtk_container_child_get_valist (GtkContainer *container,
Matthias Clasen's avatar
Matthias Clasen committed
942 943 944
                                GtkWidget    *child,
                                const gchar  *first_property_name,
                                va_list       var_args)
945
{
Tim Janik's avatar
Tim Janik committed
946 947
  const gchar *name;

948
  g_return_if_fail (GTK_IS_CONTAINER (container));
Tim Janik's avatar
Tim Janik committed
949
  g_return_if_fail (GTK_IS_WIDGET (child));
950

Tim Janik's avatar
Tim Janik committed
951 952
  g_object_ref (container);
  g_object_ref (child);
953

Tim Janik's avatar
Tim Janik committed
954 955
  name = first_property_name;
  while (name)
956
    {
Javier Jardón's avatar
Javier Jardón committed
957
      GValue value = G_VALUE_INIT;
Tim Janik's avatar
Tim Janik committed
958
      GParamSpec *pspec;
959 960
      gchar *error;

Tim Janik's avatar
Tim Janik committed
961
      pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
Matthias Clasen's avatar
Matthias Clasen committed
962 963 964
                                        name,
                                        G_OBJECT_TYPE (container),
                                        TRUE);
Tim Janik's avatar
Tim Janik committed
965
      if (!pspec)
Matthias Clasen's avatar
Matthias Clasen committed
966
        {
967
          g_warning ("%s: container class '%s' has no child property named '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
968 969 970 971 972
                     G_STRLOC,
                     G_OBJECT_TYPE_NAME (container),
                     name);
          break;
        }
Tim Janik's avatar
Tim Janik committed
973
      if (!(pspec->flags & G_PARAM_READABLE))
Matthias Clasen's avatar
Matthias Clasen committed
974
        {
975
          g_warning ("%s: child property '%s' of container class '%s' is not readable",
Matthias Clasen's avatar
Matthias Clasen committed
976 977 978 979 980
                     G_STRLOC,
                     pspec->name,
                     G_OBJECT_TYPE_NAME (container));
          break;
        }
Tim Janik's avatar
Tim Janik committed
981 982 983
      g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
      container_get_child_property (container, child, pspec, &value);
      G_VALUE_LCOPY (&value, var_args, 0, &error);
984
      if (error)
Matthias Clasen's avatar
Matthias Clasen committed
985 986 987 988 989 990
        {
          g_warning ("%s: %s", G_STRLOC, error);
          g_free (error);
          g_value_unset (&value);
          break;
        }
Tim Janik's avatar
Tim Janik committed
991 992 993 994 995 996 997 998
      g_value_unset (&value);
      name = va_arg (var_args, gchar*);
    }

  g_object_unref (child);
  g_object_unref (container);
}

Matthias Clasen's avatar
Matthias Clasen committed
999 1000 1001 1002 1003 1004
/**
 * gtk_container_child_get_property:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
 * @property_name: the name of the property to get
 * @value: a location to return the value
1005
 *
Matthias Clasen's avatar
Matthias Clasen committed
1006 1007
 * Gets the value of a child property for @child and @container.
 **/
1008 1009
void
gtk_container_child_get_property (GtkContainer *container,
Matthias Clasen's avatar
Matthias Clasen committed
1010 1011 1012
                                  GtkWidget    *child,
                                  const gchar  *property_name,
                                  GValue       *value)
1013 1014 1015 1016 1017 1018 1019
{
  GParamSpec *pspec;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (G_IS_VALUE (value));
1020

1021 1022 1023
  g_object_ref (container);
  g_object_ref (child);
  pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
Matthias Clasen's avatar
Matthias Clasen committed
1024
                                    G_OBJECT_TYPE (container), TRUE);
1025
  if (!pspec)
1026
    g_warning ("%s: container class '%s' has no child property named '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
1027 1028 1029
               G_STRLOC,
               G_OBJECT_TYPE_NAME (container),
               property_name);
1030
  else if (!(pspec->flags & G_PARAM_READABLE))
1031
    g_warning ("%s: child property '%s' of container class '%s' is not readable",
Matthias Clasen's avatar
Matthias Clasen committed
1032 1033 1034
               G_STRLOC,
               pspec->name,
               G_OBJECT_TYPE_NAME (container));
1035 1036
  else
    {
Javier Jardón's avatar
Javier Jardón committed
1037
      GValue *prop_value, tmp_value = G_VALUE_INIT;
1038 1039 1040 1041

      /* auto-conversion of the callers value type
       */
      if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
Matthias Clasen's avatar
Matthias Clasen committed
1042 1043 1044 1045
        {
          g_value_reset (value);
          prop_value = value;
        }
1046
      else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
Matthias Clasen's avatar
Matthias Clasen committed
1047
        {
1048
          g_warning ("can't retrieve child property '%s' of type '%s' as value of type '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
1049 1050 1051 1052 1053 1054 1055
                     pspec->name,
                     g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
                     G_VALUE_TYPE_NAME (value));
          g_object_unref (child);
          g_object_unref (container);
          return;
        }
1056
      else
Matthias Clasen's avatar
Matthias Clasen committed
1057 1058 1059 1060
        {
          g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
          prop_value = &tmp_value;
        }
1061 1062
      container_get_child_property (container, child, pspec, prop_value);
      if (prop_value != value)
Matthias Clasen's avatar
Matthias Clasen committed
1063 1064 1065 1066
        {
          g_value_transform (prop_value, value);
          g_value_unset (&tmp_value);
        }
1067 1068 1069 1070 1071
    }
  g_object_unref (child);
  g_object_unref (container);
}

Matthias Clasen's avatar
Matthias Clasen committed
1072 1073 1074 1075
/**
 * gtk_container_child_set_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
1076 1077
 * @first_property_name: the name of the first property to set
 * @var_args: a %NULL-terminated list of property names and values, starting
1078
 *           with @first_prop_name
1079
 *
Matthias Clasen's avatar
Matthias Clasen committed
1080 1081
 * Sets one or more child properties for @child and @container.
 **/
Tim Janik's avatar
Tim Janik committed
1082 1083
void
gtk_container_child_set_valist (GtkContainer *container,
Matthias Clasen's avatar
Matthias Clasen committed
1084 1085 1086
                                GtkWidget    *child,
                                const gchar  *first_property_name,
                                va_list       var_args)
Tim Janik's avatar
Tim Janik committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
{
  GObjectNotifyQueue *nqueue;
  const gchar *name;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));

  g_object_ref (container);
  g_object_ref (child);

  nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
  name = first_property_name;
  while (name)
    {
Javier Jardón's avatar
Javier Jardón committed
1101
      GValue value = G_VALUE_INIT;
Tim Janik's avatar
Tim Janik committed
1102 1103
      gchar *error = NULL;
      GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
Matthias Clasen's avatar
Matthias Clasen committed
1104 1105 1106
                                                    name,
                                                    G_OBJECT_TYPE (container),
                                                    TRUE);
Tim Janik's avatar
Tim Janik committed
1107
      if (!pspec)
Matthias Clasen's avatar
Matthias Clasen committed
1108
        {
1109
          g_warning ("%s: container class '%s' has no child property named '%s'",
Matthias Clasen's avatar
Matthias Clasen committed
1110 1111 1112 1113 1114
                     G_STRLOC,
                     G_OBJECT_TYPE_NAME (container),
                     name);
          break;
        }
Tim Janik's avatar
Tim Janik committed
1115
      if (!(pspec->flags & G_PARAM_WRITABLE))
Matthias Clasen's avatar
Matthias Clasen committed
1116
        {
1117
          g_warning ("%s: child property '%s' of container class '%s' is not writable",
Matthias Clasen's avatar
Matthias Clasen committed
1118 1119 1120 1121 1122
                     G_STRLOC,
                     pspec->name,
                     G_OBJECT_TYPE_NAME (container));
          break;
        }
1123 1124 1125

      G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
                            var_args, 0, &error);
Tim Janik's avatar
Tim Janik committed
1126
      if (error)
Matthias Clasen's avatar
Matthias Clasen committed
1127 1128 1129 1130 1131 1132 1133 1134 1135
        {
          g_warning ("%s: %s", G_STRLOC, error);
          g_free (error);

          /* we purposely leak the value here, it might not be
           * in a sane state if an error condition occoured
           */
          break;
        }
Tim Janik's avatar
Tim Janik committed
1136 1137 1138
      container_set_child_property (container, child, pspec, &value, nqueue);
      g_value_unset (&value);
      name = va_arg (var_args, gchar*);
1139
    }
Tim Janik's avatar
Tim Janik committed
1140
  g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1141

Tim Janik's avatar
Tim Janik committed
1142 1143
  g_object_unref (container);
  g_object_unref (child);
1144 1145
}

Matthias Clasen's avatar
Matthias Clasen committed
1146 1147 1148 1149 1150 1151
/**
 * gtk_container_child_set_property:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
 * @property_name: the name of the property to set
 * @value: the value to set the property to
1152
 *