2013-01-17 20:08:20 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2012, One Laptop Per Child.
|
|
|
|
* Copyright (C) 2014, Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* Author(s): Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
*/
|
2014-04-09 16:28:49 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-01 06:44:51 +00:00
|
|
|
* GtkGestureZoom:
|
2014-04-09 16:28:49 +00:00
|
|
|
*
|
2021-03-01 06:44:51 +00:00
|
|
|
* `GtkGestureZoom` is a `GtkGesture` for 2-finger pinch/zoom gestures.
|
|
|
|
*
|
|
|
|
* Whenever the distance between both tracked sequences changes, the
|
|
|
|
* [signal@Gtk.GestureZoom::scale-changed] signal is emitted to report
|
|
|
|
* the scale factor.
|
2014-04-09 16:28:49 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-17 20:08:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include <math.h>
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkgesturezoom.h"
|
|
|
|
#include "gtkgesturezoomprivate.h"
|
2015-09-12 13:13:00 +00:00
|
|
|
#include "gtkintl.h"
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
typedef struct _GtkGestureZoomPrivate GtkGestureZoomPrivate;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SCALE_CHANGED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkGestureZoomPrivate
|
|
|
|
{
|
2020-07-24 20:32:16 +00:00
|
|
|
double initial_distance;
|
2013-01-17 20:08:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureZoom, gtk_gesture_zoom, GTK_TYPE_GESTURE)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_zoom_init (GtkGestureZoom *gesture)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GObject *
|
|
|
|
gtk_gesture_zoom_constructor (GType type,
|
|
|
|
guint n_construct_properties,
|
|
|
|
GObjectConstructParam *construct_properties)
|
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
|
|
|
|
object = G_OBJECT_CLASS (gtk_gesture_zoom_parent_class)->constructor (type,
|
|
|
|
n_construct_properties,
|
|
|
|
construct_properties);
|
|
|
|
g_object_set (object, "n-points", 2, NULL);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gtk_gesture_zoom_get_distance (GtkGestureZoom *zoom,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *distance)
|
2013-01-17 20:08:20 +00:00
|
|
|
{
|
2020-02-16 16:09:02 +00:00
|
|
|
GdkEvent *last_event;
|
2020-07-24 20:32:16 +00:00
|
|
|
double x1, y1, x2, y2;
|
2013-01-17 20:08:20 +00:00
|
|
|
GtkGesture *gesture;
|
2017-10-18 14:44:33 +00:00
|
|
|
GList *sequences = NULL;
|
2020-07-24 20:32:16 +00:00
|
|
|
double dx, dy;
|
2017-08-26 14:53:33 +00:00
|
|
|
GdkTouchpadGesturePhase phase;
|
2017-10-18 14:44:33 +00:00
|
|
|
gboolean retval = FALSE;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
gesture = GTK_GESTURE (zoom);
|
|
|
|
|
|
|
|
if (!gtk_gesture_is_recognized (gesture))
|
2017-10-18 14:44:33 +00:00
|
|
|
goto out;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
sequences = gtk_gesture_get_sequences (gesture);
|
2015-07-09 17:31:27 +00:00
|
|
|
if (!sequences)
|
2017-10-18 14:44:33 +00:00
|
|
|
goto out;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
2015-07-09 17:31:27 +00:00
|
|
|
last_event = gtk_gesture_get_last_event (gesture, sequences->data);
|
|
|
|
|
2020-06-10 12:27:14 +00:00
|
|
|
if (gdk_event_get_event_type (last_event) == GDK_TOUCHPAD_PINCH)
|
2015-07-09 17:31:27 +00:00
|
|
|
{
|
2017-08-26 14:53:33 +00:00
|
|
|
double scale;
|
2020-06-10 12:27:14 +00:00
|
|
|
|
2015-07-09 17:31:27 +00:00
|
|
|
/* Touchpad pinch */
|
2020-06-10 12:27:14 +00:00
|
|
|
phase = gdk_touchpad_event_get_gesture_phase (last_event);
|
|
|
|
if (phase == GDK_TOUCHPAD_GESTURE_PHASE_CANCEL)
|
|
|
|
goto out;
|
2017-08-26 14:53:33 +00:00
|
|
|
|
Restructure the GdkEvent type hierarchy
GdkEvent has been a "I-can't-believe-this-is-not-OOP" type for ages,
using a union of sub-types. This has always been problematic when it
comes to implementing accessor functions: either you get generic API
that takes a GdkEvent and uses a massive switch() to determine which
event types have the data you're looking for; or you create namespaced
accessors, but break language bindings horribly, as boxed types cannot
have derived types.
The recent conversion of GskRenderNode (which had similar issues) to
GTypeInstance, and the fact that GdkEvent is now a completely opaque
type, provide us with the chance of moving GdkEvent to GTypeInstance,
and have sub-types for GdkEvent.
The change from boxed type to GTypeInstance is pretty small, all things
considered, but ends up cascading to a larger commit, as we still have
backends and code in GTK trying to access GdkEvent structures directly.
Additionally, the naming of the public getter functions requires
renaming all the data structures to conform to the namespace/type-name
pattern.
2020-04-16 16:23:36 +00:00
|
|
|
scale = gdk_touchpad_event_get_pinch_scale (last_event);
|
2017-08-26 14:53:33 +00:00
|
|
|
*distance = scale;
|
2015-07-09 17:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!sequences->next)
|
2017-10-18 14:44:33 +00:00
|
|
|
goto out;
|
2015-07-09 17:31:27 +00:00
|
|
|
|
|
|
|
gtk_gesture_get_point (gesture, sequences->data, &x1, &y1);
|
|
|
|
gtk_gesture_get_point (gesture, sequences->next->data, &x2, &y2);
|
|
|
|
|
|
|
|
dx = x1 - x2;
|
|
|
|
dy = y1 - y2;;
|
|
|
|
*distance = sqrt ((dx * dx) + (dy * dy));
|
|
|
|
}
|
2013-01-17 20:08:20 +00:00
|
|
|
|
2017-10-18 14:44:33 +00:00
|
|
|
retval = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_list_free (sequences);
|
|
|
|
return retval;
|
2013-01-17 20:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gtk_gesture_zoom_check_emit (GtkGestureZoom *gesture)
|
|
|
|
{
|
|
|
|
GtkGestureZoomPrivate *priv;
|
2020-07-24 20:32:16 +00:00
|
|
|
double distance, zoom;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
if (!_gtk_gesture_zoom_get_distance (gesture, &distance))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv = gtk_gesture_zoom_get_instance_private (gesture);
|
|
|
|
|
|
|
|
if (distance == 0 || priv->initial_distance == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
zoom = distance / priv->initial_distance;
|
|
|
|
g_signal_emit (gesture, signals[SCALE_CHANGED], 0, zoom);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-07-09 17:31:27 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_gesture_zoom_filter_event (GtkEventController *controller,
|
2020-02-16 16:09:02 +00:00
|
|
|
GdkEvent *event)
|
2015-07-09 17:31:27 +00:00
|
|
|
{
|
2021-06-28 15:50:56 +00:00
|
|
|
/* Let 2-finger touchpad pinch and hold events go through */
|
|
|
|
if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_PINCH ||
|
|
|
|
gdk_event_get_event_type (event) == GDK_TOUCHPAD_HOLD)
|
2015-07-09 17:31:27 +00:00
|
|
|
{
|
2017-08-26 14:53:33 +00:00
|
|
|
guint n_fingers;
|
|
|
|
|
2020-02-18 03:11:56 +00:00
|
|
|
n_fingers = gdk_touchpad_event_get_n_fingers (event);
|
2017-08-26 14:53:33 +00:00
|
|
|
|
|
|
|
if (n_fingers == 2)
|
2015-07-09 17:31:27 +00:00
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_zoom_parent_class)->filter_event (controller, event);
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:08:20 +00:00
|
|
|
static void
|
|
|
|
gtk_gesture_zoom_begin (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureZoom *zoom = GTK_GESTURE_ZOOM (gesture);
|
|
|
|
GtkGestureZoomPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_zoom_get_instance_private (zoom);
|
|
|
|
_gtk_gesture_zoom_get_distance (zoom, &priv->initial_distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_zoom_update (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
_gtk_gesture_zoom_check_emit (GTK_GESTURE_ZOOM (gesture));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_zoom_class_init (GtkGestureZoomClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2015-07-09 17:31:27 +00:00
|
|
|
GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
|
2013-01-17 20:08:20 +00:00
|
|
|
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructor = gtk_gesture_zoom_constructor;
|
|
|
|
|
2015-07-09 17:31:27 +00:00
|
|
|
event_controller_class->filter_event = gtk_gesture_zoom_filter_event;
|
|
|
|
|
2013-01-17 20:08:20 +00:00
|
|
|
gesture_class->begin = gtk_gesture_zoom_begin;
|
|
|
|
gesture_class->update = gtk_gesture_zoom_update;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkGestureZoom::scale-changed:
|
|
|
|
* @controller: the object on which the signal is emitted
|
|
|
|
* @scale: Scale delta, taking the initial state as 1:1
|
2014-04-09 16:28:49 +00:00
|
|
|
*
|
2021-03-01 06:44:51 +00:00
|
|
|
* Emitted whenever the distance between both tracked sequences changes.
|
2013-01-17 20:08:20 +00:00
|
|
|
*/
|
|
|
|
signals[SCALE_CHANGED] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("scale-changed"),
|
2013-01-17 20:08:20 +00:00
|
|
|
GTK_TYPE_GESTURE_ZOOM,
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureZoomClass, scale_changed),
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 1, G_TYPE_DOUBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_zoom_new:
|
|
|
|
*
|
2021-03-01 06:44:51 +00:00
|
|
|
* Returns a newly created `GtkGesture` that recognizes
|
|
|
|
* pinch/zoom gestures.
|
2013-01-17 20:08:20 +00:00
|
|
|
*
|
2021-03-01 06:44:51 +00:00
|
|
|
* Returns: a newly created `GtkGestureZoom`
|
|
|
|
*/
|
2013-01-17 20:08:20 +00:00
|
|
|
GtkGesture *
|
2018-03-08 22:40:45 +00:00
|
|
|
gtk_gesture_zoom_new (void)
|
2013-01-17 20:08:20 +00:00
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_GESTURE_ZOOM,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_zoom_get_scale_delta:
|
2021-03-01 06:44:51 +00:00
|
|
|
* @gesture: a `GtkGestureZoom`
|
2013-01-17 20:08:20 +00:00
|
|
|
*
|
2021-03-01 06:44:51 +00:00
|
|
|
* Gets the scale delta.
|
|
|
|
*
|
|
|
|
* If @gesture is active, this function returns the zooming
|
|
|
|
* difference since the gesture was recognized (hence the
|
|
|
|
* starting point is considered 1:1). If @gesture is not
|
|
|
|
* active, 1 is returned.
|
2013-01-17 20:08:20 +00:00
|
|
|
*
|
2014-05-27 16:45:04 +00:00
|
|
|
* Returns: the scale delta
|
2021-03-01 06:44:51 +00:00
|
|
|
*/
|
2020-07-24 20:32:16 +00:00
|
|
|
double
|
2014-05-26 12:32:59 +00:00
|
|
|
gtk_gesture_zoom_get_scale_delta (GtkGestureZoom *gesture)
|
2013-01-17 20:08:20 +00:00
|
|
|
{
|
|
|
|
GtkGestureZoomPrivate *priv;
|
2020-07-24 20:32:16 +00:00
|
|
|
double distance;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
2014-05-27 16:45:04 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE_ZOOM (gesture), 1.0);
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
if (!_gtk_gesture_zoom_get_distance (gesture, &distance))
|
2014-05-27 16:45:04 +00:00
|
|
|
return 1.0;
|
2013-01-17 20:08:20 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_zoom_get_instance_private (gesture);
|
|
|
|
|
2014-05-26 12:32:59 +00:00
|
|
|
return distance / priv->initial_distance;
|
2013-01-17 20:08:20 +00:00
|
|
|
}
|