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