[gtk+/wip/events: 10/11] API: gtk: Add a swipe gesture
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/events: 10/11] API: gtk: Add a swipe gesture
- Date: Fri, 9 Mar 2012 03:45:18 +0000 (UTC)
commit 5b358081d098f48c247499303e13ba2d12801811
Author: Benjamin Otte <otte redhat com>
Date: Tue Mar 6 23:38:13 2012 +0100
API: gtk: Add a swipe gesture
First event recognizer.
gtk/Makefile.am | 4 +
gtk/gtk.h | 2 +
gtk/gtkswipegesture.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++
gtk/gtkswipegesture.h | 84 ++++++++++++++++
gtk/gtkswiperecognizer.c | 95 +++++++++++++++++++
gtk/gtkswiperecognizer.h | 72 ++++++++++++++
6 files changed, 492 insertions(+), 0 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index b3a33fc..ae9cbb5 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -328,6 +328,8 @@ gtk_public_h_sources = \
gtkstylecontext.h \
gtkstyleproperties.h \
gtkstyleprovider.h \
+ gtkswipegesture.h \
+ gtkswiperecognizer.h \
gtkswitch.h \
gtksymboliccolor.h \
gtktestutils.h \
@@ -764,6 +766,8 @@ gtk_base_c_sources = \
gtkstyleproperty.c \
gtkstyleprovider.c \
gtkstyleproviderprivate.c \
+ gtkswipegesture.c \
+ gtkswiperecognizer.c \
gtkswitch.c \
gtksymboliccolor.c \
gtktestutils.c \
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 04be83b..62489e6 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -183,6 +183,8 @@
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkstyleproperties.h>
#include <gtk/gtkstyleprovider.h>
+#include <gtk/gtkswipegesture.h>
+#include <gtk/gtkswiperecognizer.h>
#include <gtk/gtkswitch.h>
#include <gtk/gtksymboliccolor.h>
#include <gtk/gtktextattributes.h>
diff --git a/gtk/gtkswipegesture.c b/gtk/gtkswipegesture.c
new file mode 100644
index 0000000..d3905e3
--- /dev/null
+++ b/gtk/gtkswipegesture.c
@@ -0,0 +1,235 @@
+/*
+ * Copyright  2012 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.1 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkswipegesture.h"
+
+#include "gtksequencetrackerprivate.h"
+#include "gtkswiperecognizer.h"
+
+#include <math.h>
+
+/**
+ * SECTION:gtkswipegesture
+ * @Short_description: Tracks the swipes from a #GtkSwipeRecognizer
+ * @Title: GtkSwipeGesture
+ * @See_also: #GtkSwipeRecognizer
+ *
+ * The #GtkSwipeGesture object - or a subclass of it - is used to track
+ * sequences of swipes as recognized by a #GtkSwipeRecognizer. Once the
+ * recognizer finds it can potentially identify a sequence of swipes, it
+ * creates a #GtkSwipeGesture and uses it to store information about the
+ * swipe. See the swipe handling howto for a highlevel overview.
+ *
+ * #GtkSwipeGesture was added in GTK 3.6.
+ */
+
+
+enum {
+ PROP_0,
+};
+
+struct _GtkSwipeGesturePrivate {
+ GtkSequenceTracker *sequence[2];
+ GtkMovementDirection direction;
+};
+
+G_DEFINE_TYPE (GtkSwipeGesture, gtk_swipe_gesture, GTK_TYPE_EVENT_TRACKER)
+
+static void
+gtk_swipe_gesture_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ //GtkSwipeGesture *gesture = GTK_SWIPE_GESTURE (object);
+ //GtkSwipeGesturePrivate *priv = gesture->priv;
+
+ switch (prop_id)
+ {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_swipe_gesture_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ //GtkSwipeGesture *gesture = GTK_SWIPE_GESTURE (object);
+ //GtkSwipeGesturePrivate *priv = gesture->priv;
+
+ switch (prop_id)
+ {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_swipe_gesture_dispose (GObject *object)
+{
+ //GtkSwipeGesture *gesture = GTK_SWIPE_GESTURE (object);
+
+ G_OBJECT_CLASS (gtk_swipe_gesture_parent_class)->dispose (object);
+}
+
+static void
+gtk_swipe_gesture_class_init (GtkSwipeGestureClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = gtk_swipe_gesture_dispose;
+ object_class->set_property = gtk_swipe_gesture_set_property;
+ object_class->get_property = gtk_swipe_gesture_get_property;
+
+ g_type_class_add_private (object_class, sizeof (GtkSwipeGesturePrivate));
+}
+
+static void
+gtk_swipe_gesture_init (GtkSwipeGesture *gesture)
+{
+ GtkSwipeGesturePrivate *priv;
+
+ priv = gesture->priv = G_TYPE_INSTANCE_GET_PRIVATE (gesture,
+ GTK_TYPE_SWIPE_GESTURE,
+ GtkSwipeGesturePrivate);
+
+ priv->direction = GTK_DIR_ANY;
+}
+
+gboolean
+_gtk_swipe_gesture_begin (GtkSwipeGesture *gesture,
+ GdkEvent *event)
+{
+ GtkSwipeGesturePrivate *priv = gesture->priv;
+
+ if (priv->sequence[1] != NULL)
+ return FALSE;
+
+ if (priv->sequence[0] == NULL)
+ priv->sequence[0] = _gtk_sequence_tracker_new (event);
+ else
+ priv->sequence[1] = _gtk_sequence_tracker_new (event);
+
+ if (priv->sequence[1])
+ gtk_event_tracker_start (GTK_EVENT_TRACKER (gesture));
+
+ return FALSE;
+}
+
+gboolean
+gtk_swipe_gesture_update_for_event (GtkSwipeGesture *gesture,
+ GdkEvent *event)
+{
+ GtkSwipeGesturePrivate *priv = gesture->priv;
+ gboolean result = FALSE;
+ guint i;
+
+ for (i = 0; i < 2; i++)
+ {
+ if (priv->sequence[i] &&
+ _gtk_sequence_tracker_update (priv->sequence[i], event))
+ {
+ priv->direction &= _gtk_sequence_tracker_get_direction (priv->sequence[i]);
+ if (priv->direction == 0)
+ {
+ gtk_event_tracker_cancel (GTK_EVENT_TRACKER (gesture));
+ return FALSE;
+ }
+
+ result = TRUE;
+ }
+ }
+
+ return result;
+}
+
+gboolean
+_gtk_swipe_gesture_update (GtkSwipeGesture *gesture,
+ GdkEvent *event)
+{
+ if (gtk_swipe_gesture_update_for_event (gesture, event) &&
+ gtk_event_tracker_is_started (GTK_EVENT_TRACKER (gesture)))
+ gtk_event_tracker_update (GTK_EVENT_TRACKER (gesture));
+
+ return FALSE;
+}
+
+gboolean
+_gtk_swipe_gesture_end (GtkSwipeGesture *gesture,
+ GdkEvent *event)
+{
+ if (gtk_swipe_gesture_update_for_event (gesture, event))
+ {
+ if (gesture->priv->sequence[1])
+ gtk_event_tracker_finish (GTK_EVENT_TRACKER (gesture));
+ else
+ gtk_event_tracker_cancel (GTK_EVENT_TRACKER (gesture));
+ }
+
+ return FALSE;
+}
+
+gboolean
+_gtk_swipe_gesture_cancel (GtkSwipeGesture *gesture,
+ GdkEvent *event)
+{
+ if (gtk_swipe_gesture_update_for_event (gesture, event))
+ gtk_event_tracker_cancel (GTK_EVENT_TRACKER (gesture));
+
+ return FALSE;
+}
+
+void
+gtk_swipe_gesture_get_offset (GtkSwipeGesture *gesture,
+ double *x,
+ double *y)
+{
+ GtkSwipeGesturePrivate *priv;
+
+ g_return_if_fail (GTK_IS_SWIPE_GESTURE (gesture));
+
+ priv = gesture->priv;
+
+ if (!gtk_event_tracker_is_started (GTK_EVENT_TRACKER (gesture)) ||
+ gtk_event_tracker_is_cancelled (GTK_EVENT_TRACKER (gesture)))
+ {
+ if (x)
+ *x = 0;
+ if (y)
+ *y = 0;
+
+ return;
+ }
+
+ if (x)
+ *x = (_gtk_sequence_tracker_get_x_offset (priv->sequence[0])
+ + _gtk_sequence_tracker_get_x_offset (priv->sequence[1])) / 2;
+ if (y)
+ *y = (_gtk_sequence_tracker_get_y_offset (priv->sequence[0])
+ + _gtk_sequence_tracker_get_y_offset (priv->sequence[1])) / 2;
+}
+
diff --git a/gtk/gtkswipegesture.h b/gtk/gtkswipegesture.h
new file mode 100644
index 0000000..63f3c13
--- /dev/null
+++ b/gtk/gtkswipegesture.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright  2012 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.1 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#ifndef __GTK_SWIPE_GESTURE_H__
+#define __GTK_SWIPE_GESTURE_H__
+
+#include <gtk/gtkeventtracker.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SWIPE_GESTURE (gtk_swipe_gesture_get_type ())
+#define GTK_SWIPE_GESTURE(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_SWIPE_GESTURE, GtkSwipeGesture))
+#define GTK_SWIPE_GESTURE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_SWIPE_GESTURE, GtkSwipeGestureClass))
+#define GTK_IS_SWIPE_GESTURE(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_SWIPE_GESTURE))
+#define GTK_IS_SWIPE_GESTURE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_SWIPE_GESTURE))
+#define GTK_SWIPE_GESTURE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SWIPE_GESTURE, GtkSwipeGestureClass))
+
+typedef struct _GtkSwipeGesture GtkSwipeGesture;
+typedef struct _GtkSwipeGestureClass GtkSwipeGestureClass;
+typedef struct _GtkSwipeGesturePrivate GtkSwipeGesturePrivate;
+
+struct _GtkSwipeGesture
+{
+ GtkEventTracker parent;
+
+ GtkSwipeGesturePrivate *priv;
+};
+
+struct _GtkSwipeGestureClass
+{
+ GtkEventTrackerClass parent_class;
+
+ /* Padding for future expansion */
+ void (*_gtk_reserved0) (void);
+ void (*_gtk_reserved1) (void);
+ void (*_gtk_reserved2) (void);
+ void (*_gtk_reserved3) (void);
+ void (*_gtk_reserved4) (void);
+ void (*_gtk_reserved5) (void);
+ void (*_gtk_reserved6) (void);
+ void (*_gtk_reserved7) (void);
+};
+
+GType gtk_swipe_gesture_get_type (void) G_GNUC_CONST;
+
+void gtk_swipe_gesture_get_offset (GtkSwipeGesture *gesture,
+ double *x,
+ double *y);
+
+#include <gdk/gdk.h>
+
+gboolean _gtk_swipe_gesture_begin (GtkSwipeGesture *gesture,
+ GdkEvent *event);
+gboolean _gtk_swipe_gesture_update (GtkSwipeGesture *gesture,
+ GdkEvent *event);
+gboolean _gtk_swipe_gesture_end (GtkSwipeGesture *gesture,
+ GdkEvent *event);
+gboolean _gtk_swipe_gesture_cancel (GtkSwipeGesture *gesture,
+ GdkEvent *event);
+
+G_END_DECLS
+
+#endif /* __GTK_SWIPE_GESTURE_H__ */
diff --git a/gtk/gtkswiperecognizer.c b/gtk/gtkswiperecognizer.c
new file mode 100644
index 0000000..047828a
--- /dev/null
+++ b/gtk/gtkswiperecognizer.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright  2012 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.1 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkswiperecognizer.h"
+
+#include "gtkswipegesture.h"
+
+/**
+ * SECTION:gtkswiperecognizer
+ * @Short_description: Recognizes vertical and horizontal swipe gestures
+ * @Title: GtkSwipeRecognizer
+ * @See_also: #GtkSwipeGesture
+ *
+ * #GtkSwipeRecoginzer recognizes vertical and horizontal swipe gestures.
+ *
+ * #GtkSwipeRecognizer was added in GTK 3.6.
+ */
+
+G_DEFINE_TYPE (GtkSwipeRecognizer, gtk_swipe_recognizer, GTK_TYPE_EVENT_RECOGNIZER)
+
+static void
+gtk_swipe_recognizer_recognize (GtkEventRecognizer *recognizer,
+ GtkWidget *widget,
+ GdkEvent *event)
+{
+ if (event->type == GDK_TOUCH_BEGIN)
+ gtk_event_recognizer_create_tracker (recognizer, widget, event);
+}
+
+static gboolean
+gtk_swipe_recognizer_track (GtkEventRecognizer *recognizer,
+ GtkEventTracker *tracker,
+ GdkEvent *event)
+{
+ GtkSwipeGesture *gesture = GTK_SWIPE_GESTURE (tracker);
+
+ switch (event->type)
+ {
+ case GDK_TOUCH_BEGIN:
+ return _gtk_swipe_gesture_begin (gesture, event);
+ case GDK_TOUCH_END:
+ return _gtk_swipe_gesture_end (gesture, event);
+ case GDK_TOUCH_UPDATE:
+ return _gtk_swipe_gesture_update (gesture, event);
+ case GDK_TOUCH_CANCEL:
+ return _gtk_swipe_gesture_cancel (gesture, event);
+ default:
+ return FALSE;
+ }
+}
+
+static void
+gtk_swipe_recognizer_class_init (GtkSwipeRecognizerClass *klass)
+{
+ GtkEventRecognizerClass *recognizer_class = GTK_EVENT_RECOGNIZER_CLASS (klass);
+
+ recognizer_class->recognize = gtk_swipe_recognizer_recognize;
+ recognizer_class->track = gtk_swipe_recognizer_track;
+
+ gtk_event_recognizer_class_set_event_mask (recognizer_class,
+ GDK_TOUCH_MASK);
+ gtk_event_recognizer_class_set_tracker_type (recognizer_class,
+ GTK_TYPE_SWIPE_GESTURE);
+}
+
+static void
+gtk_swipe_recognizer_init (GtkSwipeRecognizer *recognizer)
+{
+}
+
+GtkEventRecognizer *
+gtk_swipe_recognizer_new (void)
+{
+ return g_object_new (GTK_TYPE_SWIPE_RECOGNIZER, NULL);
+}
+
diff --git a/gtk/gtkswiperecognizer.h b/gtk/gtkswiperecognizer.h
new file mode 100644
index 0000000..0df09ef
--- /dev/null
+++ b/gtk/gtkswiperecognizer.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright  2012 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.1 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#ifndef __GTK_SWIPE_RECOGNIZER_H__
+#define __GTK_SWIPE_RECOGNIZER_H__
+
+#include <gtk/gtkeventrecognizer.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SWIPE_RECOGNIZER (gtk_swipe_recognizer_get_type ())
+#define GTK_SWIPE_RECOGNIZER(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_SWIPE_RECOGNIZER, GtkSwipeRecognizer))
+#define GTK_SWIPE_RECOGNIZER_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_SWIPE_RECOGNIZER, GtkSwipeRecognizerClass))
+#define GTK_IS_SWIPE_RECOGNIZER(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_SWIPE_RECOGNIZER))
+#define GTK_IS_SWIPE_RECOGNIZER_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_SWIPE_RECOGNIZER))
+#define GTK_SWIPE_RECOGNIZER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SWIPE_RECOGNIZER, GtkSwipeRecognizerClass))
+
+typedef struct _GtkSwipeRecognizer GtkSwipeRecognizer;
+typedef struct _GtkSwipeRecognizerClass GtkSwipeRecognizerClass;
+typedef struct _GtkSwipeRecognizerPrivate GtkSwipeRecognizerPrivate;
+
+struct _GtkSwipeRecognizer
+{
+ GtkEventRecognizer parent;
+
+ GtkSwipeRecognizerPrivate *priv;
+};
+
+struct _GtkSwipeRecognizerClass
+{
+ GtkEventRecognizerClass parent_class;
+
+ /* Padding for future expansion */
+ void (*_gtk_reserved0) (void);
+ void (*_gtk_reserved1) (void);
+ void (*_gtk_reserved2) (void);
+ void (*_gtk_reserved3) (void);
+ void (*_gtk_reserved4) (void);
+ void (*_gtk_reserved5) (void);
+ void (*_gtk_reserved6) (void);
+ void (*_gtk_reserved7) (void);
+};
+
+GType gtk_swipe_recognizer_get_type (void) G_GNUC_CONST;
+
+GtkEventRecognizer * gtk_swipe_recognizer_new (void);
+
+
+G_END_DECLS
+
+#endif /* __GTK_SWIPE_RECOGNIZER_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]