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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
42 CHILD_ARG_LEFT_ATTACH,
43 CHILD_ARG_RIGHT_ATTACH,
45 CHILD_ARG_BOTTOM_ATTACH,
53 static void gtk_table_class_init (GtkTableClass *klass);
54 static void gtk_table_init (GtkTable *table);
55 static void gtk_table_finalize (GtkObject *object);
56 static void gtk_table_map (GtkWidget *widget);
57 static void gtk_table_unmap (GtkWidget *widget);
58 static void gtk_table_draw (GtkWidget *widget,
60 static gint gtk_table_expose (GtkWidget *widget,
61 GdkEventExpose *event);
62 static void gtk_table_size_request (GtkWidget *widget,
63 GtkRequisition *requisition);
64 static void gtk_table_size_allocate (GtkWidget *widget,
65 GtkAllocation *allocation);
66 static void gtk_table_add (GtkContainer *container,
68 static void gtk_table_remove (GtkContainer *container,
70 static void gtk_table_forall (GtkContainer *container,
71 gboolean include_internals,
73 gpointer callback_data);
74 static void gtk_table_get_arg (GtkObject *object,
77 static void gtk_table_set_arg (GtkObject *object,
80 static void gtk_table_set_child_arg (GtkContainer *container,
84 static void gtk_table_get_child_arg (GtkContainer *container,
88 static GtkType 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 GtkType table_type = 0;
111 static const GtkTypeInfo table_info =
115 sizeof (GtkTableClass),
116 (GtkClassInitFunc) gtk_table_class_init,
117 (GtkObjectInitFunc) gtk_table_init,
118 /* reserved_1 */ NULL,
119 /* reserved_2 */ NULL,
120 (GtkClassInitFunc) NULL,
123 table_type = gtk_type_unique (gtk_container_get_type (), &table_info);
130 gtk_table_class_init (GtkTableClass *class)
132 GtkObjectClass *object_class;
133 GtkWidgetClass *widget_class;
134 GtkContainerClass *container_class;
136 object_class = (GtkObjectClass*) class;
137 widget_class = (GtkWidgetClass*) class;
138 container_class = (GtkContainerClass*) class;
140 parent_class = gtk_type_class (gtk_container_get_type ());
142 gtk_object_add_arg_type ("GtkTable::n_rows", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_N_ROWS);
143 gtk_object_add_arg_type ("GtkTable::n_columns", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_N_COLUMNS);
144 gtk_object_add_arg_type ("GtkTable::row_spacing", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_ROW_SPACING);
145 gtk_object_add_arg_type ("GtkTable::column_spacing", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_COLUMN_SPACING);
146 gtk_object_add_arg_type ("GtkTable::homogeneous", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HOMOGENEOUS);
147 gtk_container_add_child_arg_type ("GtkTable::left_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_LEFT_ATTACH);
148 gtk_container_add_child_arg_type ("GtkTable::right_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_RIGHT_ATTACH);
149 gtk_container_add_child_arg_type ("GtkTable::top_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_TOP_ATTACH);
150 gtk_container_add_child_arg_type ("GtkTable::bottom_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_BOTTOM_ATTACH);
151 gtk_container_add_child_arg_type ("GtkTable::x_options", GTK_TYPE_ATTACH_OPTIONS, GTK_ARG_READWRITE, CHILD_ARG_X_OPTIONS);
152 gtk_container_add_child_arg_type ("GtkTable::y_options", GTK_TYPE_ATTACH_OPTIONS, GTK_ARG_READWRITE, CHILD_ARG_Y_OPTIONS);
153 gtk_container_add_child_arg_type ("GtkTable::x_padding", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_X_PADDING);
154 gtk_container_add_child_arg_type ("GtkTable::y_padding", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_Y_PADDING);
156 object_class->get_arg = gtk_table_get_arg;
157 object_class->set_arg = gtk_table_set_arg;
158 object_class->finalize = gtk_table_finalize;
160 widget_class->map = gtk_table_map;
161 widget_class->unmap = gtk_table_unmap;
162 widget_class->draw = gtk_table_draw;
163 widget_class->expose_event = gtk_table_expose;
164 widget_class->size_request = gtk_table_size_request;
165 widget_class->size_allocate = gtk_table_size_allocate;
167 container_class->add = gtk_table_add;
168 container_class->remove = gtk_table_remove;
169 container_class->forall = gtk_table_forall;
170 container_class->child_type = gtk_table_child_type;
171 container_class->set_child_arg = gtk_table_set_child_arg;
172 container_class->get_child_arg = gtk_table_get_child_arg;
176 gtk_table_child_type (GtkContainer *container)
178 return GTK_TYPE_WIDGET;
182 gtk_table_get_arg (GtkObject *object,
188 table = GTK_TABLE (object);
193 GTK_VALUE_UINT (*arg) = table->nrows;
196 GTK_VALUE_UINT (*arg) = table->ncols;
198 case ARG_ROW_SPACING:
199 GTK_VALUE_UINT (*arg) = table->row_spacing;
201 case ARG_COLUMN_SPACING:
202 GTK_VALUE_UINT (*arg) = table->column_spacing;
204 case ARG_HOMOGENEOUS:
205 GTK_VALUE_BOOL (*arg) = table->homogeneous;
208 arg->type = GTK_TYPE_INVALID;
214 gtk_table_set_arg (GtkObject *object,
220 table = GTK_TABLE (object);
225 gtk_table_resize (table, GTK_VALUE_UINT (*arg), table->ncols);
228 gtk_table_resize (table, table->nrows, GTK_VALUE_UINT (*arg));
230 case ARG_ROW_SPACING:
231 gtk_table_set_row_spacings (table, GTK_VALUE_UINT (*arg));
233 case ARG_COLUMN_SPACING:
234 gtk_table_set_col_spacings (table, GTK_VALUE_UINT (*arg));
236 case ARG_HOMOGENEOUS:
237 gtk_table_set_homogeneous (table, GTK_VALUE_BOOL (*arg));
245 gtk_table_set_child_arg (GtkContainer *container,
251 GtkTableChild *table_child;
254 table = GTK_TABLE (container);
256 for (list = table->children; list; list = list->next)
258 table_child = list->data;
260 if (table_child->widget == child)
268 case CHILD_ARG_LEFT_ATTACH:
269 table_child->left_attach = GTK_VALUE_UINT (*arg);
270 if (table_child->right_attach <= table_child->left_attach)
271 table_child->right_attach = table_child->left_attach + 1;
272 if (table_child->right_attach >= table->ncols)
273 gtk_table_resize (table, table->ncols, table_child->right_attach);
275 case CHILD_ARG_RIGHT_ATTACH:
276 if (GTK_VALUE_UINT (*arg) > 0)
278 table_child->right_attach = GTK_VALUE_UINT (*arg);
279 if (table_child->right_attach <= table_child->left_attach)
280 table_child->left_attach = table_child->right_attach - 1;
281 if (table_child->right_attach >= table->ncols)
282 gtk_table_resize (table, table->ncols, table_child->right_attach);
285 case CHILD_ARG_TOP_ATTACH:
286 table_child->top_attach = GTK_VALUE_UINT (*arg);
287 if (table_child->bottom_attach <= table_child->top_attach)
288 table_child->bottom_attach = table_child->top_attach + 1;
289 if (table_child->bottom_attach >= table->nrows)
290 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
292 case CHILD_ARG_BOTTOM_ATTACH:
293 if (GTK_VALUE_UINT (*arg) > 0)
295 table_child->bottom_attach = GTK_VALUE_UINT (*arg);
296 if (table_child->bottom_attach <= table_child->top_attach)
297 table_child->top_attach = table_child->bottom_attach - 1;
298 if (table_child->bottom_attach >= table->nrows)
299 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
302 case CHILD_ARG_X_OPTIONS:
303 table_child->xexpand = (GTK_VALUE_FLAGS (*arg) & GTK_EXPAND) != 0;
304 table_child->xshrink = (GTK_VALUE_FLAGS (*arg) & GTK_SHRINK) != 0;
305 table_child->xfill = (GTK_VALUE_FLAGS (*arg) & GTK_FILL) != 0;
307 case CHILD_ARG_Y_OPTIONS:
308 table_child->yexpand = (GTK_VALUE_FLAGS (*arg) & GTK_EXPAND) != 0;
309 table_child->yshrink = (GTK_VALUE_FLAGS (*arg) & GTK_SHRINK) != 0;
310 table_child->yfill = (GTK_VALUE_FLAGS (*arg) & GTK_FILL) != 0;
312 case CHILD_ARG_X_PADDING:
313 table_child->xpadding = GTK_VALUE_UINT (*arg);
315 case CHILD_ARG_Y_PADDING:
316 table_child->ypadding = GTK_VALUE_UINT (*arg);
321 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
322 gtk_widget_queue_resize (child);
326 gtk_table_get_child_arg (GtkContainer *container,
332 GtkTableChild *table_child;
335 table = GTK_TABLE (container);
337 for (list = table->children; list; list = list->next)
339 table_child = list->data;
341 if (table_child->widget == child)
349 case CHILD_ARG_LEFT_ATTACH:
350 GTK_VALUE_UINT (*arg) = table_child->left_attach;
352 case CHILD_ARG_RIGHT_ATTACH:
353 GTK_VALUE_UINT (*arg) = table_child->right_attach;
355 case CHILD_ARG_TOP_ATTACH:
356 GTK_VALUE_UINT (*arg) = table_child->top_attach;
358 case CHILD_ARG_BOTTOM_ATTACH:
359 GTK_VALUE_UINT (*arg) = table_child->bottom_attach;
361 case CHILD_ARG_X_OPTIONS:
362 GTK_VALUE_FLAGS (*arg) = (table_child->xexpand * GTK_EXPAND |
363 table_child->xshrink * GTK_SHRINK |
364 table_child->xfill * GTK_FILL);
366 case CHILD_ARG_Y_OPTIONS:
367 GTK_VALUE_FLAGS (*arg) = (table_child->yexpand * GTK_EXPAND |
368 table_child->yshrink * GTK_SHRINK |
369 table_child->yfill * GTK_FILL);
371 case CHILD_ARG_X_PADDING:
372 GTK_VALUE_UINT (*arg) = table_child->xpadding;
374 case CHILD_ARG_Y_PADDING:
375 GTK_VALUE_UINT (*arg) = table_child->ypadding;
378 arg->type = GTK_TYPE_INVALID;
384 gtk_table_init (GtkTable *table)
386 GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
388 table->children = NULL;
393 table->column_spacing = 0;
394 table->row_spacing = 0;
395 table->homogeneous = FALSE;
397 gtk_table_resize (table, 1, 1);
401 gtk_table_new (guint rows,
403 gboolean homogeneous)
412 table = gtk_type_new (gtk_table_get_type ());
414 table->homogeneous = (homogeneous ? TRUE : FALSE);
416 gtk_table_resize (table, rows, columns);
418 return GTK_WIDGET (table);
422 gtk_table_resize (GtkTable *table,
426 g_return_if_fail (table != NULL);
427 g_return_if_fail (GTK_IS_TABLE (table));
429 n_rows = MAX (n_rows, 1);
430 n_cols = MAX (n_cols, 1);
432 if (n_rows != table->nrows ||
433 n_cols != table->ncols)
437 for (list = table->children; list; list = list->next)
439 GtkTableChild *child;
443 n_rows = MAX (n_rows, child->bottom_attach);
444 n_cols = MAX (n_cols, child->right_attach);
447 if (n_rows != table->nrows)
452 table->nrows = n_rows;
453 table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
455 for (; i < table->nrows; i++)
457 table->rows[i].requisition = 0;
458 table->rows[i].allocation = 0;
459 table->rows[i].spacing = table->row_spacing;
460 table->rows[i].need_expand = 0;
461 table->rows[i].need_shrink = 0;
462 table->rows[i].expand = 0;
463 table->rows[i].shrink = 0;
467 if (n_cols != table->ncols)
472 table->ncols = n_cols;
473 table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
475 for (; i < table->ncols; i++)
477 table->cols[i].requisition = 0;
478 table->cols[i].allocation = 0;
479 table->cols[i].spacing = table->column_spacing;
480 table->cols[i].need_expand = 0;
481 table->cols[i].need_shrink = 0;
482 table->cols[i].expand = 0;
483 table->cols[i].shrink = 0;
490 gtk_table_attach (GtkTable *table,
496 GtkAttachOptions xoptions,
497 GtkAttachOptions yoptions,
501 GtkTableChild *table_child;
503 g_return_if_fail (table != NULL);
504 g_return_if_fail (GTK_IS_TABLE (table));
505 g_return_if_fail (child != NULL);
506 g_return_if_fail (GTK_IS_WIDGET (child));
507 g_return_if_fail (child->parent == NULL);
509 /* g_return_if_fail (left_attach >= 0); */
510 g_return_if_fail (left_attach < right_attach);
511 /* g_return_if_fail (top_attach >= 0); */
512 g_return_if_fail (top_attach < bottom_attach);
514 if (right_attach >= table->ncols)
515 gtk_table_resize (table, table->nrows, right_attach);
517 if (bottom_attach >= table->nrows)
518 gtk_table_resize (table, bottom_attach, table->ncols);
520 table_child = g_new (GtkTableChild, 1);
521 table_child->widget = child;
522 table_child->left_attach = left_attach;
523 table_child->right_attach = right_attach;
524 table_child->top_attach = top_attach;
525 table_child->bottom_attach = bottom_attach;
526 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
527 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
528 table_child->xfill = (xoptions & GTK_FILL) != 0;
529 table_child->xpadding = xpadding;
530 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
531 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
532 table_child->yfill = (yoptions & GTK_FILL) != 0;
533 table_child->ypadding = ypadding;
535 table->children = g_list_prepend (table->children, table_child);
537 gtk_widget_set_parent (child, GTK_WIDGET (table));
539 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (table)))
541 if (GTK_WIDGET_REALIZED (GTK_WIDGET (table)) &&
542 !GTK_WIDGET_REALIZED (child))
543 gtk_widget_realize (child);
545 if (GTK_WIDGET_MAPPED (GTK_WIDGET (table)) &&
546 !GTK_WIDGET_MAPPED (child))
547 gtk_widget_map (child);
550 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
551 gtk_widget_queue_resize (child);
555 gtk_table_attach_defaults (GtkTable *table,
562 gtk_table_attach (table, widget,
563 left_attach, right_attach,
564 top_attach, bottom_attach,
565 GTK_EXPAND | GTK_FILL,
566 GTK_EXPAND | GTK_FILL,
571 gtk_table_set_row_spacing (GtkTable *table,
575 g_return_if_fail (table != NULL);
576 g_return_if_fail (GTK_IS_TABLE (table));
577 g_return_if_fail (row + 1 < table->nrows);
579 if (table->rows[row].spacing != spacing)
581 table->rows[row].spacing = spacing;
583 if (GTK_WIDGET_VISIBLE (table))
584 gtk_widget_queue_resize (GTK_WIDGET (table));
589 gtk_table_set_col_spacing (GtkTable *table,
593 g_return_if_fail (table != NULL);
594 g_return_if_fail (GTK_IS_TABLE (table));
595 g_return_if_fail (column + 1 < table->ncols);
597 if (table->cols[column].spacing != spacing)
599 table->cols[column].spacing = spacing;
601 if (GTK_WIDGET_VISIBLE (table))
602 gtk_widget_queue_resize (GTK_WIDGET (table));
607 gtk_table_set_row_spacings (GtkTable *table,
612 g_return_if_fail (table != NULL);
613 g_return_if_fail (GTK_IS_TABLE (table));
615 table->row_spacing = spacing;
616 for (row = 0; row + 1 < table->nrows; row++)
617 table->rows[row].spacing = spacing;
619 if (GTK_WIDGET_VISIBLE (table))
620 gtk_widget_queue_resize (GTK_WIDGET (table));
624 gtk_table_set_col_spacings (GtkTable *table,
629 g_return_if_fail (table != NULL);
630 g_return_if_fail (GTK_IS_TABLE (table));
632 table->column_spacing = spacing;
633 for (col = 0; col + 1 < table->ncols; col++)
634 table->cols[col].spacing = spacing;
636 if (GTK_WIDGET_VISIBLE (table))
637 gtk_widget_queue_resize (GTK_WIDGET (table));
641 gtk_table_set_homogeneous (GtkTable *table,
642 gboolean homogeneous)
644 g_return_if_fail (table != NULL);
645 g_return_if_fail (GTK_IS_TABLE (table));
647 homogeneous = (homogeneous != 0);
648 if (homogeneous != table->homogeneous)
650 table->homogeneous = homogeneous;
652 if (GTK_WIDGET_VISIBLE (table))
653 gtk_widget_queue_resize (GTK_WIDGET (table));
658 gtk_table_finalize (GtkObject *object)
662 g_return_if_fail (object != NULL);
663 g_return_if_fail (GTK_IS_TABLE (object));
665 table = GTK_TABLE (object);
667 g_free (table->rows);
668 g_free (table->cols);
670 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
674 gtk_table_map (GtkWidget *widget)
677 GtkTableChild *child;
680 g_return_if_fail (widget != NULL);
681 g_return_if_fail (GTK_IS_TABLE (widget));
683 table = GTK_TABLE (widget);
684 GTK_WIDGET_SET_FLAGS (table, GTK_MAPPED);
686 children = table->children;
689 child = children->data;
690 children = children->next;
692 if (GTK_WIDGET_VISIBLE (child->widget) &&
693 !GTK_WIDGET_MAPPED (child->widget))
694 gtk_widget_map (child->widget);
699 gtk_table_unmap (GtkWidget *widget)
702 GtkTableChild *child;
705 g_return_if_fail (widget != NULL);
706 g_return_if_fail (GTK_IS_TABLE (widget));
708 table = GTK_TABLE (widget);
709 GTK_WIDGET_UNSET_FLAGS (table, GTK_MAPPED);
711 children = table->children;
714 child = children->data;
715 children = children->next;
717 if (GTK_WIDGET_VISIBLE (child->widget) &&
718 GTK_WIDGET_MAPPED (child->widget))
719 gtk_widget_unmap (child->widget);
724 gtk_table_draw (GtkWidget *widget,
728 GtkTableChild *child;
730 GdkRectangle child_area;
732 g_return_if_fail (widget != NULL);
733 g_return_if_fail (GTK_IS_TABLE (widget));
735 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
737 table = GTK_TABLE (widget);
739 children = table->children;
742 child = children->data;
743 children = children->next;
745 if (gtk_widget_intersect (child->widget, area, &child_area))
746 gtk_widget_draw (child->widget, &child_area);
752 gtk_table_expose (GtkWidget *widget,
753 GdkEventExpose *event)
756 GtkTableChild *child;
758 GdkEventExpose child_event;
760 g_return_val_if_fail (widget != NULL, FALSE);
761 g_return_val_if_fail (GTK_IS_TABLE (widget), FALSE);
763 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
765 table = GTK_TABLE (widget);
767 child_event = *event;
769 children = table->children;
772 child = children->data;
773 children = children->next;
775 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
776 gtk_widget_intersect (child->widget, &event->area, &child_event.area))
777 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
785 gtk_table_size_request (GtkWidget *widget,
786 GtkRequisition *requisition)
791 g_return_if_fail (widget != NULL);
792 g_return_if_fail (GTK_IS_TABLE (widget));
793 g_return_if_fail (requisition != NULL);
795 table = GTK_TABLE (widget);
797 requisition->width = 0;
798 requisition->height = 0;
800 gtk_table_size_request_init (table);
801 gtk_table_size_request_pass1 (table);
802 gtk_table_size_request_pass2 (table);
803 gtk_table_size_request_pass3 (table);
804 gtk_table_size_request_pass2 (table);
806 for (col = 0; col < table->ncols; col++)
807 requisition->width += table->cols[col].requisition;
808 for (col = 0; col + 1 < table->ncols; col++)
809 requisition->width += table->cols[col].spacing;
811 for (row = 0; row < table->nrows; row++)
812 requisition->height += table->rows[row].requisition;
813 for (row = 0; row + 1 < table->nrows; row++)
814 requisition->height += table->rows[row].spacing;
816 requisition->width += GTK_CONTAINER (table)->border_width * 2;
817 requisition->height += GTK_CONTAINER (table)->border_width * 2;
821 gtk_table_size_allocate (GtkWidget *widget,
822 GtkAllocation *allocation)
826 g_return_if_fail (widget != NULL);
827 g_return_if_fail (GTK_IS_TABLE (widget));
828 g_return_if_fail (allocation != NULL);
830 widget->allocation = *allocation;
831 table = GTK_TABLE (widget);
833 gtk_table_size_allocate_init (table);
834 gtk_table_size_allocate_pass1 (table);
835 gtk_table_size_allocate_pass2 (table);
839 gtk_table_add (GtkContainer *container,
842 g_return_if_fail (container != NULL);
843 g_return_if_fail (GTK_IS_TABLE (container));
844 g_return_if_fail (widget != NULL);
846 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
850 gtk_table_remove (GtkContainer *container,
854 GtkTableChild *child;
857 g_return_if_fail (container != NULL);
858 g_return_if_fail (GTK_IS_TABLE (container));
859 g_return_if_fail (widget != NULL);
861 table = GTK_TABLE (container);
862 children = table->children;
866 child = children->data;
867 children = children->next;
869 if (child->widget == widget)
871 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
873 gtk_widget_unparent (widget);
875 table->children = g_list_remove (table->children, child);
878 if (was_visible && GTK_WIDGET_VISIBLE (container))
879 gtk_container_queue_resize (container);
886 gtk_table_forall (GtkContainer *container,
887 gboolean include_internals,
888 GtkCallback callback,
889 gpointer callback_data)
892 GtkTableChild *child;
895 g_return_if_fail (container != NULL);
896 g_return_if_fail (GTK_IS_TABLE (container));
897 g_return_if_fail (callback != NULL);
899 table = GTK_TABLE (container);
900 children = table->children;
904 child = children->data;
905 children = children->next;
907 (* callback) (child->widget, callback_data);
912 gtk_table_size_request_init (GtkTable *table)
914 GtkTableChild *child;
918 for (row = 0; row < table->nrows; row++)
919 table->rows[row].requisition = 0;
920 for (col = 0; col < table->ncols; col++)
921 table->cols[col].requisition = 0;
923 children = table->children;
926 child = children->data;
927 children = children->next;
929 if (GTK_WIDGET_VISIBLE (child->widget))
930 gtk_widget_size_request (child->widget, NULL);
935 gtk_table_size_request_pass1 (GtkTable *table)
937 GtkTableChild *child;
942 children = table->children;
945 child = children->data;
946 children = children->next;
948 if (GTK_WIDGET_VISIBLE (child->widget))
950 GtkRequisition child_requisition;
951 gtk_widget_get_child_requisition (child->widget, &child_requisition);
953 /* Child spans a single column.
955 if (child->left_attach == (child->right_attach - 1))
957 width = child_requisition.width + child->xpadding * 2;
958 table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
961 /* Child spans a single row.
963 if (child->top_attach == (child->bottom_attach - 1))
965 height = child_requisition.height + child->ypadding * 2;
966 table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
973 gtk_table_size_request_pass2 (GtkTable *table)
979 if (table->homogeneous)
984 for (col = 0; col < table->ncols; col++)
985 max_width = MAX (max_width, table->cols[col].requisition);
986 for (row = 0; row < table->nrows; row++)
987 max_height = MAX (max_height, table->rows[row].requisition);
989 for (col = 0; col < table->ncols; col++)
990 table->cols[col].requisition = max_width;
991 for (row = 0; row < table->nrows; row++)
992 table->rows[row].requisition = max_height;
997 gtk_table_size_request_pass3 (GtkTable *table)
999 GtkTableChild *child;
1005 children = table->children;
1008 child = children->data;
1009 children = children->next;
1011 if (GTK_WIDGET_VISIBLE (child->widget))
1013 /* Child spans multiple columns.
1015 if (child->left_attach != (child->right_attach - 1))
1017 GtkRequisition child_requisition;
1019 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1021 /* Check and see if there is already enough space
1025 for (col = child->left_attach; col < child->right_attach; col++)
1027 width += table->cols[col].requisition;
1028 if ((col + 1) < child->right_attach)
1029 width += table->cols[col].spacing;
1032 /* If we need to request more space for this child to fill
1033 * its requisition, then divide up the needed space evenly
1034 * amongst the columns it spans.
1036 if (width < child_requisition.width + child->xpadding * 2)
1038 width = child_requisition.width + child->xpadding * 2 - width;
1040 for (col = child->left_attach; col < child->right_attach; col++)
1042 extra = width / (child->right_attach - col);
1043 table->cols[col].requisition += extra;
1049 /* Child spans multiple rows.
1051 if (child->top_attach != (child->bottom_attach - 1))
1053 GtkRequisition child_requisition;
1055 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1057 /* Check and see if there is already enough space
1061 for (row = child->top_attach; row < child->bottom_attach; row++)
1063 height += table->rows[row].requisition;
1064 if ((row + 1) < child->bottom_attach)
1065 height += table->rows[row].spacing;
1068 /* If we need to request more space for this child to fill
1069 * its requisition, then divide up the needed space evenly
1070 * amongst the columns it spans.
1072 if (height < child_requisition.height + child->ypadding * 2)
1074 height = child_requisition.height + child->ypadding * 2 - height;
1076 for (row = child->top_attach; row < child->bottom_attach; row++)
1078 extra = height / (child->bottom_attach - row);
1079 table->rows[row].requisition += extra;
1089 gtk_table_size_allocate_init (GtkTable *table)
1091 GtkTableChild *child;
1097 /* Initialize the rows and cols.
1098 * By default, rows and cols do not expand and do shrink.
1099 * Those values are modified by the children that occupy
1100 * the rows and cols.
1102 for (col = 0; col < table->ncols; col++)
1104 table->cols[col].allocation = table->cols[col].requisition;
1105 table->cols[col].need_expand = FALSE;
1106 table->cols[col].need_shrink = TRUE;
1107 table->cols[col].expand = FALSE;
1108 table->cols[col].shrink = TRUE;
1109 table->cols[col].empty = TRUE;
1111 for (row = 0; row < table->nrows; row++)
1113 table->rows[row].allocation = table->rows[row].requisition;
1114 table->rows[row].need_expand = FALSE;
1115 table->rows[row].need_shrink = TRUE;
1116 table->rows[row].expand = FALSE;
1117 table->rows[row].shrink = TRUE;
1118 table->rows[row].empty = TRUE;
1121 /* Loop over all the children and adjust the row and col values
1122 * based on whether the children want to be allowed to expand
1123 * or shrink. This loop handles children that occupy a single
1126 children = table->children;
1129 child = children->data;
1130 children = children->next;
1132 if (GTK_WIDGET_VISIBLE (child->widget))
1134 if (child->left_attach == (child->right_attach - 1))
1137 table->cols[child->left_attach].expand = TRUE;
1139 if (!child->xshrink)
1140 table->cols[child->left_attach].shrink = FALSE;
1142 table->cols[child->left_attach].empty = FALSE;
1145 if (child->top_attach == (child->bottom_attach - 1))
1148 table->rows[child->top_attach].expand = TRUE;
1150 if (!child->yshrink)
1151 table->rows[child->top_attach].shrink = FALSE;
1153 table->rows[child->top_attach].empty = FALSE;
1158 /* Loop over all the children again and this time handle children
1159 * which span multiple rows or columns.
1161 children = table->children;
1164 child = children->data;
1165 children = children->next;
1167 if (GTK_WIDGET_VISIBLE (child->widget))
1169 if (child->left_attach != (child->right_attach - 1))
1171 for (col = child->left_attach; col < child->right_attach; col++)
1172 table->cols[col].empty = FALSE;
1177 for (col = child->left_attach; col < child->right_attach; col++)
1178 if (table->cols[col].expand)
1185 for (col = child->left_attach; col < child->right_attach; col++)
1186 table->cols[col].need_expand = TRUE;
1189 if (!child->xshrink)
1192 for (col = child->left_attach; col < child->right_attach; col++)
1193 if (!table->cols[col].shrink)
1200 for (col = child->left_attach; col < child->right_attach; col++)
1201 table->cols[col].need_shrink = FALSE;
1205 if (child->top_attach != (child->bottom_attach - 1))
1207 for (row = child->top_attach; row < child->bottom_attach; row++)
1208 table->rows[row].empty = FALSE;
1213 for (row = child->top_attach; row < child->bottom_attach; row++)
1214 if (table->rows[row].expand)
1221 for (row = child->top_attach; row < child->bottom_attach; row++)
1222 table->rows[row].need_expand = TRUE;
1225 if (!child->yshrink)
1228 for (row = child->top_attach; row < child->bottom_attach; row++)
1229 if (!table->rows[row].shrink)
1236 for (row = child->top_attach; row < child->bottom_attach; row++)
1237 table->rows[row].need_shrink = FALSE;
1243 /* Loop over the columns and set the expand and shrink values
1244 * if the column can be expanded or shrunk.
1246 for (col = 0; col < table->ncols; col++)
1248 if (table->cols[col].empty)
1250 table->cols[col].expand = FALSE;
1251 table->cols[col].shrink = FALSE;
1255 if (table->cols[col].need_expand)
1256 table->cols[col].expand = TRUE;
1257 if (!table->cols[col].need_shrink)
1258 table->cols[col].shrink = FALSE;
1262 /* Loop over the rows and set the expand and shrink values
1263 * if the row can be expanded or shrunk.
1265 for (row = 0; row < table->nrows; row++)
1267 if (table->rows[row].empty)
1269 table->rows[row].expand = FALSE;
1270 table->rows[row].shrink = FALSE;
1274 if (table->rows[row].need_expand)
1275 table->rows[row].expand = TRUE;
1276 if (!table->rows[row].need_shrink)
1277 table->rows[row].shrink = FALSE;
1283 gtk_table_size_allocate_pass1 (GtkTable *table)
1293 /* If we were allocated more space than we requested
1294 * then we have to expand any expandable rows and columns
1295 * to fill in the extra space.
1298 real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1299 real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1301 if (table->homogeneous)
1304 for (col = 0; col < table->ncols; col++)
1305 if (table->cols[col].expand)
1315 for (col = 0; col + 1 < table->ncols; col++)
1316 width -= table->cols[col].spacing;
1318 for (col = 0; col < table->ncols; col++)
1320 extra = width / (table->ncols - col);
1321 table->cols[col].allocation = MAX (1, extra);
1332 for (col = 0; col < table->ncols; col++)
1334 width += table->cols[col].requisition;
1335 if (table->cols[col].expand)
1337 if (table->cols[col].shrink)
1340 for (col = 0; col + 1 < table->ncols; col++)
1341 width += table->cols[col].spacing;
1343 /* Check to see if we were allocated more width than we requested.
1345 if ((width < real_width) && (nexpand >= 1))
1347 width = real_width - width;
1349 for (col = 0; col < table->ncols; col++)
1350 if (table->cols[col].expand)
1352 extra = width / nexpand;
1353 table->cols[col].allocation += extra;
1360 /* Check to see if we were allocated less width than we requested,
1361 * then shrink until we fit the size give.
1363 if (width > real_width)
1365 gint total_nshrink = nshrink;
1367 extra = width - real_width;
1368 while (total_nshrink > 0 && extra > 0)
1370 nshrink = total_nshrink;
1371 for (col = 0; col < table->ncols; col++)
1372 if (table->cols[col].shrink)
1374 gint allocation = table->cols[col].allocation;
1376 table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1377 extra -= allocation - table->cols[col].allocation;
1379 if (table->cols[col].allocation < 2)
1382 table->cols[col].shrink = FALSE;
1389 if (table->homogeneous)
1392 for (row = 0; row < table->nrows; row++)
1393 if (table->rows[row].expand)
1401 height = real_height;
1403 for (row = 0; row + 1 < table->nrows; row++)
1404 height -= table->rows[row].spacing;
1407 for (row = 0; row < table->nrows; row++)
1409 extra = height / (table->nrows - row);
1410 table->rows[row].allocation = MAX (1, extra);
1421 for (row = 0; row < table->nrows; row++)
1423 height += table->rows[row].requisition;
1424 if (table->rows[row].expand)
1426 if (table->rows[row].shrink)
1429 for (row = 0; row + 1 < table->nrows; row++)
1430 height += table->rows[row].spacing;
1432 /* Check to see if we were allocated more height than we requested.
1434 if ((height < real_height) && (nexpand >= 1))
1436 height = real_height - height;
1438 for (row = 0; row < table->nrows; row++)
1439 if (table->rows[row].expand)
1441 extra = height / nexpand;
1442 table->rows[row].allocation += extra;
1449 /* Check to see if we were allocated less height than we requested.
1450 * then shrink until we fit the size give.
1452 if (height > real_height)
1454 gint total_nshrink = nshrink;
1456 extra = height - real_height;
1457 while (total_nshrink > 0 && extra > 0)
1459 nshrink = total_nshrink;
1460 for (row = 0; row < table->nrows; row++)
1461 if (table->rows[row].shrink)
1463 gint allocation = table->rows[row].allocation;
1465 table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1466 extra -= allocation - table->rows[row].allocation;
1468 if (table->rows[row].allocation < 2)
1471 table->rows[row].shrink = FALSE;
1480 gtk_table_size_allocate_pass2 (GtkTable *table)
1482 GtkTableChild *child;
1488 GtkAllocation allocation;
1490 children = table->children;
1493 child = children->data;
1494 children = children->next;
1496 if (GTK_WIDGET_VISIBLE (child->widget))
1498 GtkRequisition child_requisition;
1499 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1501 x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1502 y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1506 for (col = 0; col < child->left_attach; col++)
1508 x += table->cols[col].allocation;
1509 x += table->cols[col].spacing;
1512 for (col = child->left_attach; col < child->right_attach; col++)
1514 max_width += table->cols[col].allocation;
1515 if ((col + 1) < child->right_attach)
1516 max_width += table->cols[col].spacing;
1519 for (row = 0; row < child->top_attach; row++)
1521 y += table->rows[row].allocation;
1522 y += table->rows[row].spacing;
1525 for (row = child->top_attach; row < child->bottom_attach; row++)
1527 max_height += table->rows[row].allocation;
1528 if ((row + 1) < child->bottom_attach)
1529 max_height += table->rows[row].spacing;
1534 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1535 allocation.x = x + (max_width - allocation.width) / 2;
1539 allocation.width = child_requisition.width;
1540 allocation.x = x + (max_width - allocation.width) / 2;
1545 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1546 allocation.y = y + (max_height - allocation.height) / 2;
1550 allocation.height = child_requisition.height;
1551 allocation.y = y + (max_height - allocation.height) / 2;
1554 gtk_widget_size_allocate (child->widget, &allocation);