Blame gtk/gtkaspectframe.c

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