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 "gtkprivate.h"
49 #define GTK_ALIGNMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ALIGNMENT, GtkAlignmentPrivate))
51 struct _GtkAlignmentPrivate
59 static void gtk_alignment_class_init (GtkAlignmentClass *klass);
60 static void gtk_alignment_init (GtkAlignment *alignment);
61 static void gtk_alignment_size_request (GtkWidget *widget,
62 GtkRequisition *requisition);
63 static void gtk_alignment_size_allocate (GtkWidget *widget,
64 GtkAllocation *allocation);
65 static void gtk_alignment_set_property (GObject *object,
69 static void gtk_alignment_get_property (GObject *object,
75 gtk_alignment_get_type (void)
77 static GType alignment_type = 0;
81 static const GTypeInfo alignment_info =
83 sizeof (GtkAlignmentClass),
85 NULL, /* base_finalize */
86 (GClassInitFunc) gtk_alignment_class_init,
87 NULL, /* class_finalize */
88 NULL, /* class_data */
89 sizeof (GtkAlignment),
91 (GInstanceInitFunc) gtk_alignment_init,
94 alignment_type = g_type_register_static (GTK_TYPE_BIN, I_("GtkAlignment"),
98 return alignment_type;
102 gtk_alignment_class_init (GtkAlignmentClass *class)
104 GObjectClass *gobject_class;
105 GtkWidgetClass *widget_class;
107 gobject_class = (GObjectClass*) class;
108 widget_class = (GtkWidgetClass*) class;
110 gobject_class->set_property = gtk_alignment_set_property;
111 gobject_class->get_property = gtk_alignment_get_property;
113 widget_class->size_request = gtk_alignment_size_request;
114 widget_class->size_allocate = gtk_alignment_size_allocate;
116 g_object_class_install_property (gobject_class,
118 g_param_spec_float("xalign",
119 P_("Horizontal alignment"),
120 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
124 GTK_PARAM_READWRITE));
126 g_object_class_install_property (gobject_class,
128 g_param_spec_float("yalign",
129 P_("Vertical alignment"),
130 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
134 GTK_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_float("xscale",
138 P_("Horizontal scale"),
139 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"),
143 GTK_PARAM_READWRITE));
144 g_object_class_install_property (gobject_class,
146 g_param_spec_float("yscale",
147 P_("Vertical scale"),
148 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"),
152 GTK_PARAM_READWRITE));
156 * GtkAlignment:top-padding:
158 * The padding to insert at the top of the widget.
162 g_object_class_install_property (gobject_class,
164 g_param_spec_uint("top-padding",
166 P_("The padding to insert at the top of the widget."),
170 GTK_PARAM_READWRITE));
173 * GtkAlignment:bottom-padding:
175 * The padding to insert at the bottom of the widget.
179 g_object_class_install_property (gobject_class,
181 g_param_spec_uint("bottom-padding",
182 P_("Bottom Padding"),
183 P_("The padding to insert at the bottom of the widget."),
187 GTK_PARAM_READWRITE));
190 * GtkAlignment:left-padding:
192 * The padding to insert at the left of the widget.
196 g_object_class_install_property (gobject_class,
198 g_param_spec_uint("left-padding",
200 P_("The padding to insert at the left of the widget."),
204 GTK_PARAM_READWRITE));
207 * GtkAlignment:right-padding:
209 * The padding to insert at the right of the widget.
213 g_object_class_install_property (gobject_class,
215 g_param_spec_uint("right-padding",
217 P_("The padding to insert at the right of the widget."),
221 GTK_PARAM_READWRITE));
223 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
227 gtk_alignment_init (GtkAlignment *alignment)
229 GtkAlignmentPrivate *priv;
231 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
232 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
234 alignment->xalign = 0.5;
235 alignment->yalign = 0.5;
236 alignment->xscale = 1.0;
237 alignment->yscale = 1.0;
239 /* Initialize padding with default values: */
240 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
241 priv->padding_top = 0;
242 priv->padding_bottom = 0;
243 priv->padding_left = 0;
244 priv->padding_right = 0;
248 gtk_alignment_new (gfloat xalign,
253 GtkAlignment *alignment;
255 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
257 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
258 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
259 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
260 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
262 return GTK_WIDGET (alignment);
266 gtk_alignment_set_property (GObject *object,
271 GtkAlignment *alignment;
272 GtkAlignmentPrivate *priv;
274 alignment = GTK_ALIGNMENT (object);
275 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
280 gtk_alignment_set (alignment,
281 g_value_get_float (value),
287 gtk_alignment_set (alignment,
289 g_value_get_float (value),
294 gtk_alignment_set (alignment,
297 g_value_get_float (value),
301 gtk_alignment_set (alignment,
305 g_value_get_float (value));
309 case PROP_TOP_PADDING:
310 gtk_alignment_set_padding (alignment,
311 g_value_get_uint (value),
312 priv->padding_bottom,
314 priv->padding_right);
316 case PROP_BOTTOM_PADDING:
317 gtk_alignment_set_padding (alignment,
319 g_value_get_uint (value),
321 priv->padding_right);
323 case PROP_LEFT_PADDING:
324 gtk_alignment_set_padding (alignment,
326 priv->padding_bottom,
327 g_value_get_uint (value),
328 priv->padding_right);
330 case PROP_RIGHT_PADDING:
331 gtk_alignment_set_padding (alignment,
333 priv->padding_bottom,
335 g_value_get_uint (value));
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 gtk_alignment_get_property (GObject *object,
350 GtkAlignment *alignment;
351 GtkAlignmentPrivate *priv;
353 alignment = GTK_ALIGNMENT (object);
354 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
359 g_value_set_float(value, alignment->xalign);
362 g_value_set_float(value, alignment->yalign);
365 g_value_set_float(value, alignment->xscale);
368 g_value_set_float(value, alignment->yscale);
372 case PROP_TOP_PADDING:
373 g_value_set_uint (value, priv->padding_top);
375 case PROP_BOTTOM_PADDING:
376 g_value_set_uint (value, priv->padding_bottom);
378 case PROP_LEFT_PADDING:
379 g_value_set_uint (value, priv->padding_left);
381 case PROP_RIGHT_PADDING:
382 g_value_set_uint (value, priv->padding_right);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 gtk_alignment_set (GtkAlignment *alignment,
398 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
400 xalign = CLAMP (xalign, 0.0, 1.0);
401 yalign = CLAMP (yalign, 0.0, 1.0);
402 xscale = CLAMP (xscale, 0.0, 1.0);
403 yscale = CLAMP (yscale, 0.0, 1.0);
405 if ( (alignment->xalign != xalign)
406 || (alignment->yalign != yalign)
407 || (alignment->xscale != xscale)
408 || (alignment->yscale != yscale))
410 g_object_freeze_notify (G_OBJECT (alignment));
411 if (alignment->xalign != xalign)
413 alignment->xalign = xalign;
414 g_object_notify (G_OBJECT (alignment), "xalign");
416 if (alignment->yalign != yalign)
418 alignment->yalign = yalign;
419 g_object_notify (G_OBJECT (alignment), "yalign");
421 if (alignment->xscale != xscale)
423 alignment->xscale = xscale;
424 g_object_notify (G_OBJECT (alignment), "xscale");
426 if (alignment->yscale != yscale)
428 alignment->yscale = yscale;
429 g_object_notify (G_OBJECT (alignment), "yscale");
431 g_object_thaw_notify (G_OBJECT (alignment));
433 if (GTK_BIN (alignment)->child)
434 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
435 gtk_widget_queue_draw (GTK_WIDGET (alignment));
441 gtk_alignment_size_request (GtkWidget *widget,
442 GtkRequisition *requisition)
445 GtkAlignmentPrivate *priv;
447 bin = GTK_BIN (widget);
448 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
450 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
451 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
453 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
455 GtkRequisition child_requisition;
457 gtk_widget_size_request (bin->child, &child_requisition);
459 requisition->width += child_requisition.width;
460 requisition->height += child_requisition.height;
462 /* Request extra space for the padding: */
463 requisition->width += (priv->padding_left + priv->padding_right);
464 requisition->height += (priv->padding_top + priv->padding_bottom);
469 gtk_alignment_size_allocate (GtkWidget *widget,
470 GtkAllocation *allocation)
472 GtkAlignment *alignment;
474 GtkAllocation child_allocation;
475 GtkRequisition child_requisition;
478 gint padding_horizontal, padding_vertical;
479 GtkAlignmentPrivate *priv;
481 padding_horizontal = 0;
482 padding_vertical = 0;
484 widget->allocation = *allocation;
485 alignment = GTK_ALIGNMENT (widget);
486 bin = GTK_BIN (widget);
488 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
490 gtk_widget_get_child_requisition (bin->child, &child_requisition);
492 border_width = GTK_CONTAINER (alignment)->border_width;
494 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
495 padding_horizontal = priv->padding_left + priv->padding_right;
496 padding_vertical = priv->padding_top + priv->padding_bottom;
498 width = allocation->width - padding_horizontal - 2 * border_width;
499 height = allocation->height - padding_vertical - 2 * border_width;
501 if (width > child_requisition.width)
502 child_allocation.width = (child_requisition.width *
503 (1.0 - alignment->xscale) +
504 width * alignment->xscale);
506 child_allocation.width = width;
508 if (height > child_requisition.height)
509 child_allocation.height = (child_requisition.height *
510 (1.0 - alignment->yscale) +
511 height * alignment->yscale);
513 child_allocation.height = height;
515 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
516 child_allocation.x = (1.0 - alignment->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
518 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
520 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
522 gtk_widget_size_allocate (bin->child, &child_allocation);
527 * gtk_alignment_set_padding:
528 * @alignment: a #GtkAlignment
529 * @padding_top: the padding at the top of the widget
530 * @padding_bottom: the padding at the bottom of the widget
531 * @padding_left: the padding at the left of the widget
532 * @padding_right: the padding at the right of the widget.
534 * Sets the padding on the different sides of the widget.
535 * The padding adds blank space to the sides of the widget. For instance,
536 * this can be used to indent the child widget towards the right by adding
537 * padding on the left.
542 gtk_alignment_set_padding (GtkAlignment *alignment,
544 guint padding_bottom,
548 GtkAlignmentPrivate *priv;
550 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
552 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
554 g_object_freeze_notify (G_OBJECT (alignment));
556 if (priv->padding_top != padding_top)
558 priv->padding_top = padding_top;
559 g_object_notify (G_OBJECT (alignment), "top-padding");
561 if (priv->padding_bottom != padding_bottom)
563 priv->padding_bottom = padding_bottom;
564 g_object_notify (G_OBJECT (alignment), "bottom-padding");
566 if (priv->padding_left != padding_left)
568 priv->padding_left = padding_left;
569 g_object_notify (G_OBJECT (alignment), "left-padding");
571 if (priv->padding_right != padding_right)
573 priv->padding_right = padding_right;
574 g_object_notify (G_OBJECT (alignment), "right-padding");
577 g_object_thaw_notify (G_OBJECT (alignment));
579 /* Make sure that the widget and children are redrawn with the new setting: */
580 if (GTK_BIN (alignment)->child)
581 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
583 gtk_widget_queue_draw (GTK_WIDGET (alignment));
587 * gtk_alignment_get_padding:
588 * @alignment: a #GtkAlignment
589 * @padding_top: location to store the padding for the top of the widget, or %NULL
590 * @padding_bottom: location to store the padding for the bottom of the widget, or %NULL
591 * @padding_left: location to store the padding for the left of the widget, or %NULL
592 * @padding_right: location to store the padding for the right of the widget, or %NULL
594 * Gets the padding on the different sides of the widget.
595 * See gtk_alignment_set_padding ().
600 gtk_alignment_get_padding (GtkAlignment *alignment,
602 guint *padding_bottom,
604 guint *padding_right)
606 GtkAlignmentPrivate *priv;
608 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
610 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
612 *padding_top = priv->padding_top;
614 *padding_bottom = priv->padding_bottom;
616 *padding_left = priv->padding_left;
618 *padding_right = priv->padding_right;
621 #define __GTK_ALIGNMENT_C__
622 #include "gtkaliasdef.c"