longpress: Make a subclass of GtkGestureSingle

the redundant API here has been removed.
This commit is contained in:
Carlos Garnacho 2014-03-21 18:57:32 +01:00
parent 5f24d1a519
commit 3db87cb0b7
2 changed files with 28 additions and 126 deletions

View File

@ -28,10 +28,6 @@
typedef struct _GtkGestureLongPressPrivate GtkGestureLongPressPrivate;
enum {
PROP_BUTTON = 1
};
enum {
PRESSED,
CANCELLED,
@ -43,7 +39,6 @@ struct _GtkGestureLongPressPrivate
gdouble initial_x;
gdouble initial_y;
guint button;
guint timeout_id;
guint delay;
guint cancelled : 1;
@ -52,7 +47,7 @@ struct _GtkGestureLongPressPrivate
static guint signals[N_SIGNALS] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureLongPress, gtk_gesture_long_press, GTK_TYPE_GESTURE)
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureLongPress, gtk_gesture_long_press, GTK_TYPE_GESTURE_SINGLE)
static void
gtk_gesture_long_press_init (GtkGestureLongPress *gesture)
@ -96,20 +91,15 @@ gtk_gesture_long_press_begin (GtkGesture *gesture,
GdkEventSequence *sequence)
{
GtkGestureLongPressPrivate *priv;
GdkEvent *event;
guint button;
const GdkEvent *event;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
event = gtk_gesture_get_last_event (gesture, sequence);
if (event->type == GDK_BUTTON_PRESS)
button = event->button.button;
else if (event->type == GDK_TOUCH_BEGIN)
button = 1;
else
return;
if (priv->button != 0 && priv->button != button)
if (!event ||
(event->type != GDK_BUTTON_PRESS &&
event->type != GDK_TOUCH_BEGIN))
return;
gtk_gesture_get_point (gesture, sequence,
@ -164,6 +154,22 @@ gtk_gesture_long_press_end (GtkGesture *gesture,
priv->cancelled = priv->triggered = FALSE;
}
static void
gtk_gesture_long_press_sequence_state_changed (GtkGesture *gesture,
GdkEventSequence *sequence,
GtkEventSequenceState state)
{
GtkGestureLongPressPrivate *priv;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
if (state == GTK_EVENT_SEQUENCE_DENIED)
{
gtk_gesture_long_press_end (gesture, sequence);
priv->cancelled = TRUE;
}
}
static void
gtk_gesture_long_press_finalize (GObject *object)
{
@ -177,47 +183,6 @@ gtk_gesture_long_press_finalize (GObject *object)
G_OBJECT_CLASS (gtk_gesture_long_press_parent_class)->finalize (object);
}
static void
gtk_gesture_long_press_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_BUTTON:
gtk_gesture_long_press_set_button (GTK_GESTURE_LONG_PRESS (object),
g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_gesture_long_press_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkGestureLongPressPrivate *priv;
GtkGestureLongPress *gesture;
gesture = GTK_GESTURE_LONG_PRESS (object);
priv = gtk_gesture_long_press_get_instance_private (gesture);
switch (prop_id)
{
case PROP_BUTTON:
g_value_set_uint (value, priv->button);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_gesture_long_press_class_init (GtkGestureLongPressClass *klass)
{
@ -225,21 +190,15 @@ gtk_gesture_long_press_class_init (GtkGestureLongPressClass *klass)
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
object_class->finalize = gtk_gesture_long_press_finalize;
object_class->set_property = gtk_gesture_long_press_set_property;
object_class->get_property = gtk_gesture_long_press_get_property;
gesture_class->check = gtk_gesture_long_press_check;
gesture_class->begin = gtk_gesture_long_press_begin;
gesture_class->update = gtk_gesture_long_press_update;
gesture_class->end = gtk_gesture_long_press_end;
gesture_class->cancel = gtk_gesture_long_press_end;
gesture_class->sequence_state_changed =
gtk_gesture_long_press_sequence_state_changed;
g_object_class_install_property (object_class,
PROP_BUTTON,
g_param_spec_uint ("button",
P_("Button number"),
P_("Button number to listen to"),
0, G_MAXUINT, 0,
GTK_PARAM_READWRITE));
signals[PRESSED] =
g_signal_new ("pressed",
G_TYPE_FROM_CLASS (klass),
@ -273,54 +232,3 @@ gtk_gesture_long_press_new (GtkWidget *widget)
"widget", widget,
NULL);
}
/**
* gtk_gesture_long_press_set_button:
* @gesture: a #GtkGestureLongPress
* @button: button number to listen to, or 0 for any button
*
* Sets the button number @gesture listens to. If non-0, every
* button press from a different button number will be ignored.
* Touch events implicitly match with button 1.
*
* Since: 3.14
**/
void
gtk_gesture_long_press_set_button (GtkGestureLongPress *gesture,
guint button)
{
GtkGestureLongPressPrivate *priv;
g_return_if_fail (GTK_IS_GESTURE_LONG_PRESS (gesture));
priv = gtk_gesture_long_press_get_instance_private (gesture);
if (priv->button == button)
return;
priv->button = button;
g_object_notify (G_OBJECT (gesture), "button");
}
/**
* gtk_gesture_long_press_get_button:
* @gesture: a #GtkGestureLongPress
*
* Returns the button number @gesture listens for, or 0 if @gesture
* reacts to any button press.
*
* Returns: The button number, or 0 for any button.
*
* Since: 3.14
**/
guint
gtk_gesture_long_press_get_button (GtkGestureLongPress *gesture)
{
GtkGestureLongPressPrivate *priv;
g_return_val_if_fail (GTK_IS_GESTURE_LONG_PRESS (gesture), 0);
priv = gtk_gesture_long_press_get_instance_private (gesture);
return priv->button;
}

View File

@ -24,7 +24,7 @@
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkgesture.h>
#include <gtk/gtkgesturesingle.h>
G_BEGIN_DECLS
@ -40,12 +40,12 @@ typedef struct _GtkGestureLongPressClass GtkGestureLongPressClass;
struct _GtkGestureLongPress
{
GtkGesture parent_instance;
GtkGestureSingle parent_instance;
};
struct _GtkGestureLongPressClass
{
GtkGestureClass parent_class;
GtkGestureSingleClass parent_class;
void (* pressed) (GtkGestureLongPress *gesture,
gdouble x,
@ -62,12 +62,6 @@ GType gtk_gesture_long_press_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_3_14
GtkGesture * gtk_gesture_long_press_new (GtkWidget *widget);
GDK_AVAILABLE_IN_3_14
void gtk_gesture_long_press_set_button (GtkGestureLongPress *gesture,
guint button);
GDK_AVAILABLE_IN_3_14
guint gtk_gesture_long_press_get_button (GtkGestureLongPress *gesture);
G_END_DECLS
#endif /* __GTK_GESTURE_LONG_PRESS_H__ */