gtkcontainer.c 78.4 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
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>

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

Elliot Lee's avatar
Elliot Lee committed
45 46 47 48

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

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

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

Tim Janik's avatar
Tim Janik committed
64 65

/* --- prototypes --- */
66
static void     gtk_container_base_class_init      (GtkContainerClass *klass);
Tim Janik's avatar
Tim Janik committed
67
static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
68 69 70
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
71 72 73 74 75 76 77 78
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);
79 80 81 82 83
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);
84
static gboolean gtk_container_focus                (GtkWidget         *widget,
85 86 87
						    GtkDirectionType   direction);
static void     gtk_container_real_set_focus_child (GtkContainer      *container,
						    GtkWidget         *widget);
88

89 90 91 92 93 94 95
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);
96 97
static gint     gtk_container_expose               (GtkWidget         *widget,
						    GdkEventExpose    *event);
98 99 100
static void     gtk_container_map                  (GtkWidget         *widget);
static void     gtk_container_unmap                (GtkWidget         *widget);

101 102 103
static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
							  GtkWidget    *child);

104 105
/* GtkBuildable */
static void gtk_container_buildable_init           (GtkBuildableIface *iface);
106
static void gtk_container_buildable_add_child      (GtkBuildable *buildable,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
						    GtkBuilder   *builder,
						    GObject      *child,
						    const gchar  *type);
static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
							  GtkBuilder    *builder,
							  GObject       *child,
							  const gchar   *tagname,
							  GMarkupParser *parser,
							  gpointer      *data);
static void    gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
						       GtkBuilder   *builder,
						       GObject      *child,
						       const gchar  *tagname,
						       gpointer     *data);

Elliot Lee's avatar
Elliot Lee committed
122

Tim Janik's avatar
Tim Janik committed
123
/* --- variables --- */
124
static const gchar           vadjustment_key[] = "gtk-vadjustment";
Tim Janik's avatar
Tim Janik committed
125
static guint                 vadjustment_key_id = 0;
126
static const gchar           hadjustment_key[] = "gtk-hadjustment";
Tim Janik's avatar
Tim Janik committed
127 128 129 130 131 132
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;
133
static GtkBuildableIface    *parent_buildable_iface;
Elliot Lee's avatar
Elliot Lee committed
134

135

Tim Janik's avatar
Tim Janik committed
136
/* --- functions --- */
Manish Singh's avatar
Manish Singh committed
137
GType
138
gtk_container_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
139
{
Manish Singh's avatar
Manish Singh committed
140
  static GType container_type = 0;
Elliot Lee's avatar
Elliot Lee committed
141 142 143

  if (!container_type)
    {
144
      const GTypeInfo container_info =
Manish Singh's avatar
Manish Singh committed
145
      {
Elliot Lee's avatar
Elliot Lee committed
146
	sizeof (GtkContainerClass),
Tim Janik's avatar
Tim Janik committed
147 148 149
	(GBaseInitFunc) gtk_container_base_class_init,
	(GBaseFinalizeFunc) gtk_container_base_class_finalize,
	(GClassInitFunc) gtk_container_class_init,
Manish Singh's avatar
Manish Singh committed
150
	NULL        /* class_finalize */,
Tim Janik's avatar
Tim Janik committed
151 152 153 154 155
	NULL        /* class_data */,
	sizeof (GtkContainer),
	0           /* n_preallocs */,
	(GInstanceInitFunc) gtk_container_init,
	NULL,       /* value_table */
Elliot Lee's avatar
Elliot Lee committed
156 157
      };

158
      const GInterfaceInfo buildable_info =
159 160 161 162 163 164
      {
	(GInterfaceInitFunc) gtk_container_buildable_init,
	NULL,
	NULL
      };

Manish Singh's avatar
Manish Singh committed
165
      container_type =
166
	g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"), 
Manish Singh's avatar
Manish Singh committed
167
				&container_info, G_TYPE_FLAG_ABSTRACT);
168 169 170 171 172

      g_type_add_interface_static (container_type,
				   GTK_TYPE_BUILDABLE,
				   &buildable_info);

Elliot Lee's avatar
Elliot Lee committed
173 174 175 176 177
    }

  return container_type;
}

178 179 180 181
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
182 183
  class->set_child_property = NULL;
  class->get_child_property = NULL;
184 185
}

