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_request (GtkWidget *widget,
59 GtkRequisition *requisition);
60 static void gtk_alignment_size_allocate (GtkWidget *widget,
61 GtkAllocation *allocation);
62 static void gtk_alignment_set_property (GObject *object,
66 static void gtk_alignment_get_property (GObject *object,
71 G_DEFINE_TYPE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN)
74 gtk_alignment_class_init (GtkAlignmentClass *class)
76 GObjectClass *gobject_class;
77 GtkWidgetClass *widget_class;
79 gobject_class = (GObjectClass*) class;
80 widget_class = (GtkWidgetClass*) class;
82 gobject_class->set_property = gtk_alignment_set_property;
83 gobject_class->get_property = gtk_alignment_get_property;
85 widget_class->size_request = gtk_alignment_size_request;
86 widget_class->size_allocate = gtk_alignment_size_allocate;
88 g_object_class_install_property (gobject_class,
90 g_param_spec_float("xalign",
91 P_("Horizontal alignment"),
92 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
96 GTK_PARAM_READWRITE));
98 g_object_class_install_property (gobject_class,
100 g_param_spec_float("yalign",
101 P_("Vertical alignment"),
102 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
106 GTK_PARAM_READWRITE));
107 g_object_class_install_property (gobject_class,
109 g_param_spec_float("xscale",
110 P_("Horizontal scale"),
111 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"),
115 GTK_PARAM_READWRITE));
116 g_object_class_install_property (gobject_class,
118 g_param_spec_float("yscale",
119 P_("Vertical scale"),
120 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"),
124 GTK_PARAM_READWRITE));
128 * GtkAlignment:top-padding:
130 * The padding to insert at the top of the widget.
134 g_object_class_install_property (gobject_class,
136 g_param_spec_uint("top-padding",
138 P_("The padding to insert at the top of the widget."),
142 GTK_PARAM_READWRITE));
145 * GtkAlignment:bottom-padding:
147 * The padding to insert at the bottom of the widget.
151 g_object_class_install_property (gobject_class,
153 g_param_spec_uint("bottom-padding",
154 P_("Bottom Padding"),
155 P_("The padding to insert at the bottom of the widget."),
159 GTK_PARAM_READWRITE));
162 * GtkAlignment:left-padding:
164 * The padding to insert at the left of the widget.
168 g_object_class_install_property (gobject_class,
170 g_param_spec_uint("left-padding",
172 P_("The padding to insert at the left of the widget."),
176 GTK_PARAM_READWRITE));
179 * GtkAlignment:right-padding:
181 * The padding to insert at the right of the widget.
185 g_object_class_install_property (gobject_class,
187 g_param_spec_uint("right-padding",
189 P_("The padding to insert at the right of the widget."),
193 GTK_PARAM_READWRITE));
195 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
199 gtk_alignment_init (GtkAlignment *alignment)
201 GtkAlignmentPrivate *priv;
203 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
204 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
206 alignment->xalign = 0.5;
207 alignment->yalign = 0.5;
208 alignment->xscale = 1.0;
209 alignment->yscale = 1.0;
211 /* Initialize padding with default values: */
212 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
213 priv->padding_top = 0;
214 priv->padding_bottom = 0;
215 priv->padding_left = 0;
216 priv->padding_right = 0;
220 gtk_alignment_new (gfloat xalign,
225 GtkAlignment *alignment;
227 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
229 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
230 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
231 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
232 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
234 return GTK_WIDGET (alignment);
238 gtk_alignment_set_property (GObject *object,
243 GtkAlignment *alignment;
244 GtkAlignmentPrivate *priv;
246 alignment = GTK_ALIGNMENT (object);
247 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
252 gtk_alignment_set (alignment,
253 g_value_get_float (value),
259 gtk_alignment_set (alignment,
261 g_value_get_float (value),
266 gtk_alignment_set (alignment,
269 g_value_get_float (value),
273 gtk_alignment_set (alignment,
277 g_value_get_float (value));
281 case PROP_TOP_PADDING:
282 gtk_alignment_set_padding (alignment,
283 g_value_get_uint (value),
284 priv->padding_bottom,
286 priv->padding_right);
288 case PROP_BOTTOM_PADDING:
289 gtk_alignment_set_padding (alignment,
291 g_value_get_uint (value),
293 priv->padding_right);
295 case PROP_LEFT_PADDING:
296 gtk_alignment_set_padding (alignment,
298 priv->padding_bottom,
299 g_value_get_uint (value),
300 priv->padding_right);
302 case PROP_RIGHT_PADDING:
303 gtk_alignment_set_padding (alignment,
305 priv->padding_bottom,
307 g_value_get_uint (value));
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 gtk_alignment_get_property (GObject *object,
322 GtkAlignment *alignment;
323 GtkAlignmentPrivate *priv;
325 alignment = GTK_ALIGNMENT (object);
326 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
331 g_value_set_float(value, alignment->xalign);
334 g_value_set_float(value, alignment->yalign);
337 g_value_set_float(value, alignment->xscale);
340 g_value_set_float(value, alignment->yscale);
344 case PROP_TOP_PADDING:
345 g_value_set_uint (value, priv->padding_top);
347 case PROP_BOTTOM_PADDING:
348 g_value_set_uint (value, priv->padding_bottom);
350 case PROP_LEFT_PADDING:
351 g_value_set_uint (value, priv->padding_left);
353 case PROP_RIGHT_PADDING:
354 g_value_set_uint (value, priv->padding_right);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 gtk_alignment_set (GtkAlignment *alignment,
370 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
372 xalign = CLAMP (xalign, 0.0, 1.0);
373 yalign = CLAMP (yalign, 0.0, 1.0);
374 xscale = CLAMP (xscale, 0.0, 1.0);
375 yscale = CLAMP (yscale, 0.0, 1.0);
377 if ( (alignment->xalign != xalign)
378 || (alignment->yalign != yalign)
379 || (alignment->xscale != xscale)
380 || (alignment->yscale != yscale))
382 g_object_freeze_notify (G_OBJECT (alignment));
383 if (alignment->xalign != xalign)
385 alignment->xalign = xalign;
386 g_object_notify (G_OBJECT (alignment), "xalign");
388 if (alignment->yalign != yalign)
390 alignment->yalign = yalign;
391 g_object_notify (G_OBJECT (alignment), "yalign");
393 if (alignment->xscale != xscale)
395 alignment->xscale = xscale;
396 g_object_notify (G_OBJECT (alignment), "xscale");
398 if (alignment->yscale != yscale)
400 alignment->yscale = yscale;
401 g_object_notify (G_OBJECT (alignment), "yscale");
403 g_object_thaw_notify (G_OBJECT (alignment));
405 if (GTK_BIN (alignment)->child)
406 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
407 gtk_widget_queue_draw (GTK_WIDGET (alignment));
413 gtk_alignment_size_request (GtkWidget *widget,
414 GtkRequisition *requisition)
417 GtkAlignmentPrivate *priv;
419 bin = GTK_BIN (widget);
420 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
422 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
423 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
425 if (bin->child && gtk_widget_get_visible (bin->child))
427 GtkRequisition child_requisition;
429 gtk_widget_size_request (bin->child, &child_requisition);
431 requisition->width += child_requisition.width;
432 requisition->height += child_requisition.height;
434 /* Request extra space for the padding: */
435 requisition->width += (priv->padding_left + priv->padding_right);
436 requisition->height += (priv->padding_top + priv->padding_bottom);
441 gtk_alignment_size_allocate (GtkWidget *widget,
442 GtkAllocation *allocation)
444 GtkAlignment *alignment;
446 GtkAllocation child_allocation;
447 GtkRequisition child_requisition;
450 gint padding_horizontal, padding_vertical;
451 GtkAlignmentPrivate *priv;
453 padding_horizontal = 0;
454 padding_vertical = 0;
456 widget->allocation = *allocation;
457 alignment = GTK_ALIGNMENT (widget);
458 bin = GTK_BIN (widget);
460 if (bin->child && gtk_widget_get_visible (bin->child))
462 GtkExtendedLayout *layout = GTK_EXTENDED_LAYOUT (bin->child);
464 gtk_extended_layout_get_desired_size (layout, NULL, &child_requisition);
466 border_width = GTK_CONTAINER (alignment)->border_width;
468 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
469 padding_horizontal = priv->padding_left + priv->padding_right;
470 padding_vertical = priv->padding_top + priv->padding_bottom;
472 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
473 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
475 if (child_requisition.width > width)
476 gtk_extended_layout_get_height_for_width (layout, width, NULL,
477 &child_requisition.height);
478 else if (child_requisition.height > height)
479 gtk_extended_layout_get_width_for_height (layout, height, NULL,
480 &child_requisition.width);
482 if (width > child_requisition.width)
483 child_allocation.width = (child_requisition.width *
484 (1.0 - alignment->xscale) +
485 width * alignment->xscale);
487 child_allocation.width = width;
489 if (height > child_requisition.height)
490 child_allocation.height = (child_requisition.height *
491 (1.0 - alignment->yscale) +
492 height * alignment->yscale);
494 child_allocation.height = height;
496 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
497 child_allocation.x = (1.0 - alignment->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
499 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
501 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
503 gtk_widget_size_allocate (bin->child, &child_allocation);
508 * gtk_alignment_set_padding:
509 * @alignment: a #GtkAlignment
510 * @padding_top: the padding at the top of the widget
511 * @padding_bottom: the padding at the bottom of the widget
512 * @padding_left: the padding at the left of the widget
513 * @padding_right: the padding at the right of the widget.
515 * Sets the padding on the different sides of the widget.
516 * The padding adds blank space to the sides of the widget. For instance,
517 * this can be used to indent the child widget towards the right by adding
518 * padding on the left.
523 gtk_alignment_set_padding (GtkAlignment *alignment,
525 guint padding_bottom,
529 GtkAlignmentPrivate *priv;
531 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
533 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
535 g_object_freeze_notify (G_OBJECT (alignment));
537 if (priv->padding_top != padding_top)
539 priv->padding_top = padding_top;
540 g_object_notify (G_OBJECT (alignment), "top-padding");
542 if (priv->padding_bottom != padding_bottom)
544 priv->padding_bottom = padding_bottom;
545 g_object_notify (G_OBJECT (alignment), "bottom-padding");
547 if (priv->padding_left != padding_left)
549 priv->padding_left = padding_left;
550 g_object_notify (G_OBJECT (alignment), "left-padding");
552 if (priv->padding_right != padding_right)
554 priv->padding_right = padding_right;
555 g_object_notify (G_OBJECT (alignment), "right-padding");
558 g_object_thaw_notify (G_OBJECT (alignment));
560 /* Make sure that the widget and children are redrawn with the new setting: */
561 if (GTK_BIN (alignment)->child)
562 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
564 gtk_widget_queue_draw (GTK_WIDGET (alignment));
568 * gtk_alignment_get_padding:
569 * @alignment: a #GtkAlignment
570 * @padding_top: (allow-none): location to store the padding for the top of the widget, or %NULL
571 * @padding_bottom: (allow-none): location to store the padding for the bottom of the widget, or %NULL
572 * @padding_left: (allow-none): location to store the padding for the left of the widget, or %NULL
573 * @padding_right: (allow-none): location to store the padding for the right of the widget, or %NULL
575 * Gets the padding on the different sides of the widget.
576 * See gtk_alignment_set_padding ().
581 gtk_alignment_get_padding (GtkAlignment *alignment,
583 guint *padding_bottom,
585 guint *padding_right)
587 GtkAlignmentPrivate *priv;
589 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
591 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
593 *padding_top = priv->padding_top;
595 *padding_bottom = priv->padding_bottom;
597 *padding_left = priv->padding_left;
599 *padding_right = priv->padding_right;
602 #define __GTK_ALIGNMENT_C__
603 #include "gtkaliasdef.c"