]> Pileus Git - ~andy/gtk/blob - gtk/gtktable.c
Patch from Matthias Clasen to remove remove all instances of
[~andy/gtk] / gtk / gtktable.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "gtktable.h"
28 #include "gtkintl.h"
29
30 enum
31 {
32   PROP_0,
33   PROP_N_ROWS,
34   PROP_N_COLUMNS,
35   PROP_COLUMN_SPACING,
36   PROP_ROW_SPACING,
37   PROP_HOMOGENEOUS
38 };
39
40 enum
41 {
42   CHILD_PROP_0,
43   CHILD_PROP_LEFT_ATTACH,
44   CHILD_PROP_RIGHT_ATTACH,
45   CHILD_PROP_TOP_ATTACH,
46   CHILD_PROP_BOTTOM_ATTACH,
47   CHILD_PROP_X_OPTIONS,
48   CHILD_PROP_Y_OPTIONS,
49   CHILD_PROP_X_PADDING,
50   CHILD_PROP_Y_PADDING
51 };
52   
53
54 static void gtk_table_class_init    (GtkTableClass  *klass);
55 static void gtk_table_init          (GtkTable       *table);
56 static void gtk_table_finalize      (GObject        *object);
57 static void gtk_table_map           (GtkWidget      *widget);
58 static void gtk_table_unmap         (GtkWidget      *widget);
59 static void gtk_table_size_request  (GtkWidget      *widget,
60                                      GtkRequisition *requisition);
61 static void gtk_table_size_allocate (GtkWidget      *widget,
62                                      GtkAllocation  *allocation);
63 static void gtk_table_add           (GtkContainer   *container,
64                                      GtkWidget      *widget);
65 static void gtk_table_remove        (GtkContainer   *container,
66                                      GtkWidget      *widget);
67 static void gtk_table_forall        (GtkContainer   *container,
68                                      gboolean        include_internals,
69                                      GtkCallback     callback,
70                                      gpointer        callback_data);
71 static void gtk_table_get_property  (GObject         *object,
72                                      guint            prop_id,
73                                      GValue          *value,
74                                      GParamSpec      *pspec);
75 static void gtk_table_set_property  (GObject         *object,
76                                      guint            prop_id,
77                                      const GValue    *value,
78                                      GParamSpec      *pspec);
79 static void gtk_table_set_child_property (GtkContainer    *container,
80                                           GtkWidget       *child,
81                                           guint            property_id,
82                                           const GValue    *value,
83                                           GParamSpec      *pspec);
84 static void gtk_table_get_child_property (GtkContainer    *container,
85                                           GtkWidget       *child,
86                                           guint            property_id,
87                                           GValue          *value,
88                                           GParamSpec      *pspec);
89 static GtkType gtk_table_child_type (GtkContainer   *container);
90
91
92 static void gtk_table_size_request_init  (GtkTable *table);
93 static void gtk_table_size_request_pass1 (GtkTable *table);
94 static void gtk_table_size_request_pass2 (GtkTable *table);
95 static void gtk_table_size_request_pass3 (GtkTable *table);
96
97 static void gtk_table_size_allocate_init  (GtkTable *table);
98 static void gtk_table_size_allocate_pass1 (GtkTable *table);
99 static void gtk_table_size_allocate_pass2 (GtkTable *table);
100
101
102 static GtkContainerClass *parent_class = NULL;
103
104
105 GtkType
106 gtk_table_get_type (void)
107 {
108   static GtkType table_type = 0;
109   
110   if (!table_type)
111     {
112       static const GtkTypeInfo table_info =
113       {
114         "GtkTable",
115         sizeof (GtkTable),
116         sizeof (GtkTableClass),
117         (GtkClassInitFunc) gtk_table_class_init,
118         (GtkObjectInitFunc) gtk_table_init,
119         /* reserved_1 */ NULL,
120         /* reserved_2 */ NULL,
121         (GtkClassInitFunc) NULL,
122       };
123       
124       table_type = gtk_type_unique (gtk_container_get_type (), &table_info);
125     }
126   
127   return table_type;
128 }
129
130 static void
131 gtk_table_class_init (GtkTableClass *class)
132 {
133   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
134   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
135   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
136   
137   parent_class = g_type_class_peek_parent (class);
138
139   gobject_class->finalize = gtk_table_finalize;
140
141   gobject_class->get_property = gtk_table_get_property;
142   gobject_class->set_property = gtk_table_set_property;
143   
144   widget_class->map = gtk_table_map;
145   widget_class->unmap = gtk_table_unmap;
146   widget_class->size_request = gtk_table_size_request;
147   widget_class->size_allocate = gtk_table_size_allocate;
148   
149   container_class->add = gtk_table_add;
150   container_class->remove = gtk_table_remove;
151   container_class->forall = gtk_table_forall;
152   container_class->child_type = gtk_table_child_type;
153   container_class->set_child_property = gtk_table_set_child_property;
154   container_class->get_child_property = gtk_table_get_child_property;
155   
156
157   g_object_class_install_property (gobject_class,
158                                    PROP_N_ROWS,
159                                    g_param_spec_uint ("n_rows",
160                                                      _("Rows"),
161                                                      _("The number of rows in the table"),
162                                                      0,
163                                                      G_MAXUINT,
164                                                      0,
165                                                      G_PARAM_READWRITE));
166   g_object_class_install_property (gobject_class,
167                                    PROP_N_COLUMNS,
168                                    g_param_spec_uint ("n_columns",
169                                                      _("Columns"),
170                                                      _("The number of columns in the table"),
171                                                      0,
172                                                      G_MAXUINT,
173                                                      0,
174                                                      G_PARAM_READWRITE));
175   g_object_class_install_property (gobject_class,
176                                    PROP_ROW_SPACING,
177                                    g_param_spec_uint ("row_spacing",
178                                                      _("Row spacing"),
179                                                      _("The amount of space between two consecutive rows"),
180                                                      0,
181                                                      G_MAXUINT,
182                                                      0,
183                                                      G_PARAM_READWRITE));
184   g_object_class_install_property (gobject_class,
185                                    PROP_COLUMN_SPACING,
186                                    g_param_spec_uint ("column_spacing",
187                                                      _("Column spacing"),
188                                                      _("The amount of space between two consecutive columns"),
189                                                      0,
190                                                      G_MAXUINT,
191                                                      0,
192                                                      G_PARAM_READWRITE));
193   g_object_class_install_property (gobject_class,
194                                    PROP_HOMOGENEOUS,
195                                    g_param_spec_boolean ("homogeneous",
196                                                          _("Homogenous"),
197                                                          _("If TRUE this means the table cells are all the same width/height"),
198                                                          FALSE,
199                                                          G_PARAM_READWRITE));
200
201   gtk_container_class_install_child_property (container_class,
202                                               CHILD_PROP_LEFT_ATTACH,
203                                               g_param_spec_uint ("left_attach", NULL, NULL,
204                                                                  0, 65535, 0,
205                                                                  G_PARAM_READWRITE));
206   gtk_container_class_install_child_property (container_class,
207                                               CHILD_PROP_RIGHT_ATTACH,
208                                               g_param_spec_uint ("right_attach", NULL, NULL,
209                                                                  1, 65535, 1,
210                                                                  G_PARAM_READWRITE));
211   gtk_container_class_install_child_property (container_class,
212                                               CHILD_PROP_TOP_ATTACH,
213                                               g_param_spec_uint ("top_attach", NULL, NULL,
214                                                                  0, 65535, 0,
215                                                                  G_PARAM_READWRITE));
216   gtk_container_class_install_child_property (container_class,
217                                               CHILD_PROP_BOTTOM_ATTACH,
218                                               g_param_spec_uint ("bottom_attach", NULL, NULL,
219                                                                  1, 65535, 1,
220                                                                  G_PARAM_READWRITE));
221   gtk_container_class_install_child_property (container_class,
222                                               CHILD_PROP_X_OPTIONS,
223                                               g_param_spec_flags ("x_options", NULL, NULL,
224                                                                   GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
225                                                                   G_PARAM_READWRITE));
226   gtk_container_class_install_child_property (container_class,
227                                               CHILD_PROP_Y_OPTIONS,
228                                               g_param_spec_flags ("y_options", NULL, NULL,
229                                                                   GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
230                                                                   G_PARAM_READWRITE));
231   gtk_container_class_install_child_property (container_class,
232                                               CHILD_PROP_X_PADDING,
233                                               g_param_spec_uint ("x_padding", NULL, NULL,
234                                                                  0, 65535, 0,
235                                                                  G_PARAM_READWRITE));
236   gtk_container_class_install_child_property (container_class,
237                                               CHILD_PROP_Y_PADDING,
238                                               g_param_spec_uint ("y_padding", NULL, NULL,
239                                                                  0, 65535, 0,
240                                                                  G_PARAM_READWRITE));
241 }
242
243 static GtkType
244 gtk_table_child_type (GtkContainer   *container)
245 {
246   return GTK_TYPE_WIDGET;
247 }
248
249 static void
250 gtk_table_get_property (GObject      *object,
251                         guint         prop_id,
252                         GValue       *value,
253                         GParamSpec   *pspec)
254 {
255   GtkTable *table;
256
257   table = GTK_TABLE (object);
258
259   switch (prop_id)
260     {
261     case PROP_N_ROWS:
262       g_value_set_uint (value, table->nrows);
263       break;
264     case PROP_N_COLUMNS:
265       g_value_set_uint (value, table->ncols);
266       break;
267     case PROP_ROW_SPACING:
268       g_value_set_uint (value, table->row_spacing);
269       break;
270     case PROP_COLUMN_SPACING:
271       g_value_set_uint (value, table->column_spacing);
272       break;
273     case PROP_HOMOGENEOUS:
274       g_value_set_boolean (value, table->homogeneous);
275       break;
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279     }
280 }
281
282 static void
283 gtk_table_set_property (GObject      *object,
284                         guint         prop_id,
285                         const GValue *value,
286                         GParamSpec   *pspec)
287 {
288   GtkTable *table;
289
290   table = GTK_TABLE (object);
291
292   switch (prop_id)
293     {
294     case PROP_N_ROWS:
295       gtk_table_resize (table, g_value_get_uint (value), table->ncols);
296       break;
297     case PROP_N_COLUMNS:
298       gtk_table_resize (table, table->nrows, g_value_get_uint (value));
299       break;
300     case PROP_ROW_SPACING:
301       gtk_table_set_row_spacings (table, g_value_get_uint (value));
302       break;
303     case PROP_COLUMN_SPACING:
304       gtk_table_set_col_spacings (table, g_value_get_uint (value));
305       break;
306     case PROP_HOMOGENEOUS:
307       gtk_table_set_homogeneous (table, g_value_get_boolean (value));
308       break;
309     default:
310       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311       break;
312     }
313 }
314
315 static void
316 gtk_table_set_child_property (GtkContainer    *container,
317                               GtkWidget       *child,
318                               guint            property_id,
319                               const GValue    *value,
320                               GParamSpec      *pspec)
321 {
322   GtkTable *table = GTK_TABLE (container);
323   GtkTableChild *table_child;
324   GList *list;
325
326   table_child = NULL;
327   for (list = table->children; list; list = list->next)
328     {
329       table_child = list->data;
330
331       if (table_child->widget == child)
332         break;
333     }
334   if (!list)
335     {
336       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
337       return;
338     }
339
340   switch (property_id)
341     {
342     case CHILD_PROP_LEFT_ATTACH:
343       table_child->left_attach = g_value_get_uint (value);
344       if (table_child->right_attach <= table_child->left_attach)
345         table_child->right_attach = table_child->left_attach + 1;
346       if (table_child->right_attach >= table->ncols)
347         gtk_table_resize (table, table->ncols, table_child->right_attach);
348       break;
349     case CHILD_PROP_RIGHT_ATTACH:
350       table_child->right_attach = g_value_get_uint (value);
351       if (table_child->right_attach <= table_child->left_attach)
352         table_child->left_attach = table_child->right_attach - 1;
353       if (table_child->right_attach >= table->ncols)
354         gtk_table_resize (table, table->ncols, table_child->right_attach);
355       break;
356     case CHILD_PROP_TOP_ATTACH:
357       table_child->top_attach = g_value_get_uint (value);
358       if (table_child->bottom_attach <= table_child->top_attach)
359         table_child->bottom_attach = table_child->top_attach + 1;
360       if (table_child->bottom_attach >= table->nrows)
361         gtk_table_resize (table, table_child->bottom_attach, table->ncols);
362       break;
363     case CHILD_PROP_BOTTOM_ATTACH:
364       table_child->bottom_attach = g_value_get_uint (value);
365       if (table_child->bottom_attach <= table_child->top_attach)
366         table_child->top_attach = table_child->bottom_attach - 1;
367       if (table_child->bottom_attach >= table->nrows)
368         gtk_table_resize (table, table_child->bottom_attach, table->ncols);
369       break;
370     case CHILD_PROP_X_OPTIONS:
371       table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
372       table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
373       table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
374       break;
375     case CHILD_PROP_Y_OPTIONS:
376       table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
377       table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
378       table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
379       break;
380     case CHILD_PROP_X_PADDING:
381       table_child->xpadding = g_value_get_uint (value);
382       break;
383     case CHILD_PROP_Y_PADDING:
384       table_child->ypadding = g_value_get_uint (value);
385       break;
386     default:
387       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
388       break;
389     }
390   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
391     gtk_widget_queue_resize (child);
392 }
393
394 static void
395 gtk_table_get_child_property (GtkContainer    *container,
396                               GtkWidget       *child,
397                               guint            property_id,
398                               GValue          *value,
399                               GParamSpec      *pspec)
400 {
401   GtkTable *table = GTK_TABLE (container);
402   GtkTableChild *table_child;
403   GList *list;
404
405   table_child = NULL;
406   for (list = table->children; list; list = list->next)
407     {
408       table_child = list->data;
409
410       if (table_child->widget == child)
411         break;
412     }
413   if (!list)
414     {
415       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
416       return;
417     }
418
419   switch (property_id)
420     {
421     case CHILD_PROP_LEFT_ATTACH:
422       g_value_set_uint (value, table_child->left_attach);
423       break;
424     case CHILD_PROP_RIGHT_ATTACH:
425       g_value_set_uint (value, table_child->right_attach);
426       break;
427     case CHILD_PROP_TOP_ATTACH:
428       g_value_set_uint (value, table_child->top_attach);
429       break;
430     case CHILD_PROP_BOTTOM_ATTACH:
431       g_value_set_uint (value, table_child->bottom_attach);
432       break;
433     case CHILD_PROP_X_OPTIONS:
434       g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
435                                  table_child->xshrink * GTK_SHRINK |
436                                  table_child->xfill * GTK_FILL));
437       break;
438     case CHILD_PROP_Y_OPTIONS:
439       g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
440                                  table_child->yshrink * GTK_SHRINK |
441                                  table_child->yfill * GTK_FILL));
442       break;
443     case CHILD_PROP_X_PADDING:
444       g_value_set_uint (value, table_child->xpadding);
445       break;
446     case CHILD_PROP_Y_PADDING:
447       g_value_set_uint (value, table_child->ypadding);
448       break;
449     default:
450       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
451       break;
452     }
453 }
454
455 static void
456 gtk_table_init (GtkTable *table)
457 {
458   GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
459   
460   table->children = NULL;
461   table->rows = NULL;
462   table->cols = NULL;
463   table->nrows = 0;
464   table->ncols = 0;
465   table->column_spacing = 0;
466   table->row_spacing = 0;
467   table->homogeneous = FALSE;
468
469   gtk_table_resize (table, 1, 1);
470 }
471
472 GtkWidget*
473 gtk_table_new (guint    rows,
474                guint    columns,
475                gboolean homogeneous)
476 {
477   GtkTable *table;
478
479   if (rows == 0)
480           rows = 1;
481   if (columns == 0)
482           columns = 1;
483   
484   table = gtk_type_new (gtk_table_get_type ());
485   
486   table->homogeneous = (homogeneous ? TRUE : FALSE);
487
488   gtk_table_resize (table, rows, columns);
489   
490   return GTK_WIDGET (table);
491 }
492
493 void
494 gtk_table_resize (GtkTable *table,
495                   guint     n_rows,
496                   guint     n_cols)
497 {
498   g_return_if_fail (GTK_IS_TABLE (table));
499   g_return_if_fail (n_rows > 0 && n_rows < 65536);
500   g_return_if_fail (n_rows > 0 && n_cols < 65536);
501
502   n_rows = MAX (n_rows, 1);
503   n_cols = MAX (n_cols, 1);
504
505   if (n_rows != table->nrows ||
506       n_cols != table->ncols)
507     {
508       GList *list;
509       
510       for (list = table->children; list; list = list->next)
511         {
512           GtkTableChild *child;
513           
514           child = list->data;
515           
516           n_rows = MAX (n_rows, child->bottom_attach);
517           n_cols = MAX (n_cols, child->right_attach);
518         }
519       
520       if (n_rows != table->nrows)
521         {
522           guint i;
523
524           i = table->nrows;
525           table->nrows = n_rows;
526           table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
527           
528           for (; i < table->nrows; i++)
529             {
530               table->rows[i].requisition = 0;
531               table->rows[i].allocation = 0;
532               table->rows[i].spacing = table->row_spacing;
533               table->rows[i].need_expand = 0;
534               table->rows[i].need_shrink = 0;
535               table->rows[i].expand = 0;
536               table->rows[i].shrink = 0;
537             }
538
539           g_object_notify (G_OBJECT (table), "n_rows");
540         }
541
542       if (n_cols != table->ncols)
543         {
544           guint i;
545
546           i = table->ncols;
547           table->ncols = n_cols;
548           table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
549           
550           for (; i < table->ncols; i++)
551             {
552               table->cols[i].requisition = 0;
553               table->cols[i].allocation = 0;
554               table->cols[i].spacing = table->column_spacing;
555               table->cols[i].need_expand = 0;
556               table->cols[i].need_shrink = 0;
557               table->cols[i].expand = 0;
558               table->cols[i].shrink = 0;
559             }
560
561           g_object_notify (G_OBJECT (table), "n_columns");
562         }
563     }
564 }
565
566 void
567 gtk_table_attach (GtkTable        *table,
568                   GtkWidget       *child,
569                   guint            left_attach,
570                   guint            right_attach,
571                   guint            top_attach,
572                   guint            bottom_attach,
573                   GtkAttachOptions xoptions,
574                   GtkAttachOptions yoptions,
575                   guint            xpadding,
576                   guint            ypadding)
577 {
578   GtkTableChild *table_child;
579   
580   g_return_if_fail (GTK_IS_TABLE (table));
581   g_return_if_fail (GTK_IS_WIDGET (child));
582   g_return_if_fail (child->parent == NULL);
583   
584   /* g_return_if_fail (left_attach >= 0); */
585   g_return_if_fail (left_attach < right_attach);
586   /* g_return_if_fail (top_attach >= 0); */
587   g_return_if_fail (top_attach < bottom_attach);
588   
589   if (right_attach >= table->ncols)
590     gtk_table_resize (table, table->nrows, right_attach);
591   
592   if (bottom_attach >= table->nrows)
593     gtk_table_resize (table, bottom_attach, table->ncols);
594   
595   table_child = g_new (GtkTableChild, 1);
596   table_child->widget = child;
597   table_child->left_attach = left_attach;
598   table_child->right_attach = right_attach;
599   table_child->top_attach = top_attach;
600   table_child->bottom_attach = bottom_attach;
601   table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
602   table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
603   table_child->xfill = (xoptions & GTK_FILL) != 0;
604   table_child->xpadding = xpadding;
605   table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
606   table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
607   table_child->yfill = (yoptions & GTK_FILL) != 0;
608   table_child->ypadding = ypadding;
609   
610   table->children = g_list_prepend (table->children, table_child);
611   
612   gtk_widget_set_parent (child, GTK_WIDGET (table));
613   
614   if (GTK_WIDGET_REALIZED (child->parent))
615     gtk_widget_realize (child);
616
617   if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
618     {
619       if (GTK_WIDGET_MAPPED (child->parent))
620         gtk_widget_map (child);
621
622       gtk_widget_queue_resize (child);
623     }
624 }
625
626 void
627 gtk_table_attach_defaults (GtkTable  *table,
628                            GtkWidget *widget,
629                            guint      left_attach,
630                            guint      right_attach,
631                            guint      top_attach,
632                            guint      bottom_attach)
633 {
634   gtk_table_attach (table, widget,
635                     left_attach, right_attach,
636                     top_attach, bottom_attach,
637                     GTK_EXPAND | GTK_FILL,
638                     GTK_EXPAND | GTK_FILL,
639                     0, 0);
640 }
641
642 void
643 gtk_table_set_row_spacing (GtkTable *table,
644                            guint     row,
645                            guint     spacing)
646 {
647   g_return_if_fail (GTK_IS_TABLE (table));
648   g_return_if_fail (row < table->nrows);
649   
650   if (table->rows[row].spacing != spacing)
651     {
652       table->rows[row].spacing = spacing;
653       
654       if (GTK_WIDGET_VISIBLE (table))
655         gtk_widget_queue_resize (GTK_WIDGET (table));
656     }
657
658   g_object_notify (G_OBJECT (table), "row_spacing");
659 }
660
661 /**
662  * gtk_table_get_row_spacing:
663  * @table: a #GtkTable
664  * @row: a row in the table, 0 indicates the first row
665  *
666  * Gets the amount of space between row @row, and
667  * row @row + 1. See gtk_table_set_row_spacing().
668  *
669  * Return value: the row spacing
670  **/
671 guint
672 gtk_table_get_row_spacing (GtkTable *table,
673                            guint     row)
674 {
675   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
676   g_return_val_if_fail (row < table->nrows - 1, 0);
677  
678   return table->rows[row].spacing;
679 }
680
681 void
682 gtk_table_set_col_spacing (GtkTable *table,
683                            guint     column,
684                            guint     spacing)
685 {
686   g_return_if_fail (GTK_IS_TABLE (table));
687   g_return_if_fail (column < table->ncols);
688   
689   if (table->cols[column].spacing != spacing)
690     {
691       table->cols[column].spacing = spacing;
692       
693       if (GTK_WIDGET_VISIBLE (table))
694         gtk_widget_queue_resize (GTK_WIDGET (table));
695     }
696
697   g_object_notify (G_OBJECT (table), "column_spacing");
698 }
699
700 /**
701  * gtk_table_get_col_spacing:
702  * @table: a #GtkTable
703  * @col: a column in the table, 0 indicates the first column
704  *
705  * Gets the amount of space between column @col, and
706  * column @col + 1. See gtk_table_set_col_spacing().
707  *
708  * Return value: the column spacing
709  **/
710 guint
711 gtk_table_get_col_spacing (GtkTable *table,
712                            guint     column)
713 {
714   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
715   g_return_val_if_fail (column < table->ncols, 0);
716
717   return table->cols[column].spacing;
718 }
719
720 void
721 gtk_table_set_row_spacings (GtkTable *table,
722                             guint     spacing)
723 {
724   guint row;
725   
726   g_return_if_fail (GTK_IS_TABLE (table));
727   
728   table->row_spacing = spacing;
729   for (row = 0; row < table->nrows; row++)
730     table->rows[row].spacing = spacing;
731   
732   if (GTK_WIDGET_VISIBLE (table))
733     gtk_widget_queue_resize (GTK_WIDGET (table));
734 }
735
736 /**
737  * gtk_table_get_default_row_spacing:
738  * @table: a #GtkTable
739  *
740  * Gets the default row spacing for the table. This is
741  * the spacing that will be used for newly added rows.
742  * (See gtk_table_set_row_spacings())
743  *
744  * Returns value: the default row spacing
745  **/
746 guint
747 gtk_table_get_default_row_spacing (GtkTable *table)
748 {
749   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
750
751   return table->row_spacing;
752 }
753
754 void
755 gtk_table_set_col_spacings (GtkTable *table,
756                             guint     spacing)
757 {
758   guint col;
759   
760   g_return_if_fail (GTK_IS_TABLE (table));
761   
762   table->column_spacing = spacing;
763   for (col = 0; col < table->ncols; col++)
764     table->cols[col].spacing = spacing;
765   
766   if (GTK_WIDGET_VISIBLE (table))
767     gtk_widget_queue_resize (GTK_WIDGET (table));
768 }
769
770 /**
771  * gtk_table_get_default_col_spacing:
772  * @table: a #GtkTable
773  *
774  * Gets the default column spacing for the table. This is
775  * the spacing that will be used for newly added columns.
776  * (See gtk_table_set_col_spacings())
777  *
778  * Returns value: the default column spacing
779  **/
780 guint
781 gtk_table_get_default_col_spacing (GtkTable *table)
782 {
783   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
784
785   return table->column_spacing;
786 }
787
788 void
789 gtk_table_set_homogeneous (GtkTable *table,
790                            gboolean  homogeneous)
791 {
792   g_return_if_fail (GTK_IS_TABLE (table));
793
794   homogeneous = (homogeneous != 0);
795   if (homogeneous != table->homogeneous)
796     {
797       table->homogeneous = homogeneous;
798       
799       if (GTK_WIDGET_VISIBLE (table))
800         gtk_widget_queue_resize (GTK_WIDGET (table));
801     }
802 }
803
804 /**
805  * gtk_table_get_homogeneous:
806  * @table: a #GtkTable
807  *
808  * Returns whether the table cells are all constrained to the same
809  * width and height. (See gtk_table_set_homogenous ())
810  *
811  * Return value: %TRUE if the cells are all constrained to the same size
812  **/
813 gboolean
814 gtk_table_get_homogeneous (GtkTable *table)
815 {
816   g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
817
818   return table->homogeneous;
819 }
820
821 static void
822 gtk_table_finalize (GObject *object)
823 {
824   GtkTable *table;
825   
826   g_return_if_fail (GTK_IS_TABLE (object));
827   
828   table = GTK_TABLE (object);
829   
830   g_free (table->rows);
831   g_free (table->cols);
832   
833   G_OBJECT_CLASS (parent_class)->finalize (object);
834 }
835
836 static void
837 gtk_table_map (GtkWidget *widget)
838 {
839   GtkTable *table;
840   GtkTableChild *child;
841   GList *children;
842   
843   g_return_if_fail (GTK_IS_TABLE (widget));
844   
845   table = GTK_TABLE (widget);
846   GTK_WIDGET_SET_FLAGS (table, GTK_MAPPED);
847   
848   children = table->children;
849   while (children)
850     {
851       child = children->data;
852       children = children->next;
853       
854       if (GTK_WIDGET_VISIBLE (child->widget) &&
855           !GTK_WIDGET_MAPPED (child->widget))
856         gtk_widget_map (child->widget);
857     }
858 }
859
860 static void
861 gtk_table_unmap (GtkWidget *widget)
862 {
863   GtkTable *table;
864   GtkTableChild *child;
865   GList *children;
866   
867   g_return_if_fail (GTK_IS_TABLE (widget));
868   
869   table = GTK_TABLE (widget);
870   GTK_WIDGET_UNSET_FLAGS (table, GTK_MAPPED);
871   
872   children = table->children;
873   while (children)
874     {
875       child = children->data;
876       children = children->next;
877       
878       if (GTK_WIDGET_VISIBLE (child->widget) &&
879           GTK_WIDGET_MAPPED (child->widget))
880         gtk_widget_unmap (child->widget);
881     }
882 }
883
884 static void
885 gtk_table_size_request (GtkWidget      *widget,
886                         GtkRequisition *requisition)
887 {
888   GtkTable *table;
889   gint row, col;
890   
891   g_return_if_fail (GTK_IS_TABLE (widget));
892   g_return_if_fail (requisition != NULL);
893   
894   table = GTK_TABLE (widget);
895   
896   requisition->width = 0;
897   requisition->height = 0;
898   
899   gtk_table_size_request_init (table);
900   gtk_table_size_request_pass1 (table);
901   gtk_table_size_request_pass2 (table);
902   gtk_table_size_request_pass3 (table);
903   gtk_table_size_request_pass2 (table);
904   
905   for (col = 0; col < table->ncols; col++)
906     requisition->width += table->cols[col].requisition;
907   for (col = 0; col + 1 < table->ncols; col++)
908     requisition->width += table->cols[col].spacing;
909   
910   for (row = 0; row < table->nrows; row++)
911     requisition->height += table->rows[row].requisition;
912   for (row = 0; row + 1 < table->nrows; row++)
913     requisition->height += table->rows[row].spacing;
914   
915   requisition->width += GTK_CONTAINER (table)->border_width * 2;
916   requisition->height += GTK_CONTAINER (table)->border_width * 2;
917 }
918
919 static void
920 gtk_table_size_allocate (GtkWidget     *widget,
921                          GtkAllocation *allocation)
922 {
923   GtkTable *table;
924   
925   g_return_if_fail (GTK_IS_TABLE (widget));
926   g_return_if_fail (allocation != NULL);
927   
928   widget->allocation = *allocation;
929   table = GTK_TABLE (widget);
930   
931   gtk_table_size_allocate_init (table);
932   gtk_table_size_allocate_pass1 (table);
933   gtk_table_size_allocate_pass2 (table);
934 }
935
936 static void
937 gtk_table_add (GtkContainer *container,
938                GtkWidget    *widget)
939 {
940   g_return_if_fail (GTK_IS_TABLE (container));
941   g_return_if_fail (widget != NULL);
942   
943   gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
944 }
945
946 static void
947 gtk_table_remove (GtkContainer *container,
948                   GtkWidget    *widget)
949 {
950   GtkTable *table;
951   GtkTableChild *child;
952   GList *children;
953   
954   g_return_if_fail (GTK_IS_TABLE (container));
955   g_return_if_fail (widget != NULL);
956   
957   table = GTK_TABLE (container);
958   children = table->children;
959   
960   while (children)
961     {
962       child = children->data;
963       children = children->next;
964       
965       if (child->widget == widget)
966         {
967           gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
968           
969           gtk_widget_unparent (widget);
970           
971           table->children = g_list_remove (table->children, child);
972           g_free (child);
973           
974           if (was_visible && GTK_WIDGET_VISIBLE (container))
975             gtk_container_queue_resize (container);
976           break;
977         }
978     }
979 }
980
981 static void
982 gtk_table_forall (GtkContainer *container,
983                   gboolean      include_internals,
984                   GtkCallback   callback,
985                   gpointer      callback_data)
986 {
987   GtkTable *table;
988   GtkTableChild *child;
989   GList *children;
990   
991   g_return_if_fail (GTK_IS_TABLE (container));
992   g_return_if_fail (callback != NULL);
993   
994   table = GTK_TABLE (container);
995   children = table->children;
996   
997   while (children)
998     {
999       child = children->data;
1000       children = children->next;
1001       
1002       (* callback) (child->widget, callback_data);
1003     }
1004 }
1005
1006 static void
1007 gtk_table_size_request_init (GtkTable *table)
1008 {
1009   GtkTableChild *child;
1010   GList *children;
1011   gint row, col;
1012   
1013   for (row = 0; row < table->nrows; row++)
1014     table->rows[row].requisition = 0;
1015   for (col = 0; col < table->ncols; col++)
1016     table->cols[col].requisition = 0;
1017   
1018   children = table->children;
1019   while (children)
1020     {
1021       child = children->data;
1022       children = children->next;
1023       
1024       if (GTK_WIDGET_VISIBLE (child->widget))
1025         gtk_widget_size_request (child->widget, NULL);
1026     }
1027 }
1028
1029 static void
1030 gtk_table_size_request_pass1 (GtkTable *table)
1031 {
1032   GtkTableChild *child;
1033   GList *children;
1034   gint width;
1035   gint height;
1036   
1037   children = table->children;
1038   while (children)
1039     {
1040       child = children->data;
1041       children = children->next;
1042       
1043       if (GTK_WIDGET_VISIBLE (child->widget))
1044         {
1045           GtkRequisition child_requisition;
1046           gtk_widget_get_child_requisition (child->widget, &child_requisition);
1047
1048           /* Child spans a single column.
1049            */
1050           if (child->left_attach == (child->right_attach - 1))
1051             {
1052               width = child_requisition.width + child->xpadding * 2;
1053               table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
1054             }
1055           
1056           /* Child spans a single row.
1057            */
1058           if (child->top_attach == (child->bottom_attach - 1))
1059             {
1060               height = child_requisition.height + child->ypadding * 2;
1061               table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
1062             }
1063         }
1064     }
1065 }
1066
1067 static void
1068 gtk_table_size_request_pass2 (GtkTable *table)
1069 {
1070   gint max_width;
1071   gint max_height;
1072   gint row, col;
1073   
1074   if (table->homogeneous)
1075     {
1076       max_width = 0;
1077       max_height = 0;
1078       
1079       for (col = 0; col < table->ncols; col++)
1080         max_width = MAX (max_width, table->cols[col].requisition);
1081       for (row = 0; row < table->nrows; row++)
1082         max_height = MAX (max_height, table->rows[row].requisition);
1083       
1084       for (col = 0; col < table->ncols; col++)
1085         table->cols[col].requisition = max_width;
1086       for (row = 0; row < table->nrows; row++)
1087         table->rows[row].requisition = max_height;
1088     }
1089 }
1090
1091 static void
1092 gtk_table_size_request_pass3 (GtkTable *table)
1093 {
1094   GtkTableChild *child;
1095   GList *children;
1096   gint width, height;
1097   gint row, col;
1098   gint extra;
1099   
1100   children = table->children;
1101   while (children)
1102     {
1103       child = children->data;
1104       children = children->next;
1105       
1106       if (GTK_WIDGET_VISIBLE (child->widget))
1107         {
1108           /* Child spans multiple columns.
1109            */
1110           if (child->left_attach != (child->right_attach - 1))
1111             {
1112               GtkRequisition child_requisition;
1113
1114               gtk_widget_get_child_requisition (child->widget, &child_requisition);
1115               
1116               /* Check and see if there is already enough space
1117                *  for the child.
1118                */
1119               width = 0;
1120               for (col = child->left_attach; col < child->right_attach; col++)
1121                 {
1122                   width += table->cols[col].requisition;
1123                   if ((col + 1) < child->right_attach)
1124                     width += table->cols[col].spacing;
1125                 }
1126               
1127               /* If we need to request more space for this child to fill
1128                *  its requisition, then divide up the needed space evenly
1129                *  amongst the columns it spans.
1130                */
1131               if (width < child_requisition.width + child->xpadding * 2)
1132                 {
1133                   width = child_requisition.width + child->xpadding * 2 - width;
1134                   
1135                   for (col = child->left_attach; col < child->right_attach; col++)
1136                     {
1137                       extra = width / (child->right_attach - col);
1138                       table->cols[col].requisition += extra;
1139                       width -= extra;
1140                     }
1141                 }
1142             }
1143           
1144           /* Child spans multiple rows.
1145            */
1146           if (child->top_attach != (child->bottom_attach - 1))
1147             {
1148               GtkRequisition child_requisition;
1149
1150               gtk_widget_get_child_requisition (child->widget, &child_requisition);
1151
1152               /* Check and see if there is already enough space
1153                *  for the child.
1154                */
1155               height = 0;
1156               for (row = child->top_attach; row < child->bottom_attach; row++)
1157                 {
1158                   height += table->rows[row].requisition;
1159                   if ((row + 1) < child->bottom_attach)
1160                     height += table->rows[row].spacing;
1161                 }
1162               
1163               /* If we need to request more space for this child to fill
1164                *  its requisition, then divide up the needed space evenly
1165                *  amongst the columns it spans.
1166                */
1167               if (height < child_requisition.height + child->ypadding * 2)
1168                 {
1169                   height = child_requisition.height + child->ypadding * 2 - height;
1170                   
1171                   for (row = child->top_attach; row < child->bottom_attach; row++)
1172                     {
1173                       extra = height / (child->bottom_attach - row);
1174                       table->rows[row].requisition += extra;
1175                       height -= extra;
1176                     }
1177                 }
1178             }
1179         }
1180     }
1181 }
1182
1183 static void
1184 gtk_table_size_allocate_init (GtkTable *table)
1185 {
1186   GtkTableChild *child;
1187   GList *children;
1188   gint row, col;
1189   gint has_expand;
1190   gint has_shrink;
1191   
1192   /* Initialize the rows and cols.
1193    *  By default, rows and cols do not expand and do shrink.
1194    *  Those values are modified by the children that occupy
1195    *  the rows and cols.
1196    */
1197   for (col = 0; col < table->ncols; col++)
1198     {
1199       table->cols[col].allocation = table->cols[col].requisition;
1200       table->cols[col].need_expand = FALSE;
1201       table->cols[col].need_shrink = TRUE;
1202       table->cols[col].expand = FALSE;
1203       table->cols[col].shrink = TRUE;
1204       table->cols[col].empty = TRUE;
1205     }
1206   for (row = 0; row < table->nrows; row++)
1207     {
1208       table->rows[row].allocation = table->rows[row].requisition;
1209       table->rows[row].need_expand = FALSE;
1210       table->rows[row].need_shrink = TRUE;
1211       table->rows[row].expand = FALSE;
1212       table->rows[row].shrink = TRUE;
1213       table->rows[row].empty = TRUE;
1214     }
1215   
1216   /* Loop over all the children and adjust the row and col values
1217    *  based on whether the children want to be allowed to expand
1218    *  or shrink. This loop handles children that occupy a single
1219    *  row or column.
1220    */
1221   children = table->children;
1222   while (children)
1223     {
1224       child = children->data;
1225       children = children->next;
1226       
1227       if (GTK_WIDGET_VISIBLE (child->widget))
1228         {
1229           if (child->left_attach == (child->right_attach - 1))
1230             {
1231               if (child->xexpand)
1232                 table->cols[child->left_attach].expand = TRUE;
1233               
1234               if (!child->xshrink)
1235                 table->cols[child->left_attach].shrink = FALSE;
1236               
1237               table->cols[child->left_attach].empty = FALSE;
1238             }
1239           
1240           if (child->top_attach == (child->bottom_attach - 1))
1241             {
1242               if (child->yexpand)
1243                 table->rows[child->top_attach].expand = TRUE;
1244               
1245               if (!child->yshrink)
1246                 table->rows[child->top_attach].shrink = FALSE;
1247
1248               table->rows[child->top_attach].empty = FALSE;
1249             }
1250         }
1251     }
1252   
1253   /* Loop over all the children again and this time handle children
1254    *  which span multiple rows or columns.
1255    */
1256   children = table->children;
1257   while (children)
1258     {
1259       child = children->data;
1260       children = children->next;
1261       
1262       if (GTK_WIDGET_VISIBLE (child->widget))
1263         {
1264           if (child->left_attach != (child->right_attach - 1))
1265             {
1266               for (col = child->left_attach; col < child->right_attach; col++)
1267                 table->cols[col].empty = FALSE;
1268
1269               if (child->xexpand)
1270                 {
1271                   has_expand = FALSE;
1272                   for (col = child->left_attach; col < child->right_attach; col++)
1273                     if (table->cols[col].expand)
1274                       {
1275                         has_expand = TRUE;
1276                         break;
1277                       }
1278                   
1279                   if (!has_expand)
1280                     for (col = child->left_attach; col < child->right_attach; col++)
1281                       table->cols[col].need_expand = TRUE;
1282                 }
1283               
1284               if (!child->xshrink)
1285                 {
1286                   has_shrink = TRUE;
1287                   for (col = child->left_attach; col < child->right_attach; col++)
1288                     if (!table->cols[col].shrink)
1289                       {
1290                         has_shrink = FALSE;
1291                         break;
1292                       }
1293                   
1294                   if (has_shrink)
1295                     for (col = child->left_attach; col < child->right_attach; col++)
1296                       table->cols[col].need_shrink = FALSE;
1297                 }
1298             }
1299           
1300           if (child->top_attach != (child->bottom_attach - 1))
1301             {
1302               for (row = child->top_attach; row < child->bottom_attach; row++)
1303                 table->rows[row].empty = FALSE;
1304
1305               if (child->yexpand)
1306                 {
1307                   has_expand = FALSE;
1308                   for (row = child->top_attach; row < child->bottom_attach; row++)
1309                     if (table->rows[row].expand)
1310                       {
1311                         has_expand = TRUE;
1312                         break;
1313                       }
1314                   
1315                   if (!has_expand)
1316                     for (row = child->top_attach; row < child->bottom_attach; row++)
1317                       table->rows[row].need_expand = TRUE;
1318                 }
1319               
1320               if (!child->yshrink)
1321                 {
1322                   has_shrink = TRUE;
1323                   for (row = child->top_attach; row < child->bottom_attach; row++)
1324                     if (!table->rows[row].shrink)
1325                       {
1326                         has_shrink = FALSE;
1327                         break;
1328                       }
1329                   
1330                   if (has_shrink)
1331                     for (row = child->top_attach; row < child->bottom_attach; row++)
1332                       table->rows[row].need_shrink = FALSE;
1333                 }
1334             }
1335         }
1336     }
1337   
1338   /* Loop over the columns and set the expand and shrink values
1339    *  if the column can be expanded or shrunk.
1340    */
1341   for (col = 0; col < table->ncols; col++)
1342     {
1343       if (table->cols[col].empty)
1344         {
1345           table->cols[col].expand = FALSE;
1346           table->cols[col].shrink = FALSE;
1347         }
1348       else
1349         {
1350           if (table->cols[col].need_expand)
1351             table->cols[col].expand = TRUE;
1352           if (!table->cols[col].need_shrink)
1353             table->cols[col].shrink = FALSE;
1354         }
1355     }
1356   
1357   /* Loop over the rows and set the expand and shrink values
1358    *  if the row can be expanded or shrunk.
1359    */
1360   for (row = 0; row < table->nrows; row++)
1361     {
1362       if (table->rows[row].empty)
1363         {
1364           table->rows[row].expand = FALSE;
1365           table->rows[row].shrink = FALSE;
1366         }
1367       else
1368         {
1369           if (table->rows[row].need_expand)
1370             table->rows[row].expand = TRUE;
1371           if (!table->rows[row].need_shrink)
1372             table->rows[row].shrink = FALSE;
1373         }
1374     }
1375 }
1376
1377 static void
1378 gtk_table_size_allocate_pass1 (GtkTable *table)
1379 {
1380   gint real_width;
1381   gint real_height;
1382   gint width, height;
1383   gint row, col;
1384   gint nexpand;
1385   gint nshrink;
1386   gint extra;
1387   
1388   /* If we were allocated more space than we requested
1389    *  then we have to expand any expandable rows and columns
1390    *  to fill in the extra space.
1391    */
1392   
1393   real_width = GTK_WIDGET (table)->allocation.width - GTK_CONTAINER (table)->border_width * 2;
1394   real_height = GTK_WIDGET (table)->allocation.height - GTK_CONTAINER (table)->border_width * 2;
1395   
1396   if (table->homogeneous)
1397     {
1398       nexpand = 0;
1399       for (col = 0; col < table->ncols; col++)
1400         if (table->cols[col].expand)
1401           {
1402             nexpand += 1;
1403             break;
1404           }
1405       
1406       if (nexpand > 0)
1407         {
1408           width = real_width;
1409           
1410           for (col = 0; col + 1 < table->ncols; col++)
1411             width -= table->cols[col].spacing;
1412           
1413           for (col = 0; col < table->ncols; col++)
1414             {
1415               extra = width / (table->ncols - col);
1416               table->cols[col].allocation = MAX (1, extra);
1417               width -= extra;
1418             }
1419         }
1420     }
1421   else
1422     {
1423       width = 0;
1424       nexpand = 0;
1425       nshrink = 0;
1426       
1427       for (col = 0; col < table->ncols; col++)
1428         {
1429           width += table->cols[col].requisition;
1430           if (table->cols[col].expand)
1431             nexpand += 1;
1432           if (table->cols[col].shrink)
1433             nshrink += 1;
1434         }
1435       for (col = 0; col + 1 < table->ncols; col++)
1436         width += table->cols[col].spacing;
1437       
1438       /* Check to see if we were allocated more width than we requested.
1439        */
1440       if ((width < real_width) && (nexpand >= 1))
1441         {
1442           width = real_width - width;
1443           
1444           for (col = 0; col < table->ncols; col++)
1445             if (table->cols[col].expand)
1446               {
1447                 extra = width / nexpand;
1448                 table->cols[col].allocation += extra;
1449                 
1450                 width -= extra;
1451                 nexpand -= 1;
1452               }
1453         }
1454       
1455       /* Check to see if we were allocated less width than we requested,
1456        * then shrink until we fit the size give.
1457        */
1458       if (width > real_width)
1459         {
1460           gint total_nshrink = nshrink;
1461
1462           extra = width - real_width;
1463           while (total_nshrink > 0 && extra > 0)
1464             {
1465               nshrink = total_nshrink;
1466               for (col = 0; col < table->ncols; col++)
1467                 if (table->cols[col].shrink)
1468                   {
1469                     gint allocation = table->cols[col].allocation;
1470
1471                     table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1472                     extra -= allocation - table->cols[col].allocation;
1473                     nshrink -= 1;
1474                     if (table->cols[col].allocation < 2)
1475                       {
1476                         total_nshrink -= 1;
1477                         table->cols[col].shrink = FALSE;
1478                       }
1479                   }
1480             }
1481         }
1482     }
1483   
1484   if (table->homogeneous)
1485     {
1486       nexpand = 0;
1487       for (row = 0; row < table->nrows; row++)
1488         if (table->rows[row].expand)
1489           {
1490             nexpand += 1;
1491             break;
1492           }
1493       
1494       if (nexpand > 0)
1495         {
1496           height = real_height;
1497           
1498           for (row = 0; row + 1 < table->nrows; row++)
1499             height -= table->rows[row].spacing;
1500           
1501           
1502           for (row = 0; row < table->nrows; row++)
1503             {
1504               extra = height / (table->nrows - row);
1505               table->rows[row].allocation = MAX (1, extra);
1506               height -= extra;
1507             }
1508         }
1509     }
1510   else
1511     {
1512       height = 0;
1513       nexpand = 0;
1514       nshrink = 0;
1515       
1516       for (row = 0; row < table->nrows; row++)
1517         {
1518           height += table->rows[row].requisition;
1519           if (table->rows[row].expand)
1520             nexpand += 1;
1521           if (table->rows[row].shrink)
1522             nshrink += 1;
1523         }
1524       for (row = 0; row + 1 < table->nrows; row++)
1525         height += table->rows[row].spacing;
1526       
1527       /* Check to see if we were allocated more height than we requested.
1528        */
1529       if ((height < real_height) && (nexpand >= 1))
1530         {
1531           height = real_height - height;
1532           
1533           for (row = 0; row < table->nrows; row++)
1534             if (table->rows[row].expand)
1535               {
1536                 extra = height / nexpand;
1537                 table->rows[row].allocation += extra;
1538                 
1539                 height -= extra;
1540                 nexpand -= 1;
1541               }
1542         }
1543       
1544       /* Check to see if we were allocated less height than we requested.
1545        * then shrink until we fit the size give.
1546        */
1547       if (height > real_height)
1548         {
1549           gint total_nshrink = nshrink;
1550           
1551           extra = height - real_height;
1552           while (total_nshrink > 0 && extra > 0)
1553             {
1554               nshrink = total_nshrink;
1555               for (row = 0; row < table->nrows; row++)
1556                 if (table->rows[row].shrink)
1557                   {
1558                     gint allocation = table->rows[row].allocation;
1559                     
1560                     table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1561                     extra -= allocation - table->rows[row].allocation;
1562                     nshrink -= 1;
1563                     if (table->rows[row].allocation < 2)
1564                       {
1565                         total_nshrink -= 1;
1566                         table->rows[row].shrink = FALSE;
1567                       }
1568                   }
1569             }
1570         }
1571     }
1572 }
1573
1574 static void
1575 gtk_table_size_allocate_pass2 (GtkTable *table)
1576 {
1577   GtkTableChild *child;
1578   GList *children;
1579   gint max_width;
1580   gint max_height;
1581   gint x, y;
1582   gint row, col;
1583   GtkAllocation allocation;
1584   GtkWidget *widget = GTK_WIDGET (table);
1585   
1586   children = table->children;
1587   while (children)
1588     {
1589       child = children->data;
1590       children = children->next;
1591       
1592       if (GTK_WIDGET_VISIBLE (child->widget))
1593         {
1594           GtkRequisition child_requisition;
1595           gtk_widget_get_child_requisition (child->widget, &child_requisition);
1596
1597           x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1598           y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1599           max_width = 0;
1600           max_height = 0;
1601           
1602           for (col = 0; col < child->left_attach; col++)
1603             {
1604               x += table->cols[col].allocation;
1605               x += table->cols[col].spacing;
1606             }
1607           
1608           for (col = child->left_attach; col < child->right_attach; col++)
1609             {
1610               max_width += table->cols[col].allocation;
1611               if ((col + 1) < child->right_attach)
1612                 max_width += table->cols[col].spacing;
1613             }
1614           
1615           for (row = 0; row < child->top_attach; row++)
1616             {
1617               y += table->rows[row].allocation;
1618               y += table->rows[row].spacing;
1619             }
1620           
1621           for (row = child->top_attach; row < child->bottom_attach; row++)
1622             {
1623               max_height += table->rows[row].allocation;
1624               if ((row + 1) < child->bottom_attach)
1625                 max_height += table->rows[row].spacing;
1626             }
1627           
1628           if (child->xfill)
1629             {
1630               allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1631               allocation.x = x + (max_width - allocation.width) / 2;
1632             }
1633           else
1634             {
1635               allocation.width = child_requisition.width;
1636               allocation.x = x + (max_width - allocation.width) / 2;
1637             }
1638           
1639           if (child->yfill)
1640             {
1641               allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1642               allocation.y = y + (max_height - allocation.height) / 2;
1643             }
1644           else
1645             {
1646               allocation.height = child_requisition.height;
1647               allocation.y = y + (max_height - allocation.height) / 2;
1648             }
1649
1650           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1651             allocation.x = widget->allocation.x + widget->allocation.width
1652               - (allocation.x - widget->allocation.x) - allocation.width;
1653           
1654           gtk_widget_size_allocate (child->widget, &allocation);
1655         }
1656     }
1657 }