2005-09-20 06:58:16 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpzoommodel.c
|
|
|
|
* Copyright (C) 2005 David Odin <dindinx@gimp.org>
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2005-09-20 06:58:16 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2005-09-20 06:58:16 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2005-09-20 06:58:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "gimpwidgetstypes.h"
|
|
|
|
|
2005-09-28 19:02:02 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2005-09-25 18:17:41 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2005-09-28 19:02:02 +08:00
|
|
|
#include "gimphelpui.h"
|
2005-10-01 23:12:59 +08:00
|
|
|
#include "gimpwidgetsmarshal.h"
|
2005-09-20 06:58:16 +08:00
|
|
|
#include "gimpzoommodel.h"
|
|
|
|
|
|
|
|
|
2010-07-06 00:01:28 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpzoommodel
|
|
|
|
* @title: GimpZoomModel
|
|
|
|
* @short_description: A model for zoom values.
|
|
|
|
*
|
|
|
|
* A model for zoom values.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2005-09-25 20:10:01 +08:00
|
|
|
#define ZOOM_MIN (1.0 / 256.0)
|
|
|
|
#define ZOOM_MAX (256.0)
|
|
|
|
|
2005-10-01 23:12:59 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ZOOMED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2005-09-23 01:24:50 +08:00
|
|
|
PROP_VALUE,
|
|
|
|
PROP_MINIMUM,
|
2005-09-25 05:12:56 +08:00
|
|
|
PROP_MAXIMUM,
|
|
|
|
PROP_FRACTION,
|
|
|
|
PROP_PERCENTAGE
|
2005-09-20 06:58:16 +08:00
|
|
|
};
|
|
|
|
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2018-05-03 18:51:36 +08:00
|
|
|
struct _GimpZoomModelPrivate
|
2005-09-21 03:56:52 +08:00
|
|
|
{
|
2005-09-23 01:24:50 +08:00
|
|
|
gdouble value;
|
|
|
|
gdouble minimum;
|
|
|
|
gdouble maximum;
|
2018-05-03 18:51:36 +08:00
|
|
|
};
|
2006-01-25 22:35:56 +08:00
|
|
|
|
2018-05-03 18:51:36 +08:00
|
|
|
#define GET_PRIVATE(obj) (((GimpZoomModel *) (obj))->priv)
|
2005-09-21 03:56:52 +08:00
|
|
|
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
static void gimp_zoom_model_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_zoom_model_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
|
2005-10-01 23:12:59 +08:00
|
|
|
static guint zoom_model_signals[LAST_SIGNAL] = { 0, };
|
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
G_DEFINE_TYPE (GimpZoomModel, gimp_zoom_model, G_TYPE_OBJECT)
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
#define parent_class gimp_zoom_model_parent_class
|
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
static void
|
|
|
|
gimp_zoom_model_class_init (GimpZoomModelClass *klass)
|
|
|
|
{
|
2005-10-01 23:12:59 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GimpZoomModel::zoomed:
|
|
|
|
* @model: the object that received the signal
|
|
|
|
* @old_factor: the zoom factor before it changes
|
|
|
|
* @new_factor: the zoom factor after it has changed.
|
|
|
|
*
|
2006-05-15 17:46:31 +08:00
|
|
|
* Emitted when the zoom factor of the zoom model changes.
|
2005-10-01 23:12:59 +08:00
|
|
|
*/
|
|
|
|
zoom_model_signals[ZOOMED] =
|
|
|
|
g_signal_new ("zoomed",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GimpZoomModelClass,
|
|
|
|
zoomed),
|
|
|
|
NULL, NULL,
|
|
|
|
_gimp_widgets_marshal_VOID__DOUBLE_DOUBLE,
|
|
|
|
G_TYPE_NONE, 2,
|
|
|
|
G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
2005-09-20 06:58:16 +08:00
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
object_class->set_property = gimp_zoom_model_set_property;
|
|
|
|
object_class->get_property = gimp_zoom_model_get_property;
|
2005-09-20 06:58:16 +08:00
|
|
|
|
2017-06-07 03:19:17 +08:00
|
|
|
/**
|
|
|
|
* GimpZoomModel:value:
|
|
|
|
*
|
|
|
|
* The zoom factor.
|
|
|
|
*/
|
2005-09-23 01:24:50 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_VALUE,
|
|
|
|
g_param_spec_double ("value",
|
2017-06-07 03:19:17 +08:00
|
|
|
"Value",
|
|
|
|
"Zoom factor",
|
2005-09-25 20:10:01 +08:00
|
|
|
ZOOM_MIN, ZOOM_MAX,
|
2005-09-20 06:58:16 +08:00
|
|
|
1.0,
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READWRITE));
|
2017-06-07 03:19:17 +08:00
|
|
|
/**
|
|
|
|
* GimpZoomModel:minimum:
|
|
|
|
*
|
|
|
|
* The minimum zoom factor.
|
|
|
|
*/
|
2005-09-23 01:24:50 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_MINIMUM,
|
|
|
|
g_param_spec_double ("minimum",
|
2017-06-07 03:19:17 +08:00
|
|
|
"Minimum",
|
|
|
|
"Lower limit for the zoom factor",
|
2005-09-25 20:10:01 +08:00
|
|
|
ZOOM_MIN, ZOOM_MAX,
|
|
|
|
ZOOM_MIN,
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READWRITE));
|
2017-06-07 03:19:17 +08:00
|
|
|
/**
|
|
|
|
* GimpZoomModel:maximum:
|
|
|
|
*
|
|
|
|
* The maximum zoom factor.
|
|
|
|
*/
|
2005-09-23 01:24:50 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_MAXIMUM,
|
|
|
|
g_param_spec_double ("maximum",
|
2017-06-07 03:19:17 +08:00
|
|
|
"Maximum",
|
|
|
|
"Upper limit for the zoom factor",
|
2005-09-25 20:10:01 +08:00
|
|
|
ZOOM_MIN, ZOOM_MAX,
|
|
|
|
ZOOM_MAX,
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READWRITE));
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2017-06-07 03:19:17 +08:00
|
|
|
/**
|
|
|
|
* GimpZoomModel:fraction:
|
|
|
|
*
|
|
|
|
* The zoom factor expressed as a fraction.
|
|
|
|
*/
|
2005-09-25 05:12:56 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_FRACTION,
|
|
|
|
g_param_spec_string ("fraction",
|
2017-06-07 03:19:17 +08:00
|
|
|
"Fraction",
|
|
|
|
"The zoom factor expressed as a fraction",
|
2005-09-25 05:12:56 +08:00
|
|
|
"1:1",
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READABLE));
|
2017-06-07 03:19:17 +08:00
|
|
|
/**
|
|
|
|
* GimpZoomModel:percentage:
|
|
|
|
*
|
|
|
|
* The zoom factor expressed as percentage.
|
|
|
|
*/
|
2005-09-25 05:12:56 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_PERCENTAGE,
|
|
|
|
g_param_spec_string ("percentage",
|
2017-06-07 03:19:17 +08:00
|
|
|
"Percentage",
|
|
|
|
"The zoom factor expressed as a percentage",
|
2005-09-25 05:12:56 +08:00
|
|
|
"100%",
|
2006-01-31 00:10:56 +08:00
|
|
|
GIMP_PARAM_READABLE));
|
2005-09-25 05:12:56 +08:00
|
|
|
|
2005-09-21 03:56:52 +08:00
|
|
|
g_type_class_add_private (object_class, sizeof (GimpZoomModelPrivate));
|
2005-09-20 06:58:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-09-23 01:24:50 +08:00
|
|
|
gimp_zoom_model_init (GimpZoomModel *model)
|
2005-09-20 06:58:16 +08:00
|
|
|
{
|
2006-01-25 22:35:56 +08:00
|
|
|
GimpZoomModelPrivate *priv;
|
|
|
|
|
|
|
|
model->priv = G_TYPE_INSTANCE_GET_PRIVATE (model,
|
|
|
|
GIMP_TYPE_ZOOM_MODEL,
|
|
|
|
GimpZoomModelPrivate);
|
|
|
|
|
2018-05-03 18:51:36 +08:00
|
|
|
priv = GET_PRIVATE (model);
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2005-09-25 18:17:41 +08:00
|
|
|
priv->value = 1.0;
|
2005-09-26 01:03:03 +08:00
|
|
|
priv->minimum = ZOOM_MIN;
|
|
|
|
priv->maximum = ZOOM_MAX;
|
2005-09-20 06:58:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_zoom_model_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (object);
|
2005-10-01 23:12:59 +08:00
|
|
|
gdouble previous_value;
|
2005-09-20 06:58:16 +08:00
|
|
|
|
2005-10-01 23:12:59 +08:00
|
|
|
previous_value = priv->value;
|
2005-09-25 18:17:41 +08:00
|
|
|
g_object_freeze_notify (object);
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
switch (property_id)
|
|
|
|
{
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_VALUE:
|
|
|
|
priv->value = g_value_get_double (value);
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2005-10-01 23:12:59 +08:00
|
|
|
g_object_notify (object, "value");
|
2005-09-25 05:12:56 +08:00
|
|
|
g_object_notify (object, "fraction");
|
|
|
|
g_object_notify (object, "percentage");
|
2005-09-20 06:58:16 +08:00
|
|
|
break;
|
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_MINIMUM:
|
|
|
|
priv->minimum = MIN (g_value_get_double (value), priv->maximum);
|
2005-09-21 03:56:52 +08:00
|
|
|
break;
|
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_MAXIMUM:
|
|
|
|
priv->maximum = MAX (g_value_get_double (value), priv->minimum);
|
2005-09-20 06:58:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
if (priv->value > priv->maximum || priv->value < priv->minimum)
|
2005-09-21 03:56:52 +08:00
|
|
|
{
|
2005-09-23 01:24:50 +08:00
|
|
|
priv->value = CLAMP (priv->value, priv->minimum, priv->maximum);
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
g_object_notify (object, "value");
|
2005-09-25 05:12:56 +08:00
|
|
|
g_object_notify (object, "fraction");
|
|
|
|
g_object_notify (object, "percentage");
|
2005-09-21 03:56:52 +08:00
|
|
|
}
|
2005-09-25 18:17:41 +08:00
|
|
|
|
|
|
|
g_object_thaw_notify (object);
|
2005-11-21 20:27:13 +08:00
|
|
|
|
2005-10-01 23:12:59 +08:00
|
|
|
if (priv->value != previous_value)
|
|
|
|
{
|
|
|
|
g_signal_emit (object, zoom_model_signals[ZOOMED],
|
|
|
|
0, previous_value, priv->value);
|
|
|
|
}
|
2005-09-20 06:58:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_zoom_model_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (object);
|
2005-09-25 05:12:56 +08:00
|
|
|
gchar *tmp;
|
2005-09-20 06:58:16 +08:00
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_VALUE:
|
|
|
|
g_value_set_double (value, priv->value);
|
2005-09-20 06:58:16 +08:00
|
|
|
break;
|
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_MINIMUM:
|
|
|
|
g_value_set_double (value, priv->minimum);
|
2005-09-21 03:56:52 +08:00
|
|
|
break;
|
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
case PROP_MAXIMUM:
|
|
|
|
g_value_set_double (value, priv->maximum);
|
2005-09-20 06:58:16 +08:00
|
|
|
break;
|
|
|
|
|
2005-09-25 05:12:56 +08:00
|
|
|
case PROP_FRACTION:
|
2005-09-26 01:03:03 +08:00
|
|
|
{
|
|
|
|
gint numerator;
|
|
|
|
gint denominator;
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
gimp_zoom_model_get_fraction (GIMP_ZOOM_MODEL (object),
|
|
|
|
&numerator, &denominator);
|
|
|
|
|
|
|
|
tmp = g_strdup_printf ("%d:%d", numerator, denominator);
|
|
|
|
g_value_set_string (value, tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
}
|
2005-09-25 05:12:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PERCENTAGE:
|
2005-09-26 01:03:03 +08:00
|
|
|
tmp = g_strdup_printf (priv->value >= 0.15 ? "%.0f%%" : "%.2f%%",
|
|
|
|
priv->value * 100.0);
|
2005-09-25 05:12:56 +08:00
|
|
|
g_value_set_string (value, tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
break;
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-09-23 01:24:50 +08:00
|
|
|
gimp_zoom_model_zoom_in (GimpZoomModel *model)
|
2005-09-20 06:58:16 +08:00
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (model);
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
if (priv->value < priv->maximum)
|
|
|
|
gimp_zoom_model_zoom (model, GIMP_ZOOM_IN, 0.0);
|
2005-09-20 06:58:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-09-23 01:24:50 +08:00
|
|
|
gimp_zoom_model_zoom_out (GimpZoomModel *model)
|
2005-09-20 06:58:16 +08:00
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (model);
|
2005-09-21 03:56:52 +08:00
|
|
|
|
2005-09-23 01:24:50 +08:00
|
|
|
if (priv->value > priv->minimum)
|
2005-09-26 01:03:03 +08:00
|
|
|
gimp_zoom_model_zoom (model, GIMP_ZOOM_OUT, 0.0);
|
2005-09-20 06:58:16 +08:00
|
|
|
}
|
|
|
|
|
2005-09-25 18:17:41 +08:00
|
|
|
/**
|
|
|
|
* gimp_zoom_model_new:
|
|
|
|
*
|
|
|
|
* Creates a new #GimpZoomModel.
|
|
|
|
*
|
|
|
|
* Return value: a new #GimpZoomModel.
|
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
|
|
|
GimpZoomModel *
|
|
|
|
gimp_zoom_model_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GIMP_TYPE_ZOOM_MODEL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_zoom_model_set_range:
|
|
|
|
* @model: a #GimpZoomModel
|
|
|
|
* @min: new lower limit for zoom factor
|
|
|
|
* @max: new upper limit for zoom factor
|
|
|
|
*
|
|
|
|
* Sets the allowed range of the @model.
|
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_zoom_model_set_range (GimpZoomModel *model,
|
|
|
|
gdouble min,
|
|
|
|
gdouble max)
|
|
|
|
{
|
2005-09-26 01:03:03 +08:00
|
|
|
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
|
2005-09-25 18:17:41 +08:00
|
|
|
g_return_if_fail (min < max);
|
2005-09-25 20:10:01 +08:00
|
|
|
g_return_if_fail (min >= ZOOM_MIN);
|
|
|
|
g_return_if_fail (max <= ZOOM_MAX);
|
2005-09-25 18:17:41 +08:00
|
|
|
|
|
|
|
g_object_set (model,
|
2005-09-27 14:53:19 +08:00
|
|
|
"minimum", min,
|
|
|
|
"maximum", max,
|
|
|
|
NULL);
|
2005-09-25 18:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-26 01:03:03 +08:00
|
|
|
* gimp_zoom_model_zoom:
|
|
|
|
* @model: a #GimpZoomModel
|
|
|
|
* @zoom_type: the #GimpZoomType
|
|
|
|
* @scale: ignored unless @zoom_type == %GIMP_ZOOM_TO
|
2005-09-25 18:17:41 +08:00
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
2005-09-26 01:03:03 +08:00
|
|
|
void
|
|
|
|
gimp_zoom_model_zoom (GimpZoomModel *model,
|
|
|
|
GimpZoomType zoom_type,
|
|
|
|
gdouble scale)
|
2005-09-25 18:17:41 +08:00
|
|
|
{
|
2018-05-11 08:08:51 +08:00
|
|
|
gdouble delta = 0.0;
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2018-05-11 08:08:51 +08:00
|
|
|
if (zoom_type == GIMP_ZOOM_SMOOTH)
|
|
|
|
delta = scale;
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
if (zoom_type != GIMP_ZOOM_TO)
|
|
|
|
scale = gimp_zoom_model_get_factor (model);
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
g_object_set (model,
|
2018-05-11 08:08:51 +08:00
|
|
|
"value", gimp_zoom_model_zoom_step (zoom_type, scale, delta),
|
2005-09-26 01:03:03 +08:00
|
|
|
NULL);
|
2005-09-25 18:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-26 01:03:03 +08:00
|
|
|
* gimp_zoom_model_get_factor:
|
2005-09-25 18:17:41 +08:00
|
|
|
* @model: a #GimpZoomModel
|
|
|
|
*
|
2005-09-26 01:03:03 +08:00
|
|
|
* Retrieves the current zoom factor of @model.
|
2005-09-25 18:17:41 +08:00
|
|
|
*
|
2005-09-26 01:03:03 +08:00
|
|
|
* Return value: the current scale factor
|
2005-09-25 18:17:41 +08:00
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
2005-09-26 01:03:03 +08:00
|
|
|
gdouble
|
|
|
|
gimp_zoom_model_get_factor (GimpZoomModel *model)
|
2005-09-25 18:17:41 +08:00
|
|
|
{
|
2005-09-26 01:03:03 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_ZOOM_MODEL (model), 1.0);
|
2005-09-25 18:17:41 +08:00
|
|
|
|
2018-05-03 18:51:36 +08:00
|
|
|
return GET_PRIVATE (model)->value;
|
2005-09-25 18:17:41 +08:00
|
|
|
}
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
|
2005-09-25 18:17:41 +08:00
|
|
|
/**
|
|
|
|
* gimp_zoom_model_get_fraction
|
2005-09-26 01:03:03 +08:00
|
|
|
* @model: a #GimpZoomModel
|
|
|
|
* @numerator: return location for numerator
|
2005-09-25 18:17:41 +08:00
|
|
|
* @denominator: return location for denominator
|
|
|
|
*
|
2005-09-26 01:03:03 +08:00
|
|
|
* Retrieves the current zoom factor of @model as a fraction.
|
2005-09-25 18:17:41 +08:00
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
2005-09-20 06:58:16 +08:00
|
|
|
void
|
2005-09-26 01:03:03 +08:00
|
|
|
gimp_zoom_model_get_fraction (GimpZoomModel *model,
|
|
|
|
gint *numerator,
|
|
|
|
gint *denominator)
|
2005-09-20 06:58:16 +08:00
|
|
|
{
|
|
|
|
gint p0, p1, p2;
|
|
|
|
gint q0, q1, q2;
|
2005-09-26 01:03:03 +08:00
|
|
|
gdouble zoom_factor;
|
2005-09-20 06:58:16 +08:00
|
|
|
gdouble remainder, next_cf;
|
|
|
|
gboolean swapped = FALSE;
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
|
2005-09-20 06:58:16 +08:00
|
|
|
g_return_if_fail (numerator != NULL && denominator != NULL);
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
zoom_factor = gimp_zoom_model_get_factor (model);
|
|
|
|
|
2005-09-20 06:58:16 +08:00
|
|
|
/* make sure that zooming behaves symmetrically */
|
|
|
|
if (zoom_factor < 1.0)
|
|
|
|
{
|
|
|
|
zoom_factor = 1.0 / zoom_factor;
|
|
|
|
swapped = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the continued fraction for the desired zoom factor */
|
|
|
|
|
|
|
|
p0 = 1;
|
|
|
|
q0 = 0;
|
|
|
|
p1 = floor (zoom_factor);
|
|
|
|
q1 = 1;
|
|
|
|
|
|
|
|
remainder = zoom_factor - p1;
|
|
|
|
|
|
|
|
while (fabs (remainder) >= 0.0001 &&
|
|
|
|
fabs (((gdouble) p1 / q1) - zoom_factor) > 0.0001)
|
|
|
|
{
|
|
|
|
remainder = 1.0 / remainder;
|
|
|
|
|
|
|
|
next_cf = floor (remainder);
|
|
|
|
|
|
|
|
p2 = next_cf * p1 + p0;
|
|
|
|
q2 = next_cf * q1 + q0;
|
|
|
|
|
|
|
|
/* Numerator and Denominator are limited by 256 */
|
|
|
|
/* also absurd ratios like 170:171 are excluded */
|
|
|
|
if (p2 > 256 || q2 > 256 || (p2 > 1 && q2 > 1 && p2 * q2 > 200))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* remember the last two fractions */
|
|
|
|
p0 = p1;
|
|
|
|
p1 = p2;
|
|
|
|
q0 = q1;
|
|
|
|
q1 = q2;
|
|
|
|
|
|
|
|
remainder = remainder - next_cf;
|
|
|
|
}
|
|
|
|
|
|
|
|
zoom_factor = (gdouble) p1 / q1;
|
|
|
|
|
|
|
|
/* hard upper and lower bounds for zoom ratio */
|
|
|
|
|
|
|
|
if (zoom_factor > 256.0)
|
|
|
|
{
|
|
|
|
p1 = 256;
|
|
|
|
q1 = 1;
|
|
|
|
}
|
|
|
|
else if (zoom_factor < 1.0 / 256.0)
|
|
|
|
{
|
|
|
|
p1 = 1;
|
|
|
|
q1 = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swapped)
|
|
|
|
{
|
|
|
|
*numerator = q1;
|
|
|
|
*denominator = p1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*numerator = p1;
|
|
|
|
*denominator = q1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-26 05:22:39 +08:00
|
|
|
static GtkWidget *
|
2014-05-08 03:27:57 +08:00
|
|
|
zoom_button_new (const gchar *icon_name,
|
2005-09-26 05:22:39 +08:00
|
|
|
GtkIconSize icon_size)
|
|
|
|
{
|
|
|
|
GtkWidget *button;
|
2014-05-08 03:27:57 +08:00
|
|
|
GtkWidget *image;
|
2005-09-26 05:22:39 +08:00
|
|
|
|
2014-05-08 03:27:57 +08:00
|
|
|
image = gtk_image_new_from_icon_name (icon_name,
|
|
|
|
icon_size > 0 ?
|
|
|
|
icon_size : GTK_ICON_SIZE_BUTTON);
|
2005-09-26 05:22:39 +08:00
|
|
|
|
2014-05-08 03:27:57 +08:00
|
|
|
button = gtk_button_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (button), image);
|
|
|
|
gtk_widget_show (image);
|
2005-09-26 05:22:39 +08:00
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2005-11-21 20:27:13 +08:00
|
|
|
static void
|
|
|
|
zoom_in_button_callback (GimpZoomModel *model,
|
|
|
|
gdouble old,
|
|
|
|
gdouble new,
|
|
|
|
GtkWidget *button)
|
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (model);
|
2005-11-21 20:27:13 +08:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive (button, priv->value != priv->maximum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
zoom_out_button_callback (GimpZoomModel *model,
|
|
|
|
gdouble old,
|
|
|
|
gdouble new,
|
|
|
|
GtkWidget *button)
|
|
|
|
{
|
2018-05-03 18:51:36 +08:00
|
|
|
GimpZoomModelPrivate *priv = GET_PRIVATE (model);
|
2005-11-21 20:27:13 +08:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive (button, priv->value != priv->minimum);
|
|
|
|
}
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
/**
|
2005-09-26 05:22:39 +08:00
|
|
|
* gimp_zoom_button_new:
|
|
|
|
* @model: a #GimpZoomModel
|
|
|
|
* @zoom_type:
|
|
|
|
* @icon_size: use 0 for a button with text labels
|
2005-09-26 01:03:03 +08:00
|
|
|
*
|
2005-09-26 05:22:39 +08:00
|
|
|
* Return value: a newly created GtkButton
|
2005-09-26 01:03:03 +08:00
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
|
|
|
GtkWidget *
|
2005-09-26 05:22:39 +08:00
|
|
|
gimp_zoom_button_new (GimpZoomModel *model,
|
|
|
|
GimpZoomType zoom_type,
|
|
|
|
GtkIconSize icon_size)
|
2005-09-26 01:03:03 +08:00
|
|
|
{
|
2005-09-26 05:22:39 +08:00
|
|
|
GtkWidget *button = NULL;
|
2005-09-26 01:03:03 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_ZOOM_MODEL (model), NULL);
|
|
|
|
|
2005-09-26 05:22:39 +08:00
|
|
|
switch (zoom_type)
|
2005-09-26 01:03:03 +08:00
|
|
|
{
|
2005-09-26 05:22:39 +08:00
|
|
|
case GIMP_ZOOM_IN:
|
2014-05-12 04:49:22 +08:00
|
|
|
button = zoom_button_new ("zoom-in", icon_size);
|
2005-09-26 01:03:03 +08:00
|
|
|
g_signal_connect_swapped (button, "clicked",
|
|
|
|
G_CALLBACK (gimp_zoom_model_zoom_in),
|
|
|
|
model);
|
2005-11-21 20:27:13 +08:00
|
|
|
g_signal_connect_object (model, "zoomed",
|
|
|
|
G_CALLBACK (zoom_in_button_callback),
|
|
|
|
button, 0);
|
2005-09-26 05:22:39 +08:00
|
|
|
break;
|
2005-09-26 01:03:03 +08:00
|
|
|
|
2005-09-26 05:22:39 +08:00
|
|
|
case GIMP_ZOOM_OUT:
|
2014-05-12 04:49:22 +08:00
|
|
|
button = zoom_button_new ("zoom-out", icon_size);
|
2005-09-26 01:03:03 +08:00
|
|
|
g_signal_connect_swapped (button, "clicked",
|
|
|
|
G_CALLBACK (gimp_zoom_model_zoom_out),
|
|
|
|
model);
|
2005-11-21 20:27:13 +08:00
|
|
|
g_signal_connect_object (model, "zoomed",
|
|
|
|
G_CALLBACK (zoom_out_button_callback),
|
|
|
|
button, 0);
|
2005-09-26 05:22:39 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_warning ("sorry, no button for this zoom type (%d)", zoom_type);
|
|
|
|
break;
|
2005-09-26 01:03:03 +08:00
|
|
|
}
|
|
|
|
|
2005-11-21 20:27:13 +08:00
|
|
|
if (button)
|
2005-09-28 19:02:02 +08:00
|
|
|
{
|
2005-11-21 20:27:13 +08:00
|
|
|
gdouble zoom = gimp_zoom_model_get_factor (model);
|
|
|
|
|
|
|
|
/* set initial button sensitivity */
|
|
|
|
g_signal_emit (model, zoom_model_signals[ZOOMED], 0, zoom, zoom);
|
2005-09-28 19:02:02 +08:00
|
|
|
|
2005-11-21 20:27:13 +08:00
|
|
|
if (icon_size > 0)
|
2005-09-28 19:02:02 +08:00
|
|
|
{
|
2005-11-21 20:27:13 +08:00
|
|
|
const gchar *desc;
|
|
|
|
|
|
|
|
if (gimp_enum_get_value (GIMP_TYPE_ZOOM_TYPE, zoom_type,
|
|
|
|
NULL, NULL, &desc, NULL))
|
|
|
|
{
|
|
|
|
gimp_help_set_help_data (button, desc, NULL);
|
|
|
|
}
|
2005-09-28 19:02:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-26 05:22:39 +08:00
|
|
|
return button;
|
2005-09-26 01:03:03 +08:00
|
|
|
}
|
|
|
|
|
2005-09-25 18:17:41 +08:00
|
|
|
/**
|
|
|
|
* gimp_zoom_model_zoom_step:
|
2010-07-06 01:04:15 +08:00
|
|
|
* @zoom_type: the zoom type
|
2005-09-26 05:22:39 +08:00
|
|
|
* @scale: ignored unless @zoom_type == %GIMP_ZOOM_TO
|
2018-05-11 08:08:51 +08:00
|
|
|
* @delta: the delta from a smooth zoom event
|
2005-09-25 18:17:41 +08:00
|
|
|
*
|
|
|
|
* Utility function to calculate a new scale factor.
|
|
|
|
*
|
|
|
|
* Return value: the new scale factor
|
|
|
|
*
|
|
|
|
* Since GIMP 2.4
|
|
|
|
**/
|
2005-09-25 01:25:36 +08:00
|
|
|
gdouble
|
|
|
|
gimp_zoom_model_zoom_step (GimpZoomType zoom_type,
|
2018-05-11 08:08:51 +08:00
|
|
|
gdouble scale,
|
|
|
|
gdouble delta)
|
2005-09-25 01:25:36 +08:00
|
|
|
{
|
|
|
|
gint i, n_presets;
|
|
|
|
gdouble new_scale = 1.0;
|
|
|
|
|
|
|
|
/* This table is constructed to have fractions, that approximate
|
|
|
|
* sqrt(2)^k. This gives a smooth feeling regardless of the starting
|
|
|
|
* zoom level.
|
|
|
|
*
|
2005-09-26 01:03:03 +08:00
|
|
|
* Zooming in/out always jumps to a zoom step from the list below.
|
2005-09-25 01:25:36 +08:00
|
|
|
* However, we try to guarantee a certain size of the step, to
|
|
|
|
* avoid silly jumps from 101% to 100%.
|
2005-09-26 01:03:03 +08:00
|
|
|
*
|
2005-09-25 01:25:36 +08:00
|
|
|
* The factor 1.1 is chosen a bit arbitrary, but feels better
|
|
|
|
* than the geometric median of the zoom steps (2^(1/4)).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ZOOM_MIN_STEP 1.1
|
|
|
|
|
2006-04-10 16:17:45 +08:00
|
|
|
const gdouble presets[] = {
|
2005-09-25 01:25:36 +08:00
|
|
|
1.0 / 256, 1.0 / 180, 1.0 / 128, 1.0 / 90,
|
|
|
|
1.0 / 64, 1.0 / 45, 1.0 / 32, 1.0 / 23,
|
|
|
|
1.0 / 16, 1.0 / 11, 1.0 / 8, 2.0 / 11,
|
|
|
|
1.0 / 4, 1.0 / 3, 1.0 / 2, 2.0 / 3,
|
|
|
|
1.0,
|
|
|
|
3.0 / 2, 2.0, 3.0,
|
|
|
|
4.0, 11.0 / 2, 8.0, 11.0,
|
|
|
|
16.0, 23.0, 32.0, 45.0,
|
|
|
|
64.0, 90.0, 128.0, 180.0,
|
|
|
|
256.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
n_presets = G_N_ELEMENTS (presets);
|
|
|
|
|
|
|
|
switch (zoom_type)
|
|
|
|
{
|
|
|
|
case GIMP_ZOOM_IN:
|
|
|
|
scale *= ZOOM_MIN_STEP;
|
|
|
|
|
2005-09-26 01:03:03 +08:00
|
|
|
new_scale = presets[n_presets - 1];
|
2005-09-25 01:25:36 +08:00
|
|
|
for (i = n_presets - 1; i >= 0 && presets[i] > scale; i--)
|
|
|
|
new_scale = presets[i];
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_ZOOM_OUT:
|
|
|
|
scale /= ZOOM_MIN_STEP;
|
|
|
|
|
|
|
|
new_scale = presets[0];
|
|
|
|
for (i = 0; i < n_presets && presets[i] < scale; i++)
|
|
|
|
new_scale = presets[i];
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2005-09-25 20:10:01 +08:00
|
|
|
case GIMP_ZOOM_IN_MORE:
|
2018-05-11 08:08:51 +08:00
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale, 0.0);
|
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale, 0.0);
|
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale, 0.0);
|
2005-09-25 20:10:01 +08:00
|
|
|
new_scale = scale;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_ZOOM_OUT_MORE:
|
2018-05-11 08:08:51 +08:00
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale, 0.0);
|
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale, 0.0);
|
|
|
|
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale, 0.0);
|
2005-09-25 20:10:01 +08:00
|
|
|
new_scale = scale;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_ZOOM_IN_MAX:
|
|
|
|
new_scale = ZOOM_MAX;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_ZOOM_OUT_MAX:
|
|
|
|
new_scale = ZOOM_MIN;
|
|
|
|
break;
|
|
|
|
|
2005-09-25 01:25:36 +08:00
|
|
|
case GIMP_ZOOM_TO:
|
|
|
|
new_scale = scale;
|
|
|
|
break;
|
2018-05-11 08:08:51 +08:00
|
|
|
|
|
|
|
case GIMP_ZOOM_SMOOTH:
|
|
|
|
if (delta > 0.0)
|
|
|
|
new_scale = scale * (1.0 + 0.1 * delta);
|
|
|
|
else if (delta < 0.0)
|
|
|
|
new_scale = scale / (1.0 + 0.1 * -delta);
|
|
|
|
else
|
|
|
|
new_scale = scale;
|
|
|
|
break;
|
|
|
|
|
2005-09-25 01:25:36 +08:00
|
|
|
}
|
|
|
|
|
2005-09-25 20:10:01 +08:00
|
|
|
return CLAMP (new_scale, ZOOM_MIN, ZOOM_MAX);
|
2005-09-25 01:25:36 +08:00
|
|
|
|
|
|
|
#undef ZOOM_MIN_STEP
|
|
|
|
}
|