Elliot Lee's avatar
Elliot Lee committed
186
static void
Tim Janik's avatar
Tim Janik committed
187
gtk_container_base_class_finalize (GtkContainerClass *class)
Elliot Lee's avatar
Elliot Lee committed
188
{
Tim Janik's avatar
Tim Janik committed
189
  GList *list, *node;
Elliot Lee's avatar
Elliot Lee committed
190

191
  list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
Tim Janik's avatar
Tim Janik committed
192 193 194
  for (node = list; node; node = node->next)
    {
      GParamSpec *pspec = node->data;
195

Tim Janik's avatar
Tim Janik committed
196 197 198 199 200 201 202 203 204 205 206 207 208
      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);
209

Tim Janik's avatar
Tim Janik committed
210
  parent_class = g_type_class_peek_parent (class);
211

212 213
  vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
  hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
214
  
Alexander Larsson's avatar
Alexander Larsson committed
215 216 217
  gobject_class->set_property = gtk_container_set_property;
  gobject_class->get_property = gtk_container_get_property;

Tim Janik's avatar
Tim Janik committed
218 219 220 221 222
  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;
223 224
  widget_class->map = gtk_container_map;
  widget_class->unmap = gtk_container_unmap;
Tim Janik's avatar
Tim Janik committed
225 226 227 228 229 230 231 232 233 234
  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
235 236
  g_object_class_install_property (gobject_class,
                                   PROP_RESIZE_MODE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
237
                                   g_param_spec_enum ("resize-mode",
238 239
                                                      P_("Resize mode"),
                                                      P_("Specify how resize events are handled"),
Alexander Larsson's avatar
Alexander Larsson committed
240 241
                                                      GTK_TYPE_RESIZE_MODE,
                                                      GTK_RESIZE_PARENT,
242
                                                      GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
243 244
  g_object_class_install_property (gobject_class,
                                   PROP_BORDER_WIDTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
245
                                   g_param_spec_uint ("border-width",
246 247
                                                      P_("Border width"),
                                                      P_("The width of the empty border outside the containers children"),
Alexander Larsson's avatar
Alexander Larsson committed
248
						      0,
249
						      65535,
Alexander Larsson's avatar
Alexander Larsson committed
250
						      0,
251
                                                      GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
252 253 254
  g_object_class_install_property (gobject_class,
                                   PROP_CHILD,
                                   g_param_spec_object ("child",
255 256
                                                      P_("Child"),
                                                      P_("Can be used to add a new child to the container"),
Alexander Larsson's avatar
Alexander Larsson committed
257
                                                      GTK_TYPE_WIDGET,
258
						      GTK_PARAM_WRITABLE));
Elliot Lee's avatar
Elliot Lee committed
259
  container_signals[ADD] =
260
    g_signal_new (I_("add"),
Manish Singh's avatar
Manish Singh committed
261 262 263 264 265 266 267
		  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
268
  container_signals[REMOVE] =
269
    g_signal_new (I_("remove"),
Manish Singh's avatar
Manish Singh committed
270 271 272 273 274 275 276
		  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);
277
  container_signals[CHECK_RESIZE] =
278
    g_signal_new (I_("check-resize"),
Manish Singh's avatar
Manish Singh committed
279 280 281 282 283 284
		  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);
285
  container_signals[SET_FOCUS_CHILD] =
286
    g_signal_new (I_("set-focus-child"),
Manish Singh's avatar
Manish Singh committed
287 288 289 290 291 292 293
		  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);
294 295
}

296 297 298 299
static void
gtk_container_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
300
  iface->add_child = gtk_container_buildable_add_child;
301 302 303 304 305
  iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
  iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
}

static void
306 307 308 309
gtk_container_buildable_add_child (GtkBuildable  *buildable,
				   GtkBuilder    *builder,
				   GObject       *child,
				   const gchar   *type)
310
{
311 312 313 314
  if (type)
    {
      GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
    }
315
  else if (GTK_IS_WIDGET (child) && GTK_WIDGET (child)->parent == NULL)
316 317 318 319 320 321
    {
      gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
    }
  else
    g_warning ("Cannot add an object of type %s to a container of type %s", 
	       g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
322 323 324 325
}

static void
gtk_container_buildable_set_child_property (GtkContainer *container,
326
					    GtkBuilder   *builder,
327 328 329 330 331 332
					    GtkWidget    *child,
					    gchar        *name,
					    const gchar  *value)
{
  GParamSpec *pspec;
  GValue gvalue = { 0, };
333 334
  GError *error = NULL;
  
335 336 337 338 339 340 341 342 343
  pspec = gtk_container_class_find_child_property
    (G_OBJECT_GET_CLASS (container), name);
  if (!pspec)
    {
      g_warning ("%s does not have a property called %s",
		 g_type_name (G_OBJECT_TYPE (container)), name);
      return;
    }

344
  if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
345
    {
346
      g_warning ("Could not read property %s:%s with value %s of type %s: %s",
347 348 349
		 g_type_name (G_OBJECT_TYPE (container)),
		 name,
		 value,
350 351 352
		 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
		 error->message);
      g_error_free (error);
353 354 355 356 357 358 359 360 361 362 363 364
      return;
    }

  gtk_container_child_set_property (container, child, name, &gvalue);
  g_value_unset (&gvalue);
}

typedef struct {
  GtkBuilder   *builder;
  GtkContainer *container;
  GtkWidget    *child;
  gchar        *child_prop_name;
365
  gchar        *context;
366
  gboolean     translatable;
367 368 369 370 371 372 373 374 375 376 377 378 379 380
} PackingPropertiesData;

static void
attributes_start_element (GMarkupParseContext *context,
			  const gchar         *element_name,
			  const gchar        **names,
			  const gchar        **values,
			  gpointer             user_data,
			  GError             **error)
{
  PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
  guint i;

  if (strcmp (element_name, "property") == 0)
381 382 383 384
    {
      for (i = 0; names[i]; i++)
	if (strcmp (names[i], "name") == 0)
	  parser_data->child_prop_name = g_strdup (values[i]);
385 386 387 388 389 390 391 392 393 394 395 396 397 398
	else if (strcmp (names[i], "translatable") == 0)
	  {
	    if (!_gtk_builder_boolean_from_string (values[1],
						   &parser_data->translatable,
						   error))
	      return;
	  }
	else if (strcmp (names[i], "comments") == 0)
	  ; /* for translators */
	else if (strcmp (names[i], "context") == 0)
	  parser_data->context = g_strdup (values[1]);
	else
	  g_warning ("Unsupported attribute for GtkContainer Child "
		     "property: %s\n", names[i]);
399
    }
400 401 402 403 404 405 406 407 408 409 410 411 412 413
  else if (strcmp (element_name, "packing") == 0)
    return;
  else
    g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
}

static void
attributes_text_element (GMarkupParseContext *context,
			 const gchar         *text,
			 gsize                text_len,
			 gpointer             user_data,
			 GError             **error)
{
  PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
414
  gchar* value;
415 416 417

  if (!parser_data->child_prop_name)
    return;
418 419 420 421 422 423 424 425 426 427 428 429 430 431
  
  if (parser_data->translatable && text_len)
    {
      const gchar* domain;
      domain = gtk_builder_get_translation_domain (parser_data->builder);
      
      value = _gtk_builder_parser_translate (domain,
					     parser_data->context,
					     text);
    }
  else
    {
      value = g_strdup (text);
    }
432 433

  gtk_container_buildable_set_child_property (parser_data->container,
434
					      parser_data->builder,
435 436
					      parser_data->child,
					      parser_data->child_prop_name,
437
					      value);
438 439

  g_free (parser_data->child_prop_name);
440 441
  g_free (parser_data->context);
  g_free (value);
442
  parser_data->child_prop_name = NULL;
443 444
  parser_data->context = NULL;
  parser_data->translatable = FALSE;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
}

static const GMarkupParser attributes_parser =
  {
    attributes_start_element,
    NULL,
    attributes_text_element,
  };

static gboolean
gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
					  GtkBuilder    *builder,
					  GObject       *child,
					  const gchar   *tagname,
					  GMarkupParser *parser,
					  gpointer      *data)
{
  PackingPropertiesData *parser_data;

  if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
						tagname, parser, data))
    return TRUE;

  if (child && strcmp (tagname, "packing") == 0)
    {
      parser_data = g_slice_new0 (PackingPropertiesData);
      parser_data->builder = builder;
      parser_data->container = GTK_CONTAINER (buildable);
      parser_data->child = GTK_WIDGET (child);
      parser_data->child_prop_name = NULL;

      *parser = attributes_parser;
      *data = parser_data;
      return TRUE;
    }

  return FALSE;
}

static void
gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
					GtkBuilder   *builder,
					GObject      *child,
					const gchar  *tagname,
					gpointer     *data)
{
  if (strcmp (tagname, "packing") == 0)
    {
      g_slice_free (PackingPropertiesData, (gpointer)data);
      return;

    }

  if (parent_buildable_iface->custom_tag_end)
    parent_buildable_iface->custom_tag_end (buildable, builder,
					    child, tagname, data);

}

Matthias Clasen's avatar
Matthias Clasen committed
504 505
/**
 * gtk_container_child_type: 
506
 * @container: a #GtkContainer
Matthias Clasen's avatar
Matthias Clasen committed
507 508 509
 *
 * Returns the type of the children supported by the container.
 *
Manish Singh's avatar
Manish Singh committed
510
 * Note that this may return %G_TYPE_NONE to indicate that no more
Matthias Clasen's avatar
Matthias Clasen committed
511 512 513
 * children can be added, e.g. for a #GtkPaned which already has two 
 * children.
 *
Manish Singh's avatar
Manish Singh committed
514
 * Return value: a #GType.
Matthias Clasen's avatar
Matthias Clasen committed
515
 **/
Manish Singh's avatar
Manish Singh committed
516
GType
Tim Janik's avatar
Tim Janik committed
517
gtk_container_child_type (GtkContainer *container)
518
{
Manish Singh's avatar
Manish Singh committed
519
  GType slot;
520 521 522 523
  GtkContainerClass *class;

  g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);

524
  class = GTK_CONTAINER_GET_CLASS (container);
525 526
  if (class->child_type)
    slot = class->child_type (container);
527
  else
Manish Singh's avatar
Manish Singh committed
528
    slot = G_TYPE_NONE;
529 530 531 532

  return slot;
}

