gtktable.c 43.2 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
 * 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
11
 * 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"
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtktable.h"
29
#include "gtkprivate.h"
Alexander Larsson's avatar
Alexander Larsson committed
30
#include "gtkintl.h"
31
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
32

33 34
enum
{
Alexander Larsson's avatar
Alexander Larsson committed
35 36 37 38 39 40
  PROP_0,
  PROP_N_ROWS,
  PROP_N_COLUMNS,
  PROP_COLUMN_SPACING,
  PROP_ROW_SPACING,
  PROP_HOMOGENEOUS
41 42 43 44
};

enum
{
Tim Janik's avatar
Tim Janik committed
45 46 47 48 49 50 51 52 53
  CHILD_PROP_0,
  CHILD_PROP_LEFT_ATTACH,
  CHILD_PROP_RIGHT_ATTACH,
  CHILD_PROP_TOP_ATTACH,
  CHILD_PROP_BOTTOM_ATTACH,
  CHILD_PROP_X_OPTIONS,
  CHILD_PROP_Y_OPTIONS,
  CHILD_PROP_X_PADDING,
  CHILD_PROP_Y_PADDING
54 55
};
  
Elliot Lee's avatar
Elliot Lee committed
56

57
static void gtk_table_finalize	    (GObject	    *object);
58
static void gtk_table_size_request  (GtkWidget	    *widget,
Elliot Lee's avatar
Elliot Lee committed
59
				     GtkRequisition *requisition);
60
static void gtk_table_size_allocate (GtkWidget	    *widget,
Elliot Lee's avatar
Elliot Lee committed
61
				     GtkAllocation  *allocation);
62 63 64 65
static void gtk_table_add	    (GtkContainer   *container,
				     GtkWidget	    *widget);
static void gtk_table_remove	    (GtkContainer   *container,
				     GtkWidget	    *widget);
66 67
static void gtk_table_forall	    (GtkContainer   *container,
				     gboolean	     include_internals,
Elliot Lee's avatar
Elliot Lee committed
68
				     GtkCallback     callback,
69
				     gpointer	     callback_data);
Alexander Larsson's avatar
Alexander Larsson committed
70 71 72 73 74 75 76 77
static void gtk_table_get_property  (GObject         *object,
				     guint            prop_id,
				     GValue          *value,
				     GParamSpec      *pspec);
static void gtk_table_set_property  (GObject         *object,
				     guint            prop_id,
				     const GValue    *value,
				     GParamSpec      *pspec);
Tim Janik's avatar
Tim Janik committed
78 79 80 81 82 83 84 85 86 87
static void gtk_table_set_child_property (GtkContainer    *container,
					  GtkWidget       *child,
					  guint            property_id,
					  const GValue    *value,
					  GParamSpec      *pspec);
static void gtk_table_get_child_property (GtkContainer    *container,
					  GtkWidget       *child,
					  guint            property_id,
					  GValue          *value,
					  GParamSpec      *pspec);
Manish Singh's avatar
Manish Singh committed
88
static GType gtk_table_child_type   (GtkContainer   *container);
89 90 91


static void gtk_table_size_request_init	 (GtkTable *table);
Elliot Lee's avatar
Elliot Lee committed
92 93 94 95 96 97 98 99 100
static void gtk_table_size_request_pass1 (GtkTable *table);
static void gtk_table_size_request_pass2 (GtkTable *table);
static void gtk_table_size_request_pass3 (GtkTable *table);

static void gtk_table_size_allocate_init  (GtkTable *table);
static void gtk_table_size_allocate_pass1 (GtkTable *table);
static void gtk_table_size_allocate_pass2 (GtkTable *table);


