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 #include "gtkalignment.h"
48 #include "gtkextendedlayout.h"
49 #include "gtkprivate.h"
67 #define GTK_ALIGNMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ALIGNMENT, GtkAlignmentPrivate))
69 struct _GtkAlignmentPrivate
77 static void gtk_alignment_size_allocate (GtkWidget *widget,
78 GtkAllocation *allocation);
79 static void gtk_alignment_set_property (GObject *object,
83 static void gtk_alignment_get_property (GObject *object,
88 static void gtk_alignment_extended_layout_init (GtkExtendedLayoutIface *iface);
89 static void gtk_alignment_get_desired_width (GtkExtendedLayout *layout,
92 static void gtk_alignment_get_desired_height (GtkExtendedLayout *layout,
96 G_DEFINE_TYPE_WITH_CODE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN,
97 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
98 gtk_alignment_extended_layout_init))
101 gtk_alignment_class_init (GtkAlignmentClass *class)
103 GObjectClass *gobject_class;
104 GtkWidgetClass *widget_class;
106 gobject_class = (GObjectClass*) class;
107 widget_class = (GtkWidgetClass*) class;
109 gobject_class->set_property = gtk_alignment_set_property;
110 gobject_class->get_property = gtk_alignment_get_property;
112 widget_class->size_allocate = gtk_alignment_size_allocate;
114 g_object_class_install_property (gobject_class,
116 g_param_spec_float("xalign",
117 P_("Horizontal alignment"),
118 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
122 GTK_PARAM_READWRITE));
124 g_object_class_install_property (gobject_class,
126 g_param_spec_float("yalign",
127 P_("Vertical alignment"),
128 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
132 GTK_PARAM_READWRITE));
133 g_object_class_install_property (gobject_class,
135 g_param_spec_float("xscale",
136 P_("Horizontal scale"),
137 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"),
141 GTK_PARAM_READWRITE));
142 g_object_class_install_property (gobject_class,
144 g_param_spec_float("yscale",
145 P_("Vertical scale"),
146 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"),
150 GTK_PARAM_READWRITE));
154 * GtkAlignment:top-padding:
156 * The padding to insert at the top of the widget.
160 g_object_class_install_property (gobject_class,
162 g_param_spec_uint("top-padding",
164 P_("The padding to insert at the top of the widget."),
168 GTK_PARAM_READWRITE));
171 * GtkAlignment:bottom-padding:
173 * The padding to insert at the bottom of the widget.
177 g_object_class_install_property (gobject_class,
179 g_param_spec_uint("bottom-padding",
180 P_("Bottom Padding"),
181 P_("The padding to insert at the bottom of the widget."),
185 GTK_PARAM_READWRITE));
188 * GtkAlignment:left-padding:
190 * The padding to insert at the left of the widget.
194 g_object_class_install_property (gobject_class,
196 g_param_spec_uint("left-padding",
198 P_("The padding to insert at the left of the widget."),
202 GTK_PARAM_READWRITE));
205 * GtkAlignment:right-padding:
207 * The padding to insert at the right of the widget.
211 g_object_class_install_property (gobject_class,
213 g_param_spec_uint("right-padding",
215 P_("The padding to insert at the right of the widget."),
219 GTK_PARAM_READWRITE));
221 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
225 gtk_alignment_init (GtkAlignment *alignment)
227 GtkAlignmentPrivate *priv;
229 gtk_widget_set_has_window (GTK_WIDGET (alignment), FALSE);
230 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
232 alignment->xalign = 0.5;
233 alignment->yalign = 0.5;
234 alignment->xscale = 1.0;
235 alignment->yscale = 1.0;
237 /* Initialize padding with default values: */
238 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
239 priv->padding_top = 0;
240 priv->padding_bottom = 0;
241 priv->padding_left = 0;
242 priv->padding_right = 0;
247 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
249 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
251 * @xscale: the amount that the child widget expands horizontally to fill up
252 * unused space, from 0 to 1.
253 * A value of 0 indicates that the child widget should never expand.
254 * A value of 1 indicates that the child widget will expand to fill all of the
255 * space allocated for the #GtkAlignment.
256 * @yscale: the amount that the child widget expands vertically to fill up
257 * unused space, from 0 to 1. The values are similar to @xscale.
259 * Creates a new #GtkAlignment.
261 * Returns: the new #GtkAlignment.
264 gtk_alignment_new (gfloat xalign,
269 GtkAlignment *alignment;
271 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
273 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
274 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
275 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
276 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
278 return GTK_WIDGET (alignment);
282 gtk_alignment_set_property (GObject *object,
287 GtkAlignment *alignment;
288 GtkAlignmentPrivate *priv;
290 alignment = GTK_ALIGNMENT (object);
291 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
296 gtk_alignment_set (alignment,
297 g_value_get_float (value),
303 gtk_alignment_set (alignment,
305 g_value_get_float (value),
310 gtk_alignment_set (alignment,
313 g_value_get_float (value),
317 gtk_alignment_set (alignment,
321 g_value_get_float (value));
325 case PROP_TOP_PADDING:
326 gtk_alignment_set_padding (alignment,
327 g_value_get_uint (value),
328 priv->padding_bottom,
330 priv->padding_right);
332 case PROP_BOTTOM_PADDING:
333 gtk_alignment_set_padding (alignment,
335 g_value_get_uint (value),
337 priv->padding_right);
339 case PROP_LEFT_PADDING:
340 gtk_alignment_set_padding (alignment,
342 priv->padding_bottom,
343 g_value_get_uint (value),
344 priv->padding_right);
346 case PROP_RIGHT_PADDING:
347 gtk_alignment_set_padding (alignment,
349 priv->padding_bottom,
351 g_value_get_uint (value));
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gtk_alignment_get_property (GObject *object,
366 GtkAlignment *alignment;
367 GtkAlignmentPrivate *priv;
369 alignment = GTK_ALIGNMENT (object);
370 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
375 g_value_set_float(value, alignment->xalign);
378 g_value_set_float(value, alignment->yalign);
381 g_value_set_float(value, alignment->xscale);
384 g_value_set_float(value, alignment->yscale);
388 case PROP_TOP_PADDING:
389 g_value_set_uint (value, priv->padding_top);
391 case PROP_BOTTOM_PADDING:
392 g_value_set_uint (value, priv->padding_bottom);
394 case PROP_LEFT_PADDING:
395 g_value_set_uint (value, priv->padding_left);
397 case PROP_RIGHT_PADDING:
398 g_value_set_uint (value, priv->padding_right);
402 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
409 * @alignment: a #GtkAlignment.
410 * @xalign: the horizontal alignment of the child widget, from 0 (left) to 1
412 * @yalign: the vertical alignment of the child widget, from 0 (top) to 1
414 * @xscale: the amount that the child widget expands horizontally to fill up
415 * unused space, from 0 to 1.
416 * A value of 0 indicates that the child widget should never expand.
417 * A value of 1 indicates that the child widget will expand to fill all of the
418 * space allocated for the #GtkAlignment.
419 * @yscale: the amount that the child widget expands vertically to fill up
420 * unused space, from 0 to 1. The values are similar to @xscale.
422 * Sets the #GtkAlignment values.
425 gtk_alignment_set (GtkAlignment *alignment,
431 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
433 xalign = CLAMP (xalign, 0.0, 1.0);
434 yalign = CLAMP (yalign, 0.0, 1.0);
435 xscale = CLAMP (xscale, 0.0, 1.0);
436 yscale = CLAMP (yscale, 0.0, 1.0);
438 if ( (alignment->xalign != xalign)
439 || (alignment->yalign != yalign)
440 || (alignment->xscale != xscale)
441 || (alignment->yscale != yscale))
443 g_object_freeze_notify (G_OBJECT (alignment));
444 if (alignment->xalign != xalign)
446 alignment->xalign = xalign;
447 g_object_notify (G_OBJECT (alignment), "xalign");
449 if (alignment->yalign != yalign)
451 alignment->yalign = yalign;
452 g_object_notify (G_OBJECT (alignment), "yalign");
454 if (alignment->xscale != xscale)
456 alignment->xscale = xscale;
457 g_object_notify (G_OBJECT (alignment), "xscale");
459 if (alignment->yscale != yscale)
461 alignment->yscale = yscale;
462 g_object_notify (G_OBJECT (alignment), "yscale");
464 g_object_thaw_notify (G_OBJECT (alignment));
466 if (GTK_BIN (alignment)->child)
467 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
468 gtk_widget_queue_draw (GTK_WIDGET (alignment));
474 gtk_alignment_size_allocate (GtkWidget *widget,
475 GtkAllocation *allocation)
477 GtkAlignment *alignment;
479 GtkAllocation child_allocation;
482 gint padding_horizontal, padding_vertical;
483 GtkAlignmentPrivate *priv;
485 padding_horizontal = 0;
486 padding_vertical = 0;
488 widget->allocation = *allocation;
489 alignment = GTK_ALIGNMENT (widget);
490 bin = GTK_BIN (widget);
492 if (bin->child && gtk_widget_get_visible (bin->child))
494 GtkExtendedLayout *layout = GTK_EXTENDED_LAYOUT (bin->child);
495 gint child_nat_width;
496 gint child_nat_height;
497 gint child_width, child_height;
499 border_width = GTK_CONTAINER (alignment)->border_width;
501 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
502 padding_horizontal = priv->padding_left + priv->padding_right;
503 padding_vertical = priv->padding_top + priv->padding_bottom;
505 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
506 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
508 if (gtk_extended_layout_is_height_for_width (layout))
510 gtk_extended_layout_get_desired_width (layout, NULL, &child_nat_width);
512 child_width = MIN (width, child_nat_width);
514 gtk_extended_layout_get_height_for_width (layout, child_width, NULL, &child_nat_height);
516 child_height = MIN (height, child_nat_height);
520 gtk_extended_layout_get_desired_height (layout, NULL, &child_nat_height);
522 child_height = MIN (height, child_nat_height);
524 gtk_extended_layout_get_width_for_height (layout, child_height, NULL, &child_nat_width);
526 child_width = MIN (width, child_nat_width);
529 if (width > child_width)
530 child_allocation.width = (child_width *
531 (1.0 - alignment->xscale) +
532 width * alignment->xscale);
534 child_allocation.width = width;
536 if (height > child_height)
537 child_allocation.height = (child_height *
538 (1.0 - alignment->yscale) +
539 height * alignment->yscale);
541 child_allocation.height = height;
543 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
544 child_allocation.x = (1.0 - alignment->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
546 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
548 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
550 gtk_widget_size_allocate (bin->child, &child_allocation);
556 gtk_alignment_extended_layout_init (GtkExtendedLayoutIface *iface)
558 iface->get_desired_width = gtk_alignment_get_desired_width;
559 iface->get_desired_height = gtk_alignment_get_desired_height;
563 gtk_alignment_get_desired_size (GtkExtendedLayout *layout,
564 GtkOrientation orientation,
569 GtkAlignmentPrivate *priv;
570 gint minimum, natural;
572 priv = GTK_ALIGNMENT_GET_PRIVATE (layout);
574 natural = minimum = GTK_CONTAINER (layout)->border_width * 2;
576 if ((child = gtk_bin_get_child (GTK_BIN (layout))) && gtk_widget_get_visible (child))
578 gint child_min, child_nat;
580 /* Request extra space for the padding: */
581 if (orientation == GTK_ORIENTATION_HORIZONTAL)
583 minimum += (priv->padding_left + priv->padding_right);
584 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child),
585 &child_min, &child_nat);
589 minimum += (priv->padding_top + priv->padding_bottom);
590 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child),
591 &child_min, &child_nat);
596 minimum += child_min;
597 natural += child_nat;
601 *minimum_size = minimum;
604 *natural_size = natural;
608 gtk_alignment_get_desired_width (GtkExtendedLayout *layout,
612 gtk_alignment_get_desired_size (layout, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
616 gtk_alignment_get_desired_height (GtkExtendedLayout *layout,
620 gtk_alignment_get_desired_size (layout, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
624 * gtk_alignment_set_padding:
625 * @alignment: a #GtkAlignment
626 * @padding_top: the padding at the top of the widget
627 * @padding_bottom: the padding at the bottom of the widget
628 * @padding_left: the padding at the left of the widget
629 * @padding_right: the padding at the right of the widget.
631 * Sets the padding on the different sides of the widget.
632 * The padding adds blank space to the sides of the widget. For instance,
633 * this can be used to indent the child widget towards the right by adding
634 * padding on the left.
639 gtk_alignment_set_padding (GtkAlignment *alignment,
641 guint padding_bottom,
645 GtkAlignmentPrivate *priv;
647 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
649 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
651 g_object_freeze_notify (G_OBJECT (alignment));
653 if (priv->padding_top != padding_top)
655 priv->padding_top = padding_top;
656 g_object_notify (G_OBJECT (alignment), "top-padding");
658 if (priv->padding_bottom != padding_bottom)
660 priv->padding_bottom = padding_bottom;
661 g_object_notify (G_OBJECT (alignment), "bottom-padding");
663 if (priv->padding_left != padding_left)
665 priv->padding_left = padding_left;
666 g_object_notify (G_OBJECT (alignment), "left-padding");
668 if (priv->padding_right != padding_right)
670 priv->padding_right = padding_right;
671 g_object_notify (G_OBJECT (alignment), "right-padding");
674 g_object_thaw_notify (G_OBJECT (alignment));
676 /* Make sure that the widget and children are redrawn with the new setting: */
677 if (GTK_BIN (alignment)->child)
678 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
680 gtk_widget_queue_draw (GTK_WIDGET (alignment));
684 * gtk_alignment_get_padding:
685 * @alignment: a #GtkAlignment
686 * @padding_top: (allow-none): location to store the padding for the top of the widget, or %NULL
687 * @padding_bottom: (allow-none): location to store the padding for the bottom of the widget, or %NULL
688 * @padding_left: (allow-none): location to store the padding for the left of the widget, or %NULL
689 * @padding_right: (allow-none): location to store the padding for the right of the widget, or %NULL
691 * Gets the padding on the different sides of the widget.
692 * See gtk_alignment_set_padding ().
697 gtk_alignment_get_padding (GtkAlignment *alignment,
699 guint *padding_bottom,
701 guint *padding_right)
703 GtkAlignmentPrivate *priv;
705 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
707 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
709 *padding_top = priv->padding_top;
711 *padding_bottom = priv->padding_bottom;
713 *padding_left = priv->padding_left;
715 *padding_right = priv->padding_right;
718 #define __GTK_ALIGNMENT_C__
719 #include "gtkaliasdef.c"