gtkcontainer.c 70.5 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

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

27
#include <config.h>
28
#include <stdarg.h>
Elliot Lee's avatar
Elliot Lee committed
29
#include <string.h>
30 31
#include <stdlib.h>

32
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
33
#include "gtkcontainer.h"
34
#include "gtkprivate.h"
35
#include "gtkmain.h"
36
#include "gtkmarshalers.h"
37
#include "gtkwindow.h"
Alexander Larsson's avatar
Alexander Larsson committed
38
#include "gtkintl.h"
39
#include "gtktoolbar.h"
Tim Janik's avatar
Tim Janik committed
40 41 42
#include <gobject/gobjectnotifyqueue.c>
#include <gobject/gvaluecollector.h>

Elliot Lee's avatar
Elliot Lee committed
43 44 45 46

enum {
  ADD,
  REMOVE,
47
  CHECK_RESIZE,
48
  SET_FOCUS_CHILD,
Elliot Lee's avatar
Elliot Lee committed
49 50
  LAST_SIGNAL
};
51

52
enum {
Alexander Larsson's avatar
Alexander Larsson committed
53 54 55
  PROP_0,
  PROP_BORDER_WIDTH,
  PROP_RESIZE_MODE,
56
  PROP_CHILD
57
};
Elliot Lee's avatar
Elliot Lee committed
58

Tim Janik's avatar
Tim Janik committed
59 60
#define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
#define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
61

Tim Janik's avatar
Tim Janik committed
62 63

/* --- prototypes --- */
64
static void     gtk_container_base_class_init      (GtkContainerClass *klass);
Tim Janik's avatar
Tim Janik committed
65
static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
66 67 68
static void     gtk_container_class_init           (GtkContainerClass *klass);
static void     gtk_container_init                 (GtkContainer      *container);
static void     gtk_container_destroy              (GtkObject         *object);
Alexander Larsson's avatar
Alexander Larsson committed
69 70 71 72 73 74 75 76
static void     gtk_container_set_property         (GObject         *object,
						    guint            prop_id,
						    const GValue    *value,
						    GParamSpec      *pspec);
static void     gtk_container_get_property         (GObject         *object,
						    guint            prop_id,
						    GValue          *value,
						    GParamSpec      *pspec);
77 78 79 80 81
static void     gtk_container_add_unimplemented    (GtkContainer      *container,
						    GtkWidget         *widget);
static void     gtk_container_remove_unimplemented (GtkContainer      *container,
						    GtkWidget         *widget);
static void     gtk_container_real_check_resize    (GtkContainer      *container);
82
static gboolean gtk_container_focus                (GtkWidget         *widget,
83 84 85
						    GtkDirectionType   direction);
static void     gtk_container_real_set_focus_child (GtkContainer      *container,
						    GtkWidget         *widget);
86

87 88 89 90 91 92 93
static gboolean gtk_container_focus_move           (GtkContainer      *container,
						    GList             *children,
						    GtkDirectionType   direction);
static void     gtk_container_children_callback    (GtkWidget         *widget,
						    gpointer           client_data);
static void     gtk_container_show_all             (GtkWidget         *widget);
static void     gtk_container_hide_all             (GtkWidget         *widget);
94 95
static gint     gtk_container_expose               (GtkWidget         *widget,
						    GdkEventExpose    *event);
96 97 98
static void     gtk_container_map                  (GtkWidget         *widget);
static void     gtk_container_unmap                (GtkWidget         *widget);

99 100 101
static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
							  GtkWidget    *child);

Elliot Lee's avatar
Elliot Lee committed
102

Tim Janik's avatar
Tim Janik committed
103 104 105 106 107 108 109 110 111 112
/* --- variables --- */
static const gchar          *vadjustment_key = "gtk-vadjustment";
static guint                 vadjustment_key_id = 0;
static const gchar          *hadjustment_key = "gtk-hadjustment";
static guint                 hadjustment_key_id = 0;
static GSList	            *container_resize_queue = NULL;
static guint                 container_signals[LAST_SIGNAL] = { 0 };
static GtkWidgetClass       *parent_class = NULL;
extern GParamSpecPool       *_gtk_widget_child_property_pool;
extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
Elliot Lee's avatar
Elliot Lee committed
113

114

Tim Janik's avatar
Tim Janik committed
115
/* --- functions --- */
Manish Singh's avatar
Manish Singh committed
116
GType
117
gtk_container_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
118
{
Manish Singh's avatar
Manish Singh committed
119
  static GType container_type = 0;
Elliot Lee's avatar
Elliot Lee committed
120 121 122

  if (!container_type)
    {
Manish Singh's avatar
Manish Singh committed
123 124
      static const GTypeInfo container_info =
      {
Elliot Lee's avatar
Elliot Lee committed
125
	sizeof (GtkContainerClass),
Tim Janik's avatar
Tim Janik committed
126 127 128
	(GBaseInitFunc) gtk_container_base_class_init,
	(GBaseFinalizeFunc) gtk_container_base_class_finalize,
	(GClassInitFunc) gtk_container_class_init,
Manish Singh's avatar
Manish Singh committed
129
	NULL        /* class_finalize */,
Tim Janik's avatar
Tim Janik committed
130 131 132 133 134
	NULL        /* class_data */,
	sizeof (GtkContainer),
	0           /* n_preallocs */,
	(GInstanceInitFunc) gtk_container_init,
	NULL,       /* value_table */
Elliot Lee's avatar
Elliot Lee committed
135 136
      };

Manish Singh's avatar
Manish Singh committed
137 138 139
      container_type =
	g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer", 
				&container_info, G_TYPE_FLAG_ABSTRACT);
Elliot Lee's avatar
Elliot Lee committed
140 141 142 143 144
    }

  return container_type;
}

