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/.
27 #include "gtkalignment.h"
42 static void gtk_alignment_class_init (GtkAlignmentClass *klass);
43 static void gtk_alignment_init (GtkAlignment *alignment);
44 static void gtk_alignment_size_request (GtkWidget *widget,
45 GtkRequisition *requisition);
46 static void gtk_alignment_size_allocate (GtkWidget *widget,
47 GtkAllocation *allocation);
48 static void gtk_alignment_set_property (GObject *object,
52 static void gtk_alignment_get_property (GObject *object,
58 gtk_alignment_get_type (void)
60 static GtkType alignment_type = 0;
64 static const GtkTypeInfo alignment_info =
67 sizeof (GtkAlignment),
68 sizeof (GtkAlignmentClass),
69 (GtkClassInitFunc) gtk_alignment_class_init,
70 (GtkObjectInitFunc) gtk_alignment_init,
71 /* reserved_1 */ NULL,
72 /* reserved_2 */ NULL,
73 (GtkClassInitFunc) NULL,
76 alignment_type = gtk_type_unique (GTK_TYPE_BIN, &alignment_info);
79 return alignment_type;
83 gtk_alignment_class_init (GtkAlignmentClass *class)
85 GObjectClass *gobject_class;
86 GtkObjectClass *object_class;
87 GtkWidgetClass *widget_class;
89 gobject_class = (GObjectClass*) class;
90 object_class = (GtkObjectClass*) class;
91 widget_class = (GtkWidgetClass*) class;
93 gobject_class->set_property = gtk_alignment_set_property;
94 gobject_class->get_property = gtk_alignment_get_property;
96 widget_class->size_request = gtk_alignment_size_request;
97 widget_class->size_allocate = gtk_alignment_size_allocate;
99 g_object_class_install_property (gobject_class,
101 g_param_spec_float("xalign",
102 _("Horizontal alignment"),
103 _("Value between 0.0 and 1.0 to indicate X alignment"),
107 G_PARAM_READABLE | G_PARAM_WRITABLE));
109 g_object_class_install_property (gobject_class,
111 g_param_spec_float("yalign",
112 _("Vertical alignment"),
113 _("Value between 0.0 and 1.0 to indicate Y alignment"),
117 G_PARAM_READABLE | G_PARAM_WRITABLE));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_float("xscale",
121 _("Horizontal scale"),
122 _("Value between 0.0 and 1.0 to indicate X scale"),
126 G_PARAM_READABLE | G_PARAM_WRITABLE));
127 g_object_class_install_property (gobject_class,
129 g_param_spec_float("yscale",
131 _("Value between 0.0 and 1.0 to indicate Y scale"),
135 G_PARAM_READABLE | G_PARAM_WRITABLE));
139 gtk_alignment_init (GtkAlignment *alignment)
141 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
143 alignment->xalign = 0.5;
144 alignment->yalign = 0.5;
145 alignment->xscale = 1.0;
146 alignment->yscale = 1.0;
150 gtk_alignment_new (gfloat xalign,
155 GtkAlignment *alignment;
157 alignment = gtk_type_new (gtk_alignment_get_type ());
159 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
160 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
161 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
162 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
164 return GTK_WIDGET (alignment);
168 gtk_alignment_set_property (GObject *object,
173 GtkAlignment *alignment;
175 alignment = GTK_ALIGNMENT (object);
180 gtk_alignment_set (alignment,
181 g_value_get_float(value),
187 gtk_alignment_set (alignment,
189 g_value_get_float(value),
194 gtk_alignment_set (alignment,
197 g_value_get_float(value),
201 gtk_alignment_set (alignment,
205 g_value_get_float(value));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gtk_alignment_get_property (GObject *object,
219 GtkAlignment *alignment;
221 alignment = GTK_ALIGNMENT (object);
226 g_value_set_float(value, alignment->xalign);
229 g_value_set_float(value, alignment->yalign);
232 g_value_set_float(value, alignment->xscale);
235 g_value_set_float(value, alignment->yscale);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 gtk_alignment_set (GtkAlignment *alignment,
250 gboolean values_changed = FALSE;
252 g_return_if_fail (alignment != NULL);
253 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
255 xalign = CLAMP (xalign, 0.0, 1.0);
256 yalign = CLAMP (yalign, 0.0, 1.0);
257 xscale = CLAMP (xscale, 0.0, 1.0);
258 yscale = CLAMP (yscale, 0.0, 1.0);
260 if (alignment->xalign != xalign)
262 values_changed = TRUE;
263 alignment->xalign = xalign;
264 g_object_notify (G_OBJECT(alignment), "xalign");
266 if (alignment->yalign != yalign)
268 values_changed = TRUE;
269 alignment->yalign = yalign;
270 g_object_notify (G_OBJECT(alignment), "yalign");
272 if (alignment->xscale != xscale)
274 values_changed = TRUE;
275 alignment->xscale = xscale;
276 g_object_notify (G_OBJECT(alignment), "xscale");
278 if (alignment->yscale != yscale)
280 values_changed = TRUE;
281 alignment->yscale = yscale;
282 g_object_notify (G_OBJECT(alignment), "yscale");
285 if (values_changed == TRUE)
287 gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
288 gtk_widget_queue_draw (GTK_WIDGET (alignment));
294 gtk_alignment_size_request (GtkWidget *widget,
295 GtkRequisition *requisition)
297 GtkAlignment *alignment;
300 g_return_if_fail (widget != NULL);
301 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
302 g_return_if_fail (requisition != NULL);
304 alignment = GTK_ALIGNMENT (widget);
305 bin = GTK_BIN (widget);
307 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
308 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
310 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
312 GtkRequisition child_requisition;
314 gtk_widget_size_request (bin->child, &child_requisition);
316 requisition->width += child_requisition.width;
317 requisition->height += child_requisition.height;
322 gtk_alignment_size_allocate (GtkWidget *widget,
323 GtkAllocation *allocation)
325 GtkAlignment *alignment;
327 GtkAllocation child_allocation;
328 GtkRequisition child_requisition;
332 g_return_if_fail (widget != NULL);
333 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
334 g_return_if_fail (allocation != NULL);
336 widget->allocation = *allocation;
337 alignment = GTK_ALIGNMENT (widget);
338 bin = GTK_BIN (widget);
340 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
342 gtk_widget_get_child_requisition (bin->child, &child_requisition);
344 x = GTK_CONTAINER (alignment)->border_width;
345 y = GTK_CONTAINER (alignment)->border_width;
346 width = MAX (allocation->width - 2 * x, 0);
347 height = MAX (allocation->height - 2 * y, 0);
349 if (width > child_requisition.width)
350 child_allocation.width = (child_requisition.width *
351 (1.0 - alignment->xscale) +
352 width * alignment->xscale);
354 child_allocation.width = width;
356 if (height > child_requisition.height)
357 child_allocation.height = (child_requisition.height *
358 (1.0 - alignment->yscale) +
359 height * alignment->yscale);
361 child_allocation.height = height;
363 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
364 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
366 gtk_widget_size_allocate (bin->child, &child_allocation);