Tim Janik's avatar
Tim Janik committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/* --- 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);
}
580

Matthias Clasen's avatar
Matthias Clasen committed
581 582 583 584
/**
 * gtk_container_child_get_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
585
 * @first_property_name: the name of the first property to get
586 587
 * @var_args: return location for the first property, followed 
 *     optionally by more name/return location pairs, followed by %NULL
Matthias Clasen's avatar
Matthias Clasen committed
588 589 590
 * 
 * Gets the values of one or more child properties for @child and @container.
 **/
591
void
Tim Janik's avatar
Tim Janik committed
592 593 594 595
gtk_container_child_get_valist (GtkContainer *container,
				GtkWidget    *child,
				const gchar  *first_property_name,
				va_list       var_args)
596
{
Tim Janik's avatar
Tim Janik committed
597 598
  const gchar *name;

599
  g_return_if_fail (GTK_IS_CONTAINER (container));
Tim Janik's avatar
Tim Janik committed
600 601
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == GTK_WIDGET (container));
602

Tim Janik's avatar
Tim Janik committed
603 604
  g_object_ref (container);
  g_object_ref (child);
605

Tim Janik's avatar
Tim Janik committed
606 607
  name = first_property_name;
  while (name)
608
    {
Tim Janik's avatar
Tim Janik committed
609 610
      GValue value = { 0, };
      GParamSpec *pspec;
611 612
      gchar *error;

Tim Janik's avatar
Tim Janik committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
      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);