145 146 147 148
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
149 150
  class->set_child_property = NULL;
  class->get_child_property = NULL;
151 152
}

Elliot Lee's avatar
Elliot Lee committed
153
static void
Tim Janik's avatar
Tim Janik committed
154
gtk_container_base_class_finalize (GtkContainerClass *class)
Elliot Lee's avatar
Elliot Lee committed
155
{
Tim Janik's avatar
Tim Janik committed
156
  GList *list, *node;
Elliot Lee's avatar
Elliot Lee committed
157

158
  list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
Tim Janik's avatar
Tim Janik committed
159 160 161
  for (node = list; node; node = node->next)
    {
      GParamSpec *pspec = node->data;
162

Tim Janik's avatar
Tim Janik committed
163 164 165 166 167 168 169 170 171 172 173 174 175
      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);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
176

Tim Janik's avatar
Tim Janik committed
177
  parent_class = g_type_class_peek_parent (class);
178

179 180
  vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
  hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
181
  
Alexander Larsson's avatar
Alexander Larsson committed
182 183 184
  gobject_class->set_property = gtk_container_set_property;
  gobject_class->get_property = gtk_container_get_property;

Tim Janik's avatar
Tim Janik committed
185 186 187 188 189
  object_class->destroy = gtk_container_destroy;

  widget_class->show_all = gtk_container_show_all;
  widget_class->hide_all = gtk_container_hide_all;
  widget_class->expose_event = gtk_container_expose;
190 191
  widget_class->map = gtk_container_map;
  widget_class->unmap = gtk_container_unmap;
Tim Janik's avatar
Tim Janik committed
192 193 194 195 196 197 198 199 200 201
  widget_class->focus = gtk_container_focus;
  
  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;
  class->composite_name = gtk_container_child_default_composite_name;

Alexander Larsson's avatar
Alexander Larsson committed
202 203 204
  g_object_class_install_property (gobject_class,
                                   PROP_RESIZE_MODE,
                                   g_param_spec_enum ("resize_mode",
205 206
                                                      P_("Resize mode"),
                                                      P_("Specify how resize events are handled"),
Alexander Larsson's avatar
Alexander Larsson committed
207 208 209 210 211 212
                                                      GTK_TYPE_RESIZE_MODE,
                                                      GTK_RESIZE_PARENT,
                                                      G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_BORDER_WIDTH,
                                   g_param_spec_uint ("border_width",
213 214
                                                      P_("Border width"),
                                                      P_("The width of the empty border outside the containers children"),
Alexander Larsson's avatar
Alexander Larsson committed
215 216 217 218 219 220 221
						      0,
						      G_MAXINT,
						      0,
                                                      G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_CHILD,
                                   g_param_spec_object ("child",
222 223
                                                      P_("Child"),
                                                      P_("Can be used to add a new child to the container"),
Alexander Larsson's avatar
Alexander Larsson committed
224 225
                                                      GTK_TYPE_WIDGET,
						      G_PARAM_WRITABLE));
Elliot Lee's avatar
Elliot Lee committed
226
  container_signals[ADD] =
Manish Singh's avatar
Manish Singh committed
227 228 229 230 231 232 233 234
    g_signal_new ("add",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkContainerClass, add),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_WIDGET);
Elliot Lee's avatar
Elliot Lee committed
235
  container_signals[REMOVE] =
Manish Singh's avatar
Manish Singh committed
236 237 238 239 240 241 242 243
    g_signal_new ("remove",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkContainerClass, remove),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_WIDGET);
244
  container_signals[CHECK_RESIZE] =
Manish Singh's avatar
Manish Singh committed
245 246 247 248 249 250 251
    g_signal_new ("check_resize",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkContainerClass, check_resize),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
252
  container_signals[SET_FOCUS_CHILD] =
Manish Singh's avatar
Manish Singh committed
253 254 255 256 257 258 259 260
    g_signal_new ("set-focus-child",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_WIDGET);
261 262
}

Matthias Clasen's avatar
Matthias Clasen committed
263 264 265 266 267 268
/**
 * gtk_container_child_type: 
 * @container: a #GtkContainer.
 *
 * Returns the type of the children supported by the container.
 *
Manish Singh's avatar
Manish Singh committed
269
 * Note that this may return %G_TYPE_NONE to indicate that no more
Matthias Clasen's avatar
Matthias Clasen committed
270 271 272
 * children can be added, e.g. for a #GtkPaned which already has two 
 * children.
 *
Manish Singh's avatar
Manish Singh committed
273
 * Return value: a #GType.
Matthias Clasen's avatar
Matthias Clasen committed
274
 **/
Manish Singh's avatar
Manish Singh committed
275
GType
Tim Janik's avatar
Tim Janik committed
276
gtk_container_child_type (GtkContainer *container)
277
{
Manish Singh's avatar
Manish Singh committed
278
  GType slot;
279 280 281 282
  GtkContainerClass *class;

  g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);

283
  class = GTK_CONTAINER_GET_CLASS (container);
284 285
  if (class->child_type)
    slot = class->child_type (container);
286
  else
Manish Singh's avatar
Manish Singh committed
287
    slot = G_TYPE_NONE;
288 289 290 291

  return slot;
}

Tim Janik's avatar
Tim Janik committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/* --- GtkContainer child property mechanism --- */
static inline void
container_get_child_property (GtkContainer *container,
			      GtkWidget    *child,
			      GParamSpec   *pspec,
			      GValue       *value)
{
  GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
  
  class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
}

