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 static const GtkTypeInfo aspect_frame_info =
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,
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 gtk_widget_queue_clear (widget);
140 gtk_widget_queue_resize (widget);
145 gtk_aspect_frame_paint (GtkWidget *widget,
150 gint label_area_width;
152 GtkAllocation *allocation;
154 g_return_if_fail (widget != NULL);
155 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
156 g_return_if_fail (area != NULL);
158 if (GTK_WIDGET_DRAWABLE (widget))
160 frame = GTK_FRAME (widget);
161 allocation = >K_ASPECT_FRAME(widget)->center_allocation;
163 height_extra = frame->label_height - widget->style->klass->xthickness;
164 height_extra = MAX (height_extra, 0);
166 x = GTK_CONTAINER (frame)->border_width;
167 y = GTK_CONTAINER (frame)->border_width;
171 label_area_width = (allocation->width +
172 GTK_CONTAINER (frame)->border_width * 2 -
173 widget->style->klass->xthickness * 2);
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);
179 gtk_paint_shadow_gap (widget->style, widget->window,
180 GTK_STATE_NORMAL, frame->shadow_type,
181 area, widget, "frame",
183 allocation->y + y + height_extra / 2,
184 allocation->width - x * 2,
185 allocation->height - y * 2 - height_extra / 2,
187 x2 + 2 - x, frame->label_width - 4);
189 gtk_paint_string (widget->style, widget->window, GTK_WIDGET_STATE (widget),
190 area, widget, "frame",
191 allocation->x + x2 + 3,
196 gtk_paint_shadow (widget->style, widget->window,
197 GTK_STATE_NORMAL, frame->shadow_type,
198 area, widget, "frame",
200 allocation->y + y + height_extra / 2,
201 allocation->width - x * 2,
202 allocation->height - y * 2 - height_extra / 2);
206 /* the only modification to the next two routines is to call
207 gtk_aspect_frame_paint instead of gtk_frame_paint */
210 gtk_aspect_frame_draw (GtkWidget *widget,
214 GdkRectangle child_area;
216 g_return_if_fail (widget != NULL);
217 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
218 g_return_if_fail (area != NULL);
220 if (GTK_WIDGET_DRAWABLE (widget))
222 bin = GTK_BIN (widget);
224 gtk_aspect_frame_paint (widget, area);
226 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
227 gtk_widget_draw (bin->child, &child_area);
232 gtk_aspect_frame_expose (GtkWidget *widget,
233 GdkEventExpose *event)
236 GdkEventExpose child_event;
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);
242 if (GTK_WIDGET_DRAWABLE (widget))
244 bin = GTK_BIN (widget);
246 gtk_aspect_frame_paint (widget, &event->area);
248 child_event = *event;
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);
259 gtk_aspect_frame_size_allocate (GtkWidget *widget,
260 GtkAllocation *allocation)
263 GtkAspectFrame *aspect_frame;
266 GtkAllocation child_allocation;
271 g_return_if_fail (widget != NULL);
272 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
273 g_return_if_fail (allocation != NULL);
275 aspect_frame = GTK_ASPECT_FRAME (widget);
276 frame = GTK_FRAME (widget);
277 bin = GTK_BIN (widget);
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);
292 widget->allocation = *allocation;
294 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
296 if (aspect_frame->obey_child)
298 GtkRequisition child_requisition;
300 gtk_widget_get_child_requisition (bin->child, &child_requisition);
301 if (child_requisition.height != 0)
303 ratio = (gdouble)child_requisition.width /
304 child_requisition.height;
305 if (ratio < MIN_RATIO) ratio = MIN_RATIO;
307 else if (child_requisition.width != 0)
313 ratio = aspect_frame->ratio;
315 x = (GTK_CONTAINER (frame)->border_width +
316 GTK_WIDGET (frame)->style->klass->xthickness);
317 width = allocation->width - x * 2;
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);
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);
330 if (ratio * height > width)
332 child_allocation.width = width;
333 child_allocation.height = width/ratio + 0.5;
337 child_allocation.width = ratio*height + 0.5;
338 child_allocation.height = height;
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;
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;
351 gtk_widget_size_allocate (bin->child, &child_allocation);