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
5use crate::{ffi, EventController, IMContext, PropagationLimit, PropagationPhase, Widget};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// [`EventControllerKey`][crate::EventControllerKey] is an event controller that provides access
16 /// 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`]
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 /// ## `widget`
88 /// a [`Widget`][crate::Widget]
89 ///
90 /// # Returns
91 ///
92 /// whether the @widget handled the event
93 #[doc(alias = "gtk_event_controller_key_forward")]
94 pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
95 unsafe {
96 from_glib(ffi::gtk_event_controller_key_forward(
97 self.to_glib_none().0,
98 widget.as_ref().to_glib_none().0,
99 ))
100 }
101 }
102
103 /// Gets the key group of the current event of this @self.
104 ///
105 /// See `Gdk::KeyEvent::get_layout()`.
106 ///
107 /// # Returns
108 ///
109 /// the key group
110 #[doc(alias = "gtk_event_controller_key_get_group")]
111 #[doc(alias = "get_group")]
112 pub fn group(&self) -> u32 {
113 unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
114 }
115
116 /// Gets the input method context of the key @self.
117 ///
118 /// # Returns
119 ///
120 /// the [`IMContext`][crate::IMContext]
121 #[doc(alias = "gtk_event_controller_key_get_im_context")]
122 #[doc(alias = "get_im_context")]
123 pub fn im_context(&self) -> Option<IMContext> {
124 unsafe {
125 from_glib_none(ffi::gtk_event_controller_key_get_im_context(
126 self.to_glib_none().0,
127 ))
128 }
129 }
130
131 /// Sets the input method context of the key @self.
132 /// ## `im_context`
133 /// a [`IMContext`][crate::IMContext]
134 #[doc(alias = "gtk_event_controller_key_set_im_context")]
135 pub fn set_im_context(&self, im_context: Option<&impl IsA<IMContext>>) {
136 unsafe {
137 ffi::gtk_event_controller_key_set_im_context(
138 self.to_glib_none().0,
139 im_context.map(|p| p.as_ref()).to_glib_none().0,
140 );
141 }
142 }
143
144 /// Emitted whenever the input method context filters away
145 /// a keypress and prevents the @controller receiving it.
146 ///
147 /// See [`set_im_context()`][Self::set_im_context()] and
148 /// [`IMContextExt::filter_keypress()`][crate::prelude::IMContextExt::filter_keypress()].
149 #[doc(alias = "im-update")]
150 pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
151 unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
152 this: *mut ffi::GtkEventControllerKey,
153 f: glib::ffi::gpointer,
154 ) {
155 let f: &F = &*(f as *const F);
156 f(&from_glib_borrow(this))
157 }
158 unsafe {
159 let f: Box_<F> = Box_::new(f);
160 connect_raw(
161 self.as_ptr() as *mut _,
162 b"im-update\0".as_ptr() as *const _,
163 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
164 im_update_trampoline::<F> as *const (),
165 )),
166 Box_::into_raw(f),
167 )
168 }
169 }
170
171 /// Emitted whenever the state of modifier keys and pointer buttons change.
172 /// ## `state`
173 /// the bitmask, representing the new state of modifier keys and
174 /// pointer buttons.
175 ///
176 /// # Returns
177 ///
178 /// whether to ignore modifiers
179 #[doc(alias = "modifiers")]
180 pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> glib::Propagation + 'static>(
181 &self,
182 f: F,
183 ) -> SignalHandlerId {
184 unsafe extern "C" fn modifiers_trampoline<
185 F: Fn(&EventControllerKey, gdk::ModifierType) -> glib::Propagation + 'static,
186 >(
187 this: *mut ffi::GtkEventControllerKey,
188 state: gdk::ffi::GdkModifierType,
189 f: glib::ffi::gpointer,
190 ) -> glib::ffi::gboolean {
191 let f: &F = &*(f as *const F);
192 f(&from_glib_borrow(this), from_glib(state)).into_glib()
193 }
194 unsafe {
195 let f: Box_<F> = Box_::new(f);
196 connect_raw(
197 self.as_ptr() as *mut _,
198 b"modifiers\0".as_ptr() as *const _,
199 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
200 modifiers_trampoline::<F> as *const (),
201 )),
202 Box_::into_raw(f),
203 )
204 }
205 }
206}
207
208impl Default for EventControllerKey {
209 fn default() -> Self {
210 Self::new()
211 }
212}
213
214// rustdoc-stripper-ignore-next
215/// A [builder-pattern] type to construct [`EventControllerKey`] objects.
216///
217/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
218#[must_use = "The builder must be built to be used"]
219pub struct EventControllerKeyBuilder {
220 builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
221}
222
223impl EventControllerKeyBuilder {
224 fn new() -> Self {
225 Self {
226 builder: glib::object::Object::builder(),
227 }
228 }
229
230 /// The name for this controller, typically used for debugging purposes.
231 pub fn name(self, name: impl Into<glib::GString>) -> Self {
232 Self {
233 builder: self.builder.property("name", name.into()),
234 }
235 }
236
237 /// The limit for which events this controller will handle.
238 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
239 Self {
240 builder: self
241 .builder
242 .property("propagation-limit", propagation_limit),
243 }
244 }
245
246 /// The propagation phase at which this controller will handle events.
247 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
248 Self {
249 builder: self
250 .builder
251 .property("propagation-phase", propagation_phase),
252 }
253 }
254
255 // rustdoc-stripper-ignore-next
256 /// Build the [`EventControllerKey`].
257 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
258 pub fn build(self) -> EventControllerKey {
259 assert_initialized_main_thread!();
260 self.builder.build()
261 }
262}