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"
46 CHILD_PROP_LEFT_ATTACH,
47 CHILD_PROP_RIGHT_ATTACH,
48 CHILD_PROP_TOP_ATTACH,
49 CHILD_PROP_BOTTOM_ATTACH,
57 static void gtk_table_class_init (GtkTableClass *klass);
58 static void gtk_table_init (GtkTable *table);
59 static void gtk_table_finalize (GObject *object);
60 static void gtk_table_size_request (GtkWidget *widget,
61 GtkRequisition *requisition);
62 static void gtk_table_size_allocate (GtkWidget *widget,
63 GtkAllocation *allocation);
64 static void gtk_table_add (GtkContainer *container,
66 static void gtk_table_remove (GtkContainer *container,
68 static void gtk_table_forall (GtkContainer *container,
69 gboolean include_internals,
71 gpointer callback_data);
72 static void gtk_table_get_property (GObject *object,
76 static void gtk_table_set_property (GObject *object,
80 static void gtk_table_set_child_property (GtkContainer *container,
85 static void gtk_table_get_child_property (GtkContainer *container,
90 static GType gtk_table_child_type (GtkContainer *container);
93 static void gtk_table_size_request_init (GtkTable *table);
94 static void gtk_table_size_request_pass1 (GtkTable *table);
95 static void gtk_table_size_request_pass2 (GtkTable *table);
96 static void gtk_table_size_request_pass3 (GtkTable *table);
98 static void gtk_table_size_allocate_init (GtkTable *table);
99 static void gtk_table_size_allocate_pass1 (GtkTable *table);
100 static void gtk_table_size_allocate_pass2 (GtkTable *table);
103 static GtkContainerClass *parent_class = NULL;
107 gtk_table_get_type (void)
109 static GType table_type = 0;
113 static const GTypeInfo table_info =
115 sizeof (GtkTableClass),
118 (GClassInitFunc) gtk_table_class_init,
123 (GInstanceInitFunc) gtk_table_init,
126 table_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTable",
134 gtk_table_class_init (GtkTableClass *class)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
137 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
138 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
140 parent_class = g_type_class_peek_parent (class);
142 gobject_class->finalize = gtk_table_finalize;
144 gobject_class->get_property = gtk_table_get_property;
145 gobject_class->set_property = gtk_table_set_property;
147 widget_class->size_request = gtk_table_size_request;
148 widget_class->size_allocate = gtk_table_size_allocate;
150 container_class->add = gtk_table_add;
151 container_class->remove = gtk_table_remove;
152 container_class->forall = gtk_table_forall;
153 container_class->child_type = gtk_table_child_type;
154 container_class->set_child_property = gtk_table_set_child_property;
155 container_class->get_child_property = gtk_table_get_child_property;
158 g_object_class_install_property (gobject_class,
160 g_param_spec_uint ("n-rows",
162 P_("The number of rows in the table"),
166 GTK_PARAM_READWRITE));
167 g_object_class_install_property (gobject_class,
169 g_param_spec_uint ("n-columns",
171 P_("The number of columns in the table"),
175 GTK_PARAM_READWRITE));
176 g_object_class_install_property (gobject_class,
178 g_param_spec_uint ("row-spacing",
180 P_("The amount of space between two consecutive rows"),
184 GTK_PARAM_READWRITE));
185 g_object_class_install_property (gobject_class,
187 g_param_spec_uint ("column-spacing",
188 P_("Column spacing"),
189 P_("The amount of space between two consecutive columns"),
193 GTK_PARAM_READWRITE));
194 g_object_class_install_property (gobject_class,
196 g_param_spec_boolean ("homogeneous",
198 P_("If TRUE this means the table cells are all the same width/height"),
200 GTK_PARAM_READWRITE));
202 gtk_container_class_install_child_property (container_class,
203 CHILD_PROP_LEFT_ATTACH,
204 g_param_spec_uint ("left-attach",
205 P_("Left attachment"),
206 P_("The column number to attach the left side of the child to"),
208 GTK_PARAM_READWRITE));
209 gtk_container_class_install_child_property (container_class,
210 CHILD_PROP_RIGHT_ATTACH,
211 g_param_spec_uint ("right-attach",
212 P_("Right attachment"),
213 P_("The column number to attach the right side of a child widget to"),
215 GTK_PARAM_READWRITE));
216 gtk_container_class_install_child_property (container_class,
217 CHILD_PROP_TOP_ATTACH,
218 g_param_spec_uint ("top-attach",
219 P_("Top attachment"),
220 P_("The row number to attach the top of a child widget to"),
222 GTK_PARAM_READWRITE));
223 gtk_container_class_install_child_property (container_class,
224 CHILD_PROP_BOTTOM_ATTACH,
225 g_param_spec_uint ("bottom-attach",
226 P_("Bottom attachment"),
227 P_("The row number to attach the bottom of the child to"),
229 GTK_PARAM_READWRITE));
230 gtk_container_class_install_child_property (container_class,
231 CHILD_PROP_X_OPTIONS,
232 g_param_spec_flags ("x-options",
233 P_("Horizontal options"),
234 P_("Options specifying the horizontal behaviour of the child"),
235 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
236 GTK_PARAM_READWRITE));
237 gtk_container_class_install_child_property (container_class,
238 CHILD_PROP_Y_OPTIONS,
239 g_param_spec_flags ("y-options",
240 P_("Vertical options"),
241 P_("Options specifying the vertical behaviour of the child"),
242 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
243 GTK_PARAM_READWRITE));
244 gtk_container_class_install_child_property (container_class,
245 CHILD_PROP_X_PADDING,
246 g_param_spec_uint ("x-padding",
247 P_("Horizontal padding"),
248 P_("Extra space to put between the child and its left and right neighbors, in pixels"),
250 GTK_PARAM_READWRITE));
251 gtk_container_class_install_child_property (container_class,
252 CHILD_PROP_Y_PADDING,
253 g_param_spec_uint ("y-padding",
254 P_("Vertical padding"),
255 P_("Extra space to put between the child and its upper and lower neighbors, in pixels"),
257 GTK_PARAM_READWRITE));
261 gtk_table_child_type (GtkContainer *container)
263 return GTK_TYPE_WIDGET;
267 gtk_table_get_property (GObject *object,
274 table = GTK_TABLE (object);
279 g_value_set_uint (value, table->nrows);
282 g_value_set_uint (value, table->ncols);
284 case PROP_ROW_SPACING:
285 g_value_set_uint (value, table->row_spacing);
287 case PROP_COLUMN_SPACING:
288 g_value_set_uint (value, table->column_spacing);
290 case PROP_HOMOGENEOUS:
291 g_value_set_boolean (value, table->homogeneous);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gtk_table_set_property (GObject *object,
307 table = GTK_TABLE (object);
312 gtk_table_resize (table, g_value_get_uint (value), table->ncols);
315 gtk_table_resize (table, table->nrows, g_value_get_uint (value));
317 case PROP_ROW_SPACING:
318 gtk_table_set_row_spacings (table, g_value_get_uint (value));
320 case PROP_COLUMN_SPACING:
321 gtk_table_set_col_spacings (table, g_value_get_uint (value));
323 case PROP_HOMOGENEOUS:
324 gtk_table_set_homogeneous (table, g_value_get_boolean (value));
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gtk_table_set_child_property (GtkContainer *container,
339 GtkTable *table = GTK_TABLE (container);
340 GtkTableChild *table_child;
344 for (list = table->children; list; list = list->next)
346 table_child = list->data;
348 if (table_child->widget == child)
353 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
359 case CHILD_PROP_LEFT_ATTACH:
360 table_child->left_attach = g_value_get_uint (value);
361 if (table_child->right_attach <= table_child->left_attach)
362 table_child->right_attach = table_child->left_attach + 1;
363 if (table_child->right_attach >= table->ncols)
364 gtk_table_resize (table, table->nrows, table_child->right_attach);
366 case CHILD_PROP_RIGHT_ATTACH:
367 table_child->right_attach = g_value_get_uint (value);
368 if (table_child->right_attach <= table_child->left_attach)
369 table_child->left_attach = table_child->right_attach - 1;
370 if (table_child->right_attach >= table->ncols)
371 gtk_table_resize (table, table->nrows, table_child->right_attach);
373 case CHILD_PROP_TOP_ATTACH:
374 table_child->top_attach = g_value_get_uint (value);
375 if (table_child->bottom_attach <= table_child->top_attach)
376 table_child->bottom_attach = table_child->top_attach + 1;
377 if (table_child->bottom_attach >= table->nrows)
378 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
380 case CHILD_PROP_BOTTOM_ATTACH:
381 table_child->bottom_attach = g_value_get_uint (value);
382 if (table_child->bottom_attach <= table_child->top_attach)
383 table_child->top_attach = table_child->bottom_attach - 1;
384 if (table_child->bottom_attach >= table->nrows)
385 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
387 case CHILD_PROP_X_OPTIONS:
388 table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
389 table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
390 table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
392 case CHILD_PROP_Y_OPTIONS:
393 table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
394 table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
395 table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
397 case CHILD_PROP_X_PADDING:
398 table_child->xpadding = g_value_get_uint (value);
400 case CHILD_PROP_Y_PADDING:
401 table_child->ypadding = g_value_get_uint (value);
404 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
407 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
408 gtk_widget_queue_resize (child);
412 gtk_table_get_child_property (GtkContainer *container,
418 GtkTable *table = GTK_TABLE (container);
419 GtkTableChild *table_child;
423 for (list = table->children; list; list = list->next)
425 table_child = list->data;
427 if (table_child->widget == child)
432 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
438 case CHILD_PROP_LEFT_ATTACH:
439 g_value_set_uint (value, table_child->left_attach);
441 case CHILD_PROP_RIGHT_ATTACH:
442 g_value_set_uint (value, table_child->right_attach);
444 case CHILD_PROP_TOP_ATTACH:
445 g_value_set_uint (value, table_child->top_attach);
447 case CHILD_PROP_BOTTOM_ATTACH:
448 g_value_set_uint (value, table_child->bottom_attach);
450 case CHILD_PROP_X_OPTIONS:
451 g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
452 table_child->xshrink * GTK_SHRINK |
453 table_child->xfill * GTK_FILL));
455 case CHILD_PROP_Y_OPTIONS:
456 g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
457 table_child->yshrink * GTK_SHRINK |
458 table_child->yfill * GTK_FILL));
460 case CHILD_PROP_X_PADDING:
461 g_value_set_uint (value, table_child->xpadding);
463 case CHILD_PROP_Y_PADDING:
464 g_value_set_uint (value, table_child->ypadding);
467 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
473 gtk_table_init (GtkTable *table)
475 GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
476 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
478 table->children = NULL;
483 table->column_spacing = 0;
484 table->row_spacing = 0;
485 table->homogeneous = FALSE;
487 gtk_table_resize (table, 1, 1);
491 gtk_table_new (guint rows,
493 gboolean homogeneous)
502 table = g_object_new (GTK_TYPE_TABLE, NULL);
504 table->homogeneous = (homogeneous ? TRUE : FALSE);
506 gtk_table_resize (table, rows, columns);
508 return GTK_WIDGET (table);
512 gtk_table_resize (GtkTable *table,
516 g_return_if_fail (GTK_IS_TABLE (table));
517 g_return_if_fail (n_rows > 0 && n_rows < 65536);
518 g_return_if_fail (n_cols > 0 && n_cols < 65536);
520 n_rows = MAX (n_rows, 1);
521 n_cols = MAX (n_cols, 1);
523 if (n_rows != table->nrows ||
524 n_cols != table->ncols)
528 for (list = table->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 != table->nrows)
543 table->nrows = n_rows;
544 table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
546 for (; i < table->nrows; i++)
548 table->rows[i].requisition = 0;
549 table->rows[i].allocation = 0;
550 table->rows[i].spacing = table->row_spacing;
551 table->rows[i].need_expand = 0;
552 table->rows[i].need_shrink = 0;
553 table->rows[i].expand = 0;
554 table->rows[i].shrink = 0;
557 g_object_notify (G_OBJECT (table), "n-rows");
560 if (n_cols != table->ncols)
565 table->ncols = n_cols;
566 table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
568 for (; i < table->ncols; i++)
570 table->cols[i].requisition = 0;
571 table->cols[i].allocation = 0;
572 table->cols[i].spacing = table->column_spacing;
573 table->cols[i].need_expand = 0;
574 table->cols[i].need_shrink = 0;
575 table->cols[i].expand = 0;
576 table->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 GtkTableChild *table_child;
598 g_return_if_fail (GTK_IS_TABLE (table));
599 g_return_if_fail (GTK_IS_WIDGET (child));
600 g_return_if_fail (child->parent == NULL);
602 /* g_return_if_fail (left_attach >= 0); */
603 g_return_if_fail (left_attach < right_attach);
604 /* g_return_if_fail (top_attach >= 0); */
605 g_return_if_fail (top_attach < bottom_attach);
607 if (right_attach >= table->ncols)
608 gtk_table_resize (table, table->nrows, right_attach);
610 if (bottom_attach >= table->nrows)
611 gtk_table_resize (table, bottom_attach, table->ncols);
613 table_child = g_new (GtkTableChild, 1);
614 table_child->widget = child;
615 table_child->left_attach = left_attach;
616 table_child->right_attach = right_attach;
617 table_child->top_attach = top_attach;
618 table_child->bottom_attach = bottom_attach;
619 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
620 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
621 table_child->xfill = (xoptions & GTK_FILL) != 0;
622 table_child->xpadding = xpadding;
623 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
624 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
625 table_child->yfill = (yoptions & GTK_FILL) != 0;
626 table_child->ypadding = ypadding;
628 table->children = g_list_prepend (table->children, table_child);
630 gtk_widget_set_parent (child, GTK_WIDGET (table));
634 gtk_table_attach_defaults (GtkTable *table,
641 gtk_table_attach (table, widget,
642 left_attach, right_attach,
643 top_attach, bottom_attach,
644 GTK_EXPAND | GTK_FILL,
645 GTK_EXPAND | GTK_FILL,
650 gtk_table_set_row_spacing (GtkTable *table,
654 g_return_if_fail (GTK_IS_TABLE (table));
655 g_return_if_fail (row < table->nrows);
657 if (table->rows[row].spacing != spacing)
659 table->rows[row].spacing = spacing;
661 if (GTK_WIDGET_VISIBLE (table))
662 gtk_widget_queue_resize (GTK_WIDGET (table));
667 * gtk_table_get_row_spacing:
668 * @table: a #GtkTable
669 * @row: a row in the table, 0 indicates the first row
671 * Gets the amount of space between row @row, and
672 * row @row + 1. See gtk_table_set_row_spacing().
674 * Return value: the row spacing
677 gtk_table_get_row_spacing (GtkTable *table,
680 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
681 g_return_val_if_fail (row < table->nrows - 1, 0);
683 return table->rows[row].spacing;
687 gtk_table_set_col_spacing (GtkTable *table,
691 g_return_if_fail (GTK_IS_TABLE (table));
692 g_return_if_fail (column < table->ncols);
694 if (table->cols[column].spacing != spacing)
696 table->cols[column].spacing = spacing;
698 if (GTK_WIDGET_VISIBLE (table))
699 gtk_widget_queue_resize (GTK_WIDGET (table));
704 * gtk_table_get_col_spacing:
705 * @table: a #GtkTable
706 * @column: a column in the table, 0 indicates the first column
708 * Gets the amount of space between column @col, and
709 * column @col + 1. See gtk_table_set_col_spacing().
711 * Return value: the column spacing
714 gtk_table_get_col_spacing (GtkTable *table,
717 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
718 g_return_val_if_fail (column < table->ncols, 0);
720 return table->cols[column].spacing;
724 gtk_table_set_row_spacings (GtkTable *table,
729 g_return_if_fail (GTK_IS_TABLE (table));
731 table->row_spacing = spacing;
732 for (row = 0; row < table->nrows; row++)
733 table->rows[row].spacing = spacing;
735 if (GTK_WIDGET_VISIBLE (table))
736 gtk_widget_queue_resize (GTK_WIDGET (table));
738 g_object_notify (G_OBJECT (table), "row-spacing");
742 * gtk_table_get_default_row_spacing:
743 * @table: a #GtkTable
745 * Gets the default row spacing for the table. This is
746 * the spacing that will be used for newly added rows.
747 * (See gtk_table_set_row_spacings())
749 * Returns value: the default row spacing
752 gtk_table_get_default_row_spacing (GtkTable *table)
754 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
756 return table->row_spacing;
760 gtk_table_set_col_spacings (GtkTable *table,
765 g_return_if_fail (GTK_IS_TABLE (table));
767 table->column_spacing = spacing;
768 for (col = 0; col < table->ncols; col++)
769 table->cols[col].spacing = spacing;
771 if (GTK_WIDGET_VISIBLE (table))
772 gtk_widget_queue_resize (GTK_WIDGET (table));
774 g_object_notify (G_OBJECT (table), "column-spacing");
778 * gtk_table_get_default_col_spacing:
779 * @table: a #GtkTable
781 * Gets the default column spacing for the table. This is
782 * the spacing that will be used for newly added columns.
783 * (See gtk_table_set_col_spacings())
785 * Returns value: the default column spacing
788 gtk_table_get_default_col_spacing (GtkTable *table)
790 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
792 return table->column_spacing;
796 gtk_table_set_homogeneous (GtkTable *table,
797 gboolean homogeneous)
799 g_return_if_fail (GTK_IS_TABLE (table));
801 homogeneous = (homogeneous != 0);
802 if (homogeneous != table->homogeneous)
804 table->homogeneous = homogeneous;
806 if (GTK_WIDGET_VISIBLE (table))
807 gtk_widget_queue_resize (GTK_WIDGET (table));
809 g_object_notify (G_OBJECT (table), "homogeneous");
814 * gtk_table_get_homogeneous:
815 * @table: a #GtkTable
817 * Returns whether the table cells are all constrained to the same
818 * width and height. (See gtk_table_set_homogenous ())
820 * Return value: %TRUE if the cells are all constrained to the same size
823 gtk_table_get_homogeneous (GtkTable *table)
825 g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
827 return table->homogeneous;
831 gtk_table_finalize (GObject *object)
835 g_return_if_fail (GTK_IS_TABLE (object));
837 table = GTK_TABLE (object);
839 g_free (table->rows);
840 g_free (table->cols);
842 G_OBJECT_CLASS (parent_class)->finalize (object);
846 gtk_table_size_request (GtkWidget *widget,
847 GtkRequisition *requisition)
852 g_return_if_fail (GTK_IS_TABLE (widget));
853 g_return_if_fail (requisition != NULL);
855 table = GTK_TABLE (widget);
857 requisition->width = 0;
858 requisition->height = 0;
860 gtk_table_size_request_init (table);
861 gtk_table_size_request_pass1 (table);
862 gtk_table_size_request_pass2 (table);
863 gtk_table_size_request_pass3 (table);
864 gtk_table_size_request_pass2 (table);
866 for (col = 0; col < table->ncols; col++)
867 requisition->width += table->cols[col].requisition;
868 for (col = 0; col + 1 < table->ncols; col++)
869 requisition->width += table->cols[col].spacing;
871 for (row = 0; row < table->nrows; row++)
872 requisition->height += table->rows[row].requisition;
873 for (row = 0; row + 1 < table->nrows; row++)
874 requisition->height += table->rows[row].spacing;
876 requisition->width += GTK_CONTAINER (table)->border_width * 2;
877 requisition->height += GTK_CONTAINER (table)->border_width * 2;
881 gtk_table_size_allocate (GtkWidget *widget,
882 GtkAllocation *allocation)
886 g_return_if_fail (GTK_IS_TABLE (widget));
887 g_return_if_fail (allocation != NULL);
889 widget->allocation = *allocation;
890 table = GTK_TABLE (widget);
892 gtk_table_size_allocate_init (table);
893 gtk_table_size_allocate_pass1 (table);
894 gtk_table_size_allocate_pass2 (table);
898 gtk_table_add (GtkContainer *container,
901 g_return_if_fail (GTK_IS_TABLE (container));
902 g_return_if_fail (widget != NULL);
904 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
908 gtk_table_remove (GtkContainer *container,
912 GtkTableChild *child;
915 g_return_if_fail (GTK_IS_TABLE (container));
916 g_return_if_fail (widget != NULL);
918 table = GTK_TABLE (container);
919 children = table->children;
923 child = children->data;
924 children = children->next;
926 if (child->widget == widget)
928 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
930 gtk_widget_unparent (widget);
932 table->children = g_list_remove (table->children, child);
935 if (was_visible && GTK_WIDGET_VISIBLE (container))
936 gtk_widget_queue_resize (GTK_WIDGET (container));
943 gtk_table_forall (GtkContainer *container,
944 gboolean include_internals,
945 GtkCallback callback,
946 gpointer callback_data)
949 GtkTableChild *child;
952 g_return_if_fail (GTK_IS_TABLE (container));
953 g_return_if_fail (callback != NULL);
955 table = GTK_TABLE (container);
956 children = table->children;
960 child = children->data;
961 children = children->next;
963 (* callback) (child->widget, callback_data);
968 gtk_table_size_request_init (GtkTable *table)
970 GtkTableChild *child;
974 for (row = 0; row < table->nrows; row++)
976 table->rows[row].requisition = 0;
977 table->rows[row].expand = FALSE;
979 for (col = 0; col < table->ncols; col++)
981 table->cols[col].requisition = 0;
982 table->cols[col].expand = FALSE;
985 children = table->children;
988 child = children->data;
989 children = children->next;
991 if (GTK_WIDGET_VISIBLE (child->widget))
992 gtk_widget_size_request (child->widget, NULL);
994 if (child->left_attach == (child->right_attach - 1) && child->xexpand)
995 table->cols[child->left_attach].expand = TRUE;
997 if (child->top_attach == (child->bottom_attach - 1) && child->yexpand)
998 table->rows[child->top_attach].expand = TRUE;
1003 gtk_table_size_request_pass1 (GtkTable *table)
1005 GtkTableChild *child;
1010 children = table->children;
1013 child = children->data;
1014 children = children->next;
1016 if (GTK_WIDGET_VISIBLE (child->widget))
1018 GtkRequisition child_requisition;
1019 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1021 /* Child spans a single column.
1023 if (child->left_attach == (child->right_attach - 1))
1025 width = child_requisition.width + child->xpadding * 2;
1026 table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
1029 /* Child spans a single row.
1031 if (child->top_attach == (child->bottom_attach - 1))
1033 height = child_requisition.height + child->ypadding * 2;
1034 table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
1041 gtk_table_size_request_pass2 (GtkTable *table)
1047 if (table->homogeneous)
1052 for (col = 0; col < table->ncols; col++)
1053 max_width = MAX (max_width, table->cols[col].requisition);
1054 for (row = 0; row < table->nrows; row++)
1055 max_height = MAX (max_height, table->rows[row].requisition);
1057 for (col = 0; col < table->ncols; col++)
1058 table->cols[col].requisition = max_width;
1059 for (row = 0; row < table->nrows; row++)
1060 table->rows[row].requisition = max_height;
1065 gtk_table_size_request_pass3 (GtkTable *table)
1067 GtkTableChild *child;
1073 children = table->children;
1076 child = children->data;
1077 children = children->next;
1079 if (GTK_WIDGET_VISIBLE (child->widget))
1081 /* Child spans multiple columns.
1083 if (child->left_attach != (child->right_attach - 1))
1085 GtkRequisition child_requisition;
1087 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1089 /* Check and see if there is already enough space
1093 for (col = child->left_attach; col < child->right_attach; col++)
1095 width += table->cols[col].requisition;
1096 if ((col + 1) < child->right_attach)
1097 width += table->cols[col].spacing;
1100 /* If we need to request more space for this child to fill
1101 * its requisition, then divide up the needed space amongst the
1102 * columns it spans, favoring expandable columns if any.
1104 if (width < child_requisition.width + child->xpadding * 2)
1107 gboolean force_expand = FALSE;
1109 width = child_requisition.width + child->xpadding * 2 - width;
1111 for (col = child->left_attach; col < child->right_attach; col++)
1112 if (table->cols[col].expand)
1117 n_expand = (child->right_attach - child->left_attach);
1118 force_expand = TRUE;
1121 for (col = child->left_attach; col < child->right_attach; col++)
1122 if (force_expand || table->cols[col].expand)
1124 extra = width / n_expand;
1125 table->cols[col].requisition += extra;
1132 /* Child spans multiple rows.
1134 if (child->top_attach != (child->bottom_attach - 1))
1136 GtkRequisition child_requisition;
1138 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1140 /* Check and see if there is already enough space
1144 for (row = child->top_attach; row < child->bottom_attach; row++)
1146 height += table->rows[row].requisition;
1147 if ((row + 1) < child->bottom_attach)
1148 height += table->rows[row].spacing;
1151 /* If we need to request more space for this child to fill
1152 * its requisition, then divide up the needed space amongst the
1153 * rows it spans, favoring expandable rows if any.
1155 if (height < child_requisition.height + child->ypadding * 2)
1158 gboolean force_expand = FALSE;
1160 height = child_requisition.height + child->ypadding * 2 - height;
1162 for (row = child->top_attach; row < child->bottom_attach; row++)
1164 if (table->rows[row].expand)
1170 n_expand = (child->bottom_attach - child->top_attach);
1171 force_expand = TRUE;
1174 for (row = child->top_attach; row < child->bottom_attach; row++)
1175 if (force_expand || table->rows[row].expand)
1177 extra = height / n_expand;
1178 table->rows[row].requisition += extra;
1189 gtk_table_size_allocate_init (GtkTable *table)
1191 GtkTableChild *child;
1197 /* Initialize the rows and cols.
1198 * By default, rows and cols do not expand and do shrink.
1199 * Those values are modified by the children that occupy
1200 * the rows and cols.
1202 for (col = 0; col < table->ncols; col++)
1204 table->cols[col].allocation = table->cols[col].requisition;
1205 table->cols[col].need_expand = FALSE;
1206 table->cols[col].need_shrink = TRUE;
1207 table->cols[col].expand = FALSE;
1208 table->cols[col].shrink = TRUE;
1209 table->cols[col].empty = TRUE;
1211 for (row = 0; row < table->nrows; row++)
1213 table->rows[row].allocation = table->rows[row].requisition;
1214 table->rows[row].need_expand = FALSE;
1215 table->rows[row].need_shrink = TRUE;
1216 table->rows[row].expand = FALSE;
1217 table->rows[row].shrink = TRUE;
1218 table->rows[row].empty = TRUE;
1221 /* Loop over all the children and adjust the row and col values
1222 * based on whether the children want to be allowed to expand
1223 * or shrink. This loop handles children that occupy a single
1226 children = table->children;
1229 child = children->data;
1230 children = children->next;
1232 if (GTK_WIDGET_VISIBLE (child->widget))
1234 if (child->left_attach == (child->right_attach - 1))
1237 table->cols[child->left_attach].expand = TRUE;
1239 if (!child->xshrink)
1240 table->cols[child->left_attach].shrink = FALSE;
1242 table->cols[child->left_attach].empty = FALSE;
1245 if (child->top_attach == (child->bottom_attach - 1))
1248 table->rows[child->top_attach].expand = TRUE;
1250 if (!child->yshrink)
1251 table->rows[child->top_attach].shrink = FALSE;
1253 table->rows[child->top_attach].empty = FALSE;
1258 /* Loop over all the children again and this time handle children
1259 * which span multiple rows or columns.
1261 children = table->children;
1264 child = children->data;
1265 children = children->next;
1267 if (GTK_WIDGET_VISIBLE (child->widget))
1269 if (child->left_attach != (child->right_attach - 1))
1271 for (col = child->left_attach; col < child->right_attach; col++)
1272 table->cols[col].empty = FALSE;
1277 for (col = child->left_attach; col < child->right_attach; col++)
1278 if (table->cols[col].expand)
1285 for (col = child->left_attach; col < child->right_attach; col++)
1286 table->cols[col].need_expand = TRUE;
1289 if (!child->xshrink)
1292 for (col = child->left_attach; col < child->right_attach; col++)
1293 if (!table->cols[col].shrink)
1300 for (col = child->left_attach; col < child->right_attach; col++)
1301 table->cols[col].need_shrink = FALSE;
1305 if (child->top_attach != (child->bottom_attach - 1))
1307 for (row = child->top_attach; row < child->bottom_attach; row++)
1308 table->rows[row].empty = FALSE;
1313 for (row = child->top_attach; row < child->bottom_attach; row++)
1314 if (table->rows[row].expand)
1321 for (row = child->top_attach; row < child->bottom_attach; row++)
1322 table->rows[row].need_expand = TRUE;
1325 if (!child->yshrink)
1328 for (row = child->top_attach; row < child->bottom_attach; row++)
1329 if (!table->rows[row].shrink)
1336 for (row = child->top_attach; row < child->bottom_attach; row++)
1337 table->rows[row].need_shrink = FALSE;
1343 /* Loop over the columns and set the expand and shrink values
1344 * if the column can be expanded or shrunk.
1346 for (col = 0; col < table->ncols; col++)
1348 if (table->cols[col].empty)
1350 table->cols[col].expand = FALSE;
1351 table->cols[col].shrink = FALSE;
1355 if (table->cols[col].need_expand)
1356 table->cols[col].expand = TRUE;
1357 if (!table->cols[col].need_shrink)
1358 table->cols[col].shrink = FALSE;
1362 /* Loop over the rows and set the expand and shrink values
1363 * if the row can be expanded or shrunk.
1365 for (row = 0; row < table->nrows; row++)
1367 if (table->rows[row].empty)
1369 table->rows[row].expand = FALSE;
1370 table->rows[row].shrink = FALSE;
1374 if (table->rows[row].need_expand)
1375 table->rows[row].expand = TRUE;
1376 if (!table->rows[row].need_shrink)
1377 table->rows[row].shrink = FALSE;
1383 gtk_table_size_allocate_pass1 (GtkTable *table)
1393 /* If we were allocated more space than we requested
1394 * then we have to expand any expandable rows and columns
1395 * to fill in the extra space.
1398 real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1399 real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1401 if (table->homogeneous)
1403 if (!table->children)
1408 for (col = 0; col < table->ncols; col++)
1409 if (table->cols[col].expand)
1418 for (col = 0; col + 1 < table->ncols; col++)
1419 width -= table->cols[col].spacing;
1421 for (col = 0; col < table->ncols; col++)
1423 extra = width / (table->ncols - col);
1424 table->cols[col].allocation = MAX (1, extra);
1435 for (col = 0; col < table->ncols; col++)
1437 width += table->cols[col].requisition;
1438 if (table->cols[col].expand)
1440 if (table->cols[col].shrink)
1443 for (col = 0; col + 1 < table->ncols; col++)
1444 width += table->cols[col].spacing;
1446 /* Check to see if we were allocated more width than we requested.
1448 if ((width < real_width) && (nexpand >= 1))
1450 width = real_width - width;
1452 for (col = 0; col < table->ncols; col++)
1453 if (table->cols[col].expand)
1455 extra = width / nexpand;
1456 table->cols[col].allocation += extra;
1463 /* Check to see if we were allocated less width than we requested,
1464 * then shrink until we fit the size give.
1466 if (width > real_width)
1468 gint total_nshrink = nshrink;
1470 extra = width - real_width;
1471 while (total_nshrink > 0 && extra > 0)
1473 nshrink = total_nshrink;
1474 for (col = 0; col < table->ncols; col++)
1475 if (table->cols[col].shrink)
1477 gint allocation = table->cols[col].allocation;
1479 table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1480 extra -= allocation - table->cols[col].allocation;
1482 if (table->cols[col].allocation < 2)
1485 table->cols[col].shrink = FALSE;
1492 if (table->homogeneous)
1494 if (!table->children)
1499 for (row = 0; row < table->nrows; row++)
1500 if (table->rows[row].expand)
1508 height = real_height;
1510 for (row = 0; row + 1 < table->nrows; row++)
1511 height -= table->rows[row].spacing;
1514 for (row = 0; row < table->nrows; row++)
1516 extra = height / (table->nrows - row);
1517 table->rows[row].allocation = MAX (1, extra);
1528 for (row = 0; row < table->nrows; row++)
1530 height += table->rows[row].requisition;
1531 if (table->rows[row].expand)
1533 if (table->rows[row].shrink)
1536 for (row = 0; row + 1 < table->nrows; row++)
1537 height += table->rows[row].spacing;
1539 /* Check to see if we were allocated more height than we requested.
1541 if ((height < real_height) && (nexpand >= 1))
1543 height = real_height - height;
1545 for (row = 0; row < table->nrows; row++)
1546 if (table->rows[row].expand)
1548 extra = height / nexpand;
1549 table->rows[row].allocation += extra;
1556 /* Check to see if we were allocated less height than we requested.
1557 * then shrink until we fit the size give.
1559 if (height > real_height)
1561 gint total_nshrink = nshrink;
1563 extra = height - real_height;
1564 while (total_nshrink > 0 && extra > 0)
1566 nshrink = total_nshrink;
1567 for (row = 0; row < table->nrows; row++)
1568 if (table->rows[row].shrink)
1570 gint allocation = table->rows[row].allocation;
1572 table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1573 extra -= allocation - table->rows[row].allocation;
1575 if (table->rows[row].allocation < 2)
1578 table->rows[row].shrink = FALSE;
1587 gtk_table_size_allocate_pass2 (GtkTable *table)
1589 GtkTableChild *child;
1595 GtkAllocation allocation;
1596 GtkWidget *widget = GTK_WIDGET (table);
1598 children = table->children;
1601 child = children->data;
1602 children = children->next;
1604 if (GTK_WIDGET_VISIBLE (child->widget))
1606 GtkRequisition child_requisition;
1607 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1609 x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1610 y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1614 for (col = 0; col < child->left_attach; col++)
1616 x += table->cols[col].allocation;
1617 x += table->cols[col].spacing;
1620 for (col = child->left_attach; col < child->right_attach; col++)
1622 max_width += table->cols[col].allocation;
1623 if ((col + 1) < child->right_attach)
1624 max_width += table->cols[col].spacing;
1627 for (row = 0; row < child->top_attach; row++)
1629 y += table->rows[row].allocation;
1630 y += table->rows[row].spacing;
1633 for (row = child->top_attach; row < child->bottom_attach; row++)
1635 max_height += table->rows[row].allocation;
1636 if ((row + 1) < child->bottom_attach)
1637 max_height += table->rows[row].spacing;
1642 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1643 allocation.x = x + (max_width - allocation.width) / 2;
1647 allocation.width = child_requisition.width;
1648 allocation.x = x + (max_width - allocation.width) / 2;
1653 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1654 allocation.y = y + (max_height - allocation.height) / 2;
1658 allocation.height = child_requisition.height;
1659 allocation.y = y + (max_height - allocation.height) / 2;
1662 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1663 allocation.x = widget->allocation.x + widget->allocation.width
1664 - (allocation.x - widget->allocation.x) - allocation.width;
1666 gtk_widget_size_allocate (child->widget, &allocation);
1671 #define __GTK_TABLE_C__
1672 #include "gtkaliasdef.c"