Gtk.Gesture

g GObject.Object GObject.Object Gtk.EventController Gtk.EventController GObject.Object->Gtk.EventController Gtk.Gesture Gtk.Gesture Gtk.EventController->Gtk.Gesture

Subclasses:

Gtk.GestureRotate, Gtk.GestureSingle, Gtk.GestureZoom

Methods

Inherited:

Gtk.EventController (5), GObject.Object (37)

Structs:

GObject.ObjectClass (5)

get_bounding_box ()

get_bounding_box_center ()

get_device ()

get_group ()

get_last_event (sequence)

get_last_updated_sequence ()

get_point (sequence)

get_sequence_state (sequence)

get_sequences ()

get_window ()

group (gesture)

handles_sequence (sequence)

is_active ()

is_grouped_with (other)

is_recognized ()

set_sequence_state (sequence, state)

set_state (state)

set_window (window)

ungroup ()

Virtual Methods

Inherited:

GObject.Object (7)

Properties

Inherited:

Gtk.EventController (2)

Name

Type

Flags

Short Description

n-points

int

r/w/co

Number of points needed to trigger the gesture

window

Gdk.Window

r/w

Gdk.Window to receive events about

Signals

Inherited:

GObject.Object (1)

Name

Short Description

begin

This signal is emitted when the gesture is recognized.

cancel

This signal is emitted whenever a sequence is cancelled.

end

This signal is emitted when gesture either stopped recognizing the event sequences as something to be handled (the Gtk.Gesture ::check handler returned False), or the number of touch sequences became higher or lower than Gtk.Gesture :n-points.

sequence-state-changed

This signal is emitted whenever a sequence state changes.

update

This signal is emitted whenever an event is handled while the gesture is recognized.

Fields

Inherited:

GObject.Object (1)

Class Details

class Gtk.Gesture(**kwargs)
Bases:

Gtk.EventController

Abstract:

Yes

Structure:

Gtk.GestureClass

Gtk.Gesture is the base object for gesture recognition, although this object is quite generalized to serve as a base for multi-touch gestures, it is suitable to implement single-touch and pointer-based gestures (using the special None Gdk.EventSequence value for these).

The number of touches that a Gtk.Gesture need to be recognized is controlled by the Gtk.Gesture :n-points property, if a gesture is keeping track of less or more than that number of sequences, it won’t check wether the gesture is recognized.

As soon as the gesture has the expected number of touches, the gesture will run the Gtk.Gesture ::check signal regularly on input events until the gesture is recognized, the criteria to consider a gesture as “recognized” is left to Gtk.Gesture subclasses.

A recognized gesture will then emit the following signals:

Event propagation

In order to receive events, a gesture needs to either set a propagation phase through Gtk.EventController.set_propagation_phase(), or feed those manually through Gtk.EventController.handle_event().

In the capture phase, events are propagated from the toplevel down to the target widget, and gestures that are attached to containers above the widget get a chance to interact with the event before it reaches the target.

After the capture phase, GTK+ emits the traditional Gtk.Widget ::button-press-event, Gtk.Widget ::button-release-event, Gtk.Widget ::touch-event, etc signals. Gestures with the Gtk.PropagationPhase.TARGET phase are fed events from the default Gtk.Widget ::event handlers.

In the bubble phase, events are propagated up from the target widget to the toplevel, and gestures that are attached to containers above the widget get a chance to interact with events that have not been handled yet.

States of a sequence

Whenever input interaction happens, a single event may trigger a cascade of Gtk.Gestures, both across the parents of the widget receiving the event and in parallel within an individual widget. It is a responsibility of the widgets using those gestures to set the state of touch sequences accordingly in order to enable cooperation of gestures around the Gdk.EventSequences triggering those.

Within a widget, gestures can be grouped through Gtk.Gesture.group(), grouped gestures synchronize the state of sequences, so calling Gtk.Gesture.set_sequence_state() on one will effectively propagate the state throughout the group.

By default, all sequences start out in the Gtk.EventSequenceState.NONE state, sequences in this state trigger the gesture event handler, but event propagation will continue unstopped by gestures.

If a sequence enters into the Gtk.EventSequenceState.DENIED state, the gesture group will effectively ignore the sequence, letting events go unstopped through the gesture, but the “slot” will still remain occupied while the touch is active.

If a sequence enters in the Gtk.EventSequenceState.CLAIMED state, the gesture group will grab all interaction on the sequence, by:

  • Setting the same sequence to Gtk.EventSequenceState.DENIED on every other gesture group within the widget, and every gesture on parent widgets in the propagation chain.

  • calling Gtk.Gesture ::cancel on every gesture in widgets underneath in the propagation chain.

  • Stopping event propagation after the gesture group handles the event.

Note: if a sequence is set early to Gtk.EventSequenceState.CLAIMED on Gdk.EventType.TOUCH_BEGIN/Gdk.EventType.BUTTON_PRESS (so those events are captured before reaching the event widget, this implies Gtk.PropagationPhase.CAPTURE), one similar event will emulated if the sequence changes to Gtk.EventSequenceState.DENIED. This way event coherence is preserved before event propagation is unstopped again.

