1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Red Hat, Inc.
3 * Author: Matthias Clasen
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.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
25 #include "gtkorientableprivate.h"
26 #include "gtksizerequest.h"
27 #include "gtkprivate.h"
33 * @Short_description: Pack widgets in a rows and columns
35 * @See_also: #GtkTable, #GtkHBox, #GtkVBox
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>.
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(). The behaviour of
46 * GtkGrid when several children occupy the same grid cell is undefined.
48 * GtkGrid can be used like a #GtkBox by just using gtk_container_add(),
49 * which will place children next to each other in the direction determined
50 * by the #GtkOrientable:orientation property.
53 typedef struct _GtkGridChild GtkGridChild;
54 typedef struct _GtkGridChildAttach GtkGridChildAttach;
55 typedef struct _GtkGridLine GtkGridLine;
56 typedef struct _GtkGridLines GtkGridLines;
57 typedef struct _GtkGridLineData GtkGridLineData;
58 typedef struct _GtkGridRequest GtkGridRequest;
60 struct _GtkGridChildAttach
69 GtkGridChildAttach attach[2];
72 #define CHILD_LEFT(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].pos)
73 #define CHILD_WIDTH(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].span)
74 #define CHILD_TOP(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].pos)
75 #define CHILD_HEIGHT(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].span)
77 /* A GtkGridLineData struct contains row/column specific parts
80 struct _GtkGridLineData
83 guint homogeneous : 1;
86 struct _GtkGridPrivate
90 GtkOrientation orientation;
92 GtkGridLineData linedata[2];
95 #define ROWS(priv) (&(priv)->linedata[GTK_ORIENTATION_HORIZONTAL])
96 #define COLUMNS(priv) (&(priv)->linedata[GTK_ORIENTATION_VERTICAL])
98 /* A GtkGridLine struct represents a single row or column
99 * during size requests
108 guint need_expand : 1;
119 struct _GtkGridRequest
122 GtkGridLines lines[2];
132 PROP_ROW_HOMOGENEOUS,
133 PROP_COLUMN_HOMOGENEOUS
139 CHILD_PROP_LEFT_ATTACH,
140 CHILD_PROP_TOP_ATTACH,
145 G_DEFINE_TYPE_WITH_CODE (GtkGrid, gtk_grid, GTK_TYPE_CONTAINER,
146 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
150 gtk_grid_get_property (GObject *object,
155 GtkGrid *grid = GTK_GRID (object);
156 GtkGridPrivate *priv = grid->priv;
160 case PROP_ORIENTATION:
161 g_value_set_enum (value, priv->orientation);
164 case PROP_ROW_SPACING:
165 g_value_set_int (value, COLUMNS (priv)->spacing);
168 case PROP_COLUMN_SPACING:
169 g_value_set_int (value, ROWS (priv)->spacing);
172 case PROP_ROW_HOMOGENEOUS:
173 g_value_set_boolean (value, COLUMNS (priv)->homogeneous);
176 case PROP_COLUMN_HOMOGENEOUS:
177 g_value_set_boolean (value, ROWS (priv)->homogeneous);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 gtk_grid_set_orientation (GtkGrid *grid,
188 GtkOrientation orientation)
190 GtkGridPrivate *priv = grid->priv;
192 if (priv->orientation != orientation)
194 priv->orientation = orientation;
195 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
197 g_object_notify (G_OBJECT (grid), "orientation");
202 gtk_grid_set_property (GObject *object,
207 GtkGrid *grid = GTK_GRID (object);
211 case PROP_ORIENTATION:
212 gtk_grid_set_orientation (grid, g_value_get_enum (value));
215 case PROP_ROW_SPACING:
216 gtk_grid_set_row_spacing (grid, g_value_get_int (value));
219 case PROP_COLUMN_SPACING:
220 gtk_grid_set_column_spacing (grid, g_value_get_int (value));
223 case PROP_ROW_HOMOGENEOUS:
224 gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
227 case PROP_COLUMN_HOMOGENEOUS:
228 gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 static GtkGridChild *
238 find_grid_child (GtkGrid *grid,
241 GtkGridPrivate *priv = grid->priv;
245 for (list = priv->children; list; list = list->next)
249 if (child->widget == widget)
257 gtk_grid_get_child_property (GtkContainer *container,
263 GtkGrid *grid = GTK_GRID (container);
264 GtkGridChild *grid_child;
266 grid_child = find_grid_child (grid, child);
268 if (grid_child == NULL)
270 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
276 case CHILD_PROP_LEFT_ATTACH:
277 g_value_set_int (value, CHILD_LEFT (grid_child));
280 case CHILD_PROP_TOP_ATTACH:
281 g_value_set_int (value, CHILD_TOP (grid_child));
284 case CHILD_PROP_WIDTH:
285 g_value_set_int (value, CHILD_WIDTH (grid_child));
288 case CHILD_PROP_HEIGHT:
289 g_value_set_int (value, CHILD_HEIGHT (grid_child));
293 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
299 gtk_grid_set_child_property (GtkContainer *container,
305 GtkGrid *grid = GTK_GRID (container);
306 GtkGridChild *grid_child;
308 grid_child = find_grid_child (grid, child);
310 if (grid_child == NULL)
312 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
318 case CHILD_PROP_LEFT_ATTACH:
319 CHILD_LEFT (grid_child) = g_value_get_int (value);
322 case CHILD_PROP_TOP_ATTACH:
323 CHILD_TOP (grid_child) = g_value_get_int (value);
326 case CHILD_PROP_WIDTH:
327 CHILD_WIDTH (grid_child) = g_value_get_int (value);
330 case CHILD_PROP_HEIGHT:
331 CHILD_HEIGHT (grid_child) = g_value_get_int (value);
335 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
339 if (gtk_widget_get_visible (child) &&
340 gtk_widget_get_visible (GTK_WIDGET (grid)))
341 gtk_widget_queue_resize (child);
345 gtk_grid_init (GtkGrid *grid)
347 GtkGridPrivate *priv;
349 grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, GTK_TYPE_GRID, GtkGridPrivate);
352 gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
353 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);
355 priv->children = NULL;
356 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
358 priv->linedata[0].spacing = 0;
359 priv->linedata[1].spacing = 0;
361 priv->linedata[0].homogeneous = FALSE;
362 priv->linedata[1].homogeneous = FALSE;
366 grid_attach (GtkGrid *grid,
373 GtkGridPrivate *priv = grid->priv;
376 child = g_slice_new (GtkGridChild);
377 child->widget = widget;
378 CHILD_LEFT (child) = left;
379 CHILD_TOP (child) = top;
380 CHILD_WIDTH (child) = width;
381 CHILD_HEIGHT (child) = height;
383 priv->children = g_list_prepend (priv->children, child);
385 gtk_widget_set_parent (widget, GTK_WIDGET (grid));
388 /* Find the position 'touching' existing
389 * children. @orientation and @max determine
390 * from which direction to approach (horizontal
391 * + max = right, vertical + !max = top, etc).
392 * @op_pos, @op_span determine the rows/columns
393 * in which the touching has to happen.
396 find_attach_position (GtkGrid *grid,
397 GtkOrientation orientation,
402 GtkGridPrivate *priv = grid->priv;
403 GtkGridChild *grid_child;
404 GtkGridChildAttach *attach;
405 GtkGridChildAttach *opposite;
417 for (list = priv->children; list; list = list->next)
419 grid_child = list->data;
421 attach = &grid_child->attach[orientation];
422 opposite = &grid_child->attach[1 - orientation];
424 /* check if the ranges overlap */
425 if (opposite->pos <= op_pos + op_span && op_pos <= opposite->pos + opposite->span)
430 pos = MAX (pos, attach->pos + attach->span);
432 pos = MIN (pos, attach->pos);
443 gtk_grid_add (GtkContainer *container,
446 GtkGrid *grid = GTK_GRID (container);
447 GtkGridPrivate *priv = grid->priv;
448 gint pos[2] = { 0, 0 };
450 pos[priv->orientation] = find_attach_position (grid, priv->orientation, 0, 1, TRUE);
451 grid_attach (grid, child, pos[0], pos[1], 1, 1);
455 gtk_grid_remove (GtkContainer *container,
458 GtkGrid *grid = GTK_GRID (container);
459 GtkGridPrivate *priv = grid->priv;
460 GtkGridChild *grid_child;
463 for (list = priv->children; list; list = list->next)
465 grid_child = list->data;
467 if (grid_child->widget == child)
469 gboolean was_visible = gtk_widget_get_visible (child);
471 gtk_widget_unparent (child);
473 priv->children = g_list_remove (priv->children, grid_child);
475 g_slice_free (GtkGridChild, grid_child);
477 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (grid)))
478 gtk_widget_queue_resize (GTK_WIDGET (grid));
486 gtk_grid_forall (GtkContainer *container,
487 gboolean include_internals,
488 GtkCallback callback,
489 gpointer callback_data)
491 GtkGrid *grid = GTK_GRID (container);
492 GtkGridPrivate *priv = grid->priv;
496 list = priv->children;
502 (* callback) (child->widget, callback_data);
507 gtk_grid_child_type (GtkContainer *container)
509 return GTK_TYPE_WIDGET;
512 /* Calculates the min and max numbers for both orientations.
515 gtk_grid_request_count_lines (GtkGridRequest *request)
517 GtkGridPrivate *priv = request->grid->priv;
519 GtkGridChildAttach *attach;
524 min[0] = min[1] = G_MAXINT;
525 max[0] = max[1] = G_MININT;
527 for (list = priv->children; list; list = list->next)
530 attach = child->attach;
532 min[0] = MIN (min[0], attach[0].pos);
533 max[0] = MAX (max[0], attach[0].pos + attach[0].span);
534 min[1] = MIN (min[1], attach[1].pos);
535 max[1] = MAX (max[1], attach[1].pos + attach[1].span);
538 request->lines[0].min = min[0];
539 request->lines[0].max = max[0];
540 request->lines[1].min = min[1];
541 request->lines[1].max = max[1];
544 /* Sets line sizes to 0 and marks lines as expand
545 * if they have a non-spanning expanding child.
548 gtk_grid_request_init (GtkGridRequest *request,
549 GtkOrientation orientation)
551 GtkGridPrivate *priv = request->grid->priv;
553 GtkGridChildAttach *attach;
558 lines = &request->lines[orientation];
560 for (i = 0; i < lines->max - lines->min; i++)
562 lines->lines[i].minimum = 0;
563 lines->lines[i].natural = 0;
564 lines->lines[i].expand = FALSE;
567 for (list = priv->children; list; list = list->next)
571 attach = &child->attach[orientation];
572 if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
573 lines->lines[attach->pos - lines->min].expand = TRUE;
577 /* Sums allocations for lines spanned by child and their spacing.
580 compute_allocation_for_child (GtkGridRequest *request,
582 GtkOrientation orientation)
584 GtkGridPrivate *priv = request->grid->priv;
585 GtkGridLineData *linedata;
588 GtkGridChildAttach *attach;
592 linedata = &priv->linedata[orientation];
593 lines = &request->lines[orientation];
594 attach = &child->attach[orientation];
596 size = (attach->span - 1) * linedata->spacing;
597 for (i = 0; i < attach->span; i++)
599 line = &lines->lines[attach->pos - lines->min + i];
600 size += line->allocation;
607 compute_request_for_child (GtkGridRequest *request,
609 GtkOrientation orientation,
618 size = compute_allocation_for_child (request, child, 1 - orientation);
619 if (orientation == GTK_ORIENTATION_HORIZONTAL)
620 gtk_widget_get_preferred_width_for_height (child->widget,
624 gtk_widget_get_preferred_height_for_width (child->widget,
630 if (orientation == GTK_ORIENTATION_HORIZONTAL)
631 gtk_widget_get_preferred_width (child->widget, minimum, natural);
633 gtk_widget_get_preferred_height (child->widget, minimum, natural);
637 /* Sets requisition to max. of non-spanning children.
638 * If contextual is TRUE, requires allocations of
639 * lines in the opposite orientation to be set.
642 gtk_grid_request_non_spanning (GtkGridRequest *request,
643 GtkOrientation orientation,
646 GtkGridPrivate *priv = request->grid->priv;
648 GtkGridChildAttach *attach;
655 lines = &request->lines[orientation];
657 for (list = priv->children; list; list = list->next)
661 if (!gtk_widget_get_visible (child->widget))
664 attach = &child->attach[orientation];
665 if (attach->span != 1)
668 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
670 line = &lines->lines[attach->pos - lines->min];
671 line->minimum = MAX (line->minimum, minimum);
672 line->natural = MAX (line->natural, natural);
676 /* Enforce homogeneous sizes.
679 gtk_grid_request_homogeneous (GtkGridRequest *request,
680 GtkOrientation orientation)
682 GtkGridPrivate *priv = request->grid->priv;
683 GtkGridLineData *linedata;
685 gint minimum, natural;
688 linedata = &priv->linedata[orientation];
689 lines = &request->lines[orientation];
691 if (!linedata->homogeneous)
697 for (i = 0; i < lines->max - lines->min; i++)
699 minimum = MAX (minimum, lines->lines[i].minimum);
700 natural = MAX (natural, lines->lines[i].natural);
703 for (i = 0; i < lines->max - lines->min; i++)
705 lines->lines[i].minimum = minimum;
706 lines->lines[i].natural = natural;
710 /* Deals with spanning children.
711 * Requires expand fields of lines to be set for
712 * non-spanning children.
715 gtk_grid_request_spanning (GtkGridRequest *request,
716 GtkOrientation orientation,
719 GtkGridPrivate *priv = request->grid->priv;
722 GtkGridChildAttach *attach;
723 GtkGridLineData *linedata;
731 gboolean force_expand;
737 linedata = &priv->linedata[orientation];
738 lines = &request->lines[orientation];
740 for (list = priv->children; list; list = list->next)
744 if (!gtk_widget_get_visible (child->widget))
747 attach = &child->attach[orientation];
748 if (attach->span == 1)
751 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
753 span_minimum = (attach->span - 1) * linedata->spacing;
754 span_natural = (attach->span - 1) * linedata->spacing;
756 force_expand = FALSE;
757 for (i = 0; i < attach->span; i++)
759 line = &lines->lines[attach->pos - lines->min + i];
760 span_minimum += line->minimum;
761 span_natural += line->natural;
765 if (span_expand == 0)
767 span_expand = attach->span;
771 /* If we need to request more space for this child to fill
772 * its requisition, then divide up the needed space amongst the
773 * lines it spans, favoring expandable lines if any.
775 * When doing homogeneous allocation though, try to keep the
776 * line allocations even, since we're going to force them to
777 * be the same anyway, and we don't want to introduce unnecessary
780 if (span_minimum < minimum)
782 if (linedata->homogeneous)
786 total = minimum - (attach->span - 1) * linedata->spacing;
787 m = total / attach->span + (total % attach->span ? 1 : 0);
788 for (i = 0; i < attach->span; i++)
790 line = &lines->lines[attach->pos - lines->min + i];
791 line->minimum = MAX(line->minimum, m);
796 extra = minimum - span_minimum;
797 expand = span_expand;
798 for (i = 0; i < attach->span; i++)
800 line = &lines->lines[attach->pos - lines->min + i];
801 if (force_expand || line->expand)
803 line_extra = extra / expand;
804 line->minimum += line_extra;
812 if (span_natural < natural)
814 if (linedata->homogeneous)
818 total = natural - (attach->span - 1) * linedata->spacing;
819 n = total / attach->span + (total % attach->span ? 1 : 0);
820 for (i = 0; i < attach->span; i++)
822 line = &lines->lines[attach->pos - lines->min + i];
823 line->natural = MAX(line->natural, n);
828 extra = natural - span_natural;
829 expand = span_expand;
830 for (i = 0; i < attach->span; i++)
832 line = &lines->lines[attach->pos - lines->min + i];
833 if (force_expand || line->expand)
835 line_extra = extra / expand;
836 line->natural += line_extra;
846 /* Marks empty and expanding lines and counts them.
849 gtk_grid_request_compute_expand (GtkGridRequest *request,
850 GtkOrientation orientation,
851 gint *nonempty_lines,
854 GtkGridPrivate *priv = request->grid->priv;
856 GtkGridChildAttach *attach;
865 lines = &request->lines[orientation];
867 for (i = 0; i < lines->max - lines->min; i++)
869 lines->lines[i].need_expand = FALSE;
870 lines->lines[i].expand = FALSE;
871 lines->lines[i].empty = TRUE;
874 for (list = priv->children; list; list = list->next)
878 if (!gtk_widget_get_visible (child->widget))
881 attach = &child->attach[orientation];
882 if (attach->span != 1)
885 line = &lines->lines[attach->pos - lines->min];
887 if (gtk_widget_compute_expand (child->widget, orientation))
891 for (list = priv->children; list; list = list->next)
895 if (!gtk_widget_get_visible (child->widget))
898 attach = &child->attach[orientation];
899 if (attach->span == 1)
903 for (i = 0; i < attach->span; i++)
905 line = &lines->lines[attach->pos - lines->min + i];
911 if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
913 for (i = 0; i < attach->span; i++)
915 line = &lines->lines[attach->pos - lines->min + i];
916 line->need_expand = TRUE;
923 for (i = 0; i < lines->max - lines->min; i++)
925 line = &lines->lines[i];
927 if (line->need_expand)
938 *nonempty_lines = lines->max - lines->min - empty;
941 *expand_lines = expand;
944 /* Sums the minimum and natural fields of lines and their spacing.
947 gtk_grid_request_sum (GtkGridRequest *request,
948 GtkOrientation orientation,
952 GtkGridPrivate *priv = request->grid->priv;
953 GtkGridLineData *linedata;
959 gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
961 linedata = &priv->linedata[orientation];
962 lines = &request->lines[orientation];
968 min = (nonempty - 1) * linedata->spacing;
969 nat = (nonempty - 1) * linedata->spacing;
972 for (i = 0; i < lines->max - lines->min; i++)
974 min += lines->lines[i].minimum;
975 nat += lines->lines[i].natural;
985 /* Computes minimum and natural fields of lines.
986 * When contextual is TRUE, requires allocation of
987 * lines in the opposite orientation to be set.
990 gtk_grid_request_run (GtkGridRequest *request,
991 GtkOrientation orientation,
994 gtk_grid_request_init (request, orientation);
995 gtk_grid_request_non_spanning (request, orientation, contextual);
996 gtk_grid_request_homogeneous (request, orientation);
997 gtk_grid_request_spanning (request, orientation, contextual);
998 gtk_grid_request_homogeneous (request, orientation);
1001 /* Requires that the minimum and natural fields of lines
1002 * have been set, computes the allocation field of lines
1003 * by distributing total_size among lines.
1006 gtk_grid_request_allocate (GtkGridRequest *request,
1007 GtkOrientation orientation,
1010 GtkGridPrivate *priv = request->grid->priv;
1011 GtkGridLineData *linedata;
1012 GtkGridLines *lines;
1017 GtkRequestedSize *sizes;
1022 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
1027 linedata = &priv->linedata[orientation];
1028 lines = &request->lines[orientation];
1030 size = total_size - (nonempty - 1) * linedata->spacing;
1032 if (linedata->homogeneous)
1034 extra = size / nonempty;
1035 rest = size % nonempty;
1037 for (i = 0; i < lines->max - lines->min; i++)
1039 line = &lines->lines[i];
1043 line->allocation = extra;
1046 line->allocation += 1;
1053 sizes = g_newa (GtkRequestedSize, nonempty);
1056 for (i = 0; i < lines->max - lines->min; i++)
1058 line = &lines->lines[i];
1062 size -= line->minimum;
1064 sizes[j].minimum_size = line->minimum;
1065 sizes[j].natural_size = line->natural;
1066 sizes[j].data = line;
1070 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1074 extra = size / expand;
1075 rest = size % expand;
1084 for (i = 0; i < lines->max - lines->min; i++)
1086 line = &lines->lines[i];
1090 g_assert (line == sizes[j].data);
1092 line->allocation = sizes[j].minimum_size;
1095 line->allocation += extra;
1098 line->allocation += 1;
1108 /* Computes the position fields from allocation and spacing.
1111 gtk_grid_request_position (GtkGridRequest *request,
1112 GtkOrientation orientation)
1114 GtkGridPrivate *priv = request->grid->priv;
1115 GtkGridLineData *linedata;
1116 GtkGridLines *lines;
1121 linedata = &priv->linedata[orientation];
1122 lines = &request->lines[orientation];
1125 for (i = 0; i < lines->max - lines->min; i++)
1127 line = &lines->lines[i];
1130 line->position = position;
1131 position += line->allocation + linedata->spacing;
1137 gtk_grid_get_size (GtkGrid *grid,
1138 GtkOrientation orientation,
1142 GtkGridRequest request;
1143 GtkGridLines *lines;
1151 if (grid->priv->children == NULL)
1154 request.grid = grid;
1155 gtk_grid_request_count_lines (&request);
1156 lines = &request.lines[orientation];
1157 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1158 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1160 gtk_grid_request_run (&request, orientation, FALSE);
1161 gtk_grid_request_sum (&request, orientation, minimum, natural);
1165 gtk_grid_get_size_for_size (GtkGrid *grid,
1166 GtkOrientation orientation,
1171 GtkGridRequest request;
1172 GtkGridLines *lines;
1181 if (grid->priv->children == NULL)
1184 request.grid = grid;
1185 gtk_grid_request_count_lines (&request);
1186 lines = &request.lines[0];
1187 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1188 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1189 lines = &request.lines[1];
1190 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1191 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1193 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1194 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1195 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1197 gtk_grid_request_run (&request, orientation, TRUE);
1198 gtk_grid_request_sum (&request, orientation, minimum, natural);
1202 gtk_grid_get_preferred_width (GtkWidget *widget,
1206 GtkGrid *grid = GTK_GRID (widget);
1208 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1209 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1211 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1215 gtk_grid_get_preferred_height (GtkWidget *widget,
1219 GtkGrid *grid = GTK_GRID (widget);
1221 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1222 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1224 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1228 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1233 GtkGrid *grid = GTK_GRID (widget);
1235 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1236 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1238 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1242 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1247 GtkGrid *grid = GTK_GRID (widget);
1249 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1250 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1252 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1256 allocate_child (GtkGridRequest *request,
1257 GtkOrientation orientation,
1258 GtkGridChild *child,
1262 GtkGridPrivate *priv = request->grid->priv;
1263 GtkGridLineData *linedata;
1264 GtkGridLines *lines;
1266 GtkGridChildAttach *attach;
1269 linedata = &priv->linedata[orientation];
1270 lines = &request->lines[orientation];
1271 attach = &child->attach[orientation];
1273 *position = lines->lines[attach->pos - lines->min].position;
1275 *size = (attach->span - 1) * linedata->spacing;
1276 for (i = 0; i < attach->span; i++)
1278 line = &lines->lines[attach->pos - lines->min + i];
1279 *size += line->allocation;
1284 gtk_grid_request_allocate_children (GtkGridRequest *request)
1286 GtkGridPrivate *priv = request->grid->priv;
1288 GtkGridChild *child;
1289 GtkAllocation allocation;
1290 GtkAllocation child_allocation;
1291 gint x, y, width, height;
1293 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1295 for (list = priv->children; list; list = list->next)
1299 if (!gtk_widget_get_visible (child->widget))
1302 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1303 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1305 child_allocation.x = allocation.x + x;
1306 child_allocation.y = allocation.y + y;
1307 child_allocation.width = MAX (1, width);
1308 child_allocation.height = MAX (1, height);
1310 if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
1311 child_allocation.x = allocation.x + allocation.width
1312 - (child_allocation.x - allocation.x) - child_allocation.width;
1314 gtk_widget_size_allocate (child->widget, &child_allocation);
1318 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1321 gtk_grid_size_allocate (GtkWidget *widget,
1322 GtkAllocation *allocation)
1324 GtkGrid *grid = GTK_GRID (widget);
1325 GtkGridPrivate *priv = grid->priv;
1326 GtkGridRequest request;
1327 GtkGridLines *lines;
1328 GtkOrientation orientation;
1330 if (priv->children == NULL)
1332 gtk_widget_set_allocation (widget, allocation);
1336 request.grid = grid;
1338 gtk_grid_request_count_lines (&request);
1339 lines = &request.lines[0];
1340 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1341 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1342 lines = &request.lines[1];
1343 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1344 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1346 gtk_widget_set_allocation (widget, allocation);
1348 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1349 orientation = GTK_ORIENTATION_HORIZONTAL;
1351 orientation = GTK_ORIENTATION_VERTICAL;
1353 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1354 gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
1355 gtk_grid_request_run (&request, orientation, TRUE);
1356 gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
1358 gtk_grid_request_position (&request, 0);
1359 gtk_grid_request_position (&request, 1);
1361 gtk_grid_request_allocate_children (&request);
1365 gtk_grid_class_init (GtkGridClass *class)
1367 GObjectClass *object_class = G_OBJECT_CLASS (class);
1368 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1369 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1371 object_class->get_property = gtk_grid_get_property;
1372 object_class->set_property = gtk_grid_set_property;
1374 widget_class->size_allocate = gtk_grid_size_allocate;
1375 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1376 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1377 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1378 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1380 container_class->add = gtk_grid_add;
1381 container_class->remove = gtk_grid_remove;
1382 container_class->forall = gtk_grid_forall;
1383 container_class->child_type = gtk_grid_child_type;
1384 container_class->set_child_property = gtk_grid_set_child_property;
1385 container_class->get_child_property = gtk_grid_get_child_property;
1386 gtk_container_class_handle_border_width (container_class);
1388 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1390 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1391 g_param_spec_int ("row-spacing",
1393 P_("The amount of space between two consecutive rows"),
1395 GTK_PARAM_READWRITE));
1397 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1398 g_param_spec_int ("column-spacing",
1399 P_("Column spacing"),
1400 P_("The amount of space between two consecutive columns"),
1402 GTK_PARAM_READWRITE));
1404 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1405 g_param_spec_boolean ("row-homogeneous",
1406 P_("Row Homogeneous"),
1407 P_("If TRUE, the rows are all the same height"),
1409 GTK_PARAM_READWRITE));
1411 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1412 g_param_spec_boolean ("column-homogeneous",
1413 P_("Column Homogeneous"),
1414 P_("If TRUE, the columns are all the same width"),
1416 GTK_PARAM_READWRITE));
1418 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1419 g_param_spec_int ("left-attach",
1420 P_("Left attachment"),
1421 P_("The column number to attach the left side of the child to"),
1422 G_MININT, G_MAXINT, 0,
1423 GTK_PARAM_READWRITE));
1425 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1426 g_param_spec_int ("top-attach",
1427 P_("Top attachment"),
1428 P_("The row number to attach the top side of a child widget to"),
1429 G_MININT, G_MAXINT, 0,
1430 GTK_PARAM_READWRITE));
1432 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1433 g_param_spec_int ("width",
1435 P_("The number of columns that a child spans"),
1437 GTK_PARAM_READWRITE));
1439 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1440 g_param_spec_int ("height",
1442 P_("The number of rows that a child spans"),
1444 GTK_PARAM_READWRITE));
1446 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1452 * Creates a new grid widget.
1454 * Returns: the new #GtkGrid
1459 return g_object_new (GTK_TYPE_GRID, NULL);
1465 * @child: the widget to add
1466 * @left: the column number to attach the left side of @child to
1467 * @top: the row number to attach the top side of @child to
1468 * @width: the number of columns that @child will span
1469 * @height: the number of rows that @child will span
1471 * Adds a widget to the grid.
1473 * The position of @child is determined by @left and @top. The
1474 * number of 'cells' that @child will occupy is determined by
1475 * @width and @height.
1478 gtk_grid_attach (GtkGrid *grid,
1485 g_return_if_fail (GTK_IS_GRID (grid));
1486 g_return_if_fail (GTK_IS_WIDGET (child));
1487 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1488 g_return_if_fail (width > 0);
1489 g_return_if_fail (height > 0);
1491 grid_attach (grid, child, left, top, width, height);
1495 * gtk_grid_attach_next_to:
1497 * @child: the widget to add
1498 * @sibling: (allow-none): the child of @grid that @child will be placed
1499 * next to, or %NULL to place @child at the beginning or end
1500 * @side: the side of @sibling that @child is positioned next to
1501 * @width: the number of columns that @child will span
1502 * @height: the number of rows that @child will span
1504 * Adds a widget to the grid.
1506 * The widget is placed next to @sibling, on the side determined by
1507 * @side. When @sibling is %NULL, the widget is placed in row (for
1508 * left or right placement) or column 0 (for top or bottom placement),
1509 * at the end indicated by @side.
1511 * Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and
1512 * @side == %GTK_POS_LEFT yields a layout of [3][2][1].
1515 gtk_grid_attach_next_to (GtkGrid *grid,
1518 GtkPositionType side,
1522 GtkGridChild *grid_sibling;
1525 g_return_if_fail (GTK_IS_GRID (grid));
1526 g_return_if_fail (GTK_IS_WIDGET (child));
1527 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1528 g_return_if_fail (sibling == NULL || gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1529 g_return_if_fail (width > 0);
1530 g_return_if_fail (height > 0);
1534 grid_sibling = find_grid_child (grid, sibling);
1539 left = CHILD_LEFT (grid_sibling) - width;
1540 top = CHILD_TOP (grid_sibling);
1543 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1544 top = CHILD_TOP (grid_sibling);
1547 left = CHILD_LEFT (grid_sibling);
1548 top = CHILD_TOP (grid_sibling) - height;
1550 case GTK_POS_BOTTOM:
1551 left = CHILD_LEFT (grid_sibling);
1552 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1555 g_assert_not_reached ();
1563 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
1568 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1573 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
1576 case GTK_POS_BOTTOM:
1578 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1581 g_assert_not_reached ();
1585 grid_attach (grid, child, left, top, width, height);
1589 * gtk_grid_get_child_at:
1591 * @left: the left edge of the cell
1592 * @top: the top edge of the cell
1594 * Gets the child of @grid whose area covers the grid
1595 * cell whose upper left corner is at @left, @top.
1597 * Returns: (transfer none): the child at the given position, or %NULL
1602 gtk_grid_get_child_at (GtkGrid *grid,
1606 GtkGridPrivate *priv;
1607 GtkGridChild *child;
1610 g_return_val_if_fail (GTK_IS_GRID (grid), NULL);
1614 for (list = priv->children; list; list = list->next)
1618 if (CHILD_LEFT (child) <= left &&
1619 CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
1620 CHILD_TOP (child) <= top &&
1621 CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
1622 return child->widget;
1629 * gtk_grid_insert_row:
1631 * @position: the position to insert the row at
1633 * Inserts a row at the specified position.
1635 * Children which are attached at or below this position
1636 * are moved one row down. Children which span across this
1637 * position are grown to span the new row.
1642 gtk_grid_insert_row (GtkGrid *grid,
1645 GtkGridPrivate *priv;
1646 GtkGridChild *child;
1650 g_return_if_fail (GTK_IS_GRID (grid));
1654 for (list = priv->children; list; list = list->next)
1658 top = CHILD_TOP (child);
1659 height = CHILD_HEIGHT (child);
1661 if (top >= position)
1663 CHILD_TOP (child) = top + 1;
1664 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
1666 else if (top + height > position)
1668 CHILD_HEIGHT (child) = height + 1;
1669 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
1675 * gtk_grid_insert_column:
1677 * @position: the position to insert the column at
1679 * Inserts a column at the specified position.
1681 * Children which are attached at or to the right of this position
1682 * are moved one column to the right. Children which span across this
1683 * position are grown to span the new column.
1688 gtk_grid_insert_column (GtkGrid *grid,
1691 GtkGridPrivate *priv;
1692 GtkGridChild *child;
1696 g_return_if_fail (GTK_IS_GRID (grid));
1700 for (list = priv->children; list; list = list->next)
1704 left = CHILD_LEFT (child);
1705 width = CHILD_WIDTH (child);
1707 if (left >= position)
1709 CHILD_LEFT (child) = left + 1;
1710 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
1712 else if (left + width > position)
1714 CHILD_WIDTH (child) = width + 1;
1715 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
1721 * gtk_grid_insert_next_to:
1723 * @sibling: the child of @grid that the new row or column will be
1725 * @side: the side of @sibling that @child is positioned next to
1727 * Inserts a row or column at the specified position.
1729 * The new row or column is placed next to @sibling, on the side
1730 * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
1731 * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
1732 * a column is inserted.
1737 gtk_grid_insert_next_to (GtkGrid *grid,
1739 GtkPositionType side)
1741 GtkGridChild *child;
1743 g_return_if_fail (GTK_IS_GRID (grid));
1744 g_return_if_fail (GTK_IS_WIDGET (sibling));
1745 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1747 child = find_grid_child (grid, sibling);
1752 gtk_grid_insert_column (grid, CHILD_LEFT (child));
1755 gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
1758 gtk_grid_insert_row (grid, CHILD_TOP (child));
1760 case GTK_POS_BOTTOM:
1761 gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
1764 g_assert_not_reached ();
1769 * gtk_grid_set_row_homogeneous:
1771 * @homogeneous: %TRUE to make rows homogeneous
1773 * Sets whether all rows of @grid will have the same height.
1776 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1777 gboolean homogeneous)
1779 GtkGridPrivate *priv;
1780 g_return_if_fail (GTK_IS_GRID (grid));
1784 /* Yes, homogeneous rows means all the columns have the same size */
1785 if (COLUMNS (priv)->homogeneous != homogeneous)
1787 COLUMNS (priv)->homogeneous = homogeneous;
1789 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1790 gtk_widget_queue_resize (GTK_WIDGET (grid));
1792 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1797 * gtk_grid_get_row_homogeneous:
1800 * Returns whether all rows of @grid have the same height.
1802 * Returns: whether all rows of @grid have the same height.
1805 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1807 GtkGridPrivate *priv;
1808 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1812 return COLUMNS (priv)->homogeneous;
1816 * gtk_grid_set_column_homogeneous:
1818 * @homogeneous: %TRUE to make columns homogeneous
1820 * Sets whether all columns of @grid will have the same width.
1823 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1824 gboolean homogeneous)
1826 GtkGridPrivate *priv;
1827 g_return_if_fail (GTK_IS_GRID (grid));
1831 /* Yes, homogeneous columns means all the rows have the same size */
1832 if (ROWS (priv)->homogeneous != homogeneous)
1834 ROWS (priv)->homogeneous = homogeneous;
1836 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1837 gtk_widget_queue_resize (GTK_WIDGET (grid));
1839 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1844 * gtk_grid_get_column_homogeneous:
1847 * Returns whether all columns of @grid have the same width.
1849 * Returns: whether all columns of @grid have the same width.
1852 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1854 GtkGridPrivate *priv;
1855 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1859 return ROWS (priv)->homogeneous;
1863 * gtk_grid_set_row_spacing:
1865 * @spacing: the amount of space to insert between rows
1867 * Sets the amount of space between rows of @grid.
1870 gtk_grid_set_row_spacing (GtkGrid *grid,
1873 GtkGridPrivate *priv;
1874 g_return_if_fail (GTK_IS_GRID (grid));
1875 g_return_if_fail (spacing <= G_MAXINT16);
1879 if (COLUMNS (priv)->spacing != spacing)
1881 COLUMNS (priv)->spacing = spacing;
1883 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1884 gtk_widget_queue_resize (GTK_WIDGET (grid));
1886 g_object_notify (G_OBJECT (grid), "row-spacing");
1891 * gtk_grid_get_row_spacing:
1894 * Returns the amount of space between the rows of @grid.
1896 * Returns: the row spacing of @grid
1899 gtk_grid_get_row_spacing (GtkGrid *grid)
1901 GtkGridPrivate *priv;
1902 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1906 return COLUMNS (priv)->spacing;
1910 * gtk_grid_set_column_spacing:
1912 * @spacing: the amount of space to insert between columns
1914 * Sets the amount of space between columns of @grid.
1917 gtk_grid_set_column_spacing (GtkGrid *grid,
1920 GtkGridPrivate *priv;
1921 g_return_if_fail (GTK_IS_GRID (grid));
1922 g_return_if_fail (spacing <= G_MAXINT16);
1926 if (ROWS (priv)->spacing != spacing)
1928 ROWS (priv)->spacing = spacing;
1930 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1931 gtk_widget_queue_resize (GTK_WIDGET (grid));
1933 g_object_notify (G_OBJECT (grid), "column-spacing");
1938 * gtk_grid_get_column_spacing:
1941 * Returns the amount of space between the columns of @grid.
1943 * Returns: the column spacing of @grid
1946 gtk_grid_get_column_spacing (GtkGrid *grid)
1948 GtkGridPrivate *priv;
1950 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1954 return ROWS (priv)->spacing;