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