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
/**
 * 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 45
 *
 * # CSS nodes
 *
 * GtkAspectFrame uses a CSS node with name frame.
46 47
 */

48
#include "config.h"
49

Elliot Lee's avatar
Elliot Lee committed
50
#include "gtkaspectframe.h"
51 52 53

#include "gtksizerequest.h"

54
#include "gtkprivate.h"
55
#include "gtkintl.h"
56

Elliot Lee's avatar
Elliot Lee committed
57

58

59
struct _GtkAspectFramePrivate
60 61 62 63 64 65 66 67 68 69
{
  GtkAllocation center_allocation;

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


70
enum {
71 72 73 74 75
  PROP_0,
  PROP_XALIGN,
  PROP_YALIGN,
  PROP_RATIO,
  PROP_OBEY_CHILD
76 77
};

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

#define MAX_RATIO 10000.0
#define MIN_RATIO 0.0001

92
G_DEFINE_TYPE_WITH_PRIVATE (GtkAspectFrame, gtk_aspect_frame, GTK_TYPE_FRAME)
Elliot Lee's avatar
Elliot Lee committed
93 94 95 96

static void
gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
{
97
  GObjectClass *gobject_class;
98
  GtkFrameClass *frame_class;
99
  
100 101
  gobject_class = (GObjectClass*) class;
  frame_class = (GtkFrameClass*) class;
102
  
103 104
  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
105

106
  frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
107

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

  gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (class), "frame");
Elliot Lee's avatar
Elliot Lee committed
138 139 140 141 142
}

static void
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
{
143
  aspect_frame->priv = gtk_aspect_frame_get_instance_private (aspect_frame);
144

145 146 147 148
  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
149 150
}

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

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

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

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

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

  gtk_frame_set_label (GTK_FRAME(aspect_frame), label);

  return GTK_WIDGET (aspect_frame);
}

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

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

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

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

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

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

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

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

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