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];
934 min = (nonempty - 1) * linedata->spacing;
935 nat = (nonempty - 1) * linedata->spacing;
938 for (i = 0; i < lines->max - lines->min; i++)
940 min += lines->lines[i].minimum;
941 nat += lines->lines[i].natural;
951 /* Computes minimum and natural fields of lines.
952 * When contextual is TRUE, requires allocation of
953 * lines in the opposite orientation to be set.
956 gtk_grid_request_run (GtkGridRequest *request,
957 GtkOrientation orientation,
960 gtk_grid_request_init (request, orientation);
961 gtk_grid_request_non_spanning (request, orientation, contextual);
962 gtk_grid_request_homogeneous (request, orientation);
963 gtk_grid_request_spanning (request, orientation, contextual);
964 gtk_grid_request_homogeneous (request, orientation);
967 /* Requires that the minimum and natural fields of lines
968 * have been set, computes the allocation field of lines
969 * by distributing total_size among lines.
972 gtk_grid_request_allocate (GtkGridRequest *request,
973 GtkOrientation orientation,
976 GtkGridPrivate *priv = request->grid->priv;
977 GtkGridLineData *linedata;
983 GtkRequestedSize *sizes;
988 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
990 linedata = &priv->linedata[orientation];
991 lines = &request->lines[orientation];
993 size = total_size - (nonempty - 1) * linedata->spacing;
995 if (linedata->homogeneous)
997 extra = size / nonempty;
998 rest = size % nonempty;
1000 for (i = 0; i < lines->max - lines->min; i++)
1002 line = &lines->lines[i];
1006 line->allocation = extra;
1009 line->allocation += 1;
1016 sizes = g_newa (GtkRequestedSize, nonempty);
1019 for (i = 0; i < lines->max - lines->min; i++)
1021 line = &lines->lines[i];
1025 size -= line->minimum;
1027 sizes[j].minimum_size = line->minimum;
1028 sizes[j].natural_size = line->natural;
1029 sizes[j].data = line;
1033 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1037 extra = size / expand;
1038 rest = size % expand;
1047 for (i = 0; i < lines->max - lines->min; i++)
1049 line = &lines->lines[i];
1053 g_assert (line == sizes[j].data);
1055 line->allocation = sizes[j].minimum_size;
1058 line->allocation += extra;
1061 line->allocation += 1;
1071 /* Computes the position fields from allocation and spacing.
1074 gtk_grid_request_position (GtkGridRequest *request,
1075 GtkOrientation orientation)
1077 GtkGridPrivate *priv = request->grid->priv;
1078 GtkGridLineData *linedata;
1079 GtkGridLines *lines;
1084 linedata = &priv->linedata[orientation];
1085 lines = &request->lines[orientation];
1088 for (i = 0; i < lines->max - lines->min; i++)
1090 line = &lines->lines[i];
1093 line->position = position;
1094 position += line->allocation + linedata->spacing;
1100 gtk_grid_get_size (GtkGrid *grid,
1101 GtkOrientation orientation,
1105 GtkGridRequest request;
1106 GtkGridLines *lines;
1114 if (grid->priv->children == NULL)
1117 request.grid = grid;
1118 gtk_grid_request_count_lines (&request);
1119 lines = &request.lines[orientation];
1120 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1121 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1123 gtk_grid_request_run (&request, orientation, FALSE);
1124 gtk_grid_request_sum (&request, orientation, minimum, natural);
1128 gtk_grid_get_size_for_size (GtkGrid *grid,
1129 GtkOrientation orientation,
1134 GtkGridRequest request;
1135 GtkGridLines *lines;
1144 if (grid->priv->children == NULL)
1147 request.grid = grid;
1148 gtk_grid_request_count_lines (&request);
1149 lines = &request.lines[0];
1150 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1151 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1152 lines = &request.lines[1];
1153 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1154 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1156 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1157 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1158 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1160 gtk_grid_request_run (&request, orientation, TRUE);
1161 gtk_grid_request_sum (&request, orientation, minimum, natural);
1165 gtk_grid_get_preferred_width (GtkWidget *widget,
1169 GtkGrid *grid = GTK_GRID (widget);
1171 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1172 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1174 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1178 gtk_grid_get_preferred_height (GtkWidget *widget,
1182 GtkGrid *grid = GTK_GRID (widget);
1184 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1185 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1187 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1191 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1196 GtkGrid *grid = GTK_GRID (widget);
1198 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1199 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1201 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1205 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1210 GtkGrid *grid = GTK_GRID (widget);
1212 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1213 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1215 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1219 allocate_child (GtkGridRequest *request,
1220 GtkOrientation orientation,
1221 GtkGridChild *child,
1225 GtkGridPrivate *priv = request->grid->priv;
1226 GtkGridLineData *linedata;
1227 GtkGridLines *lines;
1229 GtkGridChildAttach *attach;
1232 linedata = &priv->linedata[orientation];
1233 lines = &request->lines[orientation];
1234 attach = &child->attach[orientation];
1236 *position = lines->lines[attach->pos - lines->min].position;
1238 *size = (attach->span - 1) * linedata->spacing;
1239 for (i = 0; i < attach->span; i++)
1241 line = &lines->lines[attach->pos - lines->min + i];
1242 *size += line->allocation;
1247 gtk_grid_request_allocate_children (GtkGridRequest *request)
1249 GtkGridPrivate *priv = request->grid->priv;
1251 GtkGridChild *child;
1252 GtkAllocation allocation;
1253 GtkAllocation child_allocation;
1254 gint x, y, width, height;
1256 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1258 for (list = priv->children; list; list = list->next)
1262 if (!gtk_widget_get_visible (child->widget))
1265 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1266 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1268 child_allocation.x = allocation.x + x;
1269 child_allocation.y = allocation.y + y;
1270 child_allocation.width = MAX (1, width);
1271 child_allocation.height = MAX (1, height);
1273 if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
1274 child_allocation.x = allocation.x + allocation.width
1275 - (child_allocation.x - allocation.x) - child_allocation.width;
1277 gtk_widget_size_allocate (child->widget, &child_allocation);
1281 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1284 gtk_grid_size_allocate (GtkWidget *widget,
1285 GtkAllocation *allocation)
1287 GtkGrid *grid = GTK_GRID (widget);
1288 GtkGridPrivate *priv = grid->priv;
1289 GtkGridRequest request;
1290 GtkGridLines *lines;
1291 GtkOrientation orientation;
1293 if (priv->children == NULL)
1295 gtk_widget_set_allocation (widget, allocation);
1299 request.grid = grid;
1301 gtk_grid_request_count_lines (&request);
1302 lines = &request.lines[0];
1303 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1304 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1305 lines = &request.lines[1];
1306 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1307 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1309 gtk_widget_set_allocation (widget, allocation);
1311 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1312 orientation = GTK_ORIENTATION_HORIZONTAL;
1314 orientation = GTK_ORIENTATION_VERTICAL;
1316 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1317 gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
1318 gtk_grid_request_run (&request, orientation, TRUE);
1319 gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
1321 gtk_grid_request_position (&request, 0);
1322 gtk_grid_request_position (&request, 1);
1324 gtk_grid_request_allocate_children (&request);
1328 gtk_grid_class_init (GtkGridClass *class)
1330 GObjectClass *object_class = G_OBJECT_CLASS (class);
1331 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1332 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1334 object_class->get_property = gtk_grid_get_property;
1335 object_class->set_property = gtk_grid_set_property;
1337 widget_class->size_allocate = gtk_grid_size_allocate;
1338 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1339 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1340 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1341 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1343 container_class->add = gtk_grid_add;
1344 container_class->remove = gtk_grid_remove;
1345 container_class->forall = gtk_grid_forall;
1346 container_class->child_type = gtk_grid_child_type;
1347 container_class->set_child_property = gtk_grid_set_child_property;
1348 container_class->get_child_property = gtk_grid_get_child_property;
1349 gtk_container_class_handle_border_width (container_class);
1351 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1353 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1354 g_param_spec_int ("row-spacing",
1356 P_("The amount of space between two consecutive rows"),
1358 GTK_PARAM_READWRITE));
1360 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1361 g_param_spec_int ("column-spacing",
1362 P_("Column spacing"),
1363 P_("The amount of space between two consecutive columns"),
1365 GTK_PARAM_READWRITE));
1367 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1368 g_param_spec_boolean ("row-homogeneous",
1369 P_("Row Homogeneous"),
1370 P_("If TRUE, the rows are all the same height"),
1372 GTK_PARAM_READWRITE));
1374 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1375 g_param_spec_boolean ("column-homogeneous",
1376 P_("Column Homogeneous"),
1377 P_("If TRUE, the columns are all the same width"),
1379 GTK_PARAM_READWRITE));
1381 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1382 g_param_spec_int ("left-attach",
1383 P_("Left attachment"),
1384 P_("The column number to attach the left side of the child to"),
1385 G_MININT, G_MAXINT, 0,
1386 GTK_PARAM_READWRITE));
1388 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1389 g_param_spec_int ("top-attach",
1390 P_("Top attachment"),
1391 P_("The row number to attach the top side of a child widget to"),
1392 G_MININT, G_MAXINT, 0,
1393 GTK_PARAM_READWRITE));
1395 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1396 g_param_spec_int ("width",
1398 P_("The number of columns that a child spans"),
1400 GTK_PARAM_READWRITE));
1402 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1403 g_param_spec_int ("height",
1405 P_("The number of rows that a child spans"),
1407 GTK_PARAM_READWRITE));
1409 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1415 * Creates a new grid widget.
1417 * Returns: the new #GtkGrid
1422 return g_object_new (GTK_TYPE_GRID, NULL);
1428 * @child: the widget to add
1429 * @left: the column number to attach the left side of @child to
1430 * @top: the row number to attach the top side of @child to
1431 * @width: the number of columns that @child will span
1432 * @height: the number of rows that @child will span
1434 * Adds a widget to the grid.
1436 * The position of @child is determined by @left and @top. The
1437 * number of 'cells' that @child will occupy is determined by
1438 * @width and @height.
1441 gtk_grid_attach (GtkGrid *grid,
1448 g_return_if_fail (GTK_IS_GRID (grid));
1449 g_return_if_fail (GTK_IS_WIDGET (child));
1450 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1451 g_return_if_fail (width > 0);
1452 g_return_if_fail (height > 0);
1454 grid_attach (grid, child, left, top, width, height);
1458 * gtk_grid_attach_next_to:
1460 * @child: the widget to add
1461 * @sibling: (allow-none): the child of @grid that @child will be placed
1462 * next to, or %NULL to place @child at the beginning or end
1463 * @side: the side of @sibling that @child is positioned next to
1464 * @width: the number of columns that @child will span
1465 * @height: the number of rows that @child will span
1467 * Adds a widget to the grid.
1469 * The widget is placed next to @sibling, on the side determined by
1470 * @side. When @sibling is %NULL, the widget is placed in row (for
1471 * left or right placement) or column 0 (for top or bottom placement),
1472 * at the end indicated by @side.
1474 * Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and
1475 * @side == %GTK_POS_LEFT yields a layout of [3][2][1].
1478 gtk_grid_attach_next_to (GtkGrid *grid,
1481 GtkPositionType side,
1485 GtkGridChild *grid_sibling;
1488 g_return_if_fail (GTK_IS_GRID (grid));
1489 g_return_if_fail (GTK_IS_WIDGET (child));
1490 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1491 g_return_if_fail (sibling == NULL || gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1492 g_return_if_fail (width > 0);
1493 g_return_if_fail (height > 0);
1497 grid_sibling = find_grid_child (grid, sibling);
1502 left = CHILD_LEFT (grid_sibling) - width;
1503 top = CHILD_TOP (grid_sibling);
1506 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1507 top = CHILD_TOP (grid_sibling);
1510 left = CHILD_LEFT (grid_sibling);
1511 top = CHILD_TOP (grid_sibling) - height;
1513 case GTK_POS_BOTTOM:
1514 left = CHILD_LEFT (grid_sibling);
1515 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1518 g_assert_not_reached ();
1526 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
1531 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1536 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
1539 case GTK_POS_BOTTOM:
1541 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1544 g_assert_not_reached ();
1548 grid_attach (grid, child, left, top, width, height);
1552 * gtk_grid_get_child_at:
1554 * @left: the left edge of the cell
1555 * @top: the top edge of the cell
1557 * Gets the child of @grid whose area covers the grid
1558 * cell whose upper left corner is at @left, @top.
1560 * Returns: the child at the given position, or %NULL
1565 gtk_grid_get_child_at (GtkGrid *grid,
1569 GtkGridPrivate *priv = grid->priv;
1570 GtkGridChild *child;
1573 for (list = priv->children; list; list = list->next)
1577 if (CHILD_LEFT (child) <= left &&
1578 CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
1579 CHILD_TOP (child) <= top &&
1580 CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
1581 return child->widget;
1588 * gtk_grid_insert_row:
1590 * @position: the position to insert the row at
1592 * Inserts a row at the specified position.
1594 * Children which are attached at or below this position
1595 * are moved one row down. Children which span across this
1596 * position are grown to span the new row.
1601 gtk_grid_insert_row (GtkGrid *grid,
1604 GtkGridPrivate *priv = grid->priv;
1605 GtkGridChild *child;
1609 g_return_if_fail (GTK_IS_GRID (grid));
1611 for (list = priv->children; list; list = list->next)
1615 top = CHILD_TOP (child);
1616 height = CHILD_HEIGHT (child);
1618 if (top >= position)
1620 CHILD_TOP (child) = top + 1;
1621 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
1623 else if (top + height > position)
1625 CHILD_HEIGHT (child) = height + 1;
1626 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
1632 * gtk_grid_insert_column:
1634 * @position: the position to insert the column at
1636 * Inserts a column at the specified position.
1638 * Children which are attached at or to the right of this position
1639 * are moved one column to the right. Children which span across this
1640 * position are grown to span the new column.
1645 gtk_grid_insert_column (GtkGrid *grid,
1648 GtkGridPrivate *priv = grid->priv;
1649 GtkGridChild *child;
1653 g_return_if_fail (GTK_IS_GRID (grid));
1655 for (list = priv->children; list; list = list->next)
1659 left = CHILD_LEFT (child);
1660 width = CHILD_WIDTH (child);
1662 if (left >= position)
1664 CHILD_LEFT (child) = left + 1;
1665 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
1667 else if (left + width > position)
1669 CHILD_WIDTH (child) = width + 1;
1670 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
1676 * gtk_grid_insert_next_to:
1678 * @sibling: the child of @grid that the new row or column will be
1680 * @side: the side of @sibling that @child is positioned next to
1682 * Inserts a row or column at the specified position.
1684 * The new row or column is placed next to @sibling, on the side
1685 * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
1686 * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
1687 * a column is inserted.
1692 gtk_grid_insert_next_to (GtkGrid *grid,
1694 GtkPositionType side)
1696 GtkGridChild *child;
1698 g_return_if_fail (GTK_IS_GRID (grid));
1699 g_return_if_fail (GTK_IS_WIDGET (sibling));
1700 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1702 child = find_grid_child (grid, sibling);
1707 gtk_grid_insert_column (grid, CHILD_LEFT (child));
1710 gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
1713 gtk_grid_insert_row (grid, CHILD_TOP (child));
1715 case GTK_POS_BOTTOM:
1716 gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
1719 g_assert_not_reached ();
1724 * gtk_grid_set_row_homogeneous:
1726 * @homogeneous: %TRUE to make rows homogeneous
1728 * Sets whether all rows of @grid will have the same height.
1731 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1732 gboolean homogeneous)
1734 GtkGridPrivate *priv;
1735 g_return_if_fail (GTK_IS_GRID (grid));
1739 /* Yes, homogeneous rows means all the columns have the same size */
1740 if (COLUMNS (priv)->homogeneous != homogeneous)
1742 COLUMNS (priv)->homogeneous = homogeneous;
1744 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1745 gtk_widget_queue_resize (GTK_WIDGET (grid));
1747 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1752 * gtk_grid_get_row_homogeneous:
1755 * Returns whether all rows of @grid have the same height.
1757 * Returns: whether all rows of @grid have the same height.
1760 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1762 GtkGridPrivate *priv;
1763 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1767 return COLUMNS (priv)->homogeneous;
1771 * gtk_grid_set_column_homogeneous:
1773 * @homogeneous: %TRUE to make columns homogeneous
1775 * Sets whether all columns of @grid will have the same width.
1778 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1779 gboolean homogeneous)
1781 GtkGridPrivate *priv;
1782 g_return_if_fail (GTK_IS_GRID (grid));
1786 /* Yes, homogeneous columns means all the rows have the same size */
1787 if (ROWS (priv)->homogeneous != homogeneous)
1789 ROWS (priv)->homogeneous = homogeneous;
1791 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1792 gtk_widget_queue_resize (GTK_WIDGET (grid));
1794 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1799 * gtk_grid_get_column_homogeneous:
1802 * Returns whether all columns of @grid have the same width.
1804 * Returns: whether all columns of @grid have the same width.
1807 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1809 GtkGridPrivate *priv;
1810 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1814 return ROWS (priv)->homogeneous;
1818 * gtk_grid_set_row_spacing:
1820 * @spacing: the amount of space to insert between rows
1822 * Sets the amount of space between rows of @grid.
1825 gtk_grid_set_row_spacing (GtkGrid *grid,
1828 GtkGridPrivate *priv;
1829 g_return_if_fail (GTK_IS_GRID (grid));
1830 g_return_if_fail (spacing <= G_MAXINT16);
1834 if (COLUMNS (priv)->spacing != spacing)
1836 COLUMNS (priv)->spacing = spacing;
1838 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1839 gtk_widget_queue_resize (GTK_WIDGET (grid));
1841 g_object_notify (G_OBJECT (grid), "row-spacing");
1846 * gtk_grid_get_row_spacing:
1849 * Returns the amount of space between the rows of @grid.
1851 * Returns: the row spacing of @grid
1854 gtk_grid_get_row_spacing (GtkGrid *grid)
1856 GtkGridPrivate *priv;
1857 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1861 return COLUMNS (priv)->spacing;
1865 * gtk_grid_set_column_spacing:
1867 * @spacing: the amount of space to insert between columns
1869 * Sets the amount of space between columns of @grid.
1872 gtk_grid_set_column_spacing (GtkGrid *grid,
1875 GtkGridPrivate *priv;
1876 g_return_if_fail (GTK_IS_GRID (grid));
1877 g_return_if_fail (spacing <= G_MAXINT16);
1881 if (ROWS (priv)->spacing != spacing)
1883 ROWS (priv)->spacing = spacing;
1885 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1886 gtk_widget_queue_resize (GTK_WIDGET (grid));
1888 g_object_notify (G_OBJECT (grid), "column-spacing");
1893 * gtk_grid_get_column_spacing:
1896 * Returns the amount of space between the columns of @grid.
1898 * Returns: the column spacing of @grid
1901 gtk_grid_get_column_spacing (GtkGrid *grid)
1903 GtkGridPrivate *priv;
1905 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1909 return ROWS (priv)->spacing;