gtkaspectframe.c 12.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GtkAspectFrame: Ensure that the child window has a specified aspect ratio
 *    or, if obey_child, has the same aspect ratio as its requested size
 *
 *     Copyright Owen Taylor                          4/9/97
 *
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
11 12 13 14 15 16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
20
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
21
 */
22 23

/*
24
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
25 26 27 28 29
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

30 31 32 33 34 35 36 37 38 39 40
/**
 * SECTION:gtkaspectframe
 * @Short_description: A frame that constrains its child to a particular aspect ratio
 * @Title: GtkAspectFrame
 *
 * The #GtkAspectFrame is useful when you want
 * pack a widget so that it can resize but always retains
 * the same aspect ratio. For instance, one might be
 * drawing a small preview of a larger image. #GtkAspectFrame
 * derives from #GtkFrame, so it can draw a label and
 * a frame around the child. The frame will be
41
 * “shrink-wrapped” to the size of the child.
42 43
 */

44
#include "config.h"
45

Elliot Lee's avatar
Elliot Lee committed
46
#include "gtkaspectframe.h"
47 48 49

#include "gtksizerequest.h"

50
#include "gtkprivate.h"
51
#include "gtkintl.h"
52

Elliot Lee's avatar
Elliot Lee committed
53

54

55
struct _GtkAspectFramePrivate
56 57 58 59 60 61 62 63 64 65
{
  GtkAllocation center_allocation;

  gboolean      obey_child;
  gfloat        xalign;
  gfloat        yalign;
  gfloat        ratio;
};


66
enum {
67 68 69 70 71
  PROP_0,
  PROP_XALIGN,
  PROP_YALIGN,
  PROP_RATIO,
  PROP_OBEY_CHILD
72 73
};

74 75 76
static void gtk_aspect_frame_set_property (GObject         *object,
					   guint            prop_id,
					   const GValue    *value,
Tim Janik's avatar
Tim Janik committed
77
					   GParamSpec      *pspec);
78 79 80
static void gtk_aspect_frame_get_property (GObject         *object,
					   guint            prop_id,
					   GValue          *value,
Tim Janik's avatar
Tim Janik committed
81
					   GParamSpec      *pspec);
82 83
static void gtk_aspect_frame_compute_child_allocation (GtkFrame            *frame,
						       GtkAllocation       *child_allocation);
Elliot Lee's avatar
Elliot Lee committed
84 85 86 87

#define MAX_RATIO 10000.0
#define MIN_RATIO 0.0001

88
G_DEFINE_TYPE_WITH_PRIVATE (GtkAspectFrame, gtk_aspect_frame, GTK_TYPE_FRAME)
Elliot Lee's avatar
Elliot Lee committed
89 90 91 92

