1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 * SECTION:gtkalignment
27 * @Short_description: A widget which controls the alignment and size of its child
28 * @Title: GtkAlignment
30 * The #GtkAlignment widget controls the alignment and size of its child widget.
31 * It has four settings: xscale, yscale, xalign, and yalign.
33 * The scale settings are used to specify how much the child widget should
34 * expand to fill the space allocated to the #GtkAlignment.
35 * The values can range from 0 (meaning the child doesn't expand at all) to
36 * 1 (meaning the child expands to fill all of the available space).
38 * The align settings are used to place the child widget within the available
39 * area. The values range from 0 (top or left) to 1 (bottom or right).
40 * Of course, if the scale settings are both set to 1, the alignment settings
45 * Note that the desired effect can in most cases be achieved by using the
46 * #GtkWidget:halign, #GtkWidget:valign and #GtkWidget:margin properties
47 * on the child widget, so #GtkAlignment should not be used in new code.
53 #include "gtkalignment.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
59 struct _GtkAlignmentPrivate
86 static void gtk_alignment_size_allocate (GtkWidget *widget,
87 GtkAllocation *allocation);
88 static void gtk_alignment_set_property (GObject *object,
92 static void gtk_alignment_get_property (GObject *object,
97 static void gtk_alignment_get_preferred_width (GtkWidget *widget,
100 static void gtk_alignment_get_preferred_height (GtkWidget *widget,
103 static void gtk_alignment_get_preferred_width_for_height (GtkWidget *widget,
107 static void gtk_alignment_get_preferred_height_for_width (GtkWidget *widget,
112 G_DEFINE_TYPE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN)
115 gtk_alignment_class_init (GtkAlignmentClass *class)
117 GObjectClass *gobject_class;
118 GtkWidgetClass *widget_class;
120 gobject_class = (GObjectClass*) class;
121 widget_class = (GtkWidgetClass*) class;
123 gobject_class->set_property = gtk_alignment_set_property;
124 gobject_class->get_property = gtk_alignment_get_property;
126 widget_class->size_allocate = gtk_alignment_size_allocate;
127 widget_class->get_preferred_width = gtk_alignment_get_preferred_width;
128 widget_class->get_preferred_height = gtk_alignment_get_preferred_height;
129 widget_class->get_preferred_width_for_height = gtk_alignment_get_preferred_width_for_height;
130 widget_class->get_preferred_height_for_width = gtk_alignment_get_preferred_height_for_width;
132 g_object_class_install_property (gobject_class,
134 g_param_spec_float("xalign",
135 P_("Horizontal alignment"),
136 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
140 GTK_PARAM_READWRITE));
142 g_object_class_install_property (gobject_class,
144 g_param_spec_float("yalign",
145 P_("Vertical alignment"),
146 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
150 GTK_PARAM_READWRITE));
151 g_object_class_install_property (gobject_class,
153 g_param_spec_float("xscale",
154 P_("Horizontal scale"),
155 P_("If available horizontal space is bigger than needed for the child, how much of it to use for the child. 0.0 means none, 1.0 means all"),
159 GTK_PARAM_READWRITE));
160 g_object_class_install_property (gobject_class,
162 g_param_spec_float("yscale",
163 P_("Vertical scale"),
164 P_("If available vertical space is bigger than needed for the child, how much of it to use for the child. 0.0 means none, 1.0 means all"),
168 GTK_PARAM_READWRITE));
172 * GtkAlignment:top-padding:
174 * The padding to insert at the top of the widget.
178 g_object_class_install_property (gobject_class,
180 g_param_spec_uint("top-padding",
182 P_("The padding to insert at the top of the widget."),
186 GTK_PARAM_READWRITE));
189 * GtkAlignment:bottom-padding:
191 * The padding to insert at the bottom of the widget.
195 g_object_class_install_property (gobject_class,
197 g_param_spec_uint("bottom-padding",
198 P_("Bottom Padding"),
199 P_("The padding to insert at the bottom of the widget."),
203 GTK_PARAM_READWRITE));
206 * GtkAlignment:left-padding:
208 * The padding to insert at the left of the widget.
212 g_object_class_install_property (gobject_class,
214 g_param_spec_uint("left-padding",
216 P_("The padding to insert at the left of the widget."),
220 GTK_PARAM_READWRITE));
223 * GtkAlignment:right-padding:
225 * The padding to insert at the right of the widget.
229 g_object_class_install_property (gobject_class,
231 g_param_spec_uint("right-padding",
233 P_("The padding to insert at the right of the widget."),
237 GTK_PARAM_READWRITE));
239 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
243 gtk_alignment_init (GtkAlignment *alignment)
245 GtkAlignmentPrivate *priv;
247 alignment->priv = G_TYPE_INSTANCE_GET_PRIVATE (alignment,
249 GtkAlignmentPrivate);
250 priv = alignment->priv;
252 gtk_widget_set_has_window (GTK_WIDGET (alignment), FALSE);
253 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
260 /* Initialize padding with default values: */
261 priv->padding_top = 0;
262 priv->padding_bottom = 0;
263 priv->padding_left = 0;
264 priv->padding_right = 0;
269 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
271 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
273 * @xscale: the amount that the child widget expands horizontally to fill up
274 * unused space, from 0 to 1.
275 * A value of 0 indicates that the child widget should never expand.
276 * A value of 1 indicates that the child widget will expand to fill all of the
277 * space allocated for the #GtkAlignment.
278 * @yscale: the amount that the child widget expands vertically to fill up
279 * unused space, from 0 to 1. The values are similar to @xscale.
281 * Creates a new #GtkAlignment.
283 * Returns: the new #GtkAlignment.
286 gtk_alignment_new (gfloat xalign,
291 GtkAlignment *alignment;
292 GtkAlignmentPrivate *priv;
294 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
296 priv = alignment->priv;
298 priv->xalign = CLAMP (xalign, 0.0, 1.0);
299 priv->yalign = CLAMP (yalign, 0.0, 1.0);
300 priv->xscale = CLAMP (xscale, 0.0, 1.0);
301 priv->yscale = CLAMP (yscale, 0.0, 1.0);
303 return GTK_WIDGET (alignment);
307 gtk_alignment_set_property (GObject *object,
312 GtkAlignment *alignment = GTK_ALIGNMENT (object);
313 GtkAlignmentPrivate *priv = alignment->priv;
318 gtk_alignment_set (alignment,
319 g_value_get_float (value),
325 gtk_alignment_set (alignment,
327 g_value_get_float (value),
332 gtk_alignment_set (alignment,
335 g_value_get_float (value),
339 gtk_alignment_set (alignment,
343 g_value_get_float (value));
347 case PROP_TOP_PADDING:
348 gtk_alignment_set_padding (alignment,
349 g_value_get_uint (value),
350 priv->padding_bottom,
352 priv->padding_right);
354 case PROP_BOTTOM_PADDING:
355 gtk_alignment_set_padding (alignment,
357 g_value_get_uint (value),
359 priv->padding_right);
361 case PROP_LEFT_PADDING:
362 gtk_alignment_set_padding (alignment,
364 priv->padding_bottom,
365 g_value_get_uint (value),
366 priv->padding_right);
368 case PROP_RIGHT_PADDING:
369 gtk_alignment_set_padding (alignment,
371 priv->padding_bottom,
373 g_value_get_uint (value));
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383 gtk_alignment_get_property (GObject *object,
388 GtkAlignment *alignment = GTK_ALIGNMENT (object);
389 GtkAlignmentPrivate *priv = alignment->priv;
394 g_value_set_float(value, priv->xalign);
397 g_value_set_float(value, priv->yalign);
400 g_value_set_float(value, priv->xscale);
403 g_value_set_float(value, priv->yscale);
407 case PROP_TOP_PADDING:
408 g_value_set_uint (value, priv->padding_top);
410 case PROP_BOTTOM_PADDING:
411 g_value_set_uint (value, priv->padding_bottom);
413 case PROP_LEFT_PADDING:
414 g_value_set_uint (value, priv->padding_left);
416 case PROP_RIGHT_PADDING:
417 g_value_set_uint (value, priv->padding_right);
421 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428 * @alignment: a #GtkAlignment.
429 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
431 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
433 * @xscale: the amount that the child widget expands horizontally to fill up
434 * unused space, from 0 to 1.
435 * A value of 0 indicates that the child widget should never expand.
436 * A value of 1 indicates that the child widget will expand to fill all of the
437 * space allocated for the #GtkAlignment.
438 * @yscale: the amount that the child widget expands vertically to fill up
439 * unused space, from 0 to 1. The values are similar to @xscale.
441 * Sets the #GtkAlignment values.
444 gtk_alignment_set (GtkAlignment *alignment,
450 GtkAlignmentPrivate *priv;
453 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
455 priv = alignment->priv;
457 xalign = CLAMP (xalign, 0.0, 1.0);
458 yalign = CLAMP (yalign, 0.0, 1.0);
459 xscale = CLAMP (xscale, 0.0, 1.0);
460 yscale = CLAMP (yscale, 0.0, 1.0);
462 if ( (priv->xalign != xalign)
463 || (priv->yalign != yalign)
464 || (priv->xscale != xscale)
465 || (priv->yscale != yscale))
467 g_object_freeze_notify (G_OBJECT (alignment));
468 if (priv->xalign != xalign)
470 priv->xalign = xalign;
471 g_object_notify (G_OBJECT (alignment), "xalign");
473 if (priv->yalign != yalign)
475 priv->yalign = yalign;
476 g_object_notify (G_OBJECT (alignment), "yalign");
478 if (priv->xscale != xscale)
480 priv->xscale = xscale;
481 g_object_notify (G_OBJECT (alignment), "xscale");
483 if (priv->yscale != yscale)
485 priv->yscale = yscale;
486 g_object_notify (G_OBJECT (alignment), "yscale");
488 g_object_thaw_notify (G_OBJECT (alignment));
490 child = gtk_bin_get_child (GTK_BIN (alignment));
492 gtk_widget_queue_resize (child);
493 gtk_widget_queue_draw (GTK_WIDGET (alignment));
499 gtk_alignment_size_allocate (GtkWidget *widget,
500 GtkAllocation *allocation)
502 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
503 GtkAlignmentPrivate *priv = alignment->priv;
505 GtkAllocation child_allocation;
509 gint padding_horizontal, padding_vertical;
511 padding_horizontal = 0;
512 padding_vertical = 0;
514 gtk_widget_set_allocation (widget, allocation);
515 bin = GTK_BIN (widget);
517 child = gtk_bin_get_child (bin);
518 if (child && gtk_widget_get_visible (child))
520 gint child_nat_width;
521 gint child_nat_height;
522 gint child_width, child_height;
524 border_width = gtk_container_get_border_width (GTK_CONTAINER (alignment));
526 padding_horizontal = priv->padding_left + priv->padding_right;
527 padding_vertical = priv->padding_top + priv->padding_bottom;
529 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
530 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
532 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
534 gtk_widget_get_preferred_width (child, NULL, &child_nat_width);
536 child_width = MIN (width, child_nat_width);
538 gtk_widget_get_preferred_height_for_width (child, child_width, NULL, &child_nat_height);
540 child_height = MIN (height, child_nat_height);
544 gtk_widget_get_preferred_height (child, NULL, &child_nat_height);
546 child_height = MIN (height, child_nat_height);
548 gtk_widget_get_preferred_width_for_height (child, child_height, NULL, &child_nat_width);
550 child_width = MIN (width, child_nat_width);
553 if (width > child_width)
554 child_allocation.width = (child_width *
555 (1.0 - priv->xscale) +
556 width * priv->xscale);
558 child_allocation.width = width;
560 if (height > child_height)
561 child_allocation.height = (child_height *
562 (1.0 - priv->yscale) +
563 height * priv->yscale);
565 child_allocation.height = height;
567 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
568 child_allocation.x = (1.0 - priv->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
570 child_allocation.x = priv->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
572 child_allocation.y = priv->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
574 gtk_widget_size_allocate (child, &child_allocation);
580 gtk_alignment_get_preferred_size (GtkWidget *widget,
581 GtkOrientation orientation,
586 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
587 GtkAlignmentPrivate *priv = alignment->priv;
589 guint minimum, natural;
591 natural = minimum = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
593 if ((child = gtk_bin_get_child (GTK_BIN (widget))) && gtk_widget_get_visible (child))
595 gint child_min, child_nat;
597 /* Request extra space for the padding: */
598 if (orientation == GTK_ORIENTATION_HORIZONTAL)
600 minimum += (priv->padding_left + priv->padding_right);
603 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
608 gtk_widget_get_preferred_height (child, &min_height, NULL);
610 for_size -= (priv->padding_top + priv->padding_bottom);
612 if (for_size > min_height)
613 for_size = (min_height * (1.0 - priv->yscale) +
614 for_size * priv->yscale);
616 gtk_widget_get_preferred_width_for_height (child, for_size, &child_min, &child_nat);
621 minimum += (priv->padding_top + priv->padding_bottom);
624 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
629 gtk_widget_get_preferred_width (child, &min_width, NULL);
631 for_size -= (priv->padding_left + priv->padding_right);
633 if (for_size > min_width)
634 for_size = (min_width * (1.0 - priv->xscale) +
635 for_size * priv->xscale);
637 gtk_widget_get_preferred_height_for_width (child, for_size, &child_min, &child_nat);
643 minimum += child_min;
644 natural += child_nat;
648 *minimum_size = minimum;
651 *natural_size = natural;
655 gtk_alignment_get_preferred_width (GtkWidget *widget,
659 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum_size, natural_size);
663 gtk_alignment_get_preferred_height (GtkWidget *widget,
667 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum_size, natural_size);
672 gtk_alignment_get_preferred_width_for_height (GtkWidget *widget,
677 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, for_size, minimum_size, natural_size);
681 gtk_alignment_get_preferred_height_for_width (GtkWidget *widget,
686 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, for_size, minimum_size, natural_size);
690 * gtk_alignment_set_padding:
691 * @alignment: a #GtkAlignment
692 * @padding_top: the padding at the top of the widget
693 * @padding_bottom: the padding at the bottom of the widget
694 * @padding_left: the padding at the left of the widget
695 * @padding_right: the padding at the right of the widget.
697 * Sets the padding on the different sides of the widget.
698 * The padding adds blank space to the sides of the widget. For instance,
699 * this can be used to indent the child widget towards the right by adding
700 * padding on the left.
705 gtk_alignment_set_padding (GtkAlignment *alignment,
707 guint padding_bottom,
711 GtkAlignmentPrivate *priv;
714 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
716 priv = alignment->priv;
718 g_object_freeze_notify (G_OBJECT (alignment));
720 if (priv->padding_top != padding_top)
722 priv->padding_top = padding_top;
723 g_object_notify (G_OBJECT (alignment), "top-padding");
725 if (priv->padding_bottom != padding_bottom)
727 priv->padding_bottom = padding_bottom;
728 g_object_notify (G_OBJECT (alignment), "bottom-padding");
730 if (priv->padding_left != padding_left)
732 priv->padding_left = padding_left;
733 g_object_notify (G_OBJECT (alignment), "left-padding");
735 if (priv->padding_right != padding_right)
737 priv->padding_right = padding_right;
738 g_object_notify (G_OBJECT (alignment), "right-padding");
741 g_object_thaw_notify (G_OBJECT (alignment));
743 /* Make sure that the widget and children are redrawn with the new setting: */
744 child = gtk_bin_get_child (GTK_BIN (alignment));
746 gtk_widget_queue_resize (child);
748 gtk_widget_queue_draw (GTK_WIDGET (alignment));
752 * gtk_alignment_get_padding:
753 * @alignment: a #GtkAlignment
754 * @padding_top: (out) (allow-none): location to store the padding for
755 * the top of the widget, or %NULL
756 * @padding_bottom: (out) (allow-none): location to store the padding
757 * for the bottom of the widget, or %NULL
758 * @padding_left: (out) (allow-none): location to store the padding
759 * for the left of the widget, or %NULL
760 * @padding_right: (out) (allow-none): location to store the padding
761 * for the right of the widget, or %NULL
763 * Gets the padding on the different sides of the widget.
764 * See gtk_alignment_set_padding ().
769 gtk_alignment_get_padding (GtkAlignment *alignment,
771 guint *padding_bottom,
773 guint *padding_right)
775 GtkAlignmentPrivate *priv;
777 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
779 priv = alignment->priv;
782 *padding_top = priv->padding_top;
784 *padding_bottom = priv->padding_bottom;
786 *padding_left = priv->padding_left;
788 *padding_right = priv->padding_right;