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/.
43 CHILD_PROP_LEFT_ATTACH,
44 CHILD_PROP_RIGHT_ATTACH,
45 CHILD_PROP_TOP_ATTACH,
46 CHILD_PROP_BOTTOM_ATTACH,
54 static void gtk_table_class_init (GtkTableClass *klass);
55 static void gtk_table_init (GtkTable *table);
56 static void gtk_table_finalize (GObject *object);
57 static void gtk_table_size_request (GtkWidget *widget,
58 GtkRequisition *requisition);
59 static void gtk_table_size_allocate (GtkWidget *widget,
60 GtkAllocation *allocation);
61 static void gtk_table_add (GtkContainer *container,
63 static void gtk_table_remove (GtkContainer *container,
65 static void gtk_table_forall (GtkContainer *container,
66 gboolean include_internals,
68 gpointer callback_data);
69 static void gtk_table_get_property (GObject *object,
73 static void gtk_table_set_property (GObject *object,
77 static void gtk_table_set_child_property (GtkContainer *container,
82 static void gtk_table_get_child_property (GtkContainer *container,
87 static GtkType gtk_table_child_type (GtkContainer *container);
90 static void gtk_table_size_request_init (GtkTable *table);
91 static void gtk_table_size_request_pass1 (GtkTable *table);
92 static void gtk_table_size_request_pass2 (GtkTable *table);
93 static void gtk_table_size_request_pass3 (GtkTable *table);
95 static void gtk_table_size_allocate_init (GtkTable *table);
96 static void gtk_table_size_allocate_pass1 (GtkTable *table);
97 static void gtk_table_size_allocate_pass2 (GtkTable *table);
100 static GtkContainerClass *parent_class = NULL;
104 gtk_table_get_type (void)
106 static GtkType table_type = 0;
110 static const GtkTypeInfo table_info =
114 sizeof (GtkTableClass),
115 (GtkClassInitFunc) gtk_table_class_init,
116 (GtkObjectInitFunc) gtk_table_init,
117 /* reserved_1 */ NULL,
118 /* reserved_2 */ NULL,
119 (GtkClassInitFunc) NULL,
122 table_type = gtk_type_unique (gtk_container_get_type (), &table_info);
129 gtk_table_class_init (GtkTableClass *class)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
132 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
133 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
135 parent_class = g_type_class_peek_parent (class);
137 gobject_class->finalize = gtk_table_finalize;
139 gobject_class->get_property = gtk_table_get_property;
140 gobject_class->set_property = gtk_table_set_property;
142 widget_class->size_request = gtk_table_size_request;
143 widget_class->size_allocate = gtk_table_size_allocate;
145 container_class->add = gtk_table_add;
146 container_class->remove = gtk_table_remove;
147 container_class->forall = gtk_table_forall;
148 container_class->child_type = gtk_table_child_type;
149 container_class->set_child_property = gtk_table_set_child_property;
150 container_class->get_child_property = gtk_table_get_child_property;
153 g_object_class_install_property (gobject_class,
155 g_param_spec_uint ("n_rows",
157 _("The number of rows in the table"),
162 g_object_class_install_property (gobject_class,
164 g_param_spec_uint ("n_columns",
166 _("The number of columns in the table"),
171 g_object_class_install_property (gobject_class,
173 g_param_spec_uint ("row_spacing",
175 _("The amount of space between two consecutive rows"),
180 g_object_class_install_property (gobject_class,
182 g_param_spec_uint ("column_spacing",
184 _("The amount of space between two consecutive columns"),
189 g_object_class_install_property (gobject_class,
191 g_param_spec_boolean ("homogeneous",
193 _("If TRUE this means the table cells are all the same width/height"),
197 gtk_container_class_install_child_property (container_class,
198 CHILD_PROP_LEFT_ATTACH,
199 g_param_spec_uint ("left_attach", NULL, NULL,
202 gtk_container_class_install_child_property (container_class,
203 CHILD_PROP_RIGHT_ATTACH,
204 g_param_spec_uint ("right_attach", NULL, NULL,
207 gtk_container_class_install_child_property (container_class,
208 CHILD_PROP_TOP_ATTACH,
209 g_param_spec_uint ("top_attach", NULL, NULL,
212 gtk_container_class_install_child_property (container_class,
213 CHILD_PROP_BOTTOM_ATTACH,
214 g_param_spec_uint ("bottom_attach", NULL, NULL,
217 gtk_container_class_install_child_property (container_class,
218 CHILD_PROP_X_OPTIONS,
219 g_param_spec_flags ("x_options", NULL, NULL,
220 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
222 gtk_container_class_install_child_property (container_class,
223 CHILD_PROP_Y_OPTIONS,
224 g_param_spec_flags ("y_options", NULL, NULL,
225 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
227 gtk_container_class_install_child_property (container_class,
228 CHILD_PROP_X_PADDING,
229 g_param_spec_uint ("x_padding", NULL, NULL,
232 gtk_container_class_install_child_property (container_class,
233 CHILD_PROP_Y_PADDING,
234 g_param_spec_uint ("y_padding", NULL, NULL,
240 gtk_table_child_type (GtkContainer *container)
242 return GTK_TYPE_WIDGET;
246 gtk_table_get_property (GObject *object,
253 table = GTK_TABLE (object);
258 g_value_set_uint (value, table->nrows);
261 g_value_set_uint (value, table->ncols);
263 case PROP_ROW_SPACING:
264 g_value_set_uint (value, table->row_spacing);
266 case PROP_COLUMN_SPACING:
267 g_value_set_uint (value, table->column_spacing);
269 case PROP_HOMOGENEOUS:
270 g_value_set_boolean (value, table->homogeneous);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gtk_table_set_property (GObject *object,
286 table = GTK_TABLE (object);
291 gtk_table_resize (table, g_value_get_uint (value), table->ncols);
294 gtk_table_resize (table, table->nrows, g_value_get_uint (value));
296 case PROP_ROW_SPACING:
297 gtk_table_set_row_spacings (table, g_value_get_uint (value));
299 case PROP_COLUMN_SPACING:
300 gtk_table_set_col_spacings (table, g_value_get_uint (value));
302 case PROP_HOMOGENEOUS:
303 gtk_table_set_homogeneous (table, g_value_get_boolean (value));
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 gtk_table_set_child_property (GtkContainer *container,
318 GtkTable *table = GTK_TABLE (container);
319 GtkTableChild *table_child;
323 for (list = table->children; list; list = list->next)
325 table_child = list->data;
327 if (table_child->widget == child)
332 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
338 case CHILD_PROP_LEFT_ATTACH:
339 table_child->left_attach = g_value_get_uint (value);
340 if (table_child->right_attach <= table_child->left_attach)
341 table_child->right_attach = table_child->left_attach + 1;
342 if (table_child->right_attach >= table->ncols)
343 gtk_table_resize (table, table->nrows, table_child->right_attach);
345 case CHILD_PROP_RIGHT_ATTACH:
346 table_child->right_attach = g_value_get_uint (value);
347 if (table_child->right_attach <= table_child->left_attach)
348 table_child->left_attach = table_child->right_attach - 1;
349 if (table_child->right_attach >= table->ncols)
350 gtk_table_resize (table, table->nrows, table_child->right_attach);
352 case CHILD_PROP_TOP_ATTACH:
353 table_child->top_attach = g_value_get_uint (value);
354 if (table_child->bottom_attach <= table_child->top_attach)
355 table_child->bottom_attach = table_child->top_attach + 1;
356 if (table_child->bottom_attach >= table->nrows)
357 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
359 case CHILD_PROP_BOTTOM_ATTACH:
360 table_child->bottom_attach = g_value_get_uint (value);
361 if (table_child->bottom_attach <= table_child->top_attach)
362 table_child->top_attach = table_child->bottom_attach - 1;
363 if (table_child->bottom_attach >= table->nrows)
364 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
366 case CHILD_PROP_X_OPTIONS:
367 table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
368 table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
369 table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
371 case CHILD_PROP_Y_OPTIONS:
372 table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
373 table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
374 table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
376 case CHILD_PROP_X_PADDING:
377 table_child->xpadding = g_value_get_uint (value);
379 case CHILD_PROP_Y_PADDING:
380 table_child->ypadding = g_value_get_uint (value);
383 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
386 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
387 gtk_widget_queue_resize (child);
391 gtk_table_get_child_property (GtkContainer *container,
397 GtkTable *table = GTK_TABLE (container);
398 GtkTableChild *table_child;
402 for (list = table->children; list; list = list->next)
404 table_child = list->data;
406 if (table_child->widget == child)
411 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
417 case CHILD_PROP_LEFT_ATTACH:
418 g_value_set_uint (value, table_child->left_attach);
420 case CHILD_PROP_RIGHT_ATTACH:
421 g_value_set_uint (value, table_child->right_attach);
423 case CHILD_PROP_TOP_ATTACH:
424 g_value_set_uint (value, table_child->top_attach);
426 case CHILD_PROP_BOTTOM_ATTACH:
427 g_value_set_uint (value, table_child->bottom_attach);
429 case CHILD_PROP_X_OPTIONS:
430 g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
431 table_child->xshrink * GTK_SHRINK |
432 table_child->xfill * GTK_FILL));
434 case CHILD_PROP_Y_OPTIONS:
435 g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
436 table_child->yshrink * GTK_SHRINK |
437 table_child->yfill * GTK_FILL));
439 case CHILD_PROP_X_PADDING:
440 g_value_set_uint (value, table_child->xpadding);
442 case CHILD_PROP_Y_PADDING:
443 g_value_set_uint (value, table_child->ypadding);
446 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
452 gtk_table_init (GtkTable *table)
454 GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
455 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
457 table->children = NULL;
462 table->column_spacing = 0;
463 table->row_spacing = 0;
464 table->homogeneous = FALSE;
466 gtk_table_resize (table, 1, 1);
470 gtk_table_new (guint rows,
472 gboolean homogeneous)
481 table = gtk_type_new (gtk_table_get_type ());
483 table->homogeneous = (homogeneous ? TRUE : FALSE);
485 gtk_table_resize (table, rows, columns);
487 return GTK_WIDGET (table);
491 gtk_table_resize (GtkTable *table,
495 g_return_if_fail (GTK_IS_TABLE (table));
496 g_return_if_fail (n_rows > 0 && n_rows < 65536);
497 g_return_if_fail (n_rows > 0 && n_cols < 65536);
499 n_rows = MAX (n_rows, 1);
500 n_cols = MAX (n_cols, 1);
502 if (n_rows != table->nrows ||
503 n_cols != table->ncols)
507 for (list = table->children; list; list = list->next)
509 GtkTableChild *child;
513 n_rows = MAX (n_rows, child->bottom_attach);
514 n_cols = MAX (n_cols, child->right_attach);
517 if (n_rows != table->nrows)
522 table->nrows = n_rows;
523 table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
525 for (; i < table->nrows; i++)
527 table->rows[i].requisition = 0;
528 table->rows[i].allocation = 0;
529 table->rows[i].spacing = table->row_spacing;
530 table->rows[i].need_expand = 0;
531 table->rows[i].need_shrink = 0;
532 table->rows[i].expand = 0;
533 table->rows[i].shrink = 0;
536 g_object_notify (G_OBJECT (table), "n_rows");
539 if (n_cols != table->ncols)
544 table->ncols = n_cols;
545 table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
547 for (; i < table->ncols; i++)
549 table->cols[i].requisition = 0;
550 table->cols[i].allocation = 0;
551 table->cols[i].spacing = table->column_spacing;
552 table->cols[i].need_expand = 0;
553 table->cols[i].need_shrink = 0;
554 table->cols[i].expand = 0;
555 table->cols[i].shrink = 0;
558 g_object_notify (G_OBJECT (table), "n_columns");
564 gtk_table_attach (GtkTable *table,
570 GtkAttachOptions xoptions,
571 GtkAttachOptions yoptions,
575 GtkTableChild *table_child;
577 g_return_if_fail (GTK_IS_TABLE (table));
578 g_return_if_fail (GTK_IS_WIDGET (child));
579 g_return_if_fail (child->parent == NULL);
581 /* g_return_if_fail (left_attach >= 0); */
582 g_return_if_fail (left_attach < right_attach);
583 /* g_return_if_fail (top_attach >= 0); */
584 g_return_if_fail (top_attach < bottom_attach);
586 if (right_attach >= table->ncols)
587 gtk_table_resize (table, table->nrows, right_attach);
589 if (bottom_attach >= table->nrows)
590 gtk_table_resize (table, bottom_attach, table->ncols);
592 table_child = g_new (GtkTableChild, 1);
593 table_child->widget = child;
594 table_child->left_attach = left_attach;
595 table_child->right_attach = right_attach;
596 table_child->top_attach = top_attach;
597 table_child->bottom_attach = bottom_attach;
598 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
599 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
600 table_child->xfill = (xoptions & GTK_FILL) != 0;
601 table_child->xpadding = xpadding;
602 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
603 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
604 table_child->yfill = (yoptions & GTK_FILL) != 0;
605 table_child->ypadding = ypadding;
607 table->children = g_list_prepend (table->children, table_child);
609 gtk_widget_set_parent (child, GTK_WIDGET (table));
613 gtk_table_attach_defaults (GtkTable *table,
620 gtk_table_attach (table, widget,
621 left_attach, right_attach,
622 top_attach, bottom_attach,
623 GTK_EXPAND | GTK_FILL,
624 GTK_EXPAND | GTK_FILL,
629 gtk_table_set_row_spacing (GtkTable *table,
633 g_return_if_fail (GTK_IS_TABLE (table));
634 g_return_if_fail (row < table->nrows);
636 if (table->rows[row].spacing != spacing)
638 table->rows[row].spacing = spacing;
640 if (GTK_WIDGET_VISIBLE (table))
641 gtk_widget_queue_resize (GTK_WIDGET (table));
646 * gtk_table_get_row_spacing:
647 * @table: a #GtkTable
648 * @row: a row in the table, 0 indicates the first row
650 * Gets the amount of space between row @row, and
651 * row @row + 1. See gtk_table_set_row_spacing().
653 * Return value: the row spacing
656 gtk_table_get_row_spacing (GtkTable *table,
659 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
660 g_return_val_if_fail (row < table->nrows - 1, 0);
662 return table->rows[row].spacing;
666 gtk_table_set_col_spacing (GtkTable *table,
670 g_return_if_fail (GTK_IS_TABLE (table));
671 g_return_if_fail (column < table->ncols);
673 if (table->cols[column].spacing != spacing)
675 table->cols[column].spacing = spacing;
677 if (GTK_WIDGET_VISIBLE (table))
678 gtk_widget_queue_resize (GTK_WIDGET (table));
683 * gtk_table_get_col_spacing:
684 * @table: a #GtkTable
685 * @column: a column in the table, 0 indicates the first column
687 * Gets the amount of space between column @col, and
688 * column @col + 1. See gtk_table_set_col_spacing().
690 * Return value: the column spacing
693 gtk_table_get_col_spacing (GtkTable *table,
696 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
697 g_return_val_if_fail (column < table->ncols, 0);
699 return table->cols[column].spacing;
703 gtk_table_set_row_spacings (GtkTable *table,
708 g_return_if_fail (GTK_IS_TABLE (table));
710 table->row_spacing = spacing;
711 for (row = 0; row < table->nrows; row++)
712 table->rows[row].spacing = spacing;
714 if (GTK_WIDGET_VISIBLE (table))
715 gtk_widget_queue_resize (GTK_WIDGET (table));
717 g_object_notify (G_OBJECT (table), "row_spacing");
721 * gtk_table_get_default_row_spacing:
722 * @table: a #GtkTable
724 * Gets the default row spacing for the table. This is
725 * the spacing that will be used for newly added rows.
726 * (See gtk_table_set_row_spacings())
728 * Returns value: the default row spacing
731 gtk_table_get_default_row_spacing (GtkTable *table)
733 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
735 return table->row_spacing;
739 gtk_table_set_col_spacings (GtkTable *table,
744 g_return_if_fail (GTK_IS_TABLE (table));
746 table->column_spacing = spacing;
747 for (col = 0; col < table->ncols; col++)
748 table->cols[col].spacing = spacing;
750 if (GTK_WIDGET_VISIBLE (table))
751 gtk_widget_queue_resize (GTK_WIDGET (table));
753 g_object_notify (G_OBJECT (table), "column_spacing");
757 * gtk_table_get_default_col_spacing:
758 * @table: a #GtkTable
760 * Gets the default column spacing for the table. This is
761 * the spacing that will be used for newly added columns.
762 * (See gtk_table_set_col_spacings())
764 * Returns value: the default column spacing
767 gtk_table_get_default_col_spacing (GtkTable *table)
769 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
771 return table->column_spacing;
775 gtk_table_set_homogeneous (GtkTable *table,
776 gboolean homogeneous)
778 g_return_if_fail (GTK_IS_TABLE (table));
780 homogeneous = (homogeneous != 0);
781 if (homogeneous != table->homogeneous)
783 table->homogeneous = homogeneous;
785 if (GTK_WIDGET_VISIBLE (table))
786 gtk_widget_queue_resize (GTK_WIDGET (table));
788 g_object_notify (G_OBJECT (table), "homogeneous");
793 * gtk_table_get_homogeneous:
794 * @table: a #GtkTable
796 * Returns whether the table cells are all constrained to the same
797 * width and height. (See gtk_table_set_homogenous ())
799 * Return value: %TRUE if the cells are all constrained to the same size
802 gtk_table_get_homogeneous (GtkTable *table)
804 g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
806 return table->homogeneous;
810 gtk_table_finalize (GObject *object)
814 g_return_if_fail (GTK_IS_TABLE (object));
816 table = GTK_TABLE (object);
818 g_free (table->rows);
819 g_free (table->cols);
821 G_OBJECT_CLASS (parent_class)->finalize (object);
825 gtk_table_size_request (GtkWidget *widget,
826 GtkRequisition *requisition)
831 g_return_if_fail (GTK_IS_TABLE (widget));
832 g_return_if_fail (requisition != NULL);
834 table = GTK_TABLE (widget);
836 requisition->width = 0;
837 requisition->height = 0;
839 gtk_table_size_request_init (table);
840 gtk_table_size_request_pass1 (table);
841 gtk_table_size_request_pass2 (table);
842 gtk_table_size_request_pass3 (table);
843 gtk_table_size_request_pass2 (table);
845 for (col = 0; col < table->ncols; col++)
846 requisition->width += table->cols[col].requisition;
847 for (col = 0; col + 1 < table->ncols; col++)
848 requisition->width += table->cols[col].spacing;
850 for (row = 0; row < table->nrows; row++)
851 requisition->height += table->rows[row].requisition;
852 for (row = 0; row + 1 < table->nrows; row++)
853 requisition->height += table->rows[row].spacing;
855 requisition->width += GTK_CONTAINER (table)->border_width * 2;
856 requisition->height += GTK_CONTAINER (table)->border_width * 2;
860 gtk_table_size_allocate (GtkWidget *widget,
861 GtkAllocation *allocation)
865 g_return_if_fail (GTK_IS_TABLE (widget));
866 g_return_if_fail (allocation != NULL);
868 widget->allocation = *allocation;
869 table = GTK_TABLE (widget);
871 gtk_table_size_allocate_init (table);
872 gtk_table_size_allocate_pass1 (table);
873 gtk_table_size_allocate_pass2 (table);
877 gtk_table_add (GtkContainer *container,
880 g_return_if_fail (GTK_IS_TABLE (container));
881 g_return_if_fail (widget != NULL);
883 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
887 gtk_table_remove (GtkContainer *container,
891 GtkTableChild *child;
894 g_return_if_fail (GTK_IS_TABLE (container));
895 g_return_if_fail (widget != NULL);
897 table = GTK_TABLE (container);
898 children = table->children;
902 child = children->data;
903 children = children->next;
905 if (child->widget == widget)
907 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
909 gtk_widget_unparent (widget);
911 table->children = g_list_remove (table->children, child);
914 if (was_visible && GTK_WIDGET_VISIBLE (container))
915 gtk_widget_queue_resize (GTK_WIDGET (container));
922 gtk_table_forall (GtkContainer *container,
923 gboolean include_internals,
924 GtkCallback callback,
925 gpointer callback_data)
928 GtkTableChild *child;
931 g_return_if_fail (GTK_IS_TABLE (container));
932 g_return_if_fail (callback != NULL);
934 table = GTK_TABLE (container);
935 children = table->children;
939 child = children->data;
940 children = children->next;
942 (* callback) (child->widget, callback_data);
947 gtk_table_size_request_init (GtkTable *table)
949 GtkTableChild *child;
953 for (row = 0; row < table->nrows; row++)
955 table->rows[row].requisition = 0;
956 table->rows[row].expand = FALSE;
958 for (col = 0; col < table->ncols; col++)
960 table->cols[col].requisition = 0;
961 table->cols[col].expand = FALSE;
964 children = table->children;
967 child = children->data;
968 children = children->next;
970 if (GTK_WIDGET_VISIBLE (child->widget))
971 gtk_widget_size_request (child->widget, NULL);
973 if (child->left_attach == (child->right_attach - 1) && child->xexpand)
974 table->cols[child->left_attach].expand = TRUE;
976 if (child->top_attach == (child->bottom_attach - 1) && child->yexpand)
977 table->rows[child->top_attach].expand = TRUE;
982 gtk_table_size_request_pass1 (GtkTable *table)
984 GtkTableChild *child;
989 children = table->children;
992 child = children->data;
993 children = children->next;
995 if (GTK_WIDGET_VISIBLE (child->widget))
997 GtkRequisition child_requisition;
998 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1000 /* Child spans a single column.
1002 if (child->left_attach == (child->right_attach - 1))
1004 width = child_requisition.width + child->xpadding * 2;
1005 table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
1008 /* Child spans a single row.
1010 if (child->top_attach == (child->bottom_attach - 1))
1012 height = child_requisition.height + child->ypadding * 2;
1013 table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
1020 gtk_table_size_request_pass2 (GtkTable *table)
1026 if (table->homogeneous)
1031 for (col = 0; col < table->ncols; col++)
1032 max_width = MAX (max_width, table->cols[col].requisition);
1033 for (row = 0; row < table->nrows; row++)
1034 max_height = MAX (max_height, table->rows[row].requisition);
1036 for (col = 0; col < table->ncols; col++)
1037 table->cols[col].requisition = max_width;
1038 for (row = 0; row < table->nrows; row++)
1039 table->rows[row].requisition = max_height;
1044 gtk_table_size_request_pass3 (GtkTable *table)
1046 GtkTableChild *child;
1052 children = table->children;
1055 child = children->data;
1056 children = children->next;
1058 if (GTK_WIDGET_VISIBLE (child->widget))
1060 /* Child spans multiple columns.
1062 if (child->left_attach != (child->right_attach - 1))
1064 GtkRequisition child_requisition;
1066 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1068 /* Check and see if there is already enough space
1072 for (col = child->left_attach; col < child->right_attach; col++)
1074 width += table->cols[col].requisition;
1075 if ((col + 1) < child->right_attach)
1076 width += table->cols[col].spacing;
1079 /* If we need to request more space for this child to fill
1080 * its requisition, then divide up the needed space amongst the
1081 * columns it spans, favoring expandable columns if any.
1083 if (width < child_requisition.width + child->xpadding * 2)
1086 gboolean force_expand = FALSE;
1088 width = child_requisition.width + child->xpadding * 2 - width;
1090 for (col = child->left_attach; col < child->right_attach; col++)
1091 if (table->cols[col].expand)
1096 n_expand = (child->right_attach - child->left_attach);
1097 force_expand = TRUE;
1100 for (col = child->left_attach; col < child->right_attach; col++)
1101 if (force_expand || table->cols[col].expand)
1103 extra = width / n_expand;
1104 table->cols[col].requisition += extra;
1111 /* Child spans multiple rows.
1113 if (child->top_attach != (child->bottom_attach - 1))
1115 GtkRequisition child_requisition;
1117 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1119 /* Check and see if there is already enough space
1123 for (row = child->top_attach; row < child->bottom_attach; row++)
1125 height += table->rows[row].requisition;
1126 if ((row + 1) < child->bottom_attach)
1127 height += table->rows[row].spacing;
1130 /* If we need to request more space for this child to fill
1131 * its requisition, then divide up the needed space amongst the
1132 * rows it spans, favoring expandable rows if any.
1134 if (height < child_requisition.height + child->ypadding * 2)
1137 gboolean force_expand = FALSE;
1139 height = child_requisition.height + child->ypadding * 2 - height;
1141 for (row = child->top_attach; row < child->bottom_attach; row++)
1143 if (table->rows[row].expand)
1149 n_expand = (child->bottom_attach - child->top_attach);
1150 force_expand = TRUE;
1153 for (row = child->top_attach; row < child->bottom_attach; row++)
1154 if (force_expand || table->rows[row].expand)
1156 extra = height / n_expand;
1157 table->rows[row].requisition += extra;
1168 gtk_table_size_allocate_init (GtkTable *table)
1170 GtkTableChild *child;
1176 /* Initialize the rows and cols.
1177 * By default, rows and cols do not expand and do shrink.
1178 * Those values are modified by the children that occupy
1179 * the rows and cols.
1181 for (col = 0; col < table->ncols; col++)
1183 table->cols[col].allocation = table->cols[col].requisition;
1184 table->cols[col].need_expand = FALSE;
1185 table->cols[col].need_shrink = TRUE;
1186 table->cols[col].expand = FALSE;
1187 table->cols[col].shrink = TRUE;
1188 table->cols[col].empty = TRUE;
1190 for (row = 0; row < table->nrows; row++)
1192 table->rows[row].allocation = table->rows[row].requisition;
1193 table->rows[row].need_expand = FALSE;
1194 table->rows[row].need_shrink = TRUE;
1195 table->rows[row].expand = FALSE;
1196 table->rows[row].shrink = TRUE;
1197 table->rows[row].empty = TRUE;
1200 /* Loop over all the children and adjust the row and col values
1201 * based on whether the children want to be allowed to expand
1202 * or shrink. This loop handles children that occupy a single
1205 children = table->children;
1208 child = children->data;
1209 children = children->next;
1211 if (GTK_WIDGET_VISIBLE (child->widget))
1213 if (child->left_attach == (child->right_attach - 1))
1216 table->cols[child->left_attach].expand = TRUE;
1218 if (!child->xshrink)
1219 table->cols[child->left_attach].shrink = FALSE;
1221 table->cols[child->left_attach].empty = FALSE;
1224 if (child->top_attach == (child->bottom_attach - 1))
1227 table->rows[child->top_attach].expand = TRUE;
1229 if (!child->yshrink)
1230 table->rows[child->top_attach].shrink = FALSE;
1232 table->rows[child->top_attach].empty = FALSE;
1237 /* Loop over all the children again and this time handle children
1238 * which span multiple rows or columns.
1240 children = table->children;
1243 child = children->data;
1244 children = children->next;
1246 if (GTK_WIDGET_VISIBLE (child->widget))
1248 if (child->left_attach != (child->right_attach - 1))
1250 for (col = child->left_attach; col < child->right_attach; col++)
1251 table->cols[col].empty = FALSE;
1256 for (col = child->left_attach; col < child->right_attach; col++)
1257 if (table->cols[col].expand)
1264 for (col = child->left_attach; col < child->right_attach; col++)
1265 table->cols[col].need_expand = TRUE;
1268 if (!child->xshrink)
1271 for (col = child->left_attach; col < child->right_attach; col++)
1272 if (!table->cols[col].shrink)
1279 for (col = child->left_attach; col < child->right_attach; col++)
1280 table->cols[col].need_shrink = FALSE;
1284 if (child->top_attach != (child->bottom_attach - 1))
1286 for (row = child->top_attach; row < child->bottom_attach; row++)
1287 table->rows[row].empty = FALSE;
1292 for (row = child->top_attach; row < child->bottom_attach; row++)
1293 if (table->rows[row].expand)
1300 for (row = child->top_attach; row < child->bottom_attach; row++)
1301 table->rows[row].need_expand = TRUE;
1304 if (!child->yshrink)
1307 for (row = child->top_attach; row < child->bottom_attach; row++)
1308 if (!table->rows[row].shrink)
1315 for (row = child->top_attach; row < child->bottom_attach; row++)
1316 table->rows[row].need_shrink = FALSE;
1322 /* Loop over the columns and set the expand and shrink values
1323 * if the column can be expanded or shrunk.
1325 for (col = 0; col < table->ncols; col++)
1327 if (table->cols[col].empty)
1329 table->cols[col].expand = FALSE;
1330 table->cols[col].shrink = FALSE;
1334 if (table->cols[col].need_expand)
1335 table->cols[col].expand = TRUE;
1336 if (!table->cols[col].need_shrink)
1337 table->cols[col].shrink = FALSE;
1341 /* Loop over the rows and set the expand and shrink values
1342 * if the row can be expanded or shrunk.
1344 for (row = 0; row < table->nrows; row++)
1346 if (table->rows[row].empty)
1348 table->rows[row].expand = FALSE;
1349 table->rows[row].shrink = FALSE;
1353 if (table->rows[row].need_expand)
1354 table->rows[row].expand = TRUE;
1355 if (!table->rows[row].need_shrink)
1356 table->rows[row].shrink = FALSE;
1362 gtk_table_size_allocate_pass1 (GtkTable *table)
1372 /* If we were allocated more space than we requested
1373 * then we have to expand any expandable rows and columns
1374 * to fill in the extra space.
1377 real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1378 real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1380 if (table->homogeneous)
1382 if (!table->children)
1387 for (col = 0; col < table->ncols; col++)
1388 if (table->cols[col].expand)
1397 for (col = 0; col + 1 < table->ncols; col++)
1398 width -= table->cols[col].spacing;
1400 for (col = 0; col < table->ncols; col++)
1402 extra = width / (table->ncols - col);
1403 table->cols[col].allocation = MAX (1, extra);
1414 for (col = 0; col < table->ncols; col++)
1416 width += table->cols[col].requisition;
1417 if (table->cols[col].expand)
1419 if (table->cols[col].shrink)
1422 for (col = 0; col + 1 < table->ncols; col++)
1423 width += table->cols[col].spacing;
1425 /* Check to see if we were allocated more width than we requested.
1427 if ((width < real_width) && (nexpand >= 1))
1429 width = real_width - width;
1431 for (col = 0; col < table->ncols; col++)
1432 if (table->cols[col].expand)
1434 extra = width / nexpand;
1435 table->cols[col].allocation += extra;
1442 /* Check to see if we were allocated less width than we requested,
1443 * then shrink until we fit the size give.
1445 if (width > real_width)
1447 gint total_nshrink = nshrink;
1449 extra = width - real_width;
1450 while (total_nshrink > 0 && extra > 0)
1452 nshrink = total_nshrink;
1453 for (col = 0; col < table->ncols; col++)
1454 if (table->cols[col].shrink)
1456 gint allocation = table->cols[col].allocation;
1458 table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1459 extra -= allocation - table->cols[col].allocation;
1461 if (table->cols[col].allocation < 2)
1464 table->cols[col].shrink = FALSE;
1471 if (table->homogeneous)
1473 if (!table->children)
1478 for (row = 0; row < table->nrows; row++)
1479 if (table->rows[row].expand)
1487 height = real_height;
1489 for (row = 0; row + 1 < table->nrows; row++)
1490 height -= table->rows[row].spacing;
1493 for (row = 0; row < table->nrows; row++)
1495 extra = height / (table->nrows - row);
1496 table->rows[row].allocation = MAX (1, extra);
1507 for (row = 0; row < table->nrows; row++)
1509 height += table->rows[row].requisition;
1510 if (table->rows[row].expand)
1512 if (table->rows[row].shrink)
1515 for (row = 0; row + 1 < table->nrows; row++)
1516 height += table->rows[row].spacing;
1518 /* Check to see if we were allocated more height than we requested.
1520 if ((height < real_height) && (nexpand >= 1))
1522 height = real_height - height;
1524 for (row = 0; row < table->nrows; row++)
1525 if (table->rows[row].expand)
1527 extra = height / nexpand;
1528 table->rows[row].allocation += extra;
1535 /* Check to see if we were allocated less height than we requested.
1536 * then shrink until we fit the size give.
1538 if (height > real_height)
1540 gint total_nshrink = nshrink;
1542 extra = height - real_height;
1543 while (total_nshrink > 0 && extra > 0)
1545 nshrink = total_nshrink;
1546 for (row = 0; row < table->nrows; row++)
1547 if (table->rows[row].shrink)
1549 gint allocation = table->rows[row].allocation;
1551 table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1552 extra -= allocation - table->rows[row].allocation;
1554 if (table->rows[row].allocation < 2)
1557 table->rows[row].shrink = FALSE;
1566 gtk_table_size_allocate_pass2 (GtkTable *table)
1568 GtkTableChild *child;
1574 GtkAllocation allocation;
1575 GtkWidget *widget = GTK_WIDGET (table);
1577 children = table->children;
1580 child = children->data;
1581 children = children->next;
1583 if (GTK_WIDGET_VISIBLE (child->widget))
1585 GtkRequisition child_requisition;
1586 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1588 x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1589 y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1593 for (col = 0; col < child->left_attach; col++)
1595 x += table->cols[col].allocation;
1596 x += table->cols[col].spacing;
1599 for (col = child->left_attach; col < child->right_attach; col++)
1601 max_width += table->cols[col].allocation;
1602 if ((col + 1) < child->right_attach)
1603 max_width += table->cols[col].spacing;
1606 for (row = 0; row < child->top_attach; row++)
1608 y += table->rows[row].allocation;
1609 y += table->rows[row].spacing;
1612 for (row = child->top_attach; row < child->bottom_attach; row++)
1614 max_height += table->rows[row].allocation;
1615 if ((row + 1) < child->bottom_attach)
1616 max_height += table->rows[row].spacing;
1621 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1622 allocation.x = x + (max_width - allocation.width) / 2;
1626 allocation.width = child_requisition.width;
1627 allocation.x = x + (max_width - allocation.width) / 2;
1632 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1633 allocation.y = y + (max_height - allocation.height) / 2;
1637 allocation.height = child_requisition.height;
1638 allocation.y = y + (max_height - allocation.height) / 2;
1641 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1642 allocation.x = widget->allocation.x + widget->allocation.width
1643 - (allocation.x - widget->allocation.x) - allocation.width;
1645 gtk_widget_size_allocate (child->widget, &allocation);