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, PropagationPhase, 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    // rustdoc-stripper-ignore-next
74    /// Creates a new builder-pattern struct instance to construct [`EventControllerKey`] objects.
75    ///
76    /// This method returns an instance of [`EventControllerKeyBuilder`](crate::builders::EventControllerKeyBuilder) which can be used to create [`EventControllerKey`] objects.
77    pub fn builder() -> EventControllerKeyBuilder {
78        EventControllerKeyBuilder::new()
79    }
80
81    #[doc(alias = "gtk_event_controller_key_forward")]
82    pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
83        unsafe {
84            from_glib(ffi::gtk_event_controller_key_forward(
85                self.to_glib_none().0,
86                widget.as_ref().to_glib_none().0,
87            ))
88        }
89    }
90
91    #[doc(alias = "gtk_event_controller_key_get_group")]
92    #[doc(alias = "get_group")]
93    pub fn group(&self) -> u32 {
94        unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
95    }
96
97    /// Gets the IM context of a key controller.
98    ///
99    /// # Returns
100    ///
101    /// the IM context
102    #[doc(alias = "gtk_event_controller_key_get_im_context")]
103    #[doc(alias = "get_im_context")]
104    pub fn im_context(&self) -> Option<IMContext> {
105        unsafe {
106            from_glib_none(ffi::gtk_event_controller_key_get_im_context(
107                self.to_glib_none().0,
108            ))
109        }
110    }
111
112    #[doc(alias = "gtk_event_controller_key_set_im_context")]
113    pub fn set_im_context(&self, im_context: &impl IsA<IMContext>) {
114        unsafe {
115            ffi::gtk_event_controller_key_set_im_context(
116                self.to_glib_none().0,
117                im_context.as_ref().to_glib_none().0,
118            );
119        }
120    }
121
122    #[doc(alias = "focus-in")]
123    pub fn connect_focus_in<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
124        unsafe extern "C" fn focus_in_trampoline<F: Fn(&EventControllerKey) + 'static>(
125            this: *mut ffi::GtkEventControllerKey,
126            f: glib::ffi::gpointer,
127        ) {
128            unsafe {
129                let f: &F = &*(f as *const F);
130                f(&from_glib_borrow(this))
131            }
132        }
133        unsafe {
134            let f: Box_<F> = Box_::new(f);
135            connect_raw(
136                self.as_ptr() as *mut _,
137                c"focus-in".as_ptr(),
138                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
139                    focus_in_trampoline::<F> as *const (),
140                )),
141                Box_::into_raw(f),
142            )
143        }
144    }
145
146    #[doc(alias = "focus-out")]
147    pub fn connect_focus_out<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
148        unsafe extern "C" fn focus_out_trampoline<F: Fn(&EventControllerKey) + 'static>(
149            this: *mut ffi::GtkEventControllerKey,
150            f: glib::ffi::gpointer,
151        ) {
152            unsafe {
153                let f: &F = &*(f as *const F);
154                f(&from_glib_borrow(this))
155            }
156        }
157        unsafe {
158            let f: Box_<F> = Box_::new(f);
159            connect_raw(
160                self.as_ptr() as *mut _,
161                c"focus-out".as_ptr(),
162                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
163                    focus_out_trampoline::<F> as *const (),
164                )),
165                Box_::into_raw(f),
166            )
167        }
168    }
169
170    #[doc(alias = "im-update")]
171    pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
172        unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
173            this: *mut ffi::GtkEventControllerKey,
174            f: glib::ffi::gpointer,
175        ) {
176            unsafe {
177                let f: &F = &*(f as *const F);
178                f(&from_glib_borrow(this))
179            }
180        }
181        unsafe {
182            let f: Box_<F> = Box_::new(f);
183            connect_raw(
184                self.as_ptr() as *mut _,
185                c"im-update".as_ptr(),
186                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
187                    im_update_trampoline::<F> as *const (),
188                )),
189                Box_::into_raw(f),
190            )
191        }
192    }
193
194    /// This signal is emitted whenever a key is pressed.
195    /// ## `keyval`
196    /// the pressed key.
197    /// ## `keycode`
198    /// the raw code of the pressed key.
199    /// ## `state`
200    /// the bitmask, representing the state of modifier keys and pointer buttons. See [`gdk::ModifierType`][crate::gdk::ModifierType].
201    ///
202    /// # Returns
203    ///
204    /// [`true`] if the key press was handled, [`false`] otherwise.
205    #[cfg(feature = "v3_24")]
206    #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
207    #[doc(alias = "key-pressed")]
208    pub fn connect_key_pressed<F: Fn(&Self, u32, u32, gdk::ModifierType) -> bool + 'static>(
209        &self,
210        f: F,
211    ) -> SignalHandlerId {
212        unsafe extern "C" fn key_pressed_trampoline<
213            F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) -> bool + 'static,
214        >(
215            this: *mut ffi::GtkEventControllerKey,
216            keyval: std::ffi::c_uint,
217            keycode: std::ffi::c_uint,
218            state: gdk::ffi::GdkModifierType,
219            f: glib::ffi::gpointer,
220        ) -> glib::ffi::gboolean {
221            unsafe {
222                let f: &F = &*(f as *const F);
223                f(&from_glib_borrow(this), keyval, keycode, from_glib(state)).into_glib()
224            }
225        }
226        unsafe {
227            let f: Box_<F> = Box_::new(f);
228            connect_raw(
229                self.as_ptr() as *mut _,
230                c"key-pressed".as_ptr(),
231                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
232                    key_pressed_trampoline::<F> as *const (),
233                )),
234                Box_::into_raw(f),
235            )
236        }
237    }
238
239    /// This signal is emitted whenever a key is released.
240    /// ## `keyval`
241    /// the released key.
242    /// ## `keycode`
243    /// the raw code of the released key.
244    /// ## `state`
245    /// the bitmask, representing the state of modifier keys and pointer buttons. See [`gdk::ModifierType`][crate::gdk::ModifierType].
246    #[cfg(feature = "v3_24")]
247    #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
248    #[doc(alias = "key-released")]
249    pub fn connect_key_released<F: Fn(&Self, u32, u32, gdk::ModifierType) + 'static>(
250        &self,
251        f: F,
252    ) -> SignalHandlerId {
253        unsafe extern "C" fn key_released_trampoline<
254            F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) + 'static,
255        >(
256            this: *mut ffi::GtkEventControllerKey,
257            keyval: std::ffi::c_uint,
258            keycode: std::ffi::c_uint,
259            state: gdk::ffi::GdkModifierType,
260            f: glib::ffi::gpointer,
261        ) {
262            unsafe {
263                let f: &F = &*(f as *const F);
264                f(&from_glib_borrow(this), keyval, keycode, from_glib(state))
265            }
266        }
267        unsafe {
268            let f: Box_<F> = Box_::new(f);
269            connect_raw(
270                self.as_ptr() as *mut _,
271                c"key-released".as_ptr(),
272                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
273                    key_released_trampoline::<F> as *const (),
274                )),
275                Box_::into_raw(f),
276            )
277        }
278    }
279
280    #[doc(alias = "modifiers")]
281    pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> bool + 'static>(
282        &self,
283        f: F,
284    ) -> SignalHandlerId {
285        unsafe extern "C" fn modifiers_trampoline<
286            F: Fn(&EventControllerKey, gdk::ModifierType) -> bool + 'static,
287        >(
288            this: *mut ffi::GtkEventControllerKey,
289            object: gdk::ffi::GdkModifierType,
290            f: glib::ffi::gpointer,
291        ) -> glib::ffi::gboolean {
292            unsafe {
293                let f: &F = &*(f as *const F);
294                f(&from_glib_borrow(this), from_glib(object)).into_glib()
295            }
296        }
297        unsafe {
298            let f: Box_<F> = Box_::new(f);
299            connect_raw(
300                self.as_ptr() as *mut _,
301                c"modifiers".as_ptr(),
302                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
303                    modifiers_trampoline::<F> as *const (),
304                )),
305                Box_::into_raw(f),
306            )
307        }
308    }
309}
310
311impl Default for EventControllerKey {
312    fn default() -> Self {
313        glib::object::Object::new::<Self>()
314    }
315}
316
317// rustdoc-stripper-ignore-next
318/// A [builder-pattern] type to construct [`EventControllerKey`] objects.
319///
320/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
321#[must_use = "The builder must be built to be used"]
322pub struct EventControllerKeyBuilder {
323    builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
324}
325
326impl EventControllerKeyBuilder {
327    fn new() -> Self {
328        Self {
329            builder: glib::object::Object::builder(),
330        }
331    }
332
333    /// The propagation phase at which this controller will handle events.
334    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
335        Self {
336            builder: self
337                .builder
338                .property("propagation-phase", propagation_phase),
339        }
340    }
341
342    /// The widget receiving the `GdkEvents` that the controller will handle.
343    pub fn widget(self, widget: &impl IsA<Widget>) -> Self {
344        Self {
345            builder: self.builder.property("widget", widget.clone().upcast()),
346        }
347    }
348
349    // rustdoc-stripper-ignore-next
350    /// Build the [`EventControllerKey`].
351    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
352    pub fn build(self) -> EventControllerKey {
353        assert_initialized_main_thread!();
354        self.builder.build()
355    }
356}