source: trunk/third/gtk/gtk/gtkalignment.c @ 14482

Revision 14482, 8.3 KB checked in by ghudson, 25 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r14481, which included commits to RCS files with non-trunk default branches.
RevLine 
[14481]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
20/*
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/.
25 */
26
27#include "gtkalignment.h"
28
29
30enum {
31  ARG_0,
32  ARG_XALIGN,
33  ARG_YALIGN,
34  ARG_XSCALE,
35  ARG_YSCALE
36};
37
38
39static void gtk_alignment_class_init    (GtkAlignmentClass *klass);
40static void gtk_alignment_init          (GtkAlignment      *alignment);
41static void gtk_alignment_size_request  (GtkWidget         *widget,
42                                         GtkRequisition    *requisition);
43static void gtk_alignment_size_allocate (GtkWidget         *widget,
44                                         GtkAllocation     *allocation);
45static void gtk_alignment_set_arg       (GtkObject         *object,
46                                         GtkArg            *arg,
47                                         guint              arg_id);
48static void gtk_alignment_get_arg       (GtkObject         *object,
49                                         GtkArg            *arg,
50                                         guint              arg_id);
51
52
53
54GtkType
55gtk_alignment_get_type (void)
56{
57  static GtkType alignment_type = 0;
58
59  if (!alignment_type)
60    {
61      static const GtkTypeInfo alignment_info =
62      {
63        "GtkAlignment",
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,
71      };
72
73      alignment_type = gtk_type_unique (GTK_TYPE_BIN, &alignment_info);
74    }
75
76  return alignment_type;
77}
78
79static void
80gtk_alignment_class_init (GtkAlignmentClass *class)
81{
82  GtkObjectClass *object_class;
83  GtkWidgetClass *widget_class;
84
85  object_class = (GtkObjectClass*) class;
86  widget_class = (GtkWidgetClass*) class;
87
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);
92
93  object_class->set_arg = gtk_alignment_set_arg;
94  object_class->get_arg = gtk_alignment_get_arg;
95
96  widget_class->size_request = gtk_alignment_size_request;
97  widget_class->size_allocate = gtk_alignment_size_allocate;
98}
99
100static void
101gtk_alignment_init (GtkAlignment *alignment)
102{
103  GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW);
104
105  alignment->xalign = 0.5;
106  alignment->yalign = 0.5;
107  alignment->xscale = 1.0;
108  alignment->yscale = 1.0;
109}
110
111GtkWidget*
112gtk_alignment_new (gfloat xalign,
113                   gfloat yalign,
114                   gfloat xscale,
115                   gfloat yscale)
116{
117  GtkAlignment *alignment;
118
119  alignment = gtk_type_new (gtk_alignment_get_type ());
120
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);
125
126  return GTK_WIDGET (alignment);
127}
128
129static void
130gtk_alignment_set_arg (GtkObject         *object,
131                       GtkArg            *arg,
132                       guint              arg_id)
133{
134  GtkAlignment *alignment;
135
136  alignment = GTK_ALIGNMENT (object);
137
138  switch (arg_id)
139    {
140    case ARG_XALIGN:
141      gtk_alignment_set (alignment,
142                         GTK_VALUE_FLOAT (*arg),
143                         alignment->yalign,
144                         alignment->xscale,
145                         alignment->yscale);
146      break;
147    case ARG_YALIGN:
148      gtk_alignment_set (alignment,
149                         alignment->xalign,
150                         GTK_VALUE_FLOAT (*arg),
151                         alignment->xscale,
152                         alignment->yscale);
153      break;
154    case ARG_XSCALE:
155      gtk_alignment_set (alignment,
156                         alignment->xalign,
157                         alignment->yalign,
158                         GTK_VALUE_FLOAT (*arg),
159                         alignment->yscale);
160      break;
161    case ARG_YSCALE:
162      gtk_alignment_set (alignment,
163                         alignment->xalign,
164                         alignment->yalign,
165                         alignment->xscale,
166                         GTK_VALUE_FLOAT (*arg));
167      break;
168    default:
169      break;
170    }
171}
172
173static void
174gtk_alignment_get_arg (GtkObject         *object,
175                       GtkArg            *arg,
176                       guint              arg_id)
177{
178  GtkAlignment *alignment;
179
180  alignment = GTK_ALIGNMENT (object);
181
182  switch (arg_id)
183    {
184    case ARG_XALIGN:
185      GTK_VALUE_FLOAT (*arg) = alignment->xalign;
186      break;
187    case ARG_YALIGN:
188      GTK_VALUE_FLOAT (*arg) = alignment->yalign;
189      break;
190    case ARG_XSCALE:
191      GTK_VALUE_FLOAT (*arg) = alignment->xscale;
192      break;
193    case ARG_YSCALE:
194      GTK_VALUE_FLOAT (*arg) = alignment->yscale;
195      break;
196    default:
197      arg->type = GTK_TYPE_INVALID;
198      break;
199    }
200}
201
202void
203gtk_alignment_set (GtkAlignment *alignment,
204                   gfloat        xalign,
205                   gfloat        yalign,
206                   gfloat        xscale,
207                   gfloat        yscale)
208{
209  g_return_if_fail (alignment != NULL);
210  g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
211
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);
216
217  if ((alignment->xalign != xalign) ||
218      (alignment->yalign != yalign) ||
219      (alignment->xscale != xscale) ||
220      (alignment->yscale != yscale))
221    {
222      alignment->xalign = xalign;
223      alignment->yalign = yalign;
224      alignment->xscale = xscale;
225      alignment->yscale = yscale;
226
227      gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
228      gtk_widget_queue_draw (GTK_WIDGET (alignment));
229    }
230}
231
232
233static void
234gtk_alignment_size_request (GtkWidget      *widget,
235                            GtkRequisition *requisition)
236{
237  GtkAlignment *alignment;
238  GtkBin *bin;
239
240  g_return_if_fail (widget != NULL);
241  g_return_if_fail (GTK_IS_ALIGNMENT (widget));
242  g_return_if_fail (requisition != NULL);
243
244  alignment = GTK_ALIGNMENT (widget);
245  bin = GTK_BIN (widget);
246
247  requisition->width = GTK_CONTAINER (widget)->border_width * 2;
248  requisition->height = GTK_CONTAINER (widget)->border_width * 2;
249
250  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
251    {
252      GtkRequisition child_requisition;
253     
254      gtk_widget_size_request (bin->child, &child_requisition);
255
256      requisition->width += child_requisition.width;
257      requisition->height += child_requisition.height;
258    }
259}
260
261static void
262gtk_alignment_size_allocate (GtkWidget     *widget,
263                             GtkAllocation *allocation)
264{
265  GtkAlignment *alignment;
266  GtkBin *bin;
267  GtkAllocation child_allocation;
268  GtkRequisition child_requisition;
269  gint width, height;
270  gint x, y;
271
272  g_return_if_fail (widget != NULL);
273  g_return_if_fail (GTK_IS_ALIGNMENT (widget));
274  g_return_if_fail (allocation != NULL);
275
276  widget->allocation = *allocation;
277  alignment = GTK_ALIGNMENT (widget);
278  bin = GTK_BIN (widget);
279 
280  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
281    {
282      gtk_widget_get_child_requisition (bin->child, &child_requisition);
283     
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);
288     
289      if (width > child_requisition.width)
290        child_allocation.width = (child_requisition.width *
291                                  (1.0 - alignment->xscale) +
292                                  width * alignment->xscale);
293      else
294        child_allocation.width = width;
295     
296      if (height > child_requisition.height)
297        child_allocation.height = (child_requisition.height *
298                                   (1.0 - alignment->yscale) +
299                                   height * alignment->yscale);
300      else
301        child_allocation.height = height;
302
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;
305
306      gtk_widget_size_allocate (bin->child, &child_allocation);
307    }
308}
Note: See TracBrowser for help on using the repository browser.