gdk4/auto/event.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{
6 AxisUse, Device, DeviceTool, Display, EventSequence, EventType, ModifierType, Seat, Surface,
7 TimeCoord, ffi,
8};
9use glib::{prelude::*, translate::*};
10
11glib::wrapper! {
12 /// Represents windowing system events.
13 ///
14 /// In GTK applications the events are handled automatically by toplevel
15 /// widgets and passed on to the event controllers of appropriate widgets,
16 /// so using [`Event`][crate::Event] and its related API is rarely needed.
17 ///
18 /// [`Event`][crate::Event] structs are immutable.
19 ///
20 /// This is an Abstract Base Class, you cannot instantiate it.
21 #[doc(alias = "GdkEvent")]
22 pub struct Event(Shared<ffi::GdkEvent>);
23
24 match fn {
25 ref => |ptr| ffi::gdk_event_ref(ptr),
26 unref => |ptr| ffi::gdk_event_unref(ptr),
27 }
28}
29
30impl StaticType for Event {
31 fn static_type() -> glib::Type {
32 unsafe { from_glib(ffi::gdk_event_get_type()) }
33 }
34}
35
36impl Event {
37 pub const NONE: Option<&'static Event> = None;
38
39 /// Extracts all axis values from an event.
40 ///
41 /// To find out which axes are used, use [`DeviceTool::axes()`][crate::DeviceTool::axes()]
42 /// on the device tool returned by [`device_tool()`][Self::device_tool()].
43 ///
44 /// # Returns
45 ///
46 /// [`true`] on success, otherwise [`false`]
47 ///
48 /// ## `axes`
49 /// the array of values for all axes
50 #[doc(alias = "gdk_event_get_axes")]
51 #[doc(alias = "get_axes")]
52 pub fn axes(&self) -> Option<Vec<f64>> {
53 unsafe {
54 let mut axes = std::ptr::null_mut();
55 let mut n_axes = std::mem::MaybeUninit::uninit();
56 let ret = from_glib(ffi::gdk_event_get_axes(
57 self.as_ref().to_glib_none().0,
58 &mut axes,
59 n_axes.as_mut_ptr(),
60 ));
61 if ret {
62 Some(FromGlibContainer::from_glib_none_num(
63 axes,
64 n_axes.assume_init() as _,
65 ))
66 } else {
67 None
68 }
69 }
70 }
71
72 /// Extract the axis value for a particular axis use from
73 /// an event structure.
74 ///
75 /// To find out which axes are used, use [`DeviceTool::axes()`][crate::DeviceTool::axes()]
76 /// on the device tool returned by [`device_tool()`][Self::device_tool()].
77 /// ## `axis_use`
78 /// the axis use to look for
79 ///
80 /// # Returns
81 ///
82 /// [`true`] if the specified axis was found, otherwise [`false`]
83 ///
84 /// ## `value`
85 /// location to store the value found
86 #[doc(alias = "gdk_event_get_axis")]
87 #[doc(alias = "get_axis")]
88 pub fn axis(&self, axis_use: AxisUse) -> Option<f64> {
89 unsafe {
90 let mut value = std::mem::MaybeUninit::uninit();
91 let ret = from_glib(ffi::gdk_event_get_axis(
92 self.as_ref().to_glib_none().0,
93 axis_use.into_glib(),
94 value.as_mut_ptr(),
95 ));
96 if ret { Some(value.assume_init()) } else { None }
97 }
98 }
99
100 /// Returns the device of an event.
101 ///
102 /// # Returns
103 ///
104 /// a [`Device`][crate::Device]
105 #[doc(alias = "gdk_event_get_device")]
106 #[doc(alias = "get_device")]
107 pub fn device(&self) -> Option<Device> {
108 unsafe { from_glib_none(ffi::gdk_event_get_device(self.as_ref().to_glib_none().0)) }
109 }
110
111 /// Returns a [`DeviceTool`][crate::DeviceTool] representing the tool that
112 /// caused the event.
113 ///
114 /// If the was not generated by a device that supports
115 /// different tools (such as a tablet), this function will
116 /// return [`None`].
117 ///
118 /// Note: the [`DeviceTool`][crate::DeviceTool] will be constant during
119 /// the application lifetime, if settings must be stored
120 /// persistently across runs, see [`DeviceTool::serial()`][crate::DeviceTool::serial()].
121 ///
122 /// # Returns
123 ///
124 /// The current device tool
125 #[doc(alias = "gdk_event_get_device_tool")]
126 #[doc(alias = "get_device_tool")]
127 pub fn device_tool(&self) -> Option<DeviceTool> {
128 unsafe {
129 from_glib_none(ffi::gdk_event_get_device_tool(
130 self.as_ref().to_glib_none().0,
131 ))
132 }
133 }
134
135 /// Retrieves the display associated to the @self.
136 ///
137 /// # Returns
138 ///
139 /// a [`Display`][crate::Display]
140 #[doc(alias = "gdk_event_get_display")]
141 #[doc(alias = "get_display")]
142 pub fn display(&self) -> Option<Display> {
143 unsafe { from_glib_none(ffi::gdk_event_get_display(self.as_ref().to_glib_none().0)) }
144 }
145
146 /// Returns the event sequence to which the event belongs.
147 ///
148 /// Related touch events are connected in a sequence. Other
149 /// events typically don't have event sequence information.
150 ///
151 /// # Returns
152 ///
153 /// the event sequence that the event belongs to
154 #[doc(alias = "gdk_event_get_event_sequence")]
155 #[doc(alias = "get_event_sequence")]
156 pub fn event_sequence(&self) -> EventSequence {
157 unsafe {
158 from_glib_none(ffi::gdk_event_get_event_sequence(
159 self.as_ref().to_glib_none().0,
160 ))
161 }
162 }
163
164 /// Retrieves the type of the event.
165 ///
166 /// # Returns
167 ///
168 /// a [`Event`][crate::Event]Type
169 #[doc(alias = "gdk_event_get_event_type")]
170 #[doc(alias = "get_event_type")]
171 pub fn event_type(&self) -> EventType {
172 unsafe {
173 from_glib(ffi::gdk_event_get_event_type(
174 self.as_ref().to_glib_none().0,
175 ))
176 }
177 }
178
179 /// Retrieves the history of the device that @self is for, as a list of
180 /// time and coordinates.
181 ///
182 /// The history includes positions that are not delivered as separate events
183 /// to the application because they occurred in the same frame as @self.
184 ///
185 /// Note that only motion and scroll events record history, and motion
186 /// events do it only if one of the mouse buttons is down, or the device
187 /// has a tool.
188 ///
189 /// # Returns
190 ///
191 /// an
192 /// array of time and coordinates
193 #[doc(alias = "gdk_event_get_history")]
194 #[doc(alias = "get_history")]
195 pub fn history(&self) -> Vec<TimeCoord> {
196 unsafe {
197 let mut out_n_coords = std::mem::MaybeUninit::uninit();
198 let ret = FromGlibContainer::from_glib_container_num(
199 ffi::gdk_event_get_history(
200 self.as_ref().to_glib_none().0,
201 out_n_coords.as_mut_ptr(),
202 ),
203 out_n_coords.assume_init() as _,
204 );
205 ret
206 }
207 }
208
209 /// Returns the modifier state field of an event.
210 ///
211 /// # Returns
212 ///
213 /// the modifier state of @self
214 #[doc(alias = "gdk_event_get_modifier_state")]
215 #[doc(alias = "get_modifier_state")]
216 pub fn modifier_state(&self) -> ModifierType {
217 unsafe {
218 from_glib(ffi::gdk_event_get_modifier_state(
219 self.as_ref().to_glib_none().0,
220 ))
221 }
222 }
223
224 /// Returns whether this event is an 'emulated' pointer event.
225 ///
226 /// Emulated pointer events typically originate from a touch events.
227 ///
228 /// # Returns
229 ///
230 /// [`true`] if this event is emulated
231 #[doc(alias = "gdk_event_get_pointer_emulated")]
232 #[doc(alias = "get_pointer_emulated")]
233 pub fn is_pointer_emulated(&self) -> bool {
234 unsafe {
235 from_glib(ffi::gdk_event_get_pointer_emulated(
236 self.as_ref().to_glib_none().0,
237 ))
238 }
239 }
240
241 /// Extract the event surface relative x/y coordinates from an event.
242 ///
243 /// This position is in [surface coordinates](coordinates.html).
244 ///
245 /// # Returns
246 ///
247 /// whether the positions were set
248 ///
249 /// ## `x`
250 /// location to put event surface x coordinate
251 ///
252 /// ## `y`
253 /// location to put event surface y coordinate
254 #[doc(alias = "gdk_event_get_position")]
255 #[doc(alias = "get_position")]
256 pub fn position(&self) -> Option<(f64, f64)> {
257 unsafe {
258 let mut x = std::mem::MaybeUninit::uninit();
259 let mut y = std::mem::MaybeUninit::uninit();
260 let ret = from_glib(ffi::gdk_event_get_position(
261 self.as_ref().to_glib_none().0,
262 x.as_mut_ptr(),
263 y.as_mut_ptr(),
264 ));
265 if ret {
266 Some((x.assume_init(), y.assume_init()))
267 } else {
268 None
269 }
270 }
271 }
272
273 /// Returns the seat that originated the event.
274 ///
275 /// # Returns
276 ///
277 /// a [`Seat`][crate::Seat].
278 #[doc(alias = "gdk_event_get_seat")]
279 #[doc(alias = "get_seat")]
280 pub fn seat(&self) -> Option<Seat> {
281 unsafe { from_glib_none(ffi::gdk_event_get_seat(self.as_ref().to_glib_none().0)) }
282 }
283
284 /// Extracts the surface associated with an event.
285 ///
286 /// # Returns
287 ///
288 /// The [`Surface`][crate::Surface] associated with the event
289 #[doc(alias = "gdk_event_get_surface")]
290 #[doc(alias = "get_surface")]
291 pub fn surface(&self) -> Option<Surface> {
292 unsafe { from_glib_none(ffi::gdk_event_get_surface(self.as_ref().to_glib_none().0)) }
293 }
294
295 /// Returns the timestamp of @self.
296 ///
297 /// Not all events have timestamps. In that case, this function
298 /// returns `GDK_CURRENT_TIME`.
299 ///
300 /// # Returns
301 ///
302 /// timestamp field from @self
303 #[doc(alias = "gdk_event_get_time")]
304 #[doc(alias = "get_time")]
305 pub fn time(&self) -> u32 {
306 unsafe { ffi::gdk_event_get_time(self.as_ref().to_glib_none().0) }
307 }
308
309 /// Returns whether a [`Event`][crate::Event] should trigger a context menu,
310 /// according to platform conventions.
311 ///
312 /// The right mouse button typically triggers context menus.
313 /// On macOS, Control+left mouse button also triggers.
314 ///
315 /// This function should always be used instead of simply checking for
316 ///
317 /// **⚠️ The following code is in c ⚠️**
318 ///
319 /// ```c
320 /// event->button == GDK_BUTTON_SECONDARY
321 /// ```
322 ///
323 /// # Returns
324 ///
325 /// [`true`] if the event should trigger a context menu.
326 #[doc(alias = "gdk_event_triggers_context_menu")]
327 pub fn triggers_context_menu(&self) -> bool {
328 unsafe {
329 from_glib(ffi::gdk_event_triggers_context_menu(
330 self.as_ref().to_glib_none().0,
331 ))
332 }
333 }
334}