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, "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)
444 GtkAlignment *alignment;
446 GtkAlignmentPrivate *priv;
448 alignment = GTK_ALIGNMENT (widget);
449 bin = GTK_BIN (widget);
450 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
452 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
453 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
455 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
457 GtkRequisition child_requisition;
459 gtk_widget_size_request (bin->child, &child_requisition);
461 requisition->width += child_requisition.width;
462 requisition->height += child_requisition.height;
464 /* Request extra space for the padding: */
465 requisition->width += (priv->padding_left + priv->padding_right);
466 requisition->height += (priv->padding_top + priv->padding_bottom);
471 gtk_alignment_size_allocate (GtkWidget *widget,
472 GtkAllocation *allocation)
474 GtkAlignment *alignment;
476 GtkAllocation child_allocation;
477 GtkRequisition child_requisition;
480 gint padding_horizontal, padding_vertical;
481 GtkAlignmentPrivate *priv;
484 padding_horizontal = 0;
485 padding_vertical = 0;
487 widget->allocation = *allocation;
488 alignment = GTK_ALIGNMENT (widget);
489 bin = GTK_BIN (widget);
491 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
493 gtk_widget_get_child_requisition (bin->child, &child_requisition);
495 border_width = GTK_CONTAINER (alignment)->border_width;
497 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
498 padding_horizontal = priv->padding_left + priv->padding_right;
499 padding_vertical = priv->padding_top + priv->padding_bottom;
501 width = allocation->width - padding_horizontal - 2 * border_width;
502 height = allocation->height - padding_vertical - 2 * border_width;
504 if (width > child_requisition.width)
505 child_allocation.width = (child_requisition.width *
506 (1.0 - alignment->xscale) +
507 width * alignment->xscale);
509 child_allocation.width = width;
511 if (height > child_requisition.height)
512 child_allocation.height = (child_requisition.height *
513 (1.0 - alignment->yscale) +
514 height * alignment->yscale);
516 child_allocation.height = height;
518 xalign = alignment->xalign;
519 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
520 xalign = 1.0 - xalign;
522 child_allocation.x = xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
523 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
525 gtk_widget_size_allocate (bin->child, &child_allocation);
530 * gtk_alignment_set_padding:
531 * @alignment: a #GtkAlignment
532 * @padding_top: the padding at the top of the widget
533 * @padding_bottom: the padding at the bottom of the widget
534 * @padding_left: the padding at the left of the widget
535 * @padding_right: the padding at the right of the widget.
537 * Sets the padding on the different sides of the widget.
538 * The padding adds blank space to the sides of the widget. For instance,
539 * this can be used to indent the child widget towards the right by adding
540 * padding on the left.
545 gtk_alignment_set_padding (GtkAlignment *alignment,
547 guint padding_bottom,
551 GtkAlignmentPrivate *priv;
553 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
555 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
557 g_object_freeze_notify (G_OBJECT (alignment));
559 if (priv->padding_top != padding_top)
561 priv->padding_top = padding_top;
562 g_object_notify (G_OBJECT (alignment), "top_padding");
564 if (priv->padding_bottom != padding_bottom)
566 priv->padding_bottom = padding_bottom;
567 g_object_notify (G_OBJECT (alignment), "bottom_padding");
569 if (priv->padding_left != padding_left)
571 priv->padding_left = padding_left;
572 g_object_notify (G_OBJECT (alignment), "left_padding");
574 if (priv->padding_right != padding_right)
576 priv->padding_right = padding_right;
577 g_object_notify (G_OBJECT (alignment), "right_padding");
580 g_object_thaw_notify (G_OBJECT (alignment));
582 /* Make sure that the widget and children are redrawn with the new setting: */
583 if (GTK_BIN (alignment)->child)
584 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
586 gtk_widget_queue_draw (GTK_WIDGET (alignment));
590 * gtk_alignment_get_padding:
591 * @alignment: a #GtkAlignment
592 * @padding_top: location to store the padding for the top of the widget, or %NULL
593 * @padding_bottom: location to store the padding for the bottom of the widget, or %NULL
594 * @padding_left: location to store the padding for the left of the widget, or %NULL
595 * @padding_right: location to store the padding for the right of the widget, or %NULL
597 * Gets the padding on the different sides of the widget.
598 * See gtk_alignment_set_padding ().
603 gtk_alignment_get_padding (GtkAlignment *alignment,
605 guint *padding_bottom,
607 guint *padding_right)
609 GtkAlignmentPrivate *priv;
611 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
613 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
615 *padding_top = priv->padding_top;
617 *padding_bottom = priv->padding_bottom;
619 *padding_left = priv->padding_left;
621 *padding_right = priv->padding_right;
624 #define __GTK_ALIGNMENT_C__
625 #include "gtkaliasdef.c"