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 #include "gtkalignment.h"
29 #include "gtkextendedlayout.h"
30 #include "gtkprivate.h"
48 #define GTK_ALIGNMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ALIGNMENT, GtkAlignmentPrivate))
50 struct _GtkAlignmentPrivate
58 static void gtk_alignment_size_allocate (GtkWidget *widget,
59 GtkAllocation *allocation);
60 static void gtk_alignment_set_property (GObject *object,
64 static void gtk_alignment_get_property (GObject *object,
69 static void gtk_alignment_extended_layout_init (GtkExtendedLayoutIface *iface);
70 static void gtk_alignment_get_desired_width (GtkExtendedLayout *layout,
73 static void gtk_alignment_get_desired_height (GtkExtendedLayout *layout,
77 G_DEFINE_TYPE_WITH_CODE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN,
78 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
79 gtk_alignment_extended_layout_init))
82 gtk_alignment_class_init (GtkAlignmentClass *class)
84 GObjectClass *gobject_class;
85 GtkWidgetClass *widget_class;
87 gobject_class = (GObjectClass*) class;
88 widget_class = (GtkWidgetClass*) class;
90 gobject_class->set_property = gtk_alignment_set_property;
91 gobject_class->get_property = gtk_alignment_get_property;
93 widget_class->size_allocate = gtk_alignment_size_allocate;
95 g_object_class_install_property (gobject_class,
97 g_param_spec_float("xalign",
98 P_("Horizontal alignment"),
99 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
103 GTK_PARAM_READWRITE));
105 g_object_class_install_property (gobject_class,
107 g_param_spec_float("yalign",
108 P_("Vertical alignment"),
109 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
113 GTK_PARAM_READWRITE));
114 g_object_class_install_property (gobject_class,
116 g_param_spec_float("xscale",
117 P_("Horizontal scale"),
118 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"),
122 GTK_PARAM_READWRITE));
123 g_object_class_install_property (gobject_class,
125 g_param_spec_float("yscale",
126 P_("Vertical scale"),
127 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"),
131 GTK_PARAM_READWRITE));
135 * GtkAlignment:top-padding:
137 * The padding to insert at the top of the widget.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_uint("top-padding",
145 P_("The padding to insert at the top of the widget."),
149 GTK_PARAM_READWRITE));
152 * GtkAlignment:bottom-padding:
154 * The padding to insert at the bottom of the widget.
158 g_object_class_install_property (gobject_class,
160 g_param_spec_uint("bottom-padding",
161 P_("Bottom Padding"),
162 P_("The padding to insert at the bottom of the widget."),
166 GTK_PARAM_READWRITE));
169 * GtkAlignment:left-padding:
171 * The padding to insert at the left of the widget.
175 g_object_class_install_property (gobject_class,
177 g_param_spec_uint("left-padding",
179 P_("The padding to insert at the left of the widget."),
183 GTK_PARAM_READWRITE));
186 * GtkAlignment:right-padding:
188 * The padding to insert at the right of the widget.
192 g_object_class_install_property (gobject_class,
194 g_param_spec_uint("right-padding",
196 P_("The padding to insert at the right of the widget."),
200 GTK_PARAM_READWRITE));
202 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
206 gtk_alignment_init (GtkAlignment *alignment)
208 GtkAlignmentPrivate *priv;
210 gtk_widget_set_has_window (GTK_WIDGET (alignment), FALSE);
211 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
213 alignment->xalign = 0.5;
214 alignment->yalign = 0.5;
215 alignment->xscale = 1.0;
216 alignment->yscale = 1.0;
218 /* Initialize padding with default values: */
219 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
220 priv->padding_top = 0;
221 priv->padding_bottom = 0;
222 priv->padding_left = 0;
223 priv->padding_right = 0;
227 gtk_alignment_new (gfloat xalign,
232 GtkAlignment *alignment;
234 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
236 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
237 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
238 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
239 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
241 return GTK_WIDGET (alignment);
245 gtk_alignment_set_property (GObject *object,
250 GtkAlignment *alignment;
251 GtkAlignmentPrivate *priv;
253 alignment = GTK_ALIGNMENT (object);
254 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
259 gtk_alignment_set (alignment,
260 g_value_get_float (value),
266 gtk_alignment_set (alignment,
268 g_value_get_float (value),
273 gtk_alignment_set (alignment,
276 g_value_get_float (value),
280 gtk_alignment_set (alignment,
284 g_value_get_float (value));
288 case PROP_TOP_PADDING:
289 gtk_alignment_set_padding (alignment,
290 g_value_get_uint (value),
291 priv->padding_bottom,
293 priv->padding_right);
295 case PROP_BOTTOM_PADDING:
296 gtk_alignment_set_padding (alignment,
298 g_value_get_uint (value),
300 priv->padding_right);
302 case PROP_LEFT_PADDING:
303 gtk_alignment_set_padding (alignment,
305 priv->padding_bottom,
306 g_value_get_uint (value),
307 priv->padding_right);
309 case PROP_RIGHT_PADDING:
310 gtk_alignment_set_padding (alignment,
312 priv->padding_bottom,
314 g_value_get_uint (value));
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 gtk_alignment_get_property (GObject *object,
329 GtkAlignment *alignment;
330 GtkAlignmentPrivate *priv;
332 alignment = GTK_ALIGNMENT (object);
333 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
338 g_value_set_float(value, alignment->xalign);
341 g_value_set_float(value, alignment->yalign);
344 g_value_set_float(value, alignment->xscale);
347 g_value_set_float(value, alignment->yscale);
351 case PROP_TOP_PADDING:
352 g_value_set_uint (value, priv->padding_top);
354 case PROP_BOTTOM_PADDING:
355 g_value_set_uint (value, priv->padding_bottom);
357 case PROP_LEFT_PADDING:
358 g_value_set_uint (value, priv->padding_left);
360 case PROP_RIGHT_PADDING:
361 g_value_set_uint (value, priv->padding_right);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gtk_alignment_set (GtkAlignment *alignment,
377 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
379 xalign = CLAMP (xalign, 0.0, 1.0);
380 yalign = CLAMP (yalign, 0.0, 1.0);
381 xscale = CLAMP (xscale, 0.0, 1.0);
382 yscale = CLAMP (yscale, 0.0, 1.0);
384 if ( (alignment->xalign != xalign)
385 || (alignment->yalign != yalign)
386 || (alignment->xscale != xscale)
387 || (alignment->yscale != yscale))
389 g_object_freeze_notify (G_OBJECT (alignment));
390 if (alignment->xalign != xalign)
392 alignment->xalign = xalign;
393 g_object_notify (G_OBJECT (alignment), "xalign");
395 if (alignment->yalign != yalign)
397 alignment->yalign = yalign;
398 g_object_notify (G_OBJECT (alignment), "yalign");
400 if (alignment->xscale != xscale)
402 alignment->xscale = xscale;
403 g_object_notify (G_OBJECT (alignment), "xscale");
405 if (alignment->yscale != yscale)
407 alignment->yscale = yscale;
408 g_object_notify (G_OBJECT (alignment), "yscale");
410 g_object_thaw_notify (G_OBJECT (alignment));
412 if (GTK_BIN (alignment)->child)
413 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
414 gtk_widget_queue_draw (GTK_WIDGET (alignment));
420 gtk_alignment_size_allocate (GtkWidget *widget,
421 GtkAllocation *allocation)
423 GtkAlignment *alignment;
425 GtkAllocation child_allocation;
426 GtkRequisition child_requisition;
429 gint padding_horizontal, padding_vertical;
430 GtkAlignmentPrivate *priv;
432 padding_horizontal = 0;
433 padding_vertical = 0;
435 widget->allocation = *allocation;
436 alignment = GTK_ALIGNMENT (widget);
437 bin = GTK_BIN (widget);
439 if (bin->child && gtk_widget_get_visible (bin->child))
441 GtkExtendedLayout *layout = GTK_EXTENDED_LAYOUT (bin->child);
443 gtk_extended_layout_get_desired_size (layout, NULL, &child_requisition);
445 border_width = GTK_CONTAINER (alignment)->border_width;
447 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
448 padding_horizontal = priv->padding_left + priv->padding_right;
449 padding_vertical = priv->padding_top + priv->padding_bottom;
451 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
452 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
454 if (child_requisition.width > width)
455 gtk_extended_layout_get_height_for_width (layout, width, NULL,
456 &child_requisition.height);
457 else if (child_requisition.height > height)
458 gtk_extended_layout_get_width_for_height (layout, height, NULL,
459 &child_requisition.width);
461 if (width > child_requisition.width)
462 child_allocation.width = (child_requisition.width *
463 (1.0 - alignment->xscale) +
464 width * alignment->xscale);
466 child_allocation.width = width;
468 if (height > child_requisition.height)
469 child_allocation.height = (child_requisition.height *
470 (1.0 - alignment->yscale) +
471 height * alignment->yscale);
473 child_allocation.height = height;
475 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
476 child_allocation.x = (1.0 - alignment->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
478 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
480 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
482 gtk_widget_size_allocate (bin->child, &child_allocation);
488 gtk_alignment_extended_layout_init (GtkExtendedLayoutIface *iface)
490 iface->get_desired_width = gtk_alignment_get_desired_width;
491 iface->get_desired_height = gtk_alignment_get_desired_height;
495 gtk_alignment_get_desired_size (GtkExtendedLayout *layout,
496 GtkOrientation orientation,
501 GtkAlignmentPrivate *priv;
502 gint minimum, natural;
504 priv = GTK_ALIGNMENT_GET_PRIVATE (layout);
506 natural = minimum = GTK_CONTAINER (layout)->border_width * 2;
508 if ((child = gtk_bin_get_child (GTK_BIN (layout))) && gtk_widget_get_visible (child))
510 gint child_min, child_nat;
512 /* Request extra space for the padding: */
513 if (orientation == GTK_ORIENTATION_HORIZONTAL)
515 minimum += (priv->padding_left + priv->padding_right);
516 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child),
517 &child_min, &child_nat);
521 minimum += (priv->padding_top + priv->padding_bottom);
522 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child),
523 &child_min, &child_nat);
528 minimum += child_min;
529 natural += child_nat;
533 *minimum_size = minimum;
536 *natural_size = natural;
540 gtk_alignment_get_desired_width (GtkExtendedLayout *layout,
544 gtk_alignment_get_desired_size (layout, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
548 gtk_alignment_get_desired_height (GtkExtendedLayout *layout,
552 gtk_alignment_get_desired_size (layout, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
556 * gtk_alignment_set_padding:
557 * @alignment: a #GtkAlignment
558 * @padding_top: the padding at the top of the widget
559 * @padding_bottom: the padding at the bottom of the widget
560 * @padding_left: the padding at the left of the widget
561 * @padding_right: the padding at the right of the widget.
563 * Sets the padding on the different sides of the widget.
564 * The padding adds blank space to the sides of the widget. For instance,
565 * this can be used to indent the child widget towards the right by adding
566 * padding on the left.
571 gtk_alignment_set_padding (GtkAlignment *alignment,
573 guint padding_bottom,
577 GtkAlignmentPrivate *priv;
579 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
581 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
583 g_object_freeze_notify (G_OBJECT (alignment));
585 if (priv->padding_top != padding_top)
587 priv->padding_top = padding_top;
588 g_object_notify (G_OBJECT (alignment), "top-padding");
590 if (priv->padding_bottom != padding_bottom)
592 priv->padding_bottom = padding_bottom;
593 g_object_notify (G_OBJECT (alignment), "bottom-padding");
595 if (priv->padding_left != padding_left)
597 priv->padding_left = padding_left;
598 g_object_notify (G_OBJECT (alignment), "left-padding");
600 if (priv->padding_right != padding_right)
602 priv->padding_right = padding_right;
603 g_object_notify (G_OBJECT (alignment), "right-padding");
606 g_object_thaw_notify (G_OBJECT (alignment));
608 /* Make sure that the widget and children are redrawn with the new setting: */
609 if (GTK_BIN (alignment)->child)
610 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
612 gtk_widget_queue_draw (GTK_WIDGET (alignment));
616 * gtk_alignment_get_padding:
617 * @alignment: a #GtkAlignment
618 * @padding_top: (allow-none): location to store the padding for the top of the widget, or %NULL
619 * @padding_bottom: (allow-none): location to store the padding for the bottom of the widget, or %NULL
620 * @padding_left: (allow-none): location to store the padding for the left of the widget, or %NULL
621 * @padding_right: (allow-none): location to store the padding for the right of the widget, or %NULL
623 * Gets the padding on the different sides of the widget.
624 * See gtk_alignment_set_padding ().
629 gtk_alignment_get_padding (GtkAlignment *alignment,
631 guint *padding_bottom,
633 guint *padding_right)
635 GtkAlignmentPrivate *priv;
637 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
639 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
641 *padding_top = priv->padding_top;
643 *padding_bottom = priv->padding_bottom;
645 *padding_left = priv->padding_left;
647 *padding_right = priv->padding_right;
650 #define __GTK_ALIGNMENT_C__
651 #include "gtkaliasdef.c"