636 637
      if (error)
	{
Tim Janik's avatar
Tim Janik committed
638
	  g_warning ("%s: %s", G_STRLOC, error);
639
	  g_free (error);
Tim Janik's avatar
Tim Janik committed
640 641
	  g_value_unset (&value);
	  break;
642
	}
Tim Janik's avatar
Tim Janik committed
643 644 645 646 647 648 649 650
      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
651 652 653 654 655 656 657 658 659
/**
 * 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.
 **/
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
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
701
	  g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
		     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
725 726 727 728
/**
 * gtk_container_child_set_valist:
 * @container: a #GtkContainer
 * @child: a widget which is a child of @container
729 730
 * @first_property_name: the name of the first property to set
 * @var_args: a %NULL-terminated list of property names and values, starting
731
 *           with @first_prop_name
Matthias Clasen's avatar
Matthias Clasen committed
732 733 734
 * 
 * Sets one or more child properties for @child and @container.
 **/
Tim Janik's avatar
Tim Janik committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
void
gtk_container_child_set_valist (GtkContainer *container,
				GtkWidget    *child,
				const gchar  *first_property_name,
				va_list       var_args)
{
  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);

  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))
770
	{
Tim Janik's avatar
Tim Janik committed
771 772 773 774 775
	  g_warning ("%s: child property `%s' of container class `%s' is not writable",
		     G_STRLOC,
		     pspec->name,
		     G_OBJECT_TYPE_NAME (container));
	  break;
776
	}
Tim Janik's avatar
Tim Janik committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
      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*);
792
    }
Tim Janik's avatar
Tim Janik committed
793
  g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
794

Tim Janik's avatar
Tim Janik committed
795 796
  g_object_unref (container);
  g_object_unref (child);
797 798
}

Matthias Clasen's avatar
Matthias Clasen committed
799 800 801 802 803 804 805 806 807
/**
 * 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.
 **/
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
void
gtk_container_child_set_property (GtkContainer *container,
				  GtkWidget    *child,
				  const gchar  *property_name,
				  const GValue *value)
{
  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);

  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
