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.
25 #include "gtkorientable.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, ROWS (priv)->spacing);
167 case PROP_COLUMN_SPACING:
168 g_value_set_int (value, COLUMNS (priv)->spacing);
171 case PROP_ROW_HOMOGENEOUS:
172 g_value_set_boolean (value, ROWS (priv)->homogeneous);
175 case PROP_COLUMN_HOMOGENEOUS:
176 g_value_set_boolean (value, COLUMNS (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;
190 GtkStyleContext *context;
192 if (priv->orientation != orientation)
194 priv->orientation = orientation;
195 context = gtk_widget_get_style_context (GTK_WIDGET (grid));
197 if (grid->priv->orientation == GTK_ORIENTATION_VERTICAL)
199 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
200 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_HORIZONTAL);
204 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
205 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_VERTICAL);
208 g_object_notify (G_OBJECT (grid), "orientation");
213 gtk_grid_set_property (GObject *object,
218 GtkGrid *grid = GTK_GRID (object);
222 case PROP_ORIENTATION:
223 gtk_grid_set_orientation (grid, g_value_get_enum (value));
226 case PROP_ROW_SPACING:
227 gtk_grid_set_row_spacing (grid, g_value_get_int (value));
230 case PROP_COLUMN_SPACING:
231 gtk_grid_set_column_spacing (grid, g_value_get_int (value));
234 case PROP_ROW_HOMOGENEOUS:
235 gtk_grid_set_row_homogeneous (grid, g_value_get_boolean (value));
238 case PROP_COLUMN_HOMOGENEOUS:
239 gtk_grid_set_column_homogeneous (grid, g_value_get_boolean (value));
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 static GtkGridChild *
249 find_grid_child (GtkGrid *grid,
252 GtkGridPrivate *priv = grid->priv;
256 for (list = priv->children; list; list = list->next)
260 if (child->widget == widget)
268 gtk_grid_get_child_property (GtkContainer *container,
274 GtkGrid *grid = GTK_GRID (container);
275 GtkGridChild *grid_child;
277 grid_child = find_grid_child (grid, child);
279 if (grid_child == NULL)
281 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
287 case CHILD_PROP_LEFT_ATTACH:
288 g_value_set_int (value, CHILD_LEFT (grid_child));
291 case CHILD_PROP_TOP_ATTACH:
292 g_value_set_int (value, CHILD_TOP (grid_child));
295 case CHILD_PROP_WIDTH:
296 g_value_set_int (value, CHILD_WIDTH (grid_child));
299 case CHILD_PROP_HEIGHT:
300 g_value_set_int (value, CHILD_HEIGHT (grid_child));
304 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
310 gtk_grid_set_child_property (GtkContainer *container,
316 GtkGrid *grid = GTK_GRID (container);
317 GtkGridChild *grid_child;
319 grid_child = find_grid_child (grid, child);
321 if (grid_child == NULL)
323 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
329 case CHILD_PROP_LEFT_ATTACH:
330 CHILD_LEFT (grid_child) = g_value_get_int (value);
333 case CHILD_PROP_TOP_ATTACH:
334 CHILD_TOP (grid_child) = g_value_get_int (value);
337 case CHILD_PROP_WIDTH:
338 CHILD_WIDTH (grid_child) = g_value_get_int (value);
341 case CHILD_PROP_HEIGHT:
342 CHILD_HEIGHT (grid_child) = g_value_get_int (value);
346 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
350 if (gtk_widget_get_visible (child) &&
351 gtk_widget_get_visible (GTK_WIDGET (grid)))
352 gtk_widget_queue_resize (child);
356 gtk_grid_init (GtkGrid *grid)
358 GtkGridPrivate *priv;
360 grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, GTK_TYPE_GRID, GtkGridPrivate);
363 gtk_widget_set_has_window (GTK_WIDGET (grid), FALSE);
364 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (grid), FALSE);
366 priv->children = NULL;
367 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
369 priv->linedata[0].spacing = 0;
370 priv->linedata[1].spacing = 0;
372 priv->linedata[0].homogeneous = FALSE;
373 priv->linedata[1].homogeneous = FALSE;
376 static void grid_attach (GtkGrid *grid,
384 gtk_grid_add (GtkContainer *container,
387 GtkGrid *grid = GTK_GRID (container);
388 GtkGridPrivate *priv = grid->priv;
389 GtkGridChild *grid_child;
390 GtkGridChildAttach *attach;
391 GtkGridChildAttach *opposite;
396 for (list = priv->children; list; list = list->next)
398 grid_child = list->data;
400 attach = &grid_child->attach[priv->orientation];
401 opposite = &grid_child->attach[1 - priv->orientation];
403 if (opposite->pos <= 0 && opposite->pos + opposite->span > 0)
404 pos = MAX (pos, attach->pos + attach->span);
407 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
408 grid_attach (grid, child, pos, 0, 1, 1);
410 grid_attach (grid, child, 0, pos, 1, 1);
414 gtk_grid_remove (GtkContainer *container,
417 GtkGrid *grid = GTK_GRID (container);
418 GtkGridPrivate *priv = grid->priv;
419 GtkGridChild *grid_child;
422 for (list = priv->children; list; list = list->next)
424 grid_child = list->data;
426 if (grid_child->widget == child)
428 gboolean was_visible = gtk_widget_get_visible (child);
430 gtk_widget_unparent (child);
432 priv->children = g_list_remove (priv->children, grid_child);
434 g_slice_free (GtkGridChild, grid_child);
436 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (grid)))
437 gtk_widget_queue_resize (GTK_WIDGET (grid));
445 gtk_grid_forall (GtkContainer *container,
446 gboolean include_internals,
447 GtkCallback callback,
448 gpointer callback_data)
450 GtkGrid *grid = GTK_GRID (container);
451 GtkGridPrivate *priv = grid->priv;
455 list = priv->children;
461 (* callback) (child->widget, callback_data);
466 gtk_grid_child_type (GtkContainer *container)
468 return GTK_TYPE_WIDGET;
471 static GtkSizeRequestMode
472 gtk_grid_get_request_mode (GtkWidget *widget)
474 GtkGridPrivate *priv = GTK_GRID (widget)->priv;
476 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
477 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
479 return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
482 /* Calculates the min and max numbers for both orientations.
485 gtk_grid_request_count_lines (GtkGridRequest *request)
487 GtkGridPrivate *priv = request->grid->priv;
489 GtkGridChildAttach *attach;
494 min[0] = min[1] = G_MAXINT;
495 max[0] = max[1] = G_MININT;
497 for (list = priv->children; list; list = list->next)
500 attach = child->attach;
502 min[0] = MIN (min[0], attach[0].pos);
503 max[0] = MAX (max[0], attach[0].pos + attach[0].span);
504 min[1] = MIN (min[1], attach[1].pos);
505 max[1] = MAX (max[1], attach[1].pos + attach[1].span);
508 request->lines[0].min = min[0];
509 request->lines[0].max = max[0];
510 request->lines[1].min = min[1];
511 request->lines[1].max = max[1];
514 /* Sets line sizes to 0 and marks lines as expand
515 * if they have a non-spanning expanding child.
518 gtk_grid_request_init (GtkGridRequest *request,
519 GtkOrientation orientation)
521 GtkGridPrivate *priv = request->grid->priv;
523 GtkGridChildAttach *attach;
528 lines = &request->lines[orientation];
530 for (i = 0; i < lines->max - lines->min; i++)
532 lines->lines[i].minimum = 0;
533 lines->lines[i].natural = 0;
534 lines->lines[i].expand = FALSE;
537 for (list = priv->children; list; list = list->next)
541 attach = &child->attach[orientation];
542 if (attach->span == 1 && gtk_widget_compute_expand (child->widget, orientation))
543 lines->lines[attach->pos - lines->min].expand = TRUE;
547 /* Sums allocations for lines spanned by child and their spacing.
550 compute_allocation_for_child (GtkGridRequest *request,
552 GtkOrientation orientation)
554 GtkGridPrivate *priv = request->grid->priv;
555 GtkGridLineData *linedata;
558 GtkGridChildAttach *attach;
562 linedata = &priv->linedata[orientation];
563 lines = &request->lines[orientation];
564 attach = &child->attach[orientation];
566 size = (attach->span - 1) * linedata->spacing;
567 for (i = 0; i < attach->span; i++)
569 line = &lines->lines[attach->pos - lines->min + i];
570 size += line->allocation;
577 compute_request_for_child (GtkGridRequest *request,
579 GtkOrientation orientation,
588 size = compute_allocation_for_child (request, child, 1 - orientation);
589 if (orientation == GTK_ORIENTATION_HORIZONTAL)
590 gtk_widget_get_preferred_width_for_height (child->widget,
594 gtk_widget_get_preferred_height_for_width (child->widget,
600 if (orientation == GTK_ORIENTATION_HORIZONTAL)
601 gtk_widget_get_preferred_width (child->widget, minimum, natural);
603 gtk_widget_get_preferred_height (child->widget, minimum, natural);
607 /* Sets requisition to max. of non-spanning children.
608 * If contextual is TRUE, requires allocations of
609 * lines in the opposite orientation to be set.
612 gtk_grid_request_non_spanning (GtkGridRequest *request,
613 GtkOrientation orientation,
616 GtkGridPrivate *priv = request->grid->priv;
618 GtkGridChildAttach *attach;
625 lines = &request->lines[orientation];
627 for (list = priv->children; list; list = list->next)
631 if (!gtk_widget_get_visible (child->widget))
634 attach = &child->attach[orientation];
635 if (attach->span != 1)
638 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
640 line = &lines->lines[attach->pos - lines->min];
641 line->minimum = MAX (line->minimum, minimum);
642 line->natural = MAX (line->natural, natural);
646 /* Enforce homogeneous sizes.
649 gtk_grid_request_homogeneous (GtkGridRequest *request,
650 GtkOrientation orientation)
652 GtkGridPrivate *priv = request->grid->priv;
653 GtkGridLineData *linedata;
655 gint minimum, natural;
658 linedata = &priv->linedata[orientation];
659 lines = &request->lines[orientation];
661 if (!linedata->homogeneous)
667 for (i = 0; i < lines->max - lines->min; i++)
669 minimum = MAX (minimum, lines->lines[i].minimum);
670 natural = MAX (natural, lines->lines[i].natural);
673 for (i = 0; i < lines->max - lines->min; i++)
675 lines->lines[i].minimum = minimum;
676 lines->lines[i].natural = natural;
680 /* Deals with spanning children.
681 * Requires expand fields of lines to be set for
682 * non-spanning children.
685 gtk_grid_request_spanning (GtkGridRequest *request,
686 GtkOrientation orientation,
689 GtkGridPrivate *priv = request->grid->priv;
692 GtkGridChildAttach *attach;
693 GtkGridLineData *linedata;
701 gboolean force_expand;
707 linedata = &priv->linedata[orientation];
708 lines = &request->lines[orientation];
710 for (list = priv->children; list; list = list->next)
714 if (!gtk_widget_get_visible (child->widget))
717 attach = &child->attach[orientation];
718 if (attach->span == 1)
721 compute_request_for_child (request, child, orientation, contextual, &minimum, &natural);
723 span_minimum = (attach->span - 1) * linedata->spacing;
724 span_natural = (attach->span - 1) * linedata->spacing;
726 force_expand = FALSE;
727 for (i = 0; i < attach->span; i++)
729 line = &lines->lines[attach->pos - lines->min + i];
730 span_minimum += line->minimum;
731 span_natural += line->natural;
735 if (span_expand == 0)
737 span_expand = attach->span;
741 /* If we need to request more space for this child to fill
742 * its requisition, then divide up the needed space amongst the
743 * lines it spans, favoring expandable lines if any.
745 if (span_minimum < minimum)
747 extra = minimum - span_minimum;
748 expand = span_expand;
749 for (i = 0; i < attach->span; i++)
751 line = &lines->lines[attach->pos - lines->min + i];
752 if (force_expand || line->expand)
754 line_extra = extra / expand;
755 line->minimum += line_extra;
762 if (span_natural < natural)
764 extra = natural - span_natural;
765 expand = span_expand;
766 for (i = 0; i < attach->span; i++)
768 line = &lines->lines[attach->pos - lines->min + i];
769 if (force_expand || line->expand)
771 line_extra = extra / expand;
772 line->natural += line_extra;
781 /* Marks empty and expanding lines and counts them.
784 gtk_grid_request_compute_expand (GtkGridRequest *request,
785 GtkOrientation orientation,
786 gint *nonempty_lines,
789 GtkGridPrivate *priv = request->grid->priv;
791 GtkGridChildAttach *attach;
800 lines = &request->lines[orientation];
802 for (i = 0; i < lines->max - lines->min; i++)
804 lines->lines[i].need_expand = FALSE;
805 lines->lines[i].expand = FALSE;
806 lines->lines[i].empty = TRUE;
809 for (list = priv->children; list; list = list->next)
813 if (!gtk_widget_get_visible (child->widget))
816 attach = &child->attach[orientation];
817 if (attach->span != 1)
820 line = &lines->lines[attach->pos - lines->min];
822 if (gtk_widget_compute_expand (child->widget, orientation))
826 for (list = priv->children; list; list = list->next)
830 if (!gtk_widget_get_visible (child->widget))
833 attach = &child->attach[orientation];
834 if (attach->span == 1)
838 for (i = 0; i < attach->span; i++)
840 line = &lines->lines[attach->pos - lines->min + i];
846 if (!has_expand && gtk_widget_compute_expand (child->widget, orientation))
848 for (i = 0; i < attach->span; i++)
850 line = &lines->lines[attach->pos - lines->min + i];
851 line->need_expand = TRUE;
858 for (i = 0; i < lines->max - lines->min; i++)
860 line = &lines->lines[i];
862 if (line->need_expand)
873 *nonempty_lines = lines->max - lines->min - empty;
876 *expand_lines = expand;
879 /* Sums the minimum and natural fields of lines and their spacing.
882 gtk_grid_request_sum (GtkGridRequest *request,
883 GtkOrientation orientation,
887 GtkGridPrivate *priv = request->grid->priv;
888 GtkGridLineData *linedata;
894 gtk_grid_request_compute_expand (request, orientation, &nonempty, NULL);
896 linedata = &priv->linedata[orientation];
897 lines = &request->lines[orientation];
899 min = (nonempty - 1) * linedata->spacing;
900 nat = (nonempty - 1) * linedata->spacing;
902 for (i = 0; i < lines->max - lines->min; i++)
904 min += lines->lines[i].minimum;
905 nat += lines->lines[i].natural;
915 /* Computes minimum and natural fields of lines.
916 * When contextual is TRUE, requires allocation of
917 * lines in the opposite orientation to be set.
920 gtk_grid_request_run (GtkGridRequest *request,
921 GtkOrientation orientation,
924 gtk_grid_request_init (request, orientation);
925 gtk_grid_request_non_spanning (request, orientation, contextual);
926 gtk_grid_request_homogeneous (request, orientation);
927 gtk_grid_request_spanning (request, orientation, contextual);
928 gtk_grid_request_homogeneous (request, orientation);
931 /* Requires that the minimum and natural fields of lines
932 * have been set, computes the allocation field of lines
933 * by distributing total_size among lines.
936 gtk_grid_request_allocate (GtkGridRequest *request,
937 GtkOrientation orientation,
940 GtkGridPrivate *priv = request->grid->priv;
941 GtkGridLineData *linedata;
947 GtkRequestedSize *sizes;
952 gtk_grid_request_compute_expand (request, orientation, &nonempty, &expand);
954 linedata = &priv->linedata[orientation];
955 lines = &request->lines[orientation];
957 size = total_size - (nonempty - 1) * linedata->spacing;
959 if (linedata->homogeneous)
961 extra = size / nonempty;
962 rest = size % nonempty;
964 for (i = 0; i < lines->max - lines->min; i++)
966 line = &lines->lines[i];
970 line->allocation = extra;
973 line->allocation += 1;
980 sizes = g_newa (GtkRequestedSize, nonempty);
983 for (i = 0; i < lines->max - lines->min; i++)
985 line = &lines->lines[i];
989 size -= line->minimum;
991 sizes[j].minimum_size = line->minimum;
992 sizes[j].natural_size = line->natural;
993 sizes[j].data = line;
997 size = gtk_distribute_natural_allocation (MAX (0, size), nonempty, sizes);
1001 extra = size / expand;
1002 rest = size % expand;
1011 for (i = 0; i < lines->max - lines->min; i++)
1013 line = &lines->lines[i];
1017 g_assert (line == sizes[j].data);
1019 line->allocation = sizes[j].minimum_size;
1022 line->allocation += extra;
1025 line->allocation += 1;
1035 /* Computes the position fields from allocation and spacing.
1038 gtk_grid_request_position (GtkGridRequest *request,
1039 GtkOrientation orientation)
1041 GtkGridPrivate *priv = request->grid->priv;
1042 GtkGridLineData *linedata;
1043 GtkGridLines *lines;
1048 linedata = &priv->linedata[orientation];
1049 lines = &request->lines[orientation];
1052 for (i = 0; i < lines->max - lines->min; i++)
1054 line = &lines->lines[i];
1057 line->position = position;
1058 position += line->allocation + linedata->spacing;
1064 gtk_grid_get_size (GtkGrid *grid,
1065 GtkOrientation orientation,
1069 GtkGridRequest request;
1070 GtkGridLines *lines;
1072 request.grid = grid;
1073 gtk_grid_request_count_lines (&request);
1074 lines = &request.lines[orientation];
1075 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1077 gtk_grid_request_run (&request, orientation, FALSE);
1078 gtk_grid_request_sum (&request, orientation, minimum, natural);
1082 gtk_grid_get_size_for_size (GtkGrid *grid,
1083 GtkOrientation orientation,
1088 GtkGridRequest request;
1089 GtkGridLines *lines;
1092 request.grid = grid;
1093 gtk_grid_request_count_lines (&request);
1094 lines = &request.lines[0];
1095 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1096 lines = &request.lines[1];
1097 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1099 gtk_grid_request_run (&request, 1 - orientation, FALSE);
1100 gtk_grid_request_sum (&request, 1 - orientation, &min_size, NULL);
1101 gtk_grid_request_allocate (&request, 1 - orientation, MAX (size, min_size));
1103 gtk_grid_request_run (&request, orientation, TRUE);
1104 gtk_grid_request_sum (&request, orientation, minimum, natural);
1108 gtk_grid_get_preferred_width (GtkWidget *widget,
1112 GtkGrid *grid = GTK_GRID (widget);
1114 if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1116 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, 0, minimum, natural);
1118 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1122 gtk_grid_get_preferred_height (GtkWidget *widget,
1126 GtkGrid *grid = GTK_GRID (widget);
1128 if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1129 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, 0, minimum, natural);
1131 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1135 gtk_grid_get_preferred_width_for_height (GtkWidget *widget,
1140 GtkGrid *grid = GTK_GRID (widget);
1142 if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
1143 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
1145 gtk_grid_get_size (grid, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
1149 gtk_grid_get_preferred_height_for_width (GtkWidget *widget,
1154 GtkGrid *grid = GTK_GRID (widget);
1156 if (gtk_grid_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1157 gtk_grid_get_size_for_size (grid, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
1159 gtk_grid_get_size (grid, GTK_ORIENTATION_VERTICAL, minimum, natural);
1163 allocate_child (GtkGridRequest *request,
1164 GtkOrientation orientation,
1165 GtkGridChild *child,
1169 GtkGridPrivate *priv = request->grid->priv;
1170 GtkGridLineData *linedata;
1171 GtkGridLines *lines;
1173 GtkGridChildAttach *attach;
1176 linedata = &priv->linedata[orientation];
1177 lines = &request->lines[orientation];
1178 attach = &child->attach[orientation];
1180 *position = lines->lines[attach->pos - lines->min].position;
1182 *size = (attach->span - 1) * linedata->spacing;
1183 for (i = 0; i < attach->span; i++)
1185 line = &lines->lines[attach->pos - lines->min + i];
1186 *size += line->allocation;
1191 gtk_grid_request_allocate_children (GtkGridRequest *request)
1193 GtkGridPrivate *priv = request->grid->priv;
1195 GtkGridChild *child;
1196 GtkAllocation allocation;
1197 GtkAllocation child_allocation;
1198 gint x, y, width, height;
1200 gtk_widget_get_allocation (GTK_WIDGET (request->grid), &allocation);
1202 for (list = priv->children; list; list = list->next)
1206 if (!gtk_widget_get_visible (child->widget))
1209 allocate_child (request, GTK_ORIENTATION_HORIZONTAL, child, &x, &width);
1210 allocate_child (request, GTK_ORIENTATION_VERTICAL, child, &y, &height);
1212 child_allocation.x = allocation.x + x;
1213 child_allocation.y = allocation.y + y;
1214 child_allocation.width = MAX (1, width);
1215 child_allocation.height = MAX (1, height);
1217 gtk_widget_size_allocate (child->widget, &child_allocation);
1221 #define GET_SIZE(allocation, orientation) (orientation == GTK_ORIENTATION_HORIZONTAL ? allocation->width : allocation->height)
1224 gtk_grid_size_allocate (GtkWidget *widget,
1225 GtkAllocation *allocation)
1227 GtkGrid *grid = GTK_GRID (widget);
1228 GtkGridPrivate *priv = grid->priv;
1229 GtkGridRequest request;
1230 GtkGridLines *lines;
1232 request.grid = grid;
1233 gtk_grid_request_count_lines (&request);
1234 lines = &request.lines[0];
1235 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1236 lines = &request.lines[1];
1237 lines->lines = g_newa (GtkGridLine, lines->max - lines->min);
1239 gtk_widget_set_allocation (widget, allocation);
1241 gtk_grid_request_run (&request, 1 - priv->orientation, FALSE);
1242 gtk_grid_request_allocate (&request, 1 - priv->orientation, GET_SIZE (allocation, 1 - priv->orientation));
1243 gtk_grid_request_run (&request, priv->orientation, TRUE);
1244 gtk_grid_request_allocate (&request, priv->orientation, GET_SIZE (allocation, priv->orientation));
1246 gtk_grid_request_position (&request, 0);
1247 gtk_grid_request_position (&request, 1);
1249 gtk_grid_request_allocate_children (&request);
1253 gtk_grid_class_init (GtkGridClass *class)
1255 GObjectClass *object_class = G_OBJECT_CLASS (class);
1256 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1257 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
1259 object_class->get_property = gtk_grid_get_property;
1260 object_class->set_property = gtk_grid_set_property;
1262 widget_class->size_allocate = gtk_grid_size_allocate;
1263 widget_class->get_preferred_width = gtk_grid_get_preferred_width;
1264 widget_class->get_preferred_height = gtk_grid_get_preferred_height;
1265 widget_class->get_request_mode = gtk_grid_get_request_mode;
1266 widget_class->get_preferred_width_for_height = gtk_grid_get_preferred_width_for_height;
1267 widget_class->get_preferred_height_for_width = gtk_grid_get_preferred_height_for_width;
1269 container_class->add = gtk_grid_add;
1270 container_class->remove = gtk_grid_remove;
1271 container_class->forall = gtk_grid_forall;
1272 container_class->child_type = gtk_grid_child_type;
1273 container_class->set_child_property = gtk_grid_set_child_property;
1274 container_class->get_child_property = gtk_grid_get_child_property;
1275 gtk_container_class_handle_border_width (container_class);
1277 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
1279 g_object_class_install_property (object_class, PROP_ROW_SPACING,
1280 g_param_spec_int ("row-spacing",
1282 P_("The amount of space between two consecutive rows"),
1284 GTK_PARAM_READWRITE));
1286 g_object_class_install_property (object_class, PROP_COLUMN_SPACING,
1287 g_param_spec_int ("column-spacing",
1288 P_("Column spacing"),
1289 P_("The amount of space between two consecutive columns"),
1291 GTK_PARAM_READWRITE));
1293 g_object_class_install_property (object_class, PROP_ROW_HOMOGENEOUS,
1294 g_param_spec_boolean ("row-homogeneous",
1295 P_("Row Homogeneous"),
1296 P_("If TRUE, the rows are all the same height"),
1298 GTK_PARAM_READWRITE));
1300 g_object_class_install_property (object_class, PROP_COLUMN_HOMOGENEOUS,
1301 g_param_spec_boolean ("column-homogeneous",
1302 P_("Column Homogeneous"),
1303 P_("If TRUE, the columns are all the same width"),
1305 GTK_PARAM_READWRITE));
1307 gtk_container_class_install_child_property (container_class, CHILD_PROP_LEFT_ATTACH,
1308 g_param_spec_int ("left-attach",
1309 P_("Left attachment"),
1310 P_("The column number to attach the left side of the child to"),
1311 G_MININT, G_MAXINT, 0,
1312 GTK_PARAM_READWRITE));
1314 gtk_container_class_install_child_property (container_class, CHILD_PROP_TOP_ATTACH,
1315 g_param_spec_int ("top-attach",
1316 P_("Top attachment"),
1317 P_("The row number to attach the top side of a child widget to"),
1318 G_MININT, G_MAXINT, 0,
1319 GTK_PARAM_READWRITE));
1321 gtk_container_class_install_child_property (container_class, CHILD_PROP_WIDTH,
1322 g_param_spec_int ("width",
1324 P_("The number of columns that a child spans"),
1326 GTK_PARAM_READWRITE));
1328 gtk_container_class_install_child_property (container_class, CHILD_PROP_HEIGHT,
1329 g_param_spec_int ("height",
1331 P_("The number of rows that a child spans"),
1333 GTK_PARAM_READWRITE));
1335 g_type_class_add_private (class, sizeof (GtkGridPrivate));
1341 * Creates a new grid widget.
1343 * Returns: the new #GtkGrid
1348 return g_object_new (GTK_TYPE_GRID, NULL);
1352 grid_attach (GtkGrid *grid,
1359 GtkGridPrivate *priv = grid->priv;
1360 GtkGridChild *child;
1362 child = g_slice_new (GtkGridChild);
1363 child->widget = widget;
1364 CHILD_LEFT (child) = left;
1365 CHILD_TOP (child) = top;
1366 CHILD_WIDTH (child) = width;
1367 CHILD_HEIGHT (child) = height;
1369 priv->children = g_list_prepend (priv->children, child);
1371 gtk_widget_set_parent (widget, GTK_WIDGET (grid));
1377 * @child: the widget to add
1378 * @left: the column number to attach the left side of @child to
1379 * @top: the row number to attach the top side of @child to
1380 * @width: the number of columns that @child will span
1381 * @height: the number of rows that @child will span
1383 * Adds a widget to the grid.
1385 * The position of @child is determined by @left and @top. The
1386 * number of 'cells' that @child will occupy is determined by
1387 * @width and @height.
1390 gtk_grid_attach (GtkGrid *grid,
1397 g_return_if_fail (GTK_IS_GRID (grid));
1398 g_return_if_fail (GTK_IS_WIDGET (child));
1399 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1400 g_return_if_fail (width > 0);
1401 g_return_if_fail (height > 0);
1403 grid_attach (grid, child, left, top, width, height);
1407 * gtk_grid_attach_next_to:
1409 * @child: the widget to add
1410 * @sibling: the child of @grid that @child will be placed next to
1411 * @side: the side of @sibling that @child is positioned next to
1412 * @width: the number of columns that @child will span
1413 * @height: the number of rows that @child will span
1415 * Adds a widget to the grid.
1417 * The widget is placed next to @sibling, on the side determined by
1421 gtk_grid_attach_next_to (GtkGrid *grid,
1424 GtkPositionType side,
1428 GtkGridChild *grid_sibling;
1431 g_return_if_fail (GTK_IS_GRID (grid));
1432 g_return_if_fail (GTK_IS_WIDGET (child));
1433 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1434 g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
1435 g_return_if_fail (width > 0);
1436 g_return_if_fail (height > 0);
1438 grid_sibling = find_grid_child (grid, sibling);
1443 left = CHILD_LEFT (grid_sibling) - width;
1444 top = CHILD_TOP (grid_sibling);
1447 left = CHILD_LEFT (grid_sibling) + CHILD_WIDTH (grid_sibling);
1448 top = CHILD_TOP (grid_sibling);
1451 left = CHILD_LEFT (grid_sibling);
1452 top = CHILD_TOP (grid_sibling) - height;
1454 case GTK_POS_BOTTOM:
1455 left = CHILD_LEFT (grid_sibling);
1456 top = CHILD_TOP (grid_sibling) + CHILD_HEIGHT (grid_sibling);
1459 g_assert_not_reached ();
1462 grid_attach (grid, child, left, top, width, height);
1466 * gtk_grid_set_row_homogeneous:
1468 * @homogeneous: %TRUE to make rows homogeneous
1470 * Sets whether all rows of @grid will have the same height.
1473 gtk_grid_set_row_homogeneous (GtkGrid *grid,
1474 gboolean homogeneous)
1476 GtkGridPrivate *priv;
1477 g_return_if_fail (GTK_IS_GRID (grid));
1481 if (ROWS (priv)->homogeneous != homogeneous)
1483 ROWS (priv)->homogeneous = homogeneous;
1485 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1486 gtk_widget_queue_resize (GTK_WIDGET (grid));
1488 g_object_notify (G_OBJECT (grid), "row-homogeneous");
1493 * gtk_grid_get_row_homogeneous:
1496 * Returns whether all rows of @grid have the same height.
1498 * Returns: whether all rows of @grid have the same height.
1501 gtk_grid_get_row_homogeneous (GtkGrid *grid)
1503 GtkGridPrivate *priv;
1504 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1508 return ROWS (priv)->homogeneous;
1512 * gtk_grid_set_column_homogeneous:
1514 * @homogeneous: %TRUE to make columns homogeneous
1516 * Sets whether all columns of @grid will have the same width.
1519 gtk_grid_set_column_homogeneous (GtkGrid *grid,
1520 gboolean homogeneous)
1522 GtkGridPrivate *priv;
1523 g_return_if_fail (GTK_IS_GRID (grid));
1527 if (COLUMNS (priv)->homogeneous != homogeneous)
1529 COLUMNS (priv)->homogeneous = homogeneous;
1531 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1532 gtk_widget_queue_resize (GTK_WIDGET (grid));
1534 g_object_notify (G_OBJECT (grid), "column-homogeneous");
1539 * gtk_grid_get_column_homogeneous:
1542 * Returns whether all columns of @grid have the same width.
1544 * Returns: whether all columns of @grid have the same width.
1547 gtk_grid_get_column_homogeneous (GtkGrid *grid)
1549 GtkGridPrivate *priv;
1550 g_return_val_if_fail (GTK_IS_GRID (grid), FALSE);
1554 return COLUMNS (priv)->homogeneous;
1558 * gtk_grid_set_row_spacing:
1560 * @spacing: the amount of space to insert between rows
1562 * Sets the amount of space between rows of @grid.
1565 gtk_grid_set_row_spacing (GtkGrid *grid,
1568 GtkGridPrivate *priv;
1569 g_return_if_fail (GTK_IS_GRID (grid));
1570 g_return_if_fail (spacing <= G_MAXINT16);
1574 if (ROWS (priv)->spacing != spacing)
1576 ROWS (priv)->spacing = spacing;
1578 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1579 gtk_widget_queue_resize (GTK_WIDGET (grid));
1581 g_object_notify (G_OBJECT (grid), "row-spacing");
1586 * gtk_grid_get_row_spacing:
1589 * Returns the amount of space between the rows of @grid.
1591 * Returns: the row spacing of @grid
1594 gtk_grid_get_row_spacing (GtkGrid *grid)
1596 GtkGridPrivate *priv;
1597 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1601 return ROWS (priv)->spacing;
1605 * gtk_grid_set_column_spacing:
1607 * @spacing: the amount of space to insert between columns
1609 * Sets the amount of space between columns of @grid.
1612 gtk_grid_set_column_spacing (GtkGrid *grid,
1615 GtkGridPrivate *priv;
1616 g_return_if_fail (GTK_IS_GRID (grid));
1617 g_return_if_fail (spacing <= G_MAXINT16);
1621 if (COLUMNS (priv)->spacing != spacing)
1623 COLUMNS (priv)->spacing = spacing;
1625 if (gtk_widget_get_visible (GTK_WIDGET (grid)))
1626 gtk_widget_queue_resize (GTK_WIDGET (grid));
1628 g_object_notify (G_OBJECT (grid), "column-spacing");
1633 * gtk_grid_get_column_spacing:
1636 * Returns the amount of space between the columns of @grid.
1638 * Returns: the column spacing of @grid
1641 gtk_grid_get_column_spacing (GtkGrid *grid)
1643 GtkGridPrivate *priv;
1645 g_return_val_if_fail (GTK_IS_GRID (grid), 0);
1649 return COLUMNS (priv)->spacing;