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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "gtkalignment.h"
21 static void gtk_alignment_class_init (GtkAlignmentClass *klass);
22 static void gtk_alignment_init (GtkAlignment *alignment);
23 static void gtk_alignment_size_request (GtkWidget *widget,
24 GtkRequisition *requisition);
25 static void gtk_alignment_size_allocate (GtkWidget *widget,
26 GtkAllocation *allocation);
30 gtk_alignment_get_type ()
32 static guint alignment_type = 0;
36 GtkTypeInfo alignment_info =
39 sizeof (GtkAlignment),
40 sizeof (GtkAlignmentClass),
41 (GtkClassInitFunc) gtk_alignment_class_init,
42 (GtkObjectInitFunc) gtk_alignment_init,
46 alignment_type = gtk_type_unique (gtk_bin_get_type (), &alignment_info);
49 return alignment_type;
53 gtk_alignment_class_init (GtkAlignmentClass *class)
55 GtkWidgetClass *widget_class;
57 widget_class = (GtkWidgetClass*) class;
59 widget_class->size_request = gtk_alignment_size_request;
60 widget_class->size_allocate = gtk_alignment_size_allocate;
64 gtk_alignment_init (GtkAlignment *alignment)
66 GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW | GTK_BASIC);
68 alignment->xalign = 0.5;
69 alignment->yalign = 0.5;
70 alignment->xscale = 1.0;
71 alignment->yscale = 1.0;
75 gtk_alignment_new (gfloat xalign,
80 GtkAlignment *alignment;
82 alignment = gtk_type_new (gtk_alignment_get_type ());
84 alignment->xalign = CLAMP (xalign, 0.0, 1.0);
85 alignment->yalign = CLAMP (yalign, 0.0, 1.0);
86 alignment->xscale = CLAMP (xscale, 0.0, 1.0);
87 alignment->yscale = CLAMP (yscale, 0.0, 1.0);
89 return GTK_WIDGET (alignment);
93 gtk_alignment_set (GtkAlignment *alignment,
99 g_return_if_fail (alignment != NULL);
100 g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
102 xalign = CLAMP (xalign, 0.0, 1.0);
103 yalign = CLAMP (yalign, 0.0, 1.0);
104 xscale = CLAMP (xscale, 0.0, 1.0);
105 yscale = CLAMP (yscale, 0.0, 1.0);
107 if ((alignment->xalign != xalign) ||
108 (alignment->yalign != yalign) ||
109 (alignment->xscale != xscale) ||
110 (alignment->yscale != yscale))
112 alignment->xalign = xalign;
113 alignment->yalign = yalign;
114 alignment->xscale = xscale;
115 alignment->yscale = yscale;
117 gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
118 gtk_widget_queue_draw (GTK_WIDGET (alignment));
124 gtk_alignment_size_request (GtkWidget *widget,
125 GtkRequisition *requisition)
127 GtkAlignment *alignment;
130 g_return_if_fail (widget != NULL);
131 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
132 g_return_if_fail (requisition != NULL);
134 alignment = GTK_ALIGNMENT (widget);
135 bin = GTK_BIN (widget);
137 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
138 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
140 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
142 gtk_widget_size_request (bin->child, &bin->child->requisition);
144 requisition->width += bin->child->requisition.width;
145 requisition->height += bin->child->requisition.height;
150 gtk_alignment_size_allocate (GtkWidget *widget,
151 GtkAllocation *allocation)
153 GtkAlignment *alignment;
155 GtkAllocation child_allocation;
159 g_return_if_fail (widget != NULL);
160 g_return_if_fail (GTK_IS_ALIGNMENT (widget));
161 g_return_if_fail (allocation != NULL);
163 widget->allocation = *allocation;
164 alignment = GTK_ALIGNMENT (widget);
165 bin = GTK_BIN (widget);
167 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
169 x = GTK_CONTAINER (alignment)->border_width;
170 y = GTK_CONTAINER (alignment)->border_width;
171 width = allocation->width - 2 * x;
172 height = allocation->height - 2 * y;
174 if (width > bin->child->requisition.width)
175 child_allocation.width = (bin->child->requisition.width *
176 (1.0 - alignment->xscale) +
177 width * alignment->xscale);
179 child_allocation.width = width;
181 if (height > bin->child->requisition.height)
182 child_allocation.height = (bin->child->requisition.height *
183 (1.0 - alignment->yscale) +
184 height * alignment->yscale);
186 child_allocation.height = height;
188 child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
189 child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
191 gtk_widget_size_allocate (bin->child, &child_allocation);