848 849 850 851 852 853
/**
 * 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
854
 *           with @first_prop_name
Matthias Clasen's avatar
Matthias Clasen committed
855 856 857 858
 * 
 * Adds @widget to @container, setting child properties at the same time.
 * See gtk_container_add() and gtk_container_child_set() for more details.
 **/
859
void
Tim Janik's avatar
Tim Janik committed
860 861 862 863
gtk_container_add_with_properties (GtkContainer *container,
				   GtkWidget    *widget,
				   const gchar  *first_prop_name,
				   ...)
864
{
865 866 867
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (widget->parent == NULL);
868

Manish Singh's avatar
Manish Singh committed
869 870
  g_object_ref (container);
  g_object_ref (widget);
Tim Janik's avatar
Tim Janik committed
871
  gtk_widget_freeze_child_notify (widget);
872

Manish Singh's avatar
Manish Singh committed
873
  g_signal_emit (container, container_signals[ADD], 0, widget);
874
  if (widget->parent)
875
    {
Tim Janik's avatar
Tim Janik committed
876
      va_list var_args;
877

Tim Janik's avatar
Tim Janik committed
878 879 880
      va_start (var_args, first_prop_name);
      gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
      va_end (var_args);
881 882
    }

Tim Janik's avatar
Tim Janik committed
883
  gtk_widget_thaw_child_notify (widget);
Manish Singh's avatar
Manish Singh committed
884 885
  g_object_unref (widget);
  g_object_unref (container);
886 887
}

Matthias Clasen's avatar
Matthias Clasen committed
888 889 890 891 892 893
/**
 * 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
894
 *           with @first_prop_name
Matthias Clasen's avatar
Matthias Clasen committed
895 896 897
 * 
 * Sets one or more child properties for @child and @container.
 **/
898
void
899 900
gtk_container_child_set (GtkContainer      *container,
			 GtkWidget         *child,
Tim Janik's avatar
Tim Janik committed
901
			 const gchar       *first_prop_name,
902
			 ...)
903
{
904 905
  va_list var_args;
  
906 907
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
Tim Janik's avatar
Tim Janik committed
908
  g_return_if_fail (child->parent == GTK_WIDGET (container));
909

Tim Janik's avatar
Tim Janik committed
910 911 912
  va_start (var_args, first_prop_name);
  gtk_container_child_set_valist (container, child, first_prop_name, var_args);
  va_end (var_args);
913 914
}

Matthias Clasen's avatar
Matthias Clasen committed
915 916 917 918 919
/**
 * 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
920 921
 * @Varargs: return location for the first property, followed 
 *     optionally by more name/return location pairs, followed by %NULL
Matthias Clasen's avatar
Matthias Clasen committed
922 923 924
 * 
 * Gets the values of one or more child properties for @child and @container.
 **/
925
void
Tim Janik's avatar
Tim Janik committed
926 927 928 929
gtk_container_child_get (GtkContainer      *container,
			 GtkWidget         *child,
			 const gchar       *first_prop_name,
			 ...)
930
{
Tim Janik's avatar
Tim Janik committed
931
  va_list var_args;
932 933 934
  
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (child));
Tim Janik's avatar
Tim Janik committed
935 936 937 938 939
  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);
940 941
}

Matthias Clasen's avatar
Matthias Clasen committed
942 943 944 945 946 947 948 949
/**
 * 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. 
 **/
950
void
951
gtk_container_class_install_child_property (GtkContainerClass *cclass,
Tim Janik's avatar
Tim Janik committed
952 953
					    guint              property_id,
					    GParamSpec        *pspec)
954
{
955
  g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
Tim Janik's avatar
Tim Janik committed
956 957
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
  if (pspec->flags & G_PARAM_WRITABLE)
958
    g_return_if_fail (cclass->set_child_property != NULL);
Tim Janik's avatar
Tim Janik committed
959
  if (pspec->flags & G_PARAM_READABLE)
960
    g_return_if_fail (cclass->get_child_property != NULL);
Tim Janik's avatar
Tim Janik committed
961 962 963 964 965
  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);

966
  if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
967
    {
Matthias Clasen's avatar
Matthias Clasen committed
968
      g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
969
		 G_OBJECT_CLASS_NAME (cclass),
Tim Janik's avatar
Tim Janik committed
970
		 pspec->name);
971
      return;
972
    }
Tim Janik's avatar
Tim Janik committed
973 974 975
  g_param_spec_ref (pspec);
  g_param_spec_sink (pspec);
  PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
976
  g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
977 978
}

