]> Pileus Git - ~andy/gtk/blob - gtk/deprecated/gtktable.c
Change FSF Address
[~andy/gtk] / gtk / deprecated / 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #define GDK_DISABLE_DEPRECATION_WARNINGS
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  * #GtkTable has been deprecated. Use #GtkGrid instead. It provides the same
65  * capabilities as GtkTable for arranging widgets in a rectangular grid, but
66  * does support height-for-width 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  * Deprecated: 3.4: Use gtk_grid_new().
605  */
606 GtkWidget*
607 gtk_table_new (guint    rows,
608                guint    columns,
609                gboolean homogeneous)
610 {
611   GtkTable *table;
612   GtkTablePrivate *priv;
613
614   if (rows == 0)
615     rows = 1;
616   if (columns == 0)
617     columns = 1;
618   
619   table = g_object_new (GTK_TYPE_TABLE, NULL);
620   priv = table->priv;
621
622   priv->homogeneous = (homogeneous ? TRUE : FALSE);
623
624   gtk_table_resize (table, rows, columns);
625   
626   return GTK_WIDGET (table);
627 }
628
629 /**
630  * gtk_table_resize:
631  * @table: The #GtkTable you wish to change the size of.
632  * @rows: The new number of rows.
633  * @columns: The new number of columns.
634  *
635  * If you need to change a table's size <emphasis>after</emphasis>
636  * it has been created, this function allows you to do so.
637  *
638  * Deprecated: 3.4: #GtkGrid resizes automatically.
639  */
640 void
641 gtk_table_resize (GtkTable *table,
642                   guint     n_rows,
643                   guint     n_cols)
644 {
645   GtkTablePrivate *priv;
646
647   g_return_if_fail (GTK_IS_TABLE (table));
648   g_return_if_fail (n_rows > 0 && n_rows <= 65535);
649   g_return_if_fail (n_cols > 0 && n_cols <= 65535);
650
651   priv = table->priv;
652
653   n_rows = MAX (n_rows, 1);
654   n_cols = MAX (n_cols, 1);
655
656   if (n_rows != priv->nrows ||
657       n_cols != priv->ncols)
658     {
659       GList *list;
660       
661       for (list = priv->children; list; list = list->next)
662         {
663           GtkTableChild *child;
664           
665           child = list->data;
666           
667           n_rows = MAX (n_rows, child->bottom_attach);
668           n_cols = MAX (n_cols, child->right_attach);
669         }
670
671       if (n_rows != priv->nrows)
672         {
673           guint i;
674
675           i = priv->nrows;
676           priv->nrows = n_rows;
677           priv->rows = g_realloc (priv->rows, priv->nrows * sizeof (GtkTableRowCol));
678
679           for (; i < priv->nrows; i++)
680             {
681               priv->rows[i].requisition = 0;
682               priv->rows[i].allocation = 0;
683               priv->rows[i].spacing = priv->row_spacing;
684               priv->rows[i].need_expand = 0;
685               priv->rows[i].need_shrink = 0;
686               priv->rows[i].expand = 0;
687               priv->rows[i].shrink = 0;
688             }
689
690           g_object_notify (G_OBJECT (table), "n-rows");
691         }
692
693       if (n_cols != priv->ncols)
694         {
695           guint i;
696
697           i = priv->ncols;
698           priv->ncols = n_cols;
699           priv->cols = g_realloc (priv->cols, priv->ncols * sizeof (GtkTableRowCol));
700
701           for (; i < priv->ncols; i++)
702             {
703               priv->cols[i].requisition = 0;
704               priv->cols[i].allocation = 0;
705               priv->cols[i].spacing = priv->column_spacing;
706               priv->cols[i].need_expand = 0;
707               priv->cols[i].need_shrink = 0;
708               priv->cols[i].expand = 0;
709               priv->cols[i].shrink = 0;
710             }
711
712           g_object_notify (G_OBJECT (table), "n-columns");
713         }
714     }
715 }
716
717 /**
718  * gtk_table_attach:
719  * @table: The #GtkTable to add a new widget to.
720  * @child: The widget to add.
721  * @left_attach: the column number to attach the left side of a child widget to.
722  * @right_attach: the column number to attach the right side of a child widget to.
723  * @top_attach: the row number to attach the top of a child widget to.
724  * @bottom_attach: the row number to attach the bottom of a child widget to.
725  * @xoptions: Used to specify the properties of the child widget when the table is resized.
726  * @yoptions: The same as xoptions, except this field determines behaviour of vertical resizing.
727  * @xpadding: An integer value specifying the padding on the left and right of the widget being added to the table.
728  * @ypadding: The amount of padding above and below the child widget.
729  *
730  * Adds a widget to a table. The number of 'cells' that a widget will occupy is
731  * specified by @left_attach, @right_attach, @top_attach and @bottom_attach.
732  * These each represent the leftmost, rightmost, uppermost and lowest column
733  * and row numbers of the table. (Columns and rows are indexed from zero).
734  *
735  * To make a button occupy the lower right cell of a 2x2 table, use
736  * <informalexample><programlisting>
737  * gtk_table_attach (table, button,
738  *                   1, 2, // left, right attach
739  *                   1, 2, // top, bottom attach
740  *                   xoptions, yoptions,
741  *                   xpadding, ypadding);
742  * </programlisting></informalexample>
743  * If you want to make the button span the entire bottom row, use @left_attach == 0 and @right_attach = 2 instead.
744  *
745  * Deprecated: 3.4: Use gtk_grid_attach() with #GtkGrid. Note that the attach
746  *     arguments differ between those two functions.
747  */
748 void
749 gtk_table_attach (GtkTable        *table,
750                   GtkWidget       *child,
751                   guint            left_attach,
752                   guint            right_attach,
753                   guint            top_attach,
754                   guint            bottom_attach,
755                   GtkAttachOptions xoptions,
756                   GtkAttachOptions yoptions,
757                   guint            xpadding,
758                   guint            ypadding)
759 {
760   GtkTablePrivate *priv;
761   GtkTableChild *table_child;
762
763   g_return_if_fail (GTK_IS_TABLE (table));
764   g_return_if_fail (GTK_IS_WIDGET (child));
765   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
766
767   /* g_return_if_fail (left_attach >= 0); */
768   g_return_if_fail (left_attach < right_attach);
769   /* g_return_if_fail (top_attach >= 0); */
770   g_return_if_fail (top_attach < bottom_attach);
771
772   priv = table->priv;
773
774   if (right_attach >= priv->ncols)
775     gtk_table_resize (table, priv->nrows, right_attach);
776
777   if (bottom_attach >= priv->nrows)
778     gtk_table_resize (table, bottom_attach, priv->ncols);
779
780   table_child = g_new (GtkTableChild, 1);
781   table_child->widget = child;
782   table_child->left_attach = left_attach;
783   table_child->right_attach = right_attach;
784   table_child->top_attach = top_attach;
785   table_child->bottom_attach = bottom_attach;
786   table_child->xexpand = (xoptions & GTK_EXPAND) != 0;
787   table_child->xshrink = (xoptions & GTK_SHRINK) != 0;
788   table_child->xfill = (xoptions & GTK_FILL) != 0;
789   table_child->xpadding = xpadding;
790   table_child->yexpand = (yoptions & GTK_EXPAND) != 0;
791   table_child->yshrink = (yoptions & GTK_SHRINK) != 0;
792   table_child->yfill = (yoptions & GTK_FILL) != 0;
793   table_child->ypadding = ypadding;
794
795   priv->children = g_list_prepend (priv->children, table_child);
796
797   gtk_widget_set_parent (child, GTK_WIDGET (table));
798 }
799
800 /**
801  * gtk_table_attach_defaults:
802  * @table: The table to add a new child widget to.
803  * @widget: The child widget to add.
804  * @left_attach: The column number to attach the left side of the child widget to.
805  * @right_attach: The column number to attach the right side of the child widget to.
806  * @top_attach: The row number to attach the top of the child widget to.
807  * @bottom_attach: The row number to attach the bottom of the child widget to.
808  *
809  * As there are many options associated with gtk_table_attach(), this convenience
810  * function provides the programmer with a means to add children to a table with
811  * identical padding and expansion options. The values used for the #GtkAttachOptions
812  * are <literal>GTK_EXPAND | GTK_FILL</literal>, and the padding is set to 0.
813  *
814  * Deprecated: 3.4: Use gtk_grid_attach() with #GtkGrid. Note that the attach
815  *     arguments differ between those two functions.
816  */
817 void
818 gtk_table_attach_defaults (GtkTable  *table,
819                            GtkWidget *widget,
820                            guint      left_attach,
821                            guint      right_attach,
822                            guint      top_attach,
823                            guint      bottom_attach)
824 {
825   gtk_table_attach (table, widget,
826                     left_attach, right_attach,
827                     top_attach, bottom_attach,
828                     GTK_EXPAND | GTK_FILL,
829                     GTK_EXPAND | GTK_FILL,
830                     0, 0);
831 }
832
833 /**
834  * gtk_table_set_row_spacing:
835  * @table: a #GtkTable containing the row whose properties you wish to change.
836  * @row: row number whose spacing will be changed.
837  * @spacing: number of pixels that the spacing should take up.
838  *
839  * Changes the space between a given table row and the subsequent row.
840  *
841  * Deprecated: 3.4: Use gtk_widget_set_margin_top() and
842  *     gtk_widget_set_margin_bottom() on the widgets contained in the row if
843  *     you need this functionality. #GtkGrid does not support per-row spacing.
844  */
845 void
846 gtk_table_set_row_spacing (GtkTable *table,
847                            guint     row,
848                            guint     spacing)
849 {
850   GtkTablePrivate *priv;
851
852   g_return_if_fail (GTK_IS_TABLE (table));
853
854   priv = table->priv;
855
856   g_return_if_fail (row < priv->nrows);
857
858   if (priv->rows[row].spacing != spacing)
859     {
860       priv->rows[row].spacing = spacing;
861
862       if (gtk_widget_get_visible (GTK_WIDGET (table)))
863         gtk_widget_queue_resize (GTK_WIDGET (table));
864     }
865 }
866
867 /**
868  * gtk_table_get_row_spacing:
869  * @table: a #GtkTable
870  * @row: a row in the table, 0 indicates the first row
871  *
872  * Gets the amount of space between row @row, and
873  * row @row + 1. See gtk_table_set_row_spacing().
874  *
875  * Return value: the row spacing
876  *
877  * Deprecated: 3.4: #GtkGrid does not offer a replacement for this
878  *     functionality.
879  **/
880 guint
881 gtk_table_get_row_spacing (GtkTable *table,
882                            guint     row)
883 {
884   GtkTablePrivate *priv;
885
886   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
887
888   priv = table->priv;
889
890   g_return_val_if_fail (row < priv->nrows - 1, 0);
891
892   return priv->rows[row].spacing;
893 }
894
895 /**
896  * gtk_table_set_col_spacing:
897  * @table: a #GtkTable.
898  * @column: the column whose spacing should be changed.
899  * @spacing: number of pixels that the spacing should take up.
900  *
901  * Alters the amount of space between a given table column and the following
902  * column.
903  *
904  * Deprecated: 3.4: Use gtk_widget_set_margin_left() and
905  *     gtk_widget_set_margin_right() on the widgets contained in the row if
906  *     you need this functionality. #GtkGrid does not support per-row spacing.
907  */
908 void
909 gtk_table_set_col_spacing (GtkTable *table,
910                            guint     column,
911                            guint     spacing)
912 {
913   GtkTablePrivate *priv;
914
915   g_return_if_fail (GTK_IS_TABLE (table));
916
917   priv = table->priv;
918
919   g_return_if_fail (column < priv->ncols);
920
921   if (priv->cols[column].spacing != spacing)
922     {
923       priv->cols[column].spacing = spacing;
924
925       if (gtk_widget_get_visible (GTK_WIDGET (table)))
926         gtk_widget_queue_resize (GTK_WIDGET (table));
927     }
928 }
929
930 /**
931  * gtk_table_get_col_spacing:
932  * @table: a #GtkTable
933  * @column: a column in the table, 0 indicates the first column
934  *
935  * Gets the amount of space between column @col, and
936  * column @col + 1. See gtk_table_set_col_spacing().
937  *
938  * Return value: the column spacing
939  *
940  * Deprecated: 3.4: #GtkGrid does not offer a replacement for this
941  *     functionality.
942  **/
943 guint
944 gtk_table_get_col_spacing (GtkTable *table,
945                            guint     column)
946 {
947   GtkTablePrivate *priv;
948
949   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
950
951   priv = table->priv;
952
953   g_return_val_if_fail (column < priv->ncols, 0);
954
955   return priv->cols[column].spacing;
956 }
957
958 /**
959  * gtk_table_set_row_spacings:
960  * @table: a #GtkTable.
961  * @spacing: the number of pixels of space to place between every row in the table.
962  *
963  * Sets the space between every row in @table equal to @spacing.
964  *
965  * Deprecated: 3.4: Use gtk_grid_set_row_spacing() with #GtkGrid.
966  */
967 void
968 gtk_table_set_row_spacings (GtkTable *table,
969                             guint     spacing)
970 {
971   GtkTablePrivate *priv;
972   guint row;
973   
974   g_return_if_fail (GTK_IS_TABLE (table));
975
976   priv = table->priv;
977
978   priv->row_spacing = spacing;
979   for (row = 0; row < priv->nrows; row++)
980     priv->rows[row].spacing = spacing;
981
982   if (gtk_widget_get_visible (GTK_WIDGET (table)))
983     gtk_widget_queue_resize (GTK_WIDGET (table));
984
985   g_object_notify (G_OBJECT (table), "row-spacing");
986 }
987
988 /**
989  * gtk_table_get_default_row_spacing:
990  * @table: a #GtkTable
991  *
992  * Gets the default row spacing for the table. This is
993  * the spacing that will be used for newly added rows.
994  * (See gtk_table_set_row_spacings())
995  *
996  * Return value: the default row spacing
997  *
998  * Deprecated: 3.4: Use gtk_grid_get_row_spacing() with #GtkGrid.
999  **/
1000 guint
1001 gtk_table_get_default_row_spacing (GtkTable *table)
1002 {
1003   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
1004
1005   return table->priv->row_spacing;
1006 }
1007
1008 /**
1009  * gtk_table_set_col_spacings:
1010  * @table: a #GtkTable.
1011  * @spacing: the number of pixels of space to place between every column
1012  *   in the table.
1013  *
1014  * Sets the space between every column in @table equal to @spacing.
1015  *
1016  * Deprecated: 3.4: Use gtk_grid_set_column_spacing() with #GtkGrid.
1017  */
1018 void
1019 gtk_table_set_col_spacings (GtkTable *table,
1020                             guint     spacing)
1021 {
1022   GtkTablePrivate *priv;
1023   guint col;
1024
1025   g_return_if_fail (GTK_IS_TABLE (table));
1026
1027   priv = table->priv;
1028
1029   priv->column_spacing = spacing;
1030   for (col = 0; col < priv->ncols; col++)
1031     priv->cols[col].spacing = spacing;
1032
1033   if (gtk_widget_get_visible (GTK_WIDGET (table)))
1034     gtk_widget_queue_resize (GTK_WIDGET (table));
1035
1036   g_object_notify (G_OBJECT (table), "column-spacing");
1037 }
1038
1039 /**
1040  * gtk_table_get_default_col_spacing:
1041  * @table: a #GtkTable
1042  *
1043  * Gets the default column spacing for the table. This is
1044  * the spacing that will be used for newly added columns.
1045  * (See gtk_table_set_col_spacings())
1046  *
1047  * Return value: the default column spacing
1048  *
1049  * Deprecated: 3.4: Use gtk_grid_get_column_spacing() with #GtkGrid.
1050  **/
1051 guint
1052 gtk_table_get_default_col_spacing (GtkTable *table)
1053 {
1054   g_return_val_if_fail (GTK_IS_TABLE (table), 0);
1055
1056   return table->priv->column_spacing;
1057 }
1058
1059 /**
1060  * gtk_table_set_homogeneous:
1061  * @table: The #GtkTable you wish to set the homogeneous properties of.
1062  * @homogeneous: Set to %TRUE to ensure all table cells are the same size. Set
1063  *   to %FALSE if this is not your desired behaviour.
1064  *
1065  * Changes the homogenous property of table cells, ie. whether all cells are
1066  * an equal size or not.
1067  *
1068  * Deprecated: 3.4: Use gtk_grid_set_row_homogeneous() and
1069  *     gtk_grid_set_column_homogeneous() with #GtkGrid.
1070  */
1071 void
1072 gtk_table_set_homogeneous (GtkTable *table,
1073                            gboolean  homogeneous)
1074 {
1075   GtkTablePrivate *priv;
1076
1077   g_return_if_fail (GTK_IS_TABLE (table));
1078
1079   priv = table->priv;
1080
1081   homogeneous = (homogeneous != 0);
1082   if (homogeneous != priv->homogeneous)
1083     {
1084       priv->homogeneous = homogeneous;
1085       
1086       if (gtk_widget_get_visible (GTK_WIDGET (table)))
1087         gtk_widget_queue_resize (GTK_WIDGET (table));
1088
1089       g_object_notify (G_OBJECT (table), "homogeneous");
1090     }
1091 }
1092
1093 /**
1094  * gtk_table_get_homogeneous:
1095  * @table: a #GtkTable
1096  *
1097  * Returns whether the table cells are all constrained to the same
1098  * width and height. (See gtk_table_set_homogenous ())
1099  *
1100  * Return value: %TRUE if the cells are all constrained to the same size
1101  *
1102  * Deprecated: 3.4: Use gtk_grid_get_row_homogeneous() and
1103  *     gtk_grid_get_column_homogeneous() with #GtkGrid.
1104  **/
1105 gboolean
1106 gtk_table_get_homogeneous (GtkTable *table)
1107 {
1108   g_return_val_if_fail (GTK_IS_TABLE (table), FALSE);
1109
1110   return table->priv->homogeneous;
1111 }
1112
1113 /**
1114  * gtk_table_get_size:
1115  * @table: a #GtkTable
1116  * @rows: (out) (allow-none): return location for the number of
1117  *   rows, or %NULL
1118  * @columns: (out) (allow-none): return location for the number
1119  *   of columns, or %NULL
1120  *
1121  * Gets the number of rows and columns in the table.
1122  *
1123  * Since: 2.22
1124  *
1125  * Deprecated: 3.4: #GtkGrid does not expose the number of columns and
1126  *     rows.
1127  **/
1128 void
1129 gtk_table_get_size (GtkTable *table,
1130                     guint    *rows,
1131                     guint    *columns)
1132 {
1133   GtkTablePrivate *priv;
1134
1135   g_return_if_fail (GTK_IS_TABLE (table));
1136
1137   priv = table->priv;
1138
1139   if (rows)
1140     *rows = priv->nrows;
1141
1142   if (columns)
1143     *columns = priv->ncols;
1144 }
1145
1146 static void
1147 gtk_table_finalize (GObject *object)
1148 {
1149   GtkTable *table = GTK_TABLE (object);
1150   GtkTablePrivate *priv = table->priv;
1151
1152   g_free (priv->rows);
1153   g_free (priv->cols);
1154
1155   G_OBJECT_CLASS (gtk_table_parent_class)->finalize (object);
1156 }
1157
1158 static void
1159 gtk_table_get_preferred_width (GtkWidget *widget,
1160                                gint      *minimum,
1161                                gint      *natural)
1162 {
1163   GtkTable *table = GTK_TABLE (widget);
1164   GtkTablePrivate *priv = table->priv;
1165   gint col;
1166
1167   gtk_table_size_request_init (table);
1168   gtk_table_size_request_pass1 (table);
1169   gtk_table_size_request_pass2 (table);
1170   gtk_table_size_request_pass3 (table);
1171   gtk_table_size_request_pass2 (table);
1172
1173   *minimum = 0;
1174
1175   for (col = 0; col < priv->ncols; col++)
1176     *minimum += priv->cols[col].requisition;
1177   for (col = 0; col + 1 < priv->ncols; col++)
1178     *minimum += priv->cols[col].spacing;
1179
1180   *natural = *minimum;
1181 }
1182
1183 static void
1184 gtk_table_get_preferred_height (GtkWidget *widget,
1185                                 gint      *minimum,
1186                                 gint      *natural)
1187 {
1188   GtkTable *table = GTK_TABLE (widget);
1189   GtkTablePrivate *priv = table->priv;
1190   gint row;
1191
1192   gtk_table_size_request_init (table);
1193   gtk_table_size_request_pass1 (table);
1194   gtk_table_size_request_pass2 (table);
1195   gtk_table_size_request_pass3 (table);
1196   gtk_table_size_request_pass2 (table);
1197
1198   *minimum = 0;
1199   for (row = 0; row < priv->nrows; row++)
1200     *minimum += priv->rows[row].requisition;
1201   for (row = 0; row + 1 < priv->nrows; row++)
1202     *minimum += priv->rows[row].spacing;
1203
1204   *natural = *minimum;
1205 }
1206
1207 static void
1208 gtk_table_size_allocate (GtkWidget     *widget,
1209                          GtkAllocation *allocation)
1210 {
1211   GtkTable *table = GTK_TABLE (widget);
1212
1213   gtk_widget_set_allocation (widget, allocation);
1214
1215   gtk_table_size_allocate_init (table);
1216   gtk_table_size_allocate_pass1 (table);
1217   gtk_table_size_allocate_pass2 (table);
1218 }
1219
1220 static void
1221 gtk_table_add (GtkContainer *container,
1222                GtkWidget    *widget)
1223 {
1224   gtk_table_attach_defaults (GTK_TABLE (container), widget, 0, 1, 0, 1);
1225 }
1226
1227 static void
1228 gtk_table_remove (GtkContainer *container,
1229                   GtkWidget    *widget)
1230 {
1231   GtkTable *table = GTK_TABLE (container);
1232   GtkTablePrivate *priv = table->priv;
1233   GtkTableChild *child;
1234   GtkWidget *widget_container = GTK_WIDGET (container);
1235   GList *children;
1236
1237   children = priv->children;
1238
1239   while (children)
1240     {
1241       child = children->data;
1242       children = children->next;
1243       
1244       if (child->widget == widget)
1245         {
1246           gboolean was_visible = gtk_widget_get_visible (widget);
1247           
1248           gtk_widget_unparent (widget);
1249
1250           priv->children = g_list_remove (priv->children, child);
1251           g_free (child);
1252           
1253           if (was_visible && gtk_widget_get_visible (widget_container))
1254             gtk_widget_queue_resize (widget_container);
1255           break;
1256         }
1257     }
1258 }
1259
1260 static void
1261 gtk_table_forall (GtkContainer *container,
1262                   gboolean      include_internals,
1263                   GtkCallback   callback,
1264                   gpointer      callback_data)
1265 {
1266   GtkTable *table = GTK_TABLE (container);
1267   GtkTablePrivate *priv = table->priv;
1268   GtkTableChild *child;
1269   GList *children;
1270
1271   children = priv->children;
1272
1273   while (children)
1274     {
1275       child = children->data;
1276       children = children->next;
1277       
1278       (* callback) (child->widget, callback_data);
1279     }
1280 }
1281
1282 static void
1283 gtk_table_size_request_init (GtkTable *table)
1284 {
1285   GtkTablePrivate *priv = table->priv;
1286   GtkTableChild *child;
1287   GList *children;
1288   gint row, col;
1289
1290   for (row = 0; row < priv->nrows; row++)
1291     {
1292       priv->rows[row].requisition = 0;
1293       priv->rows[row].expand = FALSE;
1294     }
1295   for (col = 0; col < priv->ncols; col++)
1296     {
1297       priv->cols[col].requisition = 0;
1298       priv->cols[col].expand = FALSE;
1299     }
1300   
1301   children = priv->children;
1302   while (children)
1303     {
1304       child = children->data;
1305       children = children->next;
1306
1307       if (child->left_attach == (child->right_attach - 1) &&
1308           (child->xexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL)))
1309         priv->cols[child->left_attach].expand = TRUE;
1310       
1311       if (child->top_attach == (child->bottom_attach - 1) &&
1312           (child->yexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_VERTICAL)))
1313         priv->rows[child->top_attach].expand = TRUE;
1314     }
1315 }
1316
1317 static void
1318 gtk_table_size_request_pass1 (GtkTable *table)
1319 {
1320   GtkTablePrivate *priv = table->priv;
1321   GtkTableChild *child;
1322   GList *children;
1323   gint width;
1324   gint height;
1325
1326   children = priv->children;
1327   while (children)
1328     {
1329       child = children->data;
1330       children = children->next;
1331       
1332       if (gtk_widget_get_visible (child->widget))
1333         {
1334           GtkRequisition child_requisition;
1335
1336           gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
1337
1338           /* Child spans a single column.
1339            */
1340           if (child->left_attach == (child->right_attach - 1))
1341             {
1342               width = child_requisition.width + child->xpadding * 2;
1343               priv->cols[child->left_attach].requisition = MAX (priv->cols[child->left_attach].requisition, width);
1344             }
1345           
1346           /* Child spans a single row.
1347            */
1348           if (child->top_attach == (child->bottom_attach - 1))
1349             {
1350               height = child_requisition.height + child->ypadding * 2;
1351               priv->rows[child->top_attach].requisition = MAX (priv->rows[child->top_attach].requisition, height);
1352             }
1353         }
1354     }
1355 }
1356
1357 static void
1358 gtk_table_size_request_pass2 (GtkTable *table)
1359 {
1360   GtkTablePrivate *priv = table->priv;
1361   gint max_width;
1362   gint max_height;
1363   gint row, col;
1364
1365   if (priv->homogeneous)
1366     {
1367       max_width = 0;
1368       max_height = 0;
1369
1370       for (col = 0; col < priv->ncols; col++)
1371         max_width = MAX (max_width, priv->cols[col].requisition);
1372       for (row = 0; row < priv->nrows; row++)
1373         max_height = MAX (max_height, priv->rows[row].requisition);
1374
1375       for (col = 0; col < priv->ncols; col++)
1376         priv->cols[col].requisition = max_width;
1377       for (row = 0; row < priv->nrows; row++)
1378         priv->rows[row].requisition = max_height;
1379     }
1380 }
1381
1382 static void
1383 gtk_table_size_request_pass3 (GtkTable *table)
1384 {
1385   GtkTablePrivate *priv = table->priv;
1386   GtkTableChild *child;
1387   GList *children;
1388   gint width, height;
1389   gint row, col;
1390   gint extra;
1391
1392   children = priv->children;
1393   while (children)
1394     {
1395       child = children->data;
1396       children = children->next;
1397       
1398       if (gtk_widget_get_visible (child->widget))
1399         {
1400           /* Child spans multiple columns.
1401            */
1402           if (child->left_attach != (child->right_attach - 1))
1403             {
1404               GtkRequisition child_requisition;
1405
1406               gtk_widget_get_preferred_size (child->widget,
1407                                              &child_requisition, NULL);
1408
1409               /* Check and see if there is already enough space
1410                *  for the child.
1411                */
1412               width = 0;
1413               for (col = child->left_attach; col < child->right_attach; col++)
1414                 {
1415                   width += priv->cols[col].requisition;
1416                   if ((col + 1) < child->right_attach)
1417                     width += priv->cols[col].spacing;
1418                 }
1419               
1420               /* If we need to request more space for this child to fill
1421                *  its requisition, then divide up the needed space amongst the
1422                *  columns it spans, favoring expandable columns if any.
1423                */
1424               if (width < child_requisition.width + child->xpadding * 2)
1425                 {
1426                   gint n_expand = 0;
1427                   gboolean force_expand = FALSE;
1428                   
1429                   width = child_requisition.width + child->xpadding * 2 - width;
1430
1431                   for (col = child->left_attach; col < child->right_attach; col++)
1432                     if (priv->cols[col].expand)
1433                       n_expand++;
1434
1435                   if (n_expand == 0)
1436                     {
1437                       n_expand = (child->right_attach - child->left_attach);
1438                       force_expand = TRUE;
1439                     }
1440                     
1441                   for (col = child->left_attach; col < child->right_attach; col++)
1442                     if (force_expand || priv->cols[col].expand)
1443                       {
1444                         extra = width / n_expand;
1445                         priv->cols[col].requisition += extra;
1446                         width -= extra;
1447                         n_expand--;
1448                       }
1449                 }
1450             }
1451           
1452           /* Child spans multiple rows.
1453            */
1454           if (child->top_attach != (child->bottom_attach - 1))
1455             {
1456               GtkRequisition child_requisition;
1457
1458               gtk_widget_get_preferred_size (child->widget,
1459                                              &child_requisition, NULL);
1460
1461               /* Check and see if there is already enough space
1462                *  for the child.
1463                */
1464               height = 0;
1465               for (row = child->top_attach; row < child->bottom_attach; row++)
1466                 {
1467                   height += priv->rows[row].requisition;
1468                   if ((row + 1) < child->bottom_attach)
1469                     height += priv->rows[row].spacing;
1470                 }
1471               
1472               /* If we need to request more space for this child to fill
1473                *  its requisition, then divide up the needed space amongst the
1474                *  rows it spans, favoring expandable rows if any.
1475                */
1476               if (height < child_requisition.height + child->ypadding * 2)
1477                 {
1478                   gint n_expand = 0;
1479                   gboolean force_expand = FALSE;
1480                   
1481                   height = child_requisition.height + child->ypadding * 2 - height;
1482                   
1483                   for (row = child->top_attach; row < child->bottom_attach; row++)
1484                     {
1485                       if (priv->rows[row].expand)
1486                         n_expand++;
1487                     }
1488
1489                   if (n_expand == 0)
1490                     {
1491                       n_expand = (child->bottom_attach - child->top_attach);
1492                       force_expand = TRUE;
1493                     }
1494                     
1495                   for (row = child->top_attach; row < child->bottom_attach; row++)
1496                     if (force_expand || priv->rows[row].expand)
1497                       {
1498                         extra = height / n_expand;
1499                         priv->rows[row].requisition += extra;
1500                         height -= extra;
1501                         n_expand--;
1502                       }
1503                 }
1504             }
1505         }
1506     }
1507 }
1508
1509 static void
1510 gtk_table_size_allocate_init (GtkTable *table)
1511 {
1512   GtkTablePrivate *priv = table->priv;
1513   GtkTableChild *child;
1514   GList *children;
1515   gint row, col;
1516   gint has_expand;
1517   gint has_shrink;
1518   
1519   /* Initialize the rows and cols.
1520    *  By default, rows and cols do not expand and do shrink.
1521    *  Those values are modified by the children that occupy
1522    *  the rows and cols.
1523    */
1524   for (col = 0; col < priv->ncols; col++)
1525     {
1526       priv->cols[col].allocation = priv->cols[col].requisition;
1527       priv->cols[col].need_expand = FALSE;
1528       priv->cols[col].need_shrink = TRUE;
1529       priv->cols[col].expand = FALSE;
1530       priv->cols[col].shrink = TRUE;
1531       priv->cols[col].empty = TRUE;
1532     }
1533   for (row = 0; row < priv->nrows; row++)
1534     {
1535       priv->rows[row].allocation = priv->rows[row].requisition;
1536       priv->rows[row].need_expand = FALSE;
1537       priv->rows[row].need_shrink = TRUE;
1538       priv->rows[row].expand = FALSE;
1539       priv->rows[row].shrink = TRUE;
1540       priv->rows[row].empty = TRUE;
1541     }
1542   
1543   /* Loop over all the children and adjust the row and col values
1544    *  based on whether the children want to be allowed to expand
1545    *  or shrink. This loop handles children that occupy a single
1546    *  row or column.
1547    */
1548   children = priv->children;
1549   while (children)
1550     {
1551       child = children->data;
1552       children = children->next;
1553       
1554       if (gtk_widget_get_visible (child->widget))
1555         {
1556           if (child->left_attach == (child->right_attach - 1))
1557             {
1558               if (child->xexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL))
1559                 priv->cols[child->left_attach].expand = TRUE;
1560
1561               if (!child->xshrink)
1562                 priv->cols[child->left_attach].shrink = FALSE;
1563
1564               priv->cols[child->left_attach].empty = FALSE;
1565             }
1566           
1567           if (child->top_attach == (child->bottom_attach - 1))
1568             {
1569               if (child->yexpand || gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_VERTICAL))
1570                 priv->rows[child->top_attach].expand = TRUE;
1571               
1572               if (!child->yshrink)
1573                 priv->rows[child->top_attach].shrink = FALSE;
1574
1575               priv->rows[child->top_attach].empty = FALSE;
1576             }
1577         }
1578     }
1579   
1580   /* Loop over all the children again and this time handle children
1581    *  which span multiple rows or columns.
1582    */
1583   children = priv->children;
1584   while (children)
1585     {
1586       child = children->data;
1587       children = children->next;
1588       
1589       if (gtk_widget_get_visible (child->widget))
1590         {
1591           if (child->left_attach != (child->right_attach - 1))
1592             {
1593               for (col = child->left_attach; col < child->right_attach; col++)
1594                 priv->cols[col].empty = FALSE;
1595
1596               if (child->xexpand)
1597                 {
1598                   has_expand = FALSE;
1599                   for (col = child->left_attach; col < child->right_attach; col++)
1600                     if (priv->cols[col].expand)
1601                       {
1602                         has_expand = TRUE;
1603                         break;
1604                       }
1605                   
1606                   if (!has_expand)
1607                     for (col = child->left_attach; col < child->right_attach; col++)
1608                       priv->cols[col].need_expand = TRUE;
1609                 }
1610               
1611               if (!child->xshrink)
1612                 {
1613                   has_shrink = TRUE;
1614                   for (col = child->left_attach; col < child->right_attach; col++)
1615                     if (!priv->cols[col].shrink)
1616                       {
1617                         has_shrink = FALSE;
1618                         break;
1619                       }
1620                   
1621                   if (has_shrink)
1622                     for (col = child->left_attach; col < child->right_attach; col++)
1623                       priv->cols[col].need_shrink = FALSE;
1624                 }
1625             }
1626           
1627           if (child->top_attach != (child->bottom_attach - 1))
1628             {
1629               for (row = child->top_attach; row < child->bottom_attach; row++)
1630                 priv->rows[row].empty = FALSE;
1631
1632               if (child->yexpand)
1633                 {
1634                   has_expand = FALSE;
1635                   for (row = child->top_attach; row < child->bottom_attach; row++)
1636                     if (priv->rows[row].expand)
1637                       {
1638                         has_expand = TRUE;
1639                         break;
1640                       }
1641                   
1642                   if (!has_expand)
1643                     for (row = child->top_attach; row < child->bottom_attach; row++)
1644                       priv->rows[row].need_expand = TRUE;
1645                 }
1646               
1647               if (!child->yshrink)
1648                 {
1649                   has_shrink = TRUE;
1650                   for (row = child->top_attach; row < child->bottom_attach; row++)
1651                     if (!priv->rows[row].shrink)
1652                       {
1653                         has_shrink = FALSE;
1654                         break;
1655                       }
1656                   
1657                   if (has_shrink)
1658                     for (row = child->top_attach; row < child->bottom_attach; row++)
1659                       priv->rows[row].need_shrink = FALSE;
1660                 }
1661             }
1662         }
1663     }
1664   
1665   /* Loop over the columns and set the expand and shrink values
1666    *  if the column can be expanded or shrunk.
1667    */
1668   for (col = 0; col < priv->ncols; col++)
1669     {
1670       if (priv->cols[col].empty)
1671         {
1672           priv->cols[col].expand = FALSE;
1673           priv->cols[col].shrink = FALSE;
1674         }
1675       else
1676         {
1677           if (priv->cols[col].need_expand)
1678             priv->cols[col].expand = TRUE;
1679           if (!priv->cols[col].need_shrink)
1680             priv->cols[col].shrink = FALSE;
1681         }
1682     }
1683   
1684   /* Loop over the rows and set the expand and shrink values
1685    *  if the row can be expanded or shrunk.
1686    */
1687   for (row = 0; row < priv->nrows; row++)
1688     {
1689       if (priv->rows[row].empty)
1690         {
1691           priv->rows[row].expand = FALSE;
1692           priv->rows[row].shrink = FALSE;
1693         }
1694       else
1695         {
1696           if (priv->rows[row].need_expand)
1697             priv->rows[row].expand = TRUE;
1698           if (!priv->rows[row].need_shrink)
1699             priv->rows[row].shrink = FALSE;
1700         }
1701     }
1702 }
1703
1704 static void
1705 gtk_table_size_allocate_pass1 (GtkTable *table)
1706 {
1707   GtkTablePrivate *priv = table->priv;
1708   GtkAllocation allocation;
1709   gint real_width;
1710   gint real_height;
1711   gint width, height;
1712   gint row, col;
1713   gint nexpand;
1714   gint nshrink;
1715   gint extra;
1716
1717   /* If we were allocated more space than we requested
1718    *  then we have to expand any expandable rows and columns
1719    *  to fill in the extra space.
1720    */
1721   gtk_widget_get_allocation (GTK_WIDGET (table), &allocation);
1722   real_width = allocation.width;
1723   real_height = allocation.height;
1724
1725   if (priv->homogeneous)
1726     {
1727       if (!priv->children)
1728         nexpand = 1;
1729       else
1730         {
1731           nexpand = 0;
1732           for (col = 0; col < priv->ncols; col++)
1733             if (priv->cols[col].expand)
1734               {
1735                 nexpand += 1;
1736                 break;
1737               }
1738         }
1739       if (nexpand)
1740         {
1741           width = real_width;
1742           for (col = 0; col + 1 < priv->ncols; col++)
1743             width -= priv->cols[col].spacing;
1744
1745           for (col = 0; col < priv->ncols; col++)
1746             {
1747               extra = width / (priv->ncols - col);
1748               priv->cols[col].allocation = MAX (1, extra);
1749               width -= extra;
1750             }
1751         }
1752     }
1753   else
1754     {
1755       width = 0;
1756       nexpand = 0;
1757       nshrink = 0;
1758
1759       for (col = 0; col < priv->ncols; col++)
1760         {
1761           width += priv->cols[col].requisition;
1762           if (priv->cols[col].expand)
1763             nexpand += 1;
1764           if (priv->cols[col].shrink)
1765             nshrink += 1;
1766         }
1767       for (col = 0; col + 1 < priv->ncols; col++)
1768         width += priv->cols[col].spacing;
1769
1770       /* Check to see if we were allocated more width than we requested.
1771        */
1772       if ((width < real_width) && (nexpand >= 1))
1773         {
1774           width = real_width - width;
1775
1776           for (col = 0; col < priv->ncols; col++)
1777             if (priv->cols[col].expand)
1778               {
1779                 extra = width / nexpand;
1780                 priv->cols[col].allocation += extra;
1781
1782                 width -= extra;
1783                 nexpand -= 1;
1784               }
1785         }
1786       
1787       /* Check to see if we were allocated less width than we requested,
1788        * then shrink until we fit the size give.
1789        */
1790       if (width > real_width)
1791         {
1792           gint total_nshrink = nshrink;
1793
1794           extra = width - real_width;
1795           while (total_nshrink > 0 && extra > 0)
1796             {
1797               nshrink = total_nshrink;
1798               for (col = 0; col < priv->ncols; col++)
1799                 if (priv->cols[col].shrink)
1800                   {
1801                     gint allocation = priv->cols[col].allocation;
1802
1803                     priv->cols[col].allocation = MAX (1, (gint) priv->cols[col].allocation - extra / nshrink);
1804                     extra -= allocation - priv->cols[col].allocation;
1805                     nshrink -= 1;
1806                     if (priv->cols[col].allocation < 2)
1807                       {
1808                         total_nshrink -= 1;
1809                         priv->cols[col].shrink = FALSE;
1810                       }
1811                   }
1812             }
1813         }
1814     }
1815
1816   if (priv->homogeneous)
1817     {
1818       if (!priv->children)
1819         nexpand = 1;
1820       else
1821         {
1822           nexpand = 0;
1823           for (row = 0; row < priv->nrows; row++)
1824             if (priv->rows[row].expand)
1825               {
1826                 nexpand += 1;
1827                 break;
1828               }
1829         }
1830       if (nexpand)
1831         {
1832           height = real_height;
1833
1834           for (row = 0; row + 1 < priv->nrows; row++)
1835             height -= priv->rows[row].spacing;
1836
1837           for (row = 0; row < priv->nrows; row++)
1838             {
1839               extra = height / (priv->nrows - row);
1840               priv->rows[row].allocation = MAX (1, extra);
1841               height -= extra;
1842             }
1843         }
1844     }
1845   else
1846     {
1847       height = 0;
1848       nexpand = 0;
1849       nshrink = 0;
1850
1851       for (row = 0; row < priv->nrows; row++)
1852         {
1853           height += priv->rows[row].requisition;
1854           if (priv->rows[row].expand)
1855             nexpand += 1;
1856           if (priv->rows[row].shrink)
1857             nshrink += 1;
1858         }
1859       for (row = 0; row + 1 < priv->nrows; row++)
1860         height += priv->rows[row].spacing;
1861
1862       /* Check to see if we were allocated more height than we requested.
1863        */
1864       if ((height < real_height) && (nexpand >= 1))
1865         {
1866           height = real_height - height;
1867
1868           for (row = 0; row < priv->nrows; row++)
1869             if (priv->rows[row].expand)
1870               {
1871                 extra = height / nexpand;
1872                 priv->rows[row].allocation += extra;
1873
1874                 height -= extra;
1875                 nexpand -= 1;
1876               }
1877         }
1878       
1879       /* Check to see if we were allocated less height than we requested.
1880        * then shrink until we fit the size give.
1881        */
1882       if (height > real_height)
1883         {
1884           gint total_nshrink = nshrink;
1885           
1886           extra = height - real_height;
1887           while (total_nshrink > 0 && extra > 0)
1888             {
1889               nshrink = total_nshrink;
1890               for (row = 0; row < priv->nrows; row++)
1891                 if (priv->rows[row].shrink)
1892                   {
1893                     gint allocation = priv->rows[row].allocation;
1894
1895                     priv->rows[row].allocation = MAX (1, (gint) priv->rows[row].allocation - extra / nshrink);
1896                     extra -= allocation - priv->rows[row].allocation;
1897                     nshrink -= 1;
1898                     if (priv->rows[row].allocation < 2)
1899                       {
1900                         total_nshrink -= 1;
1901                         priv->rows[row].shrink = FALSE;
1902                       }
1903                   }
1904             }
1905         }
1906     }
1907 }
1908
1909 static void
1910 gtk_table_size_allocate_pass2 (GtkTable *table)
1911 {
1912   GtkTablePrivate *priv = table->priv;
1913   GtkTableChild *child;
1914   GList *children;
1915   gint max_width;
1916   gint max_height;
1917   gint x, y;
1918   gint row, col;
1919   GtkAllocation allocation;
1920   GtkAllocation table_allocation, widget_allocation;
1921   GtkWidget *widget = GTK_WIDGET (table);
1922
1923   children = priv->children;
1924   while (children)
1925     {
1926       child = children->data;
1927       children = children->next;
1928       
1929       if (gtk_widget_get_visible (child->widget))
1930         {
1931           GtkRequisition child_requisition;
1932
1933           gtk_widget_get_preferred_size (child->widget,
1934                                          &child_requisition, NULL);
1935
1936           gtk_widget_get_allocation (GTK_WIDGET (table), &table_allocation);
1937           x = table_allocation.x;
1938           y = table_allocation.y;
1939           max_width = 0;
1940           max_height = 0;
1941           
1942           for (col = 0; col < child->left_attach; col++)
1943             {
1944               x += priv->cols[col].allocation;
1945               x += priv->cols[col].spacing;
1946             }
1947           
1948           for (col = child->left_attach; col < child->right_attach; col++)
1949             {
1950               max_width += priv->cols[col].allocation;
1951               if ((col + 1) < child->right_attach)
1952                 max_width += priv->cols[col].spacing;
1953             }
1954           
1955           for (row = 0; row < child->top_attach; row++)
1956             {
1957               y += priv->rows[row].allocation;
1958               y += priv->rows[row].spacing;
1959             }
1960           
1961           for (row = child->top_attach; row < child->bottom_attach; row++)
1962             {
1963               max_height += priv->rows[row].allocation;
1964               if ((row + 1) < child->bottom_attach)
1965                 max_height += priv->rows[row].spacing;
1966             }
1967           
1968           if (child->xfill)
1969             {
1970               allocation.width = MAX (1, max_width - (gint)child->xpadding * 2);
1971               allocation.x = x + (max_width - allocation.width) / 2;
1972             }
1973           else
1974             {
1975               allocation.width = child_requisition.width;
1976               allocation.x = x + (max_width - allocation.width) / 2;
1977             }
1978           
1979           if (child->yfill)
1980             {
1981               allocation.height = MAX (1, max_height - (gint)child->ypadding * 2);
1982               allocation.y = y + (max_height - allocation.height) / 2;
1983             }
1984           else
1985             {
1986               allocation.height = child_requisition.height;
1987               allocation.y = y + (max_height - allocation.height) / 2;
1988             }
1989
1990           gtk_widget_get_allocation (widget, &widget_allocation);
1991           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1992             allocation.x = widget_allocation.x + widget_allocation.width
1993                            - (allocation.x - widget_allocation.x) - allocation.width;
1994
1995           gtk_widget_size_allocate (child->widget, &allocation);
1996         }
1997     }
1998 }