gtkeventcontroller.c 10.3 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

/**
 * SECTION:gtkeventcontroller
 * @Short_description: Self-contained handler of series of events
 * @Title: GtkEventController
 * @See_also: #GtkGesture
 *
 * #GtkEventController is a base, low-level implementation for event
28
 * controllers. Those react to a series of #GdkEvents, and possibly trigger
29 30 31
 * actions as a consequence of those.
 */

Carlos Garnacho's avatar
Carlos Garnacho committed
32
#include "config.h"
33 34
#include "gtkeventcontroller.h"
#include "gtkeventcontrollerprivate.h"
35
#include "gtkwidgetprivate.h"
36
#include "gtktypebuiltins.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
37 38 39 40 41 42 43
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"

typedef struct _GtkEventControllerPrivate GtkEventControllerPrivate;

enum {
44
  PROP_WIDGET = 1,
45 46
  PROP_PROPAGATION_PHASE,
  LAST_PROP
Carlos Garnacho's avatar
Carlos Garnacho committed
47 48
};

49 50
static GParamSpec *properties[LAST_PROP] = { NULL, };

Carlos Garnacho's avatar
Carlos Garnacho committed
51 52 53 54
struct _GtkEventControllerPrivate
{
  GtkWidget *widget;
  guint evmask;
55
  GtkPropagationPhase phase;
Carlos Garnacho's avatar
Carlos Garnacho committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
};

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkEventController, gtk_event_controller, G_TYPE_OBJECT)

static gboolean
gtk_event_controller_handle_event_default (GtkEventController *controller,
                                           const GdkEvent     *event)
{
  return FALSE;
}

static void
gtk_event_controller_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkEventControllerPrivate *priv;

  priv = gtk_event_controller_get_instance_private (GTK_EVENT_CONTROLLER (object));

  switch (prop_id)
    {
    case PROP_WIDGET:
      priv->widget = g_value_get_object (value);
81 82
      if (priv->widget)
        g_object_add_weak_pointer (G_OBJECT (priv->widget), (gpointer *) &priv->widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
83
      break;
84 85 86 87
    case PROP_PROPAGATION_PHASE:
      gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (object),
                                                  g_value_get_enum (value));
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_event_controller_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkEventControllerPrivate *priv;

  priv = gtk_event_controller_get_instance_private (GTK_EVENT_CONTROLLER (object));

  switch (prop_id)
    {
    case PROP_WIDGET:
      g_value_set_object (value, priv->widget);
      break;
108 109 110
    case PROP_PROPAGATION_PHASE:
      g_value_set_enum (value, priv->phase);
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
111 112 113 114 115
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

116 117 118 119 120 121
static void
gtk_event_controller_constructed (GObject *object)
{
  GtkEventController *controller = GTK_EVENT_CONTROLLER (object);
  GtkEventControllerPrivate *priv;

122 123
  G_OBJECT_CLASS (gtk_event_controller_parent_class)->constructed (object);

124
  priv = gtk_event_controller_get_instance_private (controller);
125 126
  if (priv->widget)
    _gtk_widget_add_controller (priv->widget, controller);
127 128 129 130 131 132 133 134 135
}

static void
gtk_event_controller_dispose (GObject *object)
{
  GtkEventController *controller = GTK_EVENT_CONTROLLER (object);
  GtkEventControllerPrivate *priv;

  priv = gtk_event_controller_get_instance_private (controller);
136
  if (priv->widget)
137 138 139 140 141
    {
      _gtk_widget_remove_controller (priv->widget, controller);
      g_object_remove_weak_pointer (G_OBJECT (priv->widget), (gpointer *) &priv->widget);
      priv->widget = NULL;
    }
142 143

  G_OBJECT_CLASS (gtk_event_controller_parent_class)->dispose (object);
144
}
Carlos Garnacho's avatar
Carlos Garnacho committed
145 146 147 148 149 150

static void
gtk_event_controller_class_init (GtkEventControllerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

151
  klass->filter_event = gtk_event_controller_handle_event_default;
Carlos Garnacho's avatar
Carlos Garnacho committed
152 153 154 155
  klass->handle_event = gtk_event_controller_handle_event_default;

  object_class->set_property = gtk_event_controller_set_property;
  object_class->get_property = gtk_event_controller_get_property;
156 157
  object_class->constructed = gtk_event_controller_constructed;
  object_class->dispose = gtk_event_controller_dispose;
Carlos Garnacho's avatar
Carlos Garnacho committed
158

159 160 161 162 163 164 165
  /**
   * GtkEventController:widget:
   *
   * The widget receiving the #GdkEvents that the controller will handle.
   *
   * Since: 3.14
   */
166 167 168 169 170 171
  properties[PROP_WIDGET] =
      g_param_spec_object ("widget",
                           P_("Widget"),
                           P_("Widget the gesture relates to"),
                           GTK_TYPE_WIDGET,
                           GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY);
172 173 174 175 176 177 178
  /**
   * GtkEventController:propagation-phase:
   *
   * The propagation phase at which this controller will handle events.
   *
   * Since: 3.14
   */
179 180 181 182 183 184 185 186 187
  properties[PROP_PROPAGATION_PHASE] =
      g_param_spec_enum ("propagation-phase",
                         P_("Propagation phase"),
                         P_("Propagation phase at which this controller is run"),
                         GTK_TYPE_PROPAGATION_PHASE,
                         GTK_PHASE_BUBBLE,
                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, properties);
Carlos Garnacho's avatar
Carlos Garnacho committed
188 189 190 191 192
}

static void
gtk_event_controller_init (GtkEventController *controller)
{
193 194 195 196
  GtkEventControllerPrivate *priv;

  priv = gtk_event_controller_get_instance_private (controller);
  priv->phase = GTK_PHASE_BUBBLE;
Carlos Garnacho's avatar
Carlos Garnacho committed
197 198 199 200 201 202 203 204 205 206 207
}

/**
 * gtk_event_controller_handle_event:
 * @controller: a #GtkEventController
 * @event: a #GdkEvent
 *
 * Feeds an events into @controller, so it can be interpreted
 * and the controller actions triggered.
 *
 * Returns: %TRUE if the event was potentially useful to trigger the
Matthias Clasen's avatar
Matthias Clasen committed
208
 *          controller action
Carlos Garnacho's avatar
Carlos Garnacho committed
209 210 211 212 213 214 215
 *
 * Since: 3.14
 **/
gboolean
gtk_event_controller_handle_event (GtkEventController *controller,
                                   const GdkEvent     *event)
{
216
  GtkEventControllerClass *controller_class;
Carlos Garnacho's avatar
Carlos Garnacho committed
217 218 219 220 221
  gboolean retval = FALSE;

  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

222 223
  controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);

224 225 226
  if (controller_class->filter_event (controller, event))
    return retval;

227
  if (controller_class->handle_event)
228 229 230 231 232
    {
      g_object_ref (controller);
      retval = controller_class->handle_event (controller, event);
      g_object_unref (controller);
    }
Carlos Garnacho's avatar
Carlos Garnacho committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

  return retval;
}

void
gtk_event_controller_set_event_mask (GtkEventController *controller,
                                     GdkEventMask        event_mask)
{
  GtkEventControllerPrivate *priv;

  g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));

  priv = gtk_event_controller_get_instance_private (controller);

  if (priv->evmask == event_mask)
    return;

  priv->evmask = event_mask;
}

