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/.
44 CHILD_PROP_LEFT_ATTACH,
45 CHILD_PROP_RIGHT_ATTACH,
46 CHILD_PROP_TOP_ATTACH,
47 CHILD_PROP_BOTTOM_ATTACH,
55 static void gtk_table_class_init (GtkTableClass *klass);
56 static void gtk_table_init (GtkTable *table);
57 static void gtk_table_finalize (GObject *object);
58 static void gtk_table_size_request (GtkWidget *widget,
59 GtkRequisition *requisition);
60 static void gtk_table_size_allocate (GtkWidget *widget,
61 GtkAllocation *allocation);
62 static void gtk_table_add (GtkContainer *container,
64 static void gtk_table_remove (GtkContainer *container,
66 static void gtk_table_forall (GtkContainer *container,
67 gboolean include_internals,
69 gpointer callback_data);
70 static void gtk_table_get_property (GObject *object,
74 static void gtk_table_set_property (GObject *object,
78 static void gtk_table_set_child_property (GtkContainer *container,
83 static void gtk_table_get_child_property (GtkContainer *container,
88 static GType gtk_table_child_type (GtkContainer *container);
91 static void gtk_table_size_request_init (GtkTable *table);
92 static void gtk_table_size_request_pass1 (GtkTable *table);
93 static void gtk_table_size_request_pass2 (GtkTable *table);
94 static void gtk_table_size_request_pass3 (GtkTable *table);
96 static void gtk_table_size_allocate_init (GtkTable *table);
97 static void gtk_table_size_allocate_pass1 (GtkTable *table);
98 static void gtk_table_size_allocate_pass2 (GtkTable *table);
101 static GtkContainerClass *parent_class = NULL;
105 gtk_table_get_type (void)
107 static GType table_type = 0;
111 static const GTypeInfo table_info =
113 sizeof (GtkTableClass),
116 (GClassInitFunc) gtk_table_class_init,
121 (GInstanceInitFunc) gtk_table_init,
124 table_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTable",
132 gtk_table_class_init (GtkTableClass *class)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
135 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
136 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
138 parent_class = g_type_class_peek_parent (class);
140 gobject_class->finalize = gtk_table_finalize;
142 gobject_class->get_property = gtk_table_get_property;
143 gobject_class->set_property = gtk_table_set_property;
145 widget_class->size_request = gtk_table_size_request;
146 widget_class->size_allocate = gtk_table_size_allocate;
148 container_class->add = gtk_table_add;
149 container_class->remove = gtk_table_remove;
150 container_class->forall = gtk_table_forall;
151 container_class->child_type = gtk_table_child_type;
152 container_class->set_child_property = gtk_table_set_child_property;
153 container_class->get_child_property = gtk_table_get_child_property;
156 g_object_class_install_property (gobject_class,
158 g_param_spec_uint ("n_rows",
160 P_("The number of rows in the table"),
165 g_object_class_install_property (gobject_class,
167 g_param_spec_uint ("n_columns",
169 P_("The number of columns in the table"),
174 g_object_class_install_property (gobject_class,
176 g_param_spec_uint ("row_spacing",
178 P_("The amount of space between two consecutive rows"),
183 g_object_class_install_property (gobject_class,
185 g_param_spec_uint ("column_spacing",
186 P_("Column spacing"),
187 P_("The amount of space between two consecutive columns"),
192 g_object_class_install_property (gobject_class,
194 g_param_spec_boolean ("homogeneous",
196 P_("If TRUE this means the table cells are all the same width/height"),
200 gtk_container_class_install_child_property (container_class,
201 CHILD_PROP_LEFT_ATTACH,
202 g_param_spec_uint ("left_attach",
203 P_("Left attachment"),
204 P_("The column number to attach the left side of the child to"),
207 gtk_container_class_install_child_property (container_class,
208 CHILD_PROP_RIGHT_ATTACH,
209 g_param_spec_uint ("right_attach",
210 P_("Right attachment"),
211 P_("The column number to attach the right side of a child widget to"),
214 gtk_container_class_install_child_property (container_class,
215 CHILD_PROP_TOP_ATTACH,
216 g_param_spec_uint ("top_attach",
217 P_("Top attachment"),
218 P_("The row number to attach the top of a child widget to"),
221 gtk_container_class_install_child_property (container_class,
222 CHILD_PROP_BOTTOM_ATTACH,
223 g_param_spec_uint ("bottom_attach",
224 P_("Bottom attachment"),
225 P_("The row number to attach the bottom of the child to"),
228 gtk_container_class_install_child_property (container_class,
229 CHILD_PROP_X_OPTIONS,
230 g_param_spec_flags ("x_options",
231 P_("Horizontal options"),
232 P_("Options specifying the horizontal behaviour of the child"),
233 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
235 gtk_container_class_install_child_property (container_class,
236 CHILD_PROP_Y_OPTIONS,
237 g_param_spec_flags ("y_options",
238 P_("Vertical options"),
239 P_("Options specifying the vertical behaviour of the child"),
240 GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
242 gtk_container_class_install_child_property (container_class,
243 CHILD_PROP_X_PADDING,
244 g_param_spec_uint ("x_padding",
245 P_("Horizontal padding"),
246 P_("Extra space to put between the child and its left and right neighbors, in pixels"),
249 gtk_container_class_install_child_property (container_class,
250 CHILD_PROP_Y_PADDING,
251 g_param_spec_uint ("y_padding",
252 P_("Vertical padding"),
253 P_("Extra space to put between the child and its upper and lower neighbors, in pixels"),
259 gtk_table_child_type (GtkContainer *container)
261 return GTK_TYPE_WIDGET;
265 gtk_table_get_property (GObject *object,
272 table = GTK_TABLE (object);
277 g_value_set_uint (value, table->nrows);
280 g_value_set_uint (value, table->ncols);
282 case PROP_ROW_SPACING:
283 g_value_set_uint (value, table->row_spacing);
285 case PROP_COLUMN_SPACING:
286 g_value_set_uint (value, table->column_spacing);
288 case PROP_HOMOGENEOUS:
289 g_value_set_boolean (value, table->homogeneous);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 gtk_table_set_property (GObject *object,
305 table = GTK_TABLE (object);
310 gtk_table_resize (table, g_value_get_uint (value), table->ncols);
313 gtk_table_resize (table, table->nrows, g_value_get_uint (value));
315 case PROP_ROW_SPACING:
316 gtk_table_set_row_spacings (table, g_value_get_uint (value));
318 case PROP_COLUMN_SPACING:
319 gtk_table_set_col_spacings (table, g_value_get_uint (value));
321 case PROP_HOMOGENEOUS:
322 gtk_table_set_homogeneous (table, g_value_get_boolean (value));
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gtk_table_set_child_property (GtkContainer *container,
337 GtkTable *table = GTK_TABLE (container);
338 GtkTableChild *table_child;
342 for (list = table->children; list; list = list->next)
344 table_child = list->data;
346 if (table_child->widget == child)
351 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
357 case CHILD_PROP_LEFT_ATTACH:
358 table_child->left_attach = g_value_get_uint (value);
359 if (table_child->right_attach <= table_child->left_attach)
360 table_child->right_attach = table_child->left_attach + 1;
361 if (table_child->right_attach >= table->ncols)
362 gtk_table_resize (table, table->nrows, table_child->right_attach);
364 case CHILD_PROP_RIGHT_ATTACH:
365 table_child->right_attach = g_value_get_uint (value);
366 if (table_child->right_attach <= table_child->left_attach)
367 table_child->left_attach = table_child->right_attach - 1;
368 if (table_child->right_attach >= table->ncols)
369 gtk_table_resize (table, table->nrows, table_child->right_attach);
371 case CHILD_PROP_TOP_ATTACH:
372 table_child->top_attach = g_value_get_uint (value);
373 if (table_child->bottom_attach <= table_child->top_attach)
374 table_child->bottom_attach = table_child->top_attach + 1;
375 if (table_child->bottom_attach >= table->nrows)
376 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
378 case CHILD_PROP_BOTTOM_ATTACH:
379 table_child->bottom_attach = g_value_get_uint (value);
380 if (table_child->bottom_attach <= table_child->top_attach)
381 table_child->top_attach = table_child->bottom_attach - 1;
382 if (table_child->bottom_attach >= table->nrows)
383 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
385 case CHILD_PROP_X_OPTIONS:
386 table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
387 table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
388 table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
390 case CHILD_PROP_Y_OPTIONS:
391 table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
392 table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
393 table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
395 case CHILD_PROP_X_PADDING:
396 table_child->xpadding = g_value_get_uint (value);
398 case CHILD_PROP_Y_PADDING:
399 table_child->ypadding = g_value_get_uint (value);
402 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
405 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
406 gtk_widget_queue_resize (child);
410 gtk_table_get_child_property (GtkContainer *container,
416 GtkTable *table = GTK_TABLE (container);
417 GtkTableChild *table_child;
421 for (list = table->children; list; list = list->next)
423 table_child = list->data;
425 if (table_child->widget == child)
430 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
436 case CHILD_PROP_LEFT_ATTACH:
437 g_value_set_uint (value, table_child->left_attach);
439 case CHILD_PROP_RIGHT_ATTACH:
440 g_value_set_uint (value, table_child->right_attach);
442 case CHILD_PROP_TOP_ATTACH:
443 g_value_set_uint (value, table_child->top_attach);
445 case CHILD_PROP_BOTTOM_ATTACH:
446 g_value_set_uint (value, table_child->bottom_attach);
448 case CHILD_PROP_X_OPTIONS:
449 g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
450 table_child->xshrink * GTK_SHRINK |
451 table_child->xfill * GTK_FILL));
453 case CHILD_PROP_Y_OPTIONS:
454 g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
455 table_child->yshrink * GTK_SHRINK |
456 table_child->yfill * GTK_FILL));
458 case CHILD_PROP_X_PADDING:
459 g_value_set_uint (value, table_child->xpadding);
461 case CHILD_PROP_Y_PADDING:
462 g_value_set_uint (value, table_child->ypadding);
465 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
471 gtk_table_init (GtkTable *table)
473 GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
474 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
476 table->children = NULL;
481 table->column_spacing = 0;
482 table->row_spacing = 0;
483 table->homogeneous = FALSE;
485 gtk_table_resize (table, 1, 1);
489 gtk_table_new (guint rows,
491 gboolean homogeneous)
500 table = g_object_new (GTK_TYPE_TABLE, NULL);
502 table->homogeneous = (homogeneous ? TRUE : FALSE);
504 gtk_table_resize (table, rows, columns);
506 return GTK_WIDGET (table);
510 gtk_table_resize (GtkTable *table,
514 g_return_if_fail (GTK_IS_TABLE (table));
515 g_return_if_fail (n_rows > 0 && n_rows < 65536);
516 g_return_if_fail (n_cols > 0 && n_cols < 65536);
518 n_rows = MAX (n_rows, 1);
519 n_cols = MAX (n_cols, 1);
521 if (n_rows != table->nrows ||
522 n_cols != table->ncols)
526 for (list = table->children; list; list = list->next)
528 GtkTableChild *child;
532 n_rows = MAX (n_rows, child->bottom_attach);
533 n_cols = MAX (n_cols, child->right_attach);
536 if (n_rows != table->nrows)
541 table->nrows = n_rows;
542 table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
544 for (; i < table->nrows; i++)
546 table->rows[i].requisition = 0;
547 table->rows[i].allocation = 0;
548 table->rows[i].spacing = table->row_spacing;
549 table->rows[i].need_expand = 0;
550 table->rows[i].need_shrink = 0;
551 table->rows[i].expand = 0;
552 table->rows[i].shrink = 0;
555 g_object_notify (G_OBJECT (table), "n_rows");
558 if (n_cols != table->ncols)
563 table->ncols = n_cols;
564 table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
566 for (; i < table->ncols; i++)
568 table->cols[i].requisition = 0;
569 table->cols[i].allocation = 0;
570 table->cols[i].spacing = table->column_spacing;
571 table->cols[i].need_expand = 0;
572 table->cols[i].need_shrink = 0;
573 table->cols[i].expand = 0;
574 table->cols[i].shrink = 0;
577 g_object_notify (G_OBJECT (table), "n_columns");
583 gtk_table_attach (GtkTable *table,
589 GtkAttachOptions xoptions,
590 GtkAttachOptions yoptions,
594 GtkTableChild *table_child;
596 g_return_if_fail (GTK_IS_TABLE (table));
597 g_return_if_fail (GTK_IS_WIDGET (child));
598 g_return_if_fail (child->parent == NULL);
600 /* g_return_if_fail (left_attach >= 0); */
601 g_return_if_fail (left_attach < right_attach);
602 /* g_return_if_fail (top_attach >= 0); */
603 g_return_if_fail (top_attach < bottom_attach);
605 if (right_attach >= table->ncols)
606 gtk_table_resize (table, table->nrows, right_attach);
608 if (bottom_attach >= table->nrows)
609 gtk_table_resize (table, bottom_attach, table->ncols);
611 table_child = g_new (GtkTableChild, 1);
612 table_child->widget = child;
613 table_child->left_attach = left_attach;
614 table_child->right_attach = right_attach;
615 table_child->top_attach = top_attach;
616 table_child->bottom_attach = bottom_attach;
617 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
618 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
619 table_child->xfill = (xoptions & GTK_FILL) != 0;
620 table_child->xpadding = xpadding;
621 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
622 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
623 table_child->yfill = (yoptions & GTK_FILL) != 0;
624 table_child->ypadding = ypadding;
626 table->children = g_list_prepend (table->children, table_child);
628 gtk_widget_set_parent (child, GTK_WIDGET (table));
632 gtk_table_attach_defaults (GtkTable *table,
639 gtk_table_attach (table, widget,
640 left_attach, right_attach,
641 top_attach, bottom_attach,
642 GTK_EXPAND | GTK_FILL,
643 GTK_EXPAND | GTK_FILL,
648 gtk_table_set_row_spacing (GtkTable *table,
652 g_return_if_fail (GTK_IS_TABLE (table));
653 g_return_if_fail (row < table->nrows);
655 if (table->rows[row].spacing != spacing)
657 table->rows[row].spacing = spacing;
659 if (GTK_WIDGET_VISIBLE (table))
660 gtk_widget_queue_resize (GTK_WIDGET (table));
665 * gtk_table_get_row_spacing:
666 * @table: a #GtkTable
667 * @row: a row in the table, 0 indicates the first row
669 * Gets the amount of space between row @row, and
670 * row @row + 1. See gtk_table_set_row_spacing().
672 * Return value: the row spacing
675 gtk_table_get_row_spacing (GtkTable *table,
678 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
679 g_return_val_if_fail (row < table->nrows - 1, 0);
681 return table->rows[row].spacing;
685 gtk_table_set_col_spacing (GtkTable *table,
689 g_return_if_fail (GTK_IS_TABLE (table));
690 g_return_if_fail (column < table->ncols);
692 if (table->cols[column].spacing != spacing)
694 table->cols[column].spacing = spacing;
696 if (GTK_WIDGET_VISIBLE (table))
697 gtk_widget_queue_resize (GTK_WIDGET (table));
702 * gtk_table_get_col_spacing:
703 * @table: a #GtkTable
704 * @column: a column in the table, 0 indicates the first column
706 * Gets the amount of space between column @col, and
707 * column @col + 1. See gtk_table_set_col_spacing().
709 * Return value: the column spacing
712 gtk_table_get_col_spacing (GtkTable *table,
715 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
716 g_return_val_if_fail (column < table->ncols, 0);
718 return table->cols[column].spacing;
722 gtk_table_set_row_spacings (GtkTable *table,
727 g_return_if_fail (GTK_IS_TABLE (table));
729 table->row_spacing = spacing;
730 for (row = 0; row < table->nrows; row++)
731 table->rows[row].spacing = spacing;
733 if (GTK_WIDGET_VISIBLE (table))
734 gtk_widget_queue_resize (GTK_WIDGET (table));
736 g_object_notify (G_OBJECT (table), "row_spacing");
740 * gtk_table_get_default_row_spacing:
741 * @table: a #GtkTable
743 * Gets the default row spacing for the table. This is
744 * the spacing that will be used for newly added rows.
745 * (See gtk_table_set_row_spacings())
747 * Returns value: the default row spacing
750 gtk_table_get_default_row_spacing (GtkTable *table)
752 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
754 return table->row_spacing;
758 gtk_table_set_col_spacings (GtkTable *table,
763 g_return_if_fail (GTK_IS_TABLE (table));
765 table->column_spacing = spacing;
766 for (col = 0; col < table->ncols; col++)
767 table->cols[col].spacing = spacing;
769 if (GTK_WIDGET_VISIBLE (table))
770 gtk_widget_queue_resize (GTK_WIDGET (table));
772 g_object_notify (G_OBJECT (table), "column_spacing");
776 * gtk_table_get_default_col_spacing:
777 * @table: a #GtkTable
779 * Gets the default column spacing for the table. This is
780 * the spacing that will be used for newly added columns.
781 * (See gtk_table_set_col_spacings())
783 * Returns value: the default column spacing
786 gtk_table_get_default_col_spacing (GtkTable *table)
788 g_return_val_if_fail (GTK_IS_TABLE (table), 0);
790 return table->column_spacing;
794 gtk_table_set_homogeneous (GtkTable *table,
795 gboolean homogeneous)
797 g_return_if_fail (GTK_IS_TABLE (table));
799 homogeneous = (homogeneous != 0);
800 if (homogeneous != table->homogeneous)
802 table->homogeneous = homogeneous;
804 if (GTK_WIDGET_VISIBLE (table))
805 gtk_widget_queue_resize (GTK_WIDGET (table));
807 g_object_notify (G_OBJECT (table), "homogeneous");
812 * gtk_table_get_homogeneous:
813 * @table: a #GtkTable
815 * Returns whether the table cells are all constrained to the same
816 * width and height. (See gtk_table_set_homogenous ())
818 * Return value: %TRUE if the cells are all constrained to the same size
821 gtk_table_get_homogeneous (GtkTable *table)
823 g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
825 return table->homogeneous;
829 gtk_table_finalize (GObject *object)
833 g_return_if_fail (GTK_IS_TABLE (object));
835 table = GTK_TABLE (object);
837 g_free (table->rows);
838 g_free (table->cols);
840 G_OBJECT_CLASS (parent_class)->finalize (object);
844 gtk_table_size_request (GtkWidget *widget,
845 GtkRequisition *requisition)
850 g_return_if_fail (GTK_IS_TABLE (widget));
851 g_return_if_fail (requisition != NULL);
853 table = GTK_TABLE (widget);
855 requisition->width = 0;
856 requisition->height = 0;
858 gtk_table_size_request_init (table);
859 gtk_table_size_request_pass1 (table);
860 gtk_table_size_request_pass2 (table);
861 gtk_table_size_request_pass3 (table);
862 gtk_table_size_request_pass2 (table);
864 for (col = 0; col < table->ncols; col++)
865 requisition->width += table->cols[col].requisition;
866 for (col = 0; col + 1 < table->ncols; col++)
867 requisition->width += table->cols[col].spacing;
869 for (row = 0; row < table->nrows; row++)
870 requisition->height += table->rows[row].requisition;
871 for (row = 0; row + 1 < table->nrows; row++)
872 requisition->height += table->rows[row].spacing;
874 requisition->width += GTK_CONTAINER (table)->border_width * 2;
875 requisition->height += GTK_CONTAINER (table)->border_width * 2;
879 gtk_table_size_allocate (GtkWidget *widget,
880 GtkAllocation *allocation)
884 g_return_if_fail (GTK_IS_TABLE (widget));
885 g_return_if_fail (allocation != NULL);
887 widget->allocation = *allocation;
888 table = GTK_TABLE (widget);
890 gtk_table_size_allocate_init (table);
891 gtk_table_size_allocate_pass1 (table);
892 gtk_table_size_allocate_pass2 (table);
896 gtk_table_add (GtkContainer *container,
899 g_return_if_fail (GTK_IS_TABLE (container));
900 g_return_if_fail (widget != NULL);
902 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
906 gtk_table_remove (GtkContainer *container,
910 GtkTableChild *child;
913 g_return_if_fail (GTK_IS_TABLE (container));
914 g_return_if_fail (widget != NULL);
916 table = GTK_TABLE (container);
917 children = table->children;
921 child = children->data;
922 children = children->next;
924 if (child->widget == widget)
926 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
928 gtk_widget_unparent (widget);
930 table->children = g_list_remove (table->children, child);
933 if (was_visible && GTK_WIDGET_VISIBLE (container))
934 gtk_widget_queue_resize (GTK_WIDGET (container));
941 gtk_table_forall (GtkContainer *container,
942 gboolean include_internals,
943 GtkCallback callback,
944 gpointer callback_data)
947 GtkTableChild *child;
950 g_return_if_fail (GTK_IS_TABLE (container));
951 g_return_if_fail (callback != NULL);
953 table = GTK_TABLE (container);
954 children = table->children;
958 child = children->data;
959 children = children->next;
961 (* callback) (child->widget, callback_data);
966 gtk_table_size_request_init (GtkTable *table)
968 GtkTableChild *child;
972 for (row = 0; row < table->nrows; row++)
974 table->rows[row].requisition = 0;
975 table->rows[row].expand = FALSE;
977 for (col = 0; col < table->ncols; col++)
979 table->cols[col].requisition = 0;
980 table->cols[col].expand = FALSE;
983 children = table->children;
986 child = children->data;
987 children = children->next;
989 if (GTK_WIDGET_VISIBLE (child->widget))
990 gtk_widget_size_request (child->widget, NULL);
992 if (child->left_attach == (child->right_attach - 1) && child->xexpand)
993 table->cols[child->left_attach].expand = TRUE;
995 if (child->top_attach == (child->bottom_attach - 1) && child->yexpand)
996 table->rows[child->top_attach].expand = TRUE;
1001 gtk_table_size_request_pass1 (GtkTable *table)
1003 GtkTableChild *child;
1008 children = table->children;
1011 child = children->data;
1012 children = children->next;
1014 if (GTK_WIDGET_VISIBLE (child->widget))
1016 GtkRequisition child_requisition;
1017 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1019 /* Child spans a single column.
1021 if (child->left_attach == (child->right_attach - 1))
1023 width = child_requisition.width + child->xpadding * 2;
1024 table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
1027 /* Child spans a single row.
1029 if (child->top_attach == (child->bottom_attach - 1))
1031 height = child_requisition.height + child->ypadding * 2;
1032 table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
1039 gtk_table_size_request_pass2 (GtkTable *table)
1045 if (table->homogeneous)
1050 for (col = 0; col < table->ncols; col++)
1051 max_width = MAX (max_width, table->cols[col].requisition);
1052 for (row = 0; row < table->nrows; row++)
1053 max_height = MAX (max_height, table->rows[row].requisition);
1055 for (col = 0; col < table->ncols; col++)
1056 table->cols[col].requisition = max_width;
1057 for (row = 0; row < table->nrows; row++)
1058 table->rows[row].requisition = max_height;
1063 gtk_table_size_request_pass3 (GtkTable *table)
1065 GtkTableChild *child;
1071 children = table->children;
1074 child = children->data;
1075 children = children->next;
1077 if (GTK_WIDGET_VISIBLE (child->widget))
1079 /* Child spans multiple columns.
1081 if (child->left_attach != (child->right_attach - 1))
1083 GtkRequisition child_requisition;
1085 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1087 /* Check and see if there is already enough space
1091 for (col = child->left_attach; col < child->right_attach; col++)
1093 width += table->cols[col].requisition;
1094 if ((col + 1) < child->right_attach)
1095 width += table->cols[col].spacing;
1098 /* If we need to request more space for this child to fill
1099 * its requisition, then divide up the needed space amongst the
1100 * columns it spans, favoring expandable columns if any.
1102 if (width < child_requisition.width + child->xpadding * 2)
1105 gboolean force_expand = FALSE;
1107 width = child_requisition.width + child->xpadding * 2 - width;
1109 for (col = child->left_attach; col < child->right_attach; col++)
1110 if (table->cols[col].expand)
1115 n_expand = (child->right_attach - child->left_attach);
1116 force_expand = TRUE;
1119 for (col = child->left_attach; col < child->right_attach; col++)
1120 if (force_expand || table->cols[col].expand)
1122 extra = width / n_expand;
1123 table->cols[col].requisition += extra;
1130 /* Child spans multiple rows.
1132 if (child->top_attach != (child->bottom_attach - 1))
1134 GtkRequisition child_requisition;
1136 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1138 /* Check and see if there is already enough space
1142 for (row = child->top_attach; row < child->bottom_attach; row++)
1144 height += table->rows[row].requisition;
1145 if ((row + 1) < child->bottom_attach)
1146 height += table->rows[row].spacing;
1149 /* If we need to request more space for this child to fill
1150 * its requisition, then divide up the needed space amongst the
1151 * rows it spans, favoring expandable rows if any.
1153 if (height < child_requisition.height + child->ypadding * 2)
1156 gboolean force_expand = FALSE;
1158 height = child_requisition.height + child->ypadding * 2 - height;
1160 for (row = child->top_attach; row < child->bottom_attach; row++)
1162 if (table->rows[row].expand)
1168 n_expand = (child->bottom_attach - child->top_attach);
1169 force_expand = TRUE;
1172 for (row = child->top_attach; row < child->bottom_attach; row++)
1173 if (force_expand || table->rows[row].expand)
1175 extra = height / n_expand;
1176 table->rows[row].requisition += extra;
1187 gtk_table_size_allocate_init (GtkTable *table)
1189 GtkTableChild *child;
1195 /* Initialize the rows and cols.
1196 * By default, rows and cols do not expand and do shrink.
1197 * Those values are modified by the children that occupy
1198 * the rows and cols.
1200 for (col = 0; col < table->ncols; col++)
1202 table->cols[col].allocation = table->cols[col].requisition;
1203 table->cols[col].need_expand = FALSE;
1204 table->cols[col].need_shrink = TRUE;
1205 table->cols[col].expand = FALSE;
1206 table->cols[col].shrink = TRUE;
1207 table->cols[col].empty = TRUE;
1209 for (row = 0; row < table->nrows; row++)
1211 table->rows[row].allocation = table->rows[row].requisition;
1212 table->rows[row].need_expand = FALSE;
1213 table->rows[row].need_shrink = TRUE;
1214 table->rows[row].expand = FALSE;
1215 table->rows[row].shrink = TRUE;
1216 table->rows[row].empty = TRUE;
1219 /* Loop over all the children and adjust the row and col values
1220 * based on whether the children want to be allowed to expand
1221 * or shrink. This loop handles children that occupy a single
1224 children = table->children;
1227 child = children->data;
1228 children = children->next;
1230 if (GTK_WIDGET_VISIBLE (child->widget))
1232 if (child->left_attach == (child->right_attach - 1))
1235 table->cols[child->left_attach].expand = TRUE;
1237 if (!child->xshrink)
1238 table->cols[child->left_attach].shrink = FALSE;
1240 table->cols[child->left_attach].empty = FALSE;
1243 if (child->top_attach == (child->bottom_attach - 1))
1246 table->rows[child->top_attach].expand = TRUE;
1248 if (!child->yshrink)
1249 table->rows[child->top_attach].shrink = FALSE;
1251 table->rows[child->top_attach].empty = FALSE;
1256 /* Loop over all the children again and this time handle children
1257 * which span multiple rows or columns.
1259 children = table->children;
1262 child = children->data;
1263 children = children->next;
1265 if (GTK_WIDGET_VISIBLE (child->widget))
1267 if (child->left_attach != (child->right_attach - 1))
1269 for (col = child->left_attach; col < child->right_attach; col++)
1270 table->cols[col].empty = FALSE;
1275 for (col = child->left_attach; col < child->right_attach; col++)
1276 if (table->cols[col].expand)
1283 for (col = child->left_attach; col < child->right_attach; col++)
1284 table->cols[col].need_expand = TRUE;
1287 if (!child->xshrink)
1290 for (col = child->left_attach; col < child->right_attach; col++)
1291 if (!table->cols[col].shrink)
1298 for (col = child->left_attach; col < child->right_attach; col++)
1299 table->cols[col].need_shrink = FALSE;
1303 if (child->top_attach != (child->bottom_attach - 1))
1305 for (row = child->top_attach; row < child->bottom_attach; row++)
1306 table->rows[row].empty = FALSE;
1311 for (row = child->top_attach; row < child->bottom_attach; row++)
1312 if (table->rows[row].expand)
1319 for (row = child->top_attach; row < child->bottom_attach; row++)
1320 table->rows[row].need_expand = TRUE;
1323 if (!child->yshrink)
1326 for (row = child->top_attach; row < child->bottom_attach; row++)
1327 if (!table->rows[row].shrink)
1334 for (row = child->top_attach; row < child->bottom_attach; row++)
1335 table->rows[row].need_shrink = FALSE;
1341 /* Loop over the columns and set the expand and shrink values
1342 * if the column can be expanded or shrunk.
1344 for (col = 0; col < table->ncols; col++)
1346 if (table->cols[col].empty)
1348 table->cols[col].expand = FALSE;
1349 table->cols[col].shrink = FALSE;
1353 if (table->cols[col].need_expand)
1354 table->cols[col].expand = TRUE;
1355 if (!table->cols[col].need_shrink)
1356 table->cols[col].shrink = FALSE;
1360 /* Loop over the rows and set the expand and shrink values
1361 * if the row can be expanded or shrunk.
1363 for (row = 0; row < table->nrows; row++)
1365 if (table->rows[row].empty)
1367 table->rows[row].expand = FALSE;
1368 table->rows[row].shrink = FALSE;
1372 if (table->rows[row].need_expand)
1373 table->rows[row].expand = TRUE;
1374 if (!table->rows[row].need_shrink)
1375 table->rows[row].shrink = FALSE;
1381 gtk_table_size_allocate_pass1 (GtkTable *table)
1391 /* If we were allocated more space than we requested
1392 * then we have to expand any expandable rows and columns
1393 * to fill in the extra space.
1396 real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1397 real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1399 if (table->homogeneous)
1401 if (!table->children)
1406 for (col = 0; col < table->ncols; col++)
1407 if (table->cols[col].expand)
1416 for (col = 0; col + 1 < table->ncols; col++)
1417 width -= table->cols[col].spacing;
1419 for (col = 0; col < table->ncols; col++)
1421 extra = width / (table->ncols - col);
1422 table->cols[col].allocation = MAX (1, extra);
1433 for (col = 0; col < table->ncols; col++)
1435 width += table->cols[col].requisition;
1436 if (table->cols[col].expand)
1438 if (table->cols[col].shrink)
1441 for (col = 0; col + 1 < table->ncols; col++)
1442 width += table->cols[col].spacing;
1444 /* Check to see if we were allocated more width than we requested.
1446 if ((width < real_width) && (nexpand >= 1))
1448 width = real_width - width;
1450 for (col = 0; col < table->ncols; col++)
1451 if (table->cols[col].expand)
1453 extra = width / nexpand;
1454 table->cols[col].allocation += extra;
1461 /* Check to see if we were allocated less width than we requested,
1462 * then shrink until we fit the size give.
1464 if (width > real_width)
1466 gint total_nshrink = nshrink;
1468 extra = width - real_width;
1469 while (total_nshrink > 0 && extra > 0)
1471 nshrink = total_nshrink;
1472 for (col = 0; col < table->ncols; col++)
1473 if (table->cols[col].shrink)
1475 gint allocation = table->cols[col].allocation;
1477 table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1478 extra -= allocation - table->cols[col].allocation;
1480 if (table->cols[col].allocation < 2)
1483 table->cols[col].shrink = FALSE;
1490 if (table->homogeneous)
1492 if (!table->children)
1497 for (row = 0; row < table->nrows; row++)
1498 if (table->rows[row].expand)
1506 height = real_height;
1508 for (row = 0; row + 1 < table->nrows; row++)
1509 height -= table->rows[row].spacing;
1512 for (row = 0; row < table->nrows; row++)
1514 extra = height / (table->nrows - row);
1515 table->rows[row].allocation = MAX (1, extra);
1526 for (row = 0; row < table->nrows; row++)
1528 height += table->rows[row].requisition;
1529 if (table->rows[row].expand)
1531 if (table->rows[row].shrink)
1534 for (row = 0; row + 1 < table->nrows; row++)
1535 height += table->rows[row].spacing;
1537 /* Check to see if we were allocated more height than we requested.
1539 if ((height < real_height) && (nexpand >= 1))
1541 height = real_height - height;
1543 for (row = 0; row < table->nrows; row++)
1544 if (table->rows[row].expand)
1546 extra = height / nexpand;
1547 table->rows[row].allocation += extra;
1554 /* Check to see if we were allocated less height than we requested.
1555 * then shrink until we fit the size give.
1557 if (height > real_height)
1559 gint total_nshrink = nshrink;
1561 extra = height - real_height;
1562 while (total_nshrink > 0 && extra > 0)
1564 nshrink = total_nshrink;
1565 for (row = 0; row < table->nrows; row++)
1566 if (table->rows[row].shrink)
1568 gint allocation = table->rows[row].allocation;
1570 table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1571 extra -= allocation - table->rows[row].allocation;
1573 if (table->rows[row].allocation < 2)
1576 table->rows[row].shrink = FALSE;
1585 gtk_table_size_allocate_pass2 (GtkTable *table)
1587 GtkTableChild *child;
1593 GtkAllocation allocation;
1594 GtkWidget *widget = GTK_WIDGET (table);
1596 children = table->children;
1599 child = children->data;
1600 children = children->next;
1602 if (GTK_WIDGET_VISIBLE (child->widget))
1604 GtkRequisition child_requisition;
1605 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1607 x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1608 y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1612 for (col = 0; col < child->left_attach; col++)
1614 x += table->cols[col].allocation;
1615 x += table->cols[col].spacing;
1618 for (col = child->left_attach; col < child->right_attach; col++)
1620 max_width += table->cols[col].allocation;
1621 if ((col + 1) < child->right_attach)
1622 max_width += table->cols[col].spacing;
1625 for (row = 0; row < child->top_attach; row++)
1627 y += table->rows[row].allocation;
1628 y += table->rows[row].spacing;
1631 for (row = child->top_attach; row < child->bottom_attach; row++)
1633 max_height += table->rows[row].allocation;
1634 if ((row + 1) < child->bottom_attach)
1635 max_height += table->rows[row].spacing;
1640 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1641 allocation.x = x + (max_width - allocation.width) / 2;
1645 allocation.width = child_requisition.width;
1646 allocation.x = x + (max_width - allocation.width) / 2;
1651 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1652 allocation.y = y + (max_height - allocation.height) / 2;
1656 allocation.height = child_requisition.height;
1657 allocation.y = y + (max_height - allocation.height) / 2;
1660 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1661 allocation.x = widget->allocation.x + widget->allocation.width
1662 - (allocation.x - widget->allocation.x) - allocation.width;
1664 gtk_widget_size_allocate (child->widget, &allocation);