]> Pileus Git - ~andy/gtk/blob - gtk/gtkalignment.c
Use gdk_window_get_origin() instead of gdk_window_get_position, because
[~andy/gtk] / gtk / gtkalignment.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include "gtkalignment.h"
20
21
22 enum {
23   ARG_0,
24   ARG_XALIGN,
25   ARG_YALIGN,
26   ARG_XSCALE,
27   ARG_YSCALE
28 };
29
30
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,
38                                          GtkArg            *arg,
39                                          guint              arg_id);
40 static void gtk_alignment_get_arg       (GtkObject         *object,
41                                          GtkArg            *arg,
42                                          guint              arg_id);
43
44
45
46 GtkType
47 gtk_alignment_get_type (void)
48 {
49   static GtkType alignment_type = 0;
50
51   if (!alignment_type)
52     {
53       static const GtkTypeInfo alignment_info =
54       {
55         "GtkAlignment",
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,
63       };
64
65       alignment_type = gtk_type_unique (GTK_TYPE_BIN, &alignment_info);
66     }
67
68   return alignment_type;
69 }
70
71 static void
72 gtk_alignment_class_init (GtkAlignmentClass *class)
73 {
74   GtkObjectClass *object_class;
75   GtkWidgetClass *widget_class;
76
77   object_class = (GtkObjectClass*) class;
78   widget_class = (GtkWidgetClass*) class;
79
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);
84
85   object_class->set_arg = gtk_alignment_set_arg;
86   object_class->get_arg = gtk_alignment_get_arg;
87
88   widget_class->size_request = gtk_alignment_size_request;
89   widget_class->size_allocate = gtk_alignment_size_allocate;
90 }
91
92 static void
93 gtk_alignment_init (GtkAlignment *alignment)
94 {
95   GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
96
97   alignment->xalign = 0.5;
98   alignment->yalign = 0.5;
99   alignment->xscale = 1.0;
100   alignment->yscale = 1.0;
101 }
102
103 GtkWidget*
104 gtk_alignment_new (gfloat xalign,
105                    gfloat yalign,
106                    gfloat xscale,
107                    gfloat yscale)
108 {
109   GtkAlignment *alignment;
110
111   alignment = gtk_type_new (gtk_alignment_get_type ());
112
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);
117
118   return GTK_WIDGET (alignment);
119 }
120
121 static void
122 gtk_alignment_set_arg (GtkObject         *object,
123                        GtkArg            *arg,
124                        guint              arg_id)
125 {
126   GtkAlignment *alignment;
127
128   alignment = GTK_ALIGNMENT (object);
129
130   switch (arg_id)
131     {
132     case ARG_XALIGN:
133       gtk_alignment_set (alignment,
134                          GTK_VALUE_FLOAT (*arg),
135                          alignment->yalign,
136                          alignment->xscale,
137                          alignment->yscale);
138       break;
139     case ARG_YALIGN:
140       gtk_alignment_set (alignment,
141                          alignment->xalign,
142                          GTK_VALUE_FLOAT (*arg),
143                          alignment->xscale,
144                          alignment->yscale);
145       break;
146     case ARG_XSCALE:
147       gtk_alignment_set (alignment,
148                          alignment->xalign,
149                          alignment->yalign,
150                          GTK_VALUE_FLOAT (*arg),
151                          alignment->yscale);
152       break;
153     case ARG_YSCALE:
154       gtk_alignment_set (alignment,
155                          alignment->xalign,
156                          alignment->yalign,
157                          alignment->xscale,
158                          GTK_VALUE_FLOAT (*arg));
159       break;
160     default:
161       break;
162     }
163 }
164
165 static void
166 gtk_alignment_get_arg (GtkObject         *object,
167                        GtkArg            *arg,
168                        guint              arg_id)
169 {
170   GtkAlignment *alignment;
171
172   alignment = GTK_ALIGNMENT (object);
173
174   switch (arg_id)
175     {
176     case ARG_XALIGN:
177       GTK_VALUE_FLOAT (*arg) = alignment->xalign;
178       break;
179     case ARG_YALIGN:
180       GTK_VALUE_FLOAT (*arg) = alignment->yalign;
181       break;
182     case ARG_XSCALE:
183       GTK_VALUE_FLOAT (*arg) = alignment->xscale;
184       break;
185     case ARG_YSCALE:
186       GTK_VALUE_FLOAT (*arg) = alignment->yscale;
187       break;
188     default:
189       arg->type = GTK_TYPE_INVALID;
190       break;
191     }
192 }
193
194 void
195 gtk_alignment_set (GtkAlignment *alignment,
196                    gfloat        xalign,
197                    gfloat        yalign,
198                    gfloat        xscale,
199                    gfloat        yscale)
200 {
201   g_return_if_fail (alignment != NULL);
202   g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
203
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);
208
209   if ((alignment->xalign != xalign) ||
210       (alignment->yalign != yalign) ||
211       (alignment->xscale != xscale) ||
212       (alignment->yscale != yscale))
213     {
214       alignment->xalign = xalign;
215       alignment->yalign = yalign;
216       alignment->xscale = xscale;
217       alignment->yscale = yscale;
218
219       gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
220       gtk_widget_queue_draw (GTK_WIDGET (alignment));
221     }
222 }
223
224
225 static void
226 gtk_alignment_size_request (GtkWidget      *widget,
227                             GtkRequisition *requisition)
228 {
229   GtkAlignment *alignment;
230   GtkBin *bin;
231
232   g_return_if_fail (widget != NULL);
233   g_return_if_fail (GTK_IS_ALIGNMENT (widget));
234   g_return_if_fail (requisition != NULL);
235
236   alignment = GTK_ALIGNMENT (widget);
237   bin = GTK_BIN (widget);
238
239   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
240   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
241
242   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
243     {
244       gtk_widget_size_request (bin->child, &bin->child->requisition);
245
246       requisition->width += bin->child->requisition.width;
247       requisition->height += bin->child->requisition.height;
248     }
249 }
250
251 static void
252 gtk_alignment_size_allocate (GtkWidget     *widget,
253                              GtkAllocation *allocation)
254 {
255   GtkAlignment *alignment;
256   GtkBin *bin;
257   GtkAllocation child_allocation;
258   gint width, height;
259   gint x, y;
260
261   g_return_if_fail (widget != NULL);
262   g_return_if_fail (GTK_IS_ALIGNMENT (widget));
263   g_return_if_fail (allocation != NULL);
264
265   widget->allocation = *allocation;
266   alignment = GTK_ALIGNMENT (widget);
267   bin = GTK_BIN (widget);
268   
269   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
270     {
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);
275       
276       if (width > bin->child->requisition.width)
277         child_allocation.width = (bin->child->requisition.width *
278                                   (1.0 - alignment->xscale) +
279                                   width * alignment->xscale);
280       else
281         child_allocation.width = width;
282       
283       if (height > bin->child->requisition.height)
284         child_allocation.height = (bin->child->requisition.height *
285                                    (1.0 - alignment->yscale) +
286                                    height * alignment->yscale);
287       else
288         child_allocation.height = height;
289
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;
292
293       gtk_widget_size_allocate (bin->child, &child_allocation);
294     }
295 }