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
|
|
|
|
};
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
struct _GtkMagnifier
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
2020-09-19 18:36:44 +00:00
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
} GtkMagnifierClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkMagnifier, gtk_magnifier, GTK_TYPE_WIDGET)
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_magnifier_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2020-09-19 18:36:44 +00:00
|
|
|
GtkMagnifier *magnifier = GTK_MAGNIFIER (object);
|
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_INSPECTED:
|
2020-09-19 18:36:44 +00:00
|
|
|
_gtk_magnifier_set_inspected (magnifier, g_value_get_object (value));
|
2013-12-11 14:42:10 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
case PROP_MAGNIFICATION:
|
2020-09-19 18:36:44 +00:00
|
|
|
_gtk_magnifier_set_magnification (magnifier, g_value_get_double (value));
|
2013-12-11 14:42:10 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
2014-12-21 00:16:46 +00:00
|
|
|
case PROP_RESIZE:
|
2020-09-19 18:36:44 +00:00
|
|
|
_gtk_magnifier_set_resize (magnifier, g_value_get_boolean (value));
|
2014-12-21 00:16:46 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2020-09-19 18:36:44 +00:00
|
|
|
GtkMagnifier *magnifier = GTK_MAGNIFIER (object);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_INSPECTED:
|
2020-09-19 18:36:44 +00:00
|
|
|
g_value_set_object (value, gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable)));
|
2013-12-11 14:42:10 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
2013-12-11 14:42:10 +00:00
|
|
|
case PROP_MAGNIFICATION:
|
2020-09-19 18:36:44 +00:00
|
|
|
g_value_set_double (value, magnifier->magnification);
|
2013-12-11 14:42:10 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
2014-12-21 00:16:46 +00:00
|
|
|
case PROP_RESIZE:
|
2020-09-19 18:36:44 +00:00
|
|
|
g_value_set_boolean (value, magnifier->resize);
|
2014-12-21 00:16:46 +00:00
|
|
|
break;
|
2020-09-19 18:36:44 +00:00
|
|
|
|
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
|
|
|
double width, height, paintable_width, paintable_height;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->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);
|
2020-09-19 18:36:44 +00:00
|
|
|
paintable_width = gdk_paintable_get_intrinsic_width (magnifier->paintable);
|
|
|
|
paintable_height = gdk_paintable_get_intrinsic_height (magnifier->paintable);
|
2018-03-23 04:01:11 +00:00
|
|
|
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);
|
2020-09-19 18:36:44 +00:00
|
|
|
if (!magnifier->resize)
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (width / 2, height / 2));
|
2020-09-19 18:36:44 +00:00
|
|
|
gtk_snapshot_scale (snapshot, magnifier->magnification, magnifier->magnification);
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (
|
2020-09-19 18:36:44 +00:00
|
|
|
- CLAMP (magnifier->x, 0, paintable_width),
|
|
|
|
- CLAMP (magnifier->y, 0, paintable_height)));
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
gdk_paintable_snapshot (magnifier->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);
|
2020-07-24 13:54:49 +00:00
|
|
|
int size;
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->resize)
|
2016-10-22 14:06:14 +00:00
|
|
|
{
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
2020-09-19 18:36:44 +00:00
|
|
|
size = magnifier->magnification * gdk_paintable_get_intrinsic_width (magnifier->paintable);
|
2016-10-22 14:06:14 +00:00
|
|
|
else
|
2020-09-19 18:36:44 +00:00
|
|
|
size = magnifier->magnification * gdk_paintable_get_intrinsic_height (magnifier->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
|
|
|
{
|
2020-09-19 18:36:44 +00:00
|
|
|
GtkMagnifier *magnifier = GTK_MAGNIFIER (object);
|
2014-12-21 00:16:46 +00:00
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->paintable)
|
|
|
|
_gtk_magnifier_set_inspected (magnifier, NULL);
|
2020-03-22 14:41:40 +00:00
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
g_clear_object (&magnifier->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
|
2020-09-19 18:36:44 +00:00
|
|
|
gtk_magnifier_init (GtkMagnifier *magnifier)
|
2013-12-11 14:42:10 +00:00
|
|
|
{
|
2020-09-19 18:36:44 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (magnifier);
|
2013-12-11 14:42:10 +00:00
|
|
|
|
2019-02-08 17:16:07 +00:00
|
|
|
gtk_widget_set_overflow (widget, GTK_OVERFLOW_HIDDEN);
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
magnifier->magnification = 1;
|
|
|
|
magnifier->resize = FALSE;
|
|
|
|
magnifier->paintable = gtk_widget_paintable_new (NULL);
|
|
|
|
g_signal_connect_swapped (magnifier->paintable, "invalidate-contents", G_CALLBACK (gtk_widget_queue_draw), magnifier);
|
|
|
|
g_signal_connect_swapped (magnifier->paintable, "invalidate-size", G_CALLBACK (gtk_widget_queue_resize), magnifier);
|
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)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), NULL);
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
return gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable));
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_inspected (GtkMagnifier *magnifier,
|
|
|
|
GtkWidget *inspected)
|
|
|
|
{
|
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
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
gtk_widget_paintable_set_widget (GTK_WIDGET_PAINTABLE (magnifier->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
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->x == x && magnifier->y == y)
|
2013-12-11 14:42:10 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
magnifier->x = x;
|
|
|
|
magnifier->y = y;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
|
|
|
if (x)
|
2020-09-19 18:36:44 +00:00
|
|
|
*x = magnifier->x;
|
2013-12-11 14:42:10 +00:00
|
|
|
|
|
|
|
if (y)
|
2020-09-19 18:36:44 +00:00
|
|
|
*y = magnifier->y;
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->magnification == magnification)
|
2013-12-11 14:42:10 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
magnifier->magnification = magnification;
|
2013-12-11 14:42:10 +00:00
|
|
|
g_object_notify (G_OBJECT (magnifier), "magnification");
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->resize)
|
2014-12-21 00:16:46 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), 1);
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
return magnifier->magnification;
|
2013-12-11 14:42:10 +00:00
|
|
|
}
|
2014-12-21 00:16:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_magnifier_set_resize (GtkMagnifier *magnifier,
|
|
|
|
gboolean resize)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
if (magnifier->resize == resize)
|
2014-12-21 00:16:46 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
magnifier->resize = resize;
|
2014-12-21 00:16:46 +00:00
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (magnifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gtk_magnifier_get_resize (GtkMagnifier *magnifier)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), FALSE);
|
|
|
|
|
2020-09-19 18:36:44 +00:00
|
|
|
return magnifier->resize;
|
2014-12-21 00:16:46 +00:00
|
|
|
}
|