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