gtkaspectframe.c 10.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9
/* GTK - The GIMP Toolkit
 * 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
20 21 22
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
23
 */
24 25

/*
26
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
27 28 29 30 31
 * 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
32
#include "gtkaspectframe.h"
33
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
34

35
enum {
36 37 38 39 40
  PROP_0,
  PROP_XALIGN,
  PROP_YALIGN,
  PROP_RATIO,
  PROP_OBEY_CHILD
41 42
};

43 44
static void gtk_aspect_frame_class_init               (GtkAspectFrameClass *klass);
static void gtk_aspect_frame_init                     (GtkAspectFrame      *aspect_frame);
45 46 47
static void gtk_aspect_frame_set_property (GObject         *object,
					   guint            prop_id,
					   const GValue    *value,
Tim Janik's avatar
Tim Janik committed
48
					   GParamSpec      *pspec);
49 50 51
static void gtk_aspect_frame_get_property (GObject         *object,
					   guint            prop_id,
					   GValue          *value,
Tim Janik's avatar
Tim Janik committed
52
					   GParamSpec      *pspec);
53 54
static void gtk_aspect_frame_compute_child_allocation (GtkFrame            *frame,
						       GtkAllocation       *child_allocation);
Elliot Lee's avatar
Elliot Lee committed
55 56 57 58

#define MAX_RATIO 10000.0
#define MIN_RATIO 0.0001

59 60
static GtkFrameClass *parent_class = NULL;

Manish Singh's avatar
Manish Singh committed
61
GType
62
gtk_aspect_frame_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
63
{
Manish Singh's avatar
Manish Singh committed
64
  static GType aspect_frame_type = 0;
65
  
Elliot Lee's avatar
Elliot Lee committed
66 67
  if (!aspect_frame_type)
    {
Manish Singh's avatar
Manish Singh committed
68
      static const GTypeInfo aspect_frame_info =
Elliot Lee's avatar
Elliot Lee committed
69 70
      {
	sizeof (GtkAspectFrameClass),
Manish Singh's avatar
Manish Singh committed
71 72 73 74 75 76 77 78
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_aspect_frame_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkAspectFrame),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_aspect_frame_init,
Elliot Lee's avatar
Elliot Lee committed
79
      };
80
      
Manish Singh's avatar
Manish Singh committed
81 82 83
      aspect_frame_type =
	g_type_register_static (GTK_TYPE_FRAME, "GtkAspectFrame",
				&aspect_frame_info, 0);
Elliot Lee's avatar
Elliot Lee committed
84
    }
85
  
Elliot Lee's avatar
Elliot Lee committed
86 87 88 89 90 91
  return aspect_frame_type;
}

static void
gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
{
92
  GObjectClass *gobject_class;
93
  GtkFrameClass *frame_class;
94
  
Manish Singh's avatar
Manish Singh committed
95
  parent_class = g_type_class_peek_parent (class);
96

97 98
  gobject_class = (GObjectClass*) class;
  frame_class = (GtkFrameClass*) class;
99
  
100 101
  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
102

103
  frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
104

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

static void
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
{
  aspect_frame->xalign = 0.5;
  aspect_frame->yalign = 0.5;
  aspect_frame->ratio = 1.0;
141
  aspect_frame->obey_child = TRUE;
Elliot Lee's avatar
Elliot Lee committed
142 143
}

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

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

Elliot Lee's avatar
Elliot Lee committed
217 218
GtkWidget*
gtk_aspect_frame_new (const gchar *label,
219 220 221 222
		      gfloat       xalign,
		      gfloat       yalign,
		      gfloat       ratio,
		      gboolean     obey_child)
Elliot Lee's avatar
Elliot Lee committed
223 224 225
{
  GtkAspectFrame *aspect_frame;

Manish Singh's avatar
Manish Singh committed
226
  aspect_frame = g_object_new (GTK_TYPE_ASPECT_FRAME, NULL);
Elliot Lee's avatar
Elliot Lee committed
227 228 229 230

  aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
  aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
  aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
231
  aspect_frame->obey_child = obey_child != FALSE;
Elliot Lee's avatar
Elliot Lee committed
232 233 234 235 236 237 238 239

  gtk_frame_set_label (GTK_FRAME(aspect_frame), label);

  return GTK_WIDGET (aspect_frame);
}

void
gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
240 241 242 243
		      gfloat          xalign,
		      gfloat          yalign,
		      gfloat          ratio,
		      gboolean        obey_child)
Elliot Lee's avatar
Elliot Lee committed
244 245
{
  g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
246
  
Elliot Lee's avatar
Elliot Lee committed
247 248 249
  xalign = CLAMP (xalign, 0.0, 1.0);
  yalign = CLAMP (yalign, 0.0, 1.0);
  ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
250 251
  obey_child = obey_child != FALSE;
  
Hans Breuer's avatar
Hans Breuer committed
252 253 254 255
  if (   (aspect_frame->xalign != xalign)
      || (aspect_frame->yalign != yalign)
      || (aspect_frame->ratio != ratio)
      || (aspect_frame->obey_child != obey_child))
Elliot Lee's avatar
Elliot Lee committed
256
    {
Hans Breuer's avatar
Hans Breuer committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      g_object_freeze_notify (G_OBJECT (aspect_frame));

      if (aspect_frame->xalign != xalign)
        {
          aspect_frame->xalign = xalign;
          g_object_notify (G_OBJECT (aspect_frame), "xalign");
        }
      if (aspect_frame->yalign != yalign)
        {
          aspect_frame->yalign = yalign;
          g_object_notify (G_OBJECT (aspect_frame), "yalign");
        }
      if (aspect_frame->ratio != ratio)
        {
          aspect_frame->ratio = ratio;
          g_object_notify (G_OBJECT (aspect_frame), "ratio");
        }
      if (aspect_frame->obey_child != obey_child)
        {
          aspect_frame->obey_child = obey_child;
          g_object_notify (G_OBJECT (aspect_frame), "obey_child");
        }
      g_object_thaw_notify (G_OBJECT (aspect_frame));

281
      gtk_widget_queue_resize (GTK_WIDGET (aspect_frame));
Hans Breuer's avatar
Hans Breuer committed
282
    }
Elliot Lee's avatar
Elliot Lee committed
283 284 285
}

static void
286 287
gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
					   GtkAllocation *child_allocation)
Elliot Lee's avatar
Elliot Lee committed
288
{
289 290
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
  GtkBin *bin = GTK_BIN (frame);
Elliot Lee's avatar
Elliot Lee committed
291 292 293 294
  gdouble ratio;

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
295 296
      GtkAllocation full_allocation;
      
Elliot Lee's avatar
Elliot Lee committed
297 298
      if (aspect_frame->obey_child)
	{
299 300 301 302
	  GtkRequisition child_requisition;

	  gtk_widget_get_child_requisition (bin->child, &child_requisition);
	  if (child_requisition.height != 0)
Elliot Lee's avatar
Elliot Lee committed
303
	    {
304 305 306 307
	      ratio = ((gdouble) child_requisition.width /
		       child_requisition.height);
	      if (ratio < MIN_RATIO)
		ratio = MIN_RATIO;
Elliot Lee's avatar
Elliot Lee committed
308
	    }
309
	  else if (child_requisition.width != 0)
310
	    ratio = MAX_RATIO;
Elliot Lee's avatar
Elliot Lee committed
311 312 313 314 315
	  else
	    ratio = 1.0;
	}
      else
	ratio = aspect_frame->ratio;
316 317

      parent_class->compute_child_allocation (frame, &full_allocation);
318
      
319
      if (ratio * full_allocation.height > full_allocation.width)
Elliot Lee's avatar
Elliot Lee committed
320
	{
321 322
	  child_allocation->width = full_allocation.width;
	  child_allocation->height = full_allocation.width / ratio + 0.5;
Elliot Lee's avatar
Elliot Lee committed
323
	}
Gnome CVS User's avatar
Gnome CVS User committed
324
      else
Elliot Lee's avatar
Elliot Lee committed
325
	{
326 327
	  child_allocation->width = ratio * full_allocation.height + 0.5;
	  child_allocation->height = full_allocation.height;
Elliot Lee's avatar
Elliot Lee committed
328
	}
329
      
330 331
      child_allocation->x = full_allocation.x + aspect_frame->xalign * (full_allocation.width - child_allocation->width);
      child_allocation->y = full_allocation.y + aspect_frame->yalign * (full_allocation.height - child_allocation->height);
Elliot Lee's avatar
Elliot Lee committed
332
    }
333 334
  else
    parent_class->compute_child_allocation (frame, child_allocation);
Elliot Lee's avatar
Elliot Lee committed
335
}