]> Pileus Git - ~andy/gtk/blob - gtk/gtkaspectframe.c
call the base class init fucntions from all parent types upon class
[~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       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         /* reversed_1 */ NULL,
55         /* reversed_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         gdk_window_clear_area (widget->window,
139                                widget->allocation.x,
140                                widget->allocation.y,
141                                widget->allocation.width,
142                                widget->allocation.height);
143
144       gtk_widget_queue_resize (widget);
145     }
146 }
147
148 static void
149 gtk_aspect_frame_paint (GtkWidget    *widget,
150                         GdkRectangle *area)
151 {
152   GtkFrame *frame;
153   gint height_extra;
154   gint label_area_width;
155   gint x, y;
156   GtkAllocation *allocation;
157
158   g_return_if_fail (widget != NULL);
159   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
160   g_return_if_fail (area != NULL);
161
162   if (GTK_WIDGET_DRAWABLE (widget))
163     {
164       frame = GTK_FRAME (widget);
165       allocation = &GTK_ASPECT_FRAME(widget)->center_allocation;
166
167       height_extra = frame->label_height - widget->style->klass->xthickness;
168       height_extra = MAX (height_extra, 0);
169
170       x = GTK_CONTAINER (frame)->border_width;
171       y = GTK_CONTAINER (frame)->border_width;
172
173       gtk_draw_shadow (widget->style, widget->window,
174                        GTK_STATE_NORMAL, frame->shadow_type,
175                        allocation->x + x,
176                        allocation->y + y + height_extra / 2,
177                        allocation->width - x * 2,
178                        allocation->height - y * 2 - height_extra / 2);
179
180       if (frame->label)
181         {
182           label_area_width = (allocation->width +
183                               GTK_CONTAINER (frame)->border_width * 2 -
184                               widget->style->klass->xthickness * 2);
185
186           x = ((label_area_width - frame->label_width) * frame->label_xalign +
187                GTK_CONTAINER (frame)->border_width + widget->style->klass->xthickness);
188           y = (GTK_CONTAINER (frame)->border_width + widget->style->font->ascent);
189
190           gdk_window_clear_area (widget->window,
191                                  allocation->x + x + 2,
192                                  allocation->y + GTK_CONTAINER (frame)->border_width,
193                                  frame->label_width - 4, frame->label_height);
194           gtk_draw_string (widget->style, widget->window, GTK_WIDGET_STATE (widget),
195                            allocation->x + x + 3,
196                            allocation->y + y,
197                            frame->label);
198         }
199     }
200 }
201
202 /* the only modification to the next two routines is to call
203    gtk_aspect_frame_paint instead of gtk_frame_paint */
204
205 static void
206 gtk_aspect_frame_draw (GtkWidget    *widget,
207                        GdkRectangle *area)
208 {
209   GtkBin *bin;
210   GdkRectangle child_area;
211
212   g_return_if_fail (widget != NULL);
213   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
214   g_return_if_fail (area != NULL);
215
216   if (GTK_WIDGET_DRAWABLE (widget))
217     {
218       bin = GTK_BIN (widget);
219
220       gtk_aspect_frame_paint (widget, area);
221
222       if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
223         gtk_widget_draw (bin->child, &child_area);
224     }
225 }
226
227 static gint
228 gtk_aspect_frame_expose (GtkWidget      *widget,
229                          GdkEventExpose *event)
230 {
231   GtkBin *bin;
232   GdkEventExpose child_event;
233
234   g_return_val_if_fail (widget != NULL, FALSE);
235   g_return_val_if_fail (GTK_IS_ASPECT_FRAME (widget), FALSE);
236   g_return_val_if_fail (event != NULL, FALSE);
237
238   if (GTK_WIDGET_DRAWABLE (widget))
239     {
240       bin = GTK_BIN (widget);
241
242       gtk_aspect_frame_paint (widget, &event->area);
243
244       child_event = *event;
245       if (bin->child &&
246           GTK_WIDGET_NO_WINDOW (bin->child) &&
247           gtk_widget_intersect (bin->child, &event->area, &child_event.area))
248         gtk_widget_event (bin->child, (GdkEvent*) &child_event);
249     }
250
251   return FALSE;
252 }
253
254 static void
255 gtk_aspect_frame_size_allocate (GtkWidget     *widget,
256                           GtkAllocation *allocation)
257 {
258   GtkFrame *frame;
259   GtkAspectFrame *aspect_frame;
260   GtkBin *bin;
261
262   GtkAllocation child_allocation;
263   gint x,y;
264   gint width,height;
265   gdouble ratio;
266
267   g_return_if_fail (widget != NULL);
268   g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
269   g_return_if_fail (allocation != NULL);
270
271   aspect_frame = GTK_ASPECT_FRAME (widget);
272   frame = GTK_FRAME (widget);
273   bin = GTK_BIN (widget);
274
275   if (GTK_WIDGET_DRAWABLE (widget) &&
276       ((widget->allocation.x != allocation->x) ||
277        (widget->allocation.y != allocation->y) ||
278        (widget->allocation.width != allocation->width) ||
279        (widget->allocation.height != allocation->height)) &&
280       (widget->allocation.width != 0) &&
281       (widget->allocation.height != 0))
282     gdk_window_clear_area (widget->window,
283                            widget->allocation.x,
284                            widget->allocation.y,
285                            widget->allocation.width,
286                            widget->allocation.height);
287
288   widget->allocation = *allocation;
289
290   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
291     {
292       if (aspect_frame->obey_child)
293         {
294           if (bin->child->requisition.height != 0)
295             {
296               ratio = (gdouble)bin->child->requisition.width /
297                 bin->child->requisition.height;
298               if (ratio < MIN_RATIO) ratio = MIN_RATIO;
299             }
300           else
301             if (bin->child->requisition.height != 0)
302               ratio = MAX_RATIO;
303           else
304             ratio = 1.0;
305         }
306       else
307         ratio = aspect_frame->ratio;
308
309       x = (GTK_CONTAINER (frame)->border_width +
310            GTK_WIDGET (frame)->style->klass->xthickness);
311       width = allocation->width - x * 2;
312
313       y = (GTK_CONTAINER (frame)->border_width +
314                             MAX (frame->label_height, GTK_WIDGET (frame)->style->klass->ythickness));
315       height = (allocation->height - y -
316                                  GTK_CONTAINER (frame)->border_width -
317                                  GTK_WIDGET (frame)->style->klass->ythickness);
318
319       /* make sure we don't allocate a negative width or height,
320        * since that will be cast to a (very big) guint16 */
321       width = MAX (1, width);
322       height = MAX (1, height);
323
324       if (ratio * height > width)
325         {
326           child_allocation.width = width;
327           child_allocation.height = width/ratio + 0.5;
328         }
329       else
330         {
331           child_allocation.width = ratio*height + 0.5;
332           child_allocation.height = height;
333         }
334
335       child_allocation.x = aspect_frame->xalign * (width - child_allocation.width) + allocation->x + x;
336       child_allocation.y = aspect_frame->yalign * (height - child_allocation.height) + allocation->y + y;
337
338       aspect_frame->center_allocation.width = child_allocation.width + 2*x;
339       aspect_frame->center_allocation.x = child_allocation.x - x;
340       aspect_frame->center_allocation.height = child_allocation.height + y +
341                                  GTK_CONTAINER (frame)->border_width +
342                                  GTK_WIDGET (frame)->style->klass->ythickness;
343       aspect_frame->center_allocation.y = child_allocation.y - y;
344
345       gtk_widget_size_allocate (bin->child, &child_allocation);
346     }
347 }