2013-01-17 19:55:24 +00:00
|
|
|
/* 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>
|
|
|
|
*/
|
2014-04-08 19:01:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-04-09 16:27:31 +00:00
|
|
|
* controllers. Those react to a series of #GdkEvents, and possibly trigger
|
2014-04-08 19:01:13 +00:00
|
|
|
* actions as a consequence of those.
|
|
|
|
*/
|
|
|
|
|
2013-01-17 19:55:24 +00:00
|
|
|
#include "config.h"
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkeventcontroller.h"
|
|
|
|
#include "gtkeventcontrollerprivate.h"
|
2014-05-26 12:02:30 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2014-04-08 18:25:53 +00:00
|
|
|
#include "gtktypebuiltins.h"
|
2013-01-17 19:55:24 +00:00
|
|
|
#include "gtkmarshalers.h"
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
#include "gtkintl.h"
|
|
|
|
|
|
|
|
typedef struct _GtkEventControllerPrivate GtkEventControllerPrivate;
|
|
|
|
|
|
|
|
enum {
|
2014-05-26 12:02:30 +00:00
|
|
|
PROP_WIDGET = 1,
|
|
|
|
PROP_PROPAGATION_PHASE
|
2013-01-17 19:55:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkEventControllerPrivate
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
guint evmask;
|
2014-05-26 12:02:30 +00:00
|
|
|
GtkPropagationPhase phase;
|
2013-01-17 19:55:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2015-03-01 12:28:21 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (priv->widget), (gpointer *) &priv->widget);
|
2013-01-17 19:55:24 +00:00
|
|
|
break;
|
2014-05-26 12:02:30 +00:00
|
|
|
case PROP_PROPAGATION_PHASE:
|
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (object),
|
|
|
|
g_value_get_enum (value));
|
|
|
|
break;
|
2013-01-17 19:55:24 +00:00
|
|
|
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;
|
2014-05-26 12:02:30 +00:00
|
|
|
case PROP_PROPAGATION_PHASE:
|
|
|
|
g_value_set_enum (value, priv->phase);
|
|
|
|
break;
|
2013-01-17 19:55:24 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 12:02:30 +00:00
|
|
|
static void
|
|
|
|
gtk_event_controller_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
GtkEventController *controller = GTK_EVENT_CONTROLLER (object);
|
|
|
|
GtkEventControllerPrivate *priv;
|
|
|
|
|
2015-03-01 12:35:02 +00:00
|
|
|
G_OBJECT_CLASS (gtk_event_controller_parent_class)->constructed (object);
|
|
|
|
|
2014-05-26 12:02:30 +00:00
|
|
|
priv = gtk_event_controller_get_instance_private (controller);
|
2014-05-27 18:30:47 +00:00
|
|
|
if (priv->widget)
|
|
|
|
_gtk_widget_add_controller (priv->widget, controller);
|
2014-05-26 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkEventController *controller = GTK_EVENT_CONTROLLER (object);
|
|
|
|
GtkEventControllerPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_event_controller_get_instance_private (controller);
|
2014-05-27 18:30:47 +00:00
|
|
|
if (priv->widget)
|
|
|
|
_gtk_widget_remove_controller (priv->widget, controller);
|
2015-01-14 15:03:10 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_event_controller_parent_class)->dispose (object);
|
2014-05-26 12:02:30 +00:00
|
|
|
}
|
2013-01-17 19:55:24 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_class_init (GtkEventControllerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
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;
|
2014-05-26 12:02:30 +00:00
|
|
|
object_class->constructed = gtk_event_controller_constructed;
|
|
|
|
object_class->dispose = gtk_event_controller_dispose;
|
2013-01-17 19:55:24 +00:00
|
|
|
|
2014-04-09 16:27:31 +00:00
|
|
|
/**
|
|
|
|
* GtkEventController:widget:
|
|
|
|
*
|
|
|
|
* The widget receiving the #GdkEvents that the controller will handle.
|
|
|
|
*
|
|
|
|
* Since: 3.14
|
|
|
|
*/
|
2013-01-17 19:55:24 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
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));
|
2014-05-26 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* GtkEventController:propagation-phase:
|
|
|
|
*
|
|
|
|
* The propagation phase at which this controller will handle events.
|
|
|
|
*
|
|
|
|
* Since: 3.14
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
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,
|
2014-08-11 18:38:51 +00:00
|
|
|
GTK_PHASE_BUBBLE,
|
2014-06-08 16:02:17 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
|
2013-01-17 19:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_init (GtkEventController *controller)
|
|
|
|
{
|
2014-08-11 18:38:51 +00:00
|
|
|
GtkEventControllerPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_event_controller_get_instance_private (controller);
|
|
|
|
priv->phase = GTK_PHASE_BUBBLE;
|
2013-01-17 19:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-05-20 02:16:03 +00:00
|
|
|
* controller action
|
2013-01-17 19:55:24 +00:00
|
|
|
*
|
|
|
|
* Since: 3.14
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
gtk_event_controller_handle_event (GtkEventController *controller,
|
|
|
|
const GdkEvent *event)
|
|
|
|
{
|
2014-05-27 10:41:30 +00:00
|
|
|
GtkEventControllerClass *controller_class;
|
2013-01-17 19:55:24 +00:00
|
|
|
gboolean retval = FALSE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), FALSE);
|
|
|
|
g_return_val_if_fail (event != NULL, FALSE);
|
|
|
|
|
2014-05-27 10:41:30 +00:00
|
|
|
controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);
|
|
|
|
|
|
|
|
if (controller_class->handle_event)
|
2014-05-28 17:39:07 +00:00
|
|
|
{
|
|
|
|
g_object_ref (controller);
|
|
|
|
retval = controller_class->handle_event (controller, event);
|
|
|
|
g_object_unref (controller);
|
|
|
|
}
|
2013-01-17 19:55:24 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2014-05-20 12:08:27 +00:00
|
|
|
* Returns: (transfer none): a #GtkWidget
|
2013-01-17 19:55:24 +00:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
2014-05-27 10:41:30 +00:00
|
|
|
GtkEventControllerClass *controller_class;
|
|
|
|
|
2013-01-17 19:55:24 +00:00
|
|
|
g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));
|
|
|
|
|
2014-05-27 10:41:30 +00:00
|
|
|
controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);
|
|
|
|
|
|
|
|
if (controller_class->reset)
|
|
|
|
controller_class->reset (controller);
|
2013-01-17 19:55:24 +00:00
|
|
|
}
|
2014-05-26 12:02:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (controller), "propagation-phase");
|
|
|
|
}
|