]> Pileus Git - ~andy/gtk/blob - gtk/gtkgrid.c
Redo patch in efae64b (Set vertical/horizontal class...)
[~andy/gtk] / gtk / gtkgrid.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Red Hat, Inc.
3  * Author: Matthias Clasen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "gtkgrid.h"
24
25 #include "gtkorientable.h"
26 #include "gtksizerequest.h"
27 #include "gtkprivate.h"
28 #include "gtkintl.h"
29
30
31 /**
32  * SECTION:gtkgrid
33  * @Short_description: Pack widgets in a rows and columns
34  * @Title: GtkGrid
35  * @See_also: #GtkTable, #GtkHBox, #GtkVBox
36  *
37  * GtkGrid is a container which arranges its child widgets in
38  * rows and columns. It is a very similar to #GtkTable and #GtkBox,
39  * but it consistently uses #GtkWidget's #GtkWidget:margin and #GtkWidget:expand
40  * properties instead of custom child properties, and it fully supports
41  * <link linkend="geometry-management">height-for-width geometry management</link>.
42  *
43  * Children are added using gtk_grid_attach(). They can span multiple
44  * rows or columns. It is also possible to add a child next to an
45  * existing child, using gtk_grid_attach_next_to().
46  *
47  * GtkGrid can be used like a #GtkBox by just using gtk_container_add(),
48  * which will place children next to each other in the direction determined
49  * by the #GtkOrientable:orientation property.
50  */
51
52 typedef struct _GtkGridChild GtkGridChild;
53 typedef struct _GtkGridChildAttach GtkGridChildAttach;
54 typedef struct _GtkGridLine GtkGridLine;
55 typedef struct _GtkGridLines GtkGridLines;
56 typedef struct _GtkGridLineData GtkGridLineData;
57 typedef struct _GtkGridRequest GtkGridRequest;
58
59 struct _GtkGridChildAttach
60 {
61   gint pos;
62   gint span;
63 };
64
65 struct _GtkGridChild
66 {
67   GtkWidget *widget;
68   GtkGridChildAttach attach[2];
69 };
70
71 #define CHILD_LEFT(child)    ((child)->attach[GTK_ORIENTATION_HORIZONTAL].pos)
72 #define CHILD_WIDTH(child)   ((child)->attach[GTK_ORIENTATION_HORIZONTAL].span)
73 #define CHILD_TOP(child)     ((child)->attach[GTK_ORIENTATION_VERTICAL].pos)
74 #define CHILD_HEIGHT(child)  ((child)->attach[GTK_ORIENTATION_VERTICAL].span)
75
76 /* A GtkGridLineData struct contains row/column specific parts
77  * of the grid.
78  */
79 struct _GtkGridLineData
80 {
81   gint16 spacing;
82   guint homogeneous : 1;
83 };
84
85 struct _GtkGridPrivate
86 {
87   GList *children;
88
89   GtkOrientation orientation;
90
91   GtkGridLineData linedata[2];
92 };
93
94 #define ROWS(priv)    (&(priv)->linedata[GTK_ORIENTATION_HORIZONTAL])
95 #define COLUMNS(priv) (&(priv)->linedata[GTK_ORIENTATION_VERTICAL])
96
97 /* A GtkGridLine struct represents a single row or column
98  * during size requests
99  */
100 struct _GtkGridLine
101 {
102   gint minimum;
103   gint natural;
104   gint position;
105   gint allocation;
106
107   guint need_expand : 1;
108   guint expand      : 1;
109   guint empty       : 1;
110 };
111
112 struct _GtkGridLines
113 {
114   GtkGridLine *lines;
115   gint min, max;
116 };
117
118 struct _GtkGridRequest
119 {
120   GtkGrid *grid;
121   GtkGridLines lines[2];
122 };
123
124
125 enum
126 {
127   PROP_0,
128   PROP_ORIENTATION,
129   PROP_ROW_SPACING,
130   PROP_COLUMN_SPACING,
131   PROP_ROW_HOMOGENEOUS,
132   PROP_COLUMN_HOMOGENEOUS
133 };
134
135 enum
136 {
137   CHILD_PROP_0,
138   CHILD_PROP_LEFT_ATTACH,
139   CHILD_PROP_TOP_ATTACH,
140   CHILD_PROP_WIDTH,
141   CHILD_PROP_HEIGHT
142 };
143
144 G_DEFINE_TYPE_WITH_CODE (GtkGrid, gtk_grid, GTK_TYPE_CONTAINER,
145                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
146
147
148 static void
149 gtk_grid_get_property (GObject    *object,
150                        guint       prop_id,
151                        GValue     *value,
152                        GParamSpec *pspec)
153 {
154   GtkGrid *grid = GTK_GRID (object);
155   GtkGridPrivate *priv = grid->priv;
156
157   switch (prop_id)
158     {
159     case PROP_ORIENTATION:
160       g_value_set_enum (value, priv->orientation);
161       break;
162
163     case PROP_ROW_SPACING:
164       g_value_set_int (value, ROWS (priv)->spacing);
165       break;
166
167     case PROP_COLUMN_SPACING:
168       g_value_set_int (value, COLUMNS (priv)->spacing);
169       break;
170
171     case PROP_ROW_HOMOGENEOUS:
172       g_value_set_boolean (value, ROWS (priv)->homogeneous);
173       break;
174
175     case PROP_COLUMN_HOMOGENEOUS:
176       g_value_set_boolean (value, COLUMNS (priv)->homogeneous);
177       break;
178
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182     }
183 }
184
185 static void
186 gtk_grid_set_orientation (GtkGrid        *grid,
187                           GtkOrientation  orientation)
188 {
189   GtkGridPrivate *priv = grid->priv;
190
191   if (priv->orientation != orientation)
192     {
193       priv->orientation = orientation;
194       _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
195
196       g_object_notify (G_OBJECT (grid), "orientation");
197     }
198 }
199
200 static void
201 gtk_grid_set_property (GObject      *object,
202                        guint         prop_id,
203                        const GValue *value,
204                        GParamSpec   *pspec)
205 {
206   GtkGrid *grid = GTK_GRID (object);
207
208   switch (prop_id)
209     {
210     case PROP_ORIENTATION:
211       gtk_grid_set_orientation (grid, g_value_get_enum (value));
212       break;
213
214     case PROP_ROW_SPACING:
215       gtk_grid_set_row_spacing (grid, g_value_get_int (value));
216       break;
217
218     case PROP_COLUMN_SPACING:
219       gtk_grid_set_column_spacing (grid, g_value_get_int (value));
220       break;
221
222     case PROP_ROW_HOMOGENEOUS:
223       gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
224       break;
225
226     case PROP_COLUMN_HOMOGENEOUS:
227       gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
228       break;
229
230     default:
231       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232       break;
233     }
234 }
235
236 static GtkGridChild *
237 find_grid_child (GtkGrid   *grid,
238                  GtkWidget *widget)
239 {
240   GtkGridPrivate *priv = grid->priv;
241   GtkGridChild *child;
242   GList *list;
243
244   for (list = priv->children; list; list = list->next)
245     {
246       child = list->data;
247
248       if (child->widget == widget)
249         return child;
250     }
251
252   return NULL;
253 }
254
255 static void
256 gtk_grid_get_child_property (GtkContainer *container,
257                              GtkWidget    *child,
258                              guint         property_id,
259                              GValue       *value,
260                              GParamSpec   *pspec)
261 {
262   GtkGrid *grid = GTK_GRID (container);
263   GtkGridChild *grid_child;
264
265   grid_child = find_grid_child (grid, child);
266
267   if (grid_child == NULL)
268     {
269       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
270       return;
271     }
272
273   switch (property_id)
274     {
275     case CHILD_PROP_LEFT_ATTACH:
276       g_value_set_int (value, CHILD_LEFT (grid_child));
277       break;
278
279     case CHILD_PROP_TOP_ATTACH:
280       g_value_set_int (value, CHILD_TOP (grid_child));
281       break;
282
283     case CHILD_PROP_WIDTH:
284       g_value_set_int (value, CHILD_WIDTH (grid_child));
285       break;
286
287     case CHILD_PROP_HEIGHT:
288       g_value_set_int (value, CHILD_HEIGHT (grid_child));
289       break;
290
291     default:
292       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
293       break;
294     }
295 }
296
297 static void
298 gtk_grid_set_child_property (GtkContainer *container,
299                              GtkWidget    *child,
300                              guint         property_id,
301                              const GValue *value,
302                              GParamSpec   *pspec)
303 {
304   GtkGrid *grid = GTK_GRID (container);
305   GtkGridChild *grid_child;
306
307   grid_child = find_grid_child (grid, child);
308
309   if (grid_child == NULL)
310     {
311       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
312       return;
313     }
314
315   switch (property_id)
316     {
317     case CHILD_PROP_LEFT_ATTACH:
318       CHILD_LEFT (grid_child) = g_value_get_int (value);
319       break;
320
321     case CHILD_PROP_TOP_ATTACH:
322       CHILD_TOP (grid_child) = g_value_get_int (value);
323       break;
324
325    case CHILD_PROP_WIDTH:
326       CHILD_WIDTH (grid_child) = g_value_get_int (value);
327       break;
328
329     case CHILD_PROP_HEIGHT:
330       CHILD_HEIGHT (grid_child) = g_value_get_int (value);
331       break;
332
333     default:
334       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
335       break;
336     }
337
338   if (gtk_widget_get_visible (child) &&
339       gtk_widget_get_visible (GTK_WIDGET (grid)))
340     gtk_widget_queue_resize (child);
341 }
342
343 static void
344 gtk_grid_init (GtkGrid *grid)
345 {
346   GtkGridPrivate *priv;
347
348   grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, GTK_TYPE_GRID, GtkGridPrivate);
349   priv = grid->priv;
350
351   gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
352   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);
353
354   priv->children = NULL;
355   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
356
357   priv->linedata[0].spacing = 0;
358   priv->linedata[1].spacing = 0;
359
360   priv->linedata[0].homogeneous = FALSE;
361   priv->linedata[1].homogeneous = FALSE;
362 }
363
364 static void grid_attach (GtkGrid   *grid,
365                          GtkWidget *child,
366                          gint       left,
367                          gint       top,
368                          gint       width,
369                          gint       height);
370
371 static void
372 gtk_grid_add (GtkContainer *container,
373               GtkWidget    *child)
374 {
375   GtkGrid *grid = GTK_GRID (container);
376   GtkGridPrivate *priv = grid->priv;
377   GtkGridChild *grid_child;
378   GtkGridChildAttach *attach;
379   GtkGridChildAttach *opposite;
380   GList *list;
381   gint pos;
382
383   pos = 0;
384   for (list = priv->children; list; list = list->next)
385     {
386       grid_child = list->data;
387
388       attach = &grid_child->attach[priv->orientation];
389       opposite = &grid_child->attach[1 - priv->orientation];
390
391       if (opposite->pos <= 0 && opposite->pos + opposite->span > 0)
392         pos = MAX (pos, attach->pos + attach->span);
393      }
394
395   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
396     grid_attach (grid, child, pos, 0, 1, 1);
397   else
398     grid_attach (grid, child, 0, pos, 1, 1);
399 }
400
401 static void
402 gtk_grid_remove (GtkContainer *container,
403                  GtkWidget    *child)
404 {
405   GtkGrid *grid = GTK_GRID (container);
406   GtkGridPrivate *priv = grid->priv;
407   GtkGridChild *grid_child;
408   GList *list;
409
410   for (list = priv->children; list; list = list->next)
411     {
412       grid_child = list->data;
413
414       if (grid_child->widget == child)
415         {
416           gboolean was_visible = gtk_widget_get_visible (child);
417
418           gtk_widget_unparent (child);
419
420           priv->children = g_list_remove (priv->children, grid_child);
421
422           g_slice_free (GtkGridChild, grid_child);
423
424           if (was_visible && gtk_widget_get_visible (GTK_WIDGET (grid)))
425             gtk_widget_queue_resize (GTK_WIDGET (grid));
426
427           break;
428         }
429     }
430 }
431
432 static void
433 gtk_grid_forall (GtkContainer *container,
434                  gboolean      include_internals,
435                  GtkCallback   callback,
436                  gpointer      callback_data)
437 {
438   GtkGrid *grid = GTK_GRID (container);
439   GtkGridPrivate *priv = grid->priv;
440   GtkGridChild *child;
441   GList *list;
442
443   list = priv->children;
444   while (list)
445     {
446       child = list->data;
447       list  = list->next;
448
449       (* callback) (child->widget, callback_data);
450     }
451 }
452
453 static GType
454 gtk_grid_child_type (GtkContainer *container)
455 {
456   return GTK_TYPE_WIDGET;
457 }
458
459 static GtkSizeRequestMode
460 gtk_grid_get_request_mode (GtkWidget *widget)
461 {
462   GtkGridPrivate *priv = GTK_GRID (widget)->priv;
463
464   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
465     return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
466   else
467     return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
468 }
469
470 /* Calculates the min and max numbers for both orientations.
471  */
472 static void
473 gtk_grid_request_count_lines (GtkGridRequest *request)
474 {
475   GtkGridPrivate *priv = request->grid->priv;
476   GtkGridChild *child;
477   GtkGridChildAttach *attach;
478   GList *list;
479   gint min[2];
480   gint max[2];
481
482   min[0] = min[1] = G_MAXINT;
483   max[0] = max[1] = G_MININT;
484
485   for (list = priv->children; list; list = list->next)
486     {
487       child = list->data;
488       attach = child->attach;
489
490       min[0] = MIN (min[0], attach[0].pos);
491       max[0] = MAX (max[0], attach[0].pos + attach[0].span);
492       min[1] = MIN (min[1], attach[1].pos);
493       max[1] = MAX (max[1], attach[1].pos + attach[1].span);
494     }
495
496   request->lines[0].min = min[0];
497   request->lines[0].max = max[0];
498   request->lines[1].min = min[1];
499   request->lines[1].max = max[1];
500 }
501
502 /* Sets line sizes to 0 and marks lines as expand
503  * if they have a non-spanning expanding child.
504  */
505 static void
506 gtk_grid_request_init (GtkGridRequest *request,
507                        GtkOrientation  orientation)
508 {
509   GtkGridPrivate *priv = request->grid->priv;
510   GtkGridChild *child;
511   GtkGridChildAttach *attach;
512   GtkGridLines *lines;
513   GList *list;
514   gint i;
515
516   lines = &request->lines[orientation];
517
518   for (i = 0; i < lines->max - lines->min; i++)
519     {
520       lines->lines[i].minimum = 0;
521       lines->lines[i].natural = 0;
522       lines->lines[i].expand = FALSE;
523     }
524
525   for (list = priv->children; list; list = list->next)
526     {
527       child = list->data;
528
529       attach = &child->attach[orientation];
530       if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
531         lines->lines[attach->pos - lines->min].expand = TRUE;
532     }
533 }
534
535 /* Sums allocations for lines spanned by child and their spacing.
536  */
537 static gint
538 compute_allocation_for_child (GtkGridRequest *request,
539                               GtkGridChild   *child,
540                               GtkOrientation  orientation)
541 {
542   GtkGridPrivate *priv = request->grid->priv;
543   GtkGridLineData *linedata;
544   GtkGridLines *lines;
545   GtkGridLine *line;
546   GtkGridChildAttach *attach;
547   gint size;
548   gint i;
549
550   linedata = &priv->linedata[orientation];
551   lines = &request->lines[orientation];
552   attach = &child->attach[orientation];
553
554   size = (attach->span - 1) * linedata->spacing;
555   for (i = 0; i < attach->span; i++)
556     {
557       line = &lines->lines[attach->pos - lines->min + i];
558       size += line->allocation;
559     }
560
561   return size;
562 }
563
564 static void
565 compute_request_for_child (GtkGridRequest *request,
566                            GtkGridChild   *child,
567                            GtkOrientation  orientation,
568                            gboolean        contextual,
569                            gint           *minimum,
570                            gint           *natural)
571 {
572   if (contextual)
573     {
574       gint size;
575
576       size = compute_allocation_for_child (request, child, 1 - orientation);
577       if (orientation == GTK_ORIENTATION_HORIZONTAL)
578         gtk_widget_get_preferred_width_for_height (child->widget,
579                                                    size,
580                                                    minimum, natural);
581       else
582         gtk_widget_get_preferred_height_for_width (child->widget,
583                                                    size,
584                                                    minimum, natural);
585     }
586   else
587     {
588       if (orientation == GTK_ORIENTATION_HORIZONTAL)
589         gtk_widget_get_preferred_width (child->widget, minimum, natural);
590       else
591         gtk_widget_get_preferred_height (child->widget, minimum, natural);
592     }
593 }
594
595 /* Sets requisition to max. of non-spanning children.
596  * If contextual is TRUE, requires allocations of
597  * lines in the opposite orientation to be set.
598  */
599 static void
600 gtk_grid_request_non_spanning (GtkGridRequest *request,
601                                GtkOrientation  orientation,
602                                gboolean        contextual)
603 {
604   GtkGridPrivate *priv = request->grid->priv;
605   GtkGridChild *child;
606   GtkGridChildAttach *attach;
607   GtkGridLines *lines;
608   GtkGridLine *line;
609   GList *list;
610   gint minimum;
611   gint natural;
612
613   lines = &request->lines[orientation];
614
615   for (list = priv->children; list; list = list->next)
616     {
617       child = list->data;
618
619       if (!gtk_widget_get_visible (child->widget))
620         continue;
621
622       attach = &child->attach[orientation];
623       if (attach->span != 1)
624         continue;
625
626       compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
627
628       line = &lines->lines[attach->pos - lines->min];
629       line->minimum = MAX (line->minimum, minimum);
630       line->natural = MAX (line->natural, natural);
631     }
632 }
633
634 /* Enforce homogeneous sizes.
635  */
636 static void
637 gtk_grid_request_homogeneous (GtkGridRequest *request,
638                               GtkOrientation  orientation)
639 {
640   GtkGridPrivate *priv = request->grid->priv;
641   GtkGridLineData *linedata;
642   GtkGridLines *lines;
643   gint minimum, natural;
644   gint i;
645
646   linedata = &priv->linedata[orientation];
647   lines = &request->lines[orientation];
648
649   if (!linedata->homogeneous)
650     return;
651
652   minimum = 0;
653   natural = 0;
654
655   for (i = 0; i < lines->max - lines->min; i++)
656     {
657       minimum = MAX (minimum, lines->lines[i].minimum);
658       natural = MAX (natural, lines->lines[i].natural);
659     }
660
661   for (i = 0; i < lines->max - lines->min; i++)
662     {
663       lines->lines[i].minimum = minimum;
664       lines->lines[i].natural = natural;
665     }
666 }
667
668 /* Deals with spanning children.
669  * Requires expand fields of lines to be set for
670  * non-spanning children.
671  */
672 static void
673 gtk_grid_request_spanning (GtkGridRequest *request,
674                            GtkOrientation  orientation,
675                            gboolean        contextual)
676 {
677   GtkGridPrivate *priv = request->grid->priv;
678   GList *list;
679   GtkGridChild *child;
680   GtkGridChildAttach *attach;
681   GtkGridLineData *linedata;
682   GtkGridLines *lines;
683   GtkGridLine *line;
684   gint minimum;
685   gint natural;
686   gint span_minimum;
687   gint span_natural;
688   gint span_expand;
689   gboolean force_expand;
690   gint extra;
691   gint expand;
692   gint line_extra;
693   gint i;
694
695   linedata = &priv->linedata[orientation];
696   lines = &request->lines[orientation];
697
698   for (list = priv->children; list; list = list->next)
699     {
700       child = list->data;
701
702       if (!gtk_widget_get_visible (child->widget))
703         continue;
704
705       attach = &child->attach[orientation];
706       if (attach->span == 1)
707         continue;
708
709       compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
710
711       span_minimum = (attach->span - 1) * linedata->spacing;
712       span_natural = (attach->span - 1) * linedata->spacing;
713       span_expand = 0;
714       force_expand = FALSE;
715       for (i = 0; i < attach->span; i++)
716         {
717           line = &lines->lines[attach->pos - lines->min + i];
718           span_minimum += line->minimum;
719           span_natural += line->natural;
720           if (line->expand)
721             span_expand += 1;
722         }
723       if (span_expand == 0)
724         {
725           span_expand = attach->span;
726           force_expand = TRUE;
727         }
728
729       /* If we need to request more space for this child to fill
730        * its requisition, then divide up the needed space amongst the
731        * lines it spans, favoring expandable lines if any.
732        */
733       if (span_minimum < minimum)
734         {
735           extra = minimum - span_minimum;
736           expand = span_expand;
737           for (i = 0; i < attach->span; i++)
738             {
739               line = &lines->lines[attach->pos - lines->min + i];
740               if (force_expand || line->expand)
741                 {
742                   line_extra = extra / expand;
743                   line->minimum += line_extra;
744                   extra -= line_extra;
745                   expand -= 1;
746                 }
747             }
748         }
749
750       if (span_natural < natural)
751         {
752           extra = natural - span_natural;
753           expand = span_expand;
754           for (i = 0; i < attach->span; i++)
755             {
756               line = &lines->lines[attach->pos - lines->min + i];
757               if (force_expand || line->expand)
758                 {
759                   line_extra = extra / expand;
760                   line->natural += line_extra;
761                   extra -= line_extra;
762                   expand -= 1;
763                 }
764             }
765         }
766     }
767 }
768
769 /* Marks empty and expanding lines and counts them.
770  */
771 static void
772 gtk_grid_request_compute_expand (GtkGridRequest *request,
773                                  GtkOrientation  orientation,
774                                  gint           *nonempty_lines,
775                                  gint           *expand_lines)
776 {
777   GtkGridPrivate *priv = request->grid->priv;
778   GtkGridChild *child;
779   GtkGridChildAttach *attach;
780   GList *list;
781   gint i;
782   GtkGridLines *lines;
783   GtkGridLine *line;
784   gboolean has_expand;
785   gint expand;
786   gint empty;
787
788   lines = &request->lines[orientation];
789
790   for (i = 0; i < lines->max - lines->min; i++)
791     {
792       lines->lines[i].need_expand = FALSE;
793       lines->lines[i].expand = FALSE;
794       lines->lines[i].empty = TRUE;
795     }
796
797   for (list = priv->children; list; list = list->next)
798     {
799       child = list->data;
800
801       if (!gtk_widget_get_visible (child->widget))
802         continue;
803
804       attach = &child->attach[orientation];
805       if (attach->span != 1)
806         continue;
807
808       line = &lines->lines[attach->pos - lines->min];
809       line->empty = FALSE;
810       if (gtk_widget_compute_expand (child->widget, orientation))
811         line->expand = TRUE;
812     }
813
814   for (list = priv->children; list; list = list->next)
815     {
816       child = list->data;
817
818       if (!gtk_widget_get_visible (child->widget))
819         continue;
820
821       attach = &child->attach[orientation];
822       if (attach->span == 1)
823         continue;
824
825       has_expand = FALSE;
826       for (i = 0; i < attach->span; i++)
827         {
828           line = &lines->lines[attach->pos - lines->min + i];
829           line->empty = FALSE;
830           if (line->expand)
831             has_expand = TRUE;
832         }
833
834       if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
835         {
836           for (i = 0; i < attach->span; i++)
837             {
838               line = &lines->lines[attach->pos - lines->min + i];
839               line->need_expand = TRUE;
840             }
841         }
842     }
843
844   empty = 0;
845   expand = 0;
846   for (i = 0; i < lines->max - lines->min; i++)
847     {
848       line = &lines->lines[i];
849
850       if (line->need_expand)
851         line->expand = TRUE;
852
853       if (line->empty)
854         empty += 1;
855
856       if (line->expand)
857         expand += 1;
858     }
859
860   if (nonempty_lines)
861     *nonempty_lines = lines->max - lines->min - empty;
862
863   if (expand_lines)
864     *expand_lines = expand;
865 }
866
867 /* Sums the minimum and natural fields of lines and their spacing.
868  */
869 static void
870 gtk_grid_request_sum (GtkGridRequest *request,
871                       GtkOrientation  orientation,
872                       gint           *minimum,
873                       gint           *natural)
874 {
875   GtkGridPrivate *priv = request->grid->priv;
876   GtkGridLineData *linedata;
877   GtkGridLines *lines;
878   gint i;
879   gint min, nat;
880   gint nonempty;
881
882   gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
883
884   linedata = &priv->linedata[orientation];
885   lines = &request->lines[orientation];
886
887   min = (nonempty - 1) * linedata->spacing;
888   nat = (nonempty - 1) * linedata->spacing;
889
890   for (i = 0; i < lines->max - lines->min; i++)
891     {
892       min += lines->lines[i].minimum;
893       nat += lines->lines[i].natural;
894     }
895
896   if (minimum)
897     *minimum = min;
898
899   if (natural)
900     *natural = nat;
901 }
902
903 /* Computes minimum and natural fields of lines.
904  * When contextual is TRUE, requires allocation of
905  * lines in the opposite orientation to be set.
906  */
907 static void
908 gtk_grid_request_run (GtkGridRequest *request,
909                       GtkOrientation  orientation,
910                       gboolean        contextual)
911 {
912   gtk_grid_request_init (request, orientation);
913   gtk_grid_request_non_spanning (request, orientation, contextual);
914   gtk_grid_request_homogeneous (request, orientation);
915   gtk_grid_request_spanning (request, orientation, contextual);
916   gtk_grid_request_homogeneous (request, orientation);
917 }
918
919 /* Requires that the minimum and natural fields of lines
920  * have been set, computes the allocation field of lines
921  * by distributing total_size among lines.
922  */
923 static void
924 gtk_grid_request_allocate (GtkGridRequest *request,
925                            GtkOrientation  orientation,
926                            gint            total_size)
927 {
928   GtkGridPrivate *priv = request->grid->priv;
929   GtkGridLineData *linedata;
930   GtkGridLines *lines;
931   GtkGridLine *line;
932   gint nonempty;
933   gint expand;
934   gint i, j;
935   GtkRequestedSize *sizes;
936   gint extra;
937   gint rest;
938   gint size;
939
940   gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
941
942   linedata = &priv->linedata[orientation];
943   lines = &request->lines[orientation];
944
945   size = total_size - (nonempty - 1) * linedata->spacing;
946
947   if (linedata->homogeneous)
948     {
949       extra = size / nonempty;
950       rest = size % nonempty;
951
952       for (i = 0; i < lines->max - lines->min; i++)
953         {
954           line = &lines->lines[i];
955           if (line->empty)
956             continue;
957
958           line->allocation = extra;
959           if (rest > 0)
960             {
961               line->allocation += 1;
962               rest -= 1;
963             }
964         }
965     }
966   else
967     {
968       sizes = g_newa (GtkRequestedSize, nonempty);
969
970       j = 0;
971       for (i = 0; i < lines->max - lines->min; i++)
972         {
973           line = &lines->lines[i];
974           if (line->empty)
975             continue;
976
977           size -= line->minimum;
978
979           sizes[j].minimum_size = line->minimum;
980           sizes[j].natural_size = line->natural;
981           sizes[j].data = line;
982           j++;
983         }
984
985       size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
986
987       if (expand > 0)
988         {
989           extra = size / expand;
990           rest = size % expand;
991         }
992       else
993         {
994           extra = 0;
995           rest = 0;
996         }
997
998       j = 0;
999       for (i = 0; i < lines->max - lines->min; i++)
1000         {
1001           line = &lines->lines[i];
1002           if (line->empty)
1003             continue;
1004
1005           g_assert (line == sizes[j].data);
1006
1007           line->allocation = sizes[j].minimum_size;
1008           if (line->expand)
1009             {
1010               line->allocation += extra;
1011               if (rest > 0)
1012                 {
1013                   line->allocation += 1;
1014                   rest -= 1;
1015                 }
1016             }
1017
1018           j++;
1019         }
1020     }
1021 }
1022
1023 /* Computes the position fields from allocation and spacing.
1024  */
1025 static void
1026 gtk_grid_request_position (GtkGridRequest *request,
1027                            GtkOrientation  orientation)
1028 {
1029   GtkGridPrivate *priv = request->grid->priv;
1030   GtkGridLineData *linedata;
1031   GtkGridLines *lines;
1032   GtkGridLine *line;
1033   gint position;
1034   gint i;
1035
1036   linedata = &priv->linedata[orientation];
1037   lines = &request->lines[orientation];
1038
1039   position = 0;
1040   for (i = 0; i < lines->max - lines->min; i++)
1041     {
1042       line = &lines->lines[i];
1043       if (!line->empty)
1044         {
1045           line->position = position;
1046           position += line->allocation + linedata->spacing;
1047         }
1048     }
1049 }
1050
1051 static void
1052 gtk_grid_get_size (GtkGrid        *grid,
1053                    GtkOrientation  orientation,
1054                    gint           *minimum,
1055                    gint           *natural)
1056 {
1057   GtkGridRequest request;
1058   GtkGridLines *lines;
1059
1060   request.grid = grid;
1061   gtk_grid_request_count_lines (&request);
1062   lines = &request.lines[orientation];
1063   lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1064
1065   gtk_grid_request_run (&request, orientation, FALSE);
1066   gtk_grid_request_sum (&request, orientation, minimum, natural);
1067 }
1068
1069 static void
1070 gtk_grid_get_size_for_size (GtkGrid        *grid,
1071                             GtkOrientation  orientation,
1072                             gint            size,
1073                             gint           *minimum,
1074                             gint           *natural)
1075 {
1076   GtkGridRequest request;
1077   GtkGridLines *lines;
1078   gint min_size;
1079
1080   request.grid = grid;
1081   gtk_grid_request_count_lines (&request);
1082   lines = &request.lines[0];
1083   lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1084   lines = &request.lines[1];
1085   lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1086
1087   gtk_grid_request_run (&request, 1 - orientation, FALSE);
1088   gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1089   gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1090
1091   gtk_grid_request_run (&request, orientation, TRUE);
1092   gtk_grid_request_sum (&request, orientation, minimum, natural);
1093 }
1094
1095 static void
1096 gtk_grid_get_preferred_width (GtkWidget *widget,
1097                               gint      *minimum,
1098                               gint      *natural)
1099 {
1100   GtkGrid *grid = GTK_GRID (widget);
1101
1102   if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1103
1104     gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1105   else
1106     gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1107 }
1108
1109 static void
1110 gtk_grid_get_preferred_height (GtkWidget *widget,
1111                                gint      *minimum,
1112                                gint      *natural)
1113 {
1114   GtkGrid *grid = GTK_GRID (widget);
1115
1116   if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1117     gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1118   else
1119     gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1120 }
1121
1122 static void
1123 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1124                                          gint       height,
1125                                          gint      *minimum,
1126                                          gint      *natural)
1127 {
1128   GtkGrid *grid = GTK_GRID (widget);
1129
1130   if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1131     gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1132   else
1133     gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1134 }
1135
1136 static void
1137 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1138                                          gint       width,
1139                                          gint      *minimum,
1140                                          gint      *natural)
1141 {
1142   GtkGrid *grid = GTK_GRID (widget);
1143
1144   if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1145     gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1146   else
1147     gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1148 }
1149
1150 static void
1151 allocate_child (GtkGridRequest *request,
1152                 GtkOrientation  orientation,
1153                 GtkGridChild   *child,
1154                 gint           *position,
1155                 gint           *size)
1156 {
1157   GtkGridPrivate *priv = request->grid->priv;
1158   GtkGridLineData *linedata;
1159   GtkGridLines *lines;
1160   GtkGridLine *line;
1161   GtkGridChildAttach *attach;
1162   gint i;
1163
1164   linedata = &priv->linedata[orientation];
1165   lines = &request->lines[orientation];
1166   attach = &child->attach[orientation];
1167
1168   *position = lines->lines[attach->pos - lines->min].position;
1169
1170   *size = (attach->span - 1) * linedata->spacing;
1171   for (i = 0; i < attach->span; i++)
1172     {
1173       line = &lines->lines[attach->pos - lines->min + i];
1174       *size += line->allocation;
1175     }
1176 }
1177
1178 static void
1179 gtk_grid_request_allocate_children (GtkGridRequest *request)
1180 {
1181   GtkGridPrivate *priv = request->grid->priv;
1182   GList *list;
1183   GtkGridChild *child;
1184   GtkAllocation allocation;
1185   GtkAllocation child_allocation;
1186   gint x, y, width, height;
1187
1188   gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1189
1190   for (list = priv->children; list; list = list->next)
1191     {
1192       child = list->data;
1193
1194       if (!gtk_widget_get_visible (child->widget))
1195         continue;
1196
1197       allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1198       allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1199
1200       child_allocation.x = allocation.x + x;
1201       child_allocation.y = allocation.y + y;
1202       child_allocation.width = MAX (1, width);
1203       child_allocation.height = MAX (1, height);
1204
1205       gtk_widget_size_allocate (child->widget, &child_allocation);
1206     }
1207 }
1208
1209 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1210
1211 static void
1212 gtk_grid_size_allocate (GtkWidget     *widget,
1213                         GtkAllocation *allocation)
1214 {
1215   GtkGrid *grid = GTK_GRID (widget);
1216   GtkGridPrivate *priv = grid->priv;
1217   GtkGridRequest request;
1218   GtkGridLines *lines;
1219
1220   request.grid = grid;
1221   gtk_grid_request_count_lines (&request);
1222   lines = &request.lines[0];
1223   lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1224   lines = &request.lines[1];
1225   lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1226
1227   gtk_widget_set_allocation (widget, allocation);
1228
1229   gtk_grid_request_run (&request, 1 - priv->orientation, FALSE);
1230   gtk_grid_request_allocate (&request, 1 - priv->orientation, GET_SIZE (allocation, 1 - priv->orientation));
1231   gtk_grid_request_run (&request, priv->orientation, TRUE);
1232   gtk_grid_request_allocate (&request, priv->orientation, GET_SIZE (allocation, priv->orientation));
1233
1234   gtk_grid_request_position (&request, 0);
1235   gtk_grid_request_position (&request, 1);
1236
1237   gtk_grid_request_allocate_children (&request);
1238 }
1239
1240 static void
1241 gtk_grid_class_init (GtkGridClass *class)
1242 {
1243   GObjectClass *object_class = G_OBJECT_CLASS (class);
1244   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1245   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1246
1247   object_class->get_property = gtk_grid_get_property;
1248   object_class->set_property = gtk_grid_set_property;
1249
1250   widget_class->size_allocate = gtk_grid_size_allocate;
1251   widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1252   widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1253   widget_class->get_request_mode = gtk_grid_get_request_mode;
1254   widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1255   widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1256
1257   container_class->add = gtk_grid_add;
1258   container_class->remove = gtk_grid_remove;
1259   container_class->forall = gtk_grid_forall;
1260   container_class->child_type = gtk_grid_child_type;
1261   container_class->set_child_property = gtk_grid_set_child_property;
1262   container_class->get_child_property = gtk_grid_get_child_property;
1263   gtk_container_class_handle_border_width (container_class);
1264
1265   g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1266
1267   g_object_class_install_property (object_class, PROP_ROW_SPACING,
1268     g_param_spec_int ("row-spacing",
1269                       P_("Row spacing"),
1270                       P_("The amount of space between two consecutive rows"),
1271                       0, G_MAXINT16, 0,
1272                       GTK_PARAM_READWRITE));
1273
1274   g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1275     g_param_spec_int ("column-spacing",
1276                       P_("Column spacing"),
1277                       P_("The amount of space between two consecutive columns"),
1278                       0, G_MAXINT16, 0,
1279                       GTK_PARAM_READWRITE));
1280
1281   g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1282     g_param_spec_boolean ("row-homogeneous",
1283                           P_("Row Homogeneous"),
1284                           P_("If TRUE, the rows are all the same height"),
1285                           FALSE,
1286                           GTK_PARAM_READWRITE));
1287
1288   g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1289     g_param_spec_boolean ("column-homogeneous",
1290                           P_("Column Homogeneous"),
1291                           P_("If TRUE, the columns are all the same width"),
1292                           FALSE,
1293                           GTK_PARAM_READWRITE));
1294
1295   gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1296     g_param_spec_int ("left-attach",
1297                       P_("Left attachment"),
1298                       P_("The column number to attach the left side of the child to"),
1299                       G_MININT, G_MAXINT, 0,
1300                       GTK_PARAM_READWRITE));
1301
1302   gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1303     g_param_spec_int ("top-attach",
1304                       P_("Top attachment"),
1305                       P_("The row number to attach the top side of a child widget to"),
1306                       G_MININT, G_MAXINT, 0,
1307                       GTK_PARAM_READWRITE));
1308
1309   gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1310     g_param_spec_int ("width",
1311                       P_("Width"),
1312                       P_("The number of columns that a child spans"),
1313                       1, G_MAXINT, 1,
1314                       GTK_PARAM_READWRITE));
1315
1316   gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1317     g_param_spec_int ("height",
1318                       P_("Height"),
1319                       P_("The number of rows that a child spans"),
1320                       1, G_MAXINT, 1,
1321                       GTK_PARAM_READWRITE));
1322
1323   g_type_class_add_private (class, sizeof (GtkGridPrivate));
1324 }
1325
1326 /**
1327  * gtk_grid_new:
1328  *
1329  * Creates a new grid widget.
1330  *
1331  * Returns: the new #GtkGrid
1332  */
1333 GtkWidget *
1334 gtk_grid_new (void)
1335 {
1336   return g_object_new (GTK_TYPE_GRID, NULL);
1337 }
1338
1339 static void
1340 grid_attach (GtkGrid   *grid,
1341              GtkWidget *widget,
1342              gint       left,
1343              gint       top,
1344              gint       width,
1345              gint       height)
1346 {
1347   GtkGridPrivate *priv = grid->priv;
1348   GtkGridChild *child;
1349
1350   child = g_slice_new (GtkGridChild);
1351   child->widget = widget;
1352   CHILD_LEFT (child) = left;
1353   CHILD_TOP (child) = top;
1354   CHILD_WIDTH (child) = width;
1355   CHILD_HEIGHT (child) = height;
1356
1357   priv->children = g_list_prepend (priv->children, child);
1358
1359   gtk_widget_set_parent (widget, GTK_WIDGET (grid));
1360 }
1361
1362 /**
1363  * gtk_grid_attach:
1364  * @grid: a #GtkGrid
1365  * @child: the widget to add
1366  * @left: the column number to attach the left side of @child to
1367  * @top: the row number to attach the top side of @child to
1368  * @width: the number of columns that @child will span
1369  * @height: the number of rows that @child will span
1370  *
1371  * Adds a widget to the grid.
1372  *
1373  * The position of @child is determined by @left and @top. The
1374  * number of 'cells' that @child will occupy is determined by
1375  * @width and @height.
1376  */
1377 void
1378 gtk_grid_attach (GtkGrid   *grid,
1379                  GtkWidget *child,
1380                  gint       left,
1381                  gint       top,
1382                  gint       width,
1383                  gint       height)
1384 {
1385   g_return_if_fail (GTK_IS_GRID (grid));
1386   g_return_if_fail (GTK_IS_WIDGET (child));
1387   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1388   g_return_if_fail (width > 0);
1389   g_return_if_fail (height > 0);
1390
1391   grid_attach (grid, child, left, top, width, height);
1392 }
1393
1394 /**
1395  * gtk_grid_attach_next_to:
1396  * @grid: a #GtkGrid
1397  * @child: the widget to add
1398  * @sibling: the child of @grid that @child will be placed next to
1399  * @side: the side of @sibling that @child is positioned next to
1400  * @width: the number of columns that @child will span
1401  * @height: the number of rows that @child will span
1402  *
1403  * Adds a widget to the grid.
1404  *
1405  * The widget is placed next to @sibling, on the side determined by
1406  * @side.
1407  */
1408 void
1409 gtk_grid_attach_next_to (GtkGrid         *grid,
1410                          GtkWidget       *child,
1411                          GtkWidget       *sibling,
1412                          GtkPositionType  side,
1413                          gint             width,
1414                          gint             height)
1415 {
1416   GtkGridChild *grid_sibling;
1417   gint left, top;
1418
1419   g_return_if_fail (GTK_IS_GRID (grid));
1420   g_return_if_fail (GTK_IS_WIDGET (child));
1421   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1422   g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1423   g_return_if_fail (width > 0);
1424   g_return_if_fail (height > 0);
1425
1426   grid_sibling = find_grid_child (grid, sibling);
1427
1428   switch (side)
1429     {
1430     case GTK_POS_LEFT:
1431       left = CHILD_LEFT (grid_sibling) - width;
1432       top = CHILD_TOP (grid_sibling);
1433       break;
1434     case GTK_POS_RIGHT:
1435       left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1436       top = CHILD_TOP (grid_sibling);
1437       break;
1438     case GTK_POS_TOP:
1439       left = CHILD_LEFT (grid_sibling);
1440       top = CHILD_TOP (grid_sibling) - height;
1441       break;
1442     case GTK_POS_BOTTOM:
1443       left = CHILD_LEFT (grid_sibling);
1444       top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1445       break;
1446     default:
1447       g_assert_not_reached ();
1448     }
1449
1450   grid_attach (grid, child, left, top, width, height);
1451 }
1452
1453 /**
1454  * gtk_grid_set_row_homogeneous:
1455  * @grid: a #GtkGrid
1456  * @homogeneous: %TRUE to make rows homogeneous
1457  *
1458  * Sets whether all rows of @grid will have the same height.
1459  */
1460 void
1461 gtk_grid_set_row_homogeneous (GtkGrid  *grid,
1462                               gboolean  homogeneous)
1463 {
1464   GtkGridPrivate *priv;
1465   g_return_if_fail (GTK_IS_GRID (grid));
1466
1467   priv = grid->priv;
1468
1469   if (ROWS (priv)->homogeneous != homogeneous)
1470     {
1471       ROWS (priv)->homogeneous = homogeneous;
1472
1473       if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1474         gtk_widget_queue_resize (GTK_WIDGET (grid));
1475
1476       g_object_notify (G_OBJECT (grid), "row-homogeneous");
1477     }
1478 }
1479
1480 /**
1481  * gtk_grid_get_row_homogeneous:
1482  * @grid: a #GtkGrid
1483  *
1484  * Returns whether all rows of @grid have the same height.
1485  *
1486  * Returns: whether all rows of @grid have the same height.
1487  */
1488 gboolean
1489 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1490 {
1491   GtkGridPrivate *priv;
1492   g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1493
1494   priv = grid->priv;
1495
1496   return ROWS (priv)->homogeneous;
1497 }
1498
1499 /**
1500  * gtk_grid_set_column_homogeneous:
1501  * @grid: a #GtkGrid
1502  * @homogeneous: %TRUE to make columns homogeneous
1503  *
1504  * Sets whether all columns of @grid will have the same width.
1505  */
1506 void
1507 gtk_grid_set_column_homogeneous (GtkGrid  *grid,
1508                                  gboolean  homogeneous)
1509 {
1510   GtkGridPrivate *priv;
1511   g_return_if_fail (GTK_IS_GRID (grid));
1512
1513   priv = grid->priv;
1514
1515   if (COLUMNS (priv)->homogeneous != homogeneous)
1516     {
1517       COLUMNS (priv)->homogeneous = homogeneous;
1518
1519       if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1520         gtk_widget_queue_resize (GTK_WIDGET (grid));
1521
1522       g_object_notify (G_OBJECT (grid), "column-homogeneous");
1523     }
1524 }
1525
1526 /**
1527  * gtk_grid_get_column_homogeneous:
1528  * @grid: a #GtkGrid
1529  *
1530  * Returns whether all columns of @grid have the same width.
1531  *
1532  * Returns: whether all columns of @grid have the same width.
1533  */
1534 gboolean
1535 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1536 {
1537   GtkGridPrivate *priv;
1538   g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1539
1540   priv = grid->priv;
1541
1542   return COLUMNS (priv)->homogeneous;
1543 }
1544
1545 /**
1546  * gtk_grid_set_row_spacing:
1547  * @grid: a #GtkGrid
1548  * @spacing: the amount of space to insert between rows
1549  *
1550  * Sets the amount of space between rows of @grid.
1551  */
1552 void
1553 gtk_grid_set_row_spacing (GtkGrid *grid,
1554                           guint    spacing)
1555 {
1556   GtkGridPrivate *priv;
1557   g_return_if_fail (GTK_IS_GRID (grid));
1558   g_return_if_fail (spacing <= G_MAXINT16);
1559
1560   priv = grid->priv;
1561
1562   if (ROWS (priv)->spacing != spacing)
1563     {
1564       ROWS (priv)->spacing = spacing;
1565
1566       if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1567         gtk_widget_queue_resize (GTK_WIDGET (grid));
1568
1569       g_object_notify (G_OBJECT (grid), "row-spacing");
1570     }
1571 }
1572
1573 /**
1574  * gtk_grid_get_row_spacing:
1575  * @grid: a #GtkGrid
1576  *
1577  * Returns the amount of space between the rows of @grid.
1578  *
1579  * Returns: the row spacing of @grid
1580  */
1581 guint
1582 gtk_grid_get_row_spacing (GtkGrid *grid)
1583 {
1584   GtkGridPrivate *priv;
1585   g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1586
1587   priv = grid->priv;
1588
1589   return ROWS (priv)->spacing;
1590 }
1591
1592 /**
1593  * gtk_grid_set_column_spacing:
1594  * @grid: a #GtkGrid
1595  * @spacing: the amount of space to insert between columns
1596  *
1597  * Sets the amount of space between columns of @grid.
1598  */
1599 void
1600 gtk_grid_set_column_spacing (GtkGrid *grid,
1601                              guint    spacing)
1602 {
1603   GtkGridPrivate *priv;
1604   g_return_if_fail (GTK_IS_GRID (grid));
1605   g_return_if_fail (spacing <= G_MAXINT16);
1606
1607   priv = grid->priv;
1608
1609   if (COLUMNS (priv)->spacing != spacing)
1610     {
1611       COLUMNS (priv)->spacing = spacing;
1612
1613       if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1614         gtk_widget_queue_resize (GTK_WIDGET (grid));
1615
1616       g_object_notify (G_OBJECT (grid), "column-spacing");
1617     }
1618 }
1619
1620 /**
1621  * gtk_grid_get_column_spacing:
1622  * @grid: a #GtkGrid
1623  *
1624  * Returns the amount of space between the columns of @grid.
1625  *
1626  * Returns: the column spacing of @grid
1627  */
1628 guint
1629 gtk_grid_get_column_spacing (GtkGrid *grid)
1630 {
1631   GtkGridPrivate *priv;
1632
1633   g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1634
1635   priv = grid->priv;
1636
1637   return COLUMNS (priv)->spacing;
1638 }