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/.
29 #include "gtkalignment.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_class_init (GtkAlignmentClass *klass);
59 static void gtk_alignment_init (GtkAlignment *alignment);
60 static void gtk_alignment_size_request (GtkWidget *widget,
61 GtkRequisition *requisition);
62 static void gtk_alignment_size_allocate (GtkWidget *widget,
63 GtkAllocation *allocation);
64 static void gtk_alignment_set_property (GObject *object,
68 static void gtk_alignment_get_property (GObject *object,
74 gtk_alignment_get_type (void)
76 static GType alignment_type = 0;
80 static const GTypeInfo alignment_info =
82 sizeof (GtkAlignmentClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) gtk_alignment_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (GtkAlignment),
90 (GInstanceInitFunc) gtk_alignment_init,
93 alignment_type = g_type_register_static (GTK_TYPE_BIN, "GtkAlignment",
97 return alignment_type;
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_request = gtk_alignment_size_request;
113 widget_class->size_allocate = gtk_alignment_size_allocate;
115 g_object_class_install_property (gobject_class,
117 g_param_spec_float("xalign",
118 P_("Horizontal alignment"),
119 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
123 G_PARAM_READABLE | G_PARAM_WRITABLE));
125 g_object_class_install_property (gobject_class,
127 g_param_spec_float("yalign",
128 P_("Vertical alignment"),
129 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
133 G_PARAM_READABLE | G_PARAM_WRITABLE));
134 g_object_class_install_property (gobject_class,
136 g_param_spec_float("xscale",
137 P_("Horizontal scale"),
138 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"),
142 G_PARAM_READABLE | G_PARAM_WRITABLE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_float("yscale",
146 P_("Vertical scale"),
147 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"),
151 G_PARAM_READABLE | G_PARAM_WRITABLE));
155 * GtkAlignment:top-padding:
157 * The padding to insert at the top of the widget.
161 g_object_class_install_property (gobject_class,
163 g_param_spec_uint("top-padding",
165 P_("The padding to insert at the top of the widget."),
169 G_PARAM_READABLE | G_PARAM_WRITABLE));
172 * GtkAlignment:top-padding:
174 * The padding to insert at the bottom of the widget.
178 g_object_class_install_property (gobject_class,
180 g_param_spec_uint("bottom-padding",
181 P_("Bottom Padding"),
182 P_("The padding to insert at the bottom of the widget."),
186 G_PARAM_READABLE | G_PARAM_WRITABLE));
189 * GtkAlignment:top-padding:
191 * The padding to insert at the left of the widget.
195 g_object_class_install_property (gobject_class,
197 g_param_spec_uint("left-padding",
199 P_("The padding to insert at the left of the widget."),
203 G_PARAM_READABLE | G_PARAM_WRITABLE));
206 * GtkAlignment:top-padding:
208 * The padding to insert at the right of the widget.
212 g_object_class_install_property (gobject_class,
214 g_param_spec_uint("right-padding",
216 P_("The padding to insert at the right of the widget."),
220 G_PARAM_READABLE | G_PARAM_WRITABLE));
222 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
226 gtk_alignment_init (GtkAlignment *alignment)
228 GtkAlignmentPrivate *priv;
230 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
231 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
233 alignment->xalign = 0.5;
234 alignment->yalign = 0.5;
235 alignment->xscale = 1.0;
236 alignment->yscale = 1.0;
238 /* Initialize padding with default values: */
239 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
240 priv->padding_top = 0;
241 priv->padding_bottom = 0;
242 priv->padding_left = 0;
243 priv->padding_right = 0;
247 gtk_alignment_new (gfloat xalign,
252 GtkAlignment *alignment;
254 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
256 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
257 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
258 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
259 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
261 return GTK_WIDGET (alignment);
265 gtk_alignment_set_property (GObject *object,
270 GtkAlignment *alignment;
271 GtkAlignmentPrivate *priv;
273 alignment = GTK_ALIGNMENT (object);
274 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
279 gtk_alignment_set (alignment,
280 g_value_get_float (value),
286 gtk_alignment_set (alignment,
288 g_value_get_float (value),
293 gtk_alignment_set (alignment,
296 g_value_get_float (value),
300 gtk_alignment_set (alignment,
304 g_value_get_float (value));
308 case PROP_TOP_PADDING:
309 gtk_alignment_set_padding (alignment,
310 g_value_get_uint (value),
311 priv->padding_bottom,
313 priv->padding_right);
315 case PROP_BOTTOM_PADDING:
316 gtk_alignment_set_padding (alignment,
318 g_value_get_uint (value),
320 priv->padding_right);
322 case PROP_LEFT_PADDING:
323 gtk_alignment_set_padding (alignment,
325 priv->padding_bottom,
326 g_value_get_uint (value),
327 priv->padding_right);
329 case PROP_RIGHT_PADDING:
330 gtk_alignment_set_padding (alignment,
332 priv->padding_bottom,
334 g_value_get_uint (value));
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 gtk_alignment_get_property (GObject *object,
349 GtkAlignment *alignment;
350 GtkAlignmentPrivate *priv;
352 alignment = GTK_ALIGNMENT (object);
353 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
358 g_value_set_float(value, alignment->xalign);
361 g_value_set_float(value, alignment->yalign);
364 g_value_set_float(value, alignment->xscale);
367 g_value_set_float(value, alignment->yscale);
371 case PROP_TOP_PADDING:
372 g_value_set_uint (value, priv->padding_top);
374 case PROP_BOTTOM_PADDING:
375 g_value_set_uint (value, priv->padding_bottom);
377 case PROP_LEFT_PADDING:
378 g_value_set_uint (value, priv->padding_left);
380 case PROP_RIGHT_PADDING:
381 g_value_set_uint (value, priv->padding_right);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gtk_alignment_set (GtkAlignment *alignment,
397 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
399 xalign = CLAMP (xalign, 0.0, 1.0);
400 yalign = CLAMP (yalign, 0.0, 1.0);
401 xscale = CLAMP (xscale, 0.0, 1.0);
402 yscale = CLAMP (yscale, 0.0, 1.0);
404 if ( (alignment->xalign != xalign)
405 || (alignment->yalign != yalign)
406 || (alignment->xscale != xscale)
407 || (alignment->yscale != yscale))
409 g_object_freeze_notify (G_OBJECT (alignment));
410 if (alignment->xalign != xalign)
412 alignment->xalign = xalign;
413 g_object_notify (G_OBJECT (alignment), "xalign");
415 if (alignment->yalign != yalign)
417 alignment->yalign = yalign;
418 g_object_notify (G_OBJECT (alignment), "yalign");
420 if (alignment->xscale != xscale)
422 alignment->xscale = xscale;
423 g_object_notify (G_OBJECT (alignment), "xscale");
425 if (alignment->yscale != yscale)
427 alignment->yscale = yscale;
428 g_object_notify (G_OBJECT (alignment), "yscale");
430 g_object_thaw_notify (G_OBJECT (alignment));
432 if (GTK_BIN (alignment)->child)
433 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
434 gtk_widget_queue_draw (GTK_WIDGET (alignment));
440 gtk_alignment_size_request (GtkWidget *widget,
441 GtkRequisition *requisition)
443 GtkAlignment *alignment;
445 GtkAlignmentPrivate *priv;
447 alignment = GTK_ALIGNMENT (widget);
448 bin = GTK_BIN (widget);
449 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
451 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
452 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
454 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
456 GtkRequisition child_requisition;
458 gtk_widget_size_request (bin->child, &child_requisition);
460 requisition->width += child_requisition.width;
461 requisition->height += child_requisition.height;
463 /* Request extra space for the padding: */
464 requisition->width += (priv->padding_left + priv->padding_right);
465 requisition->height += (priv->padding_top + priv->padding_bottom);
470 gtk_alignment_size_allocate (GtkWidget *widget,
471 GtkAllocation *allocation)
473 GtkAlignment *alignment;
475 GtkAllocation child_allocation;
476 GtkRequisition child_requisition;
479 gint padding_horizontal, padding_vertical;
480 GtkAlignmentPrivate *priv;
483 padding_horizontal = 0;
484 padding_vertical = 0;
486 widget->allocation = *allocation;
487 alignment = GTK_ALIGNMENT (widget);
488 bin = GTK_BIN (widget);
490 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
492 gtk_widget_get_child_requisition (bin->child, &child_requisition);
494 border_width = GTK_CONTAINER (alignment)->border_width;
496 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
497 padding_horizontal = priv->padding_left + priv->padding_right;
498 padding_vertical = priv->padding_top + priv->padding_bottom;
500 width = allocation->width - padding_horizontal - 2 * border_width;
501 height = allocation->height - padding_vertical - 2 * border_width;
503 if (width > child_requisition.width)
504 child_allocation.width = (child_requisition.width *
505 (1.0 - alignment->xscale) +
506 width * alignment->xscale);
508 child_allocation.width = width;
510 if (height > child_requisition.height)
511 child_allocation.height = (child_requisition.height *
512 (1.0 - alignment->yscale) +
513 height * alignment->yscale);
515 child_allocation.height = height;
517 xalign = alignment->xalign;
518 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
519 xalign = 1.0 - xalign;
521 child_allocation.x = xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
522 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
524 gtk_widget_size_allocate (bin->child, &child_allocation);
529 * gtk_alignment_set_padding:
530 * @alignment: a #GtkAlignment
531 * @padding_top: the padding at the top of the widget
532 * @padding_bottom: the padding at the bottom of the widget
533 * @padding_left: the padding at the left of the widget
534 * @padding_right: the padding at the right of the widget.
536 * Sets the padding on the different sides of the widget.
537 * The padding adds blank space to the sides of the widget. For instance,
538 * this can be used to indent the child widget towards the right by adding
539 * padding on the left.
544 gtk_alignment_set_padding (GtkAlignment *alignment,
546 guint padding_bottom,
550 GtkAlignmentPrivate *priv;
552 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
554 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
556 g_object_freeze_notify (G_OBJECT (alignment));
558 if (priv->padding_top != padding_top)
560 priv->padding_top = padding_top;
561 g_object_notify (G_OBJECT (alignment), "top_padding");
563 if (priv->padding_bottom != padding_bottom)
565 priv->padding_bottom = padding_bottom;
566 g_object_notify (G_OBJECT (alignment), "bottom_padding");
568 if (priv->padding_left != padding_left)
570 priv->padding_left = padding_left;
571 g_object_notify (G_OBJECT (alignment), "left_padding");
573 if (priv->padding_right != padding_right)
575 priv->padding_right = padding_right;
576 g_object_notify (G_OBJECT (alignment), "right_padding");
579 g_object_thaw_notify (G_OBJECT (alignment));
581 /* Make sure that the widget and children are redrawn with the new setting: */
582 if (GTK_BIN (alignment)->child)
583 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
585 gtk_widget_queue_draw (GTK_WIDGET (alignment));
589 * gtk_alignment_get_padding:
590 * @alignment: a #GtkAlignment
591 * @padding_top: location to store the padding for the top of the widget, or %NULL
592 * @padding_bottom: location to store the padding for the bottom of the widget, or %NULL
593 * @padding_left: location to store the padding for the left of the widget, or %NULL
594 * @padding_right: location to store the padding for the right of the widget, or %NULL
596 * Gets the padding on the different sides of the widget.
597 * See gtk_alignment_set_padding ().
602 gtk_alignment_get_padding (GtkAlignment *alignment,
604 guint *padding_bottom,
606 guint *padding_right)
608 GtkAlignmentPrivate *priv;
610 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
612 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
614 *padding_top = priv->padding_top;
616 *padding_bottom = priv->padding_bottom;
618 *padding_left = priv->padding_left;
620 *padding_right = priv->padding_right;