]> Pileus Git - ~andy/gtk/blob - gtk/gtkaspectframe.c
New static function to set the background of all windows.
[~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 GType
62 gtk_aspect_frame_get_type (void)
63 {
64   static GType aspect_frame_type = 0;
65   
66   if (!aspect_frame_type)
67     {
68       static const GTypeInfo aspect_frame_info =
69       {
70         sizeof (GtkAspectFrameClass),
71         NULL,           /* base_init */
72         NULL,           /* base_finalize */
73         (GClassInitFunc) gtk_aspect_frame_class_init,
74         NULL,           /* class_finalize */
75         NULL,           /* class_data */
76         sizeof (GtkAspectFrame),
77         0,              /* n_preallocs */
78         (GInstanceInitFunc) gtk_aspect_frame_init,
79       };
80       
81       aspect_frame_type =
82         g_type_register_static (GTK_TYPE_FRAME, "GtkAspectFrame",
83                                 &aspect_frame_info, 0);
84     }
85   
86   return aspect_frame_type;
87 }
88
89 static void
90 gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
91 {
92   GObjectClass *gobject_class;
93   GtkFrameClass *frame_class;
94   
95   parent_class = g_type_class_peek_parent (class);
96
97   gobject_class = (GObjectClass*) class;
98   frame_class = (GtkFrameClass*) class;
99   
100   gobject_class->set_property = gtk_aspect_frame_set_property;
101   gobject_class->get_property = gtk_aspect_frame_get_property;
102
103   frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
104
105   g_object_class_install_property (gobject_class,
106                                    PROP_XALIGN,
107                                    g_param_spec_float ("xalign",
108                                                        _("Horizontal Alignment"),
109                                                        _("X alignment of the child"),
110                                                        0.0, 1.0, 0.5,
111                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
112   g_object_class_install_property (gobject_class,
113                                    PROP_YALIGN,
114                                    g_param_spec_float ("yalign",
115                                                        _("Vertical Alignment"),
116                                                        _("Y alignment of the child"),
117                                                        0.0, 1.0, 0.5,
118                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
119   g_object_class_install_property (gobject_class,
120                                    PROP_RATIO,
121                                    g_param_spec_float ("ratio",
122                                                        _("Ratio"),
123                                                        _("Aspect ratio if obey_child is FALSE"),
124                                                        MIN_RATIO, MAX_RATIO, 0.5,
125                                                        G_PARAM_READABLE | G_PARAM_WRITABLE ));
126   g_object_class_install_property (gobject_class,
127                                    PROP_OBEY_CHILD,
128                                    g_param_spec_boolean ("obey_child",
129                                                          _("Obey child"),
130                                                          _("Force aspect ratio to match that of the frame's child"),
131                                                          TRUE,
132                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
133 }
134
135 static void
136 gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
137 {
138   aspect_frame->xalign = 0.5;
139   aspect_frame->yalign = 0.5;
140   aspect_frame->ratio = 1.0;
141   aspect_frame->obey_child = TRUE;
142 }
143
144 static void
145 gtk_aspect_frame_set_property (GObject         *object,
146                                guint            prop_id,
147                                const GValue    *value,
148                                GParamSpec      *pspec)
149 {
150   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
151   
152   switch (prop_id)
153     {
154       /* g_object_notify is handled by the _frame_set function */
155     case PROP_XALIGN:
156       gtk_aspect_frame_set (aspect_frame,
157                             g_value_get_float (value),
158                             aspect_frame->yalign,
159                             aspect_frame->ratio,
160                             aspect_frame->obey_child);
161       break;
162     case PROP_YALIGN:
163       gtk_aspect_frame_set (aspect_frame,
164                             aspect_frame->xalign,
165                             g_value_get_float (value),
166                             aspect_frame->ratio,
167                             aspect_frame->obey_child);
168       break;
169     case PROP_RATIO:
170       gtk_aspect_frame_set (aspect_frame,
171                             aspect_frame->xalign,
172                             aspect_frame->yalign,
173                             g_value_get_float (value),
174                             aspect_frame->obey_child);
175       break;
176     case PROP_OBEY_CHILD:
177       gtk_aspect_frame_set (aspect_frame,
178                             aspect_frame->xalign,
179                             aspect_frame->yalign,
180                             aspect_frame->ratio,
181                             g_value_get_boolean (value));
182       break;
183     default:
184        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186     }
187 }
188
189 static void
190 gtk_aspect_frame_get_property (GObject         *object,
191                                guint            prop_id,
192                                GValue          *value,
193                                GParamSpec      *pspec)
194 {
195   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
196   
197   switch (prop_id)
198     {
199     case PROP_XALIGN:
200       g_value_set_float (value, aspect_frame->xalign);
201       break;
202     case PROP_YALIGN:
203       g_value_set_float (value, aspect_frame->yalign);
204       break;
205     case PROP_RATIO:
206       g_value_set_float (value, aspect_frame->ratio);
207       break;
208     case PROP_OBEY_CHILD:
209       g_value_set_boolean (value, aspect_frame->obey_child);
210       break;
211     default:
212        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214     }
215 }
216
217 GtkWidget*
218 gtk_aspect_frame_new (const gchar *label,
219                       gfloat       xalign,
220                       gfloat       yalign,
221                       gfloat       ratio,
222                       gboolean     obey_child)
223 {
224   GtkAspectFrame *aspect_frame;
225
226   aspect_frame = g_object_new (GTK_TYPE_ASPECT_FRAME, NULL);
227
228   aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
229   aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
230   aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
231   aspect_frame->obey_child = obey_child != FALSE;
232
233   gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
234
235   return GTK_WIDGET (aspect_frame);
236 }
237
238 void
239 gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
240                       gfloat          xalign,
241                       gfloat          yalign,
242                       gfloat          ratio,
243                       gboolean        obey_child)
244 {
245   g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
246   
247   xalign = CLAMP (xalign, 0.0, 1.0);
248   yalign = CLAMP (yalign, 0.0, 1.0);
249   ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
250   obey_child = obey_child != FALSE;
251   
252   if (   (aspect_frame->xalign != xalign)
253       || (aspect_frame->yalign != yalign)
254       || (aspect_frame->ratio != ratio)
255       || (aspect_frame->obey_child != obey_child))
256     {
257       g_object_freeze_notify (G_OBJECT (aspect_frame));
258
259       if (aspect_frame->xalign != xalign)
260         {
261           aspect_frame->xalign = xalign;
262           g_object_notify (G_OBJECT (aspect_frame), "xalign");
263         }
264       if (aspect_frame->yalign != yalign)
265         {
266           aspect_frame->yalign = yalign;
267           g_object_notify (G_OBJECT (aspect_frame), "yalign");
268         }
269       if (aspect_frame->ratio != ratio)
270         {
271           aspect_frame->ratio = ratio;
272           g_object_notify (G_OBJECT (aspect_frame), "ratio");
273         }
274       if (aspect_frame->obey_child != obey_child)
275         {
276           aspect_frame->obey_child = obey_child;
277           g_object_notify (G_OBJECT (aspect_frame), "obey_child");
278         }
279       g_object_thaw_notify (G_OBJECT (aspect_frame));
280
281       gtk_widget_queue_resize (GTK_WIDGET (aspect_frame));
282     }
283 }
284
285 static void
286 gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
287                                            GtkAllocation *child_allocation)
288 {
289   GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
290   GtkBin *bin = GTK_BIN (frame);
291   gdouble ratio;
292
293   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
294     {
295       GtkAllocation full_allocation;
296       
297       if (aspect_frame->obey_child)
298         {
299           GtkRequisition child_requisition;
300
301           gtk_widget_get_child_requisition (bin->child, &child_requisition);
302           if (child_requisition.height != 0)
303             {
304               ratio = ((gdouble) child_requisition.width /
305                        child_requisition.height);
306               if (ratio < MIN_RATIO)
307                 ratio = MIN_RATIO;
308             }
309           else if (child_requisition.width != 0)
310             ratio = MAX_RATIO;
311           else
312             ratio = 1.0;
313         }
314       else
315         ratio = aspect_frame->ratio;
316
317       parent_class->compute_child_allocation (frame, &full_allocation);
318       
319       if (ratio * full_allocation.height > full_allocation.width)
320         {
321           child_allocation->width = full_allocation.width;
322           child_allocation->height = full_allocation.width / ratio + 0.5;
323         }
324       else
325         {
326           child_allocation->width = ratio * full_allocation.height + 0.5;
327           child_allocation->height = full_allocation.height;
328         }
329       
330       child_allocation->x = full_allocation.x + aspect_frame->xalign * (full_allocation.width - child_allocation->width);
331       child_allocation->y = full_allocation.y + aspect_frame->yalign * (full_allocation.height - child_allocation->height);
332     }
333   else
334     parent_class->compute_child_allocation (frame, child_allocation);
335 }