GdkEventMask
gtk_event_controller_get_event_mask (GtkEventController *controller)
{
  GtkEventControllerPrivate *priv;

  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), 0);

  priv = gtk_event_controller_get_instance_private (controller);

  return priv->evmask;
}

/**
 * gtk_event_controller_get_widget:
 * @controller: a #GtkEventController
 *
 * Returns the #GtkWidget this controller relates to.
 *
271
 * Returns: (transfer none): a #GtkWidget
Carlos Garnacho's avatar
Carlos Garnacho committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
 *
 * Since: 3.14
 **/
GtkWidget *
gtk_event_controller_get_widget (GtkEventController *controller)
{
  GtkEventControllerPrivate *priv;

  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), 0);

  priv = gtk_event_controller_get_instance_private (controller);

  return priv->widget;
}

/**
 * gtk_event_controller_reset:
 * @controller: a #GtkEventController
 *
 * Resets the @controller to a clean state. Every interaction
 * the controller did through #GtkEventController::handle-event
 * will be dropped at this point.
 *
 * Since: 3.14
 **/
void
gtk_event_controller_reset (GtkEventController *controller)
{
300 301
  GtkEventControllerClass *controller_class;

Carlos Garnacho's avatar
Carlos Garnacho committed
302 303
  g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));

304 305 306 307
  controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);

  if (controller_class->reset)
    controller_class->reset (controller);
Carlos Garnacho's avatar
Carlos Garnacho committed
308
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

/**
 * gtk_event_controller_get_propagation_phase:
 * @controller: a #GtkEventController
 *
 * Gets the propagation phase at which @controller handles events.
 *
 * Returns: the propagation phase
 *
 * Since: 3.14
 **/
GtkPropagationPhase
gtk_event_controller_get_propagation_phase (GtkEventController *controller)
{
  GtkEventControllerPrivate *priv;

  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), GTK_PHASE_NONE);

  priv = gtk_event_controller_get_instance_private (controller);

  return priv->phase;
}

/**
 * gtk_event_controller_set_propagation_phase:
 * @controller: a #GtkEventController
 * @phase: a propagation phase
 *
 * Sets the propagation phase at which a controller handles events.
 *
 * If @phase is %GTK_PHASE_NONE, no automatic event handling will be
 * performed, but other additional gesture maintenance will. In that phase,
 * the events can be managed by calling gtk_event_controller_handle_event().
 *
 * Since: 3.14
 **/
void
gtk_event_controller_set_propagation_phase (GtkEventController  *controller,
                                            GtkPropagationPhase  phase)
{
  GtkEventControllerPrivate *priv;

  g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));
  g_return_if_fail (phase >= GTK_PHASE_NONE && phase <= GTK_PHASE_TARGET);

  priv = gtk_event_controller_get_instance_private (controller);

  if (priv->phase == phase)
    return;

  priv->phase = phase;

  if (phase == GTK_PHASE_NONE)
    gtk_event_controller_reset (controller);

364
  g_object_notify_by_pspec (G_OBJECT (controller), properties[PROP_PROPAGATION_PHASE]);
365
}