1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
5
* Copyright (C) 2005 David Odin <dindinx@gimp.org>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
27
#include "gimpwidgetstypes.h"
29
#include "libgimpbase/gimpbase.h"
30
#include "libgimpmath/gimpmath.h"
32
#include "gimphelpui.h"
33
#include "gimpwidgetsmarshal.h"
34
#include "gimpzoommodel.h"
37
#define ZOOM_MIN (1.0 / 256.0)
38
#define ZOOM_MAX (256.0)
62
} GimpZoomModelPrivate;
64
#define GIMP_ZOOM_MODEL_GET_PRIVATE(obj) \
65
((GimpZoomModelPrivate *) ((GimpZoomModel *) (obj))->priv)
68
static void gimp_zoom_model_set_property (GObject *object,
72
static void gimp_zoom_model_get_property (GObject *object,
78
static guint zoom_model_signals[LAST_SIGNAL] = { 0, };
80
G_DEFINE_TYPE (GimpZoomModel, gimp_zoom_model, G_TYPE_OBJECT)
82
#define parent_class gimp_zoom_model_parent_class
86
gimp_zoom_model_class_init (GimpZoomModelClass *klass)
88
GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
* GimpZoomModel::zoomed:
92
* @model: the object that received the signal
93
* @old_factor: the zoom factor before it changes
94
* @new_factor: the zoom factor after it has changed.
96
* Emitted when the zoom factor of the zoom model changes.
98
zoom_model_signals[ZOOMED] =
99
g_signal_new ("zoomed",
100
G_TYPE_FROM_CLASS (klass),
102
G_STRUCT_OFFSET (GimpZoomModelClass,
105
_gimp_widgets_marshal_VOID__DOUBLE_DOUBLE,
107
G_TYPE_DOUBLE, G_TYPE_DOUBLE);
109
object_class->set_property = gimp_zoom_model_set_property;
110
object_class->get_property = gimp_zoom_model_get_property;
112
g_object_class_install_property (object_class, PROP_VALUE,
113
g_param_spec_double ("value",
117
GIMP_PARAM_READWRITE));
118
g_object_class_install_property (object_class, PROP_MINIMUM,
119
g_param_spec_double ("minimum",
120
"Lower limit for the zoom factor", NULL,
123
GIMP_PARAM_READWRITE));
124
g_object_class_install_property (object_class, PROP_MAXIMUM,
125
g_param_spec_double ("maximum",
126
"Upper limit for the zoom factor", NULL,
129
GIMP_PARAM_READWRITE));
131
g_object_class_install_property (object_class, PROP_FRACTION,
132
g_param_spec_string ("fraction",
133
"The zoom factor expressed as a fraction", NULL,
135
GIMP_PARAM_READABLE));
136
g_object_class_install_property (object_class, PROP_PERCENTAGE,
137
g_param_spec_string ("percentage",
138
"The zoom factor expressed as a percentage", NULL,
140
GIMP_PARAM_READABLE));
142
g_type_class_add_private (object_class, sizeof (GimpZoomModelPrivate));
146
gimp_zoom_model_init (GimpZoomModel *model)
148
GimpZoomModelPrivate *priv;
150
model->priv = G_TYPE_INSTANCE_GET_PRIVATE (model,
151
GIMP_TYPE_ZOOM_MODEL,
152
GimpZoomModelPrivate);
154
priv = GIMP_ZOOM_MODEL_GET_PRIVATE (model);
157
priv->minimum = ZOOM_MIN;
158
priv->maximum = ZOOM_MAX;
162
gimp_zoom_model_set_property (GObject *object,
167
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (object);
168
gdouble previous_value;
170
previous_value = priv->value;
171
g_object_freeze_notify (object);
176
priv->value = g_value_get_double (value);
178
g_object_notify (object, "value");
179
g_object_notify (object, "fraction");
180
g_object_notify (object, "percentage");
184
priv->minimum = MIN (g_value_get_double (value), priv->maximum);
188
priv->maximum = MAX (g_value_get_double (value), priv->minimum);
192
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196
if (priv->value > priv->maximum || priv->value < priv->minimum)
198
priv->value = CLAMP (priv->value, priv->minimum, priv->maximum);
200
g_object_notify (object, "value");
201
g_object_notify (object, "fraction");
202
g_object_notify (object, "percentage");
205
g_object_thaw_notify (object);
207
if (priv->value != previous_value)
209
g_signal_emit (object, zoom_model_signals[ZOOMED],
210
0, previous_value, priv->value);
215
gimp_zoom_model_get_property (GObject *object,
220
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (object);
226
g_value_set_double (value, priv->value);
230
g_value_set_double (value, priv->minimum);
234
g_value_set_double (value, priv->maximum);
242
gimp_zoom_model_get_fraction (GIMP_ZOOM_MODEL (object),
243
&numerator, &denominator);
245
tmp = g_strdup_printf ("%d:%d", numerator, denominator);
246
g_value_set_string (value, tmp);
251
case PROP_PERCENTAGE:
252
tmp = g_strdup_printf (priv->value >= 0.15 ? "%.0f%%" : "%.2f%%",
253
priv->value * 100.0);
254
g_value_set_string (value, tmp);
259
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
265
gimp_zoom_model_zoom_in (GimpZoomModel *model)
267
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (model);
269
if (priv->value < priv->maximum)
270
gimp_zoom_model_zoom (model, GIMP_ZOOM_IN, 0.0);
274
gimp_zoom_model_zoom_out (GimpZoomModel *model)
276
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (model);
278
if (priv->value > priv->minimum)
279
gimp_zoom_model_zoom (model, GIMP_ZOOM_OUT, 0.0);
283
* gimp_zoom_model_new:
285
* Creates a new #GimpZoomModel.
287
* Return value: a new #GimpZoomModel.
292
gimp_zoom_model_new (void)
294
return g_object_new (GIMP_TYPE_ZOOM_MODEL, NULL);
299
* gimp_zoom_model_set_range:
300
* @model: a #GimpZoomModel
301
* @min: new lower limit for zoom factor
302
* @max: new upper limit for zoom factor
304
* Sets the allowed range of the @model.
309
gimp_zoom_model_set_range (GimpZoomModel *model,
313
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
314
g_return_if_fail (min < max);
315
g_return_if_fail (min >= ZOOM_MIN);
316
g_return_if_fail (max <= ZOOM_MAX);
325
* gimp_zoom_model_zoom:
326
* @model: a #GimpZoomModel
327
* @zoom_type: the #GimpZoomType
328
* @scale: ignored unless @zoom_type == %GIMP_ZOOM_TO
333
gimp_zoom_model_zoom (GimpZoomModel *model,
334
GimpZoomType zoom_type,
337
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
339
if (zoom_type != GIMP_ZOOM_TO)
340
scale = gimp_zoom_model_get_factor (model);
343
"value", gimp_zoom_model_zoom_step (zoom_type, scale),
348
* gimp_zoom_model_get_factor:
349
* @model: a #GimpZoomModel
351
* Retrieves the current zoom factor of @model.
353
* Return value: the current scale factor
358
gimp_zoom_model_get_factor (GimpZoomModel *model)
360
g_return_val_if_fail (GIMP_IS_ZOOM_MODEL (model), 1.0);
362
return GIMP_ZOOM_MODEL_GET_PRIVATE (model)->value;
367
* gimp_zoom_model_get_fraction
368
* @model: a #GimpZoomModel
369
* @numerator: return location for numerator
370
* @denominator: return location for denominator
372
* Retrieves the current zoom factor of @model as a fraction.
377
gimp_zoom_model_get_fraction (GimpZoomModel *model,
384
gdouble remainder, next_cf;
385
gboolean swapped = FALSE;
387
g_return_if_fail (GIMP_IS_ZOOM_MODEL (model));
388
g_return_if_fail (numerator != NULL && denominator != NULL);
390
zoom_factor = gimp_zoom_model_get_factor (model);
392
/* make sure that zooming behaves symmetrically */
393
if (zoom_factor < 1.0)
395
zoom_factor = 1.0 / zoom_factor;
399
/* calculate the continued fraction for the desired zoom factor */
403
p1 = floor (zoom_factor);
406
remainder = zoom_factor - p1;
408
while (fabs (remainder) >= 0.0001 &&
409
fabs (((gdouble) p1 / q1) - zoom_factor) > 0.0001)
411
remainder = 1.0 / remainder;
413
next_cf = floor (remainder);
415
p2 = next_cf * p1 + p0;
416
q2 = next_cf * q1 + q0;
418
/* Numerator and Denominator are limited by 256 */
419
/* also absurd ratios like 170:171 are excluded */
420
if (p2 > 256 || q2 > 256 || (p2 > 1 && q2 > 1 && p2 * q2 > 200))
423
/* remember the last two fractions */
429
remainder = remainder - next_cf;
432
zoom_factor = (gdouble) p1 / q1;
434
/* hard upper and lower bounds for zoom ratio */
436
if (zoom_factor > 256.0)
441
else if (zoom_factor < 1.0 / 256.0)
460
zoom_button_new (const gchar *stock_id,
461
GtkIconSize icon_size)
467
GtkWidget *image = gtk_image_new_from_stock (stock_id, icon_size);
469
button = gtk_button_new ();
470
gtk_container_add (GTK_CONTAINER (button), image);
471
gtk_widget_show (image);
475
button = gtk_button_new_from_stock (stock_id);
482
zoom_in_button_callback (GimpZoomModel *model,
487
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (model);
489
gtk_widget_set_sensitive (button, priv->value != priv->maximum);
493
zoom_out_button_callback (GimpZoomModel *model,
498
GimpZoomModelPrivate *priv = GIMP_ZOOM_MODEL_GET_PRIVATE (model);
500
gtk_widget_set_sensitive (button, priv->value != priv->minimum);
504
* gimp_zoom_button_new:
505
* @model: a #GimpZoomModel
507
* @icon_size: use 0 for a button with text labels
509
* Return value: a newly created GtkButton
514
gimp_zoom_button_new (GimpZoomModel *model,
515
GimpZoomType zoom_type,
516
GtkIconSize icon_size)
518
GtkWidget *button = NULL;
520
g_return_val_if_fail (GIMP_IS_ZOOM_MODEL (model), NULL);
525
button = zoom_button_new (GTK_STOCK_ZOOM_IN, icon_size);
526
g_signal_connect_swapped (button, "clicked",
527
G_CALLBACK (gimp_zoom_model_zoom_in),
529
g_signal_connect_object (model, "zoomed",
530
G_CALLBACK (zoom_in_button_callback),
535
button = zoom_button_new (GTK_STOCK_ZOOM_OUT, icon_size);
536
g_signal_connect_swapped (button, "clicked",
537
G_CALLBACK (gimp_zoom_model_zoom_out),
539
g_signal_connect_object (model, "zoomed",
540
G_CALLBACK (zoom_out_button_callback),
545
g_warning ("sorry, no button for this zoom type (%d)", zoom_type);
551
gdouble zoom = gimp_zoom_model_get_factor (model);
553
/* set initial button sensitivity */
554
g_signal_emit (model, zoom_model_signals[ZOOMED], 0, zoom, zoom);
560
if (gimp_enum_get_value (GIMP_TYPE_ZOOM_TYPE, zoom_type,
561
NULL, NULL, &desc, NULL))
563
gimp_help_set_help_data (button, desc, NULL);
572
* gimp_zoom_model_zoom_step:
574
* @scale: ignored unless @zoom_type == %GIMP_ZOOM_TO
576
* Utility function to calculate a new scale factor.
578
* Return value: the new scale factor
583
gimp_zoom_model_zoom_step (GimpZoomType zoom_type,
587
gdouble new_scale = 1.0;
589
/* This table is constructed to have fractions, that approximate
590
* sqrt(2)^k. This gives a smooth feeling regardless of the starting
593
* Zooming in/out always jumps to a zoom step from the list below.
594
* However, we try to guarantee a certain size of the step, to
595
* avoid silly jumps from 101% to 100%.
597
* The factor 1.1 is chosen a bit arbitrary, but feels better
598
* than the geometric median of the zoom steps (2^(1/4)).
601
#define ZOOM_MIN_STEP 1.1
603
const gdouble presets[] = {
604
1.0 / 256, 1.0 / 180, 1.0 / 128, 1.0 / 90,
605
1.0 / 64, 1.0 / 45, 1.0 / 32, 1.0 / 23,
606
1.0 / 16, 1.0 / 11, 1.0 / 8, 2.0 / 11,
607
1.0 / 4, 1.0 / 3, 1.0 / 2, 2.0 / 3,
610
4.0, 11.0 / 2, 8.0, 11.0,
611
16.0, 23.0, 32.0, 45.0,
612
64.0, 90.0, 128.0, 180.0,
616
n_presets = G_N_ELEMENTS (presets);
621
scale *= ZOOM_MIN_STEP;
623
new_scale = presets[n_presets - 1];
624
for (i = n_presets - 1; i >= 0 && presets[i] > scale; i--)
625
new_scale = presets[i];
630
scale /= ZOOM_MIN_STEP;
632
new_scale = presets[0];
633
for (i = 0; i < n_presets && presets[i] < scale; i++)
634
new_scale = presets[i];
638
case GIMP_ZOOM_IN_MORE:
639
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale);
640
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale);
641
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_IN, scale);
645
case GIMP_ZOOM_OUT_MORE:
646
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale);
647
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale);
648
scale = gimp_zoom_model_zoom_step (GIMP_ZOOM_OUT, scale);
652
case GIMP_ZOOM_IN_MAX:
653
new_scale = ZOOM_MAX;
656
case GIMP_ZOOM_OUT_MAX:
657
new_scale = ZOOM_MIN;
665
return CLAMP (new_scale, ZOOM_MIN, ZOOM_MAX);