979 980
/**
 * gtk_container_class_find_child_property:
981
 * @cclass: a #GtkContainerClass
982
 * @property_name: the name of the child property to find
983
 * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
984 985 986 987
 *   child property with that name.
 *
 * Finds a child property of a container class by name.
 */
Tim Janik's avatar
Tim Janik committed
988
GParamSpec*
989
gtk_container_class_find_child_property (GObjectClass *cclass,
Tim Janik's avatar
Tim Janik committed
990
					 const gchar  *property_name)
991
{
992
  g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
Tim Janik's avatar
Tim Janik committed
993
  g_return_val_if_fail (property_name != NULL, NULL);
994

Tim Janik's avatar
Tim Janik committed
995 996
  return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
				   property_name,
997
				   G_OBJECT_CLASS_TYPE (cclass),
Tim Janik's avatar
Tim Janik committed
998
				   TRUE);
999
}
1000

1001 1002
/**
 * gtk_container_class_list_child_properties:
1003
 * @cclass: a #GtkContainerClass
1004
 * @n_properties: location to return the number of child properties found
1005 1006
 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*. 
 *           The array must be freed with g_free().
1007 1008 1009 1010
 *
 * Returns all child properties of a container class.
 */
GParamSpec**
1011
gtk_container_class_list_child_properties (GObjectClass *cclass,
Tim Janik's avatar
Tim Janik committed
1012
					   guint        *n_properties)
1013
{
Tim Janik's avatar
Tim Janik committed
1014 1015
  GParamSpec **pspecs;
  guint n;
1016

1017
  g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1018

Tim Janik's avatar
Tim Janik committed
1019
  pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1020
				   G_OBJECT_CLASS_TYPE (cclass),
Tim Janik's avatar
Tim Janik committed
1021 1022 1023
				   &n);
  if (n_properties)
    *n_properties = n;
Elliot Lee's avatar
Elliot Lee committed
1024

Tim Janik's avatar
Tim Janik committed
1025 1026
  return pspecs;
}
1027

1028 1029 1030 1031
static void
gtk_container_add_unimplemented (GtkContainer     *container,
				 GtkWidget        *widget)
{
Manish Singh's avatar
Manish Singh committed
1032
  g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1033 1034 1035 1036 1037 1038
}

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

Elliot Lee's avatar
Elliot Lee committed
1042 1043 1044 1045 1046 1047
static void
gtk_container_init (GtkContainer *container)
{
  container->focus_child = NULL;
  container->border_width = 0;
  container->need_resize = FALSE;
1048
  container->resize_mode = GTK_RESIZE_PARENT;
1049
  container->reallocate_redraws = FALSE;
1050 1051 1052 1053 1054
}