static inline void
container_set_child_property (GtkContainer       *container,
			      GtkWidget		 *child,
			      GParamSpec         *pspec,
			      const GValue       *value,
			      GObjectNotifyQueue *nqueue)
{
  GValue tmp_value = { 0, };
  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))
    g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
	       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);

      g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
		 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);
}
339

Matthias Clasen's avatar
Matthias Clasen committed
340 341 342 343
/**
 * gtk_container_child_get_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
344 345
 * @first_property_name: the name of the first property to get
 * @var_args: a %NULL-terminated list of property names and #GValue*, 
Matthias Clasen's avatar
Matthias Clasen committed
346 347 348 349
 *           starting with @first_prop_name.
 * 
 * Gets the values of one or more child properties for @child and @container.
 **/
350
void
Tim Janik's avatar
Tim Janik committed
351 352 353 354
gtk_container_child_get_valist (GtkContainer *container,
				GtkWidget    *child,
				const gchar  *first_property_name,
				va_list       var_args)
355
{
Tim Janik's avatar
Tim Janik committed
356 357
  const gchar *name;

358
  g_return_if_fail (GTK_IS_CONTAINER (container));
Tim Janik's avatar
Tim Janik committed
359 360
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == GTK_WIDGET (container));
361

Tim Janik's avatar
Tim Janik committed
362 363
  g_object_ref (container);
  g_object_ref (child);
364

Tim Janik's avatar
Tim Janik committed
365 366
  name = first_property_name;
  while (name)
367
    {
Tim Janik's avatar
Tim Janik committed
368 369
      GValue value = { 0, };
      GParamSpec *pspec;
370 371
      gchar *error;

Tim Janik's avatar
Tim Janik committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
					name,
					G_OBJECT_TYPE (container),
					TRUE);
      if (!pspec)
	{
	  g_warning ("%s: container class `%s' has no child property named `%s'",
		     G_STRLOC,
		     G_OBJECT_TYPE_NAME (container),
		     name);
	  break;
	}
      if (!(pspec->flags & G_PARAM_READABLE))
	{
	  g_warning ("%s: child property `%s' of container class `%s' is not readable",
		     G_STRLOC,
		     pspec->name,
		     G_OBJECT_TYPE_NAME (container));
	  break;
	}
      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);
395 396
      if (error)
	{
Tim Janik's avatar
Tim Janik committed
397
	  g_warning ("%s: %s", G_STRLOC, error);
398
	  g_free (error);
Tim Janik's avatar
Tim Janik committed
399 400
	  g_value_unset (&value);
	  break;
401
	}
Tim Janik's avatar
Tim Janik committed
402 403 404 405 406 407 408 409
      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
410 411 412 413 414 415 416 417 418
/**
 * 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
 * 
 * Gets the value of a child property for @child and @container.
 **/
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
void
gtk_container_child_get_property (GtkContainer *container,
				  GtkWidget    *child,
				  const gchar  *property_name,
				  GValue       *value)
{
  GParamSpec *pspec;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == GTK_WIDGET (container));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (G_IS_VALUE (value));
  
  g_object_ref (container);
  g_object_ref (child);
  pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
				    G_OBJECT_TYPE (container), TRUE);
  if (!pspec)
    g_warning ("%s: container class `%s' has no child property named `%s'",
	       G_STRLOC,
	       G_OBJECT_TYPE_NAME (container),
	       property_name);
  else if (!(pspec->flags & G_PARAM_READABLE))
    g_warning ("%s: child property `%s' of container class `%s' is not readable",
	       G_STRLOC,
	       pspec->name,
	       G_OBJECT_TYPE_NAME (container));
  else
    {
      GValue *prop_value, tmp_value = { 0, };

      /* auto-conversion of the callers value type
       */
      if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
	{
	  g_value_reset (value);
	  prop_value = value;
	}
      else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
	{
jacob berkman's avatar
jacob berkman committed
460
	  g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
		     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;
	}
      else
	{
	  g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
	  prop_value = &tmp_value;
	}
      container_get_child_property (container, child, pspec, prop_value);
      if (prop_value != value)
	{
	  g_value_transform (prop_value, value);
	  g_value_unset (&tmp_value);
	}
    }
  g_object_unref (child);
  g_object_unref (container);
}

Matthias Clasen's avatar
Matthias Clasen committed
484 485 486 487
/**
 * gtk_container_child_set_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
488 489
 * @first_property_name: the name of the first property to set
 * @var_args: a %NULL-terminated list of property names and values, starting
Matthias Clasen's avatar
Matthias Clasen committed
490 491 492 493
 *           with @first_prop_name.
 * 
 * Sets one or more child properties for @child and @container.
 **/
Tim Janik's avatar
Tim Janik committed
494 495 496 497 498 499 500 501 502 503 504 505 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
void
gtk_container_child_set_valist (GtkContainer *container,
				GtkWidget    *child,
				const gchar  *first_property_name,
				va_list       var_args)
{
  GObject *object;
  GObjectNotifyQueue *nqueue;
  const gchar *name;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == GTK_WIDGET (container));

  g_object_ref (container);
  g_object_ref (child);

  object = G_OBJECT (container);
  nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
  name = first_property_name;
  while (name)
    {
      GValue value = { 0, };
      gchar *error = NULL;
      GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
						    name,
						    G_OBJECT_TYPE (container),
						    TRUE);
      if (!pspec)
	{
	  g_warning ("%s: container class `%s' has no child property named `%s'",
		     G_STRLOC,
		     G_OBJECT_TYPE_NAME (container),
		     name);
	  break;
	}
      if (!(pspec->flags & G_PARAM_WRITABLE))