static void
gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
{
93
  GObjectClass *gobject_class;
94
  GtkFrameClass *frame_class;
95
  
96 97
  gobject_class = (GObjectClass*) class;
  frame_class = (GtkFrameClass*) class;
98
  
99 100
  gobject_class->set_property = gtk_aspect_frame_set_property;
  gobject_class->get_property = gtk_aspect_frame_get_property;
Elliot Lee's avatar
Elliot Lee committed
101

102
  frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
103

104 105 106
  g_object_class_install_property (gobject_class,
                                   PROP_XALIGN,
                                   g_param_spec_float ("xalign",
107 108
                                                       P_("Horizontal Alignment"),
                                                       P_("X alignment of the child"),
109
                                                       0.0, 1.0, 0.5,
110
                                                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
111 112 113
  g_object_class_install_property (gobject_class,
                                   PROP_YALIGN,
                                   g_param_spec_float ("yalign",
114 115
                                                       P_("Vertical Alignment"),
                                                       P_("Y alignment of the child"),
116
                                                       0.0, 1.0, 0.5,
117
                                                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
118 119 120
  g_object_class_install_property (gobject_class,
                                   PROP_RATIO,
                                   g_param_spec_float ("ratio",
121 122
                                                       P_("Ratio"),
                                                       P_("Aspect ratio if obey_child is FALSE"),
123
                                                       MIN_RATIO, MAX_RATIO, 1.0,
124
                                                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
125 126
  g_object_class_install_property (gobject_class,
                                   PROP_OBEY_CHILD,
127
                                   g_param_spec_boolean ("obey-child",
128 129
                                                         P_("Obey child"),
                                                         P_("Force aspect ratio to match that of the frame's child"),
130
                                                         TRUE,
131
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
Elliot Lee's avatar
Elliot Lee committed
132 133 134 135 136
}

static void
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
{
137
  aspect_frame->priv = gtk_aspect_frame_get_instance_private (aspect_frame);
138

139 140 141 142
  aspect_frame->priv->xalign = 0.5;
  aspect_frame->priv->yalign = 0.5;
  aspect_frame->priv->ratio = 1.0;
  aspect_frame->priv->obey_child = TRUE;
Elliot Lee's avatar
Elliot Lee committed
143 144
}

Tim Janik's avatar
Tim Janik committed
145 146 147 148 149
static void
gtk_aspect_frame_set_property (GObject         *object,
			       guint            prop_id,
			       const GValue    *value,
			       GParamSpec      *pspec)
150 151
{
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
152
  GtkAspectFramePrivate *priv = aspect_frame->priv;
153
  
154
  switch (prop_id)
155
    {
156 157
      /* g_object_notify is handled by the _frame_set function */
    case PROP_XALIGN:
158
      gtk_aspect_frame_set (aspect_frame,
159
			    g_value_get_float (value),
160 161 162
			    priv->yalign,
			    priv->ratio,
			    priv->obey_child);
163
      break;
164
    case PROP_YALIGN:
165
      gtk_aspect_frame_set (aspect_frame,
166
			    priv->xalign,
167
			    g_value_get_float (value),
168 169
			    priv->ratio,
			    priv->obey_child);
170
      break;
171
    case PROP_RATIO:
172
      gtk_aspect_frame_set (aspect_frame,
173 174
			    priv->xalign,
			    priv->yalign,
175
			    g_value_get_float (value),
176
			    priv->obey_child);
177
      break;
178
    case PROP_OBEY_CHILD:
179
      gtk_aspect_frame_set (aspect_frame,
180 181 182
			    priv->xalign,
			    priv->yalign,
			    priv->ratio,
183 184 185 186
			    g_value_get_boolean (value));
      break;
    default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 188 189 190
      break;
    }
}

Tim Janik's avatar
Tim Janik committed
191 192 193 194 195
static void
gtk_aspect_frame_get_property (GObject         *object,
			       guint            prop_id,
			       GValue          *value,
			       GParamSpec      *pspec)
196 197
{
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
198
  GtkAspectFramePrivate *priv = aspect_frame->priv;
199
  
200
  switch (prop_id)
201
    {
202
    case PROP_XALIGN:
203
      g_value_set_float (value, priv->xalign);
204
      break;
205
    case PROP_YALIGN:
206
      g_value_set_float (value, priv->yalign);
207
      break;
208
    case PROP_RATIO:
209
      g_value_set_float (value, priv->ratio);
210
      break;
211
    case PROP_OBEY_CHILD:
212
      g_value_set_boolean (value, priv->obey_child);
213 214
      break;
    default:
215
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 217 218 219
      break;
    }
}

220 221
/**
 * gtk_aspect_frame_new:
222
 * @label: (allow-none): Label text.
223 224 225 226
 * @xalign: Horizontal alignment of the child within the allocation of
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
 *  to 1.0 (right aligned)
 * @yalign: Vertical alignment of the child within the allocation of
227 228
 *  the #GtkAspectFrame. This ranges from 0.0 (top aligned)
 *  to 1.0 (bottom aligned)
229 230 231 232 233 234 235 236
 * @ratio: The desired aspect ratio.
 * @obey_child: If %TRUE, @ratio is ignored, and the aspect
 *  ratio is taken from the requistion of the child.
 *
 * Create a new #GtkAspectFrame.
 *
 * Returns: the new #GtkAspectFrame.
 */
Elliot Lee's avatar
Elliot Lee committed
237 238
GtkWidget*
gtk_aspect_frame_new (const gchar *label,
239 240 241 242
		      gfloat       xalign,
		      gfloat       yalign,
		      gfloat       ratio,
		      gboolean     obey_child)
Elliot Lee's avatar
Elliot Lee committed
243 244
{
  GtkAspectFrame *aspect_frame;
245
  GtkAspectFramePrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
246

Manish Singh's avatar
Manish Singh committed
247
  aspect_frame = g_object_new (GTK_TYPE_ASPECT_FRAME, NULL);
Elliot Lee's avatar
Elliot Lee committed
248

249 250 251 252 253 254
  priv = aspect_frame->priv;

  priv->xalign = CLAMP (xalign, 0.0, 1.0);
  priv->yalign = CLAMP (yalign, 0.0, 1.0);
  priv->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
  priv->obey_child = obey_child != FALSE;
Elliot Lee's avatar
Elliot Lee committed
255 256 257 258 259 260

  gtk_frame_set_label (GTK_FRAME(aspect_frame), label);

  return GTK_WIDGET (aspect_frame);
}

261 262 263 264 265 266 267
/**
 * gtk_aspect_frame_set:
 * @aspect_frame: a #GtkAspectFrame
 * @xalign: Horizontal alignment of the child within the allocation of
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
 *  to 1.0 (right aligned)
 * @yalign: Vertical alignment of the child within the allocation of
268 269
 *  the #GtkAspectFrame. This ranges from 0.0 (top aligned)
 *  to 1.0 (bottom aligned)
270 271 272 273 274 275
 * @ratio: The desired aspect ratio.
 * @obey_child: If %TRUE, @ratio is ignored, and the aspect
 *  ratio is taken from the requistion of the child.
 *
 * Set parameters for an existing #GtkAspectFrame.
 */
Elliot Lee's avatar
Elliot Lee committed
276 277
void
gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
278 279 280 281
		      gfloat          xalign,
		      gfloat          yalign,
		      gfloat          ratio,
		      gboolean        obey_child)
Elliot Lee's avatar
Elliot Lee committed
282
{
283
  GtkAspectFramePrivate *priv;
284

Elliot Lee's avatar
Elliot Lee committed
285
  g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
286 287

  priv = aspect_frame->priv;
288
  
Elliot Lee's avatar
Elliot Lee committed
289 290 291
  xalign = CLAMP (xalign, 0.0, 1.0);
  yalign = CLAMP (yalign, 0.0, 1.0);
  ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
292 293
  obey_child = obey_child != FALSE;
  
294 295 296 297
  if (priv->xalign != xalign
      || priv->yalign != yalign
      || priv->ratio != ratio
      || priv->obey_child != obey_child)
Elliot Lee's avatar
Elliot Lee committed
298
    {
299 300
      g_object_freeze_notify (G_OBJECT (aspect_frame));

301
      if (priv->xalign != xalign)
302
        {
303
          priv->xalign = xalign;
304 305
          g_object_notify (G_OBJECT (aspect_frame), "xalign");
        }
306
      if (priv->yalign != yalign)
307
        {
308
          priv->yalign = yalign;
309 310
          g_object_notify (G_OBJECT (aspect_frame), "yalign");
        }
311
      if (priv->ratio != ratio)
312
        {
313
          priv->ratio = ratio;
314 315
          g_object_notify (G_OBJECT (aspect_frame), "ratio");
        }
316
      if (priv->obey_child != obey_child)
317
        {
318
          priv->obey_child = obey_child;
319
          g_object_notify (G_OBJECT (aspect_frame), "obey-child");
320 321 322
        }
      g_object_thaw_notify (G_OBJECT (aspect_frame));

323
      gtk_widget_queue_resize (GTK_WIDGET (aspect_frame));
324
    }
Elliot Lee's avatar
Elliot Lee committed
325 326 327
}

static void
328 329
gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
					   GtkAllocation *child_allocation)
Elliot Lee's avatar
Elliot Lee committed
330
{
331
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
332
  GtkAspectFramePrivate *priv = aspect_frame->priv;
333
  GtkBin *bin = GTK_BIN (frame);
Javier Jardón's avatar
Javier Jardón committed
334
  GtkWidget *child;
Elliot Lee's avatar
Elliot Lee committed
335 336
  gdouble ratio;

Javier Jardón's avatar
Javier Jardón committed
337 338
  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child))
Elliot Lee's avatar
Elliot Lee committed
339
    {
340 341
      GtkAllocation full_allocation;
      
342
      if (priv->obey_child)
Elliot Lee's avatar
Elliot Lee committed
343
	{
344 345
	  GtkRequisition child_requisition;

346
          gtk_widget_get_preferred_size (child, &child_requisition, NULL);
347
	  if (child_requisition.height != 0)
Elliot Lee's avatar
Elliot Lee committed
348
	    {
349 350 351 352
	      ratio = ((gdouble) child_requisition.width /
		       child_requisition.height);
	      if (ratio < MIN_RATIO)
		ratio = MIN_RATIO;
Elliot Lee's avatar
Elliot Lee committed
353
	    }
354
	  else if (child_requisition.width != 0)
355
	    ratio = MAX_RATIO;
Elliot Lee's avatar
Elliot Lee committed
356 357 358 359
	  else
	    ratio = 1.0;
	}
      else
360
	ratio = priv->ratio;
361

Matthias Clasen's avatar
Matthias Clasen committed
362
      GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, &full_allocation);
363
      
364
      if (ratio * full_allocation.height > full_allocation.width)
Elliot Lee's avatar
Elliot Lee committed
365
	{
366 367
	  child_allocation->width = full_allocation.width;
	  child_allocation->height = full_allocation.width / ratio + 0.5;
Elliot Lee's avatar
Elliot Lee committed
368
	}
Gnome CVS User's avatar
Gnome CVS User committed
369
      else
Elliot Lee's avatar
Elliot Lee committed
370
	{
371 372
	  child_allocation->width = ratio * full_allocation.height + 0.5;
	  child_allocation->height = full_allocation.height;
Elliot Lee's avatar
Elliot Lee committed
373
	}
374
      
375 376
      child_allocation->x = full_allocation.x + priv->xalign * (full_allocation.width - child_allocation->width);
      child_allocation->y = full_allocation.y + priv->yalign * (full_allocation.height - child_allocation->height);
Elliot Lee's avatar
Elliot Lee committed
377
    }
378
  else
Matthias Clasen's avatar
Matthias Clasen committed
379
    GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, child_allocation);
Elliot Lee's avatar
Elliot Lee committed
380
}