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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
19 #include "gtkalignment.h"
31 static void gtk_alignment_class_init (GtkAlignmentClass *klass);
32 static void gtk_alignment_init (GtkAlignment *alignment);
33 static void gtk_alignment_size_request (GtkWidget *widget,
34 GtkRequisition *requisition);
35 static void gtk_alignment_size_allocate (GtkWidget *widget,
36 GtkAllocation *allocation);
37 static void gtk_alignment_set_arg (GtkObject *object,
40 static void gtk_alignment_get_arg (GtkObject *object,
47 gtk_alignment_get_type (void)
49 static GtkType alignment_type = 0;
53 static const GtkTypeInfo alignment_info =
56 sizeof (GtkAlignment),
57 sizeof (GtkAlignmentClass),
58 (GtkClassInitFunc) gtk_alignment_class_init,
59 (GtkObjectInitFunc) gtk_alignment_init,
60 /* reserved_1 */ NULL,
61 /* reserved_2 */ NULL,
62 (GtkClassInitFunc) NULL,
65 alignment_type = gtk_type_unique (GTK_TYPE_BIN, &alignment_info);
68 return alignment_type;
72 gtk_alignment_class_init (GtkAlignmentClass *class)
74 GtkObjectClass *object_class;
75 GtkWidgetClass *widget_class;
77 object_class = (GtkObjectClass*) class;
78 widget_class = (GtkWidgetClass*) class;
80 gtk_object_add_arg_type ("GtkAlignment::xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_XALIGN);
81 gtk_object_add_arg_type ("GtkAlignment::yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_YALIGN);
82 gtk_object_add_arg_type ("GtkAlignment::xscale", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_XSCALE);
83 gtk_object_add_arg_type ("GtkAlignment::yscale", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_YSCALE);
85 object_class->set_arg = gtk_alignment_set_arg;
86 object_class->get_arg = gtk_alignment_get_arg;
88 widget_class->size_request = gtk_alignment_size_request;
89 widget_class->size_allocate = gtk_alignment_size_allocate;
93 gtk_alignment_init (GtkAlignment *alignment)
95 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
97 alignment->xalign = 0.5;
98 alignment->yalign = 0.5;
99 alignment->xscale = 1.0;
100 alignment->yscale = 1.0;
104 gtk_alignment_new (gfloat xalign,
109 GtkAlignment *alignment;
111 alignment = gtk_type_new (gtk_alignment_get_type ());
113 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
114 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
115 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
116 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
118 return GTK_WIDGET (alignment);
122 gtk_alignment_set_arg (GtkObject *object,
126 GtkAlignment *alignment;
128 alignment = GTK_ALIGNMENT (object);
133 gtk_alignment_set (alignment,
134 GTK_VALUE_FLOAT (*arg),
140 gtk_alignment_set (alignment,
142 GTK_VALUE_FLOAT (*arg),
147 gtk_alignment_set (alignment,
150 GTK_VALUE_FLOAT (*arg),
154 gtk_alignment_set (alignment,
158 GTK_VALUE_FLOAT (*arg));
166 gtk_alignment_get_arg (GtkObject *object,
170 GtkAlignment *alignment;
172 alignment = GTK_ALIGNMENT (object);
177 GTK_VALUE_FLOAT (*arg) = alignment->xalign;
180 GTK_VALUE_FLOAT (*arg) = alignment->yalign;
183 GTK_VALUE_FLOAT (*arg) = alignment->xscale;
186 GTK_VALUE_FLOAT (*arg) = alignment->yscale;
189 arg->type = GTK_TYPE_INVALID;
195 gtk_alignment_set (GtkAlignment *alignment,
201 g_return_if_fail (alignment != NULL);
202 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
204 xalign = CLAMP (xalign, 0.0, 1.0);
205 yalign = CLAMP (yalign, 0.0, 1.0);
206 xscale = CLAMP (xscale, 0.0, 1.0);
207 yscale = CLAMP (yscale, 0.0, 1.0);
209 if ((alignment->xalign != xalign) ||
210 (alignment->yalign != yalign) ||
211 (alignment->xscale != xscale) ||
212 (alignment->yscale != yscale))
214 alignment->xalign = xalign;
215 alignment->yalign = yalign;
216 alignment->xscale = xscale;
217 alignment->yscale = yscale;
219 gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
220 gtk_widget_queue_draw (GTK_WIDGET (alignment));
226 gtk_alignment_size_request (GtkWidget *widget,
227 GtkRequisition *requisition)
229 GtkAlignment *alignment;
232 g_return_if_fail (widget != NULL);
233 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
234 g_return_if_fail (requisition != NULL);
236 alignment = GTK_ALIGNMENT (widget);
237 bin = GTK_BIN (widget);
239 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
240 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
242 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
244 gtk_widget_size_request (bin->child, &bin->child->requisition);
246 requisition->width += bin->child->requisition.width;
247 requisition->height += bin->child->requisition.height;
252 gtk_alignment_size_allocate (GtkWidget *widget,
253 GtkAllocation *allocation)
255 GtkAlignment *alignment;
257 GtkAllocation child_allocation;
261 g_return_if_fail (widget != NULL);
262 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
263 g_return_if_fail (allocation != NULL);
265 widget->allocation = *allocation;
266 alignment = GTK_ALIGNMENT (widget);
267 bin = GTK_BIN (widget);
269 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
271 x = GTK_CONTAINER (alignment)->border_width;
272 y = GTK_CONTAINER (alignment)->border_width;
273 width = MAX (allocation->width - 2 * x, 0);
274 height = MAX (allocation->height - 2 * y, 0);
276 if (width > bin->child->requisition.width)
277 child_allocation.width = (bin->child->requisition.width *
278 (1.0 - alignment->xscale) +
279 width * alignment->xscale);
281 child_allocation.width = width;
283 if (height > bin->child->requisition.height)
284 child_allocation.height = (bin->child->requisition.height *
285 (1.0 - alignment->yscale) +
286 height * alignment->yscale);
288 child_allocation.height = height;
290 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
291 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
293 gtk_widget_size_allocate (bin->child, &child_allocation);