1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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
7 * Copyright Owen Taylor 4/9/97
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.
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.
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.
24 #include "gtkaspectframe.h"
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,
30 static void gtk_aspect_frame_paint (GtkWidget *widget,
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);
37 #define MAX_RATIO 10000.0
38 #define MIN_RATIO 0.0001
41 gtk_aspect_frame_get_type (void)
43 static GtkType aspect_frame_type = 0;
45 if (!aspect_frame_type)
47 GtkTypeInfo aspect_frame_info =
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,
59 aspect_frame_type = gtk_type_unique (gtk_frame_get_type (), &aspect_frame_info);
62 return aspect_frame_type;
66 gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
68 GtkWidgetClass *widget_class;
70 widget_class = (GtkWidgetClass*) class;
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;
78 gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
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;
91 gtk_aspect_frame_new (const gchar *label,
97 GtkAspectFrame *aspect_frame;
99 aspect_frame = gtk_type_new (gtk_aspect_frame_get_type ());
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;
106 gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
108 return GTK_WIDGET (aspect_frame);
112 gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
118 g_return_if_fail (aspect_frame != NULL);
119 g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
121 xalign = CLAMP (xalign, 0.0, 1.0);
122 yalign = CLAMP (yalign, 0.0, 1.0);
123 ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
125 if ((aspect_frame->xalign != xalign) ||
126 (aspect_frame->yalign != yalign) ||
127 (aspect_frame->ratio != ratio) ||
128 (aspect_frame->obey_child != obey_child))
130 GtkWidget *widget = GTK_WIDGET(aspect_frame);
132 aspect_frame->xalign = xalign;
133 aspect_frame->yalign = yalign;
134 aspect_frame->ratio = ratio;
135 aspect_frame->obey_child = obey_child;
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);
144 gtk_widget_queue_resize (widget);
149 gtk_aspect_frame_paint (GtkWidget *widget,
154 gint label_area_width;
156 GtkAllocation *allocation;
158 g_return_if_fail (widget != NULL);
159 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
160 g_return_if_fail (area != NULL);
162 if (GTK_WIDGET_DRAWABLE (widget))
164 frame = GTK_FRAME (widget);
165 allocation = >K_ASPECT_FRAME(widget)->center_allocation;
167 height_extra = frame->label_height - widget->style->klass->xthickness;
168 height_extra = MAX (height_extra, 0);
170 x = GTK_CONTAINER (frame)->border_width;
171 y = GTK_CONTAINER (frame)->border_width;
173 gtk_draw_shadow (widget->style, widget->window,
174 GTK_STATE_NORMAL, frame->shadow_type,
176 allocation->y + y + height_extra / 2,
177 allocation->width - x * 2,
178 allocation->height - y * 2 - height_extra / 2);
182 label_area_width = (allocation->width +
183 GTK_CONTAINER (frame)->border_width * 2 -
184 widget->style->klass->xthickness * 2);
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);
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,
202 /* the only modification to the next two routines is to call
203 gtk_aspect_frame_paint instead of gtk_frame_paint */
206 gtk_aspect_frame_draw (GtkWidget *widget,
210 GdkRectangle child_area;
212 g_return_if_fail (widget != NULL);
213 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
214 g_return_if_fail (area != NULL);
216 if (GTK_WIDGET_DRAWABLE (widget))
218 bin = GTK_BIN (widget);
220 gtk_aspect_frame_paint (widget, area);
222 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
223 gtk_widget_draw (bin->child, &child_area);
228 gtk_aspect_frame_expose (GtkWidget *widget,
229 GdkEventExpose *event)
232 GdkEventExpose child_event;
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);
238 if (GTK_WIDGET_DRAWABLE (widget))
240 bin = GTK_BIN (widget);
242 gtk_aspect_frame_paint (widget, &event->area);
244 child_event = *event;
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);
255 gtk_aspect_frame_size_allocate (GtkWidget *widget,
256 GtkAllocation *allocation)
259 GtkAspectFrame *aspect_frame;
262 GtkAllocation child_allocation;
267 g_return_if_fail (widget != NULL);
268 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
269 g_return_if_fail (allocation != NULL);
271 aspect_frame = GTK_ASPECT_FRAME (widget);
272 frame = GTK_FRAME (widget);
273 bin = GTK_BIN (widget);
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);
288 widget->allocation = *allocation;
290 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
292 if (aspect_frame->obey_child)
294 if (bin->child->requisition.height != 0)
296 ratio = (gdouble)bin->child->requisition.width /
297 bin->child->requisition.height;
298 if (ratio < MIN_RATIO) ratio = MIN_RATIO;
301 if (bin->child->requisition.height != 0)
307 ratio = aspect_frame->ratio;
309 x = (GTK_CONTAINER (frame)->border_width +
310 GTK_WIDGET (frame)->style->klass->xthickness);
311 width = allocation->width - x * 2;
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);
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);
324 if (ratio * height > width)
326 child_allocation.width = width;
327 child_allocation.height = width/ratio + 0.5;
331 child_allocation.width = ratio*height + 0.5;
332 child_allocation.height = height;
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;
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;
345 gtk_widget_size_allocate (bin->child, &child_allocation);