2014-02-20 16:04:49 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* 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:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkgesturedrag
|
|
|
|
* @Short_description: Drag gesture
|
|
|
|
* @Title: GtkGestureDrag
|
|
|
|
* @See_also: #GtkGestureSwipe
|
|
|
|
*
|
|
|
|
* #GtkGestureDrag is a #GtkGesture implementation that recognizes drag
|
2020-08-21 12:41:13 +00:00
|
|
|
* operations. The drag operation itself can be tracked throughout the
|
2016-02-08 21:49:01 +00:00
|
|
|
* #GtkGestureDrag::drag-begin, #GtkGestureDrag::drag-update and
|
|
|
|
* #GtkGestureDrag::drag-end signals, or the relevant coordinates be
|
2014-04-09 16:28:20 +00:00
|
|
|
* extracted through gtk_gesture_drag_get_offset() and
|
|
|
|
* gtk_gesture_drag_get_start_point().
|
|
|
|
*/
|
2014-02-20 16:04:49 +00:00
|
|
|
#include "config.h"
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkgesturedrag.h"
|
|
|
|
#include "gtkgesturedragprivate.h"
|
2015-09-12 13:13:00 +00:00
|
|
|
#include "gtkintl.h"
|
2019-06-02 21:07:27 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
typedef struct _GtkGestureDragPrivate GtkGestureDragPrivate;
|
|
|
|
typedef struct _EventData EventData;
|
|
|
|
|
|
|
|
struct _GtkGestureDragPrivate
|
|
|
|
{
|
2020-07-24 20:32:16 +00:00
|
|
|
double start_x;
|
|
|
|
double start_y;
|
|
|
|
double last_x;
|
|
|
|
double last_y;
|
2014-02-20 16:04:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
DRAG_BEGIN,
|
|
|
|
DRAG_UPDATE,
|
|
|
|
DRAG_END,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[N_SIGNALS] = { 0 };
|
|
|
|
|
2014-03-21 18:39:21 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureDrag, gtk_gesture_drag, GTK_TYPE_GESTURE_SINGLE)
|
2014-02-20 16:04:49 +00:00
|
|
|
|
2015-07-24 11:05:31 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_gesture_drag_filter_event (GtkEventController *controller,
|
2020-02-16 16:09:02 +00:00
|
|
|
GdkEvent *event)
|
2015-07-24 11:05:31 +00:00
|
|
|
{
|
|
|
|
/* Let touchpad swipe events go through, only if they match n-points */
|
2017-08-25 14:47:11 +00:00
|
|
|
if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_SWIPE)
|
2015-07-24 11:05:31 +00:00
|
|
|
{
|
|
|
|
guint n_points;
|
2017-08-26 14:52:06 +00:00
|
|
|
guint n_fingers;
|
2015-07-24 11:05:31 +00:00
|
|
|
|
|
|
|
g_object_get (G_OBJECT (controller), "n-points", &n_points, NULL);
|
2020-02-18 03:11:56 +00:00
|
|
|
n_fingers = gdk_touchpad_event_get_n_fingers (event);
|
2015-07-24 11:05:31 +00:00
|
|
|
|
2017-08-26 14:52:06 +00:00
|
|
|
if (n_fingers == n_points)
|
2015-07-24 11:05:31 +00:00
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_drag_parent_class)->filter_event (controller, event);
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:04:49 +00:00
|
|
|
static void
|
|
|
|
gtk_gesture_drag_begin (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureDragPrivate *priv;
|
2014-03-25 12:22:51 +00:00
|
|
|
GdkEventSequence *current;
|
|
|
|
|
|
|
|
current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
|
2014-03-25 12:22:51 +00:00
|
|
|
gtk_gesture_get_point (gesture, current, &priv->start_x, &priv->start_y);
|
|
|
|
priv->last_x = priv->start_x;
|
|
|
|
priv->last_y = priv->start_y;
|
|
|
|
|
2014-02-20 16:04:49 +00:00
|
|
|
g_signal_emit (gesture, signals[DRAG_BEGIN], 0, priv->start_x, priv->start_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_drag_update (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureDragPrivate *priv;
|
2020-07-24 20:32:16 +00:00
|
|
|
double x, y;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
|
|
|
|
gtk_gesture_get_point (gesture, sequence, &priv->last_x, &priv->last_y);
|
|
|
|
x = priv->last_x - priv->start_x;
|
|
|
|
y = priv->last_y - priv->start_y;
|
|
|
|
|
|
|
|
g_signal_emit (gesture, signals[DRAG_UPDATE], 0, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_drag_end (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureDragPrivate *priv;
|
2014-03-25 12:22:51 +00:00
|
|
|
GdkEventSequence *current;
|
2020-07-24 20:32:16 +00:00
|
|
|
double x, y;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
2014-03-25 12:22:51 +00:00
|
|
|
current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
|
|
|
|
2014-02-20 16:04:49 +00:00
|
|
|
priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
|
2014-03-25 12:22:51 +00:00
|
|
|
gtk_gesture_get_point (gesture, current, &priv->last_x, &priv->last_y);
|
2014-02-20 16:04:49 +00:00
|
|
|
x = priv->last_x - priv->start_x;
|
|
|
|
y = priv->last_y - priv->start_y;
|
|
|
|
|
|
|
|
g_signal_emit (gesture, signals[DRAG_END], 0, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_drag_class_init (GtkGestureDragClass *klass)
|
|
|
|
{
|
|
|
|
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
|
2015-07-24 11:05:31 +00:00
|
|
|
GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
|
|
|
|
|
|
|
|
event_controller_class->filter_event = gtk_gesture_drag_filter_event;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
gesture_class->begin = gtk_gesture_drag_begin;
|
|
|
|
gesture_class->update = gtk_gesture_drag_update;
|
|
|
|
gesture_class->end = gtk_gesture_drag_end;
|
|
|
|
|
2014-04-09 16:28:20 +00:00
|
|
|
/**
|
2014-04-10 11:43:27 +00:00
|
|
|
* GtkGestureDrag::drag-begin:
|
2014-04-09 16:28:20 +00:00
|
|
|
* @gesture: the object which received the signal
|
|
|
|
* @start_x: X coordinate, relative to the widget allocation
|
|
|
|
* @start_y: Y coordinate, relative to the widget allocation
|
|
|
|
*
|
|
|
|
* This signal is emitted whenever dragging starts.
|
|
|
|
*/
|
2014-02-20 16:04:49 +00:00
|
|
|
signals[DRAG_BEGIN] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("drag-begin"),
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureDragClass, drag_begin),
|
2019-06-02 21:07:27 +00:00
|
|
|
NULL, NULL,
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLE,
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
2019-06-02 21:07:27 +00:00
|
|
|
g_signal_set_va_marshaller (signals[DRAG_BEGIN],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLEv);
|
2014-04-09 16:28:20 +00:00
|
|
|
/**
|
2014-04-10 11:43:27 +00:00
|
|
|
* GtkGestureDrag::drag-update:
|
2014-04-09 16:28:20 +00:00
|
|
|
* @gesture: the object which received the signal
|
|
|
|
* @offset_x: X offset, relative to the start point
|
|
|
|
* @offset_y: Y offset, relative to the start point
|
|
|
|
*
|
|
|
|
* This signal is emitted whenever the dragging point moves.
|
|
|
|
*/
|
2014-02-20 16:04:49 +00:00
|
|
|
signals[DRAG_UPDATE] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("drag-update"),
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureDragClass, drag_update),
|
2019-06-02 21:07:27 +00:00
|
|
|
NULL, NULL,
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLE,
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
2019-06-02 21:07:27 +00:00
|
|
|
g_signal_set_va_marshaller (signals[DRAG_UPDATE],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLEv);
|
2014-04-09 16:28:20 +00:00
|
|
|
/**
|
2014-04-10 11:43:27 +00:00
|
|
|
* GtkGestureDrag::drag-end:
|
2014-04-09 16:28:20 +00:00
|
|
|
* @gesture: the object which received the signal
|
|
|
|
* @offset_x: X offset, relative to the start point
|
|
|
|
* @offset_y: Y offset, relative to the start point
|
|
|
|
*
|
|
|
|
* This signal is emitted whenever the dragging is finished.
|
|
|
|
*/
|
2014-02-20 16:04:49 +00:00
|
|
|
signals[DRAG_END] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("drag-end"),
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureDragClass, drag_end),
|
2019-06-02 21:07:27 +00:00
|
|
|
NULL, NULL,
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLE,
|
2014-02-20 16:04:49 +00:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
2019-06-02 21:07:27 +00:00
|
|
|
g_signal_set_va_marshaller (signals[DRAG_END],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLEv);
|
2014-02-20 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_drag_init (GtkGestureDrag *gesture)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_drag_new:
|
|
|
|
*
|
2014-05-20 02:16:03 +00:00
|
|
|
* Returns a newly created #GtkGesture that recognizes drags.
|
2014-02-20 16:04:49 +00:00
|
|
|
*
|
|
|
|
* Returns: a newly created #GtkGestureDrag
|
|
|
|
**/
|
|
|
|
GtkGesture *
|
2018-03-10 17:05:57 +00:00
|
|
|
gtk_gesture_drag_new (void)
|
2014-02-20 16:04:49 +00:00
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_GESTURE_DRAG,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_drag_get_start_point:
|
|
|
|
* @gesture: a #GtkGesture
|
2014-08-05 14:22:03 +00:00
|
|
|
* @x: (out) (nullable): X coordinate for the drag start point
|
|
|
|
* @y: (out) (nullable): Y coordinate for the drag start point
|
2014-02-20 16:04:49 +00:00
|
|
|
*
|
|
|
|
* If the @gesture is active, this function returns %TRUE
|
|
|
|
* and fills in @x and @y with the drag start coordinates,
|
|
|
|
* in window-relative coordinates.
|
|
|
|
*
|
2014-05-20 02:16:03 +00:00
|
|
|
* Returns: %TRUE if the gesture is active
|
2014-02-20 16:04:49 +00:00
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
gtk_gesture_drag_get_start_point (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *x,
|
|
|
|
double *y)
|
2014-02-20 16:04:49 +00:00
|
|
|
{
|
|
|
|
GtkGestureDragPrivate *priv;
|
2014-05-12 17:11:53 +00:00
|
|
|
GdkEventSequence *sequence;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
2014-05-12 17:11:53 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE_DRAG (gesture), FALSE);
|
|
|
|
|
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
|
|
|
|
|
|
|
if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
|
2014-02-20 16:04:49 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv = gtk_gesture_drag_get_instance_private (gesture);
|
|
|
|
|
|
|
|
if (x)
|
|
|
|
*x = priv->start_x;
|
|
|
|
|
|
|
|
if (y)
|
|
|
|
*y = priv->start_y;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-25 12:22:51 +00:00
|
|
|
* gtk_gesture_drag_get_offset:
|
2014-02-20 16:04:49 +00:00
|
|
|
* @gesture: a #GtkGesture
|
2014-08-05 14:22:03 +00:00
|
|
|
* @x: (out) (nullable): X offset for the current point
|
|
|
|
* @y: (out) (nullable): Y offset for the current point
|
2014-02-20 16:04:49 +00:00
|
|
|
*
|
|
|
|
* If the @gesture is active, this function returns %TRUE and
|
|
|
|
* fills in @x and @y with the coordinates of the current point,
|
|
|
|
* as an offset to the starting drag point.
|
|
|
|
*
|
2014-05-20 02:16:03 +00:00
|
|
|
* Returns: %TRUE if the gesture is active
|
2014-02-20 16:04:49 +00:00
|
|
|
**/
|
|
|
|
gboolean
|
2014-03-25 12:22:51 +00:00
|
|
|
gtk_gesture_drag_get_offset (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *x,
|
|
|
|
double *y)
|
2014-02-20 16:04:49 +00:00
|
|
|
{
|
|
|
|
GtkGestureDragPrivate *priv;
|
2014-05-12 17:11:53 +00:00
|
|
|
GdkEventSequence *sequence;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE_DRAG (gesture), FALSE);
|
|
|
|
|
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2014-02-20 16:04:49 +00:00
|
|
|
|
2014-05-12 17:11:53 +00:00
|
|
|
if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
|
2014-02-20 16:04:49 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv = gtk_gesture_drag_get_instance_private (gesture);
|
|
|
|
|
|
|
|
if (x)
|
2014-03-25 12:22:51 +00:00
|
|
|
*x = priv->last_x - priv->start_x;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
if (y)
|
2014-03-25 12:22:51 +00:00
|
|
|
*y = priv->last_y - priv->start_y;
|
2014-02-20 16:04:49 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|