static void
gtk_container_destroy (GtkObject *object)
{
1055
  GtkContainer *container = GTK_CONTAINER (object);
1056

1057
  if (GTK_CONTAINER_RESIZE_PENDING (container))
1058
    _gtk_container_dequeue_resize_handler (container);
1059 1060 1061 1062 1063 1064

  /* 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);
1065

1066
  gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1067 1068

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
Elliot Lee's avatar
Elliot Lee committed
1069 1070 1071
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
1072 1073 1074 1075
gtk_container_set_property (GObject         *object,
			    guint            prop_id,
			    const GValue    *value,
			    GParamSpec      *pspec)
Elliot Lee's avatar
Elliot Lee committed
1076
{
1077
  GtkContainer *container = GTK_CONTAINER (object);
1078

Alexander Larsson's avatar
Alexander Larsson committed
1079
  switch (prop_id)
Elliot Lee's avatar
Elliot Lee committed
1080
    {
Alexander Larsson's avatar
Alexander Larsson committed
1081 1082
    case PROP_BORDER_WIDTH:
      gtk_container_set_border_width (container, g_value_get_uint (value));
1083
      break;
Alexander Larsson's avatar
Alexander Larsson committed
1084 1085
    case PROP_RESIZE_MODE:
      gtk_container_set_resize_mode (container, g_value_get_enum (value));
1086
      break;
Alexander Larsson's avatar
Alexander Larsson committed
1087 1088
    case PROP_CHILD:
      gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1089
      break;
Tim Janik's avatar
Tim Janik committed
1090
    default:
Alexander Larsson's avatar
Alexander Larsson committed
1091
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
1092
      break;
1093 1094 1095 1096
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
1097 1098 1099 1100
gtk_container_get_property (GObject         *object,
			    guint            prop_id,
			    GValue          *value,
			    GParamSpec      *pspec)
1101
{
1102
  GtkContainer *container = GTK_CONTAINER (object);
1103
  
Alexander Larsson's avatar
Alexander Larsson committed
1104
  switch (prop_id)
1105
    {
Alexander Larsson's avatar
Alexander Larsson committed
1106 1107
    case PROP_BORDER_WIDTH:
      g_value_set_uint (value, container->border_width);
1108
      break;
Alexander Larsson's avatar
Alexander Larsson committed
1109 1110
    case PROP_RESIZE_MODE:
      g_value_set_enum (value, container->resize_mode);
1111
      break;
1112
    default:
Alexander Larsson's avatar
Alexander Larsson committed
1113
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
1114
      break;
Elliot Lee's avatar
Elliot Lee committed
1115 1116 1117
    }
}

1118 1119 1120
/**
 * gtk_container_set_border_width:
 * @container: a #GtkContainer
1121 1122
 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> 
 *   the container. Valid values are in the range 0-65535 pixels.
1123
 *
Matthias Clasen's avatar
Matthias Clasen committed
1124 1125
 * Sets the border width of the container.
 *
1126 1127 1128 1129 1130
 * 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
1131 1132 1133
 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
 * to give it a size, and place it on the side of the container as
 * a spacer.
1134
 **/
Elliot Lee's avatar
Elliot Lee committed
1135
void
1136 1137
gtk_container_set_border_width (GtkContainer *container,
				guint         border_width)
Elliot Lee's avatar
Elliot Lee committed
1138 1139 1140 1141 1142 1143
{
  g_return_if_fail (GTK_IS_CONTAINER (container));

  if (container->border_width != border_width)
    {
      container->border_width = border_width;
1144
      g_object_notify (G_OBJECT (container), "border-width");
Alexander Larsson's avatar
Alexander Larsson committed
1145
      
Tim Janik's avatar
Tim Janik committed
1146 1147
      if (GTK_WIDGET_REALIZED (container))
	gtk_widget_queue_resize (GTK_WIDGET (container));
Elliot Lee's avatar
Elliot Lee committed
1148 1149 1150
    }
}

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
/**
 * 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;
}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
/**
 * 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
1182 1183 1184 1185
void
gtk_container_add (GtkContainer *container,
		   GtkWidget    *widget)
{
1186 1187
  g_return_if_fail (GTK_IS_CONTAINER (container));
  g_return_if_fail (GTK_IS_WIDGET (widget));
1188 1189 1190 1191

  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
1192
                 "type %s, but the widget is already inside a container of type %s, "
1193 1194
                 "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
                 "explains how to reparent a widget.",
Tim Janik's avatar
Tim Janik committed
1195 1196 1197
                 g_type_name (G_OBJECT_TYPE (widget)),
                 g_type_name (G_OBJECT_TYPE (container)),
                 g_type_name (G_OBJECT_TYPE (widget->parent)));
1198 1199
      return;
    }
1200

Manish Singh's avatar
Manish Singh committed
1201
  g_signal_emit (container, container_signals[ADD], 0, widget);
Elliot Lee's avatar
Elliot Lee committed
1202 1203
}

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
/**
 * 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
1214 1215 1216 1217
 * 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.
1218
 **/
Elliot Lee's avatar
Elliot Lee committed
1219 1220 1221 1222 1223
void
gtk_container_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
1224
  g_return_if_fail (GTK_IS_WIDGET (widget));
1225 1226 1227 1228 1229 1230 1231

  /* 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));
1232
  
Manish Singh's avatar
Manish Singh committed
1233
  g_signal_emit (container, container_signals[REMOVE], 0, widget);
Elliot Lee's avatar
Elliot Lee committed
1234 1235
}

1236
void
1237
_gtk_container_dequeue_resize_handler (GtkContainer *container)
1238 1239 1240 1241 1242 1243 1244 1245
{
  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
1246 1247
/**
 * gtk_container_set_resize_mode:
1248 1249
 * @container: a #GtkContainer
 * @resize_mode: the new resize mode
Matthias Clasen's avatar
Matthias Clasen committed
1250 1251 1252 1253 1254 1255 1256
 * 
 * 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
1257
void
1258 1259
gtk_container_set_resize_mode (GtkContainer  *container,
			       GtkResizeMode  resize_mode)
Elliot Lee's avatar
Elliot Lee committed
1260 1261
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
1262 1263
  g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
  
1264
  if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1265
      resize_mode == GTK_RESIZE_PARENT)
Alexander Larsson's avatar
Alexander Larsson committed
1266 1267 1268
    {
      resize_mode = GTK_RESIZE_QUEUE;
    }
1269 1270
  
  if (container->resize_mode != resize_mode)
1271
    {
1272 1273
      container->resize_mode = resize_mode;
      
1274
      gtk_widget_queue_resize (GTK_WIDGET (container));
1275
      g_object_notify (G_OBJECT (container), "resize-mode");
1276 1277 1278
    }
}

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
/**
 * 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
1296 1297
/**
 * gtk_container_set_reallocate_redraws:
1298 1299
 * @container: a #GtkContainer
 * @needs_redraws: the new value for the container's @reallocate_redraws flag
Matthias Clasen's avatar
Matthias Clasen committed
1300 1301 1302 1303 1304 1305
 *
 * 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. 
 **/ 
1306 1307 1308 1309 1310 1311
void
gtk_container_set_reallocate_redraws (GtkContainer *container,
				      gboolean      needs_redraws)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));

