Blame gtk/gtkaspectframe.c

Packit 98cdb6
/* GTK - The GIMP Toolkit
Packit 98cdb6
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
Packit 98cdb6
 *
Packit 98cdb6
 * GtkAspectFrame: Ensure that the child window has a specified aspect ratio
Packit 98cdb6
 *    or, if obey_child, has the same aspect ratio as its requested size
Packit 98cdb6
 *
Packit 98cdb6
 *     Copyright Owen Taylor                          4/9/97
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
Packit 98cdb6
 * file for a list of people on the GTK+ Team.  See the ChangeLog
Packit 98cdb6
 * files for a list of changes.  These files are distributed with
Packit 98cdb6
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * SECTION:gtkaspectframe
Packit 98cdb6
 * @Short_description: A frame that constrains its child to a particular aspect ratio
Packit 98cdb6
 * @Title: GtkAspectFrame
Packit 98cdb6
 *
Packit 98cdb6
 * The #GtkAspectFrame is useful when you want
Packit 98cdb6
 * pack a widget so that it can resize but always retains
Packit 98cdb6
 * the same aspect ratio. For instance, one might be
Packit 98cdb6
 * drawing a small preview of a larger image. #GtkAspectFrame
Packit 98cdb6
 * derives from #GtkFrame, so it can draw a label and
Packit 98cdb6
 * a frame around the child. The frame will be
Packit 98cdb6
 * "shrink-wrapped" to the size of the child.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include "gtkaspectframe.h"
Packit 98cdb6
#include "gtkprivate.h"
Packit 98cdb6
#include "gtkintl.h"
Packit 98cdb6
#include "gtkalias.h"
Packit 98cdb6
Packit 98cdb6
enum {
Packit 98cdb6
  PROP_0,
Packit 98cdb6
  PROP_XALIGN,
Packit 98cdb6
  PROP_YALIGN,
Packit 98cdb6
  PROP_RATIO,
Packit 98cdb6
  PROP_OBEY_CHILD
Packit 98cdb6
};
Packit 98cdb6
Packit 98cdb6
static void gtk_aspect_frame_set_property (GObject         *object,
Packit 98cdb6
					   guint            prop_id,
Packit 98cdb6
					   const GValue    *value,
Packit 98cdb6
					   GParamSpec      *pspec);
Packit 98cdb6
static void gtk_aspect_frame_get_property (GObject         *object,
Packit 98cdb6
					   guint            prop_id,
Packit 98cdb6
					   GValue          *value,
Packit 98cdb6
					   GParamSpec      *pspec);
Packit 98cdb6
static void gtk_aspect_frame_compute_child_allocation (GtkFrame            *frame,
Packit 98cdb6
						       GtkAllocation       *child_allocation);
Packit 98cdb6
Packit 98cdb6
#define MAX_RATIO 10000.0
Packit 98cdb6
#define MIN_RATIO 0.0001
Packit 98cdb6
Packit 98cdb6
G_DEFINE_TYPE (GtkAspectFrame, gtk_aspect_frame, GTK_TYPE_FRAME)
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
Packit 98cdb6
{
Packit 98cdb6
  GObjectClass *gobject_class;
Packit 98cdb6
  GtkFrameClass *frame_class;
Packit 98cdb6
  
Packit 98cdb6
  gobject_class = (GObjectClass*) class;
Packit 98cdb6
  frame_class = (GtkFrameClass*) class;
Packit 98cdb6
  
Packit 98cdb6
  gobject_class->set_property = gtk_aspect_frame_set_property;
Packit 98cdb6
  gobject_class->get_property = gtk_aspect_frame_get_property;
Packit 98cdb6
Packit 98cdb6
  frame_class->compute_child_allocation = gtk_aspect_frame_compute_child_allocation;
Packit 98cdb6
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_XALIGN,
Packit 98cdb6
                                   g_param_spec_float ("xalign",
Packit 98cdb6
                                                       P_("Horizontal Alignment"),
Packit 98cdb6
                                                       P_("X alignment of the child"),
Packit 98cdb6
                                                       0.0, 1.0, 0.5,
Packit 98cdb6
                                                       GTK_PARAM_READWRITE));
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_YALIGN,
Packit 98cdb6
                                   g_param_spec_float ("yalign",
Packit 98cdb6
                                                       P_("Vertical Alignment"),
Packit 98cdb6
                                                       P_("Y alignment of the child"),
Packit 98cdb6
                                                       0.0, 1.0, 0.5,
Packit 98cdb6
                                                       GTK_PARAM_READWRITE));
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_RATIO,
Packit 98cdb6
                                   g_param_spec_float ("ratio",
Packit 98cdb6
                                                       P_("Ratio"),
Packit 98cdb6
                                                       P_("Aspect ratio if obey_child is FALSE"),
Packit 98cdb6
                                                       MIN_RATIO, MAX_RATIO, 1.0,
Packit 98cdb6
                                                       GTK_PARAM_READWRITE));
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_OBEY_CHILD,
Packit 98cdb6
                                   g_param_spec_boolean ("obey-child",
Packit 98cdb6
                                                         P_("Obey child"),
Packit 98cdb6
                                                         P_("Force aspect ratio to match that of the frame's child"),
Packit 98cdb6
                                                         TRUE,
Packit 98cdb6
                                                         GTK_PARAM_READWRITE));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
Packit 98cdb6
{
Packit 98cdb6
  aspect_frame->xalign = 0.5;
Packit 98cdb6
  aspect_frame->yalign = 0.5;
Packit 98cdb6
  aspect_frame->ratio = 1.0;
Packit 98cdb6
  aspect_frame->obey_child = TRUE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_aspect_frame_set_property (GObject         *object,
Packit 98cdb6
			       guint            prop_id,
Packit 98cdb6
			       const GValue    *value,
Packit 98cdb6
			       GParamSpec      *pspec)
Packit 98cdb6
{
Packit 98cdb6
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
Packit 98cdb6
  
Packit 98cdb6
  switch (prop_id)
Packit 98cdb6
    {
Packit 98cdb6
      /* g_object_notify is handled by the _frame_set function */
