]> Pileus Git - ~andy/gtk/blob - gtk/gtktable.c
Minor documentation fixes
[~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_size_request  (GtkWidget      *widget,
58                                      GtkRequisition *requisition);
59 static void gtk_table_size_allocate (GtkWidget      *widget,
60                                      GtkAllocation  *allocation);
61 static void gtk_table_add           (GtkContainer   *container,
62                                      GtkWidget      *widget);
63 static void gtk_table_remove        (GtkContainer   *container,
64                                      GtkWidget      *widget);
65 static void gtk_table_forall        (GtkContainer   *container,
66                                      gboolean        include_internals,
67                                      GtkCallback     callback,
68                                      gpointer        callback_data);
69 static void gtk_table_get_property  (GObject         *object,
70                                      guint            prop_id,
71                                      GValue          *value,
72                                      GParamSpec      *pspec);
73 static void gtk_table_set_property  (GObject         *object,
74                                      guint            prop_id,
75                                      const GValue    *value,
76                                      GParamSpec      *pspec);
77 static void gtk_table_set_child_property (GtkContainer    *container,
78                                           GtkWidget       *child,
79                                           guint            property_id,
80                                           const GValue    *value,
81                                           GParamSpec      *pspec);
82 static void gtk_table_get_child_property (GtkContainer    *container,
83                                           GtkWidget       *child,
84                                           guint            property_id,
85                                           GValue          *value,
86                                           GParamSpec      *pspec);
87 static GtkType gtk_table_child_type (GtkContainer   *container);
88
89
90 static void gtk_table_size_request_init  (GtkTable *table);
91 static void gtk_table_size_request_pass1 (GtkTable *table);
92 static void gtk_table_size_request_pass2 (GtkTable *table);
93 static void gtk_table_size_request_pass3 (GtkTable *table);
94
95 static void gtk_table_size_allocate_init  (GtkTable *table);
96 static void gtk_table_size_allocate_pass1 (GtkTable *table);
97 static void gtk_table_size_allocate_pass2 (GtkTable *table);
98
99
100 static GtkContainerClass *parent_class = NULL;
101
102
103 GtkType
104 gtk_table_get_type (void)
105 {
106   static GtkType table_type = 0;
107   
108   if (!table_type)
109     {
110       static const GtkTypeInfo table_info =
111       {
112         "GtkTable",
113         sizeof (GtkTable),
114         sizeof (GtkTableClass),
115         (GtkClassInitFunc) gtk_table_class_init,
116         (GtkObjectInitFunc) gtk_table_init,
117         /* reserved_1 */ NULL,
118         /* reserved_2 */ NULL,
119         (GtkClassInitFunc) NULL,
120       };
121       
122       table_type = gtk_type_unique (gtk_container_get_type (), &table_info);
123     }
124   
125   return table_type;
126 }
127
128 static void
129 gtk_table_class_init (GtkTableClass *class)
130 {
131   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
132   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
133   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
134   
135   parent_class = g_type_class_peek_parent (class);
136
137   gobject_class->finalize = gtk_table_finalize;
138
139   gobject_class->get_property = gtk_table_get_property;
140   gobject_class->set_property = gtk_table_set_property;
141   
142   widget_class->size_request = gtk_table_size_request;
143   widget_class->size_allocate = gtk_table_size_allocate;
144   
145   container_class->add = gtk_table_add;
146   container_class->remove = gtk_table_remove;
147   container_class->forall = gtk_table_forall;
148   container_class->child_type = gtk_table_child_type;
149   container_class->set_child_property = gtk_table_set_child_property;
150   container_class->get_child_property = gtk_table_get_child_property;
151   
152
153   g_object_class_install_property (gobject_class,
154                                    PROP_N_ROWS,
155                                    g_param_spec_uint ("n_rows",
156                                                      _("Rows"),
157                                                      _("The number of rows in the table"),
158                                                      0,
159                                                      G_MAXUINT,
160                                                      0,
161                                                      G_PARAM_READWRITE));
162   g_object_class_install_property (gobject_class,
163                                    PROP_N_COLUMNS,
164                                    g_param_spec_uint ("n_columns",
165                                                      _("Columns"),
166                                                      _("The number of columns in the table"),
167                                                      0,
168                                                      G_MAXUINT,
169                                                      0,
170                                                      G_PARAM_READWRITE));
171   g_object_class_install_property (gobject_class,
172                                    PROP_ROW_SPACING,
173                                    g_param_spec_uint ("row_spacing",
174                                                      _("Row spacing"),
175                                                      _("The amount of space between two consecutive rows"),
176                                                      0,
177                                                      G_MAXUINT,
178                                                      0,
179                                                      G_PARAM_READWRITE));
180   g_object_class_install_property (gobject_class,
181                                    PROP_COLUMN_SPACING,
182                                    g_param_spec_uint ("column_spacing",
183                                                      _("Column spacing"),
184                                                      _("The amount of space between two consecutive columns"),
185                                                      0,
186                                                      G_MAXUINT,
187                                                      0,
188                                                      G_PARAM_READWRITE));
189   g_object_class_install_property (gobject_class,
190                                    PROP_HOMOGENEOUS,
191                                    g_param_spec_boolean ("homogeneous",
192                                                          _("Homogenous"),
193                                                          _("If TRUE this means the table cells are all the same width/height"),
194                                                          FALSE,
195                                                          G_PARAM_READWRITE));
196
197   gtk_container_class_install_child_property (container_class,
198                                               CHILD_PROP_LEFT_ATTACH,
199                                               g_param_spec_uint ("left_attach", 
200                                                                  _("Left attachment"), 
201                                                                  _("The leftmost column of the child"),
202                                                                  0, 65535, 0,
203                                                                  G_PARAM_READWRITE));
204   gtk_container_class_install_child_property (container_class,
205                                               CHILD_PROP_RIGHT_ATTACH,
206                                               g_param_spec_uint ("right_attach", 
207                                                                  _("Right attachment"), 
208                                                                  _("The rightmost column of the child"),
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", 
214                                                                  _("Top attachment"), 
215                                                                  _("The uppermost row of the child"),
216                                                                  0, 65535, 0,
217                                                                  G_PARAM_READWRITE));
218   gtk_container_class_install_child_property (container_class,
219                                               CHILD_PROP_BOTTOM_ATTACH,
220                                               g_param_spec_uint ("bottom_attach",
221                                                                  _("Bottom attachment"), 
222                                                                  _("The lowest row of the child"),
223                                                                  1, 65535, 1,
224                                                                  G_PARAM_READWRITE));
225   gtk_container_class_install_child_property (container_class,
226                                               CHILD_PROP_X_OPTIONS,
227                                               g_param_spec_flags ("x_options", 
228                                                                   _("Horizontal options"), 
229                                                                   _("Options specifying the horizontal behaviour of the child"),
230                                                                   GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
231                                                                   G_PARAM_READWRITE));
232   gtk_container_class_install_child_property (container_class,
233                                               CHILD_PROP_Y_OPTIONS,
234                                               g_param_spec_flags ("y_options", 
235                                                                   _("Vertical options"), 
236                                                                   _("Options specifying the vertical behaviour of the child"),
237                                                                   GTK_TYPE_ATTACH_OPTIONS, GTK_EXPAND | GTK_FILL,
238                                                                   G_PARAM_READWRITE));
239   gtk_container_class_install_child_property (container_class,
240                                               CHILD_PROP_X_PADDING,
241                                               g_param_spec_uint ("x_padding", 
242                                                                  _("Horizontal padding"), 
243                                                                  _("Extra space to put between the child and its left and right neighbors, in pixels"),
244                                                                  0, 65535, 0,
245                                                                  G_PARAM_READWRITE));
246   gtk_container_class_install_child_property (container_class,
247                                               CHILD_PROP_Y_PADDING,
248                                               g_param_spec_uint ("y_padding", 
249                                                                  _("Vertical padding"), 
250                                                                  _("Extra space to put between the child and its upper and lower neighbors, in pixels"),
251                                                                  0, 65535, 0,
252                                                                  G_PARAM_READWRITE));
253 }
254
255 static GtkType
256 gtk_table_child_type (GtkContainer   *container)
257 {
258   return GTK_TYPE_WIDGET;
259 }
260
261 static void
262 gtk_table_get_property (GObject      *object,
263                         guint         prop_id,
264                         GValue       *value,
265                         GParamSpec   *pspec)
266 {
267   GtkTable *table;
268
269   table = GTK_TABLE (object);
270
271   switch (prop_id)
272     {
273     case PROP_N_ROWS:
274       g_value_set_uint (value, table->nrows);
275       break;
276     case PROP_N_COLUMNS:
277       g_value_set_uint (value, table->ncols);
278       break;
279     case PROP_ROW_SPACING:
280       g_value_set_uint (value, table->row_spacing);
281       break;
282     case PROP_COLUMN_SPACING:
283       g_value_set_uint (value, table->column_spacing);
284       break;
285     case PROP_HOMOGENEOUS:
286       g_value_set_boolean (value, table->homogeneous);
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291     }
292 }
293
294 static void
295 gtk_table_set_property (GObject      *object,
296                         guint         prop_id,
297                         const GValue *value,
298                         GParamSpec   *pspec)
299 {
300   GtkTable *table;
301
302   table = GTK_TABLE (object);
303
304   switch (prop_id)
305     {
306     case PROP_N_ROWS:
307       gtk_table_resize (table, g_value_get_uint (value), table->ncols);
308       break;
309     case PROP_N_COLUMNS:
310       gtk_table_resize (table, table->nrows, g_value_get_uint (value));
311       break;
312     case PROP_ROW_SPACING:
313       gtk_table_set_row_spacings (table, g_value_get_uint (value));
314       break;
315     case PROP_COLUMN_SPACING:
316       gtk_table_set_col_spacings (table, g_value_get_uint (value));
317       break;
318     case PROP_HOMOGENEOUS:
319       gtk_table_set_homogeneous (table, g_value_get_boolean (value));
320       break;
321     default:
322       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323       break;
324     }
325 }
326
327 static void
328 gtk_table_set_child_property (GtkContainer    *container,
329                               GtkWidget       *child,
330                               guint            property_id,
331                               const GValue    *value,
332                               GParamSpec      *pspec)
333 {
334   GtkTable *table = GTK_TABLE (container);
335   GtkTableChild *table_child;
336   GList *list;
337
338   table_child = NULL;
339   for (list = table->children; list; list = list->next)
340     {
341       table_child = list->data;
342
343       if (table_child->widget == child)
344         break;
345     }
346   if (!list)
347     {
348       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
349       return;
350     }
351
352   switch (property_id)
353     {
354     case CHILD_PROP_LEFT_ATTACH:
355       table_child->left_attach = g_value_get_uint (value);
356       if (table_child->right_attach <= table_child->left_attach)
357         table_child->right_attach = table_child->left_attach + 1;
358       if (table_child->right_attach >= table->ncols)
359         gtk_table_resize (table, table->nrows, table_child->right_attach);
360       break;
361     case CHILD_PROP_RIGHT_ATTACH:
362       table_child->right_attach = g_value_get_uint (value);
363       if (table_child->right_attach <= table_child->left_attach)
364         table_child->left_attach = table_child->right_attach - 1;
365       if (table_child->right_attach >= table->ncols)
366         gtk_table_resize (table, table->nrows, table_child->right_attach);
367       break;
368     case CHILD_PROP_TOP_ATTACH:
369       table_child->top_attach = g_value_get_uint (value);
370       if (table_child->bottom_attach <= table_child->top_attach)
371         table_child->bottom_attach = table_child->top_attach + 1;
372       if (table_child->bottom_attach >= table->nrows)
373         gtk_table_resize (table, table_child->bottom_attach, table->ncols);
374       break;
375     case CHILD_PROP_BOTTOM_ATTACH:
376       table_child->bottom_attach = g_value_get_uint (value);
377       if (table_child->bottom_attach <= table_child->top_attach)
378         table_child->top_attach = table_child->bottom_attach - 1;
379       if (table_child->bottom_attach >= table->nrows)
380         gtk_table_resize (table, table_child->bottom_attach, table->ncols);
381       break;
382     case CHILD_PROP_X_OPTIONS:
383       table_child->xexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
384       table_child->xshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
385       table_child->xfill = (g_value_get_flags (value) & GTK_FILL) != 0;
386       break;
387     case CHILD_PROP_Y_OPTIONS:
388       table_child->yexpand = (g_value_get_flags (value) & GTK_EXPAND) != 0;
389       table_child->yshrink = (g_value_get_flags (value) & GTK_SHRINK) != 0;
390       table_child->yfill = (g_value_get_flags (value) & GTK_FILL) != 0;
391       break;
392     case CHILD_PROP_X_PADDING:
393       table_child->xpadding = g_value_get_uint (value);
394       break;
395     case CHILD_PROP_Y_PADDING:
396       table_child->ypadding = g_value_get_uint (value);
397       break;
398     default:
399       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
400       break;
401     }
402   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (table))
403     gtk_widget_queue_resize (child);
404 }
405
406 static void
407 gtk_table_get_child_property (GtkContainer    *container,
408                               GtkWidget       *child,
409                               guint            property_id,
410                               GValue          *value,
411                               GParamSpec      *pspec)
412 {
413   GtkTable *table = GTK_TABLE (container);
414   GtkTableChild *table_child;
415   GList *list;
416
417   table_child = NULL;
418   for (list = table->children; list; list = list->next)
419     {
420       table_child = list->data;
421
422       if (table_child->widget == child)
423         break;
424     }
425   if (!list)
426     {
427       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
428       return;
429     }
430
431   switch (property_id)
432     {
433     case CHILD_PROP_LEFT_ATTACH:
434       g_value_set_uint (value, table_child->left_attach);
435       break;
436     case CHILD_PROP_RIGHT_ATTACH:
437       g_value_set_uint (value, table_child->right_attach);
438       break;
439     case CHILD_PROP_TOP_ATTACH:
440       g_value_set_uint (value, table_child->top_attach);
441       break;
442     case CHILD_PROP_BOTTOM_ATTACH:
443       g_value_set_uint (value, table_child->bottom_attach);
444       break;
445     case CHILD_PROP_X_OPTIONS:
446       g_value_set_flags (value, (table_child->xexpand * GTK_EXPAND |
447                                  table_child->xshrink * GTK_SHRINK |
448                                  table_child->xfill * GTK_FILL));
449       break;
450     case CHILD_PROP_Y_OPTIONS:
451       g_value_set_flags (value, (table_child->yexpand * GTK_EXPAND |
452                                  table_child->yshrink * GTK_SHRINK |
453                                  table_child->yfill * GTK_FILL));
454       break;
455     case CHILD_PROP_X_PADDING:
456       g_value_set_uint (value, table_child->xpadding);
457       break;
458     case CHILD_PROP_Y_PADDING:
459       g_value_set_uint (value, table_child->ypadding);
460       break;
461     default:
462       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
463       break;
464     }
465 }
466
467 static void
468 gtk_table_init (GtkTable *table)
469 {
470   GTK_WIDGET_SET_FLAGS (table, GTK_NO_WINDOW);
471   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (table), FALSE);
472   
473   table->children = NULL;
474   table->rows = NULL;
475   table->cols = NULL;
476   table->nrows = 0;
477   table->ncols = 0;
478   table->column_spacing = 0;
479   table->row_spacing = 0;
480   table->homogeneous = FALSE;
481
482   gtk_table_resize (table, 1, 1);
483 }
484
485 GtkWidget*
486 gtk_table_new (guint    rows,
487                guint    columns,
488                gboolean homogeneous)
489 {
490   GtkTable *table;
491
492   if (rows == 0)
493           rows = 1;
494   if (columns == 0)
495           columns = 1;
496   
497   table = gtk_type_new (gtk_table_get_type ());
498   
499   table->homogeneous = (homogeneous ? TRUE : FALSE);
500
501   gtk_table_resize (table, rows, columns);
502   
503   return GTK_WIDGET (table);
504 }
505
506 void
507 gtk_table_resize (GtkTable *table,
508                   guint     n_rows,
509                   guint     n_cols)
510 {
511   g_return_if_fail (GTK_IS_TABLE (table));
512   g_return_if_fail (n_rows > 0 && n_rows < 65536);
513   g_return_if_fail (n_rows > 0 && n_cols < 65536);
514
515   n_rows = MAX (n_rows, 1);
516   n_cols = MAX (n_cols, 1);
517
518   if (n_rows != table->nrows ||
519       n_cols != table->ncols)
520     {
521       GList *list;
522       
523       for (list = table->children; list; list = list->next)
524         {
525           GtkTableChild *child;
526           
527           child = list->data;
528           
529           n_rows = MAX (n_rows, child->bottom_attach);
530           n_cols = MAX (n_cols, child->right_attach);
531         }
532       
533       if (n_rows != table->nrows)
534         {
535           guint i;
536
537           i = table->nrows;
538           table->nrows = n_rows;
539           table->rows = g_realloc (table->rows, table->nrows * sizeof (GtkTableRowCol));
540           
541           for (; i < table->nrows; i++)
542             {
543               table->rows[i].requisition = 0;
544               table->rows[i].allocation = 0;
545               table->rows[i].spacing = table->row_spacing;
546               table->rows[i].need_expand = 0;
547               table->rows[i].need_shrink = 0;
548               table->rows[i].expand = 0;
549               table->rows[i].shrink = 0;
550             }
551
552           g_object_notify (G_OBJECT (table), "n_rows");
553         }
554
555       if (n_cols != table->ncols)
556         {
557           guint i;
558
559           i = table->ncols;
560           table->ncols = n_cols;
561           table->cols = g_realloc (table->cols, table->ncols * sizeof (GtkTableRowCol));
562           
563           for (; i < table->ncols; i++)
564             {
565               table->cols[i].requisition = 0;
566               table->cols[i].allocation = 0;
567               table->cols[i].spacing = table->column_spacing;
568               table->cols[i].need_expand = 0;
569               table->cols[i].need_shrink = 0;
570               table->cols[i].expand = 0;
571               table->cols[i].shrink = 0;
572             }
573
574           g_object_notify (G_OBJECT (table), "n_columns");
575         }
576     }
577 }
578
579 void
580 gtk_table_attach (GtkTable        *table,
581                   GtkWidget       *child,
582                   guint            left_attach,
583                   guint            right_attach,
584                   guint            top_attach,
585                   guint            bottom_attach,
586                   GtkAttachOptions xoptions,
587                   GtkAttachOptions yoptions,
588                   guint            xpadding,
589                   guint            ypadding)
590 {
591   GtkTableChild *table_child;
592   
593   g_return_if_fail (GTK_IS_TABLE (table));
594   g_return_if_fail (GTK_IS_WIDGET (child));
595   g_return_if_fail (child->parent == NULL);
596   
597   /* g_return_if_fail (left_attach >= 0); */
598   g_return_if_fail (left_attach < right_attach);
599   /* g_return_if_fail (top_attach >= 0); */
600   g_return_if_fail (top_attach < bottom_attach);
601   
602   if (right_attach >= table->ncols)
603     gtk_table_resize (table, table->nrows, right_attach);
604   
605   if (bottom_attach >= table->nrows)
606     gtk_table_resize (table, bottom_attach, table->ncols);
607   
608   table_child = g_new (GtkTableChild, 1);
609   table_child->widget = child;
610   table_child->left_attach = left_attach;
611   table_child->right_attach = right_attach;
612   table_child->top_attach = top_attach;
613   table_child->bottom_attach = bottom_attach;
614   table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
615   table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
616   table_child->xfill = (xoptions & GTK_FILL) != 0;
617   table_child->xpadding = xpadding;
618   table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
619   table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
620   table_child->yfill = (yoptions & GTK_FILL) != 0;
621   table_child->ypadding = ypadding;
622   
623   table->children = g_list_prepend (table->children, table_child);
624   
625   gtk_widget_set_parent (child, GTK_WIDGET (table));
626 }
627
628 void
629 gtk_table_attach_defaults (GtkTable  *table,
630                            GtkWidget *widget,
631                            guint      left_attach,
632                            guint      right_attach,
633                            guint      top_attach,
634                            guint      bottom_attach)
635 {
636   gtk_table_attach (table, widget,
637                     left_attach, right_attach,
638                     top_attach, bottom_attach,
639                     GTK_EXPAND | GTK_FILL,
640                     GTK_EXPAND | GTK_FILL,
641                     0, 0);
642 }
643
644 void
645 gtk_table_set_row_spacing (GtkTable *table,
646                            guint     row,
647                            guint     spacing)
648 {
649   g_return_if_fail (GTK_IS_TABLE (table));
650   g_return_if_fail (row < table->nrows);
651   
652   if (table->rows[row].spacing != spacing)
653     {
654       table->rows[row].spacing = spacing;
655       
656       if (GTK_WIDGET_VISIBLE (table))
657         gtk_widget_queue_resize (GTK_WIDGET (table));
658     }
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
698 /**
699  * gtk_table_get_col_spacing:
700  * @table: a #GtkTable
701  * @column: a column in the table, 0 indicates the first column
702  *
703  * Gets the amount of space between column @col, and
704  * column @col + 1. See gtk_table_set_col_spacing().
705  *
706  * Return value: the column spacing
707  **/
708 guint
709 gtk_table_get_col_spacing (GtkTable *table,
710                            guint     column)
711 {
712   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
713   g_return_val_if_fail (column < table->ncols, 0);
714
715   return table->cols[column].spacing;
716 }
717
718 void
719 gtk_table_set_row_spacings (GtkTable *table,
720                             guint     spacing)
721 {
722   guint row;
723   
724   g_return_if_fail (GTK_IS_TABLE (table));
725   
726   table->row_spacing = spacing;
727   for (row = 0; row < table->nrows; row++)
728     table->rows[row].spacing = spacing;
729   
730   if (GTK_WIDGET_VISIBLE (table))
731     gtk_widget_queue_resize (GTK_WIDGET (table));
732
733   g_object_notify (G_OBJECT (table), "row_spacing");
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   g_object_notify (G_OBJECT (table), "column_spacing");
770 }
771
772 /**
773  * gtk_table_get_default_col_spacing:
774  * @table: a #GtkTable
775  *
776  * Gets the default column spacing for the table. This is
777  * the spacing that will be used for newly added columns.
778  * (See gtk_table_set_col_spacings())
779  *
780  * Returns value: the default column spacing
781  **/
782 guint
783 gtk_table_get_default_col_spacing (GtkTable *table)
784 {
785   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
786
787   return table->column_spacing;
788 }
789
790 void
791 gtk_table_set_homogeneous (GtkTable *table,
792                            gboolean  homogeneous)
793 {
794   g_return_if_fail (GTK_IS_TABLE (table));
795
796   homogeneous = (homogeneous != 0);
797   if (homogeneous != table->homogeneous)
798     {
799       table->homogeneous = homogeneous;
800       
801       if (GTK_WIDGET_VISIBLE (table))
802         gtk_widget_queue_resize (GTK_WIDGET (table));
803
804       g_object_notify (G_OBJECT (table), "homogeneous");
805     }
806 }
807
808 /**
809  * gtk_table_get_homogeneous:
810  * @table: a #GtkTable
811  *
812  * Returns whether the table cells are all constrained to the same
813  * width and height. (See gtk_table_set_homogenous ())
814  *
815  * Return value: %TRUE if the cells are all constrained to the same size
816  **/
817 gboolean
818 gtk_table_get_homogeneous (GtkTable *table)
819 {
820   g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
821
822   return table->homogeneous;
823 }
824
825 static void
826 gtk_table_finalize (GObject *object)
827 {
828   GtkTable *table;
829   
830   g_return_if_fail (GTK_IS_TABLE (object));
831   
832   table = GTK_TABLE (object);
833   
834   g_free (table->rows);
835   g_free (table->cols);
836   
837   G_OBJECT_CLASS (parent_class)->finalize (object);
838 }
839
840 static void
841 gtk_table_size_request (GtkWidget      *widget,
842                         GtkRequisition *requisition)
843 {
844   GtkTable *table;
845   gint row, col;
846   
847   g_return_if_fail (GTK_IS_TABLE (widget));
848   g_return_if_fail (requisition != NULL);
849   
850   table = GTK_TABLE (widget);
851   
852   requisition->width = 0;
853   requisition->height = 0;
854   
855   gtk_table_size_request_init (table);
856   gtk_table_size_request_pass1 (table);
857   gtk_table_size_request_pass2 (table);
858   gtk_table_size_request_pass3 (table);
859   gtk_table_size_request_pass2 (table);
860   
861   for (col = 0; col < table->ncols; col++)
862     requisition->width += table->cols[col].requisition;
863   for (col = 0; col + 1 < table->ncols; col++)
864     requisition->width += table->cols[col].spacing;
865   
866   for (row = 0; row < table->nrows; row++)
867     requisition->height += table->rows[row].requisition;
868   for (row = 0; row + 1 < table->nrows; row++)
869     requisition->height += table->rows[row].spacing;
870   
871   requisition->width += GTK_CONTAINER (table)->border_width * 2;
872   requisition->height += GTK_CONTAINER (table)->border_width * 2;
873 }
874
875 static void
876 gtk_table_size_allocate (GtkWidget     *widget,
877                          GtkAllocation *allocation)
878 {
879   GtkTable *table;
880   
881   g_return_if_fail (GTK_IS_TABLE (widget));
882   g_return_if_fail (allocation != NULL);
883   
884   widget->allocation = *allocation;
885   table = GTK_TABLE (widget);
886   
887   gtk_table_size_allocate_init (table);
888   gtk_table_size_allocate_pass1 (table);
889   gtk_table_size_allocate_pass2 (table);
890 }
891
892 static void
893 gtk_table_add (GtkContainer *container,
894                GtkWidget    *widget)
895 {
896   g_return_if_fail (GTK_IS_TABLE (container));
897   g_return_if_fail (widget != NULL);
898   
899   gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
900 }
901
902 static void
903 gtk_table_remove (GtkContainer *container,
904                   GtkWidget    *widget)
905 {
906   GtkTable *table;
907   GtkTableChild *child;
908   GList *children;
909   
910   g_return_if_fail (GTK_IS_TABLE (container));
911   g_return_if_fail (widget != NULL);
912   
913   table = GTK_TABLE (container);
914   children = table->children;
915   
916   while (children)
917     {
918       child = children->data;
919       children = children->next;
920       
921       if (child->widget == widget)
922         {
923           gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
924           
925           gtk_widget_unparent (widget);
926           
927           table->children = g_list_remove (table->children, child);
928           g_free (child);
929           
930           if (was_visible && GTK_WIDGET_VISIBLE (container))
931             gtk_widget_queue_resize (GTK_WIDGET (container));
932           break;
933         }
934     }
935 }
936
937 static void
938 gtk_table_forall (GtkContainer *container,
939                   gboolean      include_internals,
940                   GtkCallback   callback,
941                   gpointer      callback_data)
942 {
943   GtkTable *table;
944   GtkTableChild *child;
945   GList *children;
946   
947   g_return_if_fail (GTK_IS_TABLE (container));
948   g_return_if_fail (callback != NULL);
949   
950   table = GTK_TABLE (container);
951   children = table->children;
952   
953   while (children)
954     {
955       child = children->data;
956       children = children->next;
957       
958       (* callback) (child->widget, callback_data);
959     }
960 }
961
962 static void
963 gtk_table_size_request_init (GtkTable *table)
964 {
965   GtkTableChild *child;
966   GList *children;
967   gint row, col;
968   
969   for (row = 0; row < table->nrows; row++)
970     {
971       table->rows[row].requisition = 0;
972       table->rows[row].expand = FALSE;
973     }
974   for (col = 0; col < table->ncols; col++)
975     {
976       table->cols[col].requisition = 0;
977       table->cols[col].expand = FALSE;
978     }
979   
980   children = table->children;
981   while (children)
982     {
983       child = children->data;
984       children = children->next;
985       
986       if (GTK_WIDGET_VISIBLE (child->widget))
987         gtk_widget_size_request (child->widget, NULL);
988
989       if (child->left_attach == (child->right_attach - 1) && child->xexpand)
990         table->cols[child->left_attach].expand = TRUE;
991       
992       if (child->top_attach == (child->bottom_attach - 1) && child->yexpand)
993         table->rows[child->top_attach].expand = TRUE;
994     }
995 }
996
997 static void
998 gtk_table_size_request_pass1 (GtkTable *table)
999 {
1000   GtkTableChild *child;
1001   GList *children;
1002   gint width;
1003   gint height;
1004   
1005   children = table->children;
1006   while (children)
1007     {
1008       child = children->data;
1009       children = children->next;
1010       
1011       if (GTK_WIDGET_VISIBLE (child->widget))
1012         {
1013           GtkRequisition child_requisition;
1014           gtk_widget_get_child_requisition (child->widget, &child_requisition);
1015
1016           /* Child spans a single column.
1017            */
1018           if (child->left_attach == (child->right_attach - 1))
1019             {
1020               width = child_requisition.width + child->xpadding * 2;
1021               table->cols[child->left_attach].requisition = MAX (table->cols[child->left_attach].requisition, width);
1022             }
1023           
1024           /* Child spans a single row.
1025            */
1026           if (child->top_attach == (child->bottom_attach - 1))
1027             {
1028               height = child_requisition.height + child->ypadding * 2;
1029               table->rows[child->top_attach].requisition = MAX (table->rows[child->top_attach].requisition, height);
1030             }
1031         }
1032     }
1033 }
1034
1035 static void
1036 gtk_table_size_request_pass2 (GtkTable *table)
1037 {
1038   gint max_width;
1039   gint max_height;
1040   gint row, col;
1041   
1042   if (table->homogeneous)
1043     {
1044       max_width = 0;
1045       max_height = 0;
1046       
1047       for (col = 0; col < table->ncols; col++)
1048         max_width = MAX (max_width, table->cols[col].requisition);
1049       for (row = 0; row < table->nrows; row++)
1050         max_height = MAX (max_height, table->rows[row].requisition);
1051       
1052       for (col = 0; col < table->ncols; col++)
1053         table->cols[col].requisition = max_width;
1054       for (row = 0; row < table->nrows; row++)
1055         table->rows[row].requisition = max_height;
1056     }
1057 }
1058
1059 static void
1060 gtk_table_size_request_pass3 (GtkTable *table)
1061 {
1062   GtkTableChild *child;
1063   GList *children;
1064   gint width, height;
1065   gint row, col;
1066   gint extra;
1067   
1068   children = table->children;
1069   while (children)
1070     {
1071       child = children->data;
1072       children = children->next;
1073       
1074       if (GTK_WIDGET_VISIBLE (child->widget))
1075         {
1076           /* Child spans multiple columns.
1077            */
1078           if (child->left_attach != (child->right_attach - 1))
1079             {
1080               GtkRequisition child_requisition;
1081
1082               gtk_widget_get_child_requisition (child->widget, &child_requisition);
1083               
1084               /* Check and see if there is already enough space
1085                *  for the child.
1086                */
1087               width = 0;
1088               for (col = child->left_attach; col < child->right_attach; col++)
1089                 {
1090                   width += table->cols[col].requisition;
1091                   if ((col + 1) < child->right_attach)
1092                     width += table->cols[col].spacing;
1093                 }
1094               
1095               /* If we need to request more space for this child to fill
1096                *  its requisition, then divide up the needed space amongst the
1097                *  columns it spans, favoring expandable columns if any.
1098                */
1099               if (width < child_requisition.width + child->xpadding * 2)
1100                 {
1101                   gint n_expand = 0;
1102                   gboolean force_expand = FALSE;
1103                   
1104                   width = child_requisition.width + child->xpadding * 2 - width;
1105
1106                   for (col = child->left_attach; col < child->right_attach; col++)
1107                     if (table->cols[col].expand)
1108                       n_expand++;
1109
1110                   if (n_expand == 0)
1111                     {
1112                       n_expand = (child->right_attach - child->left_attach);
1113                       force_expand = TRUE;
1114                     }
1115                     
1116                   for (col = child->left_attach; col < child->right_attach; col++)
1117                     if (force_expand || table->cols[col].expand)
1118                       {
1119                         extra = width / n_expand;
1120                         table->cols[col].requisition += extra;
1121                         width -= extra;
1122                         n_expand--;
1123                       }
1124                 }
1125             }
1126           
1127           /* Child spans multiple rows.
1128            */
1129           if (child->top_attach != (child->bottom_attach - 1))
1130             {
1131               GtkRequisition child_requisition;
1132
1133               gtk_widget_get_child_requisition (child->widget, &child_requisition);
1134
1135               /* Check and see if there is already enough space
1136                *  for the child.
1137                */
1138               height = 0;
1139               for (row = child->top_attach; row < child->bottom_attach; row++)
1140                 {
1141                   height += table->rows[row].requisition;
1142                   if ((row + 1) < child->bottom_attach)
1143                     height += table->rows[row].spacing;
1144                 }
1145               
1146               /* If we need to request more space for this child to fill
1147                *  its requisition, then divide up the needed space amongst the
1148                *  rows it spans, favoring expandable rows if any.
1149                */
1150               if (height < child_requisition.height + child->ypadding * 2)
1151                 {
1152                   gint n_expand = 0;
1153                   gboolean force_expand = FALSE;
1154                   
1155                   height = child_requisition.height + child->ypadding * 2 - height;
1156                   
1157                   for (row = child->top_attach; row < child->bottom_attach; row++)
1158                     {
1159                       if (table->rows[row].expand)
1160                         n_expand++;
1161                     }
1162
1163                   if (n_expand == 0)
1164                     {
1165                       n_expand = (child->bottom_attach - child->top_attach);
1166                       force_expand = TRUE;
1167                     }
1168                     
1169                   for (row = child->top_attach; row < child->bottom_attach; row++)
1170                     if (force_expand || table->rows[row].expand)
1171                       {
1172                         extra = height / n_expand;
1173                         table->rows[row].requisition += extra;
1174                         height -= extra;
1175                         n_expand--;
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       if (!table->children)
1399         nexpand = 1;
1400       else
1401         {
1402           nexpand = 0;
1403           for (col = 0; col < table->ncols; col++)
1404             if (table->cols[col].expand)
1405               {
1406                 nexpand += 1;
1407                 break;
1408               }
1409         }
1410       if (nexpand)
1411         {
1412           width = real_width;
1413           for (col = 0; col + 1 < table->ncols; col++)
1414             width -= table->cols[col].spacing;
1415           
1416           for (col = 0; col < table->ncols; col++)
1417             {
1418               extra = width / (table->ncols - col);
1419               table->cols[col].allocation = MAX (1, extra);
1420               width -= extra;
1421             }
1422         }
1423     }
1424   else
1425     {
1426       width = 0;
1427       nexpand = 0;
1428       nshrink = 0;
1429       
1430       for (col = 0; col < table->ncols; col++)
1431         {
1432           width += table->cols[col].requisition;
1433           if (table->cols[col].expand)
1434             nexpand += 1;
1435           if (table->cols[col].shrink)
1436             nshrink += 1;
1437         }
1438       for (col = 0; col + 1 < table->ncols; col++)
1439         width += table->cols[col].spacing;
1440       
1441       /* Check to see if we were allocated more width than we requested.
1442        */
1443       if ((width < real_width) && (nexpand >= 1))
1444         {
1445           width = real_width - width;
1446           
1447           for (col = 0; col < table->ncols; col++)
1448             if (table->cols[col].expand)
1449               {
1450                 extra = width / nexpand;
1451                 table->cols[col].allocation += extra;
1452                 
1453                 width -= extra;
1454                 nexpand -= 1;
1455               }
1456         }
1457       
1458       /* Check to see if we were allocated less width than we requested,
1459        * then shrink until we fit the size give.
1460        */
1461       if (width > real_width)
1462         {
1463           gint total_nshrink = nshrink;
1464
1465           extra = width - real_width;
1466           while (total_nshrink > 0 && extra > 0)
1467             {
1468               nshrink = total_nshrink;
1469               for (col = 0; col < table->ncols; col++)
1470                 if (table->cols[col].shrink)
1471                   {
1472                     gint allocation = table->cols[col].allocation;
1473
1474                     table->cols[col].allocation = MAX (1, (gint) table->cols[col].allocation - extra / nshrink);
1475                     extra -= allocation - table->cols[col].allocation;
1476                     nshrink -= 1;
1477                     if (table->cols[col].allocation < 2)
1478                       {
1479                         total_nshrink -= 1;
1480                         table->cols[col].shrink = FALSE;
1481                       }
1482                   }
1483             }
1484         }
1485     }
1486   
1487   if (table->homogeneous)
1488     {
1489       if (!table->children)
1490         nexpand = 1;
1491       else
1492         {
1493           nexpand = 0;
1494           for (row = 0; row < table->nrows; row++)
1495             if (table->rows[row].expand)
1496               {
1497                 nexpand += 1;
1498                 break;
1499               }
1500         }
1501       if (nexpand)
1502         {
1503           height = real_height;
1504           
1505           for (row = 0; row + 1 < table->nrows; row++)
1506             height -= table->rows[row].spacing;
1507           
1508           
1509           for (row = 0; row < table->nrows; row++)
1510             {
1511               extra = height / (table->nrows - row);
1512               table->rows[row].allocation = MAX (1, extra);
1513               height -= extra;
1514             }
1515         }
1516     }
1517   else
1518     {
1519       height = 0;
1520       nexpand = 0;
1521       nshrink = 0;
1522       
1523       for (row = 0; row < table->nrows; row++)
1524         {
1525           height += table->rows[row].requisition;
1526           if (table->rows[row].expand)
1527             nexpand += 1;
1528           if (table->rows[row].shrink)
1529             nshrink += 1;
1530         }
1531       for (row = 0; row + 1 < table->nrows; row++)
1532         height += table->rows[row].spacing;
1533       
1534       /* Check to see if we were allocated more height than we requested.
1535        */
1536       if ((height < real_height) && (nexpand >= 1))
1537         {
1538           height = real_height - height;
1539           
1540           for (row = 0; row < table->nrows; row++)
1541             if (table->rows[row].expand)
1542               {
1543                 extra = height / nexpand;
1544                 table->rows[row].allocation += extra;
1545                 
1546                 height -= extra;
1547                 nexpand -= 1;
1548               }
1549         }
1550       
1551       /* Check to see if we were allocated less height than we requested.
1552        * then shrink until we fit the size give.
1553        */
1554       if (height > real_height)
1555         {
1556           gint total_nshrink = nshrink;
1557           
1558           extra = height - real_height;
1559           while (total_nshrink > 0 && extra > 0)
1560             {
1561               nshrink = total_nshrink;
1562               for (row = 0; row < table->nrows; row++)
1563                 if (table->rows[row].shrink)
1564                   {
1565                     gint allocation = table->rows[row].allocation;
1566                     
1567                     table->rows[row].allocation = MAX (1, (gint) table->rows[row].allocation - extra / nshrink);
1568                     extra -= allocation - table->rows[row].allocation;
1569                     nshrink -= 1;
1570                     if (table->rows[row].allocation < 2)
1571                       {
1572                         total_nshrink -= 1;
1573                         table->rows[row].shrink = FALSE;
1574                       }
1575                   }
1576             }
1577         }
1578     }
1579 }
1580
1581 static void
1582 gtk_table_size_allocate_pass2 (GtkTable *table)
1583 {
1584   GtkTableChild *child;
1585   GList *children;
1586   gint max_width;
1587   gint max_height;
1588   gint x, y;
1589   gint row, col;
1590   GtkAllocation allocation;
1591   GtkWidget *widget = GTK_WIDGET (table);
1592   
1593   children = table->children;
1594   while (children)
1595     {
1596       child = children->data;
1597       children = children->next;
1598       
1599       if (GTK_WIDGET_VISIBLE (child->widget))
1600         {
1601           GtkRequisition child_requisition;
1602           gtk_widget_get_child_requisition (child->widget, &child_requisition);
1603
1604           x = GTK_WIDGET (table)->allocation.x + GTK_CONTAINER (table)->border_width;
1605           y = GTK_WIDGET (table)->allocation.y + GTK_CONTAINER (table)->border_width;
1606           max_width = 0;
1607           max_height = 0;
1608           
1609           for (col = 0; col < child->left_attach; col++)
1610             {
1611               x += table->cols[col].allocation;
1612               x += table->cols[col].spacing;
1613             }
1614           
1615           for (col = child->left_attach; col < child->right_attach; col++)
1616             {
1617               max_width += table->cols[col].allocation;
1618               if ((col + 1) < child->right_attach)
1619                 max_width += table->cols[col].spacing;
1620             }
1621           
1622           for (row = 0; row < child->top_attach; row++)
1623             {
1624               y += table->rows[row].allocation;
1625               y += table->rows[row].spacing;
1626             }
1627           
1628           for (row = child->top_attach; row < child->bottom_attach; row++)
1629             {
1630               max_height += table->rows[row].allocation;
1631               if ((row + 1) < child->bottom_attach)
1632                 max_height += table->rows[row].spacing;
1633             }
1634           
1635           if (child->xfill)
1636             {
1637               allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1638               allocation.x = x + (max_width - allocation.width) / 2;
1639             }
1640           else
1641             {
1642               allocation.width = child_requisition.width;
1643               allocation.x = x + (max_width - allocation.width) / 2;
1644             }
1645           
1646           if (child->yfill)
1647             {
1648               allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1649               allocation.y = y + (max_height - allocation.height) / 2;
1650             }
1651           else
1652             {
1653               allocation.height = child_requisition.height;
1654               allocation.y = y + (max_height - allocation.height) / 2;
1655             }
1656
1657           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1658             allocation.x = widget->allocation.x + widget->allocation.width
1659               - (allocation.x - widget->allocation.x) - allocation.width;
1660           
1661           gtk_widget_size_allocate (child->widget, &allocation);
1662         }
1663     }
1664 }