1312
  container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1313 1314
}

1315 1316 1317
static GtkContainer*
gtk_container_get_resize_container (GtkContainer *container)
{
1318
  GtkWidget *widget = GTK_WIDGET (container);
1319 1320 1321 1322

  while (widget->parent)
    {
      widget = widget->parent;
1323
      if (GTK_IS_RESIZE_CONTAINER (widget))
1324 1325 1326 1327 1328 1329 1330 1331 1332
	break;
    }

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

static gboolean
gtk_container_idle_sizer (gpointer data)
{
1333 1334 1335 1336 1337 1338 1339
  /* 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)
1340
    {
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
      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));
1351
    }
1352

1353 1354
  gdk_window_process_all_updates ();

1355 1356 1357 1358
  return FALSE;
}

void
1359
_gtk_container_queue_resize (GtkContainer *container)
1360 1361
{
  GtkContainer *resize_container;
1362
  GtkWidget *widget;
1363 1364 1365
  
  g_return_if_fail (GTK_IS_CONTAINER (container));

1366
  widget = GTK_WIDGET (container);
1367
  resize_container = gtk_container_get_resize_container (container);
1368
  
1369
  while (TRUE)
1370
    {
1371 1372 1373 1374 1375
      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;
1376
      
1377 1378
      widget = widget->parent;
    }
1379
      
1380 1381
  if (resize_container)
    {
1382
      if (GTK_WIDGET_VISIBLE (resize_container) &&
1383
 	  (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) || GTK_WIDGET_REALIZED (resize_container)))
1384
	{
1385
	  switch (resize_container->resize_mode)
1386
	    {
1387 1388 1389 1390 1391
	    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)
1392
		    gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1393 1394
				     gtk_container_idle_sizer,
				     NULL, NULL);
1395 1396 1397 1398 1399 1400 1401 1402 1403
		  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:
1404
	      g_assert_not_reached ();
1405
	      break;
1406
	    }
1407
	}
1408 1409
      else
	{
1410 1411 1412
	  /* we need to let hidden resize containers know that something
	   * changed while they where hidden (currently only evaluated by
	   * toplevels).
1413
	   */
1414
	  resize_container->need_resize = TRUE;
1415
	}
1416
    }
Elliot Lee's avatar
Elliot Lee committed
1417 1418 1419
}

void
1420 1421 1422
gtk_container_check_resize (GtkContainer *container)
{
  g_return_if_fail (GTK_IS_CONTAINER (container));
1423
  
Manish Singh's avatar
Manish Singh committed
1424
  g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1425 1426 1427 1428
}

static void
gtk_container_real_check_resize (GtkContainer *container)
Elliot Lee's avatar
Elliot Lee committed
1429
{
1430
  GtkWidget *widget = GTK_WIDGET (container);
1431
  GtkRequisition requisition;
1432
  
1433
  gtk_widget_size_request (widget, &requisition);
1434
  
1435 1436
  if (requisition.width > widget->allocation.width ||
      requisition.height > widget->allocation.height)
1437
    {
1438
      if (GTK_IS_RESIZE_CONTAINER (container))
1439 1440
	gtk_widget_size_allocate (GTK_WIDGET (container),
				  &GTK_WIDGET (container)->allocation);
1441 1442
      else
	gtk_widget_queue_resize (widget);
1443 1444 1445 1446 1447
    }
  else
    {
      gtk_container_resize_children (container);
    }
Elliot Lee's avatar
Elliot Lee committed
1448 1449
}

1450
/* The container hasn't changed size but one of its children
1451 1452 1453
 *  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,
1454 1455
 *  so we simply need to reallocate and let the allocation
 *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1456 1457 1458
 */
void
gtk_container_resize_children (GtkContainer *container)
Elliot Lee's avatar
Elliot Lee committed
1459
{