2013-01-17 20:06:06 +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:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkgesturerotate
|
|
|
|
* @Short_description: Rotate gesture
|
|
|
|
* @Title: GtkGestureRotate
|
|
|
|
* @See_also: #GtkGestureZoom
|
|
|
|
*
|
|
|
|
* #GtkGestureRotate is a #GtkGesture implementation able to recognize
|
|
|
|
* 2-finger rotations, whenever the angle between both handled sequences
|
2014-04-10 11:44:49 +00:00
|
|
|
* changes, the #GtkGestureRotate::angle-changed signal is emitted.
|
2014-04-09 16:28:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-17 20:06:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include <math.h>
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkgesturerotate.h"
|
|
|
|
#include "gtkgesturerotateprivate.h"
|
2013-01-17 20:06:06 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2015-09-12 13:13:00 +00:00
|
|
|
#include "gtkintl.h"
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
typedef struct _GtkGestureRotatePrivate GtkGestureRotatePrivate;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ANGLE_CHANGED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkGestureRotatePrivate
|
|
|
|
{
|
|
|
|
gdouble initial_angle;
|
2015-07-09 17:34:09 +00:00
|
|
|
gdouble accum_touchpad_angle;
|
2013-01-17 20:06:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureRotate, gtk_gesture_rotate, GTK_TYPE_GESTURE)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_rotate_init (GtkGestureRotate *gesture)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GObject *
|
|
|
|
gtk_gesture_rotate_constructor (GType type,
|
|
|
|
guint n_construct_properties,
|
|
|
|
GObjectConstructParam *construct_properties)
|
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
|
|
|
|
object = G_OBJECT_CLASS (gtk_gesture_rotate_parent_class)->constructor (type,
|
|
|
|
n_construct_properties,
|
|
|
|
construct_properties);
|
|
|
|
g_object_set (object, "n-points", 2, NULL);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gtk_gesture_rotate_get_angle (GtkGestureRotate *rotate,
|
|
|
|
gdouble *angle)
|
|
|
|
{
|
2015-07-09 17:34:09 +00:00
|
|
|
GtkGestureRotatePrivate *priv;
|
|
|
|
const GdkEvent *last_event;
|
2013-01-17 20:06:06 +00:00
|
|
|
gdouble x1, y1, x2, y2;
|
|
|
|
GtkGesture *gesture;
|
|
|
|
gdouble dx, dy;
|
|
|
|
GList *sequences;
|
|
|
|
|
|
|
|
gesture = GTK_GESTURE (rotate);
|
2015-07-09 17:34:09 +00:00
|
|
|
priv = gtk_gesture_rotate_get_instance_private (rotate);
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
if (!gtk_gesture_is_recognized (gesture))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sequences = gtk_gesture_get_sequences (gesture);
|
2015-07-09 17:34:09 +00:00
|
|
|
if (!sequences)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
last_event = gtk_gesture_get_last_event (gesture, sequences->data);
|
|
|
|
|
2017-08-25 14:47:11 +00:00
|
|
|
if (gdk_event_get_event_type (last_event) == GDK_TOUCHPAD_PINCH &&
|
2015-07-09 17:34:09 +00:00
|
|
|
(last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN ||
|
|
|
|
last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE ||
|
|
|
|
last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END))
|
|
|
|
{
|
|
|
|
*angle = priv->accum_touchpad_angle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!sequences->next)
|
|
|
|
return FALSE;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
gtk_gesture_get_point (gesture, sequences->data, &x1, &y1);
|
|
|
|
gtk_gesture_get_point (gesture, sequences->next->data, &x2, &y2);
|
|
|
|
g_list_free (sequences);
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
dx = x1 - x2;
|
|
|
|
dy = y1 - y2;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
*angle = atan2 (dx, dy);
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
/* Invert angle */
|
|
|
|
*angle = (2 * G_PI) - *angle;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
/* And constraint it to 0°-360° */
|
|
|
|
*angle = fmod (*angle, 2 * G_PI);
|
|
|
|
}
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gtk_gesture_rotate_check_emit (GtkGestureRotate *gesture)
|
|
|
|
{
|
|
|
|
GtkGestureRotatePrivate *priv;
|
2014-05-06 09:43:31 +00:00
|
|
|
gdouble angle, delta;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv = gtk_gesture_rotate_get_instance_private (gesture);
|
2014-05-06 09:43:31 +00:00
|
|
|
delta = angle - priv->initial_angle;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
2014-05-06 09:43:31 +00:00
|
|
|
if (delta < 0)
|
|
|
|
delta += 2 * G_PI;
|
|
|
|
|
|
|
|
g_signal_emit (gesture, signals[ANGLE_CHANGED], 0, angle, delta);
|
2013-01-17 20:06:06 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_rotate_begin (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureRotate *rotate = GTK_GESTURE_ROTATE (gesture);
|
|
|
|
GtkGestureRotatePrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_rotate_get_instance_private (rotate);
|
|
|
|
_gtk_gesture_rotate_get_angle (rotate, &priv->initial_angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_rotate_update (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
_gtk_gesture_rotate_check_emit (GTK_GESTURE_ROTATE (gesture));
|
|
|
|
}
|
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_gesture_rotate_filter_event (GtkEventController *controller,
|
|
|
|
const GdkEvent *event)
|
|
|
|
{
|
|
|
|
/* Let 2-finger touchpad pinch events go through */
|
2017-08-25 14:47:11 +00:00
|
|
|
if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_PINCH)
|
2015-07-09 17:34:09 +00:00
|
|
|
{
|
|
|
|
if (event->touchpad_pinch.n_fingers == 2)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_rotate_parent_class)->filter_event (controller, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_gesture_rotate_handle_event (GtkEventController *controller,
|
|
|
|
const GdkEvent *event)
|
|
|
|
{
|
|
|
|
GtkGestureRotate *rotate = GTK_GESTURE_ROTATE (controller);
|
|
|
|
GtkGestureRotatePrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_rotate_get_instance_private (rotate);
|
|
|
|
|
2017-08-25 14:47:11 +00:00
|
|
|
if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_PINCH)
|
2015-07-09 17:34:09 +00:00
|
|
|
{
|
|
|
|
if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN ||
|
|
|
|
event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END)
|
|
|
|
priv->accum_touchpad_angle = 0;
|
|
|
|
else if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE)
|
|
|
|
priv->accum_touchpad_angle += event->touchpad_pinch.angle_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_rotate_parent_class)->handle_event (controller, event);
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:06:06 +00:00
|
|
|
static void
|
|
|
|
gtk_gesture_rotate_class_init (GtkGestureRotateClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2015-07-09 17:34:09 +00:00
|
|
|
GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
|
2013-01-17 20:06:06 +00:00
|
|
|
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructor = gtk_gesture_rotate_constructor;
|
|
|
|
|
2015-07-09 17:34:09 +00:00
|
|
|
event_controller_class->filter_event = gtk_gesture_rotate_filter_event;
|
|
|
|
event_controller_class->handle_event = gtk_gesture_rotate_handle_event;
|
|
|
|
|
2013-01-17 20:06:06 +00:00
|
|
|
gesture_class->begin = gtk_gesture_rotate_begin;
|
|
|
|
gesture_class->update = gtk_gesture_rotate_update;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkGestureRotate::angle-changed:
|
|
|
|
* @gesture: the object on which the signal is emitted
|
|
|
|
* @angle: Current angle in radians
|
2014-04-09 16:28:43 +00:00
|
|
|
* @angle_delta: Difference with the starting angle, in radians
|
|
|
|
*
|
|
|
|
* This signal is emitted when the angle between both tracked points
|
|
|
|
* changes.
|
|
|
|
*
|
|
|
|
* Since: 3.14
|
2013-01-17 20:06:06 +00:00
|
|
|
*/
|
|
|
|
signals[ANGLE_CHANGED] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("angle-changed"),
|
2013-01-17 20:06:06 +00:00
|
|
|
GTK_TYPE_GESTURE_ROTATE,
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureRotateClass, angle_changed),
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_rotate_new:
|
|
|
|
* @widget: a #GtkWidget
|
|
|
|
*
|
|
|
|
* Returns a newly created #GtkGesture that recognizes 2-touch
|
|
|
|
* rotation gestures.
|
|
|
|
*
|
|
|
|
* Returns: a newly created #GtkGestureRotate
|
|
|
|
*
|
|
|
|
* Since: 3.14
|
|
|
|
**/
|
|
|
|
GtkGesture *
|
|
|
|
gtk_gesture_rotate_new (GtkWidget *widget)
|
|
|
|
{
|
2014-05-27 18:30:47 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
|
|
|
|
|
2013-01-17 20:06:06 +00:00
|
|
|
return g_object_new (GTK_TYPE_GESTURE_ROTATE,
|
|
|
|
"widget", widget,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_rotate_get_angle_delta:
|
|
|
|
* @gesture: a #GtkGestureRotate
|
|
|
|
*
|
2014-05-26 12:29:17 +00:00
|
|
|
* If @gesture is active, this function returns the angle difference
|
2014-05-27 16:45:04 +00:00
|
|
|
* in radians since the gesture was first recognized. If @gesture is
|
|
|
|
* not active, 0 is returned.
|
2013-01-17 20:06:06 +00:00
|
|
|
*
|
2014-05-26 12:29:17 +00:00
|
|
|
* Returns: the angle delta in radians
|
2013-01-17 20:06:06 +00:00
|
|
|
*
|
|
|
|
* Since: 3.14
|
|
|
|
**/
|
2014-05-26 12:29:17 +00:00
|
|
|
gdouble
|
|
|
|
gtk_gesture_rotate_get_angle_delta (GtkGestureRotate *gesture)
|
2013-01-17 20:06:06 +00:00
|
|
|
{
|
|
|
|
GtkGestureRotatePrivate *priv;
|
|
|
|
gdouble angle;
|
|
|
|
|
2014-05-27 16:45:04 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE_ROTATE (gesture), 0.0);
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
|
2014-05-27 16:45:04 +00:00
|
|
|
return 0.0;
|
2013-01-17 20:06:06 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_rotate_get_instance_private (gesture);
|
|
|
|
|
2014-05-26 12:29:17 +00:00
|
|
|
return angle - priv->initial_angle;
|
2013-01-17 20:06:06 +00:00
|
|
|
}
|