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.
21 * Modified by the GTK+ Team and others 1997-1999. 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"
39 static void gtk_alignment_class_init (GtkAlignmentClass *klass);
40 static void gtk_alignment_init (GtkAlignment *alignment);
41 static void gtk_alignment_size_request (GtkWidget *widget,
42 GtkRequisition *requisition);
43 static void gtk_alignment_size_allocate (GtkWidget *widget,
44 GtkAllocation *allocation);
45 static void gtk_alignment_set_arg (GtkObject *object,
48 static void gtk_alignment_get_arg (GtkObject *object,
55 gtk_alignment_get_type (void)
57 static GtkType alignment_type = 0;
61 static const GtkTypeInfo alignment_info =
64 sizeof (GtkAlignment),
65 sizeof (GtkAlignmentClass),
66 (GtkClassInitFunc) gtk_alignment_class_init,
67 (GtkObjectInitFunc) gtk_alignment_init,
68 /* reserved_1 */ NULL,
69 /* reserved_2 */ NULL,
70 (GtkClassInitFunc) NULL,
73 alignment_type = gtk_type_unique (GTK_TYPE_BIN, &alignment_info);
76 return alignment_type;
80 gtk_alignment_class_init (GtkAlignmentClass *class)
82 GtkObjectClass *object_class;
83 GtkWidgetClass *widget_class;
85 object_class = (GtkObjectClass*) class;
86 widget_class = (GtkWidgetClass*) class;
88 gtk_object_add_arg_type ("GtkAlignment::xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_XALIGN);
89 gtk_object_add_arg_type ("GtkAlignment::yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_YALIGN);
90 gtk_object_add_arg_type ("GtkAlignment::xscale", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_XSCALE);
91 gtk_object_add_arg_type ("GtkAlignment::yscale", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_YSCALE);
93 object_class->set_arg = gtk_alignment_set_arg;
94 object_class->get_arg = gtk_alignment_get_arg;
96 widget_class->size_request = gtk_alignment_size_request;
97 widget_class->size_allocate = gtk_alignment_size_allocate;
101 gtk_alignment_init (GtkAlignment *alignment)
103 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
105 alignment->xalign = 0.5;
106 alignment->yalign = 0.5;
107 alignment->xscale = 1.0;
108 alignment->yscale = 1.0;
112 gtk_alignment_new (gfloat xalign,
117 GtkAlignment *alignment;
119 alignment = gtk_type_new (gtk_alignment_get_type ());
121 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
122 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
123 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
124 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
126 return GTK_WIDGET (alignment);
130 gtk_alignment_set_arg (GtkObject *object,
134 GtkAlignment *alignment;
136 alignment = GTK_ALIGNMENT (object);
141 gtk_alignment_set (alignment,
142 GTK_VALUE_FLOAT (*arg),
148 gtk_alignment_set (alignment,
150 GTK_VALUE_FLOAT (*arg),
155 gtk_alignment_set (alignment,
158 GTK_VALUE_FLOAT (*arg),
162 gtk_alignment_set (alignment,
166 GTK_VALUE_FLOAT (*arg));
174 gtk_alignment_get_arg (GtkObject *object,
178 GtkAlignment *alignment;
180 alignment = GTK_ALIGNMENT (object);
185 GTK_VALUE_FLOAT (*arg) = alignment->xalign;
188 GTK_VALUE_FLOAT (*arg) = alignment->yalign;
191 GTK_VALUE_FLOAT (*arg) = alignment->xscale;
194 GTK_VALUE_FLOAT (*arg) = alignment->yscale;
197 arg->type = GTK_TYPE_INVALID;
203 gtk_alignment_set (GtkAlignment *alignment,
209 g_return_if_fail (alignment != NULL);
210 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
212 xalign = CLAMP (xalign, 0.0, 1.0);
213 yalign = CLAMP (yalign, 0.0, 1.0);
214 xscale = CLAMP (xscale, 0.0, 1.0);
215 yscale = CLAMP (yscale, 0.0, 1.0);
217 if ((alignment->xalign != xalign) ||
218 (alignment->yalign != yalign) ||
219 (alignment->xscale != xscale) ||
220 (alignment->yscale != yscale))
222 alignment->xalign = xalign;
223 alignment->yalign = yalign;
224 alignment->xscale = xscale;
225 alignment->yscale = yscale;
227 gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
228 gtk_widget_queue_draw (GTK_WIDGET (alignment));
234 gtk_alignment_size_request (GtkWidget *widget,
235 GtkRequisition *requisition)
237 GtkAlignment *alignment;
240 g_return_if_fail (widget != NULL);
241 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
242 g_return_if_fail (requisition != NULL);
244 alignment = GTK_ALIGNMENT (widget);
245 bin = GTK_BIN (widget);
247 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
248 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
250 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
252 GtkRequisition child_requisition;
254 gtk_widget_size_request (bin->child, &child_requisition);
256 requisition->width += child_requisition.width;
257 requisition->height += child_requisition.height;
262 gtk_alignment_size_allocate (GtkWidget *widget,
263 GtkAllocation *allocation)
265 GtkAlignment *alignment;
267 GtkAllocation child_allocation;
268 GtkRequisition child_requisition;
272 g_return_if_fail (widget != NULL);
273 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
274 g_return_if_fail (allocation != NULL);
276 widget->allocation = *allocation;
277 alignment = GTK_ALIGNMENT (widget);
278 bin = GTK_BIN (widget);
280 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
282 gtk_widget_get_child_requisition (bin->child, &child_requisition);
284 x = GTK_CONTAINER (alignment)->border_width;
285 y = GTK_CONTAINER (alignment)->border_width;
286 width = MAX (allocation->width - 2 * x, 0);
287 height = MAX (allocation->height - 2 * y, 0);
289 if (width > child_requisition.width)
290 child_allocation.width = (child_requisition.width *
291 (1.0 - alignment->xscale) +
292 width * alignment->xscale);
294 child_allocation.width = width;
296 if (height > child_requisition.height)
297 child_allocation.height = (child_requisition.height *
298 (1.0 - alignment->yscale) +
299 height * alignment->yscale);
301 child_allocation.height = height;
303 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
304 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
306 gtk_widget_size_allocate (bin->child, &child_allocation);