Sequence states can’t be changed freely, see Gtk.Gesture.set_sequence_state() to know about the possible lifetimes of a Gdk.EventSequence.

Touchpad gestures

On the platforms that support it, Gtk.Gesture will handle transparently touchpad gesture events. The only precautions users of Gtk.Gesture should do to enable this support are:

get_bounding_box()[source]
Returns:

True if there are active touches, False otherwise

rect:

bounding box containing all active touches.

Return type:

(bool, rect: Gdk.Rectangle)

If there are touch sequences being currently handled by self, this function returns True and fills in rect with the bounding box containing all active touches. Otherwise, False will be returned.

Note: This function will yield unexpected results on touchpad gestures. Since there is no correlation between physical and pixel distances, these will look as if constrained in an infinitely small area, rect width and height will thus be 0 regardless of the number of touchpoints.

New in version 3.14.

get_bounding_box_center()[source]
Returns:

False if no active touches are present, True otherwise

x:

X coordinate for the bounding box center

y:

Y coordinate for the bounding box center

Return type:

(bool, x: float, y: float)

If there are touch sequences being currently handled by self, this function returns True and fills in x and y with the center of the bounding box containing all active touches. Otherwise, False will be returned.

New in version 3.14.

get_device()[source]
Returns:

a Gdk.Device, or None

Return type:

Gdk.Device or None

Returns the master Gdk.Device that is currently operating on self, or None if the gesture is not being interacted.

New in version 3.14.

get_group()[source]
Returns:

The list of Gtk.Gestures, free with g_list_free()

Return type:

[Gtk.Gesture]

Returns all gestures in the group of self

New in version 3.14.

get_last_event(sequence)[source]
Parameters:

sequence (Gdk.EventSequence or None) – a Gdk.EventSequence

Returns:

The last event from sequence

Return type:

Gdk.Event or None

Returns the last event that was processed for sequence.

Note that the returned pointer is only valid as long as the sequence is still interpreted by the self. If in doubt, you should make a copy of the event.

get_last_updated_sequence()[source]
Returns:

The last updated sequence

Return type:

Gdk.EventSequence or None

Returns the Gdk.EventSequence that was last updated on self.

New in version 3.14.

get_point(sequence)[source]
Parameters:

sequence (Gdk.EventSequence or None) – a Gdk.EventSequence, or None for pointer events

Returns:

True if sequence is currently interpreted

x:

return location for X axis of the sequence coordinates

y:

return location for Y axis of the sequence coordinates

Return type:

(bool, x: float, y: float)

If sequence is currently being interpreted by self, this function returns True and fills in x and y with the last coordinates stored for that event sequence. The coordinates are always relative to the widget allocation.

New in version 3.14.

get_sequence_state(sequence)[source]
Parameters:

sequence (Gdk.EventSequence) – a Gdk.EventSequence

Returns:

The sequence state in self

Return type:

Gtk.EventSequenceState

Returns the sequence state, as seen by self.

New in version 3.14.

get_sequences()[source]
Returns:

A list of Gdk.EventSequences, the list elements are owned by GTK+ and must not be freed or modified, the list itself must be deleted through g_list_free()

Return type:

[Gdk.EventSequence]

Returns the list of Gdk.EventSequences currently being interpreted by self.

New in version 3.14.

get_window()[source]
Returns:

the user defined window, or None if none

Return type:

Gdk.Window or None

Returns the user-defined window that receives the events handled by self. See Gtk.Gesture.set_window() for more information.

New in version 3.14.

group(gesture)[source]
Parameters:

gesture (Gtk.Gesture) – a Gtk.Gesture

Adds gesture to the same group than self. Gestures are by default isolated in their own groups.

When gestures are grouped, the state of Gdk.EventSequences is kept in sync for all of those, so calling Gtk.Gesture.set_sequence_state(), on one will transfer the same value to the others.

Groups also perform an “implicit grabbing” of sequences, if a Gdk.EventSequence state is set to Gtk.EventSequenceState.CLAIMED on one group, every other gesture group attached to the same Gtk.Widget will switch the state for that sequence to Gtk.EventSequenceState.DENIED.

New in version 3.14.

handles_sequence(sequence)[source]
Parameters:

sequence (Gdk.EventSequence or None) – a Gdk.EventSequence or None

Returns:

True if self is handling sequence, False otherwise

Return type:

bool

Returns True if self is currently handling events corresponding to sequence.

New in version 3.14.

is_active()[source]
Returns:

True if gesture is active

Return type:

bool

Returns True if the gesture is currently active. A gesture is active meanwhile there are touch sequences interacting with it.

New in version 3.14.

is_grouped_with(other)[source]
Parameters:

other (Gtk.Gesture) – another Gtk.Gesture

Returns:

whether the gestures are grouped

Return type:

bool

Returns True if both gestures pertain to the same group.

New in version 3.14.

is_recognized()[source]
Returns:

True if gesture is recognized

