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,
|
2015-09-06 14:46:15 +00:00
|
|
|
PROP_PROPAGATION_PHASE,
|
|
|
|
LAST_PROP
|
2013-01-17 19:55:24 +00:00
|
|
|
};
|
|
|
|
|
2015-09-06 14:46:15 +00:00
|
|
|
static GParamSpec *properties[LAST_PROP] = { NULL, };
|
|
|
|
|
2013-01-17 19:55:24 +00:00
|
|
|
struct _GtkEventControllerPrivate
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
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)
|
|
|
|
|
2018-03-08 02:30:44 +00:00
|
|
|
static void
|
|
|
|
gtk_event_controller_set_widget (GtkEventController *self,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
|
|
|
|
|
|
|
|
priv->widget = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_unset_widget (GtkEventController *self)
|
|
|
|
{
|
|
|
|
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
|
|
|
|
|
|
|
|
priv->widget = NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-02 19:11:30 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_event_controller_filter_event_default (GtkEventController *self,
|
|
|
|
const GdkEvent *event)
|
|
|
|
{
|
|
|
|
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
|
|
|
|
|
|
|
|
if (priv->widget)
|
2019-05-02 20:20:20 +00:00
|
|
|
return !gtk_widget_is_sensitive (priv->widget);
|
2019-05-02 19:11:30 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-01-17 19:55:24 +00:00
|
|
|
static gboolean
|
2018-03-08 02:30:44 +00:00
|
|
|
gtk_event_controller_handle_event_default (GtkEventController *self,
|
2013-01-17 19:55:24 +00:00
|
|
|
const GdkEvent *event)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2018-03-07 00:56:32 +00:00
|
|
|
GtkEventController *self = GTK_EVENT_CONTROLLER (object);
|
2013-01-17 19:55:24 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2014-05-26 12:02:30 +00:00
|
|
|
case PROP_PROPAGATION_PHASE:
|
2018-03-07 00:56:32 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (self,
|
2014-05-26 12:02:30 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-03-07 00:56:32 +00:00
|
|
|
GtkEventController *self = GTK_EVENT_CONTROLLER (object);
|
|
|
|
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
|
2013-01-17 19:55:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_event_controller_class_init (GtkEventControllerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2018-03-08 02:30:44 +00:00
|
|
|
klass->set_widget = gtk_event_controller_set_widget;
|
|
|
|
klass->unset_widget = gtk_event_controller_unset_widget;
|
2019-05-02 19:11:30 +00:00
|
|
|
klass->filter_event = gtk_event_controller_filter_event_default;
|
2013-01-17 19:55:24 +00:00
|
|
|
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-04-09 16:27:31 +00:00
|
|
|
/**
|
|
|
|
* GtkEventController:widget:
|
|
|
|
*
|
|
|
|
* The widget receiving the #GdkEvents that the controller will handle.
|
|
|
|
*/
|
2015-09-06 14:46:15 +00:00
|
|
|
properties[PROP_WIDGET] =
|
|
|
|
g_param_spec_object ("widget",
|
|
|
|
P_("Widget"),
|
|
|
|
P_("Widget the gesture relates to"),
|
|
|
|
GTK_TYPE_WIDGET,
|
2018-03-10 19:02:28 +00:00
|
|
|
GTK_PARAM_READABLE);
|
2014-05-26 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* GtkEventController:propagation-phase:
|
|
|
|
*
|
|
|
|
* The propagation phase at which this controller will handle events.
|
|
|
|
*/
|
2015-09-06 14:46:15 +00:00
|
|
|
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);
|
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
|
|
|
**/
|
|
|
|
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);
|
|
|
|
|
2015-07-09 16:55:32 +00:00
|
|
|
if (controller_class->filter_event (controller, event))
|
|
|
|
return retval;
|
|
|
|
|
2014-05-27 10:41:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
**/
|
|
|
|
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
|
2017-07-08 07:56:41 +00:00
|
|
|
* the controller did through gtk_event_controll_handle_event()
|
2013-01-17 19:55:24 +00:00
|
|
|
* will be dropped at this point.
|
|
|
|
**/
|
|
|
|
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
|
|
|
|
**/
|
|
|
|
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().
|
|
|
|
**/
|
|
|
|
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);
|
|
|
|
|
2015-09-06 14:46:15 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (controller), properties[PROP_PROPAGATION_PHASE]);
|
2014-05-26 12:02:30 +00:00
|
|
|
}
|