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 guint 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,
58 aspect_frame_type = gtk_type_unique (gtk_frame_get_type (), &aspect_frame_info);
61 return aspect_frame_type;
65 gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
67 GtkWidgetClass *widget_class;
69 widget_class = (GtkWidgetClass*) class;
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;
77 gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
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;
90 gtk_aspect_frame_new (const gchar *label,
96 GtkAspectFrame *aspect_frame;
98 aspect_frame = gtk_type_new (gtk_aspect_frame_get_type ());
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;
105 gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
107 return GTK_WIDGET (aspect_frame);
111 gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
117 g_return_if_fail (aspect_frame != NULL);
118 g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
120 xalign = CLAMP (xalign, 0.0, 1.0);
121 yalign = CLAMP (yalign, 0.0, 1.0);
122 ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
124 if ((aspect_frame->xalign != xalign) ||
125 (aspect_frame->yalign != yalign) ||
126 (aspect_frame->ratio != ratio) ||
127 (aspect_frame->obey_child != obey_child))
129 GtkWidget *widget = GTK_WIDGET(aspect_frame);
131 aspect_frame->xalign = xalign;
132 aspect_frame->yalign = yalign;
133 aspect_frame->ratio = ratio;
134 aspect_frame->obey_child = obey_child;
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);
143 gtk_widget_queue_resize (widget);
148 gtk_aspect_frame_paint (GtkWidget *widget,
153 gint label_area_width;
155 GtkAllocation *allocation;
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
159 g_return_if_fail (area != NULL);
161 if (GTK_WIDGET_DRAWABLE (widget))
163 frame = GTK_FRAME (widget);
164 allocation = >K_ASPECT_FRAME(widget)->center_allocation;
166 height_extra = frame->label_height - widget->style->klass->xthickness;
167 height_extra = MAX (height_extra, 0);
169 x = GTK_CONTAINER (frame)->border_width;
170 y = GTK_CONTAINER (frame)->border_width;
172 gtk_draw_shadow (widget->style, widget->window,
173 GTK_STATE_NORMAL, frame->shadow_type,
175 allocation->y + y + height_extra / 2,
176 allocation->width - x * 2,
177 allocation->height - y * 2 - height_extra / 2);
181 label_area_width = (allocation->width +
182 GTK_CONTAINER (frame)->border_width * 2 -
183 widget->style->klass->xthickness * 2);
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);
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,
201 /* the only modification to the next two routines is to call
202 gtk_aspect_frame_paint instead of gtk_frame_paint */
205 gtk_aspect_frame_draw (GtkWidget *widget,
209 GdkRectangle child_area;
211 g_return_if_fail (widget != NULL);
212 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
213 g_return_if_fail (area != NULL);
215 if (GTK_WIDGET_DRAWABLE (widget))
217 bin = GTK_BIN (widget);
219 gtk_aspect_frame_paint (widget, area);
221 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
222 gtk_widget_draw (bin->child, &child_area);
227 gtk_aspect_frame_expose (GtkWidget *widget,
228 GdkEventExpose *event)
231 GdkEventExpose child_event;
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);
237 if (GTK_WIDGET_DRAWABLE (widget))
239 bin = GTK_BIN (widget);
241 gtk_aspect_frame_paint (widget, &event->area);
243 child_event = *event;
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);
254 gtk_aspect_frame_size_allocate (GtkWidget *widget,
255 GtkAllocation *allocation)
258 GtkAspectFrame *aspect_frame;
261 GtkAllocation child_allocation;
266 g_return_if_fail (widget != NULL);
267 g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
268 g_return_if_fail (allocation != NULL);
270 aspect_frame = GTK_ASPECT_FRAME (widget);
271 frame = GTK_FRAME (widget);
272 bin = GTK_BIN (widget);
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);
287 widget->allocation = *allocation;
289 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
291 if (aspect_frame->obey_child)
293 if (bin->child->requisition.height != 0)
295 ratio = (gdouble)bin->child->requisition.width /
296 bin->child->requisition.height;
297 if (ratio < MIN_RATIO) ratio = MIN_RATIO;
300 if (bin->child->requisition.height != 0)
306 ratio = aspect_frame->ratio;
308 x = (GTK_CONTAINER (frame)->border_width +
309 GTK_WIDGET (frame)->style->klass->xthickness);
310 width = allocation->width - x * 2;
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);
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);
323 if (ratio * height > width)
325 child_allocation.width = width;
326 child_allocation.height = width/ratio + 0.5;
330 child_allocation.width = ratio*height + 0.5;
331 child_allocation.height = height;
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;
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;
344 gtk_widget_size_allocate (bin->child, &child_allocation);