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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 * SECTION:gtkalignment
29 * @Short_description: A widget which controls the alignment and size of its child
30 * @Title: GtkAlignment
32 * The #GtkAlignment widget controls the alignment and size of its child widget.
33 * It has four settings: xscale, yscale, xalign, and yalign.
35 * The scale settings are used to specify how much the child widget should
36 * expand to fill the space allocated to the #GtkAlignment.
37 * The values can range from 0 (meaning the child doesn't expand at all) to
38 * 1 (meaning the child expands to fill all of the available space).
40 * The align settings are used to place the child widget within the available
41 * area. The values range from 0 (top or left) to 1 (bottom or right).
42 * Of course, if the scale settings are both set to 1, the alignment settings
47 * Note that the desired effect can in most cases be achieved by using the
48 * #GtkWidget:halign, #GtkWidget:valign and #GtkWidget:margin properties
49 * on the child widget, so #GtkAlignment should not be used in new code.
55 #include "gtkalignment.h"
56 #include "gtksizerequest.h"
57 #include "gtkprivate.h"
61 struct _GtkAlignmentPrivate
88 static void gtk_alignment_size_allocate (GtkWidget *widget,
89 GtkAllocation *allocation);
90 static void gtk_alignment_set_property (GObject *object,
94 static void gtk_alignment_get_property (GObject *object,
99 static void gtk_alignment_get_preferred_width (GtkWidget *widget,
102 static void gtk_alignment_get_preferred_height (GtkWidget *widget,
105 static void gtk_alignment_get_preferred_width_for_height (GtkWidget *widget,
109 static void gtk_alignment_get_preferred_height_for_width (GtkWidget *widget,
114 G_DEFINE_TYPE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN)
117 gtk_alignment_class_init (GtkAlignmentClass *class)
119 GObjectClass *gobject_class;
120 GtkWidgetClass *widget_class;
122 gobject_class = (GObjectClass*) class;
123 widget_class = (GtkWidgetClass*) class;
125 gobject_class->set_property = gtk_alignment_set_property;
126 gobject_class->get_property = gtk_alignment_get_property;
128 widget_class->size_allocate = gtk_alignment_size_allocate;
129 widget_class->get_preferred_width = gtk_alignment_get_preferred_width;
130 widget_class->get_preferred_height = gtk_alignment_get_preferred_height;
131 widget_class->get_preferred_width_for_height = gtk_alignment_get_preferred_width_for_height;
132 widget_class->get_preferred_height_for_width = gtk_alignment_get_preferred_height_for_width;
134 g_object_class_install_property (gobject_class,
136 g_param_spec_float("xalign",
137 P_("Horizontal alignment"),
138 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
142 GTK_PARAM_READWRITE));
144 g_object_class_install_property (gobject_class,
146 g_param_spec_float("yalign",
147 P_("Vertical alignment"),
148 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
152 GTK_PARAM_READWRITE));
153 g_object_class_install_property (gobject_class,
155 g_param_spec_float("xscale",
156 P_("Horizontal scale"),
157 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"),
161 GTK_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class,
164 g_param_spec_float("yscale",
165 P_("Vertical scale"),
166 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"),
170 GTK_PARAM_READWRITE));
174 * GtkAlignment:top-padding:
176 * The padding to insert at the top of the widget.
180 g_object_class_install_property (gobject_class,
182 g_param_spec_uint("top-padding",
184 P_("The padding to insert at the top of the widget."),
188 GTK_PARAM_READWRITE));
191 * GtkAlignment:bottom-padding:
193 * The padding to insert at the bottom of the widget.
197 g_object_class_install_property (gobject_class,
199 g_param_spec_uint("bottom-padding",
200 P_("Bottom Padding"),
201 P_("The padding to insert at the bottom of the widget."),
205 GTK_PARAM_READWRITE));
208 * GtkAlignment:left-padding:
210 * The padding to insert at the left of the widget.
214 g_object_class_install_property (gobject_class,
216 g_param_spec_uint("left-padding",
218 P_("The padding to insert at the left of the widget."),
222 GTK_PARAM_READWRITE));
225 * GtkAlignment:right-padding:
227 * The padding to insert at the right of the widget.
231 g_object_class_install_property (gobject_class,
233 g_param_spec_uint("right-padding",
235 P_("The padding to insert at the right of the widget."),
239 GTK_PARAM_READWRITE));
241 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
245 gtk_alignment_init (GtkAlignment *alignment)
247 GtkAlignmentPrivate *priv;
249 alignment->priv = G_TYPE_INSTANCE_GET_PRIVATE (alignment,
251 GtkAlignmentPrivate);
252 priv = alignment->priv;
254 gtk_widget_set_has_window (GTK_WIDGET (alignment), FALSE);
255 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
262 /* Initialize padding with default values: */
263 priv->padding_top = 0;
264 priv->padding_bottom = 0;
265 priv->padding_left = 0;
266 priv->padding_right = 0;
271 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
273 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
275 * @xscale: the amount that the child widget expands horizontally to fill up
276 * unused space, from 0 to 1.
277 * A value of 0 indicates that the child widget should never expand.
278 * A value of 1 indicates that the child widget will expand to fill all of the
279 * space allocated for the #GtkAlignment.
280 * @yscale: the amount that the child widget expands vertically to fill up
281 * unused space, from 0 to 1. The values are similar to @xscale.
283 * Creates a new #GtkAlignment.
285 * Returns: the new #GtkAlignment.
288 gtk_alignment_new (gfloat xalign,
293 GtkAlignment *alignment;
294 GtkAlignmentPrivate *priv;
296 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
298 priv = alignment->priv;
300 priv->xalign = CLAMP (xalign, 0.0, 1.0);
301 priv->yalign = CLAMP (yalign, 0.0, 1.0);
302 priv->xscale = CLAMP (xscale, 0.0, 1.0);
303 priv->yscale = CLAMP (yscale, 0.0, 1.0);
305 return GTK_WIDGET (alignment);
309 gtk_alignment_set_property (GObject *object,
314 GtkAlignment *alignment = GTK_ALIGNMENT (object);
315 GtkAlignmentPrivate *priv = alignment->priv;
320 gtk_alignment_set (alignment,
321 g_value_get_float (value),
327 gtk_alignment_set (alignment,
329 g_value_get_float (value),
334 gtk_alignment_set (alignment,
337 g_value_get_float (value),
341 gtk_alignment_set (alignment,
345 g_value_get_float (value));
349 case PROP_TOP_PADDING:
350 gtk_alignment_set_padding (alignment,
351 g_value_get_uint (value),
352 priv->padding_bottom,
354 priv->padding_right);
356 case PROP_BOTTOM_PADDING:
357 gtk_alignment_set_padding (alignment,
359 g_value_get_uint (value),
361 priv->padding_right);
363 case PROP_LEFT_PADDING:
364 gtk_alignment_set_padding (alignment,
366 priv->padding_bottom,
367 g_value_get_uint (value),
368 priv->padding_right);
370 case PROP_RIGHT_PADDING:
371 gtk_alignment_set_padding (alignment,
373 priv->padding_bottom,
375 g_value_get_uint (value));
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 gtk_alignment_get_property (GObject *object,
390 GtkAlignment *alignment = GTK_ALIGNMENT (object);
391 GtkAlignmentPrivate *priv = alignment->priv;
396 g_value_set_float(value, priv->xalign);
399 g_value_set_float(value, priv->yalign);
402 g_value_set_float(value, priv->xscale);
405 g_value_set_float(value, priv->yscale);
409 case PROP_TOP_PADDING:
410 g_value_set_uint (value, priv->padding_top);
412 case PROP_BOTTOM_PADDING:
413 g_value_set_uint (value, priv->padding_bottom);
415 case PROP_LEFT_PADDING:
416 g_value_set_uint (value, priv->padding_left);
418 case PROP_RIGHT_PADDING:
419 g_value_set_uint (value, priv->padding_right);
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430 * @alignment: a #GtkAlignment.
431 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
433 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
435 * @xscale: the amount that the child widget expands horizontally to fill up
436 * unused space, from 0 to 1.
437 * A value of 0 indicates that the child widget should never expand.
438 * A value of 1 indicates that the child widget will expand to fill all of the
439 * space allocated for the #GtkAlignment.
440 * @yscale: the amount that the child widget expands vertically to fill up
441 * unused space, from 0 to 1. The values are similar to @xscale.
443 * Sets the #GtkAlignment values.
446 gtk_alignment_set (GtkAlignment *alignment,
452 GtkAlignmentPrivate *priv;
455 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
457 priv = alignment->priv;
459 xalign = CLAMP (xalign, 0.0, 1.0);
460 yalign = CLAMP (yalign, 0.0, 1.0);
461 xscale = CLAMP (xscale, 0.0, 1.0);
462 yscale = CLAMP (yscale, 0.0, 1.0);
464 if ( (priv->xalign != xalign)
465 || (priv->yalign != yalign)
466 || (priv->xscale != xscale)
467 || (priv->yscale != yscale))
469 g_object_freeze_notify (G_OBJECT (alignment));
470 if (priv->xalign != xalign)
472 priv->xalign = xalign;
473 g_object_notify (G_OBJECT (alignment), "xalign");
475 if (priv->yalign != yalign)
477 priv->yalign = yalign;
478 g_object_notify (G_OBJECT (alignment), "yalign");
480 if (priv->xscale != xscale)
482 priv->xscale = xscale;
483 g_object_notify (G_OBJECT (alignment), "xscale");
485 if (priv->yscale != yscale)
487 priv->yscale = yscale;
488 g_object_notify (G_OBJECT (alignment), "yscale");
490 g_object_thaw_notify (G_OBJECT (alignment));
492 child = gtk_bin_get_child (GTK_BIN (alignment));
494 gtk_widget_queue_resize (child);
495 gtk_widget_queue_draw (GTK_WIDGET (alignment));
501 gtk_alignment_size_allocate (GtkWidget *widget,
502 GtkAllocation *allocation)
504 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
505 GtkAlignmentPrivate *priv = alignment->priv;
507 GtkAllocation child_allocation;
511 gint padding_horizontal, padding_vertical;
513 padding_horizontal = 0;
514 padding_vertical = 0;
516 gtk_widget_set_allocation (widget, allocation);
517 bin = GTK_BIN (widget);
519 child = gtk_bin_get_child (bin);
520 if (child && gtk_widget_get_visible (child))
522 gint child_nat_width;
523 gint child_nat_height;
524 gint child_width, child_height;
526 border_width = gtk_container_get_border_width (GTK_CONTAINER (alignment));
528 padding_horizontal = priv->padding_left + priv->padding_right;
529 padding_vertical = priv->padding_top + priv->padding_bottom;
531 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
532 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
534 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
536 gtk_widget_get_preferred_width (child, NULL, &child_nat_width);
538 child_width = MIN (width, child_nat_width);
540 gtk_widget_get_preferred_height_for_width (child, child_width, NULL, &child_nat_height);
542 child_height = MIN (height, child_nat_height);
546 gtk_widget_get_preferred_height (child, NULL, &child_nat_height);
548 child_height = MIN (height, child_nat_height);
550 gtk_widget_get_preferred_width_for_height (child, child_height, NULL, &child_nat_width);
552 child_width = MIN (width, child_nat_width);
555 if (width > child_width)
556 child_allocation.width = (child_width *
557 (1.0 - priv->xscale) +
558 width * priv->xscale);
560 child_allocation.width = width;
562 if (height > child_height)
563 child_allocation.height = (child_height *
564 (1.0 - priv->yscale) +
565 height * priv->yscale);
567 child_allocation.height = height;
569 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
570 child_allocation.x = (1.0 - priv->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
572 child_allocation.x = priv->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
574 child_allocation.y = priv->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
576 gtk_widget_size_allocate (child, &child_allocation);
582 gtk_alignment_get_preferred_size (GtkWidget *widget,
583 GtkOrientation orientation,
588 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
589 GtkAlignmentPrivate *priv = alignment->priv;
591 guint minimum, natural;
593 natural = minimum = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
595 if ((child = gtk_bin_get_child (GTK_BIN (widget))) && gtk_widget_get_visible (child))
597 gint child_min, child_nat;
599 /* Request extra space for the padding: */
600 if (orientation == GTK_ORIENTATION_HORIZONTAL)
602 minimum += (priv->padding_left + priv->padding_right);
605 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
610 gtk_widget_get_preferred_height (child, &min_height, NULL);
612 for_size -= (priv->padding_top + priv->padding_bottom);
614 if (for_size > min_height)
615 for_size = (min_height * (1.0 - priv->yscale) +
616 for_size * priv->yscale);
618 gtk_widget_get_preferred_width_for_height (child, for_size, &child_min, &child_nat);
623 minimum += (priv->padding_top + priv->padding_bottom);
626 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
631 gtk_widget_get_preferred_width (child, &min_width, NULL);
633 for_size -= (priv->padding_left + priv->padding_right);
635 if (for_size > min_width)
636 for_size = (min_width * (1.0 - priv->xscale) +
637 for_size * priv->xscale);
639 gtk_widget_get_preferred_height_for_width (child, for_size, &child_min, &child_nat);
645 minimum += child_min;
646 natural += child_nat;
650 *minimum_size = minimum;
653 *natural_size = natural;
657 gtk_alignment_get_preferred_width (GtkWidget *widget,
661 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum_size, natural_size);
665 gtk_alignment_get_preferred_height (GtkWidget *widget,
669 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum_size, natural_size);
674 gtk_alignment_get_preferred_width_for_height (GtkWidget *widget,
679 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, for_size, minimum_size, natural_size);
683 gtk_alignment_get_preferred_height_for_width (GtkWidget *widget,
688 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, for_size, minimum_size, natural_size);
692 * gtk_alignment_set_padding:
693 * @alignment: a #GtkAlignment
694 * @padding_top: the padding at the top of the widget
695 * @padding_bottom: the padding at the bottom of the widget
696 * @padding_left: the padding at the left of the widget
697 * @padding_right: the padding at the right of the widget.
699 * Sets the padding on the different sides of the widget.
700 * The padding adds blank space to the sides of the widget. For instance,
701 * this can be used to indent the child widget towards the right by adding
702 * padding on the left.
707 gtk_alignment_set_padding (GtkAlignment *alignment,
709 guint padding_bottom,
713 GtkAlignmentPrivate *priv;
716 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
718 priv = alignment->priv;
720 g_object_freeze_notify (G_OBJECT (alignment));
722 if (priv->padding_top != padding_top)
724 priv->padding_top = padding_top;
725 g_object_notify (G_OBJECT (alignment), "top-padding");
727 if (priv->padding_bottom != padding_bottom)
729 priv->padding_bottom = padding_bottom;
730 g_object_notify (G_OBJECT (alignment), "bottom-padding");
732 if (priv->padding_left != padding_left)
734 priv->padding_left = padding_left;
735 g_object_notify (G_OBJECT (alignment), "left-padding");
737 if (priv->padding_right != padding_right)
739 priv->padding_right = padding_right;
740 g_object_notify (G_OBJECT (alignment), "right-padding");
743 g_object_thaw_notify (G_OBJECT (alignment));
745 /* Make sure that the widget and children are redrawn with the new setting: */
746 child = gtk_bin_get_child (GTK_BIN (alignment));
748 gtk_widget_queue_resize (child);
750 gtk_widget_queue_draw (GTK_WIDGET (alignment));
754 * gtk_alignment_get_padding:
755 * @alignment: a #GtkAlignment
756 * @padding_top: (out) (allow-none): location to store the padding for
757 * the top of the widget, or %NULL
758 * @padding_bottom: (out) (allow-none): location to store the padding
759 * for the bottom of the widget, or %NULL
760 * @padding_left: (out) (allow-none): location to store the padding
761 * for the left of the widget, or %NULL
762 * @padding_right: (out) (allow-none): location to store the padding
763 * for the right of the widget, or %NULL
765 * Gets the padding on the different sides of the widget.
766 * See gtk_alignment_set_padding ().
771 gtk_alignment_get_padding (GtkAlignment *alignment,
773 guint *padding_bottom,
775 guint *padding_right)
777 GtkAlignmentPrivate *priv;
779 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
781 priv = alignment->priv;
784 *padding_top = priv->padding_top;
786 *padding_bottom = priv->padding_bottom;
788 *padding_left = priv->padding_left;
790 *padding_right = priv->padding_right;