2014-04-08 19:21:56 +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:29:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkgesturepan
|
|
|
|
* @Short_description: Pan gesture
|
|
|
|
* @Title: GtkGesturePan
|
|
|
|
*
|
|
|
|
* #GtkGesturePan is a #GtkGesture implementation able to recognize
|
|
|
|
* pan gestures, those are drags that are locked to happen along one
|
|
|
|
* axis. The axis that a #GtkGesturePan handles is defined at
|
|
|
|
* construct time, and can be changed through
|
|
|
|
* gtk_gesture_pan_set_orientation().
|
|
|
|
*
|
|
|
|
* When the gesture starts to be recognized, #GtkGesturePan will
|
|
|
|
* attempt to determine as early as possible whether the sequence
|
|
|
|
* is moving in the expected direction, and denying the sequence if
|
|
|
|
* this does not happen.
|
|
|
|
*
|
|
|
|
* Once a panning gesture along the expected axis is recognized,
|
2014-04-10 11:44:06 +00:00
|
|
|
* the #GtkGesturePan::pan signal will be emitted as input events
|
2014-04-09 16:29:02 +00:00
|
|
|
* are received, containing the offset in the given axis.
|
|
|
|
*/
|
|
|
|
|
2014-04-08 19:21:56 +00:00
|
|
|
#include "config.h"
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkgesturepan.h"
|
|
|
|
#include "gtkgesturepanprivate.h"
|
2014-04-08 19:21:56 +00:00
|
|
|
#include "gtktypebuiltins.h"
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
#include "gtkintl.h"
|
2019-06-02 21:07:27 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2014-04-08 19:21:56 +00:00
|
|
|
|
|
|
|
typedef struct _GtkGesturePanPrivate GtkGesturePanPrivate;
|
|
|
|
|
|
|
|
struct _GtkGesturePanPrivate
|
|
|
|
{
|
|
|
|
guint orientation : 2;
|
|
|
|
guint panning : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_ORIENTATION = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PAN,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[N_SIGNALS] = { 0 };
|
|
|
|
|
2014-05-06 09:39:15 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkGesturePan, gtk_gesture_pan, GTK_TYPE_GESTURE_DRAG)
|
2014-04-08 19:21:56 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (GTK_GESTURE_PAN (object));
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ORIENTATION:
|
|
|
|
g_value_set_enum (value, priv->orientation);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ORIENTATION:
|
|
|
|
gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (object),
|
|
|
|
g_value_get_enum (value));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-07-24 20:32:16 +00:00
|
|
|
direction_from_offset (double offset_x,
|
|
|
|
double offset_y,
|
2014-05-26 09:58:18 +00:00
|
|
|
GtkOrientation orientation,
|
|
|
|
GtkPanDirection *direction)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
2014-05-26 09:58:18 +00:00
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
if (offset_x > 0)
|
|
|
|
*direction = GTK_PAN_DIRECTION_RIGHT;
|
|
|
|
else
|
|
|
|
*direction = GTK_PAN_DIRECTION_LEFT;
|
|
|
|
}
|
2014-05-26 09:58:18 +00:00
|
|
|
else if (orientation == GTK_ORIENTATION_VERTICAL)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
if (offset_y > 0)
|
|
|
|
*direction = GTK_PAN_DIRECTION_DOWN;
|
|
|
|
else
|
|
|
|
*direction = GTK_PAN_DIRECTION_UP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
guess_direction (GtkGesturePan *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
2014-04-08 19:21:56 +00:00
|
|
|
GtkPanDirection *direction)
|
|
|
|
{
|
2020-07-24 20:32:16 +00:00
|
|
|
double abs_x, abs_y;
|
2014-04-08 19:21:56 +00:00
|
|
|
|
|
|
|
abs_x = ABS (offset_x);
|
|
|
|
abs_y = ABS (offset_y);
|
|
|
|
|
2014-05-06 09:39:15 +00:00
|
|
|
#define FACTOR 2
|
2014-04-08 19:21:56 +00:00
|
|
|
if (abs_x > abs_y * FACTOR)
|
|
|
|
direction_from_offset (offset_x, offset_y,
|
2014-05-26 09:58:18 +00:00
|
|
|
GTK_ORIENTATION_HORIZONTAL, direction);
|
2014-04-08 19:21:56 +00:00
|
|
|
else if (abs_y > abs_x * FACTOR)
|
|
|
|
direction_from_offset (offset_x, offset_y,
|
2014-05-26 09:58:18 +00:00
|
|
|
GTK_ORIENTATION_VERTICAL, direction);
|
2014-04-08 19:21:56 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
#undef FACTOR
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
check_orientation_matches (GtkGesturePan *gesture,
|
|
|
|
GtkPanDirection direction)
|
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv = gtk_gesture_pan_get_instance_private (gesture);
|
|
|
|
|
|
|
|
return (((direction == GTK_PAN_DIRECTION_LEFT ||
|
|
|
|
direction == GTK_PAN_DIRECTION_RIGHT) &&
|
2014-05-26 09:58:18 +00:00
|
|
|
priv->orientation == GTK_ORIENTATION_HORIZONTAL) ||
|
2014-04-08 19:21:56 +00:00
|
|
|
((direction == GTK_PAN_DIRECTION_UP ||
|
|
|
|
direction == GTK_PAN_DIRECTION_DOWN) &&
|
2014-05-26 09:58:18 +00:00
|
|
|
priv->orientation == GTK_ORIENTATION_VERTICAL));
|
2014-04-08 19:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_drag_update (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset_x,
|
|
|
|
double offset_y)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
GtkPanDirection direction;
|
|
|
|
GtkGesturePan *pan;
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset;
|
2014-04-08 19:21:56 +00:00
|
|
|
|
|
|
|
pan = GTK_GESTURE_PAN (gesture);
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (pan);
|
|
|
|
|
|
|
|
if (!priv->panning)
|
|
|
|
{
|
|
|
|
if (!guess_direction (pan, offset_x, offset_y, &direction))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!check_orientation_matches (pan, direction))
|
|
|
|
{
|
|
|
|
gtk_gesture_set_state (GTK_GESTURE (gesture),
|
|
|
|
GTK_EVENT_SEQUENCE_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->panning = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
direction_from_offset (offset_x, offset_y, priv->orientation, &direction);
|
|
|
|
|
2014-05-26 09:58:18 +00:00
|
|
|
offset = (priv->orientation == GTK_ORIENTATION_VERTICAL) ?
|
2014-04-08 19:21:56 +00:00
|
|
|
ABS (offset_y) : ABS (offset_x);
|
|
|
|
g_signal_emit (gesture, signals[PAN], 0, direction, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_drag_end (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset_x,
|
|
|
|
double offset_y)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (GTK_GESTURE_PAN (gesture));
|
|
|
|
priv->panning = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_class_init (GtkGesturePanClass *klass)
|
|
|
|
{
|
|
|
|
GtkGestureDragClass *drag_gesture_class = GTK_GESTURE_DRAG_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->get_property = gtk_gesture_pan_get_property;
|
|
|
|
object_class->set_property = gtk_gesture_pan_set_property;
|
|
|
|
|
|
|
|
drag_gesture_class->drag_update = gtk_gesture_pan_drag_update;
|
|
|
|
drag_gesture_class->drag_end = gtk_gesture_pan_drag_end;
|
|
|
|
|
2014-04-09 16:29:02 +00:00
|
|
|
/**
|
|
|
|
* GtkGesturePan:orientation:
|
|
|
|
*
|
|
|
|
* The expected orientation of pan gestures.
|
|
|
|
*/
|
2014-04-08 19:21:56 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_ORIENTATION,
|
|
|
|
g_param_spec_enum ("orientation",
|
|
|
|
P_("Orientation"),
|
|
|
|
P_("Allowed orientations"),
|
2014-05-26 09:58:18 +00:00
|
|
|
GTK_TYPE_ORIENTATION,
|
|
|
|
GTK_ORIENTATION_HORIZONTAL,
|
2014-06-09 12:56:55 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
|
|
|
|
|
2014-04-09 16:29:02 +00:00
|
|
|
/**
|
2014-04-10 11:44:06 +00:00
|
|
|
* GtkGesturePan::pan:
|
2014-04-09 16:29:02 +00:00
|
|
|
* @gesture: The object which received the signal
|
|
|
|
* @direction: current direction of the pan gesture
|
|
|
|
* @offset: Offset along the gesture orientation
|
|
|
|
*
|
|
|
|
* This signal is emitted once a panning gesture along the
|
|
|
|
* expected axis is detected.
|
|
|
|
*/
|
2014-04-08 19:21:56 +00:00
|
|
|
signals[PAN] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("pan"),
|
2014-04-08 19:21:56 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkGesturePanClass, pan),
|
2019-06-02 21:07:27 +00:00
|
|
|
NULL, NULL,
|
|
|
|
_gtk_marshal_VOID__ENUM_DOUBLE,
|
2014-04-09 16:29:02 +00:00
|
|
|
G_TYPE_NONE, 2, GTK_TYPE_PAN_DIRECTION,
|
|
|
|
G_TYPE_DOUBLE);
|
2019-06-02 21:07:27 +00:00
|
|
|
g_signal_set_va_marshaller (signals[PAN],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_gtk_marshal_VOID__ENUM_DOUBLEv);
|
2014-04-08 19:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_pan_init (GtkGesturePan *gesture)
|
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (gesture);
|
2014-05-26 09:58:18 +00:00
|
|
|
priv->orientation = GTK_ORIENTATION_HORIZONTAL;
|
2014-04-08 19:21:56 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 16:29:02 +00:00
|
|
|
/**
|
|
|
|
* gtk_gesture_pan_new:
|
|
|
|
* @orientation: expected orientation
|
|
|
|
*
|
|
|
|
* Returns a newly created #GtkGesture that recognizes pan gestures.
|
|
|
|
*
|
|
|
|
* Returns: a newly created #GtkGesturePan
|
|
|
|
**/
|
2014-04-08 19:21:56 +00:00
|
|
|
GtkGesture *
|
2018-05-05 09:51:24 +00:00
|
|
|
gtk_gesture_pan_new (GtkOrientation orientation)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_GESTURE_PAN,
|
|
|
|
"orientation", orientation,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:29:02 +00:00
|
|
|
/**
|
|
|
|
* gtk_gesture_pan_get_orientation:
|
|
|
|
* @gesture: A #GtkGesturePan
|
|
|
|
*
|
|
|
|
* Returns the orientation of the pan gestures that this @gesture expects.
|
|
|
|
*
|
2014-07-10 15:52:44 +00:00
|
|
|
* Returns: the expected orientation for pan gestures
|
2014-04-09 16:29:02 +00:00
|
|
|
*/
|
2014-05-26 09:58:18 +00:00
|
|
|
GtkOrientation
|
2014-04-08 19:21:56 +00:00
|
|
|
gtk_gesture_pan_get_orientation (GtkGesturePan *gesture)
|
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE_PAN (gesture), 0);
|
|
|
|
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (gesture);
|
|
|
|
|
|
|
|
return priv->orientation;
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:29:02 +00:00
|
|
|
/**
|
|
|
|
* gtk_gesture_pan_set_orientation:
|
|
|
|
* @gesture: A #GtkGesturePan
|
|
|
|
* @orientation: expected orientation
|
|
|
|
*
|
|
|
|
* Sets the orientation to be expected on pan gestures.
|
|
|
|
*/
|
2014-04-08 19:21:56 +00:00
|
|
|
void
|
2014-05-26 09:58:18 +00:00
|
|
|
gtk_gesture_pan_set_orientation (GtkGesturePan *gesture,
|
|
|
|
GtkOrientation orientation)
|
2014-04-08 19:21:56 +00:00
|
|
|
{
|
|
|
|
GtkGesturePanPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_GESTURE_PAN (gesture));
|
2014-05-26 09:58:18 +00:00
|
|
|
g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL ||
|
|
|
|
orientation == GTK_ORIENTATION_VERTICAL);
|
2014-04-08 19:21:56 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_pan_get_instance_private (gesture);
|
|
|
|
|
|
|
|
if (priv->orientation == orientation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
priv->orientation = orientation;
|
|
|
|
g_object_notify (G_OBJECT (gesture), "orientation");
|
|
|
|
}
|