2013-12-11 14:42:10 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright © 2013 Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gtk/gtk.h"
|
|
|
|
#include "gtkmagnifierprivate.h"
|
2017-08-23 13:00:59 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2018-03-11 05:29:46 +00:00
|
|
|
#include "gtksnapshot.h"
|
2013-12-11 14:42:10 +00:00
|
|
|
#include "gtkintl.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_INSPECTED = 1,
|
2014-12-21 00:16:46 +00:00
|
|
|
PROP_RESIZE,
|
2013-12-11 14:42:10 +00:00
|
|
|
PROP_MAGNIFICATION
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _GtkMagnifierPrivate GtkMagnifierPrivate;
|
|
|
|
|
|
|
|
struct _GtkMagnifierPrivate
|
|
|
|
{
|
2018-03-23 04:01:11 +00:00
|
|
|
GdkPaintable *paintable;
|
2020-07-24 20:32:16 +00:00
|
|
|
double magnification;
|
2020-07-24 13:54:49 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2014-12-21 00:16:46 +00:00
|
|
|
gboolean resize;
|
2013-12-11 14:42:10 +00:00
|
|
|
};
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkMagnifier, gtk_magnifier,
|
2013-12-11 14:42:10 +00:00
|
|
|
GTK_TYPE_WIDGET)
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_magnifier_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_INSPECTED:
|
|
|
|
_gtk_magnifier_set_inspected (GTK_MAGNIFIER (object),
|
|
|
|
g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
case PROP_MAGNIFICATION:
|
|
|
|
_gtk_magnifier_set_magnification (GTK_MAGNIFIER (object),
|
|
|
|
g_value_get_double (value));
|
|
|
|
break;
|
2014-12-21 00:16:46 +00:00
|
|
|
case PROP_RESIZE:
|
|
|
|
_gtk_magnifier_set_resize (GTK_MAGNIFIER (object),
|
|
|
|
g_value_get_boolean (value));
|
|
|
|
break;
|
2013-12-11 14:42:10 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_magnifier_get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkMagnifier *magnifier;
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
magnifier = GTK_MAGNIFIER (object);
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_INSPECTED:
|
2018-03-23 04:01:11 +00:00
|
|
|
g_value_set_object (value, gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (priv->paintable)));
|
2013-12-11 14:42:10 +00:00
|
|
|
break;
|
|
|
|
case PROP_MAGNIFICATION:
|
|
|
|
g_value_set_double (value, priv->magnification);
|
|
|
|
break;
|
2014-12-21 00:16:46 +00:00
|
|
|
case PROP_RESIZE:
|
|
|
|
g_value_set_boolean (value, priv->resize);
|
|
|
|
break;
|
2013-12-11 14:42:10 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:00:59 +00:00
|
|
|
static void
|
|
|
|
gtk_magnifier_snapshot (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
2017-08-23 13:00:59 +00:00
|
|
|
GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (magnifier);
|
|
|
|
double width, height, paintable_width, paintable_height;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
if (gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (priv->paintable)) == NULL)
|
2017-08-23 13:00:59 +00:00
|
|
|
return;
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
width = gtk_widget_get_width (widget);
|
|
|
|
height = gtk_widget_get_height (widget);
|
|
|
|
paintable_width = gdk_paintable_get_intrinsic_width (priv->paintable);
|
|
|
|
paintable_height = gdk_paintable_get_intrinsic_height (priv->paintable);
|
|
|
|
if (paintable_width <= 0.0 || paintable_height <= 0.0)
|
2017-08-23 13:00:59 +00:00
|
|
|
return;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_save (snapshot);
|
2018-03-23 04:01:11 +00:00
|
|
|
if (!priv->resize)
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (width / 2, height / 2));
|
|
|
|
gtk_snapshot_scale (snapshot, priv->magnification, priv->magnification);
|
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (
|
|
|
|
- CLAMP (priv->x, 0, paintable_width),
|
|
|
|
- CLAMP (priv->y, 0, paintable_height)));
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
gdk_paintable_snapshot (priv->paintable, snapshot, paintable_width, paintable_height);
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_restore (snapshot);
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-21 00:16:46 +00:00
|
|
|
static void
|
2016-10-22 14:06:14 +00:00
|
|
|
gtk_magnifier_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
2014-12-21 00:16:46 +00:00
|
|
|
{
|
2016-10-22 14:06:14 +00:00
|
|
|
GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (magnifier);
|
2020-07-24 13:54:49 +00:00
|
|
|
int size;
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
if (priv->resize)
|
2016-10-22 14:06:14 +00:00
|
|
|
{
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
2018-03-23 04:01:11 +00:00
|
|
|
size = priv->magnification * gdk_paintable_get_intrinsic_width (priv->paintable);
|
2016-10-22 14:06:14 +00:00
|
|
|
else
|
2018-03-23 04:01:11 +00:00
|
|
|
size = priv->magnification * gdk_paintable_get_intrinsic_height (priv->paintable);
|
2016-10-22 14:06:14 +00:00
|
|
|
}
|
2014-12-21 00:16:46 +00:00
|
|
|
else
|
2016-10-22 14:06:14 +00:00
|
|
|
size = 0;
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2016-10-22 14:06:14 +00:00
|
|
|
*minimum = size;
|
|
|
|
*natural = size;
|
2014-12-21 00:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-03-23 04:01:11 +00:00
|
|
|
gtk_magnifier_dispose (GObject *object)
|
2014-12-21 00:16:46 +00:00
|
|
|
{
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifier *self = GTK_MAGNIFIER (object);
|
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (self);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2020-03-22 14:41:40 +00:00
|
|
|
if (priv->paintable)
|
|
|
|
_gtk_magnifier_set_inspected (self, NULL);
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
g_clear_object (&priv->paintable);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
G_OBJECT_CLASS (gtk_magnifier_parent_class)->dispose (object);
|
2014-12-21 00:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-03-23 04:01:11 +00:00
|
|
|
gtk_magnifier_class_init (GtkMagnifierClass *klass)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->set_property = _gtk_magnifier_set_property;
|
|
|
|
object_class->get_property = _gtk_magnifier_get_property;
|
2018-03-23 04:01:11 +00:00
|
|
|
object_class->dispose = gtk_magnifier_dispose;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2017-08-23 13:00:59 +00:00
|
|
|
widget_class->snapshot = gtk_magnifier_snapshot;
|
2016-10-22 14:06:14 +00:00
|
|
|
widget_class->measure = gtk_magnifier_measure;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_INSPECTED,
|
2014-02-07 07:17:11 +00:00
|
|
|
g_param_spec_object ("inspected",
|
2013-12-11 14:42:10 +00:00
|
|
|
P_("Inspected"),
|
|
|
|
P_("Inspected widget"),
|
|
|
|
GTK_TYPE_WIDGET,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_MAGNIFICATION,
|
2014-02-07 07:17:11 +00:00
|
|
|
g_param_spec_double ("magnification",
|
2013-12-11 14:42:10 +00:00
|
|
|
P_("magnification"),
|
|
|
|
P_("magnification"),
|
|
|
|
1, G_MAXDOUBLE, 1,
|
|
|
|
G_PARAM_READWRITE));
|
2014-12-21 00:16:46 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_RESIZE,
|
|
|
|
g_param_spec_boolean ("resize",
|
|
|
|
P_("resize"),
|
|
|
|
P_("resize"),
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE));
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-03-23 04:01:11 +00:00
|
|
|
gtk_magnifier_init (GtkMagnifier *self)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (self);
|
|
|
|
GtkWidget *widget = GTK_WIDGET (self);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2019-02-08 17:16:07 +00:00
|
|
|
gtk_widget_set_overflow (widget, GTK_OVERFLOW_HIDDEN);
|
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
priv->magnification = 1;
|
2014-12-21 00:16:46 +00:00
|
|
|
priv->resize = FALSE;
|
2018-03-23 04:01:11 +00:00
|
|
|
priv->paintable = gtk_widget_paintable_new (NULL);
|
|
|
|
g_signal_connect_swapped (priv->paintable, "invalidate-contents", G_CALLBACK (gtk_widget_queue_draw), self);
|
|
|
|
g_signal_connect_swapped (priv->paintable, "invalidate-size", G_CALLBACK (gtk_widget_queue_resize), self);
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
_gtk_magnifier_new (GtkWidget *inspected)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_WIDGET (inspected), NULL);
|
|
|
|
|
|
|
|
return g_object_new (GTK_TYPE_MAGNIFIER,
|
|
|
|
"inspected", inspected,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
_gtk_magnifier_get_inspected (GtkMagnifier *magnifier)
|
|
|
|
{
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), NULL);
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
return gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (priv->paintable));
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_inspected (GtkMagnifier *magnifier,
|
|
|
|
GtkWidget *inspected)
|
|
|
|
{
|
2018-03-23 04:01:11 +00:00
|
|
|
GtkMagnifierPrivate *priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2014-01-22 19:48:51 +00:00
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
2018-03-23 04:01:11 +00:00
|
|
|
g_return_if_fail (inspected == NULL || GTK_IS_WIDGET (inspected));
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
gtk_widget_paintable_set_widget (GTK_WIDGET_PAINTABLE (priv->paintable), inspected);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
g_object_notify (G_OBJECT (magnifier), "inspected");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_coords (GtkMagnifier *magnifier,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
if (priv->x == x && priv->y == y)
|
|
|
|
return;
|
|
|
|
|
|
|
|
priv->x = x;
|
|
|
|
priv->y = y;
|
|
|
|
|
|
|
|
if (gtk_widget_is_visible (GTK_WIDGET (magnifier)))
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (magnifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_get_coords (GtkMagnifier *magnifier,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *x,
|
|
|
|
double *y)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
if (x)
|
|
|
|
*x = priv->x;
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
*y = priv->y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_magnification (GtkMagnifier *magnifier,
|
2020-07-24 20:32:16 +00:00
|
|
|
double magnification)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
if (priv->magnification == magnification)
|
|
|
|
return;
|
|
|
|
|
|
|
|
priv->magnification = magnification;
|
|
|
|
g_object_notify (G_OBJECT (magnifier), "magnification");
|
|
|
|
|
2014-12-21 00:16:46 +00:00
|
|
|
if (priv->resize)
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (magnifier));
|
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
if (gtk_widget_is_visible (GTK_WIDGET (magnifier)))
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (magnifier));
|
|
|
|
}
|
|
|
|
|
2020-07-24 20:32:16 +00:00
|
|
|
double
|
2013-12-11 14:42:10 +00:00
|
|
|
_gtk_magnifier_get_magnification (GtkMagnifier *magnifier)
|
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), 1);
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
return priv->magnification;
|
|
|
|
}
|
2014-12-21 00:16:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_resize (GtkMagnifier *magnifier,
|
|
|
|
gboolean resize)
|
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
|
|
|
if (priv->resize == resize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
priv->resize = resize;
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (magnifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gtk_magnifier_get_resize (GtkMagnifier *magnifier)
|
|
|
|
{
|
|
|
|
GtkMagnifierPrivate *priv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), FALSE);
|
|
|
|
|
2018-03-23 04:01:11 +00:00
|
|
|
priv = gtk_magnifier_get_instance_private (magnifier);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
|
|
|
return priv->resize;
|
|
|
|
}
|