Packit 98cdb6
    case PROP_XALIGN:
Packit 98cdb6
      gtk_aspect_frame_set (aspect_frame,
Packit 98cdb6
			    g_value_get_float (value),
Packit 98cdb6
			    aspect_frame->yalign,
Packit 98cdb6
			    aspect_frame->ratio,
Packit 98cdb6
			    aspect_frame->obey_child);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YALIGN:
Packit 98cdb6
      gtk_aspect_frame_set (aspect_frame,
Packit 98cdb6
			    aspect_frame->xalign,
Packit 98cdb6
			    g_value_get_float (value),
Packit 98cdb6
			    aspect_frame->ratio,
Packit 98cdb6
			    aspect_frame->obey_child);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_RATIO:
Packit 98cdb6
      gtk_aspect_frame_set (aspect_frame,
Packit 98cdb6
			    aspect_frame->xalign,
Packit 98cdb6
			    aspect_frame->yalign,
Packit 98cdb6
			    g_value_get_float (value),
Packit 98cdb6
			    aspect_frame->obey_child);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_OBEY_CHILD:
Packit 98cdb6
      gtk_aspect_frame_set (aspect_frame,
Packit 98cdb6
			    aspect_frame->xalign,
Packit 98cdb6
			    aspect_frame->yalign,
Packit 98cdb6
			    aspect_frame->ratio,
Packit 98cdb6
			    g_value_get_boolean (value));
Packit 98cdb6
      break;
Packit 98cdb6
    default:
Packit 98cdb6
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 98cdb6
      break;
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_aspect_frame_get_property (GObject         *object,
Packit 98cdb6
			       guint            prop_id,
Packit 98cdb6
			       GValue          *value,
Packit 98cdb6
			       GParamSpec      *pspec)