531
	{
Tim Janik's avatar
Tim Janik committed
532 533 534 535 536
	  g_warning ("%s: child property `%s' of container class `%s' is not writable",
		     G_STRLOC,
		     pspec->name,
		     G_OBJECT_TYPE_NAME (container));
	  break;
537
	}
Tim Janik's avatar
Tim Janik committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
      G_VALUE_COLLECT (&value, var_args, 0, &error);
      if (error)
	{
	  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;
	}
      container_set_child_property (container, child, pspec, &value, nqueue);
      g_value_unset (&value);
      name = va_arg (var_args, gchar*);
553
    }
Tim Janik's avatar
Tim Janik committed
554
  g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
555

Tim Janik's avatar
Tim Janik committed
556 557
  g_object_unref (container);
  g_object_unref (child);
558 559
}

Matthias Clasen's avatar
Matthias Clasen committed
560 561 562 563 564 565 566 567 568
/**
 * 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
 * 
 * Sets a child property for @child and @container.
 **/
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
void
gtk_container_child_set_property (GtkContainer *container,
				  GtkWidget    *child,
				  const gchar  *property_name,
				  const GValue *value)
{
  GObject *object;
  GObjectNotifyQueue *nqueue;
  GParamSpec *pspec;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == GTK_WIDGET (container));
  g_return_if_fail (property_name != NULL);
  g_return_if_fail (G_IS_VALUE (value));
  
  g_object_ref (container);
  g_object_ref (child);

  object = G_OBJECT (container);
  nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
  pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
				    G_OBJECT_TYPE (container), TRUE);
  if (!pspec)
    g_warning ("%s: container class `%s' has no child property named `%s'",
	       G_STRLOC,
	       G_OBJECT_TYPE_NAME (container),
	       property_name);
  else if (!(pspec->flags & G_PARAM_WRITABLE))
    g_warning ("%s: child property `%s' of container class `%s' is not writable",
	       G_STRLOC,
	       pspec->name,
	       G_OBJECT_TYPE_NAME (container));
  else
    {
      container_set_child_property (container, child, pspec, value, nqueue);
    }
  g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
  g_object_unref (container);
  g_object_unref (child);
}

Matthias Clasen's avatar
Matthias Clasen committed
611 612 613 614 615 616 617 618 619 620 621
/**
 * gtk_container_add_with_properties:
 * @container: a #GtkContainer 
 * @widget: a widget to be placed inside @container 
 * @first_prop_name: the name of the first child property to set 
 * @Varargs: a %NULL-terminated list of property names and values, starting
 *           with @first_prop_name.
 * 
 * Adds @widget to @container, setting child properties at the same time.
 * See gtk_container_add() and gtk_container_child_set() for more details.
 **/
622
void
Tim Janik's avatar
Tim Janik committed
623 624 625 626
gtk_container_add_with_properties (GtkContainer *container,
				   GtkWidget    *widget,
				   const gchar  *first_prop_name,
				   ...)
627
{
628 629 630
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (widget->parent == NULL);
631

Manish Singh's avatar
Manish Singh committed
632 633
  g_object_ref (container);
  g_object_ref (widget);
Tim Janik's avatar
Tim Janik committed
634
  gtk_widget_freeze_child_notify (widget);
635

Manish Singh's avatar
Manish Singh committed
636
  g_signal_emit (container, container_signals[ADD], 0, widget);
637
  if (widget->parent)
638
    {
Tim Janik's avatar
Tim Janik committed
639
      va_list var_args;
640

Tim Janik's avatar
Tim Janik committed
641 642 643
      va_start (var_args, first_prop_name);
      gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
      va_end (var_args);
644 645
    }

Tim Janik's avatar
Tim Janik committed
646
  gtk_widget_thaw_child_notify (widget);
Manish Singh's avatar
Manish Singh committed
647 648
  g_object_unref (widget);
  g_object_unref (container);
649 650
}

Matthias Clasen's avatar
Matthias Clasen committed
651 652 653 654 655 656 657 658 659 660
/**
 * gtk_container_child_set:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
 * @first_prop_name: the name of the first property to set
 * @Varargs: a %NULL-terminated list of property names and values, starting
 *           with @first_prop_name.
 * 
 * Sets one or more child properties for @child and @container.
 **/
661
void
662 663
gtk_container_child_set (GtkContainer      *container,
			 GtkWidget         *child,
Tim Janik's avatar
Tim Janik committed
664
			 const gchar       *first_prop_name,
665
			 ...)
666
{
667 668
  va_list var_args;
  
669 670
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
Tim Janik's avatar
Tim Janik committed
671
  g_return_if_fail (child->parent == GTK_WIDGET (container));
672

Tim Janik's avatar
Tim Janik committed
673 674 675
  va_start (var_args, first_prop_name);
  gtk_container_child_set_valist (container, child, first_prop_name, var_args);
  va_end (var_args);
676 677
}

Matthias Clasen's avatar
Matthias Clasen committed
678 679 680 681 682 683 684 685 686 687
/**
 * gtk_container_child_get:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
 * @first_prop_name: the name of the first property to get
 * @Varargs: a %NULL-terminated list of property names and #GValue*, 
 *           starting with @first_prop_name.
 * 
 * Gets the values of one or more child properties for @child and @container.
 **/
688
void
Tim Janik's avatar
Tim Janik committed
689 690 691 692
gtk_container_child_get (GtkContainer      *container,
			 GtkWidget         *child,
			 const gchar       *first_prop_name,
			 ...)
