Skip to main content

gdk/
event_button.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use glib::translate::*;
5
6/// Used for button press and button release events. The
7/// `type` field will be one of [`EventType::ButtonPress`][crate::EventType::ButtonPress],
8/// [`EventType::_2buttonPress`][crate::EventType::_2buttonPress], [`EventType::_3buttonPress`][crate::EventType::_3buttonPress] or [`EventType::ButtonRelease`][crate::EventType::ButtonRelease],
9///
10/// Double and triple-clicks result in a sequence of events being received.
11/// For double-clicks the order of events will be:
12///
13/// - [`EventType::ButtonPress`][crate::EventType::ButtonPress]
14/// - [`EventType::ButtonRelease`][crate::EventType::ButtonRelease]
15/// - [`EventType::ButtonPress`][crate::EventType::ButtonPress]
16/// - [`EventType::_2buttonPress`][crate::EventType::_2buttonPress]
17/// - [`EventType::ButtonRelease`][crate::EventType::ButtonRelease]
18///
19/// Note that the first click is received just like a normal
20/// button press, while the second click results in a [`EventType::_2buttonPress`][crate::EventType::_2buttonPress]
21/// being received just after the [`EventType::ButtonPress`][crate::EventType::ButtonPress].
22///
23/// Triple-clicks are very similar to double-clicks, except that
24/// [`EventType::_3buttonPress`][crate::EventType::_3buttonPress] is inserted after the third click. The order of the
25/// events is:
26///
27/// - [`EventType::ButtonPress`][crate::EventType::ButtonPress]
28/// - [`EventType::ButtonRelease`][crate::EventType::ButtonRelease]
29/// - [`EventType::ButtonPress`][crate::EventType::ButtonPress]
30/// - [`EventType::_2buttonPress`][crate::EventType::_2buttonPress]
31/// - [`EventType::ButtonRelease`][crate::EventType::ButtonRelease]
32/// - [`EventType::ButtonPress`][crate::EventType::ButtonPress]
33/// - [`EventType::_3buttonPress`][crate::EventType::_3buttonPress]
34/// - [`EventType::ButtonRelease`][crate::EventType::ButtonRelease]
35///
36/// For a double click to occur, the second button press must occur within
37/// 1/4 of a second of the first. For a triple click to occur, the third
38/// button press must also occur within 1/2 second of the first button press.
39#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
40pub struct EventButton(crate::Event);
41
42event_wrapper!(EventButton, GdkEventButton);
43event_subtype!(
44    EventButton,
45    ffi::GDK_BUTTON_PRESS
46        | ffi::GDK_DOUBLE_BUTTON_PRESS
47        | ffi::GDK_TRIPLE_BUTTON_PRESS
48        | ffi::GDK_BUTTON_RELEASE
49);
50
51impl EventButton {
52    #[doc(alias = "get_position")]
53    pub fn position(&self) -> (f64, f64) {
54        let x = self.as_ref().x;
55        let y = self.as_ref().y;
56        (x, y)
57    }
58
59    #[doc(alias = "get_state")]
60    pub fn state(&self) -> crate::ModifierType {
61        unsafe { from_glib(self.as_ref().state) }
62    }
63
64    #[doc(alias = "get_time")]
65    pub fn time(&self) -> u32 {
66        self.as_ref().time
67    }
68
69    #[doc(alias = "get_button")]
70    pub fn button(&self) -> u32 {
71        self.as_ref().button
72    }
73
74    #[doc(alias = "get_device")]
75    pub fn device(&self) -> Option<crate::Device> {
76        unsafe { from_glib_none(self.as_ref().device) }
77    }
78
79    #[doc(alias = "get_axes")]
80    pub fn axes(&self) -> Option<(f64, f64)> {
81        let axes = self.as_ref().axes;
82
83        if axes.is_null() {
84            None
85        } else {
86            unsafe { Some((*axes, *axes.offset(1))) }
87        }
88    }
89
90    #[doc(alias = "get_root")]
91    pub fn root(&self) -> (f64, f64) {
92        let x_root = self.as_ref().x_root;
93        let y_root = self.as_ref().y_root;
94        (x_root, y_root)
95    }
96}