/* 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 .
*
* Author(s): Carlos Garnacho
*/
/**
* 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
* controllers. Those react to a series of #GdkEvents, and possibly trigger
* actions as a consequence of those.
*/
#include "config.h"
#include "gtkeventcontroller.h"
#include "gtkeventcontrollerprivate.h"
#include "gtkwidgetprivate.h"
#include "gtktypebuiltins.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"
typedef struct _GtkEventControllerPrivate GtkEventControllerPrivate;
enum {
PROP_WIDGET = 1,
PROP_PROPAGATION_PHASE,
LAST_PROP
};
static GParamSpec *properties[LAST_PROP] = { NULL, };
struct _GtkEventControllerPrivate
{
GtkWidget *widget;
GtkPropagationPhase phase;
};
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)
{
GtkEventController *self = GTK_EVENT_CONTROLLER (object);
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
switch (prop_id)
{
case PROP_WIDGET:
priv->widget = g_value_get_object (value);
if (priv->widget)
{
g_object_add_weak_pointer (G_OBJECT (priv->widget), (gpointer *) &priv->widget);
_gtk_widget_add_controller (priv->widget, self);
}
break;
case PROP_PROPAGATION_PHASE:
gtk_event_controller_set_propagation_phase (self,
g_value_get_enum (value));
break;
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)
{
GtkEventController *self = GTK_EVENT_CONTROLLER (object);
GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (self);
switch (prop_id)
{
case PROP_WIDGET:
g_value_set_object (value, priv->widget);
break;
case PROP_PROPAGATION_PHASE:
g_value_set_enum (value, priv->phase);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_event_controller_dispose (GObject *object)
{
GtkEventController *controller = GTK_EVENT_CONTROLLER (object);
GtkEventControllerPrivate *priv;
priv = gtk_event_controller_get_instance_private (controller);
if (priv->widget)
{
_gtk_widget_remove_controller (priv->widget, controller);
g_object_remove_weak_pointer (G_OBJECT (priv->widget), (gpointer *) &priv->widget);
priv->widget = NULL;
}
G_OBJECT_CLASS (gtk_event_controller_parent_class)->dispose (object);
}
static void
gtk_event_controller_class_init (GtkEventControllerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
klass->filter_event = gtk_event_controller_handle_event_default;
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;
object_class->dispose = gtk_event_controller_dispose;
/**
* GtkEventController:widget:
*
* The widget receiving the #GdkEvents that the controller will handle.
*/
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);
/**
* GtkEventController:propagation-phase:
*
* The propagation phase at which this controller will handle events.
*/
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);
}
static void
gtk_event_controller_init (GtkEventController *controller)
{
GtkEventControllerPrivate *priv;
priv = gtk_event_controller_get_instance_private (controller);
priv->phase = GTK_PHASE_BUBBLE;
}
/**
* 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
* controller action
**/
gboolean
gtk_event_controller_handle_event (GtkEventController *controller,
const GdkEvent *event)
{
GtkEventControllerClass *controller_class;
gboolean retval = FALSE;
g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER (controller), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);
if (controller_class->filter_event (controller, event))
return retval;
if (controller_class->handle_event)
{
g_object_ref (controller);
retval = controller_class->handle_event (controller, event);
g_object_unref (controller);
}
return retval;
}
/**
* gtk_event_controller_get_widget:
* @controller: a #GtkEventController
*
* Returns the #GtkWidget this controller relates to.
*
* Returns: (transfer none): a #GtkWidget
**/
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 gtk_event_controll_handle_event()
* will be dropped at this point.
**/
void
gtk_event_controller_reset (GtkEventController *controller)
{
GtkEventControllerClass *controller_class;
g_return_if_fail (GTK_IS_EVENT_CONTROLLER (controller));
controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);
if (controller_class->reset)
controller_class->reset (controller);
}
/**
* 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);
g_object_notify_by_pspec (G_OBJECT (controller), properties[PROP_PROPAGATION_PHASE]);
}