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().
47 * GtkGrid can be used like a #GtkBox by just using gtk_container_add(),
48 * which will place children next to each other in the direction determined
49 * by the #GtkOrientable:orientation property.
52 typedef struct _GtkGridChild GtkGridChild;
53 typedef struct _GtkGridChildAttach GtkGridChildAttach;
54 typedef struct _GtkGridLine GtkGridLine;
55 typedef struct _GtkGridLines GtkGridLines;
56 typedef struct _GtkGridLineData GtkGridLineData;
57 typedef struct _GtkGridRequest GtkGridRequest;
59 struct _GtkGridChildAttach
68 GtkGridChildAttach attach[2];
71 #define CHILD_LEFT(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].pos)
72 #define CHILD_WIDTH(child) ((child)->attach[GTK_ORIENTATION_HORIZONTAL].span)
73 #define CHILD_TOP(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].pos)
74 #define CHILD_HEIGHT(child) ((child)->attach[GTK_ORIENTATION_VERTICAL].span)
76 /* A GtkGridLineData struct contains row/column specific parts
79 struct _GtkGridLineData
82 guint homogeneous : 1;
85 struct _GtkGridPrivate
89 GtkOrientation orientation;
91 GtkGridLineData linedata[2];
94 #define ROWS(priv) (&(priv)->linedata[GTK_ORIENTATION_HORIZONTAL])
95 #define COLUMNS(priv) (&(priv)->linedata[GTK_ORIENTATION_VERTICAL])
97 /* A GtkGridLine struct represents a single row or column
98 * during size requests
107 guint need_expand : 1;
118 struct _GtkGridRequest
121 GtkGridLines lines[2];
131 PROP_ROW_HOMOGENEOUS,
132 PROP_COLUMN_HOMOGENEOUS
138 CHILD_PROP_LEFT_ATTACH,
139 CHILD_PROP_TOP_ATTACH,
144 G_DEFINE_TYPE_WITH_CODE (GtkGrid, gtk_grid, GTK_TYPE_CONTAINER,
145 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
149 gtk_grid_get_property (GObject *object,
154 GtkGrid *grid = GTK_GRID (object);
155 GtkGridPrivate *priv = grid->priv;
159 case PROP_ORIENTATION:
160 g_value_set_enum (value, priv->orientation);
163 case PROP_ROW_SPACING:
164 g_value_set_int (value, COLUMNS (priv)->spacing);
167 case PROP_COLUMN_SPACING:
168 g_value_set_int (value, ROWS (priv)->spacing);
171 case PROP_ROW_HOMOGENEOUS:
172 g_value_set_boolean (value, COLUMNS (priv)->homogeneous);
175 case PROP_COLUMN_HOMOGENEOUS:
176 g_value_set_boolean (value, ROWS (priv)->homogeneous);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 gtk_grid_set_orientation (GtkGrid *grid,
187 GtkOrientation orientation)
189 GtkGridPrivate *priv = grid->priv;
191 if (priv->orientation != orientation)
193 priv->orientation = orientation;
194 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (grid));
196 g_object_notify (G_OBJECT (grid), "orientation");
201 gtk_grid_set_property (GObject *object,
206 GtkGrid *grid = GTK_GRID (object);
210 case PROP_ORIENTATION:
211 gtk_grid_set_orientation (grid, g_value_get_enum (value));
214 case PROP_ROW_SPACING:
215 gtk_grid_set_row_spacing (grid, g_value_get_int (value));
218 case PROP_COLUMN_SPACING:
219 gtk_grid_set_column_spacing (grid, g_value_get_int (value));
222 case PROP_ROW_HOMOGENEOUS:
223 gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
226 case PROP_COLUMN_HOMOGENEOUS:
227 gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 static GtkGridChild *
237 find_grid_child (GtkGrid *grid,
240 GtkGridPrivate *priv = grid->priv;
244 for (list = priv->children; list; list = list->next)
248 if (child->widget == widget)
256 gtk_grid_get_child_property (GtkContainer *container,
262 GtkGrid *grid = GTK_GRID (container);
263 GtkGridChild *grid_child;
265 grid_child = find_grid_child (grid, child);
267 if (grid_child == NULL)
269 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
275 case CHILD_PROP_LEFT_ATTACH:
276 g_value_set_int (value, CHILD_LEFT (grid_child));
279 case CHILD_PROP_TOP_ATTACH:
280 g_value_set_int (value, CHILD_TOP (grid_child));
283 case CHILD_PROP_WIDTH:
284 g_value_set_int (value, CHILD_WIDTH (grid_child));
287 case CHILD_PROP_HEIGHT:
288 g_value_set_int (value, CHILD_HEIGHT (grid_child));
292 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
298 gtk_grid_set_child_property (GtkContainer *container,
304 GtkGrid *grid = GTK_GRID (container);
305 GtkGridChild *grid_child;
307 grid_child = find_grid_child (grid, child);
309 if (grid_child == NULL)
311 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
317 case CHILD_PROP_LEFT_ATTACH:
318 CHILD_LEFT (grid_child) = g_value_get_int (value);
321 case CHILD_PROP_TOP_ATTACH:
322 CHILD_TOP (grid_child) = g_value_get_int (value);
325 case CHILD_PROP_WIDTH:
326 CHILD_WIDTH (grid_child) = g_value_get_int (value);
329 case CHILD_PROP_HEIGHT:
330 CHILD_HEIGHT (grid_child) = g_value_get_int (value);
334 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
338 if (gtk_widget_get_visible (child) &&
339 gtk_widget_get_visible (GTK_WIDGET (grid)))
340 gtk_widget_queue_resize (child);
344 gtk_grid_init (GtkGrid *grid)
346 GtkGridPrivate *priv;
348 grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, GTK_TYPE_GRID, GtkGridPrivate);
351 gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
352 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);
354 priv->children = NULL;
355 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
357 priv->linedata[0].spacing = 0;
358 priv->linedata[1].spacing = 0;
360 priv->linedata[0].homogeneous = FALSE;
361 priv->linedata[1].homogeneous = FALSE;
365 grid_attach (GtkGrid *grid,
372 GtkGridPrivate *priv = grid->priv;
375 child = g_slice_new (GtkGridChild);
376 child->widget = widget;
377 CHILD_LEFT (child) = left;
378 CHILD_TOP (child) = top;
379 CHILD_WIDTH (child) = width;
380 CHILD_HEIGHT (child) = height;
382 priv->children = g_list_prepend (priv->children, child);
384 gtk_widget_set_parent (widget, GTK_WIDGET (grid));
387 /* Find the position 'touching' existing
388 * children. @orientation and @max determine
389 * from which direction to approach (horizontal
390 * + max = right, vertical + !max = top, etc).
391 * @op_pos, @op_span determine the rows/columns
392 * in which the touching has to happen.
395 find_attach_position (GtkGrid *grid,
396 GtkOrientation orientation,
401 GtkGridPrivate *priv = grid->priv;
402 GtkGridChild *grid_child;
403 GtkGridChildAttach *attach;
404 GtkGridChildAttach *opposite;
416 for (list = priv->children; list; list = list->next)
418 grid_child = list->data;
420 attach = &grid_child->attach[orientation];
421 opposite = &grid_child->attach[1 - orientation];
423 /* check if the ranges overlap */
424 if (opposite->pos <= op_pos + op_span && op_pos <= opposite->pos + opposite->span)
429 pos = MAX (pos, attach->pos + attach->span);
431 pos = MIN (pos, attach->pos);
442 gtk_grid_add (GtkContainer *container,
445 GtkGrid *grid = GTK_GRID (container);
446 GtkGridPrivate *priv = grid->priv;
447 gint pos[2] = { 0, 0 };
449 pos[priv->orientation] = find_attach_position (grid, priv->orientation, 0, 1, TRUE);
450 grid_attach (grid, child, pos[0], pos[1], 1, 1);
454 gtk_grid_remove (GtkContainer *container,
457 GtkGrid *grid = GTK_GRID (container);
458 GtkGridPrivate *priv = grid->priv;
459 GtkGridChild *grid_child;
462 for (list = priv->children; list; list = list->next)
464 grid_child = list->data;
466 if (grid_child->widget == child)
468 gboolean was_visible = gtk_widget_get_visible (child);
470 gtk_widget_unparent (child);
472 priv->children = g_list_remove (priv->children, grid_child);
474 g_slice_free (GtkGridChild, grid_child);
476 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (grid)))
477 gtk_widget_queue_resize (GTK_WIDGET (grid));
485 gtk_grid_forall (GtkContainer *container,
486 gboolean include_internals,
487 GtkCallback callback,
488 gpointer callback_data)
490 GtkGrid *grid = GTK_GRID (container);
491 GtkGridPrivate *priv = grid->priv;
495 list = priv->children;
501 (* callback) (child->widget, callback_data);
506 gtk_grid_child_type (GtkContainer *container)
508 return GTK_TYPE_WIDGET;
511 /* Calculates the min and max numbers for both orientations.
514 gtk_grid_request_count_lines (GtkGridRequest *request)
516 GtkGridPrivate *priv = request->grid->priv;
518 GtkGridChildAttach *attach;
523 min[0] = min[1] = G_MAXINT;
524 max[0] = max[1] = G_MININT;
526 for (list = priv->children; list; list = list->next)
529 attach = child->attach;
531 min[0] = MIN (min[0], attach[0].pos);
532 max[0] = MAX (max[0], attach[0].pos + attach[0].span);
533 min[1] = MIN (min[1], attach[1].pos);
534 max[1] = MAX (max[1], attach[1].pos + attach[1].span);
537 request->lines[0].min = min[0];
538 request->lines[0].max = max[0];
539 request->lines[1].min = min[1];
540 request->lines[1].max = max[1];
543 /* Sets line sizes to 0 and marks lines as expand
544 * if they have a non-spanning expanding child.
547 gtk_grid_request_init (GtkGridRequest *request,
548 GtkOrientation orientation)
550 GtkGridPrivate *priv = request->grid->priv;
552 GtkGridChildAttach *attach;
557 lines = &request->lines[orientation];
559 for (i = 0; i < lines->max - lines->min; i++)
561 lines->lines[i].minimum = 0;
562 lines->lines[i].natural = 0;
563 lines->lines[i].expand = FALSE;
566 for (list = priv->children; list; list = list->next)
570 attach = &child->attach[orientation];
571 if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
572 lines->lines[attach->pos - lines->min].expand = TRUE;
576 /* Sums allocations for lines spanned by child and their spacing.
579 compute_allocation_for_child (GtkGridRequest *request,
581 GtkOrientation orientation)
583 GtkGridPrivate *priv = request->grid->priv;
584 GtkGridLineData *linedata;
587 GtkGridChildAttach *attach;
591 linedata = &priv->linedata[orientation];
592 lines = &request->lines[orientation];
593 attach = &child->attach[orientation];
595 size = (attach->span - 1) * linedata->spacing;
596 for (i = 0; i < attach->span; i++)
598 line = &lines->lines[attach->pos - lines->min + i];
599 size += line->allocation;
606 compute_request_for_child (GtkGridRequest *request,
608 GtkOrientation orientation,
617 size = compute_allocation_for_child (request, child, 1 - orientation);
618 if (orientation == GTK_ORIENTATION_HORIZONTAL)
619 gtk_widget_get_preferred_width_for_height (child->widget,
623 gtk_widget_get_preferred_height_for_width (child->widget,
629 if (orientation == GTK_ORIENTATION_HORIZONTAL)
630 gtk_widget_get_preferred_width (child->widget, minimum, natural);
632 gtk_widget_get_preferred_height (child->widget, minimum, natural);
636 /* Sets requisition to max. of non-spanning children.
637 * If contextual is TRUE, requires allocations of
638 * lines in the opposite orientation to be set.
641 gtk_grid_request_non_spanning (GtkGridRequest *request,
642 GtkOrientation orientation,
645 GtkGridPrivate *priv = request->grid->priv;
647 GtkGridChildAttach *attach;
654 lines = &request->lines[orientation];
656 for (list = priv->children; list; list = list->next)
660 if (!gtk_widget_get_visible (child->widget))
663 attach = &child->attach[orientation];
664 if (attach->span != 1)
667 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
669 line = &lines->lines[attach->pos - lines->min];
670 line->minimum = MAX (line->minimum, minimum);
671 line->natural = MAX (line->natural, natural);
675 /* Enforce homogeneous sizes.
678 gtk_grid_request_homogeneous (GtkGridRequest *request,
679 GtkOrientation orientation)
681 GtkGridPrivate *priv = request->grid->priv;
682 GtkGridLineData *linedata;
684 gint minimum, natural;
687 linedata = &priv->linedata[orientation];
688 lines = &request->lines[orientation];
690 if (!linedata->homogeneous)
696 for (i = 0; i < lines->max - lines->min; i++)
698 minimum = MAX (minimum, lines->lines[i].minimum);
699 natural = MAX (natural, lines->lines[i].natural);
702 for (i = 0; i < lines->max - lines->min; i++)
704 lines->lines[i].minimum = minimum;
705 lines->lines[i].natural = natural;
709 /* Deals with spanning children.
710 * Requires expand fields of lines to be set for
711 * non-spanning children.
714 gtk_grid_request_spanning (GtkGridRequest *request,
715 GtkOrientation orientation,
718 GtkGridPrivate *priv = request->grid->priv;
721 GtkGridChildAttach *attach;
722 GtkGridLineData *linedata;
730 gboolean force_expand;
736 linedata = &priv->linedata[orientation];
737 lines = &request->lines[orientation];
739 for (list = priv->children; list; list = list->next)
743 if (!gtk_widget_get_visible (child->widget))
746 attach = &child->attach[orientation];
747 if (attach->span == 1)
750 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
752 span_minimum = (attach->span - 1) * linedata->spacing;
753 span_natural = (attach->span - 1) * linedata->spacing;
755 force_expand = FALSE;
756 for (i = 0; i < attach->span; i++)
758 line = &lines->lines[attach->pos - lines->min + i];
759 span_minimum += line->minimum;
760 span_natural += line->natural;
764 if (span_expand == 0)
766 span_expand = attach->span;
770 /* If we need to request more space for this child to fill
771 * its requisition, then divide up the needed space amongst the
772 * lines it spans, favoring expandable lines if any.
774 * When doing homogeneous allocation though, try to keep the
775 * line allocations even, since we're going to force them to
776 * be the same anyway, and we don't want to introduce unnecessary
779 if (span_minimum < minimum)
781 if (linedata->homogeneous)
785 total = minimum - (attach->span - 1) * linedata->spacing;
786 m = total / attach->span + (total % attach->span ? 1 : 0);
787 for (i = 0; i < attach->span; i++)
789 line = &lines->lines[attach->pos - lines->min + i];
790 line->minimum = MAX(line->minimum, m);
795 extra = minimum - span_minimum;
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->minimum += line_extra;
811 if (span_natural < natural)
813 if (linedata->homogeneous)
817 total = natural - (attach->span - 1) * linedata->spacing;
818 n = total / attach->span + (total % attach->span ? 1 : 0);
819 for (i = 0; i < attach->span; i++)
821 line = &lines->lines[attach->pos - lines->min + i];
822 line->natural = MAX(line->natural, n);
827 extra = natural - span_natural;
828 expand = span_expand;
829 for (i = 0; i < attach->span; i++)
831 line = &lines->lines[attach->pos - lines->min + i];
832 if (force_expand || line->expand)
834 line_extra = extra / expand;
835 line->natural += line_extra;
845 /* Marks empty and expanding lines and counts them.
848 gtk_grid_request_compute_expand (GtkGridRequest *request,
849 GtkOrientation orientation,
850 gint *nonempty_lines,
853 GtkGridPrivate *priv = request->grid->priv;
855 GtkGridChildAttach *attach;
864 lines = &request->lines[orientation];
866 for (i = 0; i < lines->max - lines->min; i++)
868 lines->lines[i].need_expand = FALSE;
869 lines->lines[i].expand = FALSE;
870 lines->lines[i].empty = TRUE;
873 for (list = priv->children; list; list = list->next)
877 if (!gtk_widget_get_visible (child->widget))
880 attach = &child->attach[orientation];
881 if (attach->span != 1)
884 line = &lines->lines[attach->pos - lines->min];
886 if (gtk_widget_compute_expand (child->widget, orientation))
890 for (list = priv->children; list; list = list->next)
894 if (!gtk_widget_get_visible (child->widget))
897 attach = &child->attach[orientation];
898 if (attach->span == 1)
902 for (i = 0; i < attach->span; i++)
904 line = &lines->lines[attach->pos - lines->min + i];
910 if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
912 for (i = 0; i < attach->span; i++)
914 line = &lines->lines[attach->pos - lines->min + i];
915 line->need_expand = TRUE;
922 for (i = 0; i < lines->max - lines->min; i++)
924 line = &lines->lines[i];
926 if (line->need_expand)
937 *nonempty_lines = lines->max - lines->min - empty;
940 *expand_lines = expand;
943 /* Sums the minimum and natural fields of lines and their spacing.
946 gtk_grid_request_sum (GtkGridRequest *request,
947 GtkOrientation orientation,
951 GtkGridPrivate *priv = request->grid->priv;
952 GtkGridLineData *linedata;
958 gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
960 linedata = &priv->linedata[orientation];
961 lines = &request->lines[orientation];
967 min = (nonempty - 1) * linedata->spacing;
968 nat = (nonempty - 1) * linedata->spacing;
971 for (i = 0; i < lines->max - lines->min; i++)
973 min += lines->lines[i].minimum;
974 nat += lines->lines[i].natural;
984 /* Computes minimum and natural fields of lines.
985 * When contextual is TRUE, requires allocation of
986 * lines in the opposite orientation to be set.
989 gtk_grid_request_run (GtkGridRequest *request,
990 GtkOrientation orientation,
993 gtk_grid_request_init (request, orientation);
994 gtk_grid_request_non_spanning (request, orientation, contextual);
995 gtk_grid_request_homogeneous (request, orientation);
996 gtk_grid_request_spanning (request, orientation, contextual);
997 gtk_grid_request_homogeneous (request, orientation);
1000 /* Requires that the minimum and natural fields of lines
1001 * have been set, computes the allocation field of lines
1002 * by distributing total_size among lines.
1005 gtk_grid_request_allocate (GtkGridRequest *request,
1006 GtkOrientation orientation,
1009 GtkGridPrivate *priv = request->grid->priv;
1010 GtkGridLineData *linedata;
1011 GtkGridLines *lines;
1016 GtkRequestedSize *sizes;
1021 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
1023 linedata = &priv->linedata[orientation];
1024 lines = &request->lines[orientation];
1026 size = total_size - (nonempty - 1) * linedata->spacing;
1028 if (linedata->homogeneous)
1030 extra = size / nonempty;
1031 rest = size % nonempty;
1033 for (i = 0; i < lines->max - lines->min; i++)
1035 line = &lines->lines[i];
1039 line->allocation = extra;
1042 line->allocation += 1;
1049 sizes = g_newa (GtkRequestedSize, nonempty);
1052 for (i = 0; i < lines->max - lines->min; i++)
1054 line = &lines->lines[i];
1058 size -= line->minimum;
1060 sizes[j].minimum_size = line->minimum;
1061 sizes[j].natural_size = line->natural;
1062 sizes[j].data = line;
1066 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1070 extra = size / expand;
1071 rest = size % expand;
1080 for (i = 0; i < lines->max - lines->min; i++)
1082 line = &lines->lines[i];
1086 g_assert (line == sizes[j].data);
1088 line->allocation = sizes[j].minimum_size;
1091 line->allocation += extra;
1094 line->allocation += 1;
1104 /* Computes the position fields from allocation and spacing.
1107 gtk_grid_request_position (GtkGridRequest *request,
1108 GtkOrientation orientation)
1110 GtkGridPrivate *priv = request->grid->priv;
1111 GtkGridLineData *linedata;
1112 GtkGridLines *lines;
1117 linedata = &priv->linedata[orientation];
1118 lines = &request->lines[orientation];
1121 for (i = 0; i < lines->max - lines->min; i++)
1123 line = &lines->lines[i];
1126 line->position = position;
1127 position += line->allocation + linedata->spacing;
1133 gtk_grid_get_size (GtkGrid *grid,
1134 GtkOrientation orientation,
1138 GtkGridRequest request;
1139 GtkGridLines *lines;
1147 if (grid->priv->children == NULL)
1150 request.grid = grid;
1151 gtk_grid_request_count_lines (&request);
1152 lines = &request.lines[orientation];
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, orientation, FALSE);
1157 gtk_grid_request_sum (&request, orientation, minimum, natural);
1161 gtk_grid_get_size_for_size (GtkGrid *grid,
1162 GtkOrientation orientation,
1167 GtkGridRequest request;
1168 GtkGridLines *lines;
1177 if (grid->priv->children == NULL)
1180 request.grid = grid;
1181 gtk_grid_request_count_lines (&request);
1182 lines = &request.lines[0];
1183 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1184 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1185 lines = &request.lines[1];
1186 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1187 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1189 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1190 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1191 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1193 gtk_grid_request_run (&request, orientation, TRUE);
1194 gtk_grid_request_sum (&request, orientation, minimum, natural);
1198 gtk_grid_get_preferred_width (GtkWidget *widget,
1202 GtkGrid *grid = GTK_GRID (widget);
1204 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1205 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1207 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1211 gtk_grid_get_preferred_height (GtkWidget *widget,
1215 GtkGrid *grid = GTK_GRID (widget);
1217 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1218 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1220 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1224 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1229 GtkGrid *grid = GTK_GRID (widget);
1231 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1232 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1234 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1238 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1243 GtkGrid *grid = GTK_GRID (widget);
1245 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1246 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1248 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1252 allocate_child (GtkGridRequest *request,
1253 GtkOrientation orientation,
1254 GtkGridChild *child,
1258 GtkGridPrivate *priv = request->grid->priv;
1259 GtkGridLineData *linedata;
1260 GtkGridLines *lines;
1262 GtkGridChildAttach *attach;
1265 linedata = &priv->linedata[orientation];
1266 lines = &request->lines[orientation];
1267 attach = &child->attach[orientation];
1269 *position = lines->lines[attach->pos - lines->min].position;
1271 *size = (attach->span - 1) * linedata->spacing;
1272 for (i = 0; i < attach->span; i++)
1274 line = &lines->lines[attach->pos - lines->min + i];
1275 *size += line->allocation;
1280 gtk_grid_request_allocate_children (GtkGridRequest *request)
1282 GtkGridPrivate *priv = request->grid->priv;
1284 GtkGridChild *child;
1285 GtkAllocation allocation;
1286 GtkAllocation child_allocation;
1287 gint x, y, width, height;
1289 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1291 for (list = priv->children; list; list = list->next)
1295 if (!gtk_widget_get_visible (child->widget))
1298 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1299 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1301 child_allocation.x = allocation.x + x;
1302 child_allocation.y = allocation.y + y;
1303 child_allocation.width = MAX (1, width);
1304 child_allocation.height = MAX (1, height);
1306 if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
1307 child_allocation.x = allocation.x + allocation.width
1308 - (child_allocation.x - allocation.x) - child_allocation.width;
1310 gtk_widget_size_allocate (child->widget, &child_allocation);
1314 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1317 gtk_grid_size_allocate (GtkWidget *widget,
1318 GtkAllocation *allocation)
1320 GtkGrid *grid = GTK_GRID (widget);
1321 GtkGridPrivate *priv = grid->priv;
1322 GtkGridRequest request;
1323 GtkGridLines *lines;
1324 GtkOrientation orientation;
1326 if (priv->children == NULL)
1328 gtk_widget_set_allocation (widget, allocation);
1332 request.grid = grid;
1334 gtk_grid_request_count_lines (&request);
1335 lines = &request.lines[0];
1336 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1337 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1338 lines = &request.lines[1];
1339 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1340 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1342 gtk_widget_set_allocation (widget, allocation);
1344 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1345 orientation = GTK_ORIENTATION_HORIZONTAL;
1347 orientation = GTK_ORIENTATION_VERTICAL;
1349 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1350 gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
1351 gtk_grid_request_run (&request, orientation, TRUE);
1352 gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
1354 gtk_grid_request_position (&request, 0);
1355 gtk_grid_request_position (&request, 1);
1357 gtk_grid_request_allocate_children (&request);
1361 gtk_grid_class_init (GtkGridClass *class)
1363 GObjectClass *object_class = G_OBJECT_CLASS (class);
1364 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1365 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1367 object_class->get_property = gtk_grid_get_property;
1368 object_class->set_property = gtk_grid_set_property;
1370 widget_class->size_allocate = gtk_grid_size_allocate;
1371 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1372 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1373 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1374 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1376 container_class->add = gtk_grid_add;
1377 container_class->remove = gtk_grid_remove;
1378 container_class->forall = gtk_grid_forall;
1379 container_class->child_type = gtk_grid_child_type;
1380 container_class->set_child_property = gtk_grid_set_child_property;
1381 container_class->get_child_property = gtk_grid_get_child_property;
1382 gtk_container_class_handle_border_width (container_class);
1384 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1386 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1387 g_param_spec_int ("row-spacing",
1389 P_("The amount of space between two consecutive rows"),
1391 GTK_PARAM_READWRITE));
1393 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1394 g_param_spec_int ("column-spacing",
1395 P_("Column spacing"),
1396 P_("The amount of space between two consecutive columns"),
1398 GTK_PARAM_READWRITE));
1400 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1401 g_param_spec_boolean ("row-homogeneous",
1402 P_("Row Homogeneous"),
1403 P_("If TRUE, the rows are all the same height"),
1405 GTK_PARAM_READWRITE));
1407 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1408 g_param_spec_boolean ("column-homogeneous",
1409 P_("Column Homogeneous"),
1410 P_("If TRUE, the columns are all the same width"),
1412 GTK_PARAM_READWRITE));
1414 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1415 g_param_spec_int ("left-attach",
1416 P_("Left attachment"),
1417 P_("The column number to attach the left side of the child to"),
1418 G_MININT, G_MAXINT, 0,
1419 GTK_PARAM_READWRITE));
1421 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1422 g_param_spec_int ("top-attach",
1423 P_("Top attachment"),
1424 P_("The row number to attach the top side of a child widget to"),
1425 G_MININT, G_MAXINT, 0,
1426 GTK_PARAM_READWRITE));
1428 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1429 g_param_spec_int ("width",
1431 P_("The number of columns that a child spans"),
1433 GTK_PARAM_READWRITE));
1435 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1436 g_param_spec_int ("height",
1438 P_("The number of rows that a child spans"),
1440 GTK_PARAM_READWRITE));
1442 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1448 * Creates a new grid widget.
1450 * Returns: the new #GtkGrid
1455 return g_object_new (GTK_TYPE_GRID, NULL);
1461 * @child: the widget to add
1462 * @left: the column number to attach the left side of @child to
1463 * @top: the row number to attach the top side of @child 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 position of @child is determined by @left and @top. The
1470 * number of 'cells' that @child will occupy is determined by
1471 * @width and @height.
1474 gtk_grid_attach (GtkGrid *grid,
1481 g_return_if_fail (GTK_IS_GRID (grid));
1482 g_return_if_fail (GTK_IS_WIDGET (child));
1483 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1484 g_return_if_fail (width > 0);
1485 g_return_if_fail (height > 0);
1487 grid_attach (grid, child, left, top, width, height);
1491 * gtk_grid_attach_next_to:
1493 * @child: the widget to add
1494 * @sibling: (allow-none): the child of @grid that @child will be placed
1495 * next to, or %NULL to place @child at the beginning or end
1496 * @side: the side of @sibling that @child is positioned next to
1497 * @width: the number of columns that @child will span
1498 * @height: the number of rows that @child will span
1500 * Adds a widget to the grid.
1502 * The widget is placed next to @sibling, on the side determined by
1503 * @side. When @sibling is %NULL, the widget is placed in row (for
1504 * left or right placement) or column 0 (for top or bottom placement),
1505 * at the end indicated by @side.
1507 * Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and
1508 * @side == %GTK_POS_LEFT yields a layout of [3][2][1].
1511 gtk_grid_attach_next_to (GtkGrid *grid,
1514 GtkPositionType side,
1518 GtkGridChild *grid_sibling;
1521 g_return_if_fail (GTK_IS_GRID (grid));
1522 g_return_if_fail (GTK_IS_WIDGET (child));
1523 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1524 g_return_if_fail (sibling == NULL || gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1525 g_return_if_fail (width > 0);
1526 g_return_if_fail (height > 0);
1530 grid_sibling = find_grid_child (grid, sibling);
1535 left = CHILD_LEFT (grid_sibling) - width;
1536 top = CHILD_TOP (grid_sibling);
1539 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1540 top = CHILD_TOP (grid_sibling);
1543 left = CHILD_LEFT (grid_sibling);
1544 top = CHILD_TOP (grid_sibling) - height;
1546 case GTK_POS_BOTTOM:
1547 left = CHILD_LEFT (grid_sibling);
1548 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1551 g_assert_not_reached ();
1559 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
1564 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1569 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
1572 case GTK_POS_BOTTOM:
1574 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1577 g_assert_not_reached ();
1581 grid_attach (grid, child, left, top, width, height);
1585 * gtk_grid_get_child_at:
1587 * @left: the left edge of the cell
1588 * @top: the top edge of the cell
1590 * Gets the child of @grid whose area covers the grid
1591 * cell whose upper left corner is at @left, @top.
1593 * Returns: the child at the given position, or %NULL
1598 gtk_grid_get_child_at (GtkGrid *grid,
1602 GtkGridPrivate *priv = grid->priv;
1603 GtkGridChild *child;
1606 for (list = priv->children; list; list = list->next)
1610 if (CHILD_LEFT (child) <= left &&
1611 CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
1612 CHILD_TOP (child) <= top &&
1613 CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
1614 return child->widget;
1621 * gtk_grid_insert_row:
1623 * @position: the position to insert the row at
1625 * Inserts a row at the specified position.
1627 * Children which are attached at or below this position
1628 * are moved one row down. Children which span across this
1629 * position are grown to span the new row.
1634 gtk_grid_insert_row (GtkGrid *grid,
1637 GtkGridPrivate *priv = grid->priv;
1638 GtkGridChild *child;
1642 g_return_if_fail (GTK_IS_GRID (grid));
1644 for (list = priv->children; list; list = list->next)
1648 top = CHILD_TOP (child);
1649 height = CHILD_HEIGHT (child);
1651 if (top >= position)
1653 CHILD_TOP (child) = top + 1;
1654 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
1656 else if (top + height > position)
1658 CHILD_HEIGHT (child) = height + 1;
1659 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
1665 * gtk_grid_insert_column:
1667 * @position: the position to insert the column at
1669 * Inserts a column at the specified position.
1671 * Children which are attached at or to the right of this position
1672 * are moved one column to the right. Children which span across this
1673 * position are grown to span the new column.
1678 gtk_grid_insert_column (GtkGrid *grid,
1681 GtkGridPrivate *priv = grid->priv;
1682 GtkGridChild *child;
1686 g_return_if_fail (GTK_IS_GRID (grid));
1688 for (list = priv->children; list; list = list->next)
1692 left = CHILD_LEFT (child);
1693 width = CHILD_WIDTH (child);
1695 if (left >= position)
1697 CHILD_LEFT (child) = left + 1;
1698 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
1700 else if (left + width > position)
1702 CHILD_WIDTH (child) = width + 1;
1703 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
1709 * gtk_grid_insert_next_to:
1711 * @sibling: the child of @grid that the new row or column will be
1713 * @side: the side of @sibling that @child is positioned next to
1715 * Inserts a row or column at the specified position.
1717 * The new row or column is placed next to @sibling, on the side
1718 * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
1719 * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
1720 * a column is inserted.
1725 gtk_grid_insert_next_to (GtkGrid *grid,
1727 GtkPositionType side)
1729 GtkGridChild *child;
1731 g_return_if_fail (GTK_IS_GRID (grid));
1732 g_return_if_fail (GTK_IS_WIDGET (sibling));
1733 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1735 child = find_grid_child (grid, sibling);
1740 gtk_grid_insert_column (grid, CHILD_LEFT (child));
1743 gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
1746 gtk_grid_insert_row (grid, CHILD_TOP (child));
1748 case GTK_POS_BOTTOM:
1749 gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
1752 g_assert_not_reached ();
1757 * gtk_grid_set_row_homogeneous:
1759 * @homogeneous: %TRUE to make rows homogeneous
1761 * Sets whether all rows of @grid will have the same height.
1764 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1765 gboolean homogeneous)
1767 GtkGridPrivate *priv;
1768 g_return_if_fail (GTK_IS_GRID (grid));
1772 /* Yes, homogeneous rows means all the columns have the same size */
1773 if (COLUMNS (priv)->homogeneous != homogeneous)
1775 COLUMNS (priv)->homogeneous = homogeneous;
1777 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1778 gtk_widget_queue_resize (GTK_WIDGET (grid));
1780 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1785 * gtk_grid_get_row_homogeneous:
1788 * Returns whether all rows of @grid have the same height.
1790 * Returns: whether all rows of @grid have the same height.
1793 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1795 GtkGridPrivate *priv;
1796 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1800 return COLUMNS (priv)->homogeneous;
1804 * gtk_grid_set_column_homogeneous:
1806 * @homogeneous: %TRUE to make columns homogeneous
1808 * Sets whether all columns of @grid will have the same width.
1811 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1812 gboolean homogeneous)
1814 GtkGridPrivate *priv;
1815 g_return_if_fail (GTK_IS_GRID (grid));
1819 /* Yes, homogeneous columns means all the rows have the same size */
1820 if (ROWS (priv)->homogeneous != homogeneous)
1822 ROWS (priv)->homogeneous = homogeneous;
1824 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1825 gtk_widget_queue_resize (GTK_WIDGET (grid));
1827 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1832 * gtk_grid_get_column_homogeneous:
1835 * Returns whether all columns of @grid have the same width.
1837 * Returns: whether all columns of @grid have the same width.
1840 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1842 GtkGridPrivate *priv;
1843 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1847 return ROWS (priv)->homogeneous;
1851 * gtk_grid_set_row_spacing:
1853 * @spacing: the amount of space to insert between rows
1855 * Sets the amount of space between rows of @grid.
1858 gtk_grid_set_row_spacing (GtkGrid *grid,
1861 GtkGridPrivate *priv;
1862 g_return_if_fail (GTK_IS_GRID (grid));
1863 g_return_if_fail (spacing <= G_MAXINT16);
1867 if (COLUMNS (priv)->spacing != spacing)
1869 COLUMNS (priv)->spacing = spacing;
1871 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1872 gtk_widget_queue_resize (GTK_WIDGET (grid));
1874 g_object_notify (G_OBJECT (grid), "row-spacing");
1879 * gtk_grid_get_row_spacing:
1882 * Returns the amount of space between the rows of @grid.
1884 * Returns: the row spacing of @grid
1887 gtk_grid_get_row_spacing (GtkGrid *grid)
1889 GtkGridPrivate *priv;
1890 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1894 return COLUMNS (priv)->spacing;
1898 * gtk_grid_set_column_spacing:
1900 * @spacing: the amount of space to insert between columns
1902 * Sets the amount of space between columns of @grid.
1905 gtk_grid_set_column_spacing (GtkGrid *grid,
1908 GtkGridPrivate *priv;
1909 g_return_if_fail (GTK_IS_GRID (grid));
1910 g_return_if_fail (spacing <= G_MAXINT16);
1914 if (ROWS (priv)->spacing != spacing)
1916 ROWS (priv)->spacing = spacing;
1918 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1919 gtk_widget_queue_resize (GTK_WIDGET (grid));
1921 g_object_notify (G_OBJECT (grid), "column-spacing");
1926 * gtk_grid_get_column_spacing:
1929 * Returns the amount of space between the columns of @grid.
1931 * Returns: the column spacing of @grid
1934 gtk_grid_get_column_spacing (GtkGrid *grid)
1936 GtkGridPrivate *priv;
1938 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1942 return ROWS (priv)->spacing;