693
{
Tim Janik's avatar
Tim Janik committed
694
  va_list var_args;
695 696 697
  
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
Tim Janik's avatar
Tim Janik committed
698 699 700 701 702
  g_return_if_fail (child->parent == GTK_WIDGET (container));

  va_start (var_args, first_prop_name);
  gtk_container_child_get_valist (container, child, first_prop_name, var_args);
  va_end (var_args);
703 704
}

Matthias Clasen's avatar
Matthias Clasen committed
705 706 707 708 709 710 711 712
/**
 * gtk_container_class_install_child_property:
 * @cclass: a #GtkContainerClass
 * @property_id: the id for the property
 * @pspec: the #GParamSpec for the property
 * 
 * Installs a child property on a container class. 
 **/
713
void
714
gtk_container_class_install_child_property (GtkContainerClass *cclass,
Tim Janik's avatar
Tim Janik committed
715 716
					    guint              property_id,
					    GParamSpec        *pspec)
717
{
718
  g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
Tim Janik's avatar
Tim Janik committed
719 720
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
  if (pspec->flags & G_PARAM_WRITABLE)
721
    g_return_if_fail (cclass->set_child_property != NULL);
Tim Janik's avatar
Tim Janik committed
722
  if (pspec->flags & G_PARAM_READABLE)
723
    g_return_if_fail (cclass->get_child_property != NULL);
Tim Janik's avatar
Tim Janik committed
724 725 726 727 728
  g_return_if_fail (property_id > 0);
  g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
  if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
    g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);

729
  if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
730
    {
Matthias Clasen's avatar
Matthias Clasen committed
731
      g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
732
		 G_OBJECT_CLASS_NAME (cclass),
Tim Janik's avatar
Tim Janik committed
733
		 pspec->name);
734
      return;
735
    }
Tim Janik's avatar
Tim Janik committed
736 737 738
  g_param_spec_ref (pspec);
  g_param_spec_sink (pspec);
  PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
739
  g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
740 741
}

742 743
/**
 * gtk_container_class_find_child_property:
744
 * @cclass: a #GtkContainerClass
745
 * @property_name: the name of the child property to find
746
 * @returns: the #GParamSpec of the child property or %NULL if @class has no
747 748 749 750
 *   child property with that name.
 *
 * Finds a child property of a container class by name.
 */
Tim Janik's avatar
Tim Janik committed
751
GParamSpec*
752
gtk_container_class_find_child_property (GObjectClass *cclass,
Tim Janik's avatar
Tim Janik committed
753
					 const gchar  *property_name)
754
{
755
  g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
Tim Janik's avatar
Tim Janik committed
756
  g_return_val_if_fail (property_name != NULL, NULL);
757

Tim Janik's avatar
Tim Janik committed
758 759
  return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
				   property_name,
760
				   G_OBJECT_CLASS_TYPE (cclass),
Tim Janik's avatar
Tim Janik committed
761
				   TRUE);
762
}
763

764 765
/**
 * gtk_container_class_list_child_properties:
766
 * @cclass: a #GtkContainerClass
767
 * @n_properties: location to return the number of child properties found
Matthias Clasen's avatar
Matthias Clasen committed
768 769
 * @returns: a newly allocated array of #GParamSpec*. The array must be 
 *           freed with g_free().
770 771 772 773
 *
 * Returns all child properties of a container class.
 */
GParamSpec**
774
gtk_container_class_list_child_properties (GObjectClass *cclass,
Tim Janik's avatar
Tim Janik committed
775
					   guint        *n_properties)
776
{
Tim Janik's avatar
Tim Janik committed
777 778
  GParamSpec **pspecs;
  guint n;
779

780
  g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
781

Tim Janik's avatar
Tim Janik committed
782
  pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
783
				   G_OBJECT_CLASS_TYPE (cclass),
Tim Janik's avatar
Tim Janik committed
784 785 786
				   &n);
  if (n_properties)
    *n_properties = n;
Elliot Lee's avatar
Elliot Lee committed
787

Tim Janik's avatar
Tim Janik committed
788 789
  return pspecs;
}
790

791 792 793 794
static void
gtk_container_add_unimplemented (GtkContainer     *container,
				 GtkWidget        *widget)
{
Manish Singh's avatar
Manish Singh committed
795
  g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
796 797 798 799 800 801
}

static void
gtk_container_remove_unimplemented (GtkContainer     *container,
				    GtkWidget        *widget)
{
Manish Singh's avatar
Manish Singh committed
802
  g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
803 804
}

Elliot Lee's avatar
Elliot Lee committed
805 806 807 808 809 810
static void
gtk_container_init (GtkContainer *container)
{
  container->focus_child = NULL;
  container->border_width = 0;
  container->need_resize = FALSE;
811
  container->resize_mode = GTK_RESIZE_PARENT;
812
  container->reallocate_redraws = FALSE;
813 814 815 816 817
}