Return type:

bool

Returns True if the gesture is currently recognized. A gesture is recognized if there are as many interacting touch sequences as required by self, and Gtk.Gesture ::check returned True for the sequences being currently interpreted.

New in version 3.14.

set_sequence_state(sequence, state)[source]
Parameters:
Returns:

True if sequence is handled by self, and the state is changed successfully

Return type:

bool

Sets the state of sequence in self. Sequences start in state Gtk.EventSequenceState.NONE, and whenever they change state, they can never go back to that state. Likewise, sequences in state Gtk.EventSequenceState.DENIED cannot turn back to a not denied state. With these rules, the lifetime of an event sequence is constrained to the next four:

  • None

  • None → Denied

  • None → Claimed

  • None → Claimed → Denied

Note: Due to event handling ordering, it may be unsafe to set the state on another gesture within a Gtk.Gesture ::begin signal handler, as the callback might be executed before the other gesture knows about the sequence. A safe way to perform this could be:

static void
first_gesture_begin_cb (GtkGesture       *first_gesture,
                        GdkEventSequence *sequence,
                        gpointer          user_data)
{
  gtk_gesture_set_sequence_state (first_gesture, sequence, GTK_EVENT_SEQUENCE_CLAIMED);
  gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
}

static void
second_gesture_begin_cb (GtkGesture       *second_gesture,
                         GdkEventSequence *sequence,
                         gpointer          user_data)
{
  if (gtk_gesture_get_sequence_state (first_gesture, sequence) == GTK_EVENT_SEQUENCE_CLAIMED)
    gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
}

If both gestures are in the same group, just set the state on the gesture emitting the event, the sequence will be already be initialized to the group’s global state when the second gesture processes the event.

New in version 3.14.

set_state(state)[source]
Parameters:

state (Gtk.EventSequenceState) – the sequence state

Returns:

True if the state of at least one sequence was changed successfully

Return type:

bool

Sets the state of all sequences that self is currently interacting with. See Gtk.Gesture.set_sequence_state() for more details on sequence states.

New in version 3.14.

set_window(window)[source]
Parameters:

window (Gdk.Window or None) – a Gdk.Window, or None

Sets a specific window to receive events about, so self will effectively handle only events targeting window, or a child of it. window must pertain to Gtk.EventController.get_widget().

New in version 3.14.

ungroup()[source]

Separates self into an isolated group.

New in version 3.14.

Signal Details

Gtk.Gesture.signals.begin(gesture, sequence)
Signal Name:

begin

Flags:

RUN_LAST

Parameters:

This signal is emitted when the gesture is recognized. This means the number of touch sequences matches Gtk.Gesture :n-points, and the Gtk.Gesture ::check handler(s) returned True.

Note: These conditions may also happen when an extra touch (eg. a third touch on a 2-touches gesture) is lifted, in that situation sequence won’t pertain to the current set of active touches, so don’t rely on this being true.

New in version 3.14.

Gtk.Gesture.signals.cancel(gesture, sequence)
Signal Name:

cancel

Flags:

RUN_LAST

Parameters:

This signal is emitted whenever a sequence is cancelled. This usually happens on active touches when Gtk.EventController.reset() is called on gesture (manually, due to grabs…), or the individual sequence was claimed by parent widgets’ controllers (see Gtk.Gesture.set_sequence_state()).

gesture must forget everything about sequence as a reaction to this signal.

New in version 3.14.

Gtk.Gesture.signals.end(gesture, sequence)
Signal Name:

end

Flags:

RUN_LAST

Parameters:

This signal is emitted when gesture either stopped recognizing the event sequences as something to be handled (the Gtk.Gesture ::check handler returned False), or the number of touch sequences became higher or lower than Gtk.Gesture :n-points.

Note: sequence might not pertain to the group of sequences that were previously triggering recognition on gesture (ie. a just pressed touch sequence that exceeds Gtk.Gesture :n-points). This situation may be detected by checking through Gtk.Gesture.handles_sequence().

New in version 3.14.

Gtk.Gesture.signals.sequence_state_changed(gesture, sequence, state)
Signal Name:

sequence-state-changed

Flags:

RUN_LAST

Parameters:

This signal is emitted whenever a sequence state changes. See Gtk.Gesture.set_sequence_state() to know more about the expectable sequence lifetimes.

New in version 3.14.

Gtk.Gesture.signals.update(gesture, sequence)
Signal Name:

update

Flags:

RUN_LAST

Parameters:

This signal is emitted whenever an event is handled while the gesture is recognized. sequence is guaranteed to pertain to the set of active touches.

New in version 3.14.

Property Details

Gtk.Gesture.props.n_points
Name:

n-points

Type:

int

Default Value:

1

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The number of touch points that trigger recognition on this gesture,

New in version 3.14.

Gtk.Gesture.props.window
Name:

window

Type:

Gdk.Window

Default Value:

None

Flags:

READABLE, WRITABLE

If non-None, the gesture will only listen for events that happen on this Gdk.Window, or a child of it.

New in version 3.14.