Skip to main content

gtk/auto/
event_controller_key.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::{EventController, IMContext, Widget};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::{boxed::Box as Box_, fmt, mem::transmute};
12
13glib::wrapper! {
14    /// [`EventControllerKey`][crate::EventControllerKey] is an event controller meant for situations
15    /// where you need access to key events.
16    ///
17    /// This object was added in 3.24.
18    ///
19    /// ## Signals
20    ///
21    ///
22    /// #### `focus-in`
23    ///
24    ///
25    ///
26    /// #### `focus-out`
27    ///
28    ///
29    ///
30    /// #### `im-update`
31    ///
32    ///
33    ///
34    /// #### `key-pressed`
35    ///  This signal is emitted whenever a key is pressed.
36    ///
37    ///
38    ///
39    ///
40    /// #### `key-released`
41    ///  This signal is emitted whenever a key is released.
42    ///
43    ///
44    ///
45    ///
46    /// #### `modifiers`
47    ///
48    ///
49    /// # Implements
50    ///
51    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`]
52    #[doc(alias = "GtkEventControllerKey")]
53    pub struct EventControllerKey(Object<ffi::GtkEventControllerKey, ffi::GtkEventControllerKeyClass>) @extends EventController;
54
55    match fn {
56        type_ => || ffi::gtk_event_controller_key_get_type(),
57    }
58}
59
60impl EventControllerKey {
61    #[doc(alias = "gtk_event_controller_key_new")]
62    pub fn new(widget: &impl IsA<Widget>) -> EventControllerKey {
63        skip_assert_initialized!();
64        unsafe {
65            EventController::from_glib_full(ffi::gtk_event_controller_key_new(
66                widget.as_ref().to_glib_none().0,
67            ))
68            .unsafe_cast()
69        }
70    }
71
72    #[doc(alias = "gtk_event_controller_key_forward")]
73    pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
74        unsafe {
75            from_glib(ffi::gtk_event_controller_key_forward(
76                self.to_glib_none().0,
77                widget.as_ref().to_glib_none().0,
78            ))
79        }
80    }
81
82    #[doc(alias = "gtk_event_controller_key_get_group")]
83    #[doc(alias = "get_group")]
84    pub fn group(&self) -> u32 {
85        unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
86    }
87
88    /// Gets the IM context of a key controller.
89    ///
90    /// # Returns
91    ///
92    /// the IM context
93    #[doc(alias = "gtk_event_controller_key_get_im_context")]
94    #[doc(alias = "get_im_context")]
95    pub fn im_context(&self) -> Option<IMContext> {
96        unsafe {
97            from_glib_none(ffi::gtk_event_controller_key_get_im_context(
98                self.to_glib_none().0,
99            ))
100        }
101    }
102
103    #[doc(alias = "gtk_event_controller_key_set_im_context")]
104    pub fn set_im_context(&self, im_context: &impl IsA<IMContext>) {
105        unsafe {
106            ffi::gtk_event_controller_key_set_im_context(
107                self.to_glib_none().0,
108                im_context.as_ref().to_glib_none().0,
109            );
110        }
111    }
112
113    #[doc(alias = "focus-in")]
114    pub fn connect_focus_in<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
115        unsafe extern "C" fn focus_in_trampoline<F: Fn(&EventControllerKey) + 'static>(
116            this: *mut ffi::GtkEventControllerKey,
117            f: glib::ffi::gpointer,
118        ) {
119            let f: &F = &*(f as *const F);
120            f(&from_glib_borrow(this))
121        }
122        unsafe {
123            let f: Box_<F> = Box_::new(f);
124            connect_raw(
125                self.as_ptr() as *mut _,
126                b"focus-in\0".as_ptr() as *const _,
127                Some(transmute::<_, unsafe extern "C" fn()>(
128                    focus_in_trampoline::<F> as *const (),
129                )),
130                Box_::into_raw(f),
131            )
132        }
133    }
134
135    #[doc(alias = "focus-out")]
136    pub fn connect_focus_out<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
137        unsafe extern "C" fn focus_out_trampoline<F: Fn(&EventControllerKey) + 'static>(
138            this: *mut ffi::GtkEventControllerKey,
139            f: glib::ffi::gpointer,
140        ) {
141            let f: &F = &*(f as *const F);
142            f(&from_glib_borrow(this))
143        }
144        unsafe {
145            let f: Box_<F> = Box_::new(f);
146            connect_raw(
147                self.as_ptr() as *mut _,
148                b"focus-out\0".as_ptr() as *const _,
149                Some(transmute::<_, unsafe extern "C" fn()>(
150                    focus_out_trampoline::<F> as *const (),
151                )),
152                Box_::into_raw(f),
153            )
154        }
155    }
156
157    #[doc(alias = "im-update")]
158    pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
159        unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
160            this: *mut ffi::GtkEventControllerKey,
161            f: glib::ffi::gpointer,
162        ) {
163            let f: &F = &*(f as *const F);
164            f(&from_glib_borrow(this))
165        }
166        unsafe {
167            let f: Box_<F> = Box_::new(f);
168            connect_raw(
169                self.as_ptr() as *mut _,
170                b"im-update\0".as_ptr() as *const _,
171                Some(transmute::<_, unsafe extern "C" fn()>(
172                    im_update_trampoline::<F> as *const (),
173                )),
174                Box_::into_raw(f),
175            )
176        }
177    }
178
179    /// This signal is emitted whenever a key is pressed.
180    /// ## `keyval`
181    /// the pressed key.
182    /// ## `keycode`
183    /// the raw code of the pressed key.
184    /// ## `state`
185    /// the bitmask, representing the state of modifier keys and pointer buttons. See [`gdk::ModifierType`][crate::gdk::ModifierType].
186    ///
187    /// # Returns
188    ///
189    /// [`true`] if the key press was handled, [`false`] otherwise.
190    #[cfg(feature = "v3_24")]
191    #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
192    #[doc(alias = "key-pressed")]
193    pub fn connect_key_pressed<F: Fn(&Self, u32, u32, gdk::ModifierType) -> bool + 'static>(
194        &self,
195        f: F,
196    ) -> SignalHandlerId {
197        unsafe extern "C" fn key_pressed_trampoline<
198            F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) -> bool + 'static,
199        >(
200            this: *mut ffi::GtkEventControllerKey,
201            keyval: libc::c_uint,
202            keycode: libc::c_uint,
203            state: gdk::ffi::GdkModifierType,
204            f: glib::ffi::gpointer,
205        ) -> glib::ffi::gboolean {
206            let f: &F = &*(f as *const F);
207            f(&from_glib_borrow(this), keyval, keycode, from_glib(state)).into_glib()
208        }
209        unsafe {
210            let f: Box_<F> = Box_::new(f);
211            connect_raw(
212                self.as_ptr() as *mut _,
213                b"key-pressed\0".as_ptr() as *const _,
214                Some(transmute::<_, unsafe extern "C" fn()>(
215                    key_pressed_trampoline::<F> as *const (),
216                )),
217                Box_::into_raw(f),
218            )
219        }
220    }
221
222    /// This signal is emitted whenever a key is released.
223    /// ## `keyval`
224    /// the released key.
225    /// ## `keycode`
226    /// the raw code of the released key.
227    /// ## `state`
228    /// the bitmask, representing the state of modifier keys and pointer buttons. See [`gdk::ModifierType`][crate::gdk::ModifierType].
229    #[cfg(feature = "v3_24")]
230    #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
231    #[doc(alias = "key-released")]
232    pub fn connect_key_released<F: Fn(&Self, u32, u32, gdk::ModifierType) + 'static>(
233        &self,
234        f: F,
235    ) -> SignalHandlerId {
236        unsafe extern "C" fn key_released_trampoline<
237            F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) + 'static,
238        >(
239            this: *mut ffi::GtkEventControllerKey,
240            keyval: libc::c_uint,
241            keycode: libc::c_uint,
242            state: gdk::ffi::GdkModifierType,
243            f: glib::ffi::gpointer,
244        ) {
245            let f: &F = &*(f as *const F);
246            f(&from_glib_borrow(this), keyval, keycode, from_glib(state))
247        }
248        unsafe {
249            let f: Box_<F> = Box_::new(f);
250            connect_raw(
251                self.as_ptr() as *mut _,
252                b"key-released\0".as_ptr() as *const _,
253                Some(transmute::<_, unsafe extern "C" fn()>(
254                    key_released_trampoline::<F> as *const (),
255                )),
256                Box_::into_raw(f),
257            )
258        }
259    }
260
261    #[doc(alias = "modifiers")]
262    pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> bool + 'static>(
263        &self,
264        f: F,
265    ) -> SignalHandlerId {
266        unsafe extern "C" fn modifiers_trampoline<
267            F: Fn(&EventControllerKey, gdk::ModifierType) -> bool + 'static,
268        >(
269            this: *mut ffi::GtkEventControllerKey,
270            object: gdk::ffi::GdkModifierType,
271            f: glib::ffi::gpointer,
272        ) -> glib::ffi::gboolean {
273            let f: &F = &*(f as *const F);
274            f(&from_glib_borrow(this), from_glib(object)).into_glib()
275        }
276        unsafe {
277            let f: Box_<F> = Box_::new(f);
278            connect_raw(
279                self.as_ptr() as *mut _,
280                b"modifiers\0".as_ptr() as *const _,
281                Some(transmute::<_, unsafe extern "C" fn()>(
282                    modifiers_trampoline::<F> as *const (),
283                )),
284                Box_::into_raw(f),
285            )
286        }
287    }
288}
289
290impl fmt::Display for EventControllerKey {
291    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
292        f.write_str("EventControllerKey")
293    }
294}