Skip to main content

gtk4/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#![allow(deprecated)]
5
6use crate::{EventController, IMContext, PropagationLimit, PropagationPhase, Widget, ffi};
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{SignalHandlerId, connect_raw},
11    translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16    /// Provides access to key events.
17    ///
18    /// ## Signals
19    ///
20    ///
21    /// #### `im-update`
22    ///  Emitted whenever the input method context filters away
23    /// a keypress and prevents the @controller receiving it.
24    ///
25    /// See [`EventControllerKey::set_im_context()`][crate::EventControllerKey::set_im_context()] and
26    /// [`IMContextExt::filter_keypress()`][crate::prelude::IMContextExt::filter_keypress()].
27    ///
28    ///
29    ///
30    ///
31    /// #### `key-pressed`
32    ///  Emitted whenever a key is pressed.
33    ///
34    ///
35    ///
36    ///
37    /// #### `key-released`
38    ///  Emitted whenever a key is released.
39    ///
40    ///
41    ///
42    ///
43    /// #### `modifiers`
44    ///  Emitted whenever the state of modifier keys and pointer buttons change.
45    ///
46    ///
47    ///
48    /// # Implements
49    ///
50    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`], [`EventControllerExtManual`][trait@crate::prelude::EventControllerExtManual]
51    #[doc(alias = "GtkEventControllerKey")]
52    pub struct EventControllerKey(Object<ffi::GtkEventControllerKey, ffi::GtkEventControllerKeyClass>) @extends EventController;
53
54    match fn {
55        type_ => || ffi::gtk_event_controller_key_get_type(),
56    }
57}
58
59impl EventControllerKey {
60    /// Creates a new event controller that will handle key events.
61    ///
62    /// # Returns
63    ///
64    /// a new [`EventControllerKey`][crate::EventControllerKey]
65    #[doc(alias = "gtk_event_controller_key_new")]
66    pub fn new() -> EventControllerKey {
67        assert_initialized_main_thread!();
68        unsafe {
69            EventController::from_glib_full(ffi::gtk_event_controller_key_new()).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    /// Forwards the current event of this @self to a @widget.
82    ///
83    /// This function can only be used in handlers for the
84    /// [`key-pressed`][struct@crate::EventControllerKey#key-pressed],
85    /// [`key-released`][struct@crate::EventControllerKey#key-released]
86    /// or [`modifiers`][struct@crate::EventControllerKey#modifiers] signals.
87    ///
88    /// # Deprecated since 4.24
89    ///
90    /// Use [`EditableExt::set_input_interceptor()`][crate::prelude::EditableExt::set_input_interceptor()] instead
91    /// ## `widget`
92    /// a [`Widget`][crate::Widget]
93    ///
94    /// # Returns
95    ///
96    /// whether the @widget handled the event
97    #[cfg_attr(feature = "v4_24", deprecated = "Since 4.24")]
98    #[allow(deprecated)]
99    #[doc(alias = "gtk_event_controller_key_forward")]
100    pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
101        unsafe {
102            from_glib(ffi::gtk_event_controller_key_forward(
103                self.to_glib_none().0,
104                widget.as_ref().to_glib_none().0,
105            ))
106        }
107    }
108
109    /// Gets the key group of the current event of this @self.
110    ///
111    /// See `Gdk::KeyEvent::get_layout()`.
112    ///
113    /// # Returns
114    ///
115    /// the key group
116    #[doc(alias = "gtk_event_controller_key_get_group")]
117    #[doc(alias = "get_group")]
118    pub fn group(&self) -> u32 {
119        unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
120    }
121
122    /// Gets the input method context of the key @self.
123    ///
124    /// # Returns
125    ///
126    /// the [`IMContext`][crate::IMContext]
127    #[doc(alias = "gtk_event_controller_key_get_im_context")]
128    #[doc(alias = "get_im_context")]
129    pub fn im_context(&self) -> Option<IMContext> {
130        unsafe {
131            from_glib_none(ffi::gtk_event_controller_key_get_im_context(
132                self.to_glib_none().0,
133            ))
134        }
135    }
136
137    /// Sets the input method context of the key @self.
138    /// ## `im_context`
139    /// a [`IMContext`][crate::IMContext]
140    #[doc(alias = "gtk_event_controller_key_set_im_context")]
141    pub fn set_im_context(&self, im_context: Option<&impl IsA<IMContext>>) {
142        unsafe {
143            ffi::gtk_event_controller_key_set_im_context(
144                self.to_glib_none().0,
145                im_context.map(|p| p.as_ref()).to_glib_none().0,
146            );
147        }
148    }
149
150    /// Emitted whenever the input method context filters away
151    /// a keypress and prevents the @controller receiving it.
152    ///
153    /// See [`set_im_context()`][Self::set_im_context()] and
154    /// [`IMContextExt::filter_keypress()`][crate::prelude::IMContextExt::filter_keypress()].
155    #[doc(alias = "im-update")]
156    pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
157        unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
158            this: *mut ffi::GtkEventControllerKey,
159            f: glib::ffi::gpointer,
160        ) {
161            unsafe {
162                let f: &F = &*(f as *const F);
163                f(&from_glib_borrow(this))
164            }
165        }
166        unsafe {
167            let f: Box_<F> = Box_::new(f);
168            connect_raw(
169                self.as_ptr() as *mut _,
170                c"im-update".as_ptr(),
171                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
172                    im_update_trampoline::<F> as *const (),
173                )),
174                Box_::into_raw(f),
175            )
176        }
177    }
178
179    /// Emitted whenever the state of modifier keys and pointer buttons change.
180    /// ## `state`
181    /// the bitmask, representing the new state of modifier keys and
182    ///   pointer buttons.
183    ///
184    /// # Returns
185    ///
186    /// whether to ignore modifiers
187    #[doc(alias = "modifiers")]
188    pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> glib::Propagation + 'static>(
189        &self,
190        f: F,
191    ) -> SignalHandlerId {
192        unsafe extern "C" fn modifiers_trampoline<
193            F: Fn(&EventControllerKey, gdk::ModifierType) -> glib::Propagation + 'static,
194        >(
195            this: *mut ffi::GtkEventControllerKey,
196            state: gdk::ffi::GdkModifierType,
197            f: glib::ffi::gpointer,
198        ) -> glib::ffi::gboolean {
199            unsafe {
200                let f: &F = &*(f as *const F);
201                f(&from_glib_borrow(this), from_glib(state)).into_glib()
202            }
203        }
204        unsafe {
205            let f: Box_<F> = Box_::new(f);
206            connect_raw(
207                self.as_ptr() as *mut _,
208                c"modifiers".as_ptr(),
209                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
210                    modifiers_trampoline::<F> as *const (),
211                )),
212                Box_::into_raw(f),
213            )
214        }
215    }
216}
217
218impl Default for EventControllerKey {
219    fn default() -> Self {
220        Self::new()
221    }
222}
223
224// rustdoc-stripper-ignore-next
225/// A [builder-pattern] type to construct [`EventControllerKey`] objects.
226///
227/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
228#[must_use = "The builder must be built to be used"]
229pub struct EventControllerKeyBuilder {
230    builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
231}
232
233impl EventControllerKeyBuilder {
234    fn new() -> Self {
235        Self {
236            builder: glib::object::Object::builder(),
237        }
238    }
239
240    /// The name for this controller, typically used for debugging purposes.
241    pub fn name(self, name: impl Into<glib::GString>) -> Self {
242        Self {
243            builder: self.builder.property("name", name.into()),
244        }
245    }
246
247    /// The limit for which events this controller will handle.
248    pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
249        Self {
250            builder: self
251                .builder
252                .property("propagation-limit", propagation_limit),
253        }
254    }
255
256    /// The propagation phase at which this controller will handle events.
257    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
258        Self {
259            builder: self
260                .builder
261                .property("propagation-phase", propagation_phase),
262        }
263    }
264
265    // rustdoc-stripper-ignore-next
266    /// Build the [`EventControllerKey`].
267    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
268    pub fn build(self) -> EventControllerKey {
269        assert_initialized_main_thread!();
270        self.builder.build()
271    }
272}