]> Pileus Git - ~andy/gtk/blob - gtk/gtkaspectframe.c
9faaeb3a702346cf37061d85cd8ff3f0d5bee894
[~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 Library 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  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library 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 #include "gtkaspectframe.h"
25
26 static void gtk_aspect_frame_class_init    (GtkAspectFrameClass *klass);
27 static void gtk_aspect_frame_init          (GtkAspectFrame      *aspect_frame);
28 static void gtk_aspect_frame_draw          (GtkWidget      *widget,
29                                             GdkRectangle   *area);
30 static void gtk_aspect_frame_paint         (GtkWidget      *widget,
31                                             GdkRectangle   *area);
32 static gint gtk_aspect_frame_expose        (GtkWidget      *widget,
33                                             GdkEventExpose *event);
34 static void gtk_aspect_frame_size_allocate (GtkWidget         *widget,
35                                             GtkAllocation     *allocation);
36
37 #define MAX_RATIO 10000.0
38 #define MIN_RATIO 0.0001
39
40 GtkType
41 gtk_aspect_frame_get_type (void)
42 {
43   static GtkType aspect_frame_type = 0;
44
45   if (!aspect_frame_type)
46     {
47       static const GtkTypeInfo aspect_frame_info =
48       {
49         "GtkAspectFrame",
50         sizeof (GtkAspectFrame),
51         sizeof (GtkAspectFrameClass),
52         (GtkClassInitFunc) gtk_aspect_frame_class_init,
53         (GtkObjectInitFunc) gtk_aspect_frame_init,
54         /* reserved_1 */ NULL,
55         /* reserved_2 */ NULL,
56         (GtkClassInitFunc) NULL,
57       };
58
59       aspect_frame_type = gtk_type_unique (gtk_frame_get_type (), &aspect_frame_info);
60     }
61
62   return aspect_frame_type;
63 }
64
65 static void
66 gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
67 {
68   GtkWidgetClass *widget_class;
69
70   widget_class = (GtkWidgetClass*) class;
71
72   widget_class->draw = gtk_aspect_frame_draw;
73   widget_class->expose_event = gtk_aspect_frame_expose;
74   widget_class->size_allocate = gtk_aspect_frame_size_allocate;
75 }
76
77 static void
78 gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
79 {
80   aspect_frame->xalign = 0.5;
81   aspect_frame->yalign = 0.5;
82   aspect_frame->ratio = 1.0;
83   aspect_frame->obey_child = 1;
84   aspect_frame->center_allocation.x = -1;
85   aspect_frame->center_allocation.y = -1;
86   aspect_frame->center_allocation.width = 1;
87   aspect_frame->center_allocation.height = 1;
88 }
89
90 GtkWidget*
91 gtk_aspect_frame_new (const gchar *label,
92                       gfloat xalign,
93                       gfloat yalign,
94                       gfloat ratio,
95                       gint   obey_child)
96 {
97   GtkAspectFrame *aspect_frame;
98
99   aspect_frame = gtk_type_new (gtk_aspect_frame_get_type ());
100
101   aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
102   aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
103   aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
104   aspect_frame->obey_child = obey_child;
105
106   gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
107
108   return GTK_WIDGET (aspect_frame);
109 }
110
111 void
112 gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
113                       gfloat        xalign,
114                       gfloat        yalign,
115                       gfloat        ratio,
116                       gint          obey_child)
117 {
118   g_return_if_fail (aspect_frame != NULL);
119   g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
120
121   xalign = CLAMP (xalign, 0.0, 1.0);
122   yalign = CLAMP (yalign, 0.0, 1.0);
123   ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
124
125   if ((aspect_frame->xalign != xalign) ||
126       (aspect_frame->yalign != yalign) ||
127       (aspect_frame->ratio != ratio) ||
128       (aspect_frame->obey_child != obey_child))
129     {
130       GtkWidget *widget = GTK_WIDGET(aspect_frame);
131
132       aspect_frame->xalign = xalign;
133       aspect_frame->yalign = yalign;
134       aspect_frame->ratio = ratio;
135       aspect_frame->obey_child = obey_child;
136
137       if (GTK_WIDGET_DRAWABLE(widget))
138         gtk_widget_queue_clear (widget);
139
140       gtk_widget_queue_resize (widget);
141     }
142 }
143
144 static void
145 gtk_aspect_frame_paint (GtkWidget    *widget,
146                         GdkRectangle *area)
147 {
148   GtkFrame *frame;
149   gint height_extra;
150   gint label_area_width;
151   gint x, y, x2, y2;
152   GtkAllocation *allocation;
153
154   g_return_if_fail (widget != NULL);
155   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
156   g_return_if_fail (area != NULL);
157
158   if (GTK_WIDGET_DRAWABLE (widget))
159     {
160       frame = GTK_FRAME (widget);
161       allocation = &GTK_ASPECT_FRAME(widget)->center_allocation;
162
163       height_extra = frame->label_height - widget->style->klass->xthickness;
164       height_extra = MAX (height_extra, 0);
165
166       x = GTK_CONTAINER (frame)->border_width;
167       y = GTK_CONTAINER (frame)->border_width;
168
169       if (frame->label)
170         {
171           label_area_width = (allocation->width +
172                               GTK_CONTAINER (frame)->border_width * 2 -
173                               widget->style->klass->xthickness * 2);
174
175           x2 = ((label_area_width - frame->label_width) * frame->label_xalign +
176                 GTK_CONTAINER (frame)->border_width + widget->style->klass->xthickness);
177           y2 = (GTK_CONTAINER (frame)->border_width + widget->style->font->ascent);
178           
179           gtk_paint_shadow_gap (widget->style, widget->window,
180                                 GTK_STATE_NORMAL, frame->shadow_type,
181                                 area, widget, "frame",
182                                 allocation->x + x,
183                                 allocation->y + y + height_extra / 2,
184                                 allocation->width - x * 2,
185                                 allocation->height - y * 2 - height_extra / 2,
186                                 GTK_POS_TOP, 
187                                 x2 + 2 - x, frame->label_width - 4);
188           
189           gtk_paint_string (widget->style, widget->window, GTK_WIDGET_STATE (widget),
190                             area, widget, "frame",
191                             allocation->x + x2 + 3,
192                             allocation->y + y2,
193                             frame->label);
194         }
195       else
196         gtk_paint_shadow (widget->style, widget->window,
197                           GTK_STATE_NORMAL, frame->shadow_type,
198                           area, widget, "frame",
199                           allocation->x + x,
200                           allocation->y + y + height_extra / 2,
201                           allocation->width - x * 2,
202                           allocation->height - y * 2 - height_extra / 2);
203     }
204 }
205
206 /* the only modification to the next two routines is to call
207    gtk_aspect_frame_paint instead of gtk_frame_paint */
208
209 static void
210 gtk_aspect_frame_draw (GtkWidget    *widget,
211                        GdkRectangle *area)
212 {
213   GtkBin *bin;
214   GdkRectangle child_area;
215
216   g_return_if_fail (widget != NULL);
217   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
218   g_return_if_fail (area != NULL);
219
220   if (GTK_WIDGET_DRAWABLE (widget))
221     {
222       bin = GTK_BIN (widget);
223
224       gtk_aspect_frame_paint (widget, area);
225
226       if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
227         gtk_widget_draw (bin->child, &child_area);
228     }
229 }
230
231 static gint
232 gtk_aspect_frame_expose (GtkWidget      *widget,
233                          GdkEventExpose *event)
234 {
235   GtkBin *bin;
236   GdkEventExpose child_event;
237
238   g_return_val_if_fail (widget != NULL, FALSE);
239   g_return_val_if_fail (GTK_IS_ASPECT_FRAME (widget), FALSE);
240   g_return_val_if_fail (event != NULL, FALSE);
241
242   if (GTK_WIDGET_DRAWABLE (widget))
243     {
244       bin = GTK_BIN (widget);
245
246       gtk_aspect_frame_paint (widget, &event->area);
247
248       child_event = *event;
249       if (bin->child &&
250           GTK_WIDGET_NO_WINDOW (bin->child) &&
251           gtk_widget_intersect (bin->child, &event->area, &child_event.area))
252         gtk_widget_event (bin->child, (GdkEvent*) &child_event);
253     }
254
255   return FALSE;
256 }
257
258 static void
259 gtk_aspect_frame_size_allocate (GtkWidget     *widget,
260                           GtkAllocation *allocation)
261 {
262   GtkFrame *frame;
263   GtkAspectFrame *aspect_frame;
264   GtkBin *bin;
265
266   GtkAllocation child_allocation;
267   gint x,y;
268   gint width,height;
269   gdouble ratio;
270
271   g_return_if_fail (widget != NULL);
272   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
273   g_return_if_fail (allocation != NULL);
274
275   aspect_frame = GTK_ASPECT_FRAME (widget);
276   frame = GTK_FRAME (widget);
277   bin = GTK_BIN (widget);
278
279   if (GTK_WIDGET_DRAWABLE (widget) &&
280       ((widget->allocation.x != allocation->x) ||
281        (widget->allocation.y != allocation->y) ||
282        (widget->allocation.width != allocation->width) ||
283        (widget->allocation.height != allocation->height)) &&
284       (widget->allocation.width != 0) &&
285       (widget->allocation.height != 0))
286     gdk_window_clear_area (widget->window,
287                            widget->allocation.x,
288                            widget->allocation.y,
289                            widget->allocation.width,
290                            widget->allocation.height);
291
292   widget->allocation = *allocation;
293
294   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
295     {
296       if (aspect_frame->obey_child)
297         {
298           GtkRequisition child_requisition;
299
300           gtk_widget_get_child_requisition (bin->child, &child_requisition);
301           if (child_requisition.height != 0)
302             {
303               ratio = (gdouble)child_requisition.width /
304                                child_requisition.height;
305               if (ratio < MIN_RATIO) ratio = MIN_RATIO;
306             }
307           else if (child_requisition.width != 0)
308             ratio = MAX_RATIO;
309           else
310             ratio = 1.0;
311         }
312       else
313         ratio = aspect_frame->ratio;
314
315       x = (GTK_CONTAINER (frame)->border_width +
316            GTK_WIDGET (frame)->style->klass->xthickness);
317       width = allocation->width - x * 2;
318
319       y = (GTK_CONTAINER (frame)->border_width +
320                             MAX (frame->label_height, GTK_WIDGET (frame)->style->klass->ythickness));
321       height = (allocation->height - y -
322                                  GTK_CONTAINER (frame)->border_width -
323                                  GTK_WIDGET (frame)->style->klass->ythickness);
324
325       /* make sure we don't allocate a negative width or height,
326        * since that will be cast to a (very big) guint16 */
327       width = MAX (1, width);
328       height = MAX (1, height);
329
330       if (ratio * height > width)
331         {
332           child_allocation.width = width;
333           child_allocation.height = width/ratio + 0.5;
334         }
335       else
336         {
337           child_allocation.width = ratio*height + 0.5;
338           child_allocation.height = height;
339         }
340
341       child_allocation.x = aspect_frame->xalign * (width - child_allocation.width) + allocation->x + x;
342       child_allocation.y = aspect_frame->yalign * (height - child_allocation.height) + allocation->y + y;
343
344       aspect_frame->center_allocation.width = child_allocation.width + 2*x;
345       aspect_frame->center_allocation.x = child_allocation.x - x;
346       aspect_frame->center_allocation.height = child_allocation.height + y +
347                                  GTK_CONTAINER (frame)->border_width +
348                                  GTK_WIDGET (frame)->style->klass->ythickness;
349       aspect_frame->center_allocation.y = child_allocation.y - y;
350
351       gtk_widget_size_allocate (bin->child, &child_allocation);
352     }
353 }