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