gtkaspectframe.c 12.2 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 41 42 43
/**
 * 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
 * "shrink-wrapped" to the size of the child.
 */

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

Matthias Clasen's avatar
Matthias Clasen committed
88
G_DEFINE_TYPE (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));
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));
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));
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));
132

133
  g_type_class_add_private (class, sizeof (GtkAspectFramePrivate));
Elliot Lee's avatar
Elliot Lee committed
134 135 136 137 138
}

static void
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
{
139
  GtkAspectFramePrivate *priv;
140 141 142

  aspect_frame->priv = G_TYPE_INSTANCE_GET_PRIVATE (aspect_frame,
                                                    GTK_TYPE_ASPECT_FRAME,
143
                                                    GtkAspectFramePrivate);
144 145 146 147 148 149
  priv = aspect_frame->priv;

  priv->xalign = 0.5;
  priv->yalign = 0.5;
  priv->ratio = 1.0;
  priv->obey_child = TRUE;
Elliot Lee's avatar
Elliot Lee committed
150 151
}

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

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

227 228 229 230 231 232 233
/**
 * gtk_aspect_frame_new:
 * @label: Label text.
 * @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
234 235
 *  the #GtkAspectFrame. This ranges from 0.0 (top aligned)
 *  to 1.0 (bottom aligned)
236 237 238 239 240 241 242 243
 * @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
244 245
GtkWidget*
gtk_aspect_frame_new (const gchar *label,
246 247 248 249
		      gfloat       xalign,
		      gfloat       yalign,
		      gfloat       ratio,
		      gboolean     obey_child)
Elliot Lee's avatar
Elliot Lee committed
250 251
{
  GtkAspectFrame *aspect_frame;
252
  GtkAspectFramePrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
253

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

256 257 258 259 260 261
  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
262 263 264 265 266 267

  gtk_frame_set_label (GTK_FRAME(aspect_frame), label);

  return GTK_WIDGET (aspect_frame);
}

268 269 270 271 272 273 274
/**
 * 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
275 276
 *  the #GtkAspectFrame. This ranges from 0.0 (top aligned)
 *  to 1.0 (bottom aligned)
277 278 279 280 281 282
 * @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
283 284
void
gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
285 286 287 288
		      gfloat          xalign,
		      gfloat          yalign,
		      gfloat          ratio,
		      gboolean        obey_child)
Elliot Lee's avatar
Elliot Lee committed
289
{
290
  GtkAspectFramePrivate *priv;
291

Elliot Lee's avatar
Elliot Lee committed
292
  g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
293 294

  priv = aspect_frame->priv;
295
  
Elliot Lee's avatar
Elliot Lee committed
296 297 298
  xalign = CLAMP (xalign, 0.0, 1.0);
  yalign = CLAMP (yalign, 0.0, 1.0);
  ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
299 300
  obey_child = obey_child != FALSE;
  
301 302 303 304
  if (priv->xalign != xalign
      || priv->yalign != yalign
      || priv->ratio != ratio
      || priv->obey_child != obey_child)
Elliot Lee's avatar
Elliot Lee committed
305
    {
306 307
      g_object_freeze_notify (G_OBJECT (aspect_frame));

308
      if (priv->xalign != xalign)
309
        {
310
          priv->xalign = xalign;
311 312
          g_object_notify (G_OBJECT (aspect_frame), "xalign");
        }
313
      if (priv->yalign != yalign)
314
        {
315
          priv->yalign = yalign;
316 317
          g_object_notify (G_OBJECT (aspect_frame), "yalign");
        }
318
      if (priv->ratio != ratio)
319
        {
320
          priv->ratio = ratio;
321 322
          g_object_notify (G_OBJECT (aspect_frame), "ratio");
        }
323
      if (priv->obey_child != obey_child)
324
        {
325
          priv->obey_child = obey_child;
326
          g_object_notify (G_OBJECT (aspect_frame), "obey-child");
327 328 329
        }
      g_object_thaw_notify (G_OBJECT (aspect_frame));

330
      gtk_widget_queue_resize (GTK_WIDGET (aspect_frame));
331
    }
Elliot Lee's avatar
Elliot Lee committed
332 333 334
}

static void
335 336
gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
					   GtkAllocation *child_allocation)
Elliot Lee's avatar
Elliot Lee committed
337
{
338
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
339
  GtkAspectFramePrivate *priv = aspect_frame->priv;
340
  GtkBin *bin = GTK_BIN (frame);
Javier Jardón's avatar
Javier Jardón committed
341
  GtkWidget *child;
Elliot Lee's avatar
Elliot Lee committed
342 343
  gdouble ratio;

Javier Jardón's avatar
Javier Jardón committed
344 345
  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child))
Elliot Lee's avatar
Elliot Lee committed
346
    {
347 348
      GtkAllocation full_allocation;
      
349
      if (priv->obey_child)
Elliot Lee's avatar
Elliot Lee committed
350
	{
351 352
	  GtkRequisition child_requisition;

353
          gtk_widget_get_preferred_size (child, &child_requisition, NULL);
354
	  if (child_requisition.height != 0)
Elliot Lee's avatar
Elliot Lee committed
355
	    {
356 357 358 359
	      ratio = ((gdouble) child_requisition.width /
		       child_requisition.height);
	      if (ratio < MIN_RATIO)
		ratio = MIN_RATIO;
Elliot Lee's avatar
Elliot Lee committed
360
	    }
361
	  else if (child_requisition.width != 0)
362
	    ratio = MAX_RATIO;
Elliot Lee's avatar
Elliot Lee committed
363 364 365 366
	  else
	    ratio = 1.0;
	}
      else
367
	ratio = priv->ratio;
368

Matthias Clasen's avatar
Matthias Clasen committed
369
      GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, &full_allocation);
370
      
371
      if (ratio * full_allocation.height > full_allocation.width)
Elliot Lee's avatar
Elliot Lee committed
372
	{
373 374
	  child_allocation->width = full_allocation.width;
	  child_allocation->height = full_allocation.width / ratio + 0.5;
Elliot Lee's avatar
Elliot Lee committed
375
	}
Gnome CVS User's avatar
Gnome CVS User committed
376
      else
Elliot Lee's avatar
Elliot Lee committed
377
	{
378 379
	  child_allocation->width = ratio * full_allocation.height + 0.5;
	  child_allocation->height = full_allocation.height;
Elliot Lee's avatar
Elliot Lee committed
380
	}
381
      
382 383
      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
384
    }
385
  else
Matthias Clasen's avatar
Matthias Clasen committed
386
    GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, child_allocation);
Elliot Lee's avatar
Elliot Lee committed
387
}