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 if (span_minimum < minimum)
776 extra = minimum - span_minimum;
777 expand = span_expand;
778 for (i = 0; i < attach->span; i++)
780 line = &lines->lines[attach->pos - lines->min + i];
781 if (force_expand || line->expand)
783 line_extra = extra / expand;
784 line->minimum += line_extra;
791 if (span_natural < natural)
793 extra = natural - span_natural;
794 expand = span_expand;
795 for (i = 0; i < attach->span; i++)
797 line = &lines->lines[attach->pos - lines->min + i];
798 if (force_expand || line->expand)
800 line_extra = extra / expand;
801 line->natural += line_extra;
810 /* Marks empty and expanding lines and counts them.
813 gtk_grid_request_compute_expand (GtkGridRequest *request,
814 GtkOrientation orientation,
815 gint *nonempty_lines,
818 GtkGridPrivate *priv = request->grid->priv;
820 GtkGridChildAttach *attach;
829 lines = &request->lines[orientation];
831 for (i = 0; i < lines->max - lines->min; i++)
833 lines->lines[i].need_expand = FALSE;
834 lines->lines[i].expand = FALSE;
835 lines->lines[i].empty = TRUE;
838 for (list = priv->children; list; list = list->next)
842 if (!gtk_widget_get_visible (child->widget))
845 attach = &child->attach[orientation];
846 if (attach->span != 1)
849 line = &lines->lines[attach->pos - lines->min];
851 if (gtk_widget_compute_expand (child->widget, orientation))
855 for (list = priv->children; list; list = list->next)
859 if (!gtk_widget_get_visible (child->widget))
862 attach = &child->attach[orientation];
863 if (attach->span == 1)
867 for (i = 0; i < attach->span; i++)
869 line = &lines->lines[attach->pos - lines->min + i];
875 if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
877 for (i = 0; i < attach->span; i++)
879 line = &lines->lines[attach->pos - lines->min + i];
880 line->need_expand = TRUE;
887 for (i = 0; i < lines->max - lines->min; i++)
889 line = &lines->lines[i];
891 if (line->need_expand)
902 *nonempty_lines = lines->max - lines->min - empty;
905 *expand_lines = expand;
908 /* Sums the minimum and natural fields of lines and their spacing.
911 gtk_grid_request_sum (GtkGridRequest *request,
912 GtkOrientation orientation,
916 GtkGridPrivate *priv = request->grid->priv;
917 GtkGridLineData *linedata;
923 gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
925 linedata = &priv->linedata[orientation];
926 lines = &request->lines[orientation];
932 min = (nonempty - 1) * linedata->spacing;
933 nat = (nonempty - 1) * linedata->spacing;
936 for (i = 0; i < lines->max - lines->min; i++)
938 min += lines->lines[i].minimum;
939 nat += lines->lines[i].natural;
949 /* Computes minimum and natural fields of lines.
950 * When contextual is TRUE, requires allocation of
951 * lines in the opposite orientation to be set.
954 gtk_grid_request_run (GtkGridRequest *request,
955 GtkOrientation orientation,
958 gtk_grid_request_init (request, orientation);
959 gtk_grid_request_non_spanning (request, orientation, contextual);
960 gtk_grid_request_homogeneous (request, orientation);
961 gtk_grid_request_spanning (request, orientation, contextual);
962 gtk_grid_request_homogeneous (request, orientation);
965 /* Requires that the minimum and natural fields of lines
966 * have been set, computes the allocation field of lines
967 * by distributing total_size among lines.
970 gtk_grid_request_allocate (GtkGridRequest *request,
971 GtkOrientation orientation,
974 GtkGridPrivate *priv = request->grid->priv;
975 GtkGridLineData *linedata;
981 GtkRequestedSize *sizes;
986 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
988 linedata = &priv->linedata[orientation];
989 lines = &request->lines[orientation];
991 size = total_size - (nonempty - 1) * linedata->spacing;
993 if (linedata->homogeneous)
995 extra = size / nonempty;
996 rest = size % nonempty;
998 for (i = 0; i < lines->max - lines->min; i++)
1000 line = &lines->lines[i];
1004 line->allocation = extra;
1007 line->allocation += 1;
1014 sizes = g_newa (GtkRequestedSize, nonempty);
1017 for (i = 0; i < lines->max - lines->min; i++)
1019 line = &lines->lines[i];
1023 size -= line->minimum;
1025 sizes[j].minimum_size = line->minimum;
1026 sizes[j].natural_size = line->natural;
1027 sizes[j].data = line;
1031 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1035 extra = size / expand;
1036 rest = size % expand;
1045 for (i = 0; i < lines->max - lines->min; i++)
1047 line = &lines->lines[i];
1051 g_assert (line == sizes[j].data);
1053 line->allocation = sizes[j].minimum_size;
1056 line->allocation += extra;
1059 line->allocation += 1;
1069 /* Computes the position fields from allocation and spacing.
1072 gtk_grid_request_position (GtkGridRequest *request,
1073 GtkOrientation orientation)
1075 GtkGridPrivate *priv = request->grid->priv;
1076 GtkGridLineData *linedata;
1077 GtkGridLines *lines;
1082 linedata = &priv->linedata[orientation];
1083 lines = &request->lines[orientation];
1086 for (i = 0; i < lines->max - lines->min; i++)
1088 line = &lines->lines[i];
1091 line->position = position;
1092 position += line->allocation + linedata->spacing;
1098 gtk_grid_get_size (GtkGrid *grid,
1099 GtkOrientation orientation,
1103 GtkGridRequest request;
1104 GtkGridLines *lines;
1112 if (grid->priv->children == NULL)
1115 request.grid = grid;
1116 gtk_grid_request_count_lines (&request);
1117 lines = &request.lines[orientation];
1118 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1119 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1121 gtk_grid_request_run (&request, orientation, FALSE);
1122 gtk_grid_request_sum (&request, orientation, minimum, natural);
1126 gtk_grid_get_size_for_size (GtkGrid *grid,
1127 GtkOrientation orientation,
1132 GtkGridRequest request;
1133 GtkGridLines *lines;
1142 if (grid->priv->children == NULL)
1145 request.grid = grid;
1146 gtk_grid_request_count_lines (&request);
1147 lines = &request.lines[0];
1148 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1149 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1150 lines = &request.lines[1];
1151 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1152 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1154 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1155 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1156 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1158 gtk_grid_request_run (&request, orientation, TRUE);
1159 gtk_grid_request_sum (&request, orientation, minimum, natural);
1163 gtk_grid_get_preferred_width (GtkWidget *widget,
1167 GtkGrid *grid = GTK_GRID (widget);
1169 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1170 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1172 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1176 gtk_grid_get_preferred_height (GtkWidget *widget,
1180 GtkGrid *grid = GTK_GRID (widget);
1182 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1183 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1185 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1189 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1194 GtkGrid *grid = GTK_GRID (widget);
1196 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1197 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1199 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1203 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1208 GtkGrid *grid = GTK_GRID (widget);
1210 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1211 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1213 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1217 allocate_child (GtkGridRequest *request,
1218 GtkOrientation orientation,
1219 GtkGridChild *child,
1223 GtkGridPrivate *priv = request->grid->priv;
1224 GtkGridLineData *linedata;
1225 GtkGridLines *lines;
1227 GtkGridChildAttach *attach;
1230 linedata = &priv->linedata[orientation];
1231 lines = &request->lines[orientation];
1232 attach = &child->attach[orientation];
1234 *position = lines->lines[attach->pos - lines->min].position;
1236 *size = (attach->span - 1) * linedata->spacing;
1237 for (i = 0; i < attach->span; i++)
1239 line = &lines->lines[attach->pos - lines->min + i];
1240 *size += line->allocation;
1245 gtk_grid_request_allocate_children (GtkGridRequest *request)
1247 GtkGridPrivate *priv = request->grid->priv;
1249 GtkGridChild *child;
1250 GtkAllocation allocation;
1251 GtkAllocation child_allocation;
1252 gint x, y, width, height;
1254 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1256 for (list = priv->children; list; list = list->next)
1260 if (!gtk_widget_get_visible (child->widget))
1263 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1264 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1266 child_allocation.x = allocation.x + x;
1267 child_allocation.y = allocation.y + y;
1268 child_allocation.width = MAX (1, width);
1269 child_allocation.height = MAX (1, height);
1271 if (gtk_widget_get_direction (GTK_WIDGET (request->grid)) == GTK_TEXT_DIR_RTL)
1272 child_allocation.x = allocation.x + allocation.width
1273 - (child_allocation.x - allocation.x) - child_allocation.width;
1275 gtk_widget_size_allocate (child->widget, &child_allocation);
1279 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1282 gtk_grid_size_allocate (GtkWidget *widget,
1283 GtkAllocation *allocation)
1285 GtkGrid *grid = GTK_GRID (widget);
1286 GtkGridPrivate *priv = grid->priv;
1287 GtkGridRequest request;
1288 GtkGridLines *lines;
1289 GtkOrientation orientation;
1291 if (priv->children == NULL)
1293 gtk_widget_set_allocation (widget, allocation);
1297 request.grid = grid;
1299 gtk_grid_request_count_lines (&request);
1300 lines = &request.lines[0];
1301 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1302 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1303 lines = &request.lines[1];
1304 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1305 memset (lines->lines, 0, (lines->max - lines->min) * sizeof (GtkGridLine));
1307 gtk_widget_set_allocation (widget, allocation);
1309 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1310 orientation = GTK_ORIENTATION_HORIZONTAL;
1312 orientation = GTK_ORIENTATION_VERTICAL;
1314 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1315 gtk_grid_request_allocate (&request, 1 - orientation, GET_SIZE (allocation, 1 - orientation));
1316 gtk_grid_request_run (&request, orientation, TRUE);
1317 gtk_grid_request_allocate (&request, orientation, GET_SIZE (allocation, orientation));
1319 gtk_grid_request_position (&request, 0);
1320 gtk_grid_request_position (&request, 1);
1322 gtk_grid_request_allocate_children (&request);
1326 gtk_grid_class_init (GtkGridClass *class)
1328 GObjectClass *object_class = G_OBJECT_CLASS (class);
1329 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1330 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1332 object_class->get_property = gtk_grid_get_property;
1333 object_class->set_property = gtk_grid_set_property;
1335 widget_class->size_allocate = gtk_grid_size_allocate;
1336 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1337 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1338 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1339 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1341 container_class->add = gtk_grid_add;
1342 container_class->remove = gtk_grid_remove;
1343 container_class->forall = gtk_grid_forall;
1344 container_class->child_type = gtk_grid_child_type;
1345 container_class->set_child_property = gtk_grid_set_child_property;
1346 container_class->get_child_property = gtk_grid_get_child_property;
1347 gtk_container_class_handle_border_width (container_class);
1349 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1351 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1352 g_param_spec_int ("row-spacing",
1354 P_("The amount of space between two consecutive rows"),
1356 GTK_PARAM_READWRITE));
1358 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1359 g_param_spec_int ("column-spacing",
1360 P_("Column spacing"),
1361 P_("The amount of space between two consecutive columns"),
1363 GTK_PARAM_READWRITE));
1365 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1366 g_param_spec_boolean ("row-homogeneous",
1367 P_("Row Homogeneous"),
1368 P_("If TRUE, the rows are all the same height"),
1370 GTK_PARAM_READWRITE));
1372 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1373 g_param_spec_boolean ("column-homogeneous",
1374 P_("Column Homogeneous"),
1375 P_("If TRUE, the columns are all the same width"),
1377 GTK_PARAM_READWRITE));
1379 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1380 g_param_spec_int ("left-attach",
1381 P_("Left attachment"),
1382 P_("The column number to attach the left side of the child to"),
1383 G_MININT, G_MAXINT, 0,
1384 GTK_PARAM_READWRITE));
1386 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1387 g_param_spec_int ("top-attach",
1388 P_("Top attachment"),
1389 P_("The row number to attach the top side of a child widget to"),
1390 G_MININT, G_MAXINT, 0,
1391 GTK_PARAM_READWRITE));
1393 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1394 g_param_spec_int ("width",
1396 P_("The number of columns that a child spans"),
1398 GTK_PARAM_READWRITE));
1400 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1401 g_param_spec_int ("height",
1403 P_("The number of rows that a child spans"),
1405 GTK_PARAM_READWRITE));
1407 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1413 * Creates a new grid widget.
1415 * Returns: the new #GtkGrid
1420 return g_object_new (GTK_TYPE_GRID, NULL);
1426 * @child: the widget to add
1427 * @left: the column number to attach the left side of @child to
1428 * @top: the row number to attach the top side of @child to
1429 * @width: the number of columns that @child will span
1430 * @height: the number of rows that @child will span
1432 * Adds a widget to the grid.
1434 * The position of @child is determined by @left and @top. The
1435 * number of 'cells' that @child will occupy is determined by
1436 * @width and @height.
1439 gtk_grid_attach (GtkGrid *grid,
1446 g_return_if_fail (GTK_IS_GRID (grid));
1447 g_return_if_fail (GTK_IS_WIDGET (child));
1448 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1449 g_return_if_fail (width > 0);
1450 g_return_if_fail (height > 0);
1452 grid_attach (grid, child, left, top, width, height);
1456 * gtk_grid_attach_next_to:
1458 * @child: the widget to add
1459 * @sibling: (allow-none): the child of @grid that @child will be placed
1460 * next to, or %NULL to place @child at the beginning or end
1461 * @side: the side of @sibling that @child is positioned next to
1462 * @width: the number of columns that @child will span
1463 * @height: the number of rows that @child will span
1465 * Adds a widget to the grid.
1467 * The widget is placed next to @sibling, on the side determined by
1468 * @side. When @sibling is %NULL, the widget is placed in row (for
1469 * left or right placement) or column 0 (for top or bottom placement),
1470 * at the end indicated by @side.
1472 * Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and
1473 * @side == %GTK_POS_LEFT yields a layout of [3][2][1].
1476 gtk_grid_attach_next_to (GtkGrid *grid,
1479 GtkPositionType side,
1483 GtkGridChild *grid_sibling;
1486 g_return_if_fail (GTK_IS_GRID (grid));
1487 g_return_if_fail (GTK_IS_WIDGET (child));
1488 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1489 g_return_if_fail (sibling == NULL || gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1490 g_return_if_fail (width > 0);
1491 g_return_if_fail (height > 0);
1495 grid_sibling = find_grid_child (grid, sibling);
1500 left = CHILD_LEFT (grid_sibling) - width;
1501 top = CHILD_TOP (grid_sibling);
1504 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1505 top = CHILD_TOP (grid_sibling);
1508 left = CHILD_LEFT (grid_sibling);
1509 top = CHILD_TOP (grid_sibling) - height;
1511 case GTK_POS_BOTTOM:
1512 left = CHILD_LEFT (grid_sibling);
1513 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1516 g_assert_not_reached ();
1524 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, FALSE);
1529 left = find_attach_position (grid, GTK_ORIENTATION_HORIZONTAL, 0, height, TRUE);
1534 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, FALSE);
1537 case GTK_POS_BOTTOM:
1539 top = find_attach_position (grid, GTK_ORIENTATION_VERTICAL, 0, width, TRUE);
1542 g_assert_not_reached ();
1546 grid_attach (grid, child, left, top, width, height);
1550 * gtk_grid_get_child_at:
1552 * @left: the left edge of the cell
1553 * @top: the top edge of the cell
1555 * Gets the child of @grid whose area covers the grid
1556 * cell whose upper left corner is at @left, @top.
1558 * Returns: the child at the given position, or %NULL
1563 gtk_grid_get_child_at (GtkGrid *grid,
1567 GtkGridPrivate *priv = grid->priv;
1568 GtkGridChild *child;
1571 for (list = priv->children; list; list = list->next)
1575 if (CHILD_LEFT (child) <= left &&
1576 CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
1577 CHILD_TOP (child) <= top &&
1578 CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
1579 return child->widget;
1586 * gtk_grid_insert_row:
1588 * @position: the position to insert the row at
1590 * Inserts a row at the specified position.
1592 * Children which are attached at or below this position
1593 * are moved one row down. Children which span across this
1594 * position are grown to span the new row.
1599 gtk_grid_insert_row (GtkGrid *grid,
1602 GtkGridPrivate *priv = grid->priv;
1603 GtkGridChild *child;
1607 g_return_if_fail (GTK_IS_GRID (grid));
1609 for (list = priv->children; list; list = list->next)
1613 top = CHILD_TOP (child);
1614 height = CHILD_HEIGHT (child);
1616 if (top >= position)
1618 CHILD_TOP (child) = top + 1;
1619 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
1621 else if (top + height > position)
1623 CHILD_HEIGHT (child) = height + 1;
1624 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
1630 * gtk_grid_insert_column:
1632 * @position: the position to insert the column at
1634 * Inserts a column at the specified position.
1636 * Children which are attached at or to the right of this position
1637 * are moved one column to the right. Children which span across this
1638 * position are grown to span the new column.
1643 gtk_grid_insert_column (GtkGrid *grid,
1646 GtkGridPrivate *priv = grid->priv;
1647 GtkGridChild *child;
1651 g_return_if_fail (GTK_IS_GRID (grid));
1653 for (list = priv->children; list; list = list->next)
1657 left = CHILD_LEFT (child);
1658 width = CHILD_WIDTH (child);
1660 if (left >= position)
1662 CHILD_LEFT (child) = left + 1;
1663 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
1665 else if (left + width > position)
1667 CHILD_WIDTH (child) = width + 1;
1668 gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
1674 * gtk_grid_insert_next_to:
1676 * @sibling: the child of @grid that the new row or column will be
1678 * @side: the side of @sibling that @child is positioned next to
1680 * Inserts a row or column at the specified position.
1682 * The new row or column is placed next to @sibling, on the side
1683 * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
1684 * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
1685 * a column is inserted.
1690 gtk_grid_insert_next_to (GtkGrid *grid,
1692 GtkPositionType side)
1694 GtkGridChild *child;
1696 g_return_if_fail (GTK_IS_GRID (grid));
1697 g_return_if_fail (GTK_IS_WIDGET (sibling));
1698 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1700 child = find_grid_child (grid, sibling);
1705 gtk_grid_insert_column (grid, CHILD_LEFT (child));
1708 gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
1711 gtk_grid_insert_row (grid, CHILD_TOP (child));
1713 case GTK_POS_BOTTOM:
1714 gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
1717 g_assert_not_reached ();
1722 * gtk_grid_set_row_homogeneous:
1724 * @homogeneous: %TRUE to make rows homogeneous
1726 * Sets whether all rows of @grid will have the same height.
1729 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1730 gboolean homogeneous)
1732 GtkGridPrivate *priv;
1733 g_return_if_fail (GTK_IS_GRID (grid));
1737 /* Yes, homogeneous rows means all the columns have the same size */
1738 if (COLUMNS (priv)->homogeneous != homogeneous)
1740 COLUMNS (priv)->homogeneous = homogeneous;
1742 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1743 gtk_widget_queue_resize (GTK_WIDGET (grid));
1745 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1750 * gtk_grid_get_row_homogeneous:
1753 * Returns whether all rows of @grid have the same height.
1755 * Returns: whether all rows of @grid have the same height.
1758 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1760 GtkGridPrivate *priv;
1761 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1765 return COLUMNS (priv)->homogeneous;
1769 * gtk_grid_set_column_homogeneous:
1771 * @homogeneous: %TRUE to make columns homogeneous
1773 * Sets whether all columns of @grid will have the same width.
1776 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1777 gboolean homogeneous)
1779 GtkGridPrivate *priv;
1780 g_return_if_fail (GTK_IS_GRID (grid));
1784 /* Yes, homogeneous columns means all the rows have the same size */
1785 if (ROWS (priv)->homogeneous != homogeneous)
1787 ROWS (priv)->homogeneous = homogeneous;
1789 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1790 gtk_widget_queue_resize (GTK_WIDGET (grid));
1792 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1797 * gtk_grid_get_column_homogeneous:
1800 * Returns whether all columns of @grid have the same width.
1802 * Returns: whether all columns of @grid have the same width.
1805 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1807 GtkGridPrivate *priv;
1808 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1812 return ROWS (priv)->homogeneous;
1816 * gtk_grid_set_row_spacing:
1818 * @spacing: the amount of space to insert between rows
1820 * Sets the amount of space between rows of @grid.
1823 gtk_grid_set_row_spacing (GtkGrid *grid,
1826 GtkGridPrivate *priv;
1827 g_return_if_fail (GTK_IS_GRID (grid));
1828 g_return_if_fail (spacing <= G_MAXINT16);
1832 if (COLUMNS (priv)->spacing != spacing)
1834 COLUMNS (priv)->spacing = spacing;
1836 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1837 gtk_widget_queue_resize (GTK_WIDGET (grid));
1839 g_object_notify (G_OBJECT (grid), "row-spacing");
1844 * gtk_grid_get_row_spacing:
1847 * Returns the amount of space between the rows of @grid.
1849 * Returns: the row spacing of @grid
1852 gtk_grid_get_row_spacing (GtkGrid *grid)
1854 GtkGridPrivate *priv;
1855 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1859 return COLUMNS (priv)->spacing;
1863 * gtk_grid_set_column_spacing:
1865 * @spacing: the amount of space to insert between columns
1867 * Sets the amount of space between columns of @grid.
1870 gtk_grid_set_column_spacing (GtkGrid *grid,
1873 GtkGridPrivate *priv;
1874 g_return_if_fail (GTK_IS_GRID (grid));
1875 g_return_if_fail (spacing <= G_MAXINT16);
1879 if (ROWS (priv)->spacing != spacing)
1881 ROWS (priv)->spacing = spacing;
1883 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1884 gtk_widget_queue_resize (GTK_WIDGET (grid));
1886 g_object_notify (G_OBJECT (grid), "column-spacing");
1891 * gtk_grid_get_column_spacing:
1894 * Returns the amount of space between the columns of @grid.
1896 * Returns: the column spacing of @grid
1899 gtk_grid_get_column_spacing (GtkGrid *grid)
1901 GtkGridPrivate *priv;
1903 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1907 return ROWS (priv)->spacing;