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