1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtktypebuiltins.h"
32 #include "gtkprivate.h"
37 struct _GtkTablePrivate
44 guint16 column_spacing;
49 guint homogeneous : 1;
65 CHILD_PROP_LEFT_ATTACH,
66 CHILD_PROP_RIGHT_ATTACH,
67 CHILD_PROP_TOP_ATTACH,
68 CHILD_PROP_BOTTOM_ATTACH,
76 static void gtk_table_finalize (GObject *object);
77 static void gtk_table_get_preferred_width (GtkWidget *widget,
80 static void gtk_table_get_preferred_height (GtkWidget *widget,
83 static void gtk_table_size_allocate (GtkWidget *widget,
84 GtkAllocation *allocation);
85 static void gtk_table_compute_expand (GtkWidget *widget,
88 static void gtk_table_add (GtkContainer *container,
90 static void gtk_table_remove (GtkContainer *container,
92 static void gtk_table_forall (GtkContainer *container,
93 gboolean include_internals,
95 gpointer callback_data);
96 static void gtk_table_get_property (GObject *object,
100 static void gtk_table_set_property (GObject *object,
104 static void gtk_table_set_child_property (GtkContainer *container,
109 static void gtk_table_get_child_property (GtkContainer *container,
114 static GType gtk_table_child_type (GtkContainer *container);
117 static void gtk_table_size_request_init (GtkTable *table);
118 static void gtk_table_size_request_pass1 (GtkTable *table);
119 static void gtk_table_size_request_pass2 (GtkTable *table);
120 static void gtk_table_size_request_pass3 (GtkTable *table);
122 static void gtk_table_size_allocate_init (GtkTable *table);
123 static void gtk_table_size_allocate_pass1 (GtkTable *table);
124 static void gtk_table_size_allocate_pass2 (GtkTable *table);
127 G_DEFINE_TYPE (GtkTable, gtk_table, GTK_TYPE_CONTAINER)
130 gtk_table_class_init (GtkTableClass *class)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
133 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
134 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
136 gobject_class->finalize = gtk_table_finalize;
138 gobject_class->get_property = gtk_table_get_property;
139 gobject_class->set_property = gtk_table_set_property;
141 widget_class->get_preferred_width = gtk_table_get_preferred_width;
142 widget_class->get_preferred_height = gtk_table_get_preferred_height;
143 widget_class->size_allocate = gtk_table_size_allocate;
144 widget_class->compute_expand = gtk_table_compute_expand;
146 container_class->add = gtk_table_add;
147 container_class->remove = gtk_table_remove;
148 container_class->forall = gtk_table_forall;
149 container_class->child_type = gtk_table_child_type;
150 container_class->set_child_property = gtk_table_set_child_property;
151 container_class->get_child_property = gtk_table_get_child_property;
152 gtk_container_class_handle_border_width (container_class);
154 g_object_class_install_property (gobject_class,
156 g_param_spec_uint ("n-rows",
158 P_("The number of rows in the table"),
162 GTK_PARAM_READWRITE));
163 g_object_class_install_property (gobject_class,
165 g_param_spec_uint ("n-columns",
167 P_("The number of columns in the table"),
171 GTK_PARAM_READWRITE));
172 g_object_class_install_property (gobject_class,
174 g_param_spec_uint ("row-spacing",
176 P_("The amount of space between two consecutive rows"),
180 GTK_PARAM_READWRITE));
181 g_object_class_install_property (gobject_class,
183 g_param_spec_uint ("column-spacing",
184 P_("Column spacing"),
185 P_("The amount of space between two consecutive columns"),
189 GTK_PARAM_READWRITE));
190 g_object_class_install_property (gobject_class,
192 g_param_spec_boolean ("homogeneous",
194 P_("If TRUE, the table cells are all the same width/height"),
196 GTK_PARAM_READWRITE));
198 gtk_container_class_install_child_property (container_class,
199 CHILD_PROP_LEFT_ATTACH,
200 g_param_spec_uint ("left-attach",
201 P_("Left attachment"),
202 P_("The column number to attach the left side of the child to"),
204 GTK_PARAM_READWRITE));
205 gtk_container_class_install_child_property (container_class,
206 CHILD_PROP_RIGHT_ATTACH,
207 g_param_spec_uint ("right-attach",
208 P_("Right attachment"),
209 P_("The column number to attach the right side of a child widget to"),
211 GTK_PARAM_READWRITE));
212 gtk_container_class_install_child_property (container_class,
213 CHILD_PROP_TOP_ATTACH,
214 g_param_spec_uint ("top-attach",
215 P_("Top attachment"),
216 P_("The row number to attach the top of a child widget to"),
218 GTK_PARAM_READWRITE));
219 gtk_container_class_install_child_property (container_class,
220 CHILD_PROP_BOTTOM_ATTACH,
221 g_param_spec_uint ("bottom-attach",
222 P_("Bottom attachment"),
223 P_("The row number to attach the bottom of the child to"),
225 GTK_PARAM_READWRITE));
226 gtk_container_class_install_child_property (container_class,
227 CHILD_PROP_X_OPTIONS,
228 g_param_spec_flags ("x-options",
229 P_("Horizontal options"),
230 P_("Options specifying the horizontal behaviour of the child"),
231 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
232 GTK_PARAM_READWRITE));
233 gtk_container_class_install_child_property (container_class,
234 CHILD_PROP_Y_OPTIONS,
235 g_param_spec_flags ("y-options",
236 P_("Vertical options"),
237 P_("Options specifying the vertical behaviour of the child"),
238 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
239 GTK_PARAM_READWRITE));
240 gtk_container_class_install_child_property (container_class,
241 CHILD_PROP_X_PADDING,
242 g_param_spec_uint ("x-padding",
243 P_("Horizontal padding"),
244 P_("Extra space to put between the child and its left and right neighbors, in pixels"),
246 GTK_PARAM_READWRITE));
247 gtk_container_class_install_child_property (container_class,
248 CHILD_PROP_Y_PADDING,
249 g_param_spec_uint ("y-padding",
250 P_("Vertical padding"),
251 P_("Extra space to put between the child and its upper and lower neighbors, in pixels"),
253 GTK_PARAM_READWRITE));
255 g_type_class_add_private (class, sizeof (GtkTablePrivate));
259 gtk_table_compute_expand (GtkWidget *widget,
263 GtkTable *table = GTK_TABLE (widget);
264 GtkTablePrivate *priv = table->priv;
266 GtkTableChild *child;
273 for (list = priv->children; list; list = list->next)
278 hexpand = child->xexpand ||
279 gtk_widget_compute_expand (child->widget,
280 GTK_ORIENTATION_HORIZONTAL);
283 vexpand = child->yexpand ||
284 gtk_widget_compute_expand (child->widget,
285 GTK_ORIENTATION_VERTICAL);
287 if (hexpand && vexpand)
291 *hexpand_p = hexpand;
292 *vexpand_p = vexpand;
296 gtk_table_child_type (GtkContainer *container)
298 return GTK_TYPE_WIDGET;
302 gtk_table_get_property (GObject *object,
307 GtkTable *table = GTK_TABLE (object);
308 GtkTablePrivate *priv = table->priv;
313 g_value_set_uint (value, priv->nrows);
316 g_value_set_uint (value, priv->ncols);
318 case PROP_ROW_SPACING:
319 g_value_set_uint (value, priv->row_spacing);
321 case PROP_COLUMN_SPACING:
322 g_value_set_uint (value, priv->column_spacing);
324 case PROP_HOMOGENEOUS:
325 g_value_set_boolean (value, priv->homogeneous);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 gtk_table_set_property (GObject *object,
339 GtkTable *table = GTK_TABLE (object);
340 GtkTablePrivate *priv = table->priv;
345 gtk_table_resize (table, g_value_get_uint (value), priv->ncols);
348 gtk_table_resize (table, priv->nrows, g_value_get_uint (value));
350 case PROP_ROW_SPACING:
351 gtk_table_set_row_spacings (table, g_value_get_uint (value));
353 case PROP_COLUMN_SPACING:
354 gtk_table_set_col_spacings (table, g_value_get_uint (value));
356 case PROP_HOMOGENEOUS:
357 gtk_table_set_homogeneous (table, g_value_get_boolean (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_table_set_child_property (GtkContainer *container,
372 GtkTable *table = GTK_TABLE (container);
373 GtkTablePrivate *priv = table->priv;
374 GtkTableChild *table_child;
378 for (list = priv->children; list; list = list->next)
380 table_child = list->data;
382 if (table_child->widget == child)
387 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
393 case CHILD_PROP_LEFT_ATTACH:
394 table_child->left_attach = g_value_get_uint (value);
395 if (table_child->right_attach <= table_child->left_attach)
396 table_child->right_attach = table_child->left_attach + 1;
397 if (table_child->right_attach >= priv->ncols)
398 gtk_table_resize (table, priv->nrows, table_child->right_attach);
400 case CHILD_PROP_RIGHT_ATTACH:
401 table_child->right_attach = g_value_get_uint (value);
402 if (table_child->right_attach <= table_child->left_attach)
403 table_child->left_attach = table_child->right_attach - 1;
404 if (table_child->right_attach >= priv->ncols)
405 gtk_table_resize (table, priv->nrows, table_child->right_attach);
407 case CHILD_PROP_TOP_ATTACH:
408 table_child->top_attach = g_value_get_uint (value);
409 if (table_child->bottom_attach <= table_child->top_attach)
410 table_child->bottom_attach = table_child->top_attach + 1;
411 if (table_child->bottom_attach >= priv->nrows)
412 gtk_table_resize (table, table_child->bottom_attach, priv->ncols);
414 case CHILD_PROP_BOTTOM_ATTACH:
415 table_child->bottom_attach = g_value_get_uint (value);
416 if (table_child->bottom_attach <= table_child->top_attach)
417 table_child->top_attach = table_child->bottom_attach - 1;
418 if (table_child->bottom_attach >= priv->nrows)
419 gtk_table_resize (table, table_child->bottom_attach, priv->ncols);
421 case CHILD_PROP_X_OPTIONS:
425 xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
427 if (table_child->xexpand != xexpand)
429 table_child->xexpand = xexpand;
430 gtk_widget_queue_compute_expand (GTK_WIDGET (table));
433 table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
434 table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
437 case CHILD_PROP_Y_OPTIONS:
441 yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
443 if (table_child->yexpand != yexpand)
445 table_child->yexpand = yexpand;
446 gtk_widget_queue_compute_expand (GTK_WIDGET (table));
449 table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
450 table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
453 case CHILD_PROP_X_PADDING:
454 table_child->xpadding = g_value_get_uint (value);
456 case CHILD_PROP_Y_PADDING:
457 table_child->ypadding = g_value_get_uint (value);
460 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
463 if (gtk_widget_get_visible (child) &&
464 gtk_widget_get_visible (GTK_WIDGET (table)))
465 gtk_widget_queue_resize (child);
469 gtk_table_get_child_property (GtkContainer *container,
475 GtkTable *table = GTK_TABLE (container);
476 GtkTablePrivate *priv = table->priv;
477 GtkTableChild *table_child;
481 for (list = priv->children; list; list = list->next)
483 table_child = list->data;
485 if (table_child->widget == child)
490 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
496 case CHILD_PROP_LEFT_ATTACH:
497 g_value_set_uint (value, table_child->left_attach);
499 case CHILD_PROP_RIGHT_ATTACH:
500 g_value_set_uint (value, table_child->right_attach);
502 case CHILD_PROP_TOP_ATTACH:
503 g_value_set_uint (value, table_child->top_attach);
505 case CHILD_PROP_BOTTOM_ATTACH:
506 g_value_set_uint (value, table_child->bottom_attach);
508 case CHILD_PROP_X_OPTIONS:
509 g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
510 table_child->xshrink * GTK_SHRINK |
511 table_child->xfill * GTK_FILL));
513 case CHILD_PROP_Y_OPTIONS:
514 g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
515 table_child->yshrink * GTK_SHRINK |
516 table_child->yfill * GTK_FILL));
518 case CHILD_PROP_X_PADDING:
519 g_value_set_uint (value, table_child->xpadding);
521 case CHILD_PROP_Y_PADDING:
522 g_value_set_uint (value, table_child->ypadding);
525 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
531 gtk_table_init (GtkTable *table)
533 GtkTablePrivate *priv;
535 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
540 gtk_widget_set_has_window (GTK_WIDGET (table), FALSE);
541 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
543 priv->children = NULL;
548 priv->column_spacing = 0;
549 priv->row_spacing = 0;
550 priv->homogeneous = FALSE;
552 gtk_table_resize (table, 1, 1);
556 gtk_table_new (guint rows,
558 gboolean homogeneous)
561 GtkTablePrivate *priv;
568 table = g_object_new (GTK_TYPE_TABLE, NULL);
571 priv->homogeneous = (homogeneous ? TRUE : FALSE);
573 gtk_table_resize (table, rows, columns);
575 return GTK_WIDGET (table);
579 gtk_table_resize (GtkTable *table,
583 GtkTablePrivate *priv;
585 g_return_if_fail (GTK_IS_TABLE (table));
586 g_return_if_fail (n_rows > 0 && n_rows <= 65535);
587 g_return_if_fail (n_cols > 0 && n_cols <= 65535);
591 n_rows = MAX (n_rows, 1);
592 n_cols = MAX (n_cols, 1);
594 if (n_rows != priv->nrows ||
595 n_cols != priv->ncols)
599 for (list = priv->children; list; list = list->next)
601 GtkTableChild *child;
605 n_rows = MAX (n_rows, child->bottom_attach);
606 n_cols = MAX (n_cols, child->right_attach);
609 if (n_rows != priv->nrows)
614 priv->nrows = n_rows;
615 priv->rows = g_realloc (priv->rows, priv->nrows * sizeof (GtkTableRowCol));
617 for (; i < priv->nrows; i++)
619 priv->rows[i].requisition = 0;
620 priv->rows[i].allocation = 0;
621 priv->rows[i].spacing = priv->row_spacing;
622 priv->rows[i].need_expand = 0;
623 priv->rows[i].need_shrink = 0;
624 priv->rows[i].expand = 0;
625 priv->rows[i].shrink = 0;
628 g_object_notify (G_OBJECT (table), "n-rows");
631 if (n_cols != priv->ncols)
636 priv->ncols = n_cols;
637 priv->cols = g_realloc (priv->cols, priv->ncols * sizeof (GtkTableRowCol));
639 for (; i < priv->ncols; i++)
641 priv->cols[i].requisition = 0;
642 priv->cols[i].allocation = 0;
643 priv->cols[i].spacing = priv->column_spacing;
644 priv->cols[i].need_expand = 0;
645 priv->cols[i].need_shrink = 0;
646 priv->cols[i].expand = 0;
647 priv->cols[i].shrink = 0;
650 g_object_notify (G_OBJECT (table), "n-columns");
656 gtk_table_attach (GtkTable *table,
662 GtkAttachOptions xoptions,
663 GtkAttachOptions yoptions,
667 GtkTablePrivate *priv = table->priv;
668 GtkTableChild *table_child;
670 g_return_if_fail (GTK_IS_TABLE (table));
671 g_return_if_fail (GTK_IS_WIDGET (child));
672 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
674 /* g_return_if_fail (left_attach >= 0); */
675 g_return_if_fail (left_attach < right_attach);
676 /* g_return_if_fail (top_attach >= 0); */
677 g_return_if_fail (top_attach < bottom_attach);
681 if (right_attach >= priv->ncols)
682 gtk_table_resize (table, priv->nrows, right_attach);
684 if (bottom_attach >= priv->nrows)
685 gtk_table_resize (table, bottom_attach, priv->ncols);
687 table_child = g_new (GtkTableChild, 1);
688 table_child->widget = child;
689 table_child->left_attach = left_attach;
690 table_child->right_attach = right_attach;
691 table_child->top_attach = top_attach;
692 table_child->bottom_attach = bottom_attach;
693 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
694 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
695 table_child->xfill = (xoptions & GTK_FILL) != 0;
696 table_child->xpadding = xpadding;
697 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
698 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
699 table_child->yfill = (yoptions & GTK_FILL) != 0;
700 table_child->ypadding = ypadding;
702 priv->children = g_list_prepend (priv->children, table_child);
704 gtk_widget_set_parent (child, GTK_WIDGET (table));
708 gtk_table_attach_defaults (GtkTable *table,
715 gtk_table_attach (table, widget,
716 left_attach, right_attach,
717 top_attach, bottom_attach,
718 GTK_EXPAND | GTK_FILL,
719 GTK_EXPAND | GTK_FILL,
724 gtk_table_set_row_spacing (GtkTable *table,
728 GtkTablePrivate *priv;
730 g_return_if_fail (GTK_IS_TABLE (table));
734 g_return_if_fail (row < priv->nrows);
736 if (priv->rows[row].spacing != spacing)
738 priv->rows[row].spacing = spacing;
740 if (gtk_widget_get_visible (GTK_WIDGET (table)))
741 gtk_widget_queue_resize (GTK_WIDGET (table));
746 * gtk_table_get_row_spacing:
747 * @table: a #GtkTable
748 * @row: a row in the table, 0 indicates the first row
750 * Gets the amount of space between row @row, and
751 * row @row + 1. See gtk_table_set_row_spacing().
753 * Return value: the row spacing
756 gtk_table_get_row_spacing (GtkTable *table,
759 GtkTablePrivate *priv;
761 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
765 g_return_val_if_fail (row < priv->nrows - 1, 0);
767 return priv->rows[row].spacing;
771 gtk_table_set_col_spacing (GtkTable *table,
775 GtkTablePrivate *priv;
777 g_return_if_fail (GTK_IS_TABLE (table));
781 g_return_if_fail (column < priv->ncols);
783 if (priv->cols[column].spacing != spacing)
785 priv->cols[column].spacing = spacing;
787 if (gtk_widget_get_visible (GTK_WIDGET (table)))
788 gtk_widget_queue_resize (GTK_WIDGET (table));
793 * gtk_table_get_col_spacing:
794 * @table: a #GtkTable
795 * @column: a column in the table, 0 indicates the first column
797 * Gets the amount of space between column @col, and
798 * column @col + 1. See gtk_table_set_col_spacing().
800 * Return value: the column spacing
803 gtk_table_get_col_spacing (GtkTable *table,
806 GtkTablePrivate *priv;
808 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
812 g_return_val_if_fail (column < priv->ncols, 0);
814 return priv->cols[column].spacing;
818 gtk_table_set_row_spacings (GtkTable *table,
821 GtkTablePrivate *priv;
824 g_return_if_fail (GTK_IS_TABLE (table));
828 priv->row_spacing = spacing;
829 for (row = 0; row < priv->nrows; row++)
830 priv->rows[row].spacing = spacing;
832 if (gtk_widget_get_visible (GTK_WIDGET (table)))
833 gtk_widget_queue_resize (GTK_WIDGET (table));
835 g_object_notify (G_OBJECT (table), "row-spacing");
839 * gtk_table_get_default_row_spacing:
840 * @table: a #GtkTable
842 * Gets the default row spacing for the table. This is
843 * the spacing that will be used for newly added rows.
844 * (See gtk_table_set_row_spacings())
846 * Return value: the default row spacing
849 gtk_table_get_default_row_spacing (GtkTable *table)
851 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
853 return table->priv->row_spacing;
857 gtk_table_set_col_spacings (GtkTable *table,
860 GtkTablePrivate *priv;
863 g_return_if_fail (GTK_IS_TABLE (table));
867 priv->column_spacing = spacing;
868 for (col = 0; col < priv->ncols; col++)
869 priv->cols[col].spacing = spacing;
871 if (gtk_widget_get_visible (GTK_WIDGET (table)))
872 gtk_widget_queue_resize (GTK_WIDGET (table));
874 g_object_notify (G_OBJECT (table), "column-spacing");
878 * gtk_table_get_default_col_spacing:
879 * @table: a #GtkTable
881 * Gets the default column spacing for the table. This is
882 * the spacing that will be used for newly added columns.
883 * (See gtk_table_set_col_spacings())
885 * Return value: the default column spacing
888 gtk_table_get_default_col_spacing (GtkTable *table)
890 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
892 return table->priv->column_spacing;
896 gtk_table_set_homogeneous (GtkTable *table,
897 gboolean homogeneous)
899 GtkTablePrivate *priv;
901 g_return_if_fail (GTK_IS_TABLE (table));
905 homogeneous = (homogeneous != 0);
906 if (homogeneous != priv->homogeneous)
908 priv->homogeneous = homogeneous;
910 if (gtk_widget_get_visible (GTK_WIDGET (table)))
911 gtk_widget_queue_resize (GTK_WIDGET (table));
913 g_object_notify (G_OBJECT (table), "homogeneous");
918 * gtk_table_get_homogeneous:
919 * @table: a #GtkTable
921 * Returns whether the table cells are all constrained to the same
922 * width and height. (See gtk_table_set_homogenous ())
924 * Return value: %TRUE if the cells are all constrained to the same size
927 gtk_table_get_homogeneous (GtkTable *table)
929 g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
931 return table->priv->homogeneous;
935 * gtk_table_get_size:
936 * @table: a #GtkTable
937 * @rows: (out) (allow-none): return location for the number of
939 * @columns: (out) (allow-none): return location for the number
940 * of columns, or %NULL
942 * Gets the number of rows and columns in the table.
947 gtk_table_get_size (GtkTable *table,
951 GtkTablePrivate *priv;
953 g_return_if_fail (GTK_IS_TABLE (table));
961 *columns = priv->ncols;
965 gtk_table_finalize (GObject *object)
967 GtkTable *table = GTK_TABLE (object);
968 GtkTablePrivate *priv = table->priv;
973 G_OBJECT_CLASS (gtk_table_parent_class)->finalize (object);
977 gtk_table_get_preferred_width (GtkWidget *widget,
981 GtkTable *table = GTK_TABLE (widget);
982 GtkTablePrivate *priv = table->priv;
985 gtk_table_size_request_init (table);
986 gtk_table_size_request_pass1 (table);
987 gtk_table_size_request_pass2 (table);
988 gtk_table_size_request_pass3 (table);
989 gtk_table_size_request_pass2 (table);
993 for (col = 0; col < priv->ncols; col++)
994 *minimum += priv->cols[col].requisition;
995 for (col = 0; col + 1 < priv->ncols; col++)
996 *minimum += priv->cols[col].spacing;
1002 gtk_table_get_preferred_height (GtkWidget *widget,
1006 GtkTable *table = GTK_TABLE (widget);
1007 GtkTablePrivate *priv = table->priv;
1010 gtk_table_size_request_init (table);
1011 gtk_table_size_request_pass1 (table);
1012 gtk_table_size_request_pass2 (table);
1013 gtk_table_size_request_pass3 (table);
1014 gtk_table_size_request_pass2 (table);
1017 for (row = 0; row < priv->nrows; row++)
1018 *minimum += priv->rows[row].requisition;
1019 for (row = 0; row + 1 < priv->nrows; row++)
1020 *minimum += priv->rows[row].spacing;
1022 *natural = *minimum;
1026 gtk_table_size_allocate (GtkWidget *widget,
1027 GtkAllocation *allocation)
1029 GtkTable *table = GTK_TABLE (widget);
1031 gtk_widget_set_allocation (widget, allocation);
1033 gtk_table_size_allocate_init (table);
1034 gtk_table_size_allocate_pass1 (table);
1035 gtk_table_size_allocate_pass2 (table);
1039 gtk_table_add (GtkContainer *container,
1042 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
1046 gtk_table_remove (GtkContainer *container,
1049 GtkTable *table = GTK_TABLE (container);
1050 GtkTablePrivate *priv = table->priv;
1051 GtkTableChild *child;
1052 GtkWidget *widget_container = GTK_WIDGET (container);
1055 children = priv->children;
1059 child = children->data;
1060 children = children->next;
1062 if (child->widget == widget)
1064 gboolean was_visible = gtk_widget_get_visible (widget);
1066 gtk_widget_unparent (widget);
1068 priv->children = g_list_remove (priv->children, child);
1071 if (was_visible && gtk_widget_get_visible (widget_container))
1072 gtk_widget_queue_resize (widget_container);
1079 gtk_table_forall (GtkContainer *container,
1080 gboolean include_internals,
1081 GtkCallback callback,
1082 gpointer callback_data)
1084 GtkTable *table = GTK_TABLE (container);
1085 GtkTablePrivate *priv = table->priv;
1086 GtkTableChild *child;
1089 children = priv->children;
1093 child = children->data;
1094 children = children->next;
1096 (* callback) (child->widget, callback_data);
1101 gtk_table_size_request_init (GtkTable *table)
1103 GtkTablePrivate *priv = table->priv;
1104 GtkTableChild *child;
1108 for (row = 0; row < priv->nrows; row++)
1110 priv->rows[row].requisition = 0;
1111 priv->rows[row].expand = FALSE;
1113 for (col = 0; col < priv->ncols; col++)
1115 priv->cols[col].requisition = 0;
1116 priv->cols[col].expand = FALSE;
1119 children = priv->children;
1122 child = children->data;
1123 children = children->next;
1125 if (child->left_attach == (child->right_attach - 1) &&
1126 (child->xexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL)))
1127 priv->cols[child->left_attach].expand = TRUE;
1129 if (child->top_attach == (child->bottom_attach - 1) &&
1130 (child->yexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_VERTICAL)))
1131 priv->rows[child->top_attach].expand = TRUE;
1136 gtk_table_size_request_pass1 (GtkTable *table)
1138 GtkTablePrivate *priv = table->priv;
1139 GtkTableChild *child;
1144 children = priv->children;
1147 child = children->data;
1148 children = children->next;
1150 if (gtk_widget_get_visible (child->widget))
1152 GtkRequisition child_requisition;
1154 gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
1156 /* Child spans a single column.
1158 if (child->left_attach == (child->right_attach - 1))
1160 width = child_requisition.width + child->xpadding * 2;
1161 priv->cols[child->left_attach].requisition = MAX (priv->cols[child->left_attach].requisition, width);
1164 /* Child spans a single row.
1166 if (child->top_attach == (child->bottom_attach - 1))
1168 height = child_requisition.height + child->ypadding * 2;
1169 priv->rows[child->top_attach].requisition = MAX (priv->rows[child->top_attach].requisition, height);
1176 gtk_table_size_request_pass2 (GtkTable *table)
1178 GtkTablePrivate *priv = table->priv;
1183 if (priv->homogeneous)
1188 for (col = 0; col < priv->ncols; col++)
1189 max_width = MAX (max_width, priv->cols[col].requisition);
1190 for (row = 0; row < priv->nrows; row++)
1191 max_height = MAX (max_height, priv->rows[row].requisition);
1193 for (col = 0; col < priv->ncols; col++)
1194 priv->cols[col].requisition = max_width;
1195 for (row = 0; row < priv->nrows; row++)
1196 priv->rows[row].requisition = max_height;
1201 gtk_table_size_request_pass3 (GtkTable *table)
1203 GtkTablePrivate *priv = table->priv;
1204 GtkTableChild *child;
1210 children = priv->children;
1213 child = children->data;
1214 children = children->next;
1216 if (gtk_widget_get_visible (child->widget))
1218 /* Child spans multiple columns.
1220 if (child->left_attach != (child->right_attach - 1))
1222 GtkRequisition child_requisition;
1224 gtk_widget_get_preferred_size (child->widget,
1225 &child_requisition, NULL);
1227 /* Check and see if there is already enough space
1231 for (col = child->left_attach; col < child->right_attach; col++)
1233 width += priv->cols[col].requisition;
1234 if ((col + 1) < child->right_attach)
1235 width += priv->cols[col].spacing;
1238 /* If we need to request more space for this child to fill
1239 * its requisition, then divide up the needed space amongst the
1240 * columns it spans, favoring expandable columns if any.
1242 if (width < child_requisition.width + child->xpadding * 2)
1245 gboolean force_expand = FALSE;
1247 width = child_requisition.width + child->xpadding * 2 - width;
1249 for (col = child->left_attach; col < child->right_attach; col++)
1250 if (priv->cols[col].expand)
1255 n_expand = (child->right_attach - child->left_attach);
1256 force_expand = TRUE;
1259 for (col = child->left_attach; col < child->right_attach; col++)
1260 if (force_expand || priv->cols[col].expand)
1262 extra = width / n_expand;
1263 priv->cols[col].requisition += extra;
1270 /* Child spans multiple rows.
1272 if (child->top_attach != (child->bottom_attach - 1))
1274 GtkRequisition child_requisition;
1276 gtk_widget_get_preferred_size (child->widget,
1277 &child_requisition, NULL);
1279 /* Check and see if there is already enough space
1283 for (row = child->top_attach; row < child->bottom_attach; row++)
1285 height += priv->rows[row].requisition;
1286 if ((row + 1) < child->bottom_attach)
1287 height += priv->rows[row].spacing;
1290 /* If we need to request more space for this child to fill
1291 * its requisition, then divide up the needed space amongst the
1292 * rows it spans, favoring expandable rows if any.
1294 if (height < child_requisition.height + child->ypadding * 2)
1297 gboolean force_expand = FALSE;
1299 height = child_requisition.height + child->ypadding * 2 - height;
1301 for (row = child->top_attach; row < child->bottom_attach; row++)
1303 if (priv->rows[row].expand)
1309 n_expand = (child->bottom_attach - child->top_attach);
1310 force_expand = TRUE;
1313 for (row = child->top_attach; row < child->bottom_attach; row++)
1314 if (force_expand || priv->rows[row].expand)
1316 extra = height / n_expand;
1317 priv->rows[row].requisition += extra;
1328 gtk_table_size_allocate_init (GtkTable *table)
1330 GtkTablePrivate *priv = table->priv;
1331 GtkTableChild *child;
1337 /* Initialize the rows and cols.
1338 * By default, rows and cols do not expand and do shrink.
1339 * Those values are modified by the children that occupy
1340 * the rows and cols.
1342 for (col = 0; col < priv->ncols; col++)
1344 priv->cols[col].allocation = priv->cols[col].requisition;
1345 priv->cols[col].need_expand = FALSE;
1346 priv->cols[col].need_shrink = TRUE;
1347 priv->cols[col].expand = FALSE;
1348 priv->cols[col].shrink = TRUE;
1349 priv->cols[col].empty = TRUE;
1351 for (row = 0; row < priv->nrows; row++)
1353 priv->rows[row].allocation = priv->rows[row].requisition;
1354 priv->rows[row].need_expand = FALSE;
1355 priv->rows[row].need_shrink = TRUE;
1356 priv->rows[row].expand = FALSE;
1357 priv->rows[row].shrink = TRUE;
1358 priv->rows[row].empty = TRUE;
1361 /* Loop over all the children and adjust the row and col values
1362 * based on whether the children want to be allowed to expand
1363 * or shrink. This loop handles children that occupy a single
1366 children = priv->children;
1369 child = children->data;
1370 children = children->next;
1372 if (gtk_widget_get_visible (child->widget))
1374 if (child->left_attach == (child->right_attach - 1))
1376 if (child->xexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL))
1377 priv->cols[child->left_attach].expand = TRUE;
1379 if (!child->xshrink)
1380 priv->cols[child->left_attach].shrink = FALSE;
1382 priv->cols[child->left_attach].empty = FALSE;
1385 if (child->top_attach == (child->bottom_attach - 1))
1387 if (child->yexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_VERTICAL))
1388 priv->rows[child->top_attach].expand = TRUE;
1390 if (!child->yshrink)
1391 priv->rows[child->top_attach].shrink = FALSE;
1393 priv->rows[child->top_attach].empty = FALSE;
1398 /* Loop over all the children again and this time handle children
1399 * which span multiple rows or columns.
1401 children = priv->children;
1404 child = children->data;
1405 children = children->next;
1407 if (gtk_widget_get_visible (child->widget))
1409 if (child->left_attach != (child->right_attach - 1))
1411 for (col = child->left_attach; col < child->right_attach; col++)
1412 priv->cols[col].empty = FALSE;
1417 for (col = child->left_attach; col < child->right_attach; col++)
1418 if (priv->cols[col].expand)
1425 for (col = child->left_attach; col < child->right_attach; col++)
1426 priv->cols[col].need_expand = TRUE;
1429 if (!child->xshrink)
1432 for (col = child->left_attach; col < child->right_attach; col++)
1433 if (!priv->cols[col].shrink)
1440 for (col = child->left_attach; col < child->right_attach; col++)
1441 priv->cols[col].need_shrink = FALSE;
1445 if (child->top_attach != (child->bottom_attach - 1))
1447 for (row = child->top_attach; row < child->bottom_attach; row++)
1448 priv->rows[row].empty = FALSE;
1453 for (row = child->top_attach; row < child->bottom_attach; row++)
1454 if (priv->rows[row].expand)
1461 for (row = child->top_attach; row < child->bottom_attach; row++)
1462 priv->rows[row].need_expand = TRUE;
1465 if (!child->yshrink)
1468 for (row = child->top_attach; row < child->bottom_attach; row++)
1469 if (!priv->rows[row].shrink)
1476 for (row = child->top_attach; row < child->bottom_attach; row++)
1477 priv->rows[row].need_shrink = FALSE;
1483 /* Loop over the columns and set the expand and shrink values
1484 * if the column can be expanded or shrunk.
1486 for (col = 0; col < priv->ncols; col++)
1488 if (priv->cols[col].empty)
1490 priv->cols[col].expand = FALSE;
1491 priv->cols[col].shrink = FALSE;
1495 if (priv->cols[col].need_expand)
1496 priv->cols[col].expand = TRUE;
1497 if (!priv->cols[col].need_shrink)
1498 priv->cols[col].shrink = FALSE;
1502 /* Loop over the rows and set the expand and shrink values
1503 * if the row can be expanded or shrunk.
1505 for (row = 0; row < priv->nrows; row++)
1507 if (priv->rows[row].empty)
1509 priv->rows[row].expand = FALSE;
1510 priv->rows[row].shrink = FALSE;
1514 if (priv->rows[row].need_expand)
1515 priv->rows[row].expand = TRUE;
1516 if (!priv->rows[row].need_shrink)
1517 priv->rows[row].shrink = FALSE;
1523 gtk_table_size_allocate_pass1 (GtkTable *table)
1525 GtkTablePrivate *priv = table->priv;
1526 GtkAllocation allocation;
1535 /* If we were allocated more space than we requested
1536 * then we have to expand any expandable rows and columns
1537 * to fill in the extra space.
1539 gtk_widget_get_allocation (GTK_WIDGET (table), &allocation);
1540 real_width = allocation.width;
1541 real_height = allocation.height;
1543 if (priv->homogeneous)
1545 if (!priv->children)
1550 for (col = 0; col < priv->ncols; col++)
1551 if (priv->cols[col].expand)
1560 for (col = 0; col + 1 < priv->ncols; col++)
1561 width -= priv->cols[col].spacing;
1563 for (col = 0; col < priv->ncols; col++)
1565 extra = width / (priv->ncols - col);
1566 priv->cols[col].allocation = MAX (1, extra);
1577 for (col = 0; col < priv->ncols; col++)
1579 width += priv->cols[col].requisition;
1580 if (priv->cols[col].expand)
1582 if (priv->cols[col].shrink)
1585 for (col = 0; col + 1 < priv->ncols; col++)
1586 width += priv->cols[col].spacing;
1588 /* Check to see if we were allocated more width than we requested.
1590 if ((width < real_width) && (nexpand >= 1))
1592 width = real_width - width;
1594 for (col = 0; col < priv->ncols; col++)
1595 if (priv->cols[col].expand)
1597 extra = width / nexpand;
1598 priv->cols[col].allocation += extra;
1605 /* Check to see if we were allocated less width than we requested,
1606 * then shrink until we fit the size give.
1608 if (width > real_width)
1610 gint total_nshrink = nshrink;
1612 extra = width - real_width;
1613 while (total_nshrink > 0 && extra > 0)
1615 nshrink = total_nshrink;
1616 for (col = 0; col < priv->ncols; col++)
1617 if (priv->cols[col].shrink)
1619 gint allocation = priv->cols[col].allocation;
1621 priv->cols[col].allocation = MAX (1, (gint) priv->cols[col].allocation - extra / nshrink);
1622 extra -= allocation - priv->cols[col].allocation;
1624 if (priv->cols[col].allocation < 2)
1627 priv->cols[col].shrink = FALSE;
1634 if (priv->homogeneous)
1636 if (!priv->children)
1641 for (row = 0; row < priv->nrows; row++)
1642 if (priv->rows[row].expand)
1650 height = real_height;
1652 for (row = 0; row + 1 < priv->nrows; row++)
1653 height -= priv->rows[row].spacing;
1655 for (row = 0; row < priv->nrows; row++)
1657 extra = height / (priv->nrows - row);
1658 priv->rows[row].allocation = MAX (1, extra);
1669 for (row = 0; row < priv->nrows; row++)
1671 height += priv->rows[row].requisition;
1672 if (priv->rows[row].expand)
1674 if (priv->rows[row].shrink)
1677 for (row = 0; row + 1 < priv->nrows; row++)
1678 height += priv->rows[row].spacing;
1680 /* Check to see if we were allocated more height than we requested.
1682 if ((height < real_height) && (nexpand >= 1))
1684 height = real_height - height;
1686 for (row = 0; row < priv->nrows; row++)
1687 if (priv->rows[row].expand)
1689 extra = height / nexpand;
1690 priv->rows[row].allocation += extra;
1697 /* Check to see if we were allocated less height than we requested.
1698 * then shrink until we fit the size give.
1700 if (height > real_height)
1702 gint total_nshrink = nshrink;
1704 extra = height - real_height;
1705 while (total_nshrink > 0 && extra > 0)
1707 nshrink = total_nshrink;
1708 for (row = 0; row < priv->nrows; row++)
1709 if (priv->rows[row].shrink)
1711 gint allocation = priv->rows[row].allocation;
1713 priv->rows[row].allocation = MAX (1, (gint) priv->rows[row].allocation - extra / nshrink);
1714 extra -= allocation - priv->rows[row].allocation;
1716 if (priv->rows[row].allocation < 2)
1719 priv->rows[row].shrink = FALSE;
1728 gtk_table_size_allocate_pass2 (GtkTable *table)
1730 GtkTablePrivate *priv = table->priv;
1731 GtkTableChild *child;
1737 GtkAllocation allocation;
1738 GtkAllocation table_allocation, widget_allocation;
1739 GtkWidget *widget = GTK_WIDGET (table);
1741 children = priv->children;
1744 child = children->data;
1745 children = children->next;
1747 if (gtk_widget_get_visible (child->widget))
1749 GtkRequisition child_requisition;
1751 gtk_widget_get_preferred_size (child->widget,
1752 &child_requisition, NULL);
1754 gtk_widget_get_allocation (GTK_WIDGET (table), &table_allocation);
1755 x = table_allocation.x;
1756 y = table_allocation.y;
1760 for (col = 0; col < child->left_attach; col++)
1762 x += priv->cols[col].allocation;
1763 x += priv->cols[col].spacing;
1766 for (col = child->left_attach; col < child->right_attach; col++)
1768 max_width += priv->cols[col].allocation;
1769 if ((col + 1) < child->right_attach)
1770 max_width += priv->cols[col].spacing;
1773 for (row = 0; row < child->top_attach; row++)
1775 y += priv->rows[row].allocation;
1776 y += priv->rows[row].spacing;
1779 for (row = child->top_attach; row < child->bottom_attach; row++)
1781 max_height += priv->rows[row].allocation;
1782 if ((row + 1) < child->bottom_attach)
1783 max_height += priv->rows[row].spacing;
1788 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1789 allocation.x = x + (max_width - allocation.width) / 2;
1793 allocation.width = child_requisition.width;
1794 allocation.x = x + (max_width - allocation.width) / 2;
1799 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1800 allocation.y = y + (max_height - allocation.height) / 2;
1804 allocation.height = child_requisition.height;
1805 allocation.y = y + (max_height - allocation.height) / 2;
1808 gtk_widget_get_allocation (widget, &widget_allocation);
1809 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1810 allocation.x = widget_allocation.x + widget_allocation.width
1811 - (allocation.x - widget_allocation.x) - allocation.width;
1813 gtk_widget_size_allocate (child->widget, &allocation);