static void
gtk_container_destroy (GtkObject *object)
{
818
  GtkContainer *container = GTK_CONTAINER (object);
819
  
820
  if (GTK_CONTAINER_RESIZE_PENDING (container))
821
    _gtk_container_dequeue_resize_handler (container);
822 823 824 825 826 827

  /* do this before walking child widgets, to avoid
   * removing children from focus chain one by one.
   */
  if (container->has_focus_chain)
    gtk_container_unset_focus_chain (container);
828
  
829
  gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
830 831 832
  
  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
Elliot Lee's avatar
Elliot Lee committed
833 834 835
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
836 837 838 839
gtk_container_set_property (GObject         *object,
			    guint            prop_id,
			    const GValue    *value,
			    GParamSpec      *pspec)
Elliot Lee's avatar
Elliot Lee committed
840
{
841
  GtkContainer *container = GTK_CONTAINER (object);
842

Alexander Larsson's avatar
Alexander Larsson committed
843
  switch (prop_id)
Elliot Lee's avatar
Elliot Lee committed
844
    {
Alexander Larsson's avatar
Alexander Larsson committed
845 846
    case PROP_BORDER_WIDTH:
      gtk_container_set_border_width (container, g_value_get_uint (value));
847
      break;
Alexander Larsson's avatar
Alexander Larsson committed
848 849
    case PROP_RESIZE_MODE:
      gtk_container_set_resize_mode (container, g_value_get_enum (value));
850
      break;
Alexander Larsson's avatar
Alexander Larsson committed
851 852
    case PROP_CHILD:
      gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
853
      break;
Tim Janik's avatar
Tim Janik committed
854
    default:
Alexander Larsson's avatar
Alexander Larsson committed
855
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
856
      break;
857 858 859 860
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
861 862 863 864
gtk_container_get_property (GObject         *object,
			    guint            prop_id,
			    GValue          *value,
			    GParamSpec      *pspec)
865
{
866
  GtkContainer *container = GTK_CONTAINER (object);
867
  
Alexander Larsson's avatar
Alexander Larsson committed
868
  switch (prop_id)
869
    {
Alexander Larsson's avatar
Alexander Larsson committed
870 871
    case PROP_BORDER_WIDTH:
      g_value_set_uint (value, container->border_width);
872
      break;
Alexander Larsson's avatar
Alexander Larsson committed
873 874
    case PROP_RESIZE_MODE:
      g_value_set_enum (value, container->resize_mode);
875
      break;
876
    default:
Alexander Larsson's avatar
Alexander Larsson committed
877
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
878
      break;
Elliot Lee's avatar
Elliot Lee committed
879 880 881
    }
}

882 883 884
/**
 * gtk_container_set_border_width:
 * @container: a #GtkContainer
885
 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
886 887
 *   Valid values are in the range 0-65535 pixels.
 *
Matthias Clasen's avatar
Matthias Clasen committed
888 889
 * Sets the border width of the container.
 *
890 891 892 893 894 895 896 897
 * The border width of a container is the amount of space to leave
 * around the outside of the container. The only exception to this is
 * #GtkWindow; because toplevel windows can't leave space outside,
 * they leave the space inside. The border is added on all sides of
 * the container. To add space to only one side, one approach is to
 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
 * it a size, and place it on the side of the container as a spacer.
 **/
Elliot Lee's avatar
Elliot Lee committed
898
void
899 900
gtk_container_set_border_width (GtkContainer *container,
				guint         border_width)
Elliot Lee's avatar
Elliot Lee committed
901 902 903 904 905 906
{
  g_return_if_fail (GTK_IS_CONTAINER (container));

  if (container->border_width != border_width)
    {
      container->border_width = border_width;
Alexander Larsson's avatar
Alexander Larsson committed
907 908
      g_object_notify (G_OBJECT (container), "border_width");
      
Tim Janik's avatar
Tim Janik committed
909 910
      if (GTK_WIDGET_REALIZED (container))
	gtk_widget_queue_resize (GTK_WIDGET (container));
Elliot Lee's avatar
Elliot Lee committed
911 912 913
    }
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
/**
 * gtk_container_get_border_width:
 * @container: a #GtkContainer
 * 
 * Retrieves the border width of the container. See
 * gtk_container_set_border_width().
 *
 * Return value: the current border width
 **/
guint
gtk_container_get_border_width (GtkContainer *container)
{
  g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);

  return container->border_width;
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944
/**
 * gtk_container_add:
 * @container: a #GtkContainer
 * @widget: a widget to be placed inside @container
 * 
 * Adds @widget to @container. Typically used for simple containers
 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
 * layout containers such as #GtkBox or #GtkTable, this function will
 * pick default packing parameters that may not be correct.  So
 * consider functions such as gtk_box_pack_start() and
 * gtk_table_attach() as an alternative to gtk_container_add() in
 * those cases. A widget may be added to only one container at a time;
 * you can't place the same widget inside two different containers.
 **/
Elliot Lee's avatar
Elliot Lee committed
945 946 947 948
void
gtk_container_add (GtkContainer *container,
		   GtkWidget    *widget)
{
949 950
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (widget));
951 952 953 954

  if (widget->parent != NULL)
    {
      g_warning ("Attempting to add a widget with type %s to a container of "
Havoc Pennington's avatar
Havoc Pennington committed
955 956
                 "type %s, but the widget is already inside a container of type %s, "
                 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
Tim Janik's avatar
Tim Janik committed
957 958 959
                 g_type_name (G_OBJECT_TYPE (widget)),
                 g_type_name (G_OBJECT_TYPE (container)),
                 g_type_name (G_OBJECT_TYPE (widget->parent)));
960 961
      return;
    }
962

Manish Singh's avatar
Manish Singh committed
963
  g_signal_emit (container, container_signals[ADD], 0, widget);
Elliot Lee's avatar
Elliot Lee committed
964 965
}

966 967 968 969 970 971 972 973 974 975
/**
 * gtk_container_remove:
 * @container: a #GtkContainer
 * @widget: a current child of @container
 * 
 * Removes @widget from @container. @widget must be inside @container.
 * Note that @container will own a reference to @widget, and that this
 * may be the last reference held; so removing a widget from its
 * container can destroy that widget. If you want to use @widget
 * again, you need to add a reference to it while it's not inside
976 977 978 979
 * a container, using g_object_ref(). If you don't want to use @widget
 * again it's usually more efficient to simply destroy it directly
 * using gtk_widget_destroy() since this will remove it from the
 * container and help break any circular reference count cycles.
980
 **/
Elliot Lee's avatar
Elliot Lee committed
981 982 983 984 985
void
gtk_container_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
986
  g_return_if_fail (GTK_IS_WIDGET (widget));
987 988 989 990 991 992 993

  /* When using the deprecated API of the toolbar, it is possible
   * to legitimately call this function with a widget that is not
   * a direct child of the container.
   */
  g_return_if_fail (GTK_IS_TOOLBAR (container) ||
		    widget->parent == GTK_WIDGET (container));
994
  
Manish Singh's avatar
Manish Singh committed
995
  g_signal_emit (container, container_signals[REMOVE], 0, widget);
Elliot Lee's avatar
Elliot Lee committed
996 997
}

998
void
999
_gtk_container_dequeue_resize_handler (GtkContainer *container)
1000 1001 1002 1003 1004 1005 1006 1007
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));

  container_resize_queue = g_slist_remove (container_resize_queue, container);
  GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
}