Matthias Clasen's avatar
Matthias Clasen committed
101
G_DEFINE_TYPE (GtkTable, gtk_table, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
102 103 104 105

static void
gtk_table_class_init (GtkTableClass *class)
{
106
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Tim Janik's avatar
Tim Janik committed
107 108
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
109
  
110
  gobject_class->finalize = gtk_table_finalize;
Elliot Lee's avatar
Elliot Lee committed
111

Alexander Larsson's avatar
Alexander Larsson committed
112 113
  gobject_class->get_property = gtk_table_get_property;
  gobject_class->set_property = gtk_table_set_property;
114
  
Elliot Lee's avatar
Elliot Lee committed
115 116
  widget_class->size_request = gtk_table_size_request;
  widget_class->size_allocate = gtk_table_size_allocate;
117
  
Elliot Lee's avatar
Elliot Lee committed
118 119
  container_class->add = gtk_table_add;
  container_class->remove = gtk_table_remove;
120
  container_class->forall = gtk_table_forall;
121
  container_class->child_type = gtk_table_child_type;
Tim Janik's avatar
Tim Janik committed
122 123
  container_class->set_child_property = gtk_table_set_child_property;
  container_class->get_child_property = gtk_table_get_child_property;
124
  
Alexander Larsson's avatar
Alexander Larsson committed
125 126 127

  g_object_class_install_property (gobject_class,
                                   PROP_N_ROWS,
128
                                   g_param_spec_uint ("n-rows",
129 130
						     P_("Rows"),
						     P_("The number of rows in the table"),
131
						     1,
132
						     65535,
133
						     1,
134
						     GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
135 136
  g_object_class_install_property (gobject_class,
                                   PROP_N_COLUMNS,
137
                                   g_param_spec_uint ("n-columns",
138 139
						     P_("Columns"),
						     P_("The number of columns in the table"),
140
						     1,
141
						     65535,
142
						     1,
143
						     GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
144 145
  g_object_class_install_property (gobject_class,
                                   PROP_ROW_SPACING,
146
                                   g_param_spec_uint ("row-spacing",
147 148
						     P_("Row spacing"),
						     P_("The amount of space between two consecutive rows"),
Alexander Larsson's avatar
Alexander Larsson committed
149
						     0,
150
						     65535,
Alexander Larsson's avatar
Alexander Larsson committed
151
						     0,
152
						     GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
153 154
  g_object_class_install_property (gobject_class,
                                   PROP_COLUMN_SPACING,
155
                                   g_param_spec_uint ("column-spacing",
156 157
						     P_("Column spacing"),
						     P_("The amount of space between two consecutive columns"),
Alexander Larsson's avatar
Alexander Larsson committed
158
						     0,
159
						     65535,
Alexander Larsson's avatar
Alexander Larsson committed
160
						     0,
161
						     GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
162 163 164
  g_object_class_install_property (gobject_class,
                                   PROP_HOMOGENEOUS,
                                   g_param_spec_boolean ("homogeneous",
165 166
							 P_("Homogeneous"),
							 P_("If TRUE, the table cells are all the same width/height"),
Alexander Larsson's avatar
Alexander Larsson committed
167
							 FALSE,
168
							 GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
169

Tim Janik's avatar
Tim Janik committed
170 171
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_LEFT_ATTACH,
172
					      g_param_spec_uint ("left-attach", 
173 174
								 P_("Left attachment"), 
								 P_("The column number to attach the left side of the child to"),
Tim Janik's avatar
Tim Janik committed
175
								 0, 65535, 0,
176
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
177 178
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_RIGHT_ATTACH,
179
					      g_param_spec_uint ("right-attach", 
180 181
								 P_("Right attachment"), 
								 P_("The column number to attach the right side of a child widget to"),
Tim Janik's avatar
Tim Janik committed
182
								 1, 65535, 1,
183
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
184 185
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_TOP_ATTACH,
186
					      g_param_spec_uint ("top-attach", 
187 188
								 P_("Top attachment"), 
								 P_("The row number to attach the top of a child widget to"),
Tim Janik's avatar
Tim Janik committed
189
								 0, 65535, 0,
190
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
191 192
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_BOTTOM_ATTACH,
193
					      g_param_spec_uint ("bottom-attach",
194 195
								 P_("Bottom attachment"), 
								 P_("The row number to attach the bottom of the child to"),
Tim Janik's avatar
Tim Janik committed
196
								 1, 65535, 1,
197
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
198 199
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_X_OPTIONS,
200
					      g_param_spec_flags ("x-options", 
201 202
								  P_("Horizontal options"), 
								  P_("Options specifying the horizontal behaviour of the child"),
Tim Janik's avatar
Tim Janik committed
203
								  GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
204
								  GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
205 206
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_Y_OPTIONS,
207
					      g_param_spec_flags ("y-options", 
208 209
								  P_("Vertical options"), 
								  P_("Options specifying the vertical behaviour of the child"),
Tim Janik's avatar
Tim Janik committed
210
								  GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
211
								  GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
212 213
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_X_PADDING,
214
					      g_param_spec_uint ("x-padding", 
215 216
								 P_("Horizontal padding"), 
								 P_("Extra space to put between the child and its left and right neighbors, in pixels"),
Tim Janik's avatar
Tim Janik committed
217
								 0, 65535, 0,
218
								 GTK_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
219 220
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_Y_PADDING,
221
					      g_param_spec_uint ("y-padding", 
222 223
								 P_("Vertical padding"), 
								 P_("Extra space to put between the child and its upper and lower neighbors, in pixels"),
Tim Janik's avatar
Tim Janik committed
224
								 0, 65535, 0,
225
								 GTK_PARAM_READWRITE));
226 227
}

Manish Singh's avatar
Manish Singh committed
228
static GType
229 230 231
gtk_table_child_type (GtkContainer   *container)
{
  return GTK_TYPE_WIDGET;
Elliot Lee's avatar
Elliot Lee committed
232 233 234
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
235 236 237 238
gtk_table_get_property (GObject      *object,
			guint         prop_id,
			GValue       *value,
			GParamSpec   *pspec)
Elliot Lee's avatar
Elliot Lee committed
239
{
240
  GtkTable *table;
Elliot Lee's avatar
Elliot Lee committed
241

242 243
  table = GTK_TABLE (object);

Alexander Larsson's avatar
Alexander Larsson committed
244
  switch (prop_id)
245
    {
Alexander Larsson's avatar
Alexander Larsson committed
246 247
    case PROP_N_ROWS:
      g_value_set_uint (value, table->nrows);
248
      break;
Alexander Larsson's avatar
Alexander Larsson committed
249 250
    case PROP_N_COLUMNS:
      g_value_set_uint (value, table->ncols);
251
      break;
Alexander Larsson's avatar
Alexander Larsson committed
252 253
    case PROP_ROW_SPACING:
      g_value_set_uint (value, table->row_spacing);
254
      break;
Alexander Larsson's avatar
Alexander Larsson committed
255 256
    case PROP_COLUMN_SPACING:
      g_value_set_uint (value, table->column_spacing);
257
      break;
Alexander Larsson's avatar
Alexander Larsson committed
258 259
    case PROP_HOMOGENEOUS:
      g_value_set_boolean (value, table->homogeneous);
260 261
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
262
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 264
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
265 266
}

267
static void
Alexander Larsson's avatar
Alexander Larsson committed
268 269 270 271
gtk_table_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
Elliot Lee's avatar
Elliot Lee committed
272
{
273 274 275
  GtkTable *table;

  table = GTK_TABLE (object);
Elliot Lee's avatar
Elliot Lee committed
276

Alexander Larsson's avatar
Alexander Larsson committed
277
  switch (prop_id)
Elliot Lee's avatar
Elliot Lee committed
278
    {
Alexander Larsson's avatar
Alexander Larsson committed
279 280
    case PROP_N_ROWS:
      gtk_table_resize (table, g_value_get_uint (value), table->ncols);
281
      break;
Alexander Larsson's avatar
Alexander Larsson committed
282 283
    case PROP_N_COLUMNS:
      gtk_table_resize (table, table->nrows, g_value_get_uint (value));
284
      break;
Alexander Larsson's avatar
Alexander Larsson committed
285 286
    case PROP_ROW_SPACING:
      gtk_table_set_row_spacings (table, g_value_get_uint (value));
287
      break;
Alexander Larsson's avatar
Alexander Larsson committed
288 289
    case PROP_COLUMN_SPACING:
      gtk_table_set_col_spacings (table, g_value_get_uint (value));
290
      break;
Alexander Larsson's avatar
Alexander Larsson committed
291 292
    case PROP_HOMOGENEOUS:
      gtk_table_set_homogeneous (table, g_value_get_boolean (value));
293 294
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
295
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296
      break;
Elliot Lee's avatar
Elliot Lee committed
297
    }
298
}
Elliot Lee's avatar
Elliot Lee committed
299

300
static void
Tim Janik's avatar
Tim Janik committed
301 302 303 304 305
gtk_table_set_child_property (GtkContainer    *container,
			      GtkWidget       *child,
			      guint            property_id,
			      const GValue    *value,
			      GParamSpec      *pspec)
306
{
Tim Janik's avatar
Tim Janik committed
307
  GtkTable *table = GTK_TABLE (container);
308 309 310 311 312 313 314 315 316 317 318 319
  GtkTableChild *table_child;
  GList *list;

  table_child = NULL;
  for (list = table->children; list; list = list->next)
    {
      table_child = list->data;

      if (table_child->widget == child)
	break;
    }
  if (!list)
Tim Janik's avatar
Tim Janik committed
320 321 322 323
    {
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      return;
    }
324

Tim Janik's avatar
Tim Janik committed
325
  switch (property_id)
Elliot Lee's avatar
Elliot Lee committed
326
    {
Tim Janik's avatar
Tim Janik committed
327 328
    case CHILD_PROP_LEFT_ATTACH:
      table_child->left_attach = g_value_get_uint (value);
329 330
      if (table_child->right_attach <= table_child->left_attach)
	table_child->right_attach = table_child->left_attach + 1;
331
      if (table_child->right_attach >= table->ncols)
332
	gtk_table_resize (table, table->nrows, table_child->right_attach);
333
      break;
Tim Janik's avatar
Tim Janik committed
334 335 336 337 338
    case CHILD_PROP_RIGHT_ATTACH:
      table_child->right_attach = g_value_get_uint (value);
      if (table_child->right_attach <= table_child->left_attach)
	table_child->left_attach = table_child->right_attach - 1;
      if (table_child->right_attach >= table->ncols)
339
	gtk_table_resize (table, table->nrows, table_child->right_attach);
340
      break;
Tim Janik's avatar
Tim Janik committed
341 342
    case CHILD_PROP_TOP_ATTACH:
      table_child->top_attach = g_value_get_uint (value);
343 344
      if (table_child->bottom_attach <= table_child->top_attach)
	table_child->bottom_attach = table_child->top_attach + 1;
345
      if (table_child->bottom_attach >= table->nrows)
346
	gtk_table_resize (table, table_child->bottom_attach, table->ncols);
347
      break;
Tim Janik's avatar
Tim Janik committed
348 349 350 351 352 353
    case CHILD_PROP_BOTTOM_ATTACH:
      table_child->bottom_attach = g_value_get_uint (value);
      if (table_child->bottom_attach <= table_child->top_attach)
	table_child->top_attach = table_child->bottom_attach - 1;
      if (table_child->bottom_attach >= table->nrows)
	gtk_table_resize (table, table_child->bottom_attach, table->ncols);
354
      break;
Tim Janik's avatar
Tim Janik committed
355 356 357 358
    case CHILD_PROP_X_OPTIONS:
      table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
      table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
      table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
359
      break;
Tim Janik's avatar
Tim Janik committed
360 361 362 363
    case CHILD_PROP_Y_OPTIONS:
      table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
      table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
      table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
364
      break;
Tim Janik's avatar
Tim Janik committed
365 366
    case CHILD_PROP_X_PADDING:
      table_child->xpadding = g_value_get_uint (value);
367
      break;
Tim Janik's avatar
Tim Janik committed
368 369
    case CHILD_PROP_Y_PADDING:
      table_child->ypadding = g_value_get_uint (value);
370 371
      break;
    default:
Tim Janik's avatar
Tim Janik committed
372
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
373
      break;
Elliot Lee's avatar
Elliot Lee committed
374
    }
375
  if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
376
    gtk_widget_queue_resize (child);
377 378
}

379
static void
Tim Janik's avatar
Tim Janik committed
380 381 382 383 384
gtk_table_get_child_property (GtkContainer    *container,
			      GtkWidget       *child,
			      guint            property_id,
			      GValue          *value,
			      GParamSpec      *pspec)
385
{
Tim Janik's avatar
Tim Janik committed
386
  GtkTable *table = GTK_TABLE (container);
387 388
  GtkTableChild *table_child;
  GList *list;
389

390 391 392 393
  table_child = NULL;
  for (list = table->children; list; list = list->next)
    {
      table_child = list->data;
394

395 396 397 398
      if (table_child->widget == child)
	break;
    }
  if (!list)
Tim Janik's avatar
Tim Janik committed
399 400 401 402
    {
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      return;
    }
403

Tim Janik's avatar
Tim Janik committed
404
  switch (property_id)
405
    {
Tim Janik's avatar
Tim Janik committed
406 407
    case CHILD_PROP_LEFT_ATTACH:
      g_value_set_uint (value, table_child->left_attach);
408
      break;
Tim Janik's avatar
Tim Janik committed
409 410
    case CHILD_PROP_RIGHT_ATTACH:
      g_value_set_uint (value, table_child->right_attach);
411
      break;
Tim Janik's avatar
Tim Janik committed
412 413
    case CHILD_PROP_TOP_ATTACH:
      g_value_set_uint (value, table_child->top_attach);
414
      break;
Tim Janik's avatar
Tim Janik committed
415 416
    case CHILD_PROP_BOTTOM_ATTACH:
      g_value_set_uint (value, table_child->bottom_attach);
417
      break;
Tim Janik's avatar
Tim Janik committed
418 419 420 421
    case CHILD_PROP_X_OPTIONS:
      g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
				 table_child->xshrink * GTK_SHRINK |
				 table_child->xfill * GTK_FILL));
422
      break;
Tim Janik's avatar
Tim Janik committed
423 424 425 426
    case CHILD_PROP_Y_OPTIONS:
      g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
				 table_child->yshrink * GTK_SHRINK |
				 table_child->yfill * GTK_FILL));
427
      break;
Tim Janik's avatar
Tim Janik committed
428 429
    case CHILD_PROP_X_PADDING:
      g_value_set_uint (value, table_child->xpadding);
430
      break;
Tim Janik's avatar
Tim Janik committed
431 432
    case CHILD_PROP_Y_PADDING:
      g_value_set_uint (value, table_child->ypadding);
433 434
      break;
    default:
Tim Janik's avatar
Tim Janik committed
435
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
436 437 438 439 440 441 442
      break;
    }
}

static void
gtk_table_init (GtkTable *table)
{
443
  GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
444
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
445 446 447 448 449 450
  
  table->children = NULL;
  table->rows = NULL;
  table->cols = NULL;
  table->nrows = 0;
  table->ncols = 0;
451 452
  table->column_spacing = 0;
  table->row_spacing = 0;
453
  table->homogeneous = FALSE;
454 455

  gtk_table_resize (table, 1, 1);
456 457 458 459 460 461 462 463
}

GtkWidget*
gtk_table_new (guint	rows,
	       guint	columns,
	       gboolean homogeneous)
{
  GtkTable *table;
464

465
  if (rows == 0)
Manish Singh's avatar
Manish Singh committed
466
    rows = 1;
467
  if (columns == 0)
Manish Singh's avatar
Manish Singh committed
468
    columns = 1;
469
  
Manish Singh's avatar
Manish Singh committed
470
  table = g_object_new (GTK_TYPE_TABLE, NULL);
471 472
  
  table->homogeneous = (homogeneous ? TRUE : FALSE);
Elliot Lee's avatar
Elliot Lee committed
473

474 475
  gtk_table_resize (table, rows, columns);
  
Elliot Lee's avatar
Elliot Lee committed
476 477 478
  return GTK_WIDGET (table);
}

479 480 481 482
void
gtk_table_resize (GtkTable *table,
		  guint     n_rows,
		  guint     n_cols)
483
{
484
  g_return_if_fail (GTK_IS_TABLE (table));
485 486
  g_return_if_fail (n_rows > 0 && n_rows <= 65535);
  g_return_if_fail (n_cols > 0 && n_cols <= 65535);
487

488 489
  n_rows = MAX (n_rows, 1);
  n_cols = MAX (n_cols, 1);
490

491 492 493 494 495 496 497 498 499 500 501
  if (n_rows != table->nrows ||
      n_cols != table->ncols)
    {
      GList *list;
      
      for (list = table->children; list; list = list->next)
	{
	  GtkTableChild *child;
	  
	  child = list->data;
	  
502 503
	  n_rows = MAX (n_rows, child->bottom_attach);
	  n_cols = MAX (n_cols, child->right_attach);
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	}
      
      if (n_rows != table->nrows)
	{
	  guint i;

	  i = table->nrows;
	  table->nrows = n_rows;
	  table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
	  
	  for (; i < table->nrows; i++)
	    {
	      table->rows[i].requisition = 0;
	      table->rows[i].allocation = 0;
	      table->rows[i].spacing = table->row_spacing;
	      table->rows[i].need_expand = 0;
	      table->rows[i].need_shrink = 0;
	      table->rows[i].expand = 0;
	      table->rows[i].shrink = 0;
	    }
Alexander Larsson's avatar
Alexander Larsson committed
524

525
	  g_object_notify (G_OBJECT (table), "n-rows");
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	}

      if (n_cols != table->ncols)
	{
	  guint i;

	  i = table->ncols;
	  table->ncols = n_cols;
	  table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
	  
	  for (; i < table->ncols; i++)
	    {
	      table->cols[i].requisition = 0;
	      table->cols[i].allocation = 0;
	      table->cols[i].spacing = table->column_spacing;
	      table->cols[i].need_expand = 0;
	      table->cols[i].need_shrink = 0;
	      table->cols[i].expand = 0;
	      table->cols[i].shrink = 0;
	    }
Alexander Larsson's avatar
Alexander Larsson committed
546

547
	  g_object_notify (G_OBJECT (table), "n-columns");
548 549
	}
    }
550 551
}

Elliot Lee's avatar
Elliot Lee committed
552
void
553 554 555 556 557 558 559 560 561 562
gtk_table_attach (GtkTable	  *table,
		  GtkWidget	  *child,
		  guint		   left_attach,
		  guint		   right_attach,
		  guint		   top_attach,
		  guint		   bottom_attach,
		  GtkAttachOptions xoptions,
		  GtkAttachOptions yoptions,
		  guint		   xpadding,
		  guint		   ypadding)
Elliot Lee's avatar
Elliot Lee committed
563 564
{
  GtkTableChild *table_child;
565
  
Elliot Lee's avatar
Elliot Lee committed
566
  g_return_if_fail (GTK_IS_TABLE (table));
567 568 569 570
  g_return_if_fail (GTK_IS_WIDGET (child));
  g_return_if_fail (child->parent == NULL);
  
  /* g_return_if_fail (left_attach >= 0); */
571
  g_return_if_fail (left_attach < right_attach);
572
  /* g_return_if_fail (top_attach >= 0); */
573
  g_return_if_fail (top_attach < bottom_attach);
574
  
575
  if (right_attach >= table->ncols)
576
    gtk_table_resize (table, table->nrows, right_attach);
577
  
578
  if (bottom_attach >= table->nrows)
579
    gtk_table_resize (table, bottom_attach, table->ncols);
580
  
Elliot Lee's avatar
Elliot Lee committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594
  table_child = g_new (GtkTableChild, 1);
  table_child->widget = child;
  table_child->left_attach = left_attach;
  table_child->right_attach = right_attach;
  table_child->top_attach = top_attach;
  table_child->bottom_attach = bottom_attach;
  table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
  table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
  table_child->xfill = (xoptions & GTK_FILL) != 0;
  table_child->xpadding = xpadding;
  table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
  table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
  table_child->yfill = (yoptions & GTK_FILL) != 0;
  table_child->ypadding = ypadding;
595
  
Elliot Lee's avatar
Elliot Lee committed
596
  table->children = g_list_prepend (table->children, table_child);
597
  
Elliot Lee's avatar
Elliot Lee committed
598 599 600 601 602 603
  gtk_widget_set_parent (child, GTK_WIDGET (table));
}

void
gtk_table_attach_defaults (GtkTable  *table,
			   GtkWidget *widget,
604 605 606 607
			   guint      left_attach,
			   guint      right_attach,
			   guint      top_attach,
			   guint      bottom_attach)
Elliot Lee's avatar
Elliot Lee committed
608 609 610 611 612 613 614 615 616 617 618
{
  gtk_table_attach (table, widget,
		    left_attach, right_attach,
		    top_attach, bottom_attach,
		    GTK_EXPAND | GTK_FILL,
		    GTK_EXPAND | GTK_FILL,
		    0, 0);
}

void
gtk_table_set_row_spacing (GtkTable *table,
619 620
			   guint     row,
			   guint     spacing)
Elliot Lee's avatar
Elliot Lee committed
621 622
{
  g_return_if_fail (GTK_IS_TABLE (table));
623
  g_return_if_fail (row < table->nrows);
624
  
Elliot Lee's avatar
Elliot Lee committed
625 626 627
  if (table->rows[row].spacing != spacing)
    {
      table->rows[row].spacing = spacing;
628
      
Elliot Lee's avatar
Elliot Lee committed
629 630 631 632 633
      if (GTK_WIDGET_VISIBLE (table))
	gtk_widget_queue_resize (GTK_WIDGET (table));
    }
}

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/**
 * gtk_table_get_row_spacing:
 * @table: a #GtkTable
 * @row: a row in the table, 0 indicates the first row
 *
 * Gets the amount of space between row @row, and
 * row @row + 1. See gtk_table_set_row_spacing().
 *
 * Return value: the row spacing
 **/
guint
gtk_table_get_row_spacing (GtkTable *table,
			   guint     row)
{
  g_return_val_if_fail (GTK_IS_TABLE (table), 0);
  g_return_val_if_fail (row < table->nrows - 1, 0);
 
  return table->rows[row].spacing;
}

Elliot Lee's avatar
Elliot Lee committed
654 655
void
gtk_table_set_col_spacing (GtkTable *table,
656 657
			   guint     column,
			   guint     spacing)
Elliot Lee's avatar
Elliot Lee committed
658 659
{
  g_return_if_fail (GTK_IS_TABLE (table));
660
  g_return_if_fail (column < table->ncols);
661
  
Elliot Lee's avatar
Elliot Lee committed
662 663 664
  if (table->cols[column].spacing != spacing)
    {
      table->cols[column].spacing = spacing;
665
      
Elliot Lee's avatar
Elliot Lee committed
666 667 668 669 670
      if (GTK_WIDGET_VISIBLE (table))
	gtk_widget_queue_resize (GTK_WIDGET (table));
    }
}

671 672 673
/**
 * gtk_table_get_col_spacing:
 * @table: a #GtkTable
674
 * @column: a column in the table, 0 indicates the first column
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
 *
 * Gets the amount of space between column @col, and
 * column @col + 1. See gtk_table_set_col_spacing().
 *
 * Return value: the column spacing
 **/
guint
gtk_table_get_col_spacing (GtkTable *table,
			   guint     column)
{
  g_return_val_if_fail (GTK_IS_TABLE (table), 0);
  g_return_val_if_fail (column < table->ncols, 0);

  return table->cols[column].spacing;
}

Elliot Lee's avatar
Elliot Lee committed
691 692
void
gtk_table_set_row_spacings (GtkTable *table,
693
			    guint     spacing)
Elliot Lee's avatar
Elliot Lee committed
694
{
695 696
  guint row;
  
Elliot Lee's avatar
Elliot Lee committed
697
  g_return_if_fail (GTK_IS_TABLE (table));
698
  
699
  table->row_spacing = spacing;
700
  for (row = 0; row < table->nrows; row++)
Elliot Lee's avatar
Elliot Lee committed
701
    table->rows[row].spacing = spacing;
702
  
Elliot Lee's avatar
Elliot Lee committed
703 704
  if (GTK_WIDGET_VISIBLE (table))
    gtk_widget_queue_resize (GTK_WIDGET (table));
705

706
  g_object_notify (G_OBJECT (table), "row-spacing");
Elliot Lee's avatar
Elliot Lee committed
707 708
}

709 710 711 712 713 714 715 716
/**
 * gtk_table_get_default_row_spacing:
 * @table: a #GtkTable
 *
 * Gets the default row spacing for the table. This is
 * the spacing that will be used for newly added rows.
 * (See gtk_table_set_row_spacings())
 *
717
 * Return value: the default row spacing
718 719 720 721 722 723 724 725 726
 **/
guint
gtk_table_get_default_row_spacing (GtkTable *table)
{
  g_return_val_if_fail (GTK_IS_TABLE (table), 0);

  return table->row_spacing;
}

Elliot Lee's avatar
Elliot Lee committed
727 728
void
gtk_table_set_col_spacings (GtkTable *table,
729
			    guint     spacing)
Elliot Lee's avatar
Elliot Lee committed
730
{
731 732
  guint col;
  
Elliot Lee's avatar
Elliot Lee committed
733
  g_return_if_fail (GTK_IS_TABLE (table));
734
  
735
  table->column_spacing = spacing;
736
  for (col = 0; col < table->ncols; col++)
Elliot Lee's avatar
Elliot Lee committed
737
    table->cols[col].spacing = spacing;
738
  
Elliot Lee's avatar
Elliot Lee committed
739 740
  if (GTK_WIDGET_VISIBLE (table))
    gtk_widget_queue_resize (GTK_WIDGET (table));
741

742
  g_object_notify (G_OBJECT (table), "column-spacing");
Elliot Lee's avatar
Elliot Lee committed
743 744
}

745 746 747 748 749 750 751 752
/**
 * gtk_table_get_default_col_spacing:
 * @table: a #GtkTable
 *
 * Gets the default column spacing for the table. This is
 * the spacing that will be used for newly added columns.
 * (See gtk_table_set_col_spacings())
 *
753
 * Return value: the default column spacing
754 755 756 757 758 759 760 761 762
 **/
guint
gtk_table_get_default_col_spacing (GtkTable *table)
{
  g_return_val_if_fail (GTK_IS_TABLE (table), 0);

  return table->column_spacing;
}

763 764
void
gtk_table_set_homogeneous (GtkTable *table,
765
			   gboolean  homogeneous)
766 767 768
{
  g_return_if_fail (GTK_IS_TABLE (table));

769 770 771 772 773 774 775
  homogeneous = (homogeneous != 0);
  if (homogeneous != table->homogeneous)
    {
      table->homogeneous = homogeneous;
      
      if (GTK_WIDGET_VISIBLE (table))
	gtk_widget_queue_resize (GTK_WIDGET (table));
776 777

      g_object_notify (G_OBJECT (table), "homogeneous");
778
    }
779 780
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
/**
 * gtk_table_get_homogeneous:
 * @table: a #GtkTable
 *
 * Returns whether the table cells are all constrained to the same
 * width and height. (See gtk_table_set_homogenous ())
 *
 * Return value: %TRUE if the cells are all constrained to the same size
 **/
gboolean
gtk_table_get_homogeneous (GtkTable *table)
{
  g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);

  return table->homogeneous;
}

Elliot Lee's avatar
Elliot Lee committed
798
static void
799
gtk_table_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
800
{
801 802
  GtkTable *table = GTK_TABLE (object);

Elliot Lee's avatar
Elliot Lee committed
803 804
  g_free (table->rows);
  g_free (table->cols);
805
  
Matthias Clasen's avatar
Matthias Clasen committed
806
  G_OBJECT_CLASS (gtk_table_parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
807 808 809 810 811 812
}

static void
gtk_table_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
813
  GtkTable *table = GTK_TABLE (widget);
Elliot Lee's avatar
Elliot Lee committed
814
  gint row, col;
815

Elliot Lee's avatar
Elliot Lee committed
816 817
  requisition->width = 0;
  requisition->height = 0;
818
  
Elliot Lee's avatar
Elliot Lee committed
819 820 821 822 823
  gtk_table_size_request_init (table);
  gtk_table_size_request_pass1 (table);
  gtk_table_size_request_pass2 (table);
  gtk_table_size_request_pass3 (table);
  gtk_table_size_request_pass2 (table);
824
  
Elliot Lee's avatar
Elliot Lee committed
825 826
  for (col = 0; col < table->ncols; col++)
    requisition->width += table->cols[col].requisition;
827
  for (col = 0; col + 1 < table->ncols; col++)
Elliot Lee's avatar
Elliot Lee committed
828
    requisition->width += table->cols[col].spacing;
829
  
Elliot Lee's avatar
Elliot Lee committed
830 831
  for (row = 0; row < table->nrows; row++)
    requisition->height += table->rows[row].requisition;
832
  for (row = 0; row + 1 < table->nrows; row++)
Elliot Lee's avatar
Elliot Lee committed
833
    requisition->height += table->rows[row].spacing;
834
  
Elliot Lee's avatar
Elliot Lee committed
835 836 837 838 839 840 841 842
  requisition->width += GTK_CONTAINER (table)->border_width * 2;
  requisition->height += GTK_CONTAINER (table)->border_width * 2;
}

static void
gtk_table_size_allocate (GtkWidget     *widget,
			 GtkAllocation *allocation)
{
843 844
  GtkTable *table = GTK_TABLE (widget);

Elliot Lee's avatar
Elliot Lee committed
845
  widget->allocation = *allocation;
846

Elliot Lee's avatar
Elliot Lee committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  gtk_table_size_allocate_init (table);
  gtk_table_size_allocate_pass1 (table);
  gtk_table_size_allocate_pass2 (table);
}

static void
gtk_table_add (GtkContainer *container,
	       GtkWidget    *widget)
{
  gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
}

static void
gtk_table_remove (GtkContainer *container,
		  GtkWidget    *widget)
{
863
  GtkTable *table = GTK_TABLE (container);
Elliot Lee's avatar
Elliot Lee committed
864 865
  GtkTableChild *child;
  GList *children;
866

Elliot Lee's avatar
Elliot Lee committed
867
  children = table->children;
868
  
Elliot Lee's avatar
Elliot Lee committed
869 870 871 872
  while (children)
    {
      child = children->data;
      children = children->next;
873
      
Elliot Lee's avatar
Elliot Lee committed
874
      if (child->widget == widget)
875
	{
876 877
	  gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
	  
Elliot Lee's avatar
Elliot Lee committed
878
	  gtk_widget_unparent (widget);
879 880 881 882 883
	  
	  table->children = g_list_remove (table->children, child);
	  g_free (child);
	  
	  if (was_visible && GTK_WIDGET_VISIBLE (container))
884
	    gtk_widget_queue_resize (GTK_WIDGET (container));
885 886
	  break;
	}
Elliot Lee's avatar
Elliot Lee committed
887 888 889 890
    }
}

static void
891 892 893 894
gtk_table_forall (GtkContainer *container,
		  gboolean	include_internals,
		  GtkCallback	callback,
		  gpointer	callback_data)
Elliot Lee's avatar
Elliot Lee committed
895
{
896
  GtkTable *table = GTK_TABLE (container);
Elliot Lee's avatar
Elliot Lee committed
897 898
  GtkTableChild *child;
  GList *children;
899

Elliot Lee's avatar
Elliot Lee committed
900
  children = table->children;