gtk2/gtk/gtkmagnifier.c

332 lines
9.9 KiB
C
Raw Normal View History

/* 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"
#include "gtkwidgetprivate.h"
#include "gtksnapshot.h"
#include "gtkintl.h"
enum {
PROP_INSPECTED = 1,
PROP_RESIZE,
PROP_MAGNIFICATION
};
2020-09-19 18:36:44 +00:00
struct _GtkMagnifier
{
2020-09-19 18:36:44 +00:00
GtkWidget parent_instance;
GdkPaintable *paintable;
2020-07-24 20:32:16 +00:00
double magnification;
2020-07-24 13:54:49 +00:00
int x;
int y;
gboolean resize;
};
2020-09-19 18:36:44 +00:00
typedef struct
{
GtkWidgetClass parent_class;
} GtkMagnifierClass;
G_DEFINE_TYPE (GtkMagnifier, gtk_magnifier, GTK_TYPE_WIDGET)
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);
switch (param_id)
{
case PROP_INSPECTED:
2020-09-19 18:36:44 +00:00
_gtk_magnifier_set_inspected (magnifier, g_value_get_object (value));
break;
2020-09-19 18:36:44 +00:00
case PROP_MAGNIFICATION:
2020-09-19 18:36:44 +00:00
_gtk_magnifier_set_magnification (magnifier, g_value_get_double (value));
break;
2020-09-19 18:36:44 +00:00
case PROP_RESIZE:
2020-09-19 18:36:44 +00:00
_gtk_magnifier_set_resize (magnifier, g_value_get_boolean (value));
break;
2020-09-19 18:36:44 +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);
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)));
break;
2020-09-19 18:36:44 +00:00
case PROP_MAGNIFICATION:
2020-09-19 18:36:44 +00:00
g_value_set_double (value, magnifier->magnification);
break;
2020-09-19 18:36:44 +00:00
case PROP_RESIZE:
2020-09-19 18:36:44 +00:00
g_value_set_boolean (value, magnifier->resize);
break;
2020-09-19 18:36:44 +00:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
}
static void
gtk_magnifier_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
double width, height, paintable_width, paintable_height;
2020-09-19 18:36:44 +00:00
if (gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable)) == NULL)
return;
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);
if (paintable_width <= 0.0 || paintable_height <= 0.0)
return;
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)));
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);
}
static void
gtk_magnifier_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
2020-07-24 13:54:49 +00:00
int size;
2020-09-19 18:36:44 +00:00
if (magnifier->resize)
{
if (orientation == GTK_ORIENTATION_HORIZONTAL)
2020-09-19 18:36:44 +00:00
size = magnifier->magnification * gdk_paintable_get_intrinsic_width (magnifier->paintable);
else
2020-09-19 18:36:44 +00:00
size = magnifier->magnification * gdk_paintable_get_intrinsic_height (magnifier->paintable);
}
else
size = 0;
*minimum = size;
*natural = size;
}
static void
gtk_magnifier_dispose (GObject *object)
{
2020-09-19 18:36:44 +00:00
GtkMagnifier *magnifier = GTK_MAGNIFIER (object);
2020-09-19 18:36:44 +00:00
if (magnifier->paintable)
_gtk_magnifier_set_inspected (magnifier, NULL);
2020-09-19 18:36:44 +00:00
g_clear_object (&magnifier->paintable);
G_OBJECT_CLASS (gtk_magnifier_parent_class)->dispose (object);
}
static void
gtk_magnifier_class_init (GtkMagnifierClass *klass)
{
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;
object_class->dispose = gtk_magnifier_dispose;
widget_class->snapshot = gtk_magnifier_snapshot;
widget_class->measure = gtk_magnifier_measure;
g_object_class_install_property (object_class,
PROP_INSPECTED,
g_param_spec_object ("inspected",
P_("Inspected"),
P_("Inspected widget"),
GTK_TYPE_WIDGET,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_MAGNIFICATION,
g_param_spec_double ("magnification",
P_("magnification"),
P_("magnification"),
1, G_MAXDOUBLE, 1,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_RESIZE,
g_param_spec_boolean ("resize",
P_("resize"),
P_("resize"),
FALSE,
G_PARAM_READWRITE));
gtk_widget_class_set_css_name (widget_class, "magnifier");
}
static void
2020-09-19 18:36:44 +00:00
gtk_magnifier_init (GtkMagnifier *magnifier)
{
2020-09-19 18:36:44 +00:00
GtkWidget *widget = GTK_WIDGET (magnifier);
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);
}
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));
}
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));
g_return_if_fail (inspected == NULL || GTK_IS_WIDGET (inspected));
2020-09-19 18:36:44 +00:00
gtk_widget_paintable_set_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable), inspected);
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)
{
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
2020-09-19 18:36:44 +00:00
if (magnifier->x == x && magnifier->y == y)
return;
2020-09-19 18:36:44 +00:00
magnifier->x = x;
magnifier->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)
{
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
if (x)
2020-09-19 18:36:44 +00:00
*x = magnifier->x;
if (y)
2020-09-19 18:36:44 +00:00
*y = magnifier->y;
}
void
_gtk_magnifier_set_magnification (GtkMagnifier *magnifier,
2020-07-24 20:32:16 +00:00
double magnification)
{
g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
2020-09-19 18:36:44 +00:00
if (magnifier->magnification == magnification)
return;
2020-09-19 18:36:44 +00:00
magnifier->magnification = magnification;
g_object_notify (G_OBJECT (magnifier), "magnification");
2020-09-19 18:36:44 +00:00
if (magnifier->resize)
gtk_widget_queue_resize (GTK_WIDGET (magnifier));
if (gtk_widget_is_visible (GTK_WIDGET (magnifier)))
gtk_widget_queue_draw (GTK_WIDGET (magnifier));
}
2020-07-24 20:32:16 +00:00
double
_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;
}
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)
return;
2020-09-19 18:36:44 +00:00
magnifier->resize = resize;
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;
}