[gtk+/gestures: 5/8] Add GtkGestureRotate



commit a38645595d2dc83d6ff79b3ddbabd74d595dfc26
Author: Carlos Garnacho <carlos lanedo com>
Date:   Thu Jan 17 21:06:06 2013 +0100

    Add GtkGestureRotate
    
    This gesture implementation recognizes rotations when fed with
    events from two different GdkEventSequences

 gtk/Makefile.am        |    2 +
 gtk/gtk.h              |    1 +
 gtk/gtkgesturerotate.c |  201 ++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkgesturerotate.h |   66 ++++++++++++++++
 4 files changed, 270 insertions(+), 0 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 9285464..c58fdce 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -259,6 +259,7 @@ gtk_public_h_sources = 		\
 	gtkframe.h		\
 	gtkgesture.h		\
 	gtkgesturelongpress.h	\
+	gtkgesturerotate.h	\
 	gtkgestureswipe.h	\
 	gtkgrid.h		\
 	gtkiconfactory.h	\
@@ -733,6 +734,7 @@ gtk_base_c_sources = 		\
 	gtkframe.c		\
 	gtkgesture.c		\
 	gtkgesturelongpress.c	\
+	gtkgesturerotate.c	\
 	gtkgestureswipe.c	\
 	gtkgrid.c		\
 	gtkhsla.c		\
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 06c6ee4..3a22f00 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -110,6 +110,7 @@
 #include <gtk/gtkframe.h>
 #include <gtk/gtkgesture.h>
 #include <gtk/gtkgesturelongpress.h>
+#include <gtk/gtkgesturerotate.h>
 #include <gtk/gtkgestureswipe.h>
 #include <gtk/gtkgrid.h>
 #include <gtk/gtkiconfactory.h>
