gtkgesturerotate.c 8.16 KB
Newer Older
Carlos Garnacho's avatar
Carlos Garnacho committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GTK - The GIMP Toolkit
 * Copyright (C) 2012, One Laptop Per Child.
 * 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>
 */
20 21 22 23 24 25 26 27 28

/**
 * SECTION:gtkgesturerotate
 * @Short_description: Rotate gesture
 * @Title: GtkGestureRotate
 * @See_also: #GtkGestureZoom
 *
 * #GtkGestureRotate is a #GtkGesture implementation able to recognize
 * 2-finger rotations, whenever the angle between both handled sequences
Carlos Garnacho's avatar
Carlos Garnacho committed
29
 * changes, the #GtkGestureRotate::angle-changed signal is emitted.
30 31
 */

Carlos Garnacho's avatar
Carlos Garnacho committed
32 33
#include "config.h"
#include <math.h>
34 35
#include "gtkgesturerotate.h"
#include "gtkgesturerotateprivate.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
36
#include "gtkmarshalers.h"
37
#include "gtkintl.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
38 39 40 41 42 43 44 45 46 47 48

typedef struct _GtkGestureRotatePrivate GtkGestureRotatePrivate;

enum {
  ANGLE_CHANGED,
  LAST_SIGNAL
};

struct _GtkGestureRotatePrivate
{
  gdouble initial_angle;
49
  gdouble accum_touchpad_angle;
Carlos Garnacho's avatar
Carlos Garnacho committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureRotate, gtk_gesture_rotate, GTK_TYPE_GESTURE)

static void
gtk_gesture_rotate_init (GtkGestureRotate *gesture)
{
}

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)
{
80 81
  GtkGestureRotatePrivate *priv;
  const GdkEvent *last_event;
Carlos Garnacho's avatar
Carlos Garnacho committed
82 83 84 85 86 87
  gdouble x1, y1, x2, y2;
  GtkGesture *gesture;
  gdouble dx, dy;
  GList *sequences;

  gesture = GTK_GESTURE (rotate);
88
  priv = gtk_gesture_rotate_get_instance_private (rotate);
Carlos Garnacho's avatar
Carlos Garnacho committed
89 90 91 92 93

  if (!gtk_gesture_is_recognized (gesture))
    return FALSE;

  sequences = gtk_gesture_get_sequences (gesture);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  if (!sequences)
    return FALSE;

  last_event = gtk_gesture_get_last_event (gesture, sequences->data);

  if (last_event->type == GDK_TOUCHPAD_PINCH &&
      (last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN ||
       last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE ||
       last_event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END))
    {
      *angle = priv->accum_touchpad_angle;
    }
  else
    {
      if (!sequences->next)
        return FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
110

111 112 113
      gtk_gesture_get_point (gesture, sequences->data, &x1, &y1);
      gtk_gesture_get_point (gesture, sequences->next->data, &x2, &y2);
      g_list_free (sequences);
Carlos Garnacho's avatar
Carlos Garnacho committed
114

115 116
      dx = x1 - x2;
      dy = y1 - y2;
Carlos Garnacho's avatar
Carlos Garnacho committed
117

118
      *angle = atan2 (dx, dy);
Carlos Garnacho's avatar
Carlos Garnacho committed
119

120 121
      /* Invert angle */
      *angle = (2 * G_PI) - *angle;
Carlos Garnacho's avatar
Carlos Garnacho committed
122

123 124 125
      /* And constraint it to 0°-360° */
      *angle = fmod (*angle, 2 * G_PI);
    }
Carlos Garnacho's avatar
Carlos Garnacho committed
126 127 128 129 130 131 132 133

  return TRUE;
}

static gboolean
_gtk_gesture_rotate_check_emit (GtkGestureRotate *gesture)
{
  GtkGestureRotatePrivate *priv;
134
  gdouble angle, delta;
Carlos Garnacho's avatar
Carlos Garnacho committed
135 136 137 138 139

  if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
    return FALSE;

  priv = gtk_gesture_rotate_get_instance_private (gesture);
140
  delta = angle - priv->initial_angle;
Carlos Garnacho's avatar
Carlos Garnacho committed
141

142 143 144 145
  if (delta < 0)
    delta += 2 * G_PI;

  g_signal_emit (gesture, signals[ANGLE_CHANGED], 0, angle, delta);
Carlos Garnacho's avatar
Carlos Garnacho committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  return TRUE;
}