Matthias Clasen's avatar
Matthias Clasen committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
/**
 * gtk_container_set_resize_mode:
 * @container: a #GtkContainer.
 * @resize_mode: the new resize mode.
 * 
 * Sets the resize mode for the container.
 *
 * The resize mode of a container determines whether a resize request 
 * will be passed to the container's parent, queued for later execution
 * or executed immediately.
 **/
Elliot Lee's avatar
Elliot Lee committed
1019
void
1020 1021
gtk_container_set_resize_mode (GtkContainer  *container,
			       GtkResizeMode  resize_mode)
Elliot Lee's avatar
Elliot Lee committed
1022 1023
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
1024 1025 1026 1027
  g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
  
  if (GTK_WIDGET_TOPLEVEL (container) &&
      resize_mode == GTK_RESIZE_PARENT)
Alexander Larsson's avatar
Alexander Larsson committed
1028 1029 1030
    {
      resize_mode = GTK_RESIZE_QUEUE;
    }
1031 1032
  
  if (container->resize_mode != resize_mode)
1033
    {
1034 1035
      container->resize_mode = resize_mode;
      
1036 1037
      gtk_widget_queue_resize (GTK_WIDGET (container));
      g_object_notify (G_OBJECT (container), "resize_mode");
1038 1039 1040
    }
}

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
/**
 * gtk_container_get_resize_mode:
 * @container: a #GtkContainer
 * 
 * Returns the resize mode for the container. See
 * gtk_container_set_resize_mode ().
 *
 * Return value: the current resize mode
 **/
GtkResizeMode
gtk_container_get_resize_mode (GtkContainer *container)
{
  g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);

  return container->resize_mode;
}

Matthias Clasen's avatar
Matthias Clasen committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
/**
 * gtk_container_set_reallocate_redraws:
 * @container: a #GtkContainer.
 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
 *
 * Sets the @reallocate_redraws flag of the container to the given value.
 * 
 * Containers requesting reallocation redraws get automatically
 * redrawn if any of their children changed allocation. 
 **/ 
1068 1069 1070 1071 1072 1073
void
gtk_container_set_reallocate_redraws (GtkContainer *container,
				      gboolean      needs_redraws)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));

1074
  container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1075 1076
}

1077 1078 1079
static GtkContainer*
gtk_container_get_resize_container (GtkContainer *container)
{
1080
  GtkWidget *widget = GTK_WIDGET (container);
1081 1082 1083 1084

  while (widget->parent)
    {
      widget = widget->parent;
1085
      if (GTK_IS_RESIZE_CONTAINER (widget))
1086 1087 1088 1089 1090 1091 1092 1093 1094
	break;
    }

  return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
}

static gboolean
gtk_container_idle_sizer (gpointer data)
{
1095
  GDK_THREADS_ENTER ();
1096

1097 1098 1099 1100 1101 1102 1103
  /* we may be invoked with a container_resize_queue of NULL, because
   * queue_resize could have been adding an extra idle function while
   * the queue still got processed. we better just ignore such case
   * than trying to explicitely work around them with some extra flags,
   * since it doesn't cause any actual harm.
   */
  while (container_resize_queue)
1104
    {
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
      GSList *slist;
      GtkWidget *widget;

      slist = container_resize_queue;
      container_resize_queue = slist->next;
      widget = slist->data;
      g_slist_free_1 (slist);

      GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
      gtk_container_check_resize (GTK_CONTAINER (widget));
1115
    }
1116

1117 1118 1119 1120
  gdk_window_process_all_updates ();

  GDK_THREADS_LEAVE ();

1121 1122 1123 1124
  return FALSE;
}

void
1125
_gtk_container_queue_resize (GtkContainer *container)
1126 1127
{
  GtkContainer *resize_container;
1128
  GtkWidget *widget;
1129 1130 1131
  
  g_return_if_fail (GTK_IS_CONTAINER (container));

1132
  widget = GTK_WIDGET (container);
1133
  resize_container = gtk_container_get_resize_container (container);
1134
  
1135
  while (TRUE)
1136
    {
1137 1138 1139 1140 1141
      GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
      GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
      if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
	  !widget->parent)
	break;
1142
      
1143 1144
      widget = widget->parent;
    }
1145
      
