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,
106 G_DEFINE_TYPE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN)
109 gtk_alignment_class_init (GtkAlignmentClass *class)
111 GObjectClass *gobject_class;
112 GtkWidgetClass *widget_class;
114 gobject_class = (GObjectClass*) class;
115 widget_class = (GtkWidgetClass*) class;
117 gobject_class->set_property = gtk_alignment_set_property;
118 gobject_class->get_property = gtk_alignment_get_property;
120 widget_class->size_allocate = gtk_alignment_size_allocate;
121 widget_class->get_preferred_width = gtk_alignment_get_preferred_width;
122 widget_class->get_preferred_height = gtk_alignment_get_preferred_height;
124 g_object_class_install_property (gobject_class,
126 g_param_spec_float("xalign",
127 P_("Horizontal alignment"),
128 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
132 GTK_PARAM_READWRITE));
134 g_object_class_install_property (gobject_class,
136 g_param_spec_float("yalign",
137 P_("Vertical alignment"),
138 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
142 GTK_PARAM_READWRITE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_float("xscale",
146 P_("Horizontal scale"),
147 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"),
151 GTK_PARAM_READWRITE));
152 g_object_class_install_property (gobject_class,
154 g_param_spec_float("yscale",
155 P_("Vertical scale"),
156 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"),
160 GTK_PARAM_READWRITE));
164 * GtkAlignment:top-padding:
166 * The padding to insert at the top of the widget.
170 g_object_class_install_property (gobject_class,
172 g_param_spec_uint("top-padding",
174 P_("The padding to insert at the top of the widget."),
178 GTK_PARAM_READWRITE));
181 * GtkAlignment:bottom-padding:
183 * The padding to insert at the bottom of the widget.
187 g_object_class_install_property (gobject_class,
189 g_param_spec_uint("bottom-padding",
190 P_("Bottom Padding"),
191 P_("The padding to insert at the bottom of the widget."),
195 GTK_PARAM_READWRITE));
198 * GtkAlignment:left-padding:
200 * The padding to insert at the left of the widget.
204 g_object_class_install_property (gobject_class,
206 g_param_spec_uint("left-padding",
208 P_("The padding to insert at the left of the widget."),
212 GTK_PARAM_READWRITE));
215 * GtkAlignment:right-padding:
217 * The padding to insert at the right of the widget.
221 g_object_class_install_property (gobject_class,
223 g_param_spec_uint("right-padding",
225 P_("The padding to insert at the right of the widget."),
229 GTK_PARAM_READWRITE));
231 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
235 gtk_alignment_init (GtkAlignment *alignment)
237 GtkAlignmentPrivate *priv;
239 alignment->priv = G_TYPE_INSTANCE_GET_PRIVATE (alignment,
241 GtkAlignmentPrivate);
242 priv = alignment->priv;
244 gtk_widget_set_has_window (GTK_WIDGET (alignment), FALSE);
245 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
252 /* Initialize padding with default values: */
253 priv->padding_top = 0;
254 priv->padding_bottom = 0;
255 priv->padding_left = 0;
256 priv->padding_right = 0;
261 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
263 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
265 * @xscale: the amount that the child widget expands horizontally to fill up
266 * unused space, from 0 to 1.
267 * A value of 0 indicates that the child widget should never expand.
268 * A value of 1 indicates that the child widget will expand to fill all of the
269 * space allocated for the #GtkAlignment.
270 * @yscale: the amount that the child widget expands vertically to fill up
271 * unused space, from 0 to 1. The values are similar to @xscale.
273 * Creates a new #GtkAlignment.
275 * Returns: the new #GtkAlignment.
278 gtk_alignment_new (gfloat xalign,
283 GtkAlignment *alignment;
284 GtkAlignmentPrivate *priv;
286 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
288 priv = alignment->priv;
290 priv->xalign = CLAMP (xalign, 0.0, 1.0);
291 priv->yalign = CLAMP (yalign, 0.0, 1.0);
292 priv->xscale = CLAMP (xscale, 0.0, 1.0);
293 priv->yscale = CLAMP (yscale, 0.0, 1.0);
295 return GTK_WIDGET (alignment);
299 gtk_alignment_set_property (GObject *object,
304 GtkAlignment *alignment = GTK_ALIGNMENT (object);
305 GtkAlignmentPrivate *priv = alignment->priv;
310 gtk_alignment_set (alignment,
311 g_value_get_float (value),
317 gtk_alignment_set (alignment,
319 g_value_get_float (value),
324 gtk_alignment_set (alignment,
327 g_value_get_float (value),
331 gtk_alignment_set (alignment,
335 g_value_get_float (value));
339 case PROP_TOP_PADDING:
340 gtk_alignment_set_padding (alignment,
341 g_value_get_uint (value),
342 priv->padding_bottom,
344 priv->padding_right);
346 case PROP_BOTTOM_PADDING:
347 gtk_alignment_set_padding (alignment,
349 g_value_get_uint (value),
351 priv->padding_right);
353 case PROP_LEFT_PADDING:
354 gtk_alignment_set_padding (alignment,
356 priv->padding_bottom,
357 g_value_get_uint (value),
358 priv->padding_right);
360 case PROP_RIGHT_PADDING:
361 gtk_alignment_set_padding (alignment,
363 priv->padding_bottom,
365 g_value_get_uint (value));
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 gtk_alignment_get_property (GObject *object,
380 GtkAlignment *alignment = GTK_ALIGNMENT (object);
381 GtkAlignmentPrivate *priv = alignment->priv;
386 g_value_set_float(value, priv->xalign);
389 g_value_set_float(value, priv->yalign);
392 g_value_set_float(value, priv->xscale);
395 g_value_set_float(value, priv->yscale);
399 case PROP_TOP_PADDING:
400 g_value_set_uint (value, priv->padding_top);
402 case PROP_BOTTOM_PADDING:
403 g_value_set_uint (value, priv->padding_bottom);
405 case PROP_LEFT_PADDING:
406 g_value_set_uint (value, priv->padding_left);
408 case PROP_RIGHT_PADDING:
409 g_value_set_uint (value, priv->padding_right);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
420 * @alignment: a #GtkAlignment.
421 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
423 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
425 * @xscale: the amount that the child widget expands horizontally to fill up
426 * unused space, from 0 to 1.
427 * A value of 0 indicates that the child widget should never expand.
428 * A value of 1 indicates that the child widget will expand to fill all of the
429 * space allocated for the #GtkAlignment.
430 * @yscale: the amount that the child widget expands vertically to fill up
431 * unused space, from 0 to 1. The values are similar to @xscale.
433 * Sets the #GtkAlignment values.
436 gtk_alignment_set (GtkAlignment *alignment,
442 GtkAlignmentPrivate *priv;
445 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
447 priv = alignment->priv;
449 xalign = CLAMP (xalign, 0.0, 1.0);
450 yalign = CLAMP (yalign, 0.0, 1.0);
451 xscale = CLAMP (xscale, 0.0, 1.0);
452 yscale = CLAMP (yscale, 0.0, 1.0);
454 if ( (priv->xalign != xalign)
455 || (priv->yalign != yalign)
456 || (priv->xscale != xscale)
457 || (priv->yscale != yscale))
459 g_object_freeze_notify (G_OBJECT (alignment));
460 if (priv->xalign != xalign)
462 priv->xalign = xalign;
463 g_object_notify (G_OBJECT (alignment), "xalign");
465 if (priv->yalign != yalign)
467 priv->yalign = yalign;
468 g_object_notify (G_OBJECT (alignment), "yalign");
470 if (priv->xscale != xscale)
472 priv->xscale = xscale;
473 g_object_notify (G_OBJECT (alignment), "xscale");
475 if (priv->yscale != yscale)
477 priv->yscale = yscale;
478 g_object_notify (G_OBJECT (alignment), "yscale");
480 g_object_thaw_notify (G_OBJECT (alignment));
482 child = gtk_bin_get_child (GTK_BIN (alignment));
484 gtk_widget_queue_resize (child);
485 gtk_widget_queue_draw (GTK_WIDGET (alignment));
491 gtk_alignment_size_allocate (GtkWidget *widget,
492 GtkAllocation *allocation)
494 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
495 GtkAlignmentPrivate *priv = alignment->priv;
497 GtkAllocation child_allocation;
501 gint padding_horizontal, padding_vertical;
503 padding_horizontal = 0;
504 padding_vertical = 0;
506 gtk_widget_set_allocation (widget, allocation);
507 bin = GTK_BIN (widget);
509 child = gtk_bin_get_child (bin);
510 if (child && gtk_widget_get_visible (child))
512 gint child_nat_width;
513 gint child_nat_height;
514 gint child_width, child_height;
516 border_width = gtk_container_get_border_width (GTK_CONTAINER (alignment));
518 padding_horizontal = priv->padding_left + priv->padding_right;
519 padding_vertical = priv->padding_top + priv->padding_bottom;
521 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
522 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
524 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
526 gtk_widget_get_preferred_width (child, NULL, &child_nat_width);
528 child_width = MIN (width, child_nat_width);
530 gtk_widget_get_preferred_height_for_width (child, child_width, NULL, &child_nat_height);
532 child_height = MIN (height, child_nat_height);
536 gtk_widget_get_preferred_height (child, NULL, &child_nat_height);
538 child_height = MIN (height, child_nat_height);
540 gtk_widget_get_preferred_width_for_height (child, child_height, NULL, &child_nat_width);
542 child_width = MIN (width, child_nat_width);
545 if (width > child_width)
546 child_allocation.width = (child_width *
547 (1.0 - priv->xscale) +
548 width * priv->xscale);
550 child_allocation.width = width;
552 if (height > child_height)
553 child_allocation.height = (child_height *
554 (1.0 - priv->yscale) +
555 height * priv->yscale);
557 child_allocation.height = height;
559 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
560 child_allocation.x = (1.0 - priv->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
562 child_allocation.x = priv->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
564 child_allocation.y = priv->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
566 gtk_widget_size_allocate (child, &child_allocation);
572 gtk_alignment_get_preferred_size (GtkWidget *widget,
573 GtkOrientation orientation,
577 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
578 GtkAlignmentPrivate *priv = alignment->priv;
580 guint minimum, natural;
582 natural = minimum = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
584 if ((child = gtk_bin_get_child (GTK_BIN (widget))) && gtk_widget_get_visible (child))
586 gint child_min, child_nat;
588 /* Request extra space for the padding: */
589 if (orientation == GTK_ORIENTATION_HORIZONTAL)
591 minimum += (priv->padding_left + priv->padding_right);
592 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
596 minimum += (priv->padding_top + priv->padding_bottom);
597 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
602 minimum += child_min;
603 natural += child_nat;
607 *minimum_size = minimum;
610 *natural_size = natural;
614 gtk_alignment_get_preferred_width (GtkWidget *widget,
618 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
622 gtk_alignment_get_preferred_height (GtkWidget *widget,
626 gtk_alignment_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
630 * gtk_alignment_set_padding:
631 * @alignment: a #GtkAlignment
632 * @padding_top: the padding at the top of the widget
633 * @padding_bottom: the padding at the bottom of the widget
634 * @padding_left: the padding at the left of the widget
635 * @padding_right: the padding at the right of the widget.
637 * Sets the padding on the different sides of the widget.
638 * The padding adds blank space to the sides of the widget. For instance,
639 * this can be used to indent the child widget towards the right by adding
640 * padding on the left.
645 gtk_alignment_set_padding (GtkAlignment *alignment,
647 guint padding_bottom,
651 GtkAlignmentPrivate *priv;
654 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
656 priv = alignment->priv;
658 g_object_freeze_notify (G_OBJECT (alignment));
660 if (priv->padding_top != padding_top)
662 priv->padding_top = padding_top;
663 g_object_notify (G_OBJECT (alignment), "top-padding");
665 if (priv->padding_bottom != padding_bottom)
667 priv->padding_bottom = padding_bottom;
668 g_object_notify (G_OBJECT (alignment), "bottom-padding");
670 if (priv->padding_left != padding_left)
672 priv->padding_left = padding_left;
673 g_object_notify (G_OBJECT (alignment), "left-padding");
675 if (priv->padding_right != padding_right)
677 priv->padding_right = padding_right;
678 g_object_notify (G_OBJECT (alignment), "right-padding");
681 g_object_thaw_notify (G_OBJECT (alignment));
683 /* Make sure that the widget and children are redrawn with the new setting: */
684 child = gtk_bin_get_child (GTK_BIN (alignment));
686 gtk_widget_queue_resize (child);
688 gtk_widget_queue_draw (GTK_WIDGET (alignment));
692 * gtk_alignment_get_padding:
693 * @alignment: a #GtkAlignment
694 * @padding_top: (allow-none): location to store the padding for the top of the widget, or %NULL
695 * @padding_bottom: (allow-none): location to store the padding for the bottom of the widget, or %NULL
696 * @padding_left: (allow-none): location to store the padding for the left of the widget, or %NULL
697 * @padding_right: (allow-none): location to store the padding for the right of the widget, or %NULL
699 * Gets the padding on the different sides of the widget.
700 * See gtk_alignment_set_padding ().
705 gtk_alignment_get_padding (GtkAlignment *alignment,
707 guint *padding_bottom,
709 guint *padding_right)
711 GtkAlignmentPrivate *priv;
713 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
715 priv = alignment->priv;
718 *padding_top = priv->padding_top;
720 *padding_bottom = priv->padding_bottom;
722 *padding_left = priv->padding_left;
724 *padding_right = priv->padding_right;