]> Pileus Git - ~andy/gtk/blob - gtk/gtkaspectframe.c
Patch from Matthias Clasen to remove remove all instances of
[~andy/gtk] / gtk / gtkaspectframe.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GtkAspectFrame: Ensure that the child window has a specified aspect ratio
5  *    or, if obey_child, has the same aspect ratio as its requested size
6  *
7  *     Copyright Owen Taylor                          4/9/97
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /*
26  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
30  */
31
32 #include "gtkaspectframe.h"
33 #include "gtkintl.h"
34
35 enum {
36   PROP_0,
37   PROP_XALIGN,
38   PROP_YALIGN,
39   PROP_RATIO,
40   PROP_OBEY_CHILD
41 };
42
43 static void gtk_aspect_frame_class_init               (GtkAspectFrameClass *klass);
44 static void gtk_aspect_frame_init                     (GtkAspectFrame      *aspect_frame);
45 static void gtk_aspect_frame_set_property (GObject         *object,
46                                            guint            prop_id,
47                                            const GValue    *value,
48                                            GParamSpec      *pspec);
49 static void gtk_aspect_frame_get_property (GObject         *object,
50                                            guint            prop_id,
51                                            GValue          *value,
52                                            GParamSpec      *pspec);
53 static void gtk_aspect_frame_compute_child_allocation (GtkFrame            *frame,
54                                                        GtkAllocation       *child_allocation);
55
56 #define MAX_RATIO 10000.0
57 #define MIN_RATIO 0.0001
58
59 static GtkFrameClass *parent_class = NULL;
60
61 GtkType
62 gtk_aspect_frame_get_type (void)
63 {
64   static GtkType aspect_frame_type = 0;
65   
66   if (!aspect_frame_type)
67     {
68       static const GtkTypeInfo aspect_frame_info =
69       {
70         "GtkAspectFrame",
71         sizeof (GtkAspectFrame),
72         sizeof (GtkAspectFrameClass),
73         (GtkClassInitFunc) gtk_aspect_frame_class_init,
74         (GtkObjectInitFunc) gtk_aspect_frame_init,
75         /* reserved_1 */ NULL,
76         /* reserved_2 */ NULL,
77         (GtkClassInitFunc) NULL,
78       };
79       
80       aspect_frame_type = gtk_type_unique (GTK_TYPE_FRAME, &aspect_frame_info);
81     }
82   
83   return aspect_frame_type;
84 }
85
86 static void
87 gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
88 {
89   GObjectClass *gobject_class;
90   GtkObjectClass *object_class;
91   GtkFrameClass *frame_class;
92   
93   parent_class = gtk_type_class (GTK_TYPE_FRAME);
94
95   gobject_class = (GObjectClass*) class;
96   object_class = (GtkObjectClass*) class;
97   frame_class = (GtkFrameClass*) class;
98   
99   gobject_class->set_property = gtk_aspect_frame_set_property;
100   gobject_class->get_property = gtk_aspect_frame_get_property;
101
102   frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
103
104   g_object_class_install_property (gobject_class,
105                                    PROP_XALIGN,
106                                    g_param_spec_float ("xalign",
107                                                        _("Horizontal Alignment"),
108                                                        _("X alignment of the child"),
109                                                        0.0, 1.0, 0.5,
110                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
111   g_object_class_install_property (gobject_class,
112                                    PROP_YALIGN,
113                                    g_param_spec_float ("yalign",
114                                                        _("Vertical Alignment"),
115                                                        _("Y alignment of the child"),
116                                                        0.0, 1.0, 0.5,
117                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
118   g_object_class_install_property (gobject_class,
119                                    PROP_RATIO,
120                                    g_param_spec_float ("ratio",
121                                                        _("Ratio"),
122                                                        _("Aspect ratio if obey_child is FALSE"),
123                                                        0.0, 1.0, 0.5,
124                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
125   g_object_class_install_property (gobject_class,
126                                    PROP_OBEY_CHILD,
127                                    g_param_spec_boolean ("obey_child",
128                                                          _("Obey child"),
129                                                          _("Force aspect ratio to match that of the frame's child"),
130                                                          TRUE,
131                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
132 }
133
134 static void
135 gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
136 {
137   aspect_frame->xalign = 0.5;
138   aspect_frame->yalign = 0.5;
139   aspect_frame->ratio = 1.0;
140   aspect_frame->obey_child = TRUE;
141 }
142
143 static void
144 gtk_aspect_frame_set_property (GObject         *object,
145                                guint            prop_id,
146                                const GValue    *value,
147                                GParamSpec      *pspec)
148 {
149   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
150   
151   switch (prop_id)
152     {
153       /* g_object_notify is handled by the _frame_set function */
154     case PROP_XALIGN:
155       gtk_aspect_frame_set (aspect_frame,
156                             g_value_get_float (value),
157                             aspect_frame->yalign,
158                             aspect_frame->ratio,
159                             aspect_frame->obey_child);
160       break;
161     case PROP_YALIGN:
162       gtk_aspect_frame_set (aspect_frame,
163                             aspect_frame->xalign,
164                             g_value_get_float (value),
165                             aspect_frame->ratio,
166                             aspect_frame->obey_child);
167       break;
168     case PROP_RATIO:
169       gtk_aspect_frame_set (aspect_frame,
170                             aspect_frame->xalign,
171                             aspect_frame->yalign,
172                             g_value_get_float (value),
173                             aspect_frame->obey_child);
174       break;
175     case PROP_OBEY_CHILD:
176       gtk_aspect_frame_set (aspect_frame,
177                             aspect_frame->xalign,
178                             aspect_frame->yalign,
179                             aspect_frame->ratio,
180                             g_value_get_boolean (value));
181       break;
182     default:
183        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185     }
186 }
187
188 static void
189 gtk_aspect_frame_get_property (GObject         *object,
190                                guint            prop_id,
191                                GValue          *value,
192                                GParamSpec      *pspec)
193 {
194   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
195   
196   switch (prop_id)
197     {
198     case PROP_XALIGN:
199       g_value_set_float (value, aspect_frame->xalign);
200       break;
201     case PROP_YALIGN:
202       g_value_set_float (value, aspect_frame->yalign);
203       break;
204     case PROP_RATIO:
205       g_value_set_float (value, aspect_frame->ratio);
206       break;
207     case PROP_OBEY_CHILD:
208       g_value_set_boolean (value, aspect_frame->obey_child);
209       break;
210     default:
211        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213     }
214 }
215
216 GtkWidget*
217 gtk_aspect_frame_new (const gchar *label,
218                       gfloat       xalign,
219                       gfloat       yalign,
220                       gfloat       ratio,
221                       gboolean     obey_child)
222 {
223   GtkAspectFrame *aspect_frame;
224
225   aspect_frame = gtk_type_new (gtk_aspect_frame_get_type ());
226
227   aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
228   aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
229   aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
230   aspect_frame->obey_child = obey_child != FALSE;
231
232   gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
233
234   return GTK_WIDGET (aspect_frame);
235 }
236
237 void
238 gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
239                       gfloat          xalign,
240                       gfloat          yalign,
241                       gfloat          ratio,
242                       gboolean        obey_child)
243 {
244   gboolean needs_resize = FALSE;
245    
246   g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
247   
248   xalign = CLAMP (xalign, 0.0, 1.0);
249   yalign = CLAMP (yalign, 0.0, 1.0);
250   ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
251   obey_child = obey_child != FALSE;
252   
253   if (aspect_frame->xalign != xalign)
254     {
255       aspect_frame->xalign = xalign;
256       g_object_notify (G_OBJECT (aspect_frame), "xalign");
257       needs_resize = TRUE;
258     }
259    if (aspect_frame->yalign != yalign)
260     {
261       aspect_frame->yalign = yalign;
262       g_object_notify (G_OBJECT (aspect_frame), "yalign");
263       needs_resize = TRUE;
264     }
265    if (aspect_frame->ratio != ratio)
266     {
267       aspect_frame->ratio = ratio;
268       g_object_notify (G_OBJECT (aspect_frame), "ratio");
269       needs_resize = TRUE;
270     }
271    if (aspect_frame->obey_child != obey_child)
272     {
273       aspect_frame->obey_child = obey_child;
274       g_object_notify (G_OBJECT (aspect_frame), "obey_child");
275       needs_resize = TRUE;
276     }
277    
278    if (needs_resize == TRUE)
279       gtk_widget_queue_resize (GTK_WIDGET(aspect_frame));
280 }
281
282 static void
283 gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
284                                            GtkAllocation *child_allocation)
285 {
286   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
287   GtkBin *bin = GTK_BIN (frame);
288   gdouble ratio;
289
290   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
291     {
292       GtkAllocation full_allocation;
293       
294       if (aspect_frame->obey_child)
295         {
296           GtkRequisition child_requisition;
297
298           gtk_widget_get_child_requisition (bin->child, &child_requisition);
299           if (child_requisition.height != 0)
300             {
301               ratio = ((gdouble) child_requisition.width /
302                        child_requisition.height);
303               if (ratio < MIN_RATIO)
304                 ratio = MIN_RATIO;
305             }
306           else if (child_requisition.width != 0)
307             ratio = MAX_RATIO;
308           else
309             ratio = 1.0;
310         }
311       else
312         ratio = aspect_frame->ratio;
313
314       parent_class->compute_child_allocation (frame, &full_allocation);
315       
316       if (ratio * full_allocation.height > full_allocation.width)
317         {
318           child_allocation->width = full_allocation.width;
319           child_allocation->height = full_allocation.width / ratio + 0.5;
320         }
321       else
322         {
323           child_allocation->width = ratio * full_allocation.height + 0.5;
324           child_allocation->height = full_allocation.height;
325         }
326       
327       child_allocation->x = full_allocation.x + aspect_frame->xalign * (full_allocation.width - child_allocation->width);
328       child_allocation->y = full_allocation.y + aspect_frame->yalign * (full_allocation.height - child_allocation->height);
329     }
330   else
331     parent_class->compute_child_allocation (frame, child_allocation);
332 }