1146 1147
  if (resize_container)
    {
1148
      if (GTK_WIDGET_VISIBLE (resize_container) &&
1149
 	  (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1150
	{
1151
	  switch (resize_container->resize_mode)
1152
	    {
1153 1154 1155 1156 1157
	    case GTK_RESIZE_QUEUE:
	      if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
		{
		  GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
		  if (container_resize_queue == NULL)
1158 1159 1160
		    g_idle_add_full (GTK_PRIORITY_RESIZE,
				     gtk_container_idle_sizer,
				     NULL, NULL);
1161 1162 1163 1164 1165 1166 1167 1168 1169
		  container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
		}
	      break;

	    case GTK_RESIZE_IMMEDIATE:
	      gtk_container_check_resize (resize_container);
	      break;

	    case GTK_RESIZE_PARENT:
1170
	      g_assert_not_reached ();
1171
	      break;
1172
	    }
1173
	}
1174 1175
      else
	{
1176 1177 1178
	  /* we need to let hidden resize containers know that something
	   * changed while they where hidden (currently only evaluated by
	   * toplevels).
1179
	   */
1180
	  resize_container->need_resize = TRUE;
1181
	}
1182
    }
Elliot Lee's avatar
Elliot Lee committed
1183 1184 1185
}

void
1186 1187 1188
gtk_container_check_resize (GtkContainer *container)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
1189
  
Manish Singh's avatar
Manish Singh committed
1190
  g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1191 1192 1193 1194
}

static void
gtk_container_real_check_resize (GtkContainer *container)
Elliot Lee's avatar
Elliot Lee committed
1195
{
1196
  GtkWidget *widget = GTK_WIDGET (container);
1197
  GtkRequisition requisition;
1198
  
1199
  gtk_widget_size_request (widget, &requisition);
1200
  
1201 1202
  if (requisition.width > widget->allocation.width ||
      requisition.height > widget->allocation.height)
1203
    {
1204
      if (GTK_IS_RESIZE_CONTAINER (container))
1205 1206
	gtk_widget_size_allocate (GTK_WIDGET (container),
				  &GTK_WIDGET (container)->allocation);
1207 1208
      else
	gtk_widget_queue_resize (widget);
1209 1210 1211 1212 1213
    }
  else
    {
      gtk_container_resize_children (container);
    }
Elliot Lee's avatar
Elliot Lee committed
1214 1215
}

1216
/* The container hasn't changed size but one of its children
1217 1218 1219
 *  queued a resize request. Which means that the allocation
 *  is not sufficient for the requisition of some child.
 *  We've already performed a size request at this point,
1220 1221
 *  so we simply need to reallocate and let the allocation
 *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1222 1223 1224
 */
void
gtk_container_resize_children (GtkContainer *container)
Elliot Lee's avatar
Elliot Lee committed
1225
{
1226
  GtkWidget *widget;
1227
  
1228 1229 1230 1231 1232
  /* resizing invariants:
   * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
   * containers that have an idle sizer pending must be flagged with
   * RESIZE_PENDING.
   */
1233 1234
  g_return_if_fail (GTK_IS_CONTAINER (container));

1235 1236
  widget = GTK_WIDGET (container);
  gtk_widget_size_allocate (widget, &widget->allocation);
Elliot Lee's avatar
Elliot Lee committed
1237 1238
}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
/**
 * gtk_container_forall:
 * @container: a #GtkContainer
 * @callback: a callback
 * @callback_data: callback user data
 * 
 * Invokes @callback on each child of @container, including children
 * that are considered "internal" (implementation details of the
 * container). "Internal" children generally weren't added by the user
 * of the container, but were added by the container implementation
 * itself.  Most applications should use gtk_container_foreach(),
 * rather than gtk_container_forall().
 **/
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
void
gtk_container_forall (GtkContainer *container,
		      GtkCallback   callback,
		      gpointer      callback_data)
{
  GtkContainerClass *class;

  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (callback != NULL);

1262
  class = GTK_CONTAINER_GET_CLASS (container);
1263 1264 1265 1266 1267

  if (class->forall)
    class->forall (container, TRUE, callback, callback_data);
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
/**
 * gtk_container_foreach:
 * @container: a #GtkContainer
 * @callback: a callback
 * @callback_data: callback user data
 * 
 * Invokes @callback on each non-internal child of @container.  See
 * gtk_container_forall() for details on what constitutes an
 * "internal" child.  Most applications should use
 * gtk_container_foreach(), rather than gtk_container_forall().
 **/
Elliot Lee's avatar
Elliot Lee committed
1279 1280 1281 1282 1283
void
gtk_container_foreach (GtkContainer *container,
		       GtkCallback   callback,
		       gpointer      callback_data)
{
1284 1285
  GtkContainerClass *class;
  
1286 1287 1288
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (callback != NULL);

1289
  class = GTK_CONTAINER_GET_CLASS (container);
1290 1291 1292

  if (class->forall)
    class->forall (container, FALSE, callback, callback_data);
Elliot Lee's avatar
Elliot Lee committed
1293 1294
}

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
typedef struct _GtkForeachData	GtkForeachData;
struct _GtkForeachData
{
  GtkObject         *container;
  GtkCallbackMarshal callback;
  gpointer           callback_data;
};

static void
gtk_container_foreach_unmarshal (GtkWidget *child,
				 gpointer data)
{
  GtkForeachData *fdata = (GtkForeachData*) data;
  GtkArg args[2];
  
  /* first argument */
  args[0].name = NULL;
Manish Singh's avatar
Manish Singh committed
1312
  args[0].type = G_TYPE_FROM_INSTANCE (child);
1313
  GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1314 1315 1316
  
  /* location for return value */
  args[1].name = NULL;
Manish Singh's avatar
Manish Singh committed
1317
  args[1].type = G_TYPE_NONE;
1318 1319 1320 1321
  
  fdata->callback (fdata->container, fdata->callback_data, 1, args);
}

1322 1323 1324 1325 1326 1327 1328
void
gtk_container_foreach_full (GtkContainer       *container,
			    GtkCallback         callback,
			    GtkCallbackMarshal  marshal,
			    gpointer            callback_data,