[gtk+/gestures: 6/8] Add GtkGestureZoom
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/gestures: 6/8] Add GtkGestureZoom
- Date: Thu, 17 Jan 2013 23:37:58 +0000 (UTC)
commit 779384ac151e79647e0b597fd48a960b2a9264a9
Author: Carlos Garnacho <carlos lanedo com>
Date: Thu Jan 17 21:08:20 2013 +0100
Add GtkGestureZoom
This gesture interprets and reports relative scale differences when fed
with events from two different GdkEventSequences.
gtk/Makefile.am | 2 +
gtk/gtk.h | 1 +
gtk/gtkgesturezoom.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++
gtk/gtkgesturezoom.h | 65 ++++++++++++++++
4 files changed, 267 insertions(+), 0 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index c58fdce..c55ac19 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -261,6 +261,7 @@ gtk_public_h_sources = \
gtkgesturelongpress.h \
gtkgesturerotate.h \
gtkgestureswipe.h \
+ gtkgesturezoom.h \
gtkgrid.h \
gtkiconfactory.h \
gtkicontheme.h \
@@ -736,6 +737,7 @@ gtk_base_c_sources = \
gtkgesturelongpress.c \
gtkgesturerotate.c \
gtkgestureswipe.c \
+ gtkgesturezoom.c \
gtkgrid.c \
gtkhsla.c \
gtkiconcache.c \
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 3a22f00..ad34617 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -112,6 +112,7 @@
#include <gtk/gtkgesturelongpress.h>
#include <gtk/gtkgesturerotate.h>
#include <gtk/gtkgestureswipe.h>
+#include <gtk/gtkgesturezoom.h>
#include <gtk/gtkgrid.h>
#include <gtk/gtkiconfactory.h>
#include <gtk/gtkicontheme.h>
diff --git a/gtk/gtkgesturezoom.c b/gtk/gtkgesturezoom.c
new file mode 100644
index 0000000..164026e
--- /dev/null
+++ b/gtk/gtkgesturezoom.c
@@ -0,0 +1,199 @@
+/* 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/gtkgesturezoom.h>
+
+enum {
+ SCALE_CHANGED,
+ LAST_SIGNAL
+};
+
+struct _GtkGestureZoomPriv
+{
+ gdouble initial_distance;
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+G_DEFINE_TYPE (GtkGestureZoom, gtk_gesture_zoom, GTK_TYPE_GESTURE)
+
+static void
+gtk_gesture_zoom_init (GtkGestureZoom *gesture)
+{
+ gesture->_priv = G_TYPE_INSTANCE_GET_PRIVATE (gesture,
+ GTK_TYPE_GESTURE_ZOOM,
+ GtkGestureZoomPriv);
+}
+
+static GObject *
+gtk_gesture_zoom_constructor (GType type,
+ guint n_construct_properties,
+ GObjectConstructParam *construct_properties)
+{
+ GObject *object;
+
+ object = G_OBJECT_CLASS (gtk_gesture_zoom_parent_class)->constructor (type,
+ n_construct_properties,
+ construct_properties);
+ g_object_set (object, "n-points", 2, NULL);
+
+ return object;
+}
+
+static gboolean
+_gtk_gesture_zoom_get_distance (GtkGestureZoom *zoom,
+ gdouble *distance)
+{
+ gdouble x1, y1, x2, y2;
+ GtkGesture *gesture;
+ GList *sequences;
+ gdouble dx, dy;
+
+ gesture = GTK_GESTURE (zoom);
+
+ 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;;
+ *distance = sqrt ((dx * dx) + (dy * dy));
+
+ return TRUE;
+}
+
+static gboolean
+_gtk_gesture_zoom_check_emit (GtkGestureZoom *gesture)
+{
+ GtkGestureZoomPriv *priv;
+ gdouble distance, zoom;
+
+ if (!_gtk_gesture_zoom_get_distance (gesture, &distance))
+ return FALSE;
+
+ priv = gesture->_priv;
+
+ if (distance == 0 || priv->initial_distance == 0)
+ return FALSE;
+
+ zoom = distance / priv->initial_distance;
+ g_signal_emit (gesture, signals[SCALE_CHANGED], 0, zoom);
+
+ return TRUE;
+}
+
+static void
+gtk_gesture_zoom_begin (GtkGesture *gesture)
+{
+ GtkGestureZoom *zoom = GTK_GESTURE_ZOOM (gesture);
+ GtkGestureZoomPriv *priv = zoom->_priv;
+
+ _gtk_gesture_zoom_get_distance (zoom, &priv->initial_distance);
+}
+
+static void
+gtk_gesture_zoom_update (GtkGesture *gesture,
+ GdkEventSequence *sequence)
+{
+ _gtk_gesture_zoom_check_emit (GTK_GESTURE_ZOOM (gesture));
+}
+
+static void
+gtk_gesture_zoom_class_init (GtkGestureZoomClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
+
+ object_class->constructor = gtk_gesture_zoom_constructor;
+
+ gesture_class->begin = gtk_gesture_zoom_begin;
+ gesture_class->update = gtk_gesture_zoom_update;
+
+ /**
+ * GtkGestureZoom::scale-changed:
+ * @controller: the object on which the signal is emitted
+ * @scale: Scale delta, taking the initial state as 1:1
+ */
+ signals[SCALE_CHANGED] =
+ g_signal_new ("scale-changed",
+ GTK_TYPE_GESTURE_ZOOM,
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GtkGestureZoomClass, scale_changed),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__DOUBLE,
+ G_TYPE_NONE, 1,
+ G_TYPE_DOUBLE);
+
+ g_type_class_add_private (klass, sizeof (GtkGestureZoomPriv));
+}
+
+/**
+ * gtk_gesture_zoom_new:
+ *
+ * Returns a newly created #GtkGesture that recognizes zoom
+ * in/out gestures (usually known as pinch/zoom)
+ *
+ * Returns: a newly created #GtkGestureZoom
+ *
+ * Since: 3.8
+ **/
+GtkGesture *
+gtk_gesture_zoom_new (void)
+{
+ return g_object_new (GTK_TYPE_GESTURE_ZOOM, NULL);
+}
+
+/**
+ * gtk_gesture_zoom_get_scale_delta:
+ * @gesture: a #GtkGestureZoom
+ * @scale: (out) (transfer none): zoom delta
+ *
+ * If @controller is active, this function returns %TRUE and fills
+ * in @scale with the zooming difference since the gesture was
+ * recognized (hence the starting point is considered 1:1).
+ *
+ * Returns: %TRUE if @controller is recognizing a zoom gesture
+ *
+ * Since: 3.8
+ **/
+gboolean
+gtk_gesture_zoom_get_scale_delta (GtkGestureZoom *gesture,
+ gdouble *scale)
+{
+ GtkGestureZoomPriv *priv;
+ gdouble distance;
+
+ g_return_val_if_fail (GTK_IS_GESTURE_ZOOM (gesture), FALSE);
+
+ if (!_gtk_gesture_zoom_get_distance (gesture, &distance))
+ return FALSE;
+
+ priv = gesture->_priv;
+
+ if (scale)
+ *scale = distance / priv->initial_distance;
+
+ return TRUE;
+}
diff --git a/gtk/gtkgesturezoom.h b/gtk/gtkgesturezoom.h
new file mode 100644
index 0000000..3709b0b
--- /dev/null
+++ b/gtk/gtkgesturezoom.h
@@ -0,0 +1,65 @@
+/* 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_ZOOM_H__
+#define __GTK_GESTURE_ZOOM_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkgesture.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_GESTURE_ZOOM (gtk_gesture_zoom_get_type ())
+#define GTK_GESTURE_ZOOM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoom))
+#define GTK_GESTURE_ZOOM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoomClass))
+#define GTK_IS_GESTURE_ZOOM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_GESTURE_ZOOM))
+#define GTK_IS_GESTURE_ZOOM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_GESTURE_ZOOM))
+#define GTK_GESTURE_ZOOM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoomClass))
+
+typedef struct _GtkGestureZoom GtkGestureZoom;
+typedef struct _GtkGestureZoomClass GtkGestureZoomClass;
+typedef struct _GtkGestureZoomPriv GtkGestureZoomPriv;
+
+struct _GtkGestureZoom
+{
+ GtkGesture parent_instance;
+
+ /*< private >*/
+ GtkGestureZoomPriv *_priv;
+};
+
+struct _GtkGestureZoomClass
+{
+ GtkGestureClass parent_class;
+
+ void (* scale_changed) (GtkGestureZoom *gesture,
+ gdouble scale);
+};
+
+GType gtk_gesture_zoom_get_type (void) G_GNUC_CONST;
+GtkGesture * gtk_gesture_zoom_new (void);
+gboolean gtk_gesture_zoom_get_scale_delta (GtkGestureZoom *gesture,
+ gdouble *scale);
+
+G_END_DECLS
+
+#endif /* __GTK_GESTURE_ZOOM_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]