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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include "gtkorientableprivate.h"
28 #include "gtksizerequest.h"
29 #include "gtkprivate.h"
35 * @Short_description: Pack widgets in a rows and columns
37 * @See_also: #GtkTable, #GtkHBox, #GtkVBox
39 * GtkGrid is a container which arranges its child widgets in
40 * rows and columns. It is a very similar to #GtkTable and #GtkBox,
41 * but it consistently uses #GtkWidget's #GtkWidget:margin and #GtkWidget:expand
42 * properties instead of custom child properties, and it fully supports
43 * <link linkend="geometry-management">height-for-width geometry management</link>.
45 * Children are added using gtk_grid_attach(). They can span multiple
46 * rows or columns. It is also possible to add a child next to an
47 * existing child, using gtk_grid_attach_next_to().
49 * GtkGrid can be used like a #GtkBox by just using gtk_container_add(),
50 * which will place children next to each other in the direction determined
51 * by the #GtkOrientable:orientation property.
54 typedef struct _GtkGridChild GtkGridChild;
55 typedef struct _GtkGridChildAttach GtkGridChildAttach;
56 typedef struct _GtkGridLine GtkGridLine;
57 typedef struct _GtkGridLines GtkGridLines;
58 typedef struct _GtkGridLineData GtkGridLineData;
59 typedef struct _GtkGridRequest GtkGridRequest;
61 struct _GtkGridChildAttach
70 GtkGridChildAttach attach[2];
73 #define CHILD_LEFT(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].pos)
74 #define CHILD_WIDTH(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].span)
75 #define CHILD_TOP(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].pos)
76 #define CHILD_HEIGHT(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].span)
78 /* A GtkGridLineData struct contains row/column specific parts
81 struct _GtkGridLineData
84 guint homogeneous : 1;
87 struct _GtkGridPrivate
91 GtkOrientation orientation;
93 GtkGridLineData linedata[2];
96 #define ROWS(priv) (&(priv)->linedata[GTK_ORIENTATION_HORIZONTAL])
97 #define COLUMNS(priv) (&(priv)->linedata[GTK_ORIENTATION_VERTICAL])
99 /* A GtkGridLine struct represents a single row or column
100 * during size requests
109 guint need_expand : 1;
120 struct _GtkGridRequest
123 GtkGridLines lines[2];
133 PROP_ROW_HOMOGENEOUS,
134 PROP_COLUMN_HOMOGENEOUS
140 CHILD_PROP_LEFT_ATTACH,
141 CHILD_PROP_TOP_ATTACH,
146 G_DEFINE_TYPE_WITH_CODE (GtkGrid, gtk_grid, GTK_TYPE_CONTAINER,
147 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
151 gtk_grid_get_property (GObject *object,
156 GtkGrid *grid = GTK_GRID (object);
157 GtkGridPrivate *priv = grid->priv;
161 case PROP_ORIENTATION:
162 g_value_set_enum (value, priv->orientation);
165 case PROP_ROW_SPACING:
166 g_value_set_int (value, COLUMNS (priv)->spacing);
169 case PROP_COLUMN_SPACING:
170 g_value_set_int (value, ROWS (priv)->spacing);
173 case PROP_ROW_HOMOGENEOUS:
174 g_value_set_boolean (value, COLUMNS (priv)->homogeneous);
177 case PROP_COLUMN_HOMOGENEOUS:
178 g_value_set_boolean (value, ROWS (priv)->homogeneous);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gtk_grid_set_orientation (GtkGrid *grid,
189 GtkOrientation orientation)
191 GtkGridPrivate *priv = grid->priv;
193 if (priv->orientation != orientation)
195 priv->orientation = orientation;
196 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
198 g_object_notify (G_OBJECT (grid), "orientation");
203 gtk_grid_set_property (GObject *object,
208 GtkGrid *grid = GTK_GRID (object);
212 case PROP_ORIENTATION:
213 gtk_grid_set_orientation (grid, g_value_get_enum (value));
216 case PROP_ROW_SPACING:
217 gtk_grid_set_row_spacing (grid, g_value_get_int (value));
220 case PROP_COLUMN_SPACING:
221 gtk_grid_set_column_spacing (grid, g_value_get_int (value));
224 case PROP_ROW_HOMOGENEOUS:
225 gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
228 case PROP_COLUMN_HOMOGENEOUS:
229 gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 static GtkGridChild *
239 find_grid_child (GtkGrid *grid,
242 GtkGridPrivate *priv = grid->priv;
246 for (list = priv->children; list; list = list->next)
250 if (child->widget == widget)
258 gtk_grid_get_child_property (GtkContainer *container,
264 GtkGrid *grid = GTK_GRID (container);
265 GtkGridChild *grid_child;
267 grid_child = find_grid_child (grid, child);
269 if (grid_child == NULL)
271 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
277 case CHILD_PROP_LEFT_ATTACH:
278 g_value_set_int (value, CHILD_LEFT (grid_child));
281 case CHILD_PROP_TOP_ATTACH:
282 g_value_set_int (value, CHILD_TOP (grid_child));
285 case CHILD_PROP_WIDTH:
286 g_value_set_int (value, CHILD_WIDTH (grid_child));
289 case CHILD_PROP_HEIGHT:
290 g_value_set_int (value, CHILD_HEIGHT (grid_child));
294 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
300 gtk_grid_set_child_property (GtkContainer *container,
306 GtkGrid *grid = GTK_GRID (container);
307 GtkGridChild *grid_child;
309 grid_child = find_grid_child (grid, child);
311 if (grid_child == NULL)
313 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
319 case CHILD_PROP_LEFT_ATTACH:
320 CHILD_LEFT (grid_child) = g_value_get_int (value);
323 case CHILD_PROP_TOP_ATTACH:
324 CHILD_TOP (grid_child) = g_value_get_int (value);
327 case CHILD_PROP_WIDTH:
328 CHILD_WIDTH (grid_child) = g_value_get_int (value);
331 case CHILD_PROP_HEIGHT:
332 CHILD_HEIGHT (grid_child) = g_value_get_int (value);
336 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
340 if (gtk_widget_get_visible (child) &&
341 gtk_widget_get_visible (GTK_WIDGET (grid)))
342 gtk_widget_queue_resize (child);
346 gtk_grid_init (GtkGrid *grid)
348 GtkGridPrivate *priv;
350 grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, GTK_TYPE_GRID, GtkGridPrivate);
353 gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
354 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);
356 priv->children = NULL;
357 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
359 priv->linedata[0].spacing = 0;
360 priv->linedata[1].spacing = 0;
362 priv->linedata[0].homogeneous = FALSE;
363 priv->linedata[1].homogeneous = FALSE;
367 grid_attach (GtkGrid *grid,
374 GtkGridPrivate *priv = grid->priv;
377 child = g_slice_new (GtkGridChild);
378 child->widget = widget;
379 CHILD_LEFT (child) = left;
380 CHILD_TOP (child) = top;
381 CHILD_WIDTH (child) = width;
382 CHILD_HEIGHT (child) = height;
384 priv->children = g_list_prepend (priv->children, child);
386 gtk_widget_set_parent (widget, GTK_WIDGET (grid));
389 /* Find the position 'touching' existing
390 * children. @orientation and @max determine
391 * from which direction to approach (horizontal
392 * + max = right, vertical + !max = top, etc).
393 * @op_pos, @op_span determine the rows/columns
394 * in which the touching has to happen.
397 find_attach_position (GtkGrid *grid,
398 GtkOrientation orientation,
403 GtkGridPrivate *priv = grid->priv;
404 GtkGridChild *grid_child;
405 GtkGridChildAttach *attach;
406 GtkGridChildAttach *opposite;
418 for (list = priv->children; list; list = list->next)
420 grid_child = list->data;
422 attach = &grid_child->attach[orientation];
423 opposite = &grid_child->attach[1 - orientation];
425 /* check if the ranges overlap */
426 if (opposite->pos <= op_pos + op_span && op_pos <= opposite->pos + opposite->span)
431 pos = MAX (pos, attach->pos + attach->span);
433 pos = MIN (pos, attach->pos);
444 gtk_grid_add (GtkContainer *container,
447 GtkGrid *grid = GTK_GRID (container);
448 GtkGridPrivate *priv = grid->priv;
449 gint pos[2] = { 0, 0 };
451 pos[priv->orientation] = find_attach_position (grid, priv->orientation, 0, 1, TRUE);
452 grid_attach (grid, child, pos[0], pos[1], 1, 1);
456 gtk_grid_remove (GtkContainer *container,
459 GtkGrid *grid = GTK_GRID (container);
460 GtkGridPrivate *priv = grid->priv;
461 GtkGridChild *grid_child;
464 for (list = priv->children; list; list = list->next)
466 grid_child = list->data;
468 if (grid_child->widget == child)
470 gboolean was_visible = gtk_widget_get_visible (child);
472 gtk_widget_unparent (child);
474 priv->children = g_list_remove (priv->children, grid_child);
476 g_slice_free (GtkGridChild, grid_child);
478 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (grid)))
479 gtk_widget_queue_resize (GTK_WIDGET (grid));
487 gtk_grid_forall (GtkContainer *container,
488 gboolean include_internals,
489 GtkCallback callback,
490 gpointer callback_data)
492 GtkGrid *grid = GTK_GRID (container);
493 GtkGridPrivate *priv = grid->priv;
497 list = priv->children;
503 (* callback) (child->widget, callback_data);
508 gtk_grid_child_type (GtkContainer *container)
510 return GTK_TYPE_WIDGET;
513 /* Calculates the min and max numbers for both orientations.
516 gtk_grid_request_count_lines (GtkGridRequest *request)
518 GtkGridPrivate *priv = request->grid->priv;
520 GtkGridChildAttach *attach;
525 min[0] = min[1] = G_MAXINT;
526 max[0] = max[1] = G_MININT;
528 for (list = priv->children; list; list = list->next)
531 attach = child->attach;
533 min[0] = MIN (min[0], attach[0].pos);
534 max[0] = MAX (max[0], attach[0].pos + attach[0].span);
535 min[1] = MIN (min[1], attach[1].pos);
536 max[1] = MAX (max[1], attach[1].pos + attach[1].span);
539 request->lines[0].min = min[0];
540 request->lines[0].max = max[0];
541 request->lines[1].min = min[1];
542 request->lines[1].max = max[1];
545 /* Sets line sizes to 0 and marks lines as expand
546 * if they have a non-spanning expanding child.
549 gtk_grid_request_init (GtkGridRequest *request,
550 GtkOrientation orientation)
552 GtkGridPrivate *priv = request->grid->priv;
554 GtkGridChildAttach *attach;
559 lines = &request->lines[orientation];
561 for (i = 0; i < lines->max - lines->min; i++)
563 lines->lines[i].minimum = 0;
564 lines->lines[i].natural = 0;
565 lines->lines[i].expand = FALSE;
568 for (list = priv->children; list; list = list->next)
572 attach = &child->attach[orientation];
573 if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
574 lines->lines[attach->pos - lines->min].expand = TRUE;
578 /* Sums allocations for lines spanned by child and their spacing.
581 compute_allocation_for_child (GtkGridRequest *request,
583 GtkOrientation orientation)
585 GtkGridPrivate *priv = request->grid->priv;
586 GtkGridLineData *linedata;
589 GtkGridChildAttach *attach;
593 linedata = &priv->linedata[orientation];
594 lines = &request->lines[orientation];
595 attach = &child->attach[orientation];
597 size = (attach->span - 1) * linedata->spacing;
598 for (i = 0; i < attach->span; i++)
600 line = &lines->lines[attach->pos - lines->min + i];
601 size += line->allocation;
608 compute_request_for_child (GtkGridRequest *request,
610 GtkOrientation orientation,
619 size = compute_allocation_for_child (request, child, 1 - orientation);
620 if (orientation == GTK_ORIENTATION_HORIZONTAL)
621 gtk_widget_get_preferred_width_for_height (child->widget,
625 gtk_widget_get_preferred_height_for_width (child->widget,
631 if (orientation == GTK_ORIENTATION_HORIZONTAL)
632 gtk_widget_get_preferred_width (child->widget, minimum, natural);
634 gtk_widget_get_preferred_height (child->widget, minimum, natural);
638 /* Sets requisition to max. of non-spanning children.
639 * If contextual is TRUE, requires allocations of
640 * lines in the opposite orientation to be set.
643 gtk_grid_request_non_spanning (GtkGridRequest *request,
644 GtkOrientation orientation,
647 GtkGridPrivate *priv = request->grid->priv;
649 GtkGridChildAttach *attach;
656 lines = &request->lines[orientation];
658 for (list = priv->children; list; list = list->next)
662 if (!gtk_widget_get_visible (child->widget))
665 attach = &child->attach[orientation];
666 if (attach->span != 1)
669 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
671 line = &lines->lines[attach->pos - lines->min];
672 line->minimum = MAX (line->minimum, minimum);
673 line->natural = MAX (line->natural, natural);
677 /* Enforce homogeneous sizes.
680 gtk_grid_request_homogeneous (GtkGridRequest *request,
681 GtkOrientation orientation)
683 GtkGridPrivate *priv = request->grid->priv;
684 GtkGridLineData *linedata;
686 gint minimum, natural;
689 linedata = &priv->linedata[orientation];
690 lines = &request->lines[orientation];
692 if (!linedata->homogeneous)
698 for (i = 0; i < lines->max - lines->min; i++)
700 minimum = MAX (minimum, lines->lines[i].minimum);
701 natural = MAX (natural, lines->lines[i].natural);
704 for (i = 0; i < lines->max - lines->min; i++)
706 lines->lines[i].minimum = minimum;
707 lines->lines[i].natural = natural;
711 /* Deals with spanning children.
712 * Requires expand fields of lines to be set for
713 * non-spanning children.
716 gtk_grid_request_spanning (GtkGridRequest *request,
717 GtkOrientation orientation,
720 GtkGridPrivate *priv = request->grid->priv;
723 GtkGridChildAttach *attach;
724 GtkGridLineData *linedata;
732 gboolean force_expand;
738 linedata = &priv->linedata[orientation];
739 lines = &request->lines[orientation];
741 for (list = priv->children; list; list = list->next)
745 if (!gtk_widget_get_visible (child->widget))
748 attach = &child->attach[orientation];
749 if (attach->span == 1)
752 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
754 span_minimum = (attach->span - 1) * linedata->spacing;
755 span_natural = (attach->span - 1) * linedata->spacing;
757 force_expand = FALSE;
758 for (i = 0; i < attach->span; i++)
760 line = &lines->lines[attach->pos - lines->min + i];
761 span_minimum += line->minimum;
762 span_natural += line->natural;
766 if (span_expand == 0)
768 span_expand = attach->span;
772 /* If we need to request more space for this child to fill
773 * its requisition, then divide up the needed space amongst the
774 * lines it spans, favoring expandable lines if any.
776 if (span_minimum < minimum)
778 extra = minimum - span_minimum;
779 expand = span_expand;
780 for (i = 0; i < attach->span; i++)
782 line = &lines->lines[attach->pos - lines->min + i];
783 if (force_expand || line->expand)
785 line_extra = extra / expand;
786 line->minimum += line_extra;
793 if (span_natural < natural)
795 extra = natural - span_natural;
796 expand = span_expand;
797 for (i = 0; i < attach->span; i++)
799 line = &lines->lines[attach->pos - lines->min + i];
800 if (force_expand || line->expand)
802 line_extra = extra / expand;
803 line->natural += line_extra;
812 /* Marks empty and expanding lines and counts them.
815 gtk_grid_request_compute_expand (GtkGridRequest *request,
816 GtkOrientation orientation,
817 gint *nonempty_lines,
820 GtkGridPrivate *priv = request->grid->priv;
822 GtkGridChildAttach *attach;
831 lines = &request->lines[orientation];
833 for (i = 0; i < lines->max - lines->min; i++)
835 lines->lines[i].need_expand = FALSE;
836 lines->lines[i].expand = FALSE;
837 lines->lines[i].empty = TRUE;
840 for (list = priv->children; list; list = list->next)
844 if (!gtk_widget_get_visible (child->widget))
847 attach = &child->attach[orientation];
848 if (attach->span != 1)
851 line = &lines->lines[attach->pos - lines->min];
853 if (gtk_widget_compute_expand (child->widget, orientation))
857 for (list = priv->children; list; list = list->next)
861 if (!gtk_widget_get_visible (child->widget))
864 attach = &child->attach[orientation];
865 if (attach->span == 1)
869 for (i = 0; i < attach->span; i++)
871 line = &lines->lines[attach->pos - lines->min + i];
877 if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
879 for (i = 0; i < attach->span; i++)
881 line = &lines->lines[attach->pos - lines->min + i];
882 line->need_expand = TRUE;
889 for (i = 0; i < lines->max - lines->min; i++)
891 line = &lines->lines[i];
893 if (line->need_expand)
904 *nonempty_lines = lines->max - lines->min - empty;
907 *expand_lines = expand;
910 /* Sums the minimum and natural fields of lines and their spacing.
913 gtk_grid_request_sum (GtkGridRequest *request,
914 GtkOrientation orientation,
918 GtkGridPrivate *priv = request->grid->priv;
919 GtkGridLineData *linedata;
925 gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
927 linedata = &priv->linedata[orientation];
928 lines = &request->lines[orientation];
930 min = (nonempty - 1) * linedata->spacing;
931 nat = (nonempty - 1) * linedata->spacing;
933 for (i = 0; i < lines->max - lines->min; i++)
935 min += lines->lines[i].minimum;
936 nat += lines->lines[i].natural;
946 /* Computes minimum and natural fields of lines.
947 * When contextual is TRUE, requires allocation of
948 * lines in the opposite orientation to be set.
951 gtk_grid_request_run (GtkGridRequest *request,
952 GtkOrientation orientation,
955 gtk_grid_request_init (request, orientation);
956 gtk_grid_request_non_spanning (request, orientation, contextual);
957 gtk_grid_request_homogeneous (request, orientation);
958 gtk_grid_request_spanning (request, orientation, contextual);
959 gtk_grid_request_homogeneous (request, orientation);
962 /* Requires that the minimum and natural fields of lines
963 * have been set, computes the allocation field of lines
964 * by distributing total_size among lines.
967 gtk_grid_request_allocate (GtkGridRequest *request,
968 GtkOrientation orientation,
971 GtkGridPrivate *priv = request->grid->priv;
972 GtkGridLineData *linedata;
978 GtkRequestedSize *sizes;
983 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
985 linedata = &priv->linedata[orientation];
986 lines = &request->lines[orientation];
988 size = total_size - (nonempty - 1) * linedata->spacing;
990 if (linedata->homogeneous)
992 extra = size / nonempty;
993 rest = size % nonempty;
995 for (i = 0; i < lines->max - lines->min; i++)
997 line = &lines->lines[i];
1001 line->allocation = extra;
1004 line->allocation += 1;
1011 sizes = g_newa (GtkRequestedSize, nonempty);
1014 for (i = 0; i < lines->max - lines->min; i++)
1016 line = &lines->lines[i];
1020 size -= line->minimum;
1022 sizes[j].minimum_size = line->minimum;
1023 sizes[j].natural_size = line->natural;
1024 sizes[j].data = line;
1028 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1032 extra = size / expand;
1033 rest = size % expand;
1042 for (i = 0; i < lines->max - lines->min; i++)
1044 line = &lines->lines[i];
1048 g_assert (line == sizes[j].data);
1050 line->allocation = sizes[j].minimum_size;
1053 line->allocation += extra;
1056 line->allocation += 1;
1066 /* Computes the position fields from allocation and spacing.
1069 gtk_grid_request_position (GtkGridRequest *request,
1070 GtkOrientation orientation)
1072 GtkGridPrivate *priv = request->grid->priv;
1073 GtkGridLineData *linedata;
1074 GtkGridLines *lines;
1079 linedata = &priv->linedata[orientation];
1080 lines = &request->lines[orientation];
1083 for (i = 0; i < lines->max - lines->min; i++)
1085 line = &lines->lines[i];
1088 line->position = position;
1089 position += line->allocation + linedata->spacing;
1095 gtk_grid_get_size (GtkGrid *grid,
1096 GtkOrientation orientation,
1100 GtkGridRequest request;
1101 GtkGridLines *lines;
1109 if (grid->priv->children == NULL)
1112 request.grid = grid;
1113 gtk_grid_request_count_lines (&request);
1114 lines = &request.lines[orientation];
1115 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1116 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1118 gtk_grid_request_run (&request, orientation, FALSE);
1119 gtk_grid_request_sum (&request, orientation, minimum, natural);
1123 gtk_grid_get_size_for_size (GtkGrid *grid,
1124 GtkOrientation orientation,
1129 GtkGridRequest request;
1130 GtkGridLines *lines;
1139 if (grid->priv->children == NULL)
1142 request.grid = grid;
1143 gtk_grid_request_count_lines (&request);
1144 lines = &request.lines[0];
1145 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1146 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1147 lines = &request.lines[1];
1148 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1149 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1151 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1152 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1153 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1155 gtk_grid_request_run (&request, orientation, TRUE);
1156 gtk_grid_request_sum (&request, orientation, minimum, natural);
1160 gtk_grid_get_preferred_width (GtkWidget *widget,
1164 GtkGrid *grid = GTK_GRID (widget);
1166 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1167 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1169 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1173 gtk_grid_get_preferred_height (GtkWidget *widget,
1177 GtkGrid *grid = GTK_GRID (widget);
1179 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1180 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1182 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1186 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1191 GtkGrid *grid = GTK_GRID (widget);
1193 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1194 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1196 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1200 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1205 GtkGrid *grid = GTK_GRID (widget);
1207 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1208 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1210 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1214 allocate_child (GtkGridRequest *request,
1215 GtkOrientation orientation,
1216 GtkGridChild *child,
1220 GtkGridPrivate *priv = request->grid->priv;
1221 GtkGridLineData *linedata;
1222 GtkGridLines *lines;
1224 GtkGridChildAttach *attach;
1227 linedata = &priv->linedata[orientation];
1228 lines = &request->lines[orientation];
1229 attach = &child->attach[orientation];
1231 *position = lines->lines[attach->pos - lines->min].position;
1233 *size = (attach->span - 1) * linedata->spacing;
1234 for (i = 0; i < attach->span; i++)
1236 line = &lines->lines[attach->pos - lines->min + i];
1237 *size += line->allocation;
1242 gtk_grid_request_allocate_children (GtkGridRequest *request)
1244 GtkGridPrivate *priv = request->grid->priv;
1246 GtkGridChild *child;
1247 GtkAllocation allocation;
1248 GtkAllocation child_allocation;
1249 gint x, y, width, height;
1251 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1253 for (list = priv->children; list; list = list->next)
1257 if (!gtk_widget_get_visible (child->widget))
1260 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1261 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1263 child_allocation.x = allocation.x + x;
1264 child_allocation.y = allocation.y + y;
1265 child_allocation.width = MAX (1, width);
1266 child_allocation.height = MAX (1, height);
1268 if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
1269 child_allocation.x = allocation.x + allocation.width
1270 - (child_allocation.x - allocation.x) - child_allocation.width;
1272 gtk_widget_size_allocate (child->widget, &child_allocation);
1276 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1279 gtk_grid_size_allocate (GtkWidget *widget,
1280 GtkAllocation *allocation)
1282 GtkGrid *grid = GTK_GRID (widget);
1283 GtkGridPrivate *priv = grid->priv;
1284 GtkGridRequest request;
1285 GtkGridLines *lines;
1286 GtkOrientation orientation;
1288 if (priv->children == NULL)
1290 gtk_widget_set_allocation (widget, allocation);
1294 request.grid = grid;
1296 gtk_grid_request_count_lines (&request);
1297 lines = &request.lines[0];
1298 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1299 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1300 lines = &request.lines[1];
1301 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1302 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1304 gtk_widget_set_allocation (widget, allocation);
1306 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1307 orientation = GTK_ORIENTATION_HORIZONTAL;
1309 orientation = GTK_ORIENTATION_VERTICAL;
1311 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1312 gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
1313 gtk_grid_request_run (&request, orientation, TRUE);
1314 gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
1316 gtk_grid_request_position (&request, 0);
1317 gtk_grid_request_position (&request, 1);
1319 gtk_grid_request_allocate_children (&request);
1323 gtk_grid_class_init (GtkGridClass *class)
1325 GObjectClass *object_class = G_OBJECT_CLASS (class);
1326 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1327 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1329 object_class->get_property = gtk_grid_get_property;
1330 object_class->set_property = gtk_grid_set_property;
1332 widget_class->size_allocate = gtk_grid_size_allocate;
1333 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1334 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1335 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1336 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1338 container_class->add = gtk_grid_add;
1339 container_class->remove = gtk_grid_remove;
1340 container_class->forall = gtk_grid_forall;
1341 container_class->child_type = gtk_grid_child_type;
1342 container_class->set_child_property = gtk_grid_set_child_property;
1343 container_class->get_child_property = gtk_grid_get_child_property;
1344 gtk_container_class_handle_border_width (container_class);
1346 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1348 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1349 g_param_spec_int ("row-spacing",
1351 P_("The amount of space between two consecutive rows"),
1353 GTK_PARAM_READWRITE));
1355 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1356 g_param_spec_int ("column-spacing",
1357 P_("Column spacing"),
1358 P_("The amount of space between two consecutive columns"),
1360 GTK_PARAM_READWRITE));
1362 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1363 g_param_spec_boolean ("row-homogeneous",
1364 P_("Row Homogeneous"),
1365 P_("If TRUE, the rows are all the same height"),
1367 GTK_PARAM_READWRITE));
1369 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1370 g_param_spec_boolean ("column-homogeneous",
1371 P_("Column Homogeneous"),
1372 P_("If TRUE, the columns are all the same width"),
1374 GTK_PARAM_READWRITE));
1376 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1377 g_param_spec_int ("left-attach",
1378 P_("Left attachment"),
1379 P_("The column number to attach the left side of the child to"),
1380 G_MININT, G_MAXINT, 0,
1381 GTK_PARAM_READWRITE));
1383 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1384 g_param_spec_int ("top-attach",
1385 P_("Top attachment"),
1386 P_("The row number to attach the top side of a child widget to"),
1387 G_MININT, G_MAXINT, 0,
1388 GTK_PARAM_READWRITE));
1390 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1391 g_param_spec_int ("width",
1393 P_("The number of columns that a child spans"),
1395 GTK_PARAM_READWRITE));
1397 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1398 g_param_spec_int ("height",
1400 P_("The number of rows that a child spans"),
1402 GTK_PARAM_READWRITE));
1404 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1410 * Creates a new grid widget.
1412 * Returns: the new #GtkGrid
1417 return g_object_new (GTK_TYPE_GRID, NULL);
1423 * @child: the widget to add
1424 * @left: the column number to attach the left side of @child to
1425 * @top: the row number to attach the top side of @child to
1426 * @width: the number of columns that @child will span
1427 * @height: the number of rows that @child will span
1429 * Adds a widget to the grid.
1431 * The position of @child is determined by @left and @top. The
1432 * number of 'cells' that @child will occupy is determined by
1433 * @width and @height.
1436 gtk_grid_attach (GtkGrid *grid,
1443 g_return_if_fail (GTK_IS_GRID (grid));
1444 g_return_if_fail (GTK_IS_WIDGET (child));
1445 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1446 g_return_if_fail (width > 0);
1447 g_return_if_fail (height > 0);
1449 grid_attach (grid, child, left, top, width, height);
1453 * gtk_grid_attach_next_to:
1455 * @child: the widget to add
1456 * @sibling (allow-none): the child of @grid that @child will be placed
1457 * next to, or %NULL to place @child at the beginning or end
1458 * @side: the side of @sibling that @child is positioned next to
1459 * @width: the number of columns that @child will span
1460 * @height: the number of rows that @child will span
1462 * Adds a widget to the grid.
1464 * The widget is placed next to @sibling, on the side determined by
1465 * @side. When @sibling is %NULL, the widget is placed in row (for
1466 * left or right placement) or column 0 (for top or bottom placement),
1467 * at the end indicated by @side.
1470 gtk_grid_attach_next_to (GtkGrid *grid,
1473 GtkPositionType side,
1477 GtkGridChild *grid_sibling;
1480 g_return_if_fail (GTK_IS_GRID (grid));
1481 g_return_if_fail (GTK_IS_WIDGET (child));
1482 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1483 g_return_if_fail (sibling == NULL || gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1484 g_return_if_fail (width > 0);
1485 g_return_if_fail (height > 0);
1489 grid_sibling = find_grid_child (grid, sibling);
1494 left = CHILD_LEFT (grid_sibling) - width;
1495 top = CHILD_TOP (grid_sibling);
1498 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1499 top = CHILD_TOP (grid_sibling);
1502 left = CHILD_LEFT (grid_sibling);
1503 top = CHILD_TOP (grid_sibling) - height;
1505 case GTK_POS_BOTTOM:
1506 left = CHILD_LEFT (grid_sibling);
1507 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1510 g_assert_not_reached ();
1518 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1522 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
1527 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1529 case GTK_POS_BOTTOM:
1531 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
1534 g_assert_not_reached ();
1538 grid_attach (grid, child, left, top, width, height);
1542 * gtk_grid_get_child_at:
1544 * @left: the left edge of the cell
1545 * @top: the top edge of the cell
1547 * Gets the child of @grid whose area covers the grid
1548 * cell whose upper left corner is at @left, @top.
1550 * Returns: the child at the given position, or %NULL
1555 gtk_grid_get_child_at (GtkGrid *grid,
1559 GtkGridPrivate *priv = grid->priv;
1560 GtkGridChild *child;
1563 for (list = priv->children; list; list = list->next)
1567 if (CHILD_LEFT (child) <= left &&
1568 CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
1569 CHILD_TOP (child) <= top &&
1570 CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
1571 return child->widget;
1578 * gtk_grid_insert_row:
1580 * @position: the position to insert the row at
1582 * Inserts a row at the specified position.
1584 * Children which are attached at or below this position
1585 * are moved one row down. Children which span across this
1586 * position are grown to span the new row.
1591 gtk_grid_insert_row (GtkGrid *grid,
1594 GtkGridPrivate *priv = grid->priv;
1595 GtkGridChild *child;
1599 g_return_if_fail (GTK_IS_GRID (grid));
1601 for (list = priv->children; list; list = list->next)
1605 top = CHILD_TOP (child);
1606 height = CHILD_HEIGHT (child);
1608 if (top >= position)
1610 CHILD_TOP (child) = top + 1;
1611 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
1613 else if (top + height > position)
1615 CHILD_HEIGHT (child) = height + 1;
1616 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
1622 * gtk_grid_insert_column:
1624 * @position: the position to insert the column at
1626 * Inserts a column at the specified position.
1628 * Children which are attached at or to the right of this position
1629 * are moved one column to the right. Children which span across this
1630 * position are grown to span the new column.
1635 gtk_grid_insert_column (GtkGrid *grid,
1638 GtkGridPrivate *priv = grid->priv;
1639 GtkGridChild *child;
1643 g_return_if_fail (GTK_IS_GRID (grid));
1645 for (list = priv->children; list; list = list->next)
1649 left = CHILD_LEFT (child);
1650 width = CHILD_WIDTH (child);
1652 if (left >= position)
1654 CHILD_LEFT (child) = left + 1;
1655 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
1657 else if (left + width > position)
1659 CHILD_WIDTH (child) = width + 1;
1660 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
1666 * gtk_grid_insert_next_to:
1668 * @sibling: the child of @grid that the new row or column will be
1670 * @side: the side of @sibling that @child is positioned next to
1672 * Inserts a row or column at the specified position.
1674 * The new row or column is placed next to @sibling, on the side
1675 * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
1676 * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
1677 * a column is inserted.
1682 gtk_grid_insert_next_to (GtkGrid *grid,
1684 GtkPositionType side)
1686 GtkGridChild *child;
1688 g_return_if_fail (GTK_IS_GRID (grid));
1689 g_return_if_fail (GTK_IS_WIDGET (sibling));
1690 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1692 child = find_grid_child (grid, sibling);
1697 gtk_grid_insert_column (grid, CHILD_LEFT (child));
1700 gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
1703 gtk_grid_insert_row (grid, CHILD_TOP (child));
1705 case GTK_POS_BOTTOM:
1706 gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
1709 g_assert_not_reached ();
1714 * gtk_grid_set_row_homogeneous:
1716 * @homogeneous: %TRUE to make rows homogeneous
1718 * Sets whether all rows of @grid will have the same height.
1721 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1722 gboolean homogeneous)
1724 GtkGridPrivate *priv;
1725 g_return_if_fail (GTK_IS_GRID (grid));
1729 /* Yes, homogeneous rows means all the columns have the same size */
1730 if (COLUMNS (priv)->homogeneous != homogeneous)
1732 COLUMNS (priv)->homogeneous = homogeneous;
1734 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1735 gtk_widget_queue_resize (GTK_WIDGET (grid));
1737 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1742 * gtk_grid_get_row_homogeneous:
1745 * Returns whether all rows of @grid have the same height.
1747 * Returns: whether all rows of @grid have the same height.
1750 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1752 GtkGridPrivate *priv;
1753 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1757 return COLUMNS (priv)->homogeneous;
1761 * gtk_grid_set_column_homogeneous:
1763 * @homogeneous: %TRUE to make columns homogeneous
1765 * Sets whether all columns of @grid will have the same width.
1768 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1769 gboolean homogeneous)
1771 GtkGridPrivate *priv;
1772 g_return_if_fail (GTK_IS_GRID (grid));
1776 /* Yes, homogeneous columns means all the rows have the same size */
1777 if (ROWS (priv)->homogeneous != homogeneous)
1779 ROWS (priv)->homogeneous = homogeneous;
1781 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1782 gtk_widget_queue_resize (GTK_WIDGET (grid));
1784 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1789 * gtk_grid_get_column_homogeneous:
1792 * Returns whether all columns of @grid have the same width.
1794 * Returns: whether all columns of @grid have the same width.
1797 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1799 GtkGridPrivate *priv;
1800 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1804 return ROWS (priv)->homogeneous;
1808 * gtk_grid_set_row_spacing:
1810 * @spacing: the amount of space to insert between rows
1812 * Sets the amount of space between rows of @grid.
1815 gtk_grid_set_row_spacing (GtkGrid *grid,
1818 GtkGridPrivate *priv;
1819 g_return_if_fail (GTK_IS_GRID (grid));
1820 g_return_if_fail (spacing <= G_MAXINT16);
1824 if (COLUMNS (priv)->spacing != spacing)
1826 COLUMNS (priv)->spacing = spacing;
1828 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1829 gtk_widget_queue_resize (GTK_WIDGET (grid));
1831 g_object_notify (G_OBJECT (grid), "row-spacing");
1836 * gtk_grid_get_row_spacing:
1839 * Returns the amount of space between the rows of @grid.
1841 * Returns: the row spacing of @grid
1844 gtk_grid_get_row_spacing (GtkGrid *grid)
1846 GtkGridPrivate *priv;
1847 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1851 return COLUMNS (priv)->spacing;
1855 * gtk_grid_set_column_spacing:
1857 * @spacing: the amount of space to insert between columns
1859 * Sets the amount of space between columns of @grid.
1862 gtk_grid_set_column_spacing (GtkGrid *grid,
1865 GtkGridPrivate *priv;
1866 g_return_if_fail (GTK_IS_GRID (grid));
1867 g_return_if_fail (spacing <= G_MAXINT16);
1871 if (ROWS (priv)->spacing != spacing)
1873 ROWS (priv)->spacing = spacing;
1875 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1876 gtk_widget_queue_resize (GTK_WIDGET (grid));
1878 g_object_notify (G_OBJECT (grid), "column-spacing");
1883 * gtk_grid_get_column_spacing:
1886 * Returns the amount of space between the columns of @grid.
1888 * Returns: the column spacing of @grid
1891 gtk_grid_get_column_spacing (GtkGrid *grid)
1893 GtkGridPrivate *priv;
1895 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1899 return ROWS (priv)->spacing;