static void
gtk_gesture_rotate_begin (GtkGesture       *gesture,
                          GdkEventSequence *sequence)
{
  GtkGestureRotate *rotate = GTK_GESTURE_ROTATE (gesture);
  GtkGestureRotatePrivate *priv;

  priv = gtk_gesture_rotate_get_instance_private (rotate);
  _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));
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static gboolean
gtk_gesture_rotate_filter_event (GtkEventController *controller,
                                 const GdkEvent     *event)
{
  /* Let 2-finger touchpad pinch events go through */
  if (event->type == GDK_TOUCHPAD_PINCH)
    {
      if (event->touchpad_pinch.n_fingers == 2)
        return FALSE;
      else
        return TRUE;
    }

  return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_rotate_parent_class)->filter_event (controller, event);
}

static gboolean
gtk_gesture_rotate_handle_event (GtkEventController *controller,
                                 const GdkEvent     *event)
{
  GtkGestureRotate *rotate = GTK_GESTURE_ROTATE (controller);
  GtkGestureRotatePrivate *priv;

  priv = gtk_gesture_rotate_get_instance_private (rotate);

  if (event->type == GDK_TOUCHPAD_PINCH)
    {
      if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN ||
          event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END)
        priv->accum_touchpad_angle = 0;
      else if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE)
        priv->accum_touchpad_angle += event->touchpad_pinch.angle_delta;
    }

  return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_rotate_parent_class)->handle_event (controller, event);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
204 205 206 207
static void
gtk_gesture_rotate_class_init (GtkGestureRotateClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
208
  GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
Carlos Garnacho's avatar
Carlos Garnacho committed
209 210 211 212
  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);

  object_class->constructor = gtk_gesture_rotate_constructor;

213 214 215
  event_controller_class->filter_event = gtk_gesture_rotate_filter_event;
  event_controller_class->handle_event = gtk_gesture_rotate_handle_event;

Carlos Garnacho's avatar
Carlos Garnacho committed
216 217 218 219 220 221 222
  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
223 224 225 226 227 228
   * @angle_delta: Difference with the starting angle, in radians
   *
   * This signal is emitted when the angle between both tracked points
   * changes.
   *
   * Since: 3.14
Carlos Garnacho's avatar
Carlos Garnacho committed
229 230
   */
  signals[ANGLE_CHANGED] =
231
    g_signal_new (I_("angle-changed"),
Carlos Garnacho's avatar
Carlos Garnacho committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                  GTK_TYPE_GESTURE_ROTATE,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkGestureRotateClass, angle_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
}

/**
 * gtk_gesture_rotate_new:
 * @widget: a #GtkWidget
 *
 * Returns a newly created #GtkGesture that recognizes 2-touch
 * rotation gestures.
 *
 * Returns: a newly created #GtkGestureRotate
 *
 * Since: 3.14
 **/
GtkGesture *
gtk_gesture_rotate_new (GtkWidget *widget)
{
253 254
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

Carlos Garnacho's avatar
Carlos Garnacho committed
255 256 257 258 259 260 261 262 263
  return g_object_new (GTK_TYPE_GESTURE_ROTATE,
                       "widget", widget,
                       NULL);
}

/**
 * gtk_gesture_rotate_get_angle_delta:
 * @gesture: a #GtkGestureRotate
 *
264
 * If @gesture is active, this function returns the angle difference
265 266
 * in radians since the gesture was first recognized. If @gesture is
 * not active, 0 is returned.
Carlos Garnacho's avatar
Carlos Garnacho committed
267
 *
268
 * Returns: the angle delta in radians
Carlos Garnacho's avatar
Carlos Garnacho committed
269 270 271
 *
 * Since: 3.14
 **/
272 273
gdouble
gtk_gesture_rotate_get_angle_delta (GtkGestureRotate *gesture)
Carlos Garnacho's avatar
Carlos Garnacho committed
274 275 276 277
{
  GtkGestureRotatePrivate *priv;
  gdouble angle;

278
  g_return_val_if_fail (GTK_IS_GESTURE_ROTATE (gesture), 0.0);
Carlos Garnacho's avatar
Carlos Garnacho committed
279 280

  if (!_gtk_gesture_rotate_get_angle (gesture, &angle))
281
    return 0.0;
Carlos Garnacho's avatar
Carlos Garnacho committed
282 283 284

  priv = gtk_gesture_rotate_get_instance_private (gesture);

285
  return angle - priv->initial_angle;
Carlos Garnacho's avatar
Carlos Garnacho committed
286
}