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"
47 #define GTK_ALIGNMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ALIGNMENT, GtkAlignmentPrivate))
49 struct _GtkAlignmentPrivate
57 static void gtk_alignment_size_request (GtkWidget *widget,
58 GtkRequisition *requisition);
59 static void gtk_alignment_size_allocate (GtkWidget *widget,
60 GtkAllocation *allocation);
61 static void gtk_alignment_set_property (GObject *object,
65 static void gtk_alignment_get_property (GObject *object,
70 G_DEFINE_TYPE (GtkAlignment, gtk_alignment, GTK_TYPE_BIN)
73 gtk_alignment_class_init (GtkAlignmentClass *class)
75 GObjectClass *gobject_class;
76 GtkWidgetClass *widget_class;
78 gobject_class = (GObjectClass*) class;
79 widget_class = (GtkWidgetClass*) class;
81 gobject_class->set_property = gtk_alignment_set_property;
82 gobject_class->get_property = gtk_alignment_get_property;
84 widget_class->size_request = gtk_alignment_size_request;
85 widget_class->size_allocate = gtk_alignment_size_allocate;
87 g_object_class_install_property (gobject_class,
89 g_param_spec_float("xalign",
90 P_("Horizontal alignment"),
91 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
95 GTK_PARAM_READWRITE));
97 g_object_class_install_property (gobject_class,
99 g_param_spec_float("yalign",
100 P_("Vertical alignment"),
101 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
105 GTK_PARAM_READWRITE));
106 g_object_class_install_property (gobject_class,
108 g_param_spec_float("xscale",
109 P_("Horizontal scale"),
110 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"),
114 GTK_PARAM_READWRITE));
115 g_object_class_install_property (gobject_class,
117 g_param_spec_float("yscale",
118 P_("Vertical scale"),
119 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"),
123 GTK_PARAM_READWRITE));
127 * GtkAlignment:top-padding:
129 * The padding to insert at the top of the widget.
133 g_object_class_install_property (gobject_class,
135 g_param_spec_uint("top-padding",
137 P_("The padding to insert at the top of the widget."),
141 GTK_PARAM_READWRITE));
144 * GtkAlignment:bottom-padding:
146 * The padding to insert at the bottom of the widget.
150 g_object_class_install_property (gobject_class,
152 g_param_spec_uint("bottom-padding",
153 P_("Bottom Padding"),
154 P_("The padding to insert at the bottom of the widget."),
158 GTK_PARAM_READWRITE));
161 * GtkAlignment:left-padding:
163 * The padding to insert at the left of the widget.
167 g_object_class_install_property (gobject_class,
169 g_param_spec_uint("left-padding",
171 P_("The padding to insert at the left of the widget."),
175 GTK_PARAM_READWRITE));
178 * GtkAlignment:right-padding:
180 * The padding to insert at the right of the widget.
184 g_object_class_install_property (gobject_class,
186 g_param_spec_uint("right-padding",
188 P_("The padding to insert at the right of the widget."),
192 GTK_PARAM_READWRITE));
194 g_type_class_add_private (gobject_class, sizeof (GtkAlignmentPrivate));
198 gtk_alignment_init (GtkAlignment *alignment)
200 GtkAlignmentPrivate *priv;
202 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
203 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (alignment), FALSE);
205 alignment->xalign = 0.5;
206 alignment->yalign = 0.5;
207 alignment->xscale = 1.0;
208 alignment->yscale = 1.0;
210 /* Initialize padding with default values: */
211 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
212 priv->padding_top = 0;
213 priv->padding_bottom = 0;
214 priv->padding_left = 0;
215 priv->padding_right = 0;
219 gtk_alignment_new (gfloat xalign,
224 GtkAlignment *alignment;
226 alignment = g_object_new (GTK_TYPE_ALIGNMENT, NULL);
228 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
229 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
230 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
231 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
233 return GTK_WIDGET (alignment);
237 gtk_alignment_set_property (GObject *object,
242 GtkAlignment *alignment;
243 GtkAlignmentPrivate *priv;
245 alignment = GTK_ALIGNMENT (object);
246 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
251 gtk_alignment_set (alignment,
252 g_value_get_float (value),
258 gtk_alignment_set (alignment,
260 g_value_get_float (value),
265 gtk_alignment_set (alignment,
268 g_value_get_float (value),
272 gtk_alignment_set (alignment,
276 g_value_get_float (value));
280 case PROP_TOP_PADDING:
281 gtk_alignment_set_padding (alignment,
282 g_value_get_uint (value),
283 priv->padding_bottom,
285 priv->padding_right);
287 case PROP_BOTTOM_PADDING:
288 gtk_alignment_set_padding (alignment,
290 g_value_get_uint (value),
292 priv->padding_right);
294 case PROP_LEFT_PADDING:
295 gtk_alignment_set_padding (alignment,
297 priv->padding_bottom,
298 g_value_get_uint (value),
299 priv->padding_right);
301 case PROP_RIGHT_PADDING:
302 gtk_alignment_set_padding (alignment,
304 priv->padding_bottom,
306 g_value_get_uint (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gtk_alignment_get_property (GObject *object,
321 GtkAlignment *alignment;
322 GtkAlignmentPrivate *priv;
324 alignment = GTK_ALIGNMENT (object);
325 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
330 g_value_set_float(value, alignment->xalign);
333 g_value_set_float(value, alignment->yalign);
336 g_value_set_float(value, alignment->xscale);
339 g_value_set_float(value, alignment->yscale);
343 case PROP_TOP_PADDING:
344 g_value_set_uint (value, priv->padding_top);
346 case PROP_BOTTOM_PADDING:
347 g_value_set_uint (value, priv->padding_bottom);
349 case PROP_LEFT_PADDING:
350 g_value_set_uint (value, priv->padding_left);
352 case PROP_RIGHT_PADDING:
353 g_value_set_uint (value, priv->padding_right);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gtk_alignment_set (GtkAlignment *alignment,
369 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
371 xalign = CLAMP (xalign, 0.0, 1.0);
372 yalign = CLAMP (yalign, 0.0, 1.0);
373 xscale = CLAMP (xscale, 0.0, 1.0);
374 yscale = CLAMP (yscale, 0.0, 1.0);
376 if ( (alignment->xalign != xalign)
377 || (alignment->yalign != yalign)
378 || (alignment->xscale != xscale)
379 || (alignment->yscale != yscale))
381 g_object_freeze_notify (G_OBJECT (alignment));
382 if (alignment->xalign != xalign)
384 alignment->xalign = xalign;
385 g_object_notify (G_OBJECT (alignment), "xalign");
387 if (alignment->yalign != yalign)
389 alignment->yalign = yalign;
390 g_object_notify (G_OBJECT (alignment), "yalign");
392 if (alignment->xscale != xscale)
394 alignment->xscale = xscale;
395 g_object_notify (G_OBJECT (alignment), "xscale");
397 if (alignment->yscale != yscale)
399 alignment->yscale = yscale;
400 g_object_notify (G_OBJECT (alignment), "yscale");
402 g_object_thaw_notify (G_OBJECT (alignment));
404 if (GTK_BIN (alignment)->child)
405 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
406 gtk_widget_queue_draw (GTK_WIDGET (alignment));
412 gtk_alignment_size_request (GtkWidget *widget,
413 GtkRequisition *requisition)
416 GtkAlignmentPrivate *priv;
418 bin = GTK_BIN (widget);
419 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
421 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
422 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
424 if (bin->child && gtk_widget_get_visible (bin->child))
426 GtkRequisition child_requisition;
428 gtk_widget_size_request (bin->child, &child_requisition);
430 requisition->width += child_requisition.width;
431 requisition->height += child_requisition.height;
433 /* Request extra space for the padding: */
434 requisition->width += (priv->padding_left + priv->padding_right);
435 requisition->height += (priv->padding_top + priv->padding_bottom);
440 gtk_alignment_size_allocate (GtkWidget *widget,
441 GtkAllocation *allocation)
443 GtkAlignment *alignment;
445 GtkAllocation child_allocation;
446 GtkRequisition child_requisition;
449 gint padding_horizontal, padding_vertical;
450 GtkAlignmentPrivate *priv;
452 padding_horizontal = 0;
453 padding_vertical = 0;
455 widget->allocation = *allocation;
456 alignment = GTK_ALIGNMENT (widget);
457 bin = GTK_BIN (widget);
459 if (bin->child && gtk_widget_get_visible (bin->child))
461 gtk_widget_get_child_requisition (bin->child, &child_requisition);
463 border_width = GTK_CONTAINER (alignment)->border_width;
465 priv = GTK_ALIGNMENT_GET_PRIVATE (widget);
466 padding_horizontal = priv->padding_left + priv->padding_right;
467 padding_vertical = priv->padding_top + priv->padding_bottom;
469 width = MAX (1, allocation->width - padding_horizontal - 2 * border_width);
470 height = MAX (1, allocation->height - padding_vertical - 2 * border_width);
472 if (width > child_requisition.width)
473 child_allocation.width = (child_requisition.width *
474 (1.0 - alignment->xscale) +
475 width * alignment->xscale);
477 child_allocation.width = width;
479 if (height > child_requisition.height)
480 child_allocation.height = (child_requisition.height *
481 (1.0 - alignment->yscale) +
482 height * alignment->yscale);
484 child_allocation.height = height;
486 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
487 child_allocation.x = (1.0 - alignment->xalign) * (width - child_allocation.width) + allocation->x + border_width + priv->padding_right;
489 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + border_width + priv->padding_left;
491 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + border_width + priv->padding_top;
493 gtk_widget_size_allocate (bin->child, &child_allocation);
498 * gtk_alignment_set_padding:
499 * @alignment: a #GtkAlignment
500 * @padding_top: the padding at the top of the widget
501 * @padding_bottom: the padding at the bottom of the widget
502 * @padding_left: the padding at the left of the widget
503 * @padding_right: the padding at the right of the widget.
505 * Sets the padding on the different sides of the widget.
506 * The padding adds blank space to the sides of the widget. For instance,
507 * this can be used to indent the child widget towards the right by adding
508 * padding on the left.
513 gtk_alignment_set_padding (GtkAlignment *alignment,
515 guint padding_bottom,
519 GtkAlignmentPrivate *priv;
521 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
523 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
525 g_object_freeze_notify (G_OBJECT (alignment));
527 if (priv->padding_top != padding_top)
529 priv->padding_top = padding_top;
530 g_object_notify (G_OBJECT (alignment), "top-padding");
532 if (priv->padding_bottom != padding_bottom)
534 priv->padding_bottom = padding_bottom;
535 g_object_notify (G_OBJECT (alignment), "bottom-padding");
537 if (priv->padding_left != padding_left)
539 priv->padding_left = padding_left;
540 g_object_notify (G_OBJECT (alignment), "left-padding");
542 if (priv->padding_right != padding_right)
544 priv->padding_right = padding_right;
545 g_object_notify (G_OBJECT (alignment), "right-padding");
548 g_object_thaw_notify (G_OBJECT (alignment));
550 /* Make sure that the widget and children are redrawn with the new setting: */
551 if (GTK_BIN (alignment)->child)
552 gtk_widget_queue_resize (GTK_BIN (alignment)->child);
554 gtk_widget_queue_draw (GTK_WIDGET (alignment));
558 * gtk_alignment_get_padding:
559 * @alignment: a #GtkAlignment
560 * @padding_top: (allow-none): location to store the padding for the top of the widget, or %NULL
561 * @padding_bottom: (allow-none): location to store the padding for the bottom of the widget, or %NULL
562 * @padding_left: (allow-none): location to store the padding for the left of the widget, or %NULL
563 * @padding_right: (allow-none): location to store the padding for the right of the widget, or %NULL
565 * Gets the padding on the different sides of the widget.
566 * See gtk_alignment_set_padding ().
571 gtk_alignment_get_padding (GtkAlignment *alignment,
573 guint *padding_bottom,
575 guint *padding_right)
577 GtkAlignmentPrivate *priv;
579 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
581 priv = GTK_ALIGNMENT_GET_PRIVATE (alignment);
583 *padding_top = priv->padding_top;
585 *padding_bottom = priv->padding_bottom;
587 *padding_left = priv->padding_left;
589 *padding_right = priv->padding_right;
592 #define __GTK_ALIGNMENT_C__
593 #include "gtkaliasdef.c"