diff --git a/gtk/gtkgesturerotate.c b/gtk/gtkgesturerotate.c
new file mode 100644
index 0000000..f9e8040
--- /dev/null
+++ b/gtk/gtkgesturerotate.c
@@ -0,0 +1,201 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2012, One Laptop Per Child.
+ *
+ * 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 <carlos lanedo com>
+ */
+#include <math.h>
+#include <gtk/gtkgesturerotate.h>
+#include "gtkmarshalers.h"
+
+enum {
+  ANGLE_CHANGED,
+  LAST_SIGNAL
+};
+
+struct _GtkGestureRotatePriv
+{
+  gdouble initial_angle;
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+G_DEFINE_TYPE (GtkGestureRotate, gtk_gesture_rotate, GTK_TYPE_GESTURE)
+
+static void
+gtk_gesture_rotate_init (GtkGestureRotate *gesture)
+{
+  gesture->_priv = G_TYPE_INSTANCE_GET_PRIVATE (gesture,
+                                                GTK_TYPE_GESTURE_ROTATE,
+                                                GtkGestureRotatePriv);
+}
+
+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)
+{
+  gdouble x1, y1, x2, y2;
+  GtkGesture *gesture;
+  gdouble dx, dy;
+  GList *sequences;
+
+  gesture = GTK_GESTURE (rotate);
+
+  if (!gtk_gesture_is_active (gesture))
+    return FALSE;
+
+  sequences = gtk_gesture_get_sequences (gesture);
+  g_assert (sequences && sequences->next);
+
+  gtk_gesture_get_point (gesture, sequences->data, &x1, &y1);
+  gtk_gesture_get_point (gesture, sequences->next->data, &x2, &y2);
+  g_list_free (sequences);
+
+  dx = x1 - x2;
+  dy = y1 - y2;
+
+  *angle = atan2 (dx, dy);
+
+  /* Invert angle */
+  *angle = (2 * G_PI) - *angle;
+
+  /* And constraint it to 0Â-360Â */
+  *angle = fmod (*angle, 2 * G_PI);
+
+  return TRUE;
+}
+
+static gboolean
+_gtk_gesture_rotate_check_emit (GtkGestureRotate *gesture)
+{
+  GtkGestureRotatePriv *priv = gesture->_priv;
+  gdouble angle;
+
+  if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
+    return FALSE;
+
+  g_signal_emit (gesture, signals[ANGLE_CHANGED], 0,
+                 angle, angle - priv->initial_angle);
+  return TRUE;
+}
+
+static void
+gtk_gesture_rotate_begin (GtkGesture *gesture)
+{
+  GtkGestureRotate *rotate = GTK_GESTURE_ROTATE (gesture);
+  GtkGestureRotatePriv *priv = rotate->_priv;
+
+  _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));
+}
+
+static void
+gtk_gesture_rotate_class_init (GtkGestureRotateClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
+
+  object_class->constructor = gtk_gesture_rotate_constructor;
+
+  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
+   * @angle_delta: Difference with the starting angle in radians
+   */
+  signals[ANGLE_CHANGED] =
+    g_signal_new ("angle-changed",
+                  GTK_TYPE_GESTURE_ROTATE,
+                  G_SIGNAL_RUN_FIRST,
+                  G_STRUCT_OFFSET (GtkGestureRotateClass, angle_changed),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
+                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
+
+  g_type_class_add_private (klass, sizeof (GtkGestureRotatePriv));
+}
+
+/**
+ * gtk_gesture_rotate_new:
+ *
+ * Returns a newly created #GtkGesture that recognizes 2-touch
+ * rotation gestures.
+ *
+ * Returns: a newly created #GtkGestureRotate
+ *
+ * Since: 3.8
+ **/
+GtkGesture *
+gtk_gesture_rotate_new (void)
+{
+  return g_object_new (GTK_TYPE_GESTURE_ROTATE, NULL);
+}
+
+/**
+ * gtk_gesture_rotate_get_angle_delta:
+ * @gesture: a #GtkGestureRotate
+ * @delta: (out) (transfer none): angle delta
+ *
+ * If @gesture is active, this function returns %TRUE and fills
+ * in @delta with the angle difference in radians since the
+ * gesture was first recognized.
+ *
+ * Returns: %TRUE if @controller is recognizing a rotate gesture
+ *
+ * Since: 3.8
+ **/
+gboolean
+gtk_gesture_rotate_get_angle_delta (GtkGestureRotate *gesture,
+                                    gdouble          *delta)
+{
+  GtkGestureRotatePriv *priv;
+  gdouble angle;
+
+  g_return_val_if_fail (GTK_IS_GESTURE_ROTATE (gesture), FALSE);
+
+  if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
+    return FALSE;
+
+  priv = gesture->_priv;
+
+  if (delta)
+    *delta = angle - priv->initial_angle;
+
+  return TRUE;
+}
diff --git a/gtk/gtkgesturerotate.h b/gtk/gtkgesturerotate.h
new file mode 100644
index 0000000..f50ac9b
--- /dev/null
+++ b/gtk/gtkgesturerotate.h
@@ -0,0 +1,66 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2012, One Laptop Per Child.
+ *
+ * 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 <carlos lanedo com>
+ */
+#ifndef __GTK_GESTURE_ROTATE_H__
+#define __GTK_GESTURE_ROTATE_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkgesture.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_GESTURE_ROTATE         (gtk_gesture_rotate_get_type ())
+#define GTK_GESTURE_ROTATE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_GESTURE_ROTATE, GtkGestureRotate))
+#define GTK_GESTURE_ROTATE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_GESTURE_ROTATE, GtkGestureRotateClass))
+#define GTK_IS_GESTURE_ROTATE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_GESTURE_ROTATE))
+#define GTK_IS_GESTURE_ROTATE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_GESTURE_ROTATE))
+#define GTK_GESTURE_ROTATE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_GESTURE_ROTATE, GtkGestureRotateClass))
+
+typedef struct _GtkGestureRotate GtkGestureRotate;
+typedef struct _GtkGestureRotateClass GtkGestureRotateClass;
+typedef struct _GtkGestureRotatePriv GtkGestureRotatePriv;
+
+struct _GtkGestureRotate
+{
+  GtkGesture parent_instance;
+
+  /*< private >*/
+  GtkGestureRotatePriv *_priv;
+};
+
+struct _GtkGestureRotateClass
+{
+  GtkGestureClass parent_class;
+
+  void (* angle_changed) (GtkGestureRotate *gesture,
+                          gdouble           angle,
+                          gdouble           delta);
+};
+
+GType        gtk_gesture_rotate_get_type        (void) G_GNUC_CONST;
+GtkGesture * gtk_gesture_rotate_new             (void);
+
+gboolean     gtk_gesture_rotate_get_angle_delta (GtkGestureRotate *gesture,
+                                                 gdouble          *delta);
+
+G_END_DECLS
+
+#endif /* __GTK_GESTURE_ROTATE_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]