2014-05-20 14:10:31 +00:00
|
|
|
<?xml version="1.0"?>
|
|
|
|
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
|
|
|
|
]>
|
|
|
|
<refentry id="chap-input-handling">
|
|
|
|
<refmeta>
|
2019-05-03 04:22:07 +00:00
|
|
|
<refentrytitle>The GTK Input Model</refentrytitle>
|
2014-05-20 14:10:31 +00:00
|
|
|
<manvolnum>3</manvolnum>
|
|
|
|
<refmiscinfo>GTK Library</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
2019-05-03 04:22:07 +00:00
|
|
|
<refname>The GTK Input Model</refname>
|
2014-05-20 14:10:31 +00:00
|
|
|
<refpurpose>
|
2019-05-03 04:22:07 +00:00
|
|
|
input and event handling in detail
|
2014-05-20 14:10:31 +00:00
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
|
|
|
|
<refsect1 id="input-overview">
|
2019-02-05 09:14:31 +00:00
|
|
|
<title>Overview of GTK input and event handling</title>
|
2014-05-20 14:10:31 +00:00
|
|
|
|
2014-05-22 19:16:09 +00:00
|
|
|
<para>
|
2019-02-05 09:14:31 +00:00
|
|
|
This chapter describes in detail how GTK handles input. If you are interested
|
2014-05-22 19:16:09 +00:00
|
|
|
in what happens to translate a key press or mouse motion of the users into a
|
2019-02-05 09:14:31 +00:00
|
|
|
change of a GTK widget, you should read this chapter. This knowledge will also
|
2014-05-22 19:16:09 +00:00
|
|
|
be useful if you decide to implement your own widgets.
|
|
|
|
</para>
|
|
|
|
|
2014-05-20 14:10:31 +00:00
|
|
|
<refsect2>
|
|
|
|
<title>Devices and events</title>
|
|
|
|
|
|
|
|
<!-- input devices: master/slave, keyboard/pointer/touch -->
|
2014-05-22 19:16:09 +00:00
|
|
|
<para>
|
|
|
|
The most basic input devices that every computer user has interacted with are
|
2019-02-05 09:14:31 +00:00
|
|
|
keyboards and mice; beyond these, GTK supports touchpads, touchscreens and
|
|
|
|
more exotic input devices such as graphics tablets. Inside GTK, every such
|
2014-05-22 19:16:09 +00:00
|
|
|
input device is represented by a #GdkDevice object.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-02-05 09:14:31 +00:00
|
|
|
To simplify dealing with the variability between these input devices, GTK
|
2014-05-22 19:16:09 +00:00
|
|
|
has a concept of master and slave devices. The concrete physical devices that
|
|
|
|
have many different characteristics (mice may have 2 or 3 or 8 buttons,
|
|
|
|
keyboards have different layouts and may or may not have a separate number
|
|
|
|
block, etc) are represented as slave devices. Each slave device is
|
|
|
|
associated with a virtual master device. Master devices always come in
|
|
|
|
pointer/keyboard pairs - you can think of such a pair as a 'seat'.
|
|
|
|
</para>
|
|
|
|
<para>
|
2019-02-05 09:14:31 +00:00
|
|
|
GTK widgets generally deal with the master devices, and thus can be used
|
2014-05-22 19:16:09 +00:00
|
|
|
with any pointing device or keyboard.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
When a user interacts with an input device (e.g. moves a mouse or presses
|
2019-02-05 09:14:31 +00:00
|
|
|
a key on the keyboard), GTK receives events from the windowing system.
|
2019-05-03 04:22:07 +00:00
|
|
|
These are typically directed at a specific surface - for pointer events,
|
|
|
|
the surface under the pointer (grabs complicate this), for keyboard events,
|
|
|
|
the surface with the keyboard focus.
|
2014-05-22 19:16:09 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2014-05-22 19:45:31 +00:00
|
|
|
GDK translates these raw windowing system events into #GdkEvents.
|
2020-05-01 13:26:06 +00:00
|
|
|
Typical input events are button clicks, pointer motion, key presses
|
|
|
|
or touch events. These are all represented as #GdkEvents, but you can
|
|
|
|
differentiate between different events by looking at their type, using
|
2020-03-24 20:11:07 +00:00
|
|
|
gdk_event_get_event_type().
|
2014-05-22 19:45:31 +00:00
|
|
|
</para>
|
2020-04-15 12:07:48 +00:00
|
|
|
<para>
|
|
|
|
Some events, such as touch events or button press-release pairs,
|
|
|
|
are connected in to each other in an “event sequence” that
|
|
|
|
univocally identifies events that are related to the same
|
|
|
|
interaction.
|
|
|
|
</para>
|
2014-05-28 13:50:06 +00:00
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
When GTK creates a GdkSurface, it connects to the #GdkSurface::event
|
|
|
|
signal on it, which receives all of these input events. Surfaces have
|
2019-05-03 04:22:07 +00:00
|
|
|
have signals and properties, e.g. to deal with window management
|
|
|
|
related events.
|
2014-05-22 19:16:09 +00:00
|
|
|
</para>
|
2014-05-20 14:10:31 +00:00
|
|
|
</refsect2>
|
|
|
|
|
2015-02-18 11:13:28 +00:00
|
|
|
<refsect2 id="event-propagation">
|
2014-05-20 14:10:31 +00:00
|
|
|
<title>Event propagation</title>
|
|
|
|
|
2014-05-24 05:56:44 +00:00
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
The function which initially receives input events on the GTK
|
2020-02-09 15:38:01 +00:00
|
|
|
side is responsible for a number of tasks.
|
2014-05-24 05:56:44 +00:00
|
|
|
</para>
|
2020-02-09 15:38:01 +00:00
|
|
|
<orderedlist>
|
|
|
|
<listitem><para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Find the widget which got the event.
|
2020-02-09 15:38:01 +00:00
|
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Generate crossing (i.e. enter and leave) events when the focus or hover
|
|
|
|
location change from one widget to another.
|
2020-02-09 15:38:01 +00:00
|
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
2020-05-01 13:26:06 +00:00
|
|
|
The event is sent to widgets.
|
2020-02-09 15:38:01 +00:00
|
|
|
</para></listitem>
|
|
|
|
</orderedlist>
|
2014-05-24 05:56:44 +00:00
|
|
|
|
2015-02-18 11:13:28 +00:00
|
|
|
<para>
|
2020-02-09 16:00:53 +00:00
|
|
|
An event is propagated down and up the widget hierarchy in three phases
|
2019-05-03 04:22:07 +00:00
|
|
|
(see #GtkPropagationPhase) towards a target widget.
|
2015-02-18 11:13:28 +00:00
|
|
|
</para>
|
|
|
|
|
2020-05-01 13:26:06 +00:00
|
|
|
<informalfigure>
|
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
|
|
|
<imagedata fileref="capture-bubble.png" format="PNG"/>
|
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
|
|
|
</informalfigure>
|
|
|
|
|
2015-02-18 11:13:28 +00:00
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
For key events, the top-level window gets a first shot at activating
|
|
|
|
mnemonics and accelerators. If that does not consume the events,
|
|
|
|
the target widget for event propagation is window's current focus
|
|
|
|
widget (see gtk_window_get_focus()).
|
2015-02-18 11:13:28 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
For pointer events, the target widget is determined by picking
|
|
|
|
the widget at the events coordinates (see gtk_window_pick()).
|
2014-05-24 05:56:44 +00:00
|
|
|
</para>
|
|
|
|
|
2019-05-03 04:22:07 +00:00
|
|
|
<para>In the first phase (the “capture” phase) the event is
|
|
|
|
delivered to each widget from the top-most (the top-level
|
|
|
|
#GtkWindow or grab widget) down to the target #GtkWidget.
|
|
|
|
<link linkend="event-controllers-and-gestures">Event
|
|
|
|
controllers</link> that are attached with %GTK_PHASE_CAPTURE
|
|
|
|
get a chance to react to the event.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
2014-05-24 05:56:44 +00:00
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
After the “capture” phase, the widget that was intended to be the
|
|
|
|
destination of the event will run event controllers attached to
|
|
|
|
it with %GTK_PHASE_TARGET. This is known as the “target” phase,
|
|
|
|
and only happens on that widget.
|
2014-05-24 05:56:44 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
In the last phase (the “bubble” phase), the event is delivered
|
|
|
|
to each widget from the target to the top-most, and event
|
|
|
|
controllers attached with %GTK_PHASE_BUBBLE are run.
|
2014-05-24 05:56:44 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Events are not delivered to a widget which is insensitive or
|
|
|
|
unmapped.
|
2015-02-18 11:13:28 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Any time during the propagation phase, a controller may indicate
|
|
|
|
that a received event was consumed and propagation should
|
|
|
|
therefore be stopped. If gestures are used, this may happen
|
|
|
|
when the gesture claims the event touch sequence (or the
|
|
|
|
pointer events) for its own. See the “gesture states” section
|
|
|
|
below to learn more about gestures and sequences.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
</refsect2>
|
|
|
|
|
|
|
|
<refsect2>
|
2020-03-24 20:11:07 +00:00
|
|
|
<title>Keyboard input</title>
|
2014-05-28 13:50:06 +00:00
|
|
|
|
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Every #GtkWindow maintains a single focus location (in the
|
|
|
|
#GtkWindow:focus-widget property). The focus widget is the target
|
|
|
|
widget for key events sent to the window. Only widgets which have
|
|
|
|
#GtkWidget:can-focus set to %TRUE can become the focus. Typically
|
|
|
|
these are input controls such as entries or text fields, but e.g.
|
|
|
|
buttons can take the focus too.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Input widgets can be given the focus by clicking on them, but focus
|
|
|
|
can also be moved around with certain key events (this is known as
|
|
|
|
“keyboard navigation”). GTK reserves the Tab key to move the focus
|
|
|
|
to the next location, and Shift-Tab to move it back to the previous
|
|
|
|
one. In addition many containers allow “directional navigation” with
|
|
|
|
the arrow keys.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Many widgets can be “activated” to trigger and action. E.g., you can
|
|
|
|
activate a button or switch by clicking on them, but you can also
|
|
|
|
activate them with the keyboard, by using the Enter or Space keys.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Apart from keyboard navigation, activation and directly typing into
|
|
|
|
entries or text views, GTK widgets can use key events for activating
|
|
|
|
“shortcuts”. Shortcuts generally act as a quick way to move the focus
|
|
|
|
around or to activate a widget that does not currently have the focus.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
2014-05-20 14:10:31 +00:00
|
|
|
|
2019-05-03 04:22:07 +00:00
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
GTK has traditionally supported different kinds of shortcuts:
|
|
|
|
<variablelist>
|
2020-05-01 13:26:06 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term>Accelerators</term>
|
|
|
|
<listitem><para>
|
|
|
|
Accelerators are any other shortcuts that can be activated regardless
|
|
|
|
of where the focus is, and typically trigger global actions, such as
|
|
|
|
Ctrl-Q to quit an application.
|
|
|
|
</para></listitem>
|
|
|
|
</varlistentry>
|
2020-03-24 20:11:07 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term>Mnmemonics</term>
|
|
|
|
<listitem><para>
|
|
|
|
Mnemonics are usually triggered using Alt as a modifier for a letter.
|
|
|
|
They are used in places where a label is associated with a control,
|
|
|
|
and are indicated by underlining the letter in the label. As a special
|
|
|
|
case, inside menus (i.e. inside #GtkPopoverMenu), mnemonics can be
|
|
|
|
trigered without the modifier.
|
|
|
|
</para></listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>Key bindings</term>
|
|
|
|
<listitem><para>
|
|
|
|
Key bindings are specific to individual widgets, such as Ctrl-C or
|
|
|
|
Ctrl-V in an entry copy to or paste from the clipboard. They are only
|
|
|
|
triggered when the widget has focus.
|
|
|
|
</para></listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2019-05-03 04:22:07 +00:00
|
|
|
</para>
|
2020-05-01 13:26:06 +00:00
|
|
|
<para>
|
|
|
|
GTK traditionally handles accelerators and mnemonics in a global scope,
|
|
|
|
during the capture phase, and key bindings locally, during the target phase.
|
|
|
|
</para>
|
2019-05-03 04:22:07 +00:00
|
|
|
<para>
|
2020-03-24 20:11:07 +00:00
|
|
|
Under the hood, all shortcuts are represented as instances of #GtkShortcut,
|
|
|
|
and they are managed by #GtkShortcutController.
|
2019-05-03 04:22:07 +00:00
|
|
|
</para>
|
2014-05-20 14:10:31 +00:00
|
|
|
</refsect2>
|
|
|
|
|
2015-02-18 11:13:28 +00:00
|
|
|
<refsect2 id="event-controllers-and-gestures">
|
2014-05-28 13:50:06 +00:00
|
|
|
<title>Event controllers and gestures</title>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Event controllers are standalone objects that can perform
|
|
|
|
specific actions upon received #GdkEvents. These are tied
|
|
|
|
to a #GtkWidget, and can be told of the event propagation
|
|
|
|
phase at which they will manage the events.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
2014-05-20 14:10:31 +00:00
|
|
|
|
2014-05-28 13:50:06 +00:00
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Gestures are a set of specific controllers that are prepared
|
|
|
|
to handle pointer and/or touch events, each gesture
|
|
|
|
implementation attempts to recognize specific actions out the
|
|
|
|
received events, notifying of the state/progress accordingly to
|
|
|
|
let the widget react to those. On multi-touch gestures, every
|
|
|
|
interacting touch sequence will be tracked independently.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Since gestures are “simple” units, it is not uncommon to tie
|
|
|
|
several together to perform higher level actions, grouped
|
|
|
|
gestures handle the same event sequences simultaneously, and
|
|
|
|
those sequences share a same state across all grouped
|
2014-05-28 13:50:06 +00:00
|
|
|
gestures. Some examples of grouping may be:
|
|
|
|
|
|
|
|
<simplelist>
|
|
|
|
<member>
|
2019-05-03 04:22:07 +00:00
|
|
|
A “drag” and a “swipe” gestures may want grouping.
|
|
|
|
The former will report events as the dragging happens,
|
|
|
|
the latter will tell the swipe X/Y velocities only after
|
|
|
|
recognition has finished.
|
2014-05-28 13:50:06 +00:00
|
|
|
</member>
|
|
|
|
<member>
|
2019-05-03 04:22:07 +00:00
|
|
|
Grouping a “drag” gesture with a “pan” gesture will only
|
|
|
|
effectively allow dragging in the panning orientation, as
|
|
|
|
both gestures share state.
|
2014-05-28 13:50:06 +00:00
|
|
|
</member>
|
|
|
|
<member>
|
2019-05-03 04:22:07 +00:00
|
|
|
If “press” and “long press” are wanted simultaneously,
|
|
|
|
those would need grouping.
|
2014-05-28 13:50:06 +00:00
|
|
|
</member>
|
|
|
|
</simplelist>
|
|
|
|
</para>
|
2020-03-24 20:11:07 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Shortcuts are handled by #GtkShortcutController, which is
|
|
|
|
a complex event handler that can either activate shortcuts
|
|
|
|
itself, or propagate them to another controller, depending
|
|
|
|
on its #GtkShortcutController:scope.
|
|
|
|
</para>
|
2014-05-28 13:50:06 +00:00
|
|
|
</refsect2>
|
|
|
|
|
|
|
|
<refsect2>
|
|
|
|
<title>Gesture states</title>
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Gestures have a notion of “state” for each individual touch
|
|
|
|
sequence. When events from a touch sequence are first received,
|
|
|
|
the touch sequence will have “none” state, this means the touch
|
|
|
|
sequence is being handled by the gesture to possibly trigger
|
2014-05-28 13:50:06 +00:00
|
|
|
actions, but the event propagation will not be stopped.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
When the gesture enters recognition, or at a later point in time,
|
|
|
|
the widget may choose to claim the touch sequences (individually
|
|
|
|
or as a group), hence stopping event propagation after the event
|
|
|
|
is run through every gesture in that widget and propagation phase.
|
|
|
|
Anytime this happens, the touch sequences are cancelled downwards
|
|
|
|
the propagation chain, to let these know that no further events
|
|
|
|
will be sent.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Alternatively, or at a later point in time, the widget may choose
|
|
|
|
to deny the touch sequences, thus letting those go through again
|
|
|
|
in event propagation. When this happens in the capture phase, and
|
|
|
|
if there are no other claiming gestures in the widget,
|
2015-02-18 11:33:36 +00:00
|
|
|
a %GDK_TOUCH_BEGIN/%GDK_BUTTON_PRESS event will be emulated and
|
2014-05-28 13:50:06 +00:00
|
|
|
propagated downwards, in order to preserve consistency.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-05-03 04:22:07 +00:00
|
|
|
Grouped gestures always share the same state for a given touch
|
|
|
|
sequence, so setting the state on one does transfer the state to
|
|
|
|
the others. They also are mutually exclusive, within a widget
|
|
|
|
there may be only one gesture group claiming a given sequence.
|
|
|
|
If another gesture group claims later that same sequence, the
|
|
|
|
first group will deny the sequence.
|
2014-05-28 13:50:06 +00:00
|
|
|
</para>
|
2014-05-20 14:10:31 +00:00
|
|
|
</refsect2>
|
|
|
|
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|