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 (GObject *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 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
133 GtkObjectClass *object_class;
134 GtkWidgetClass *widget_class;
135 GtkContainerClass *container_class;
137 object_class = (GtkObjectClass*) class;
138 widget_class = (GtkWidgetClass*) class;
139 container_class = (GtkContainerClass*) class;
141 parent_class = gtk_type_class (gtk_container_get_type ());
143 gobject_class->finalize = gtk_table_finalize;
145 object_class->get_arg = gtk_table_get_arg;
146 object_class->set_arg = gtk_table_set_arg;
148 widget_class->map = gtk_table_map;
149 widget_class->unmap = gtk_table_unmap;
150 widget_class->draw = gtk_table_draw;
151 widget_class->expose_event = gtk_table_expose;
152 widget_class->size_request = gtk_table_size_request;
153 widget_class->size_allocate = gtk_table_size_allocate;
155 container_class->add = gtk_table_add;
156 container_class->remove = gtk_table_remove;
157 container_class->forall = gtk_table_forall;
158 container_class->child_type = gtk_table_child_type;
159 container_class->set_child_arg = gtk_table_set_child_arg;
160 container_class->get_child_arg = gtk_table_get_child_arg;
162 gtk_object_add_arg_type ("GtkTable::n_rows", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_N_ROWS);
163 gtk_object_add_arg_type ("GtkTable::n_columns", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_N_COLUMNS);
164 gtk_object_add_arg_type ("GtkTable::row_spacing", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_ROW_SPACING);
165 gtk_object_add_arg_type ("GtkTable::column_spacing", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_COLUMN_SPACING);
166 gtk_object_add_arg_type ("GtkTable::homogeneous", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HOMOGENEOUS);
167 gtk_container_add_child_arg_type ("GtkTable::left_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_LEFT_ATTACH);
168 gtk_container_add_child_arg_type ("GtkTable::right_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_RIGHT_ATTACH);
169 gtk_container_add_child_arg_type ("GtkTable::top_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_TOP_ATTACH);
170 gtk_container_add_child_arg_type ("GtkTable::bottom_attach", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_BOTTOM_ATTACH);
171 gtk_container_add_child_arg_type ("GtkTable::x_options", GTK_TYPE_ATTACH_OPTIONS, GTK_ARG_READWRITE, CHILD_ARG_X_OPTIONS);
172 gtk_container_add_child_arg_type ("GtkTable::y_options", GTK_TYPE_ATTACH_OPTIONS, GTK_ARG_READWRITE, CHILD_ARG_Y_OPTIONS);
173 gtk_container_add_child_arg_type ("GtkTable::x_padding", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_X_PADDING);
174 gtk_container_add_child_arg_type ("GtkTable::y_padding", GTK_TYPE_UINT, GTK_ARG_READWRITE, CHILD_ARG_Y_PADDING);
178 gtk_table_child_type (GtkContainer *container)
180 return GTK_TYPE_WIDGET;
184 gtk_table_get_arg (GtkObject *object,
190 table = GTK_TABLE (object);
195 GTK_VALUE_UINT (*arg) = table->nrows;
198 GTK_VALUE_UINT (*arg) = table->ncols;
200 case ARG_ROW_SPACING:
201 GTK_VALUE_UINT (*arg) = table->row_spacing;
203 case ARG_COLUMN_SPACING:
204 GTK_VALUE_UINT (*arg) = table->column_spacing;
206 case ARG_HOMOGENEOUS:
207 GTK_VALUE_BOOL (*arg) = table->homogeneous;
210 arg->type = GTK_TYPE_INVALID;
216 gtk_table_set_arg (GtkObject *object,
222 table = GTK_TABLE (object);
227 gtk_table_resize (table, GTK_VALUE_UINT (*arg), table->ncols);
230 gtk_table_resize (table, table->nrows, GTK_VALUE_UINT (*arg));
232 case ARG_ROW_SPACING:
233 gtk_table_set_row_spacings (table, GTK_VALUE_UINT (*arg));
235 case ARG_COLUMN_SPACING:
236 gtk_table_set_col_spacings (table, GTK_VALUE_UINT (*arg));
238 case ARG_HOMOGENEOUS:
239 gtk_table_set_homogeneous (table, GTK_VALUE_BOOL (*arg));
247 gtk_table_set_child_arg (GtkContainer *container,
253 GtkTableChild *table_child;
256 table = GTK_TABLE (container);
258 for (list = table->children; list; list = list->next)
260 table_child = list->data;
262 if (table_child->widget == child)
270 case CHILD_ARG_LEFT_ATTACH:
271 table_child->left_attach = GTK_VALUE_UINT (*arg);
272 if (table_child->right_attach <= table_child->left_attach)
273 table_child->right_attach = table_child->left_attach + 1;
274 if (table_child->right_attach >= table->ncols)
275 gtk_table_resize (table, table->ncols, table_child->right_attach);
277 case CHILD_ARG_RIGHT_ATTACH:
278 if (GTK_VALUE_UINT (*arg) > 0)
280 table_child->right_attach = GTK_VALUE_UINT (*arg);
281 if (table_child->right_attach <= table_child->left_attach)
282 table_child->left_attach = table_child->right_attach - 1;
283 if (table_child->right_attach >= table->ncols)
284 gtk_table_resize (table, table->ncols, table_child->right_attach);
287 case CHILD_ARG_TOP_ATTACH:
288 table_child->top_attach = GTK_VALUE_UINT (*arg);
289 if (table_child->bottom_attach <= table_child->top_attach)
290 table_child->bottom_attach = table_child->top_attach + 1;
291 if (table_child->bottom_attach >= table->nrows)
292 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
294 case CHILD_ARG_BOTTOM_ATTACH:
295 if (GTK_VALUE_UINT (*arg) > 0)
297 table_child->bottom_attach = GTK_VALUE_UINT (*arg);
298 if (table_child->bottom_attach <= table_child->top_attach)
299 table_child->top_attach = table_child->bottom_attach - 1;
300 if (table_child->bottom_attach >= table->nrows)
301 gtk_table_resize (table, table_child->bottom_attach, table->ncols);
304 case CHILD_ARG_X_OPTIONS:
305 table_child->xexpand = (GTK_VALUE_FLAGS (*arg) & GTK_EXPAND) != 0;
306 table_child->xshrink = (GTK_VALUE_FLAGS (*arg) & GTK_SHRINK) != 0;
307 table_child->xfill = (GTK_VALUE_FLAGS (*arg) & GTK_FILL) != 0;
309 case CHILD_ARG_Y_OPTIONS:
310 table_child->yexpand = (GTK_VALUE_FLAGS (*arg) & GTK_EXPAND) != 0;
311 table_child->yshrink = (GTK_VALUE_FLAGS (*arg) & GTK_SHRINK) != 0;
312 table_child->yfill = (GTK_VALUE_FLAGS (*arg) & GTK_FILL) != 0;
314 case CHILD_ARG_X_PADDING:
315 table_child->xpadding = GTK_VALUE_UINT (*arg);
317 case CHILD_ARG_Y_PADDING:
318 table_child->ypadding = GTK_VALUE_UINT (*arg);
323 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
324 gtk_widget_queue_resize (child);
328 gtk_table_get_child_arg (GtkContainer *container,
334 GtkTableChild *table_child;
337 table = GTK_TABLE (container);
339 for (list = table->children; list; list = list->next)
341 table_child = list->data;
343 if (table_child->widget == child)
351 case CHILD_ARG_LEFT_ATTACH:
352 GTK_VALUE_UINT (*arg) = table_child->left_attach;
354 case CHILD_ARG_RIGHT_ATTACH:
355 GTK_VALUE_UINT (*arg) = table_child->right_attach;
357 case CHILD_ARG_TOP_ATTACH:
358 GTK_VALUE_UINT (*arg) = table_child->top_attach;
360 case CHILD_ARG_BOTTOM_ATTACH:
361 GTK_VALUE_UINT (*arg) = table_child->bottom_attach;
363 case CHILD_ARG_X_OPTIONS:
364 GTK_VALUE_FLAGS (*arg) = (table_child->xexpand * GTK_EXPAND |
365 table_child->xshrink * GTK_SHRINK |
366 table_child->xfill * GTK_FILL);
368 case CHILD_ARG_Y_OPTIONS:
369 GTK_VALUE_FLAGS (*arg) = (table_child->yexpand * GTK_EXPAND |
370 table_child->yshrink * GTK_SHRINK |
371 table_child->yfill * GTK_FILL);
373 case CHILD_ARG_X_PADDING:
374 GTK_VALUE_UINT (*arg) = table_child->xpadding;
376 case CHILD_ARG_Y_PADDING:
377 GTK_VALUE_UINT (*arg) = table_child->ypadding;
380 arg->type = GTK_TYPE_INVALID;
386 gtk_table_init (GtkTable *table)
388 GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
390 table->children = NULL;
395 table->column_spacing = 0;
396 table->row_spacing = 0;
397 table->homogeneous = FALSE;
399 gtk_table_resize (table, 1, 1);
403 gtk_table_new (guint rows,
405 gboolean homogeneous)
414 table = gtk_type_new (gtk_table_get_type ());
416 table->homogeneous = (homogeneous ? TRUE : FALSE);
418 gtk_table_resize (table, rows, columns);
420 return GTK_WIDGET (table);
424 gtk_table_resize (GtkTable *table,
428 g_return_if_fail (table != NULL);
429 g_return_if_fail (GTK_IS_TABLE (table));
431 n_rows = MAX (n_rows, 1);
432 n_cols = MAX (n_cols, 1);
434 if (n_rows != table->nrows ||
435 n_cols != table->ncols)
439 for (list = table->children; list; list = list->next)
441 GtkTableChild *child;
445 n_rows = MAX (n_rows, child->bottom_attach);
446 n_cols = MAX (n_cols, child->right_attach);
449 if (n_rows != table->nrows)
454 table->nrows = n_rows;
455 table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
457 for (; i < table->nrows; i++)
459 table->rows[i].requisition = 0;
460 table->rows[i].allocation = 0;
461 table->rows[i].spacing = table->row_spacing;
462 table->rows[i].need_expand = 0;
463 table->rows[i].need_shrink = 0;
464 table->rows[i].expand = 0;
465 table->rows[i].shrink = 0;
469 if (n_cols != table->ncols)
474 table->ncols = n_cols;
475 table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
477 for (; i < table->ncols; i++)
479 table->cols[i].requisition = 0;
480 table->cols[i].allocation = 0;
481 table->cols[i].spacing = table->column_spacing;
482 table->cols[i].need_expand = 0;
483 table->cols[i].need_shrink = 0;
484 table->cols[i].expand = 0;
485 table->cols[i].shrink = 0;
492 gtk_table_attach (GtkTable *table,
498 GtkAttachOptions xoptions,
499 GtkAttachOptions yoptions,
503 GtkTableChild *table_child;
505 g_return_if_fail (table != NULL);
506 g_return_if_fail (GTK_IS_TABLE (table));
507 g_return_if_fail (child != NULL);
508 g_return_if_fail (GTK_IS_WIDGET (child));
509 g_return_if_fail (child->parent == NULL);
511 /* g_return_if_fail (left_attach >= 0); */
512 g_return_if_fail (left_attach < right_attach);
513 /* g_return_if_fail (top_attach >= 0); */
514 g_return_if_fail (top_attach < bottom_attach);
516 if (right_attach >= table->ncols)
517 gtk_table_resize (table, table->nrows, right_attach);
519 if (bottom_attach >= table->nrows)
520 gtk_table_resize (table, bottom_attach, table->ncols);
522 table_child = g_new (GtkTableChild, 1);
523 table_child->widget = child;
524 table_child->left_attach = left_attach;
525 table_child->right_attach = right_attach;
526 table_child->top_attach = top_attach;
527 table_child->bottom_attach = bottom_attach;
528 table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
529 table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
530 table_child->xfill = (xoptions & GTK_FILL) != 0;
531 table_child->xpadding = xpadding;
532 table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
533 table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
534 table_child->yfill = (yoptions & GTK_FILL) != 0;
535 table_child->ypadding = ypadding;
537 table->children = g_list_prepend (table->children, table_child);
539 gtk_widget_set_parent (child, GTK_WIDGET (table));
541 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (table)))
543 if (GTK_WIDGET_REALIZED (GTK_WIDGET (table)) &&
544 !GTK_WIDGET_REALIZED (child))
545 gtk_widget_realize (child);
547 if (GTK_WIDGET_MAPPED (GTK_WIDGET (table)) &&
548 !GTK_WIDGET_MAPPED (child))
549 gtk_widget_map (child);
552 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
553 gtk_widget_queue_resize (child);
557 gtk_table_attach_defaults (GtkTable *table,
564 gtk_table_attach (table, widget,
565 left_attach, right_attach,
566 top_attach, bottom_attach,
567 GTK_EXPAND | GTK_FILL,
568 GTK_EXPAND | GTK_FILL,
573 gtk_table_set_row_spacing (GtkTable *table,
577 g_return_if_fail (table != NULL);
578 g_return_if_fail (GTK_IS_TABLE (table));
579 g_return_if_fail (row + 1 < table->nrows);
581 if (table->rows[row].spacing != spacing)
583 table->rows[row].spacing = spacing;
585 if (GTK_WIDGET_VISIBLE (table))
586 gtk_widget_queue_resize (GTK_WIDGET (table));
591 gtk_table_set_col_spacing (GtkTable *table,
595 g_return_if_fail (table != NULL);
596 g_return_if_fail (GTK_IS_TABLE (table));
597 g_return_if_fail (column + 1 < table->ncols);
599 if (table->cols[column].spacing != spacing)
601 table->cols[column].spacing = spacing;
603 if (GTK_WIDGET_VISIBLE (table))
604 gtk_widget_queue_resize (GTK_WIDGET (table));
609 gtk_table_set_row_spacings (GtkTable *table,
614 g_return_if_fail (table != NULL);
615 g_return_if_fail (GTK_IS_TABLE (table));
617 table->row_spacing = spacing;
618 for (row = 0; row + 1 < table->nrows; row++)
619 table->rows[row].spacing = spacing;
621 if (GTK_WIDGET_VISIBLE (table))
622 gtk_widget_queue_resize (GTK_WIDGET (table));
626 gtk_table_set_col_spacings (GtkTable *table,
631 g_return_if_fail (table != NULL);
632 g_return_if_fail (GTK_IS_TABLE (table));
634 table->column_spacing = spacing;
635 for (col = 0; col + 1 < table->ncols; col++)
636 table->cols[col].spacing = spacing;
638 if (GTK_WIDGET_VISIBLE (table))
639 gtk_widget_queue_resize (GTK_WIDGET (table));
643 gtk_table_set_homogeneous (GtkTable *table,
644 gboolean homogeneous)
646 g_return_if_fail (table != NULL);
647 g_return_if_fail (GTK_IS_TABLE (table));
649 homogeneous = (homogeneous != 0);
650 if (homogeneous != table->homogeneous)
652 table->homogeneous = homogeneous;
654 if (GTK_WIDGET_VISIBLE (table))
655 gtk_widget_queue_resize (GTK_WIDGET (table));
660 gtk_table_finalize (GObject *object)
664 g_return_if_fail (GTK_IS_TABLE (object));
666 table = GTK_TABLE (object);
668 g_free (table->rows);
669 g_free (table->cols);
671 G_OBJECT_CLASS (parent_class)->finalize (object);
675 gtk_table_map (GtkWidget *widget)
678 GtkTableChild *child;
681 g_return_if_fail (widget != NULL);
682 g_return_if_fail (GTK_IS_TABLE (widget));
684 table = GTK_TABLE (widget);
685 GTK_WIDGET_SET_FLAGS (table, GTK_MAPPED);
687 children = table->children;
690 child = children->data;
691 children = children->next;
693 if (GTK_WIDGET_VISIBLE (child->widget) &&
694 !GTK_WIDGET_MAPPED (child->widget))
695 gtk_widget_map (child->widget);
700 gtk_table_unmap (GtkWidget *widget)
703 GtkTableChild *child;
706 g_return_if_fail (widget != NULL);
707 g_return_if_fail (GTK_IS_TABLE (widget));
709 table = GTK_TABLE (widget);
710 GTK_WIDGET_UNSET_FLAGS (table, GTK_MAPPED);
712 children = table->children;
715 child = children->data;
716 children = children->next;
718 if (GTK_WIDGET_VISIBLE (child->widget) &&
719 GTK_WIDGET_MAPPED (child->widget))
720 gtk_widget_unmap (child->widget);
725 gtk_table_draw (GtkWidget *widget,
729 GtkTableChild *child;
731 GdkRectangle child_area;
733 g_return_if_fail (widget != NULL);
734 g_return_if_fail (GTK_IS_TABLE (widget));
736 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
738 table = GTK_TABLE (widget);
740 children = table->children;
743 child = children->data;
744 children = children->next;
746 if (gtk_widget_intersect (child->widget, area, &child_area))
747 gtk_widget_draw (child->widget, &child_area);
753 gtk_table_expose (GtkWidget *widget,
754 GdkEventExpose *event)
757 GtkTableChild *child;
759 GdkEventExpose child_event;
761 g_return_val_if_fail (widget != NULL, FALSE);
762 g_return_val_if_fail (GTK_IS_TABLE (widget), FALSE);
764 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
766 table = GTK_TABLE (widget);
768 child_event = *event;
770 children = table->children;
773 child = children->data;
774 children = children->next;
776 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
777 gtk_widget_intersect (child->widget, &event->area, &child_event.area))
778 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
786 gtk_table_size_request (GtkWidget *widget,
787 GtkRequisition *requisition)
792 g_return_if_fail (widget != NULL);
793 g_return_if_fail (GTK_IS_TABLE (widget));
794 g_return_if_fail (requisition != NULL);
796 table = GTK_TABLE (widget);
798 requisition->width = 0;
799 requisition->height = 0;
801 gtk_table_size_request_init (table);
802 gtk_table_size_request_pass1 (table);
803 gtk_table_size_request_pass2 (table);
804 gtk_table_size_request_pass3 (table);
805 gtk_table_size_request_pass2 (table);
807 for (col = 0; col < table->ncols; col++)
808 requisition->width += table->cols[col].requisition;
809 for (col = 0; col + 1 < table->ncols; col++)
810 requisition->width += table->cols[col].spacing;
812 for (row = 0; row < table->nrows; row++)
813 requisition->height += table->rows[row].requisition;
814 for (row = 0; row + 1 < table->nrows; row++)
815 requisition->height += table->rows[row].spacing;
817 requisition->width += GTK_CONTAINER (table)->border_width * 2;
818 requisition->height += GTK_CONTAINER (table)->border_width * 2;
822 gtk_table_size_allocate (GtkWidget *widget,
823 GtkAllocation *allocation)
827 g_return_if_fail (widget != NULL);
828 g_return_if_fail (GTK_IS_TABLE (widget));
829 g_return_if_fail (allocation != NULL);
831 widget->allocation = *allocation;
832 table = GTK_TABLE (widget);
834 gtk_table_size_allocate_init (table);
835 gtk_table_size_allocate_pass1 (table);
836 gtk_table_size_allocate_pass2 (table);
840 gtk_table_add (GtkContainer *container,
843 g_return_if_fail (container != NULL);
844 g_return_if_fail (GTK_IS_TABLE (container));
845 g_return_if_fail (widget != NULL);
847 gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
851 gtk_table_remove (GtkContainer *container,
855 GtkTableChild *child;
858 g_return_if_fail (container != NULL);
859 g_return_if_fail (GTK_IS_TABLE (container));
860 g_return_if_fail (widget != NULL);
862 table = GTK_TABLE (container);
863 children = table->children;
867 child = children->data;
868 children = children->next;
870 if (child->widget == widget)
872 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
874 gtk_widget_unparent (widget);
876 table->children = g_list_remove (table->children, child);
879 if (was_visible && GTK_WIDGET_VISIBLE (container))
880 gtk_container_queue_resize (container);
887 gtk_table_forall (GtkContainer *container,
888 gboolean include_internals,
889 GtkCallback callback,
890 gpointer callback_data)
893 GtkTableChild *child;
896 g_return_if_fail (container != NULL);
897 g_return_if_fail (GTK_IS_TABLE (container));
898 g_return_if_fail (callback != NULL);
900 table = GTK_TABLE (container);
901 children = table->children;
905 child = children->data;
906 children = children->next;
908 (* callback) (child->widget, callback_data);
913 gtk_table_size_request_init (GtkTable *table)
915 GtkTableChild *child;
919 for (row = 0; row < table->nrows; row++)
920 table->rows[row].requisition = 0;
921 for (col = 0; col < table->ncols; col++)
922 table->cols[col].requisition = 0;
924 children = table->children;
927 child = children->data;
928 children = children->next;
930 if (GTK_WIDGET_VISIBLE (child->widget))
931 gtk_widget_size_request (child->widget, NULL);
936 gtk_table_size_request_pass1 (GtkTable *table)
938 GtkTableChild *child;
943 children = table->children;
946 child = children->data;
947 children = children->next;
949 if (GTK_WIDGET_VISIBLE (child->widget))
951 GtkRequisition child_requisition;
952 gtk_widget_get_child_requisition (child->widget, &child_requisition);
954 /* Child spans a single column.
956 if (child->left_attach == (child->right_attach - 1))
958 width = child_requisition.width + child->xpadding * 2;
959 table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
962 /* Child spans a single row.
964 if (child->top_attach == (child->bottom_attach - 1))
966 height = child_requisition.height + child->ypadding * 2;
967 table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
974 gtk_table_size_request_pass2 (GtkTable *table)
980 if (table->homogeneous)
985 for (col = 0; col < table->ncols; col++)
986 max_width = MAX (max_width, table->cols[col].requisition);
987 for (row = 0; row < table->nrows; row++)
988 max_height = MAX (max_height, table->rows[row].requisition);
990 for (col = 0; col < table->ncols; col++)
991 table->cols[col].requisition = max_width;
992 for (row = 0; row < table->nrows; row++)
993 table->rows[row].requisition = max_height;
998 gtk_table_size_request_pass3 (GtkTable *table)
1000 GtkTableChild *child;
1006 children = table->children;
1009 child = children->data;
1010 children = children->next;
1012 if (GTK_WIDGET_VISIBLE (child->widget))
1014 /* Child spans multiple columns.
1016 if (child->left_attach != (child->right_attach - 1))
1018 GtkRequisition child_requisition;
1020 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1022 /* Check and see if there is already enough space
1026 for (col = child->left_attach; col < child->right_attach; col++)
1028 width += table->cols[col].requisition;
1029 if ((col + 1) < child->right_attach)
1030 width += table->cols[col].spacing;
1033 /* If we need to request more space for this child to fill
1034 * its requisition, then divide up the needed space evenly
1035 * amongst the columns it spans.
1037 if (width < child_requisition.width + child->xpadding * 2)
1039 width = child_requisition.width + child->xpadding * 2 - width;
1041 for (col = child->left_attach; col < child->right_attach; col++)
1043 extra = width / (child->right_attach - col);
1044 table->cols[col].requisition += extra;
1050 /* Child spans multiple rows.
1052 if (child->top_attach != (child->bottom_attach - 1))
1054 GtkRequisition child_requisition;
1056 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1058 /* Check and see if there is already enough space
1062 for (row = child->top_attach; row < child->bottom_attach; row++)
1064 height += table->rows[row].requisition;
1065 if ((row + 1) < child->bottom_attach)
1066 height += table->rows[row].spacing;
1069 /* If we need to request more space for this child to fill
1070 * its requisition, then divide up the needed space evenly
1071 * amongst the columns it spans.
1073 if (height < child_requisition.height + child->ypadding * 2)
1075 height = child_requisition.height + child->ypadding * 2 - height;
1077 for (row = child->top_attach; row < child->bottom_attach; row++)
1079 extra = height / (child->bottom_attach - row);
1080 table->rows[row].requisition += extra;
1090 gtk_table_size_allocate_init (GtkTable *table)
1092 GtkTableChild *child;
1098 /* Initialize the rows and cols.
1099 * By default, rows and cols do not expand and do shrink.
1100 * Those values are modified by the children that occupy
1101 * the rows and cols.
1103 for (col = 0; col < table->ncols; col++)
1105 table->cols[col].allocation = table->cols[col].requisition;
1106 table->cols[col].need_expand = FALSE;
1107 table->cols[col].need_shrink = TRUE;
1108 table->cols[col].expand = FALSE;
1109 table->cols[col].shrink = TRUE;
1110 table->cols[col].empty = TRUE;
1112 for (row = 0; row < table->nrows; row++)
1114 table->rows[row].allocation = table->rows[row].requisition;
1115 table->rows[row].need_expand = FALSE;
1116 table->rows[row].need_shrink = TRUE;
1117 table->rows[row].expand = FALSE;
1118 table->rows[row].shrink = TRUE;
1119 table->rows[row].empty = TRUE;
1122 /* Loop over all the children and adjust the row and col values
1123 * based on whether the children want to be allowed to expand
1124 * or shrink. This loop handles children that occupy a single
1127 children = table->children;
1130 child = children->data;
1131 children = children->next;
1133 if (GTK_WIDGET_VISIBLE (child->widget))
1135 if (child->left_attach == (child->right_attach - 1))
1138 table->cols[child->left_attach].expand = TRUE;
1140 if (!child->xshrink)
1141 table->cols[child->left_attach].shrink = FALSE;
1143 table->cols[child->left_attach].empty = FALSE;
1146 if (child->top_attach == (child->bottom_attach - 1))
1149 table->rows[child->top_attach].expand = TRUE;
1151 if (!child->yshrink)
1152 table->rows[child->top_attach].shrink = FALSE;
1154 table->rows[child->top_attach].empty = FALSE;
1159 /* Loop over all the children again and this time handle children
1160 * which span multiple rows or columns.
1162 children = table->children;
1165 child = children->data;
1166 children = children->next;
1168 if (GTK_WIDGET_VISIBLE (child->widget))
1170 if (child->left_attach != (child->right_attach - 1))
1172 for (col = child->left_attach; col < child->right_attach; col++)
1173 table->cols[col].empty = FALSE;
1178 for (col = child->left_attach; col < child->right_attach; col++)
1179 if (table->cols[col].expand)
1186 for (col = child->left_attach; col < child->right_attach; col++)
1187 table->cols[col].need_expand = TRUE;
1190 if (!child->xshrink)
1193 for (col = child->left_attach; col < child->right_attach; col++)
1194 if (!table->cols[col].shrink)
1201 for (col = child->left_attach; col < child->right_attach; col++)
1202 table->cols[col].need_shrink = FALSE;
1206 if (child->top_attach != (child->bottom_attach - 1))
1208 for (row = child->top_attach; row < child->bottom_attach; row++)
1209 table->rows[row].empty = FALSE;
1214 for (row = child->top_attach; row < child->bottom_attach; row++)
1215 if (table->rows[row].expand)
1222 for (row = child->top_attach; row < child->bottom_attach; row++)
1223 table->rows[row].need_expand = TRUE;
1226 if (!child->yshrink)
1229 for (row = child->top_attach; row < child->bottom_attach; row++)
1230 if (!table->rows[row].shrink)
1237 for (row = child->top_attach; row < child->bottom_attach; row++)
1238 table->rows[row].need_shrink = FALSE;
1244 /* Loop over the columns and set the expand and shrink values
1245 * if the column can be expanded or shrunk.
1247 for (col = 0; col < table->ncols; col++)
1249 if (table->cols[col].empty)
1251 table->cols[col].expand = FALSE;
1252 table->cols[col].shrink = FALSE;
1256 if (table->cols[col].need_expand)
1257 table->cols[col].expand = TRUE;
1258 if (!table->cols[col].need_shrink)
1259 table->cols[col].shrink = FALSE;
1263 /* Loop over the rows and set the expand and shrink values
1264 * if the row can be expanded or shrunk.
1266 for (row = 0; row < table->nrows; row++)
1268 if (table->rows[row].empty)
1270 table->rows[row].expand = FALSE;
1271 table->rows[row].shrink = FALSE;
1275 if (table->rows[row].need_expand)
1276 table->rows[row].expand = TRUE;
1277 if (!table->rows[row].need_shrink)
1278 table->rows[row].shrink = FALSE;
1284 gtk_table_size_allocate_pass1 (GtkTable *table)
1294 /* If we were allocated more space than we requested
1295 * then we have to expand any expandable rows and columns
1296 * to fill in the extra space.
1299 real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1300 real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1302 if (table->homogeneous)
1305 for (col = 0; col < table->ncols; col++)
1306 if (table->cols[col].expand)
1316 for (col = 0; col + 1 < table->ncols; col++)
1317 width -= table->cols[col].spacing;
1319 for (col = 0; col < table->ncols; col++)
1321 extra = width / (table->ncols - col);
1322 table->cols[col].allocation = MAX (1, extra);
1333 for (col = 0; col < table->ncols; col++)
1335 width += table->cols[col].requisition;
1336 if (table->cols[col].expand)
1338 if (table->cols[col].shrink)
1341 for (col = 0; col + 1 < table->ncols; col++)
1342 width += table->cols[col].spacing;
1344 /* Check to see if we were allocated more width than we requested.
1346 if ((width < real_width) && (nexpand >= 1))
1348 width = real_width - width;
1350 for (col = 0; col < table->ncols; col++)
1351 if (table->cols[col].expand)
1353 extra = width / nexpand;
1354 table->cols[col].allocation += extra;
1361 /* Check to see if we were allocated less width than we requested,
1362 * then shrink until we fit the size give.
1364 if (width > real_width)
1366 gint total_nshrink = nshrink;
1368 extra = width - real_width;
1369 while (total_nshrink > 0 && extra > 0)
1371 nshrink = total_nshrink;
1372 for (col = 0; col < table->ncols; col++)
1373 if (table->cols[col].shrink)
1375 gint allocation = table->cols[col].allocation;
1377 table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1378 extra -= allocation - table->cols[col].allocation;
1380 if (table->cols[col].allocation < 2)
1383 table->cols[col].shrink = FALSE;
1390 if (table->homogeneous)
1393 for (row = 0; row < table->nrows; row++)
1394 if (table->rows[row].expand)
1402 height = real_height;
1404 for (row = 0; row + 1 < table->nrows; row++)
1405 height -= table->rows[row].spacing;
1408 for (row = 0; row < table->nrows; row++)
1410 extra = height / (table->nrows - row);
1411 table->rows[row].allocation = MAX (1, extra);
1422 for (row = 0; row < table->nrows; row++)
1424 height += table->rows[row].requisition;
1425 if (table->rows[row].expand)
1427 if (table->rows[row].shrink)
1430 for (row = 0; row + 1 < table->nrows; row++)
1431 height += table->rows[row].spacing;
1433 /* Check to see if we were allocated more height than we requested.
1435 if ((height < real_height) && (nexpand >= 1))
1437 height = real_height - height;
1439 for (row = 0; row < table->nrows; row++)
1440 if (table->rows[row].expand)
1442 extra = height / nexpand;
1443 table->rows[row].allocation += extra;
1450 /* Check to see if we were allocated less height than we requested.
1451 * then shrink until we fit the size give.
1453 if (height > real_height)
1455 gint total_nshrink = nshrink;
1457 extra = height - real_height;
1458 while (total_nshrink > 0 && extra > 0)
1460 nshrink = total_nshrink;
1461 for (row = 0; row < table->nrows; row++)
1462 if (table->rows[row].shrink)
1464 gint allocation = table->rows[row].allocation;
1466 table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1467 extra -= allocation - table->rows[row].allocation;
1469 if (table->rows[row].allocation < 2)
1472 table->rows[row].shrink = FALSE;
1481 gtk_table_size_allocate_pass2 (GtkTable *table)
1483 GtkTableChild *child;
1489 GtkAllocation allocation;
1490 GtkWidget *widget = GTK_WIDGET (table);
1492 children = table->children;
1495 child = children->data;
1496 children = children->next;
1498 if (GTK_WIDGET_VISIBLE (child->widget))
1500 GtkRequisition child_requisition;
1501 gtk_widget_get_child_requisition (child->widget, &child_requisition);
1503 x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1504 y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1508 for (col = 0; col < child->left_attach; col++)
1510 x += table->cols[col].allocation;
1511 x += table->cols[col].spacing;
1514 for (col = child->left_attach; col < child->right_attach; col++)
1516 max_width += table->cols[col].allocation;
1517 if ((col + 1) < child->right_attach)
1518 max_width += table->cols[col].spacing;
1521 for (row = 0; row < child->top_attach; row++)
1523 y += table->rows[row].allocation;
1524 y += table->rows[row].spacing;
1527 for (row = child->top_attach; row < child->bottom_attach; row++)
1529 max_height += table->rows[row].allocation;
1530 if ((row + 1) < child->bottom_attach)
1531 max_height += table->rows[row].spacing;
1536 allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1537 allocation.x = x + (max_width - allocation.width) / 2;
1541 allocation.width = child_requisition.width;
1542 allocation.x = x + (max_width - allocation.width) / 2;
1547 allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1548 allocation.y = y + (max_height - allocation.height) / 2;
1552 allocation.height = child_requisition.height;
1553 allocation.y = y + (max_height - allocation.height) / 2;
1556 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1557 allocation.x = widget->allocation.x + widget->allocation.width
1558 - (allocation.x - widget->allocation.x) - allocation.width;
1560 gtk_widget_size_allocate (child->widget, &allocation);