Packit 98cdb6
{
Packit 98cdb6
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (object);
Packit 98cdb6
  
Packit 98cdb6
  switch (prop_id)
Packit 98cdb6
    {
Packit 98cdb6
    case PROP_XALIGN:
Packit 98cdb6
      g_value_set_float (value, aspect_frame->xalign);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YALIGN:
Packit 98cdb6
      g_value_set_float (value, aspect_frame->yalign);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_RATIO:
Packit 98cdb6
      g_value_set_float (value, aspect_frame->ratio);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_OBEY_CHILD:
Packit 98cdb6
      g_value_set_boolean (value, aspect_frame->obey_child);
Packit 98cdb6
      break;
Packit 98cdb6
    default:
Packit 98cdb6
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 98cdb6
      break;
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gtk_aspect_frame_new:
Packit 98cdb6
 * @label: Label text.
Packit 98cdb6
 * @xalign: Horizontal alignment of the child within the allocation of
Packit 98cdb6
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
Packit 98cdb6
 *  to 1.0 (right aligned)
Packit 98cdb6
 * @yalign: Vertical alignment of the child within the allocation of
Packit 98cdb6
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
Packit 98cdb6
 *  to 1.0 (right aligned)
Packit 98cdb6
 * @ratio: The desired aspect ratio.
Packit 98cdb6
 * @obey_child: If %TRUE, @ratio is ignored, and the aspect
Packit 98cdb6
 *  ratio is taken from the requistion of the child.
Packit 98cdb6
 *
Packit 98cdb6
 * Create a new #GtkAspectFrame.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: the new #GtkAspectFrame.
Packit 98cdb6
 */
Packit 98cdb6
GtkWidget*
Packit 98cdb6
gtk_aspect_frame_new (const gchar *label,
Packit 98cdb6
		      gfloat       xalign,
Packit 98cdb6
		      gfloat       yalign,
Packit 98cdb6
		      gfloat       ratio,
Packit 98cdb6
		      gboolean     obey_child)
Packit 98cdb6
{
Packit 98cdb6
  GtkAspectFrame *aspect_frame;
Packit 98cdb6
Packit 98cdb6
  aspect_frame = g_object_new (GTK_TYPE_ASPECT_FRAME, NULL);
Packit 98cdb6
Packit 98cdb6
  aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
Packit 98cdb6
  aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
Packit 98cdb6
  aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
Packit 98cdb6
  aspect_frame->obey_child = obey_child != FALSE;
Packit 98cdb6
Packit 98cdb6
  gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
Packit 98cdb6
Packit 98cdb6
  return GTK_WIDGET (aspect_frame);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gtk_aspect_frame_set:
Packit 98cdb6
 * @aspect_frame: a #GtkAspectFrame
Packit 98cdb6
 * @xalign: Horizontal alignment of the child within the allocation of
Packit 98cdb6
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
Packit 98cdb6
 *  to 1.0 (right aligned)
Packit 98cdb6
 * @yalign: Vertical alignment of the child within the allocation of
Packit 98cdb6
 *  the #GtkAspectFrame. This ranges from 0.0 (left aligned)
Packit 98cdb6
 *  to 1.0 (right aligned)
Packit 98cdb6
 * @ratio: The desired aspect ratio.
Packit 98cdb6
 * @obey_child: If %TRUE, @ratio is ignored, and the aspect
Packit 98cdb6
 *  ratio is taken from the requistion of the child.
Packit 98cdb6
 *
Packit 98cdb6
 * Set parameters for an existing #GtkAspectFrame.
Packit 98cdb6
 */
Packit 98cdb6
void
Packit 98cdb6
gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
Packit 98cdb6
		      gfloat          xalign,
Packit 98cdb6
		      gfloat          yalign,
Packit 98cdb6
		      gfloat          ratio,
Packit 98cdb6
		      gboolean        obey_child)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
Packit 98cdb6
  
Packit 98cdb6
  xalign = CLAMP (xalign, 0.0, 1.0);
Packit 98cdb6
  yalign = CLAMP (yalign, 0.0, 1.0);
Packit 98cdb6
  ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
Packit 98cdb6
  obey_child = obey_child != FALSE;
Packit 98cdb6
  
Packit 98cdb6
  if (   (aspect_frame->xalign != xalign)
Packit 98cdb6
      || (aspect_frame->yalign != yalign)
Packit 98cdb6
      || (aspect_frame->ratio != ratio)
Packit 98cdb6
      || (aspect_frame->obey_child != obey_child))
Packit 98cdb6
    {
Packit 98cdb6
      g_object_freeze_notify (G_OBJECT (aspect_frame));
Packit 98cdb6
Packit 98cdb6
      if (aspect_frame->xalign != xalign)
Packit 98cdb6
        {
Packit 98cdb6
          aspect_frame->xalign = xalign;
Packit 98cdb6
          g_object_notify (G_OBJECT (aspect_frame), "xalign");
Packit 98cdb6
        }
Packit 98cdb6
      if (aspect_frame->yalign != yalign)
Packit 98cdb6
        {
Packit 98cdb6
          aspect_frame->yalign = yalign;
Packit 98cdb6
          g_object_notify (G_OBJECT (aspect_frame), "yalign");
Packit 98cdb6
        }
Packit 98cdb6
      if (aspect_frame->ratio != ratio)
Packit 98cdb6
        {
Packit 98cdb6
          aspect_frame->ratio = ratio;
Packit 98cdb6
          g_object_notify (G_OBJECT (aspect_frame), "ratio");
Packit 98cdb6
        }
Packit 98cdb6
      if (aspect_frame->obey_child != obey_child)
Packit 98cdb6
        {
Packit 98cdb6
          aspect_frame->obey_child = obey_child;
Packit 98cdb6
          g_object_notify (G_OBJECT (aspect_frame), "obey-child");
Packit 98cdb6
        }
Packit 98cdb6
      g_object_thaw_notify (G_OBJECT (aspect_frame));
Packit 98cdb6
Packit 98cdb6
      gtk_widget_queue_resize (GTK_WIDGET (aspect_frame));
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_aspect_frame_compute_child_allocation (GtkFrame      *frame,
Packit 98cdb6
					   GtkAllocation *child_allocation)
Packit 98cdb6
{
Packit 98cdb6
  GtkAspectFrame *aspect_frame = GTK_ASPECT_FRAME (frame);
Packit 98cdb6
  GtkBin *bin = GTK_BIN (frame);
Packit 98cdb6
  gdouble ratio;
Packit 98cdb6
Packit 98cdb6
  if (bin->child && gtk_widget_get_visible (bin->child))
Packit 98cdb6
    {
Packit 98cdb6
      GtkAllocation full_allocation;
Packit 98cdb6
      
Packit 98cdb6
      if (aspect_frame->obey_child)
Packit 98cdb6
	{
Packit 98cdb6
	  GtkRequisition child_requisition;
Packit 98cdb6
Packit 98cdb6
	  gtk_widget_get_child_requisition (bin->child, &child_requisition);
Packit 98cdb6
	  if (child_requisition.height != 0)
Packit 98cdb6
	    {
Packit 98cdb6
	      ratio = ((gdouble) child_requisition.width /
Packit 98cdb6
		       child_requisition.height);
Packit 98cdb6
	      if (ratio < MIN_RATIO)
Packit 98cdb6
		ratio = MIN_RATIO;
Packit 98cdb6
	    }
Packit 98cdb6
	  else if (child_requisition.width != 0)
Packit 98cdb6
	    ratio = MAX_RATIO;
Packit 98cdb6
	  else
Packit 98cdb6
	    ratio = 1.0;
Packit 98cdb6
	}
Packit 98cdb6
      else
Packit 98cdb6
	ratio = aspect_frame->ratio;
Packit 98cdb6
Packit 98cdb6
      GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, &full_allocation);
Packit 98cdb6
      
Packit 98cdb6
      if (ratio * full_allocation.height > full_allocation.width)
Packit 98cdb6
	{
Packit 98cdb6
	  child_allocation->width = full_allocation.width;
Packit 98cdb6
	  child_allocation->height = full_allocation.width / ratio + 0.5;
Packit 98cdb6
	}
Packit 98cdb6
      else
Packit 98cdb6
	{
Packit 98cdb6
	  child_allocation->width = ratio * full_allocation.height + 0.5;
Packit 98cdb6
	  child_allocation->height = full_allocation.height;
Packit 98cdb6
	}
Packit 98cdb6
      
Packit 98cdb6
      child_allocation->x = full_allocation.x + aspect_frame->xalign * (full_allocation.width - child_allocation->width);
Packit 98cdb6
      child_allocation->y = full_allocation.y + aspect_frame->yalign * (full_allocation.height - child_allocation->height);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    GTK_FRAME_CLASS (gtk_aspect_frame_parent_class)->compute_child_allocation (frame, child_allocation);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#define __GTK_ASPECT_FRAME_C__
Packit 98cdb6
#include "gtkaliasdef.c"