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/.
29 #include "gtkprivate.h"
34 struct _GtkTablePrivate
41 guint16 column_spacing;
46 guint homogeneous : 1;
62 CHILD_PROP_LEFT_ATTACH,
63 CHILD_PROP_RIGHT_ATTACH,
64 CHILD_PROP_TOP_ATTACH,
65 CHILD_PROP_BOTTOM_ATTACH,
73 static void gtk_table_finalize (GObject *object);
74 static void gtk_table_size_request (GtkWidget *widget,
75 GtkRequisition *requisition);
76 static void gtk_table_size_allocate (GtkWidget *widget,
77 GtkAllocation *allocation);
78 static void gtk_table_add (GtkContainer *container,
80 static void gtk_table_remove (GtkContainer *container,
82 static void gtk_table_forall (GtkContainer *container,
83 gboolean include_internals,
85 gpointer callback_data);
86 static void gtk_table_get_property (GObject *object,
90 static void gtk_table_set_property (GObject *object,
94 static void gtk_table_set_child_property (GtkContainer *container,
99 static void gtk_table_get_child_property (GtkContainer *container,
104 static GType gtk_table_child_type (GtkContainer *container);
107 static void gtk_table_size_request_init (GtkTable *table);
108 static void gtk_table_size_request_pass1 (GtkTable *table);
109 static void gtk_table_size_request_pass2 (GtkTable *table);
110 static void gtk_table_size_request_pass3 (GtkTable *table);
112 static void gtk_table_size_allocate_init (GtkTable *table);
113 static void gtk_table_size_allocate_pass1 (GtkTable *table);
114 static void gtk_table_size_allocate_pass2 (GtkTable *table);
117 G_DEFINE_TYPE (GtkTable, gtk_table, GTK_TYPE_CONTAINER)
120 gtk_table_class_init (GtkTableClass *class)
122 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
123 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
124 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
126 gobject_class->finalize = gtk_table_finalize;
128 gobject_class->get_property = gtk_table_get_property;
129 gobject_class->set_property = gtk_table_set_property;
131 widget_class->size_request = gtk_table_size_request;
132 widget_class->size_allocate = gtk_table_size_allocate;
134 container_class->add = gtk_table_add;
135 container_class->remove = gtk_table_remove;
136 container_class->forall = gtk_table_forall;
137 container_class->child_type = gtk_table_child_type;
138 container_class->set_child_property = gtk_table_set_child_property;
139 container_class->get_child_property = gtk_table_get_child_property;
142 g_object_class_install_property (gobject_class,
144 g_param_spec_uint ("n-rows",
146 P_("The number of rows in the table"),
150 GTK_PARAM_READWRITE));
151 g_object_class_install_property (gobject_class,
153 g_param_spec_uint ("n-columns",
155 P_("The number of columns in the table"),
159 GTK_PARAM_READWRITE));
160 g_object_class_install_property (gobject_class,
162 g_param_spec_uint ("row-spacing",
164 P_("The amount of space between two consecutive rows"),
168 GTK_PARAM_READWRITE));
169 g_object_class_install_property (gobject_class,
171 g_param_spec_uint ("column-spacing",
172 P_("Column spacing"),
173 P_("The amount of space between two consecutive columns"),
177 GTK_PARAM_READWRITE));
178 g_object_class_install_property (gobject_class,
180 g_param_spec_boolean ("homogeneous",
182 P_("If TRUE, the table cells are all the same width/height"),
184 GTK_PARAM_READWRITE));
186 gtk_container_class_install_child_property (container_class,
187 CHILD_PROP_LEFT_ATTACH,
188 g_param_spec_uint ("left-attach",
189 P_("Left attachment"),
190 P_("The column number to attach the left side of the child to"),
192 GTK_PARAM_READWRITE));
193 gtk_container_class_install_child_property (container_class,
194 CHILD_PROP_RIGHT_ATTACH,
195 g_param_spec_uint ("right-attach",
196 P_("Right attachment"),
197 P_("The column number to attach the right side of a child widget to"),
199 GTK_PARAM_READWRITE));
200 gtk_container_class_install_child_property (container_class,
201 CHILD_PROP_TOP_ATTACH,
202 g_param_spec_uint ("top-attach",
203 P_("Top attachment"),
204 P_("The row number to attach the top of a child widget to"),
206 GTK_PARAM_READWRITE));
207 gtk_container_class_install_child_property (container_class,
208 CHILD_PROP_BOTTOM_ATTACH,
209 g_param_spec_uint ("bottom-attach",
210 P_("Bottom attachment"),
211 P_("The row number to attach the bottom of the child to"),
213 GTK_PARAM_READWRITE));
214 gtk_container_class_install_child_property (container_class,
215 CHILD_PROP_X_OPTIONS,
216 g_param_spec_flags ("x-options",
217 P_("Horizontal options"),
218 P_("Options specifying the horizontal behaviour of the child"),
219 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
220 GTK_PARAM_READWRITE));
221 gtk_container_class_install_child_property (container_class,
222 CHILD_PROP_Y_OPTIONS,
223 g_param_spec_flags ("y-options",
224 P_("Vertical options"),
225 P_("Options specifying the vertical behaviour of the child"),
226 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
227 GTK_PARAM_READWRITE));
228 gtk_container_class_install_child_property (container_class,
229 CHILD_PROP_X_PADDING,
230 g_param_spec_uint ("x-padding",
231 P_("Horizontal padding"),
232 P_("Extra space to put between the child and its left and right neighbors, in pixels"),
234 GTK_PARAM_READWRITE));
235 gtk_container_class_install_child_property (container_class,
236 CHILD_PROP_Y_PADDING,
237 g_param_spec_uint ("y-padding",
238 P_("Vertical padding"),
239 P_("Extra space to put between the child and its upper and lower neighbors, in pixels"),
241 GTK_PARAM_READWRITE));
243 g_type_class_add_private (class, sizeof (GtkTablePrivate));
247 gtk_table_child_type (GtkContainer *container)
249 return GTK_TYPE_WIDGET;
253 gtk_table_get_property (GObject *object,
258 GtkTable *table = GTK_TABLE (object);
259 GtkTablePrivate *priv = table->priv;
264 g_value_set_uint (value, priv->nrows);
267 g_value_set_uint (value, priv->ncols);
269 case PROP_ROW_SPACING:
270 g_value_set_uint (value, priv->row_spacing);
272 case PROP_COLUMN_SPACING:
273 g_value_set_uint (value, priv->column_spacing);
275 case PROP_HOMOGENEOUS:
276 g_value_set_boolean (value, priv->homogeneous);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285 gtk_table_set_property (GObject *object,
290 GtkTable *table = GTK_TABLE (object);
291 GtkTablePrivate *priv = table->priv;
296 gtk_table_resize (table, g_value_get_uint (value), priv->ncols);
299 gtk_table_resize (table, priv->nrows, g_value_get_uint (value));
301 case PROP_ROW_SPACING:
302 gtk_table_set_row_spacings (table, g_value_get_uint (value));
304 case PROP_COLUMN_SPACING:
305 gtk_table_set_col_spacings (table, g_value_get_uint (value));
307 case PROP_HOMOGENEOUS:
308 gtk_table_set_homogeneous (table, g_value_get_boolean (value));
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 gtk_table_set_child_property (GtkContainer *container,
323 GtkTable *table = GTK_TABLE (container);
324 GtkTablePrivate *priv = table->priv;
325 GtkTableChild *table_child;
329 for (list = priv->children; list; list = list->next)
331 table_child = list->data;
333 if (table_child->widget == child)
338 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
344 case CHILD_PROP_LEFT_ATTACH:
345 table_child->left_attach = g_value_get_uint (value);
346 if (table_child->right_attach <= table_child->left_attach)
347 table_child->right_attach = table_child->left_attach + 1;
348 if (table_child->right_attach >= priv->ncols)
349 gtk_table_resize (table, priv->nrows, table_child->right_attach);
351 case CHILD_PROP_RIGHT_ATTACH:
352 table_child->right_attach = g_value_get_uint (value);
353 if (table_child->right_attach <= table_child->left_attach)
354 table_child->left_attach = table_child->right_attach - 1;
355 if (table_child->right_attach >= priv->ncols)
356 gtk_table_resize (table, priv->nrows, table_child->right_attach);
358 case CHILD_PROP_TOP_ATTACH:
359 table_child->top_attach = g_value_get_uint (value);
360 if (table_child->bottom_attach <= table_child->top_attach)
361 table_child->bottom_attach = table_child->top_attach + 1;
362 if (table_child->bottom_attach >= priv->nrows)
363 gtk_table_resize (table, table_child->bottom_attach, priv->ncols);
365 case CHILD_PROP_BOTTOM_ATTACH:
366 table_child->bottom_attach = g_value_get_uint (value);
367 if (table_child->bottom_attach <= table_child->top_attach)
368 table_child->top_attach = table_child->bottom_attach - 1;
369 if (table_child->bottom_attach >= priv->nrows)
370 gtk_table_resize (table, table_child->bottom_attach, priv->ncols);
372 case CHILD_PROP_X_OPTIONS:
373 table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
374 table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
375 table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
377 case CHILD_PROP_Y_OPTIONS:
378 table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
379 table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
380 table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
382 case CHILD_PROP_X_PADDING:
383 table_child->xpadding = g_value_get_uint (value);
385 case CHILD_PROP_Y_PADDING:
386 table_child->ypadding = g_value_get_uint (value);
389 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
392 if (gtk_widget_get_visible (child) &&
393 gtk_widget_get_visible (GTK_WIDGET (table)))
394 gtk_widget_queue_resize (child);
398 gtk_table_get_child_property (GtkContainer *container,
404 GtkTable *table = GTK_TABLE (container);
405 GtkTablePrivate *priv = table->priv;
406 GtkTableChild *table_child;
410 for (list = priv->children; list; list = list->next)
412 table_child = list->data;
414 if (table_child->widget == child)
419 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
425 case CHILD_PROP_LEFT_ATTACH:
426 g_value_set_uint (value, table_child->left_attach);
428 case CHILD_PROP_RIGHT_ATTACH:
429 g_value_set_uint (value, table_child->right_attach);
431 case CHILD_PROP_TOP_ATTACH:
432 g_value_set_uint (value, table_child->top_attach);
434 case CHILD_PROP_BOTTOM_ATTACH:
435 g_value_set_uint (value, table_child->bottom_attach);
437 case CHILD_PROP_X_OPTIONS:
438 g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
439 table_child->xshrink * GTK_SHRINK |
440 table_child->xfill * GTK_FILL));
442 case CHILD_PROP_Y_OPTIONS:
443 g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
444 table_child->yshrink * GTK_SHRINK |
445 table_child->yfill * GTK_FILL));
447 case CHILD_PROP_X_PADDING:
448 g_value_set_uint (value, table_child->xpadding);
450 case CHILD_PROP_Y_PADDING:
451 g_value_set_uint (value, table_child->ypadding);
454 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
460 gtk_table_init (GtkTable *table)
462 GtkTablePrivate *priv;
464 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
469 gtk_widget_set_has_window (GTK_WIDGET (table), FALSE);
470 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
472 priv->children = NULL;
477 priv->column_spacing = 0;
478 priv->row_spacing = 0;
479 priv->homogeneous = FALSE;
481 gtk_table_resize (table, 1, 1);
485 gtk_table_new (guint rows,
487 gboolean homogeneous)
490 GtkTablePrivate *priv;
497 table = g_object_new (GTK_TYPE_TABLE, NULL);
500 priv->homogeneous = (homogeneous ? TRUE : FALSE);
502 gtk_table_resize (table, rows, columns);
504 return GTK_WIDGET (table);
508 gtk_table_resize (GtkTable *table,
512 GtkTablePrivate *priv;
514 g_return_if_fail (GTK_IS_TABLE (table));
515 g_return_if_fail (n_rows > 0 && n_rows <= 65535);
516 g_return_if_fail (n_cols > 0 && n_cols <= 65535);
520 n_rows = MAX (n_rows, 1);
521 n_cols = MAX (n_cols, 1);
523 if (n_rows != priv->nrows ||
524 n_cols != priv->ncols)
528 for (list = priv->children; list; list = list->next)
530 GtkTableChild *child;
534 n_rows = MAX (n_rows, child->bottom_attach);
535 n_cols = MAX (n_cols, child->right_attach);
538 if (n_rows != priv->nrows)
543 priv->nrows = n_rows;
544 priv->rows = g_realloc (priv->rows, priv->nrows * sizeof (GtkTableRowCol));
546 for (; i < priv->nrows; i++)
548 priv->rows[i].requisition = 0;
549 priv->rows[i].allocation = 0;
550 priv->rows[i].spacing = priv->row_spacing;
551 priv->rows[i].need_expand = 0;
552 priv->rows[i].need_shrink = 0;
553 priv->rows[i].expand = 0;
554 priv->rows[i].shrink = 0;
557 g_object_notify (G_OBJECT (table), "n-rows");
560 if (n_cols != priv->ncols)
565 priv->ncols = n_cols;
566 priv->cols = g_realloc (priv->cols, priv->ncols * sizeof (GtkTableRowCol));
568 for (; i < priv->ncols; i++)
570 priv->cols[i].requisition = 0;
571 priv->cols[i].allocation = 0;
572 priv->cols[i].spacing = priv->column_spacing;
573 priv->cols[i].need_expand = 0;
574 priv->cols[i].need_shrink = 0;
575 priv->cols[i].expand = 0;
576 priv->cols[i].shrink = 0;
579 g_object_notify (G_OBJECT (table), "n-columns");
585 gtk_table_attach (GtkTable *table,
591 GtkAttachOptions xoptions,
592 GtkAttachOptions yoptions,
596 GtkTablePrivate *priv = table->priv;
597 GtkTableChild *table_child;
599 g_return_if_fail (GTK_IS_TABLE (table));
600 g_return_if_fail (GTK_IS_WIDGET (child));
601 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
603 /* g_return_if_fail (left_attach >= 0); */
604 g_return_if_fail (left_attach < right_attach);
605 /* g_return_if_fail (top_attach >= 0); */
606 g_return_if_fail (top_attach < bottom_attach);
610 if (right_attach >= priv->ncols)
611 gtk_table_resize (table, priv->nrows, right_attach);
613 if (bottom_attach >= priv->nrows)
614 gtk_table_resize (table, bottom_attach, priv->ncols);
616 table_child = g_new (GtkTableChild, 1);
617 table_child->widget = child;
618 table_child->left_attach = left_attach;
619 table_child->right_attach = right_attach;
620 table_child->top_attach = top_attach;
621 table_child->bottom_attach = bottom_attach;
622 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
623 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
624 table_child->xfill = (xoptions & GTK_FILL) != 0;
625 table_child->xpadding = xpadding;
626 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
627 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
628 table_child->yfill = (yoptions & GTK_FILL) != 0;
629 table_child->ypadding = ypadding;
631 priv->children = g_list_prepend (priv->children, table_child);
633 gtk_widget_set_parent (child, GTK_WIDGET (table));
637 gtk_table_attach_defaults (GtkTable *table,
644 gtk_table_attach (table, widget,
645 left_attach, right_attach,
646 top_attach, bottom_attach,
647 GTK_EXPAND | GTK_FILL,
648 GTK_EXPAND | GTK_FILL,
653 gtk_table_set_row_spacing (GtkTable *table,
657 GtkTablePrivate *priv;
659 g_return_if_fail (GTK_IS_TABLE (table));
663 g_return_if_fail (row < priv->nrows);
665 if (priv->rows[row].spacing != spacing)
667 priv->rows[row].spacing = spacing;
669 if (gtk_widget_get_visible (GTK_WIDGET (table)))
670 gtk_widget_queue_resize (GTK_WIDGET (table));
675 * gtk_table_get_row_spacing:
676 * @table: a #GtkTable
677 * @row: a row in the table, 0 indicates the first row
679 * Gets the amount of space between row @row, and
680 * row @row + 1. See gtk_table_set_row_spacing().
682 * Return value: the row spacing
685 gtk_table_get_row_spacing (GtkTable *table,
688 GtkTablePrivate *priv;
690 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
694 g_return_val_if_fail (row < priv->nrows - 1, 0);
696 return priv->rows[row].spacing;
700 gtk_table_set_col_spacing (GtkTable *table,
704 GtkTablePrivate *priv;
706 g_return_if_fail (GTK_IS_TABLE (table));
710 g_return_if_fail (column < priv->ncols);
712 if (priv->cols[column].spacing != spacing)
714 priv->cols[column].spacing = spacing;
716 if (gtk_widget_get_visible (GTK_WIDGET (table)))
717 gtk_widget_queue_resize (GTK_WIDGET (table));
722 * gtk_table_get_col_spacing:
723 * @table: a #GtkTable
724 * @column: a column in the table, 0 indicates the first column
726 * Gets the amount of space between column @col, and
727 * column @col + 1. See gtk_table_set_col_spacing().
729 * Return value: the column spacing
732 gtk_table_get_col_spacing (GtkTable *table,
735 GtkTablePrivate *priv;
737 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
741 g_return_val_if_fail (column < priv->ncols, 0);
743 return priv->cols[column].spacing;
747 gtk_table_set_row_spacings (GtkTable *table,
750 GtkTablePrivate *priv;
753 g_return_if_fail (GTK_IS_TABLE (table));
757 priv->row_spacing = spacing;
758 for (row = 0; row < priv->nrows; row++)
759 priv->rows[row].spacing = spacing;
761 if (gtk_widget_get_visible (GTK_WIDGET (table)))
762 gtk_widget_queue_resize (GTK_WIDGET (table));
764 g_object_notify (G_OBJECT (table), "row-spacing");
768 * gtk_table_get_default_row_spacing:
769 * @table: a #GtkTable
771 * Gets the default row spacing for the table. This is
772 * the spacing that will be used for newly added rows.
773 * (See gtk_table_set_row_spacings())
775 * Return value: the default row spacing
778 gtk_table_get_default_row_spacing (GtkTable *table)
780 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
782 return table->priv->row_spacing;
786 gtk_table_set_col_spacings (GtkTable *table,
789 GtkTablePrivate *priv;
792 g_return_if_fail (GTK_IS_TABLE (table));
796 priv->column_spacing = spacing;
797 for (col = 0; col < priv->ncols; col++)
798 priv->cols[col].spacing = spacing;
800 if (gtk_widget_get_visible (GTK_WIDGET (table)))
801 gtk_widget_queue_resize (GTK_WIDGET (table));
803 g_object_notify (G_OBJECT (table), "column-spacing");
807 * gtk_table_get_default_col_spacing:
808 * @table: a #GtkTable
810 * Gets the default column spacing for the table. This is
811 * the spacing that will be used for newly added columns.
812 * (See gtk_table_set_col_spacings())
814 * Return value: the default column spacing
817 gtk_table_get_default_col_spacing (GtkTable *table)
819 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
821 return table->priv->column_spacing;
825 gtk_table_set_homogeneous (GtkTable *table,
826 gboolean homogeneous)
828 GtkTablePrivate *priv;
830 g_return_if_fail (GTK_IS_TABLE (table));
834 homogeneous = (homogeneous != 0);
835 if (homogeneous != priv->homogeneous)
837 priv->homogeneous = homogeneous;
839 if (gtk_widget_get_visible (GTK_WIDGET (table)))
840 gtk_widget_queue_resize (GTK_WIDGET (table));
842 g_object_notify (G_OBJECT (table), "homogeneous");
847 * gtk_table_get_homogeneous:
848 * @table: a #GtkTable
850 * Returns whether the table cells are all constrained to the same
851 * width and height. (See gtk_table_set_homogenous ())
853 * Return value: %TRUE if the cells are all constrained to the same size
856 gtk_table_get_homogeneous (GtkTable *table)
858 g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
860 return table->priv->homogeneous;
864 * gtk_table_get_size:
865 * @table: a #GtkTable
866 * @rows: (out) (allow-none): return location for the number of
868 * @columns: (out) (allow-none): return location for the number
869 * of columns, or %NULL
871 * Returns the number of rows and columns in the table.
876 gtk_table_get_size (GtkTable *table,
880 GtkTablePrivate *priv;
882 g_return_if_fail (GTK_IS_TABLE (table));
890 *columns = priv->ncols;
894 gtk_table_finalize (GObject *object)
896 GtkTable *table = GTK_TABLE (object);
897 GtkTablePrivate *priv = table->priv;
902 G_OBJECT_CLASS (gtk_table_parent_class)->finalize (object);
906 gtk_table_size_request (GtkWidget *widget,
907 GtkRequisition *requisition)
909 GtkTable *table = GTK_TABLE (widget);
910 GtkTablePrivate *priv = table->priv;
914 requisition->width = 0;
915 requisition->height = 0;
917 gtk_table_size_request_init (table);
918 gtk_table_size_request_pass1 (table);
919 gtk_table_size_request_pass2 (table);
920 gtk_table_size_request_pass3 (table);
921 gtk_table_size_request_pass2 (table);
923 for (col = 0; col < priv->ncols; col++)
924 requisition->width += priv->cols[col].requisition;
925 for (col = 0; col + 1 < priv->ncols; col++)
926 requisition->width += priv->cols[col].spacing;
928 for (row = 0; row < priv->nrows; row++)
929 requisition->height += priv->rows[row].requisition;
930 for (row = 0; row + 1 < priv->nrows; row++)
931 requisition->height += priv->rows[row].spacing;
933 border_width = gtk_container_get_border_width (GTK_CONTAINER (table));
934 requisition->width += border_width * 2;
935 requisition->height += border_width * 2;
939 gtk_table_size_allocate (GtkWidget *widget,
940 GtkAllocation *allocation)
942 GtkTable *table = GTK_TABLE (widget);
944 gtk_widget_set_allocation (widget, allocation);
946 gtk_table_size_allocate_init (table);
947 gtk_table_size_allocate_pass1 (table);
948 gtk_table_size_allocate_pass2 (table);
952 gtk_table_add (GtkContainer *container,
955 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
959 gtk_table_remove (GtkContainer *container,
962 GtkTable *table = GTK_TABLE (container);
963 GtkTablePrivate *priv = table->priv;
964 GtkTableChild *child;
965 GtkWidget *widget_container = GTK_WIDGET (container);
968 children = priv->children;
972 child = children->data;
973 children = children->next;
975 if (child->widget == widget)
977 gboolean was_visible = gtk_widget_get_visible (widget);
979 gtk_widget_unparent (widget);
981 priv->children = g_list_remove (priv->children, child);
984 if (was_visible && gtk_widget_get_visible (widget_container))
985 gtk_widget_queue_resize (widget_container);
992 gtk_table_forall (GtkContainer *container,
993 gboolean include_internals,
994 GtkCallback callback,
995 gpointer callback_data)
997 GtkTable *table = GTK_TABLE (container);
998 GtkTablePrivate *priv = table->priv;
999 GtkTableChild *child;
1002 children = priv->children;
1006 child = children->data;
1007 children = children->next;
1009 (* callback) (child->widget, callback_data);
1014 gtk_table_size_request_init (GtkTable *table)
1016 GtkTablePrivate *priv = table->priv;
1017 GtkTableChild *child;
1021 for (row = 0; row < priv->nrows; row++)
1023 priv->rows[row].requisition = 0;
1024 priv->rows[row].expand = FALSE;
1026 for (col = 0; col < priv->ncols; col++)
1028 priv->cols[col].requisition = 0;
1029 priv->cols[col].expand = FALSE;
1032 children = priv->children;
1035 child = children->data;
1036 children = children->next;
1038 if (gtk_widget_get_visible (child->widget))
1039 gtk_widget_size_request (child->widget, NULL);
1041 if (child->left_attach == (child->right_attach - 1) && child->xexpand)
1042 priv->cols[child->left_attach].expand = TRUE;
1044 if (child->top_attach == (child->bottom_attach - 1) && child->yexpand)
1045 priv->rows[child->top_attach].expand = TRUE;
1050 gtk_table_size_request_pass1 (GtkTable *table)
1052 GtkTablePrivate *priv = table->priv;
1053 GtkTableChild *child;
1058 children = priv->children;
1061 child = children->data;
1062 children = children->next;
1064 if (gtk_widget_get_visible (child->widget))
1066 GtkRequisition child_requisition;
1067 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1069 /* Child spans a single column.
1071 if (child->left_attach == (child->right_attach - 1))
1073 width = child_requisition.width + child->xpadding * 2;
1074 priv->cols[child->left_attach].requisition = MAX (priv->cols[child->left_attach].requisition, width);
1077 /* Child spans a single row.
1079 if (child->top_attach == (child->bottom_attach - 1))
1081 height = child_requisition.height + child->ypadding * 2;
1082 priv->rows[child->top_attach].requisition = MAX (priv->rows[child->top_attach].requisition, height);
1089 gtk_table_size_request_pass2 (GtkTable *table)
1091 GtkTablePrivate *priv = table->priv;
1096 if (priv->homogeneous)
1101 for (col = 0; col < priv->ncols; col++)
1102 max_width = MAX (max_width, priv->cols[col].requisition);
1103 for (row = 0; row < priv->nrows; row++)
1104 max_height = MAX (max_height, priv->rows[row].requisition);
1106 for (col = 0; col < priv->ncols; col++)
1107 priv->cols[col].requisition = max_width;
1108 for (row = 0; row < priv->nrows; row++)
1109 priv->rows[row].requisition = max_height;
1114 gtk_table_size_request_pass3 (GtkTable *table)
1116 GtkTablePrivate *priv = table->priv;
1117 GtkTableChild *child;
1123 children = priv->children;
1126 child = children->data;
1127 children = children->next;
1129 if (gtk_widget_get_visible (child->widget))
1131 /* Child spans multiple columns.
1133 if (child->left_attach != (child->right_attach - 1))
1135 GtkRequisition child_requisition;
1137 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1139 /* Check and see if there is already enough space
1143 for (col = child->left_attach; col < child->right_attach; col++)
1145 width += priv->cols[col].requisition;
1146 if ((col + 1) < child->right_attach)
1147 width += priv->cols[col].spacing;
1150 /* If we need to request more space for this child to fill
1151 * its requisition, then divide up the needed space amongst the
1152 * columns it spans, favoring expandable columns if any.
1154 if (width < child_requisition.width + child->xpadding * 2)
1157 gboolean force_expand = FALSE;
1159 width = child_requisition.width + child->xpadding * 2 - width;
1161 for (col = child->left_attach; col < child->right_attach; col++)
1162 if (priv->cols[col].expand)
1167 n_expand = (child->right_attach - child->left_attach);
1168 force_expand = TRUE;
1171 for (col = child->left_attach; col < child->right_attach; col++)
1172 if (force_expand || priv->cols[col].expand)
1174 extra = width / n_expand;
1175 priv->cols[col].requisition += extra;
1182 /* Child spans multiple rows.
1184 if (child->top_attach != (child->bottom_attach - 1))
1186 GtkRequisition child_requisition;
1188 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1190 /* Check and see if there is already enough space
1194 for (row = child->top_attach; row < child->bottom_attach; row++)
1196 height += priv->rows[row].requisition;
1197 if ((row + 1) < child->bottom_attach)
1198 height += priv->rows[row].spacing;
1201 /* If we need to request more space for this child to fill
1202 * its requisition, then divide up the needed space amongst the
1203 * rows it spans, favoring expandable rows if any.
1205 if (height < child_requisition.height + child->ypadding * 2)
1208 gboolean force_expand = FALSE;
1210 height = child_requisition.height + child->ypadding * 2 - height;
1212 for (row = child->top_attach; row < child->bottom_attach; row++)
1214 if (priv->rows[row].expand)
1220 n_expand = (child->bottom_attach - child->top_attach);
1221 force_expand = TRUE;
1224 for (row = child->top_attach; row < child->bottom_attach; row++)
1225 if (force_expand || priv->rows[row].expand)
1227 extra = height / n_expand;
1228 priv->rows[row].requisition += extra;
1239 gtk_table_size_allocate_init (GtkTable *table)
1241 GtkTablePrivate *priv = table->priv;
1242 GtkTableChild *child;
1248 /* Initialize the rows and cols.
1249 * By default, rows and cols do not expand and do shrink.
1250 * Those values are modified by the children that occupy
1251 * the rows and cols.
1253 for (col = 0; col < priv->ncols; col++)
1255 priv->cols[col].allocation = priv->cols[col].requisition;
1256 priv->cols[col].need_expand = FALSE;
1257 priv->cols[col].need_shrink = TRUE;
1258 priv->cols[col].expand = FALSE;
1259 priv->cols[col].shrink = TRUE;
1260 priv->cols[col].empty = TRUE;
1262 for (row = 0; row < priv->nrows; row++)
1264 priv->rows[row].allocation = priv->rows[row].requisition;
1265 priv->rows[row].need_expand = FALSE;
1266 priv->rows[row].need_shrink = TRUE;
1267 priv->rows[row].expand = FALSE;
1268 priv->rows[row].shrink = TRUE;
1269 priv->rows[row].empty = TRUE;
1272 /* Loop over all the children and adjust the row and col values
1273 * based on whether the children want to be allowed to expand
1274 * or shrink. This loop handles children that occupy a single
1277 children = priv->children;
1280 child = children->data;
1281 children = children->next;
1283 if (gtk_widget_get_visible (child->widget))
1285 if (child->left_attach == (child->right_attach - 1))
1288 priv->cols[child->left_attach].expand = TRUE;
1290 if (!child->xshrink)
1291 priv->cols[child->left_attach].shrink = FALSE;
1293 priv->cols[child->left_attach].empty = FALSE;
1296 if (child->top_attach == (child->bottom_attach - 1))
1299 priv->rows[child->top_attach].expand = TRUE;
1301 if (!child->yshrink)
1302 priv->rows[child->top_attach].shrink = FALSE;
1304 priv->rows[child->top_attach].empty = FALSE;
1309 /* Loop over all the children again and this time handle children
1310 * which span multiple rows or columns.
1312 children = priv->children;
1315 child = children->data;
1316 children = children->next;
1318 if (gtk_widget_get_visible (child->widget))
1320 if (child->left_attach != (child->right_attach - 1))
1322 for (col = child->left_attach; col < child->right_attach; col++)
1323 priv->cols[col].empty = FALSE;
1328 for (col = child->left_attach; col < child->right_attach; col++)
1329 if (priv->cols[col].expand)
1336 for (col = child->left_attach; col < child->right_attach; col++)
1337 priv->cols[col].need_expand = TRUE;
1340 if (!child->xshrink)
1343 for (col = child->left_attach; col < child->right_attach; col++)
1344 if (!priv->cols[col].shrink)
1351 for (col = child->left_attach; col < child->right_attach; col++)
1352 priv->cols[col].need_shrink = FALSE;
1356 if (child->top_attach != (child->bottom_attach - 1))
1358 for (row = child->top_attach; row < child->bottom_attach; row++)
1359 priv->rows[row].empty = FALSE;
1364 for (row = child->top_attach; row < child->bottom_attach; row++)
1365 if (priv->rows[row].expand)
1372 for (row = child->top_attach; row < child->bottom_attach; row++)
1373 priv->rows[row].need_expand = TRUE;
1376 if (!child->yshrink)
1379 for (row = child->top_attach; row < child->bottom_attach; row++)
1380 if (!priv->rows[row].shrink)
1387 for (row = child->top_attach; row < child->bottom_attach; row++)
1388 priv->rows[row].need_shrink = FALSE;
1394 /* Loop over the columns and set the expand and shrink values
1395 * if the column can be expanded or shrunk.
1397 for (col = 0; col < priv->ncols; col++)
1399 if (priv->cols[col].empty)
1401 priv->cols[col].expand = FALSE;
1402 priv->cols[col].shrink = FALSE;
1406 if (priv->cols[col].need_expand)
1407 priv->cols[col].expand = TRUE;
1408 if (!priv->cols[col].need_shrink)
1409 priv->cols[col].shrink = FALSE;
1413 /* Loop over the rows and set the expand and shrink values
1414 * if the row can be expanded or shrunk.
1416 for (row = 0; row < priv->nrows; row++)
1418 if (priv->rows[row].empty)
1420 priv->rows[row].expand = FALSE;
1421 priv->rows[row].shrink = FALSE;
1425 if (priv->rows[row].need_expand)
1426 priv->rows[row].expand = TRUE;
1427 if (!priv->rows[row].need_shrink)
1428 priv->rows[row].shrink = FALSE;
1434 gtk_table_size_allocate_pass1 (GtkTable *table)
1436 GtkTablePrivate *priv = table->priv;
1437 GtkAllocation allocation;
1447 /* If we were allocated more space than we requested
1448 * then we have to expand any expandable rows and columns
1449 * to fill in the extra space.
1451 gtk_widget_get_allocation (GTK_WIDGET (table), &allocation);
1452 border_width = gtk_container_get_border_width (GTK_CONTAINER (table));
1453 real_width = allocation.width - border_width * 2;
1454 real_height = allocation.height - border_width * 2;
1456 if (priv->homogeneous)
1458 if (!priv->children)
1463 for (col = 0; col < priv->ncols; col++)
1464 if (priv->cols[col].expand)
1473 for (col = 0; col + 1 < priv->ncols; col++)
1474 width -= priv->cols[col].spacing;
1476 for (col = 0; col < priv->ncols; col++)
1478 extra = width / (priv->ncols - col);
1479 priv->cols[col].allocation = MAX (1, extra);
1490 for (col = 0; col < priv->ncols; col++)
1492 width += priv->cols[col].requisition;
1493 if (priv->cols[col].expand)
1495 if (priv->cols[col].shrink)
1498 for (col = 0; col + 1 < priv->ncols; col++)
1499 width += priv->cols[col].spacing;
1501 /* Check to see if we were allocated more width than we requested.
1503 if ((width < real_width) && (nexpand >= 1))
1505 width = real_width - width;
1507 for (col = 0; col < priv->ncols; col++)
1508 if (priv->cols[col].expand)
1510 extra = width / nexpand;
1511 priv->cols[col].allocation += extra;
1518 /* Check to see if we were allocated less width than we requested,
1519 * then shrink until we fit the size give.
1521 if (width > real_width)
1523 gint total_nshrink = nshrink;
1525 extra = width - real_width;
1526 while (total_nshrink > 0 && extra > 0)
1528 nshrink = total_nshrink;
1529 for (col = 0; col < priv->ncols; col++)
1530 if (priv->cols[col].shrink)
1532 gint allocation = priv->cols[col].allocation;
1534 priv->cols[col].allocation = MAX (1, (gint) priv->cols[col].allocation - extra / nshrink);
1535 extra -= allocation - priv->cols[col].allocation;
1537 if (priv->cols[col].allocation < 2)
1540 priv->cols[col].shrink = FALSE;
1547 if (priv->homogeneous)
1549 if (!priv->children)
1554 for (row = 0; row < priv->nrows; row++)
1555 if (priv->rows[row].expand)
1563 height = real_height;
1565 for (row = 0; row + 1 < priv->nrows; row++)
1566 height -= priv->rows[row].spacing;
1568 for (row = 0; row < priv->nrows; row++)
1570 extra = height / (priv->nrows - row);
1571 priv->rows[row].allocation = MAX (1, extra);
1582 for (row = 0; row < priv->nrows; row++)
1584 height += priv->rows[row].requisition;
1585 if (priv->rows[row].expand)
1587 if (priv->rows[row].shrink)
1590 for (row = 0; row + 1 < priv->nrows; row++)
1591 height += priv->rows[row].spacing;
1593 /* Check to see if we were allocated more height than we requested.
1595 if ((height < real_height) && (nexpand >= 1))
1597 height = real_height - height;
1599 for (row = 0; row < priv->nrows; row++)
1600 if (priv->rows[row].expand)
1602 extra = height / nexpand;
1603 priv->rows[row].allocation += extra;
1610 /* Check to see if we were allocated less height than we requested.
1611 * then shrink until we fit the size give.
1613 if (height > real_height)
1615 gint total_nshrink = nshrink;
1617 extra = height - real_height;
1618 while (total_nshrink > 0 && extra > 0)
1620 nshrink = total_nshrink;
1621 for (row = 0; row < priv->nrows; row++)
1622 if (priv->rows[row].shrink)
1624 gint allocation = priv->rows[row].allocation;
1626 priv->rows[row].allocation = MAX (1, (gint) priv->rows[row].allocation - extra / nshrink);
1627 extra -= allocation - priv->rows[row].allocation;
1629 if (priv->rows[row].allocation < 2)
1632 priv->rows[row].shrink = FALSE;
1641 gtk_table_size_allocate_pass2 (GtkTable *table)
1643 GtkTablePrivate *priv = table->priv;
1644 GtkTableChild *child;
1650 GtkAllocation allocation;
1651 GtkAllocation table_allocation, widget_allocation;
1652 GtkWidget *widget = GTK_WIDGET (table);
1654 children = priv->children;
1657 child = children->data;
1658 children = children->next;
1660 if (gtk_widget_get_visible (child->widget))
1662 GtkRequisition child_requisition;
1665 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1667 gtk_widget_get_allocation (GTK_WIDGET (table), &table_allocation);
1668 border_width = gtk_container_get_border_width (GTK_CONTAINER (table));
1669 x = table_allocation.x + border_width;
1670 y = table_allocation.y + border_width;
1674 for (col = 0; col < child->left_attach; col++)
1676 x += priv->cols[col].allocation;
1677 x += priv->cols[col].spacing;
1680 for (col = child->left_attach; col < child->right_attach; col++)
1682 max_width += priv->cols[col].allocation;
1683 if ((col + 1) < child->right_attach)
1684 max_width += priv->cols[col].spacing;
1687 for (row = 0; row < child->top_attach; row++)
1689 y += priv->rows[row].allocation;
1690 y += priv->rows[row].spacing;
1693 for (row = child->top_attach; row < child->bottom_attach; row++)
1695 max_height += priv->rows[row].allocation;
1696 if ((row + 1) < child->bottom_attach)
1697 max_height += priv->rows[row].spacing;
1702 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1703 allocation.x = x + (max_width - allocation.width) / 2;
1707 allocation.width = child_requisition.width;
1708 allocation.x = x + (max_width - allocation.width) / 2;
1713 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1714 allocation.y = y + (max_height - allocation.height) / 2;
1718 allocation.height = child_requisition.height;
1719 allocation.y = y + (max_height - allocation.height) / 2;
1722 gtk_widget_get_allocation (widget, &widget_allocation);
1723 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1724 allocation.x = widget_allocation.x + widget_allocation.width
1725 - (allocation.x - widget_allocation.x) - allocation.width;
1727 gtk_widget_size_allocate (child->widget, &allocation);