1#![allow(deprecated)]
5
6use crate::{ffi, InputHints, InputPurpose, Widget};
7use glib::{
8 object::ObjectType as _,
9 prelude::*,
10 signal::{connect_raw, SignalHandlerId},
11 translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16 #[doc(alias = "GtkIMContext")]
17 pub struct IMContext(Object<ffi::GtkIMContext, ffi::GtkIMContextClass>);
18
19 match fn {
20 type_ => || ffi::gtk_im_context_get_type(),
21 }
22}
23
24impl IMContext {
25 pub const NONE: Option<&'static IMContext> = None;
26}
27
28pub trait IMContextExt: IsA<IMContext> + 'static {
29 #[cfg(feature = "v4_14")]
30 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
31 #[doc(alias = "gtk_im_context_activate_osk")]
32 fn activate_osk(&self, event: Option<impl AsRef<gdk::Event>>) -> bool {
33 unsafe {
34 from_glib(ffi::gtk_im_context_activate_osk(
35 self.as_ref().to_glib_none().0,
36 event.as_ref().map(|p| p.as_ref()).to_glib_none().0,
37 ))
38 }
39 }
40
41 #[doc(alias = "gtk_im_context_delete_surrounding")]
42 fn delete_surrounding(&self, offset: i32, n_chars: i32) -> bool {
43 unsafe {
44 from_glib(ffi::gtk_im_context_delete_surrounding(
45 self.as_ref().to_glib_none().0,
46 offset,
47 n_chars,
48 ))
49 }
50 }
51
52 #[doc(alias = "gtk_im_context_filter_key")]
53 fn filter_key(
54 &self,
55 press: bool,
56 surface: &impl IsA<gdk::Surface>,
57 device: &gdk::Device,
58 time: u32,
59 keycode: u32,
60 state: gdk::ModifierType,
61 group: i32,
62 ) -> bool {
63 unsafe {
64 from_glib(ffi::gtk_im_context_filter_key(
65 self.as_ref().to_glib_none().0,
66 press.into_glib(),
67 surface.as_ref().to_glib_none().0,
68 device.to_glib_none().0,
69 time,
70 keycode,
71 state.into_glib(),
72 group,
73 ))
74 }
75 }
76
77 #[doc(alias = "gtk_im_context_filter_keypress")]
78 fn filter_keypress(&self, event: impl AsRef<gdk::Event>) -> bool {
79 unsafe {
80 from_glib(ffi::gtk_im_context_filter_keypress(
81 self.as_ref().to_glib_none().0,
82 event.as_ref().to_glib_none().0,
83 ))
84 }
85 }
86
87 #[doc(alias = "gtk_im_context_focus_in")]
88 fn focus_in(&self) {
89 unsafe {
90 ffi::gtk_im_context_focus_in(self.as_ref().to_glib_none().0);
91 }
92 }
93
94 #[doc(alias = "gtk_im_context_focus_out")]
95 fn focus_out(&self) {
96 unsafe {
97 ffi::gtk_im_context_focus_out(self.as_ref().to_glib_none().0);
98 }
99 }
100
101 #[doc(alias = "gtk_im_context_get_preedit_string")]
102 #[doc(alias = "get_preedit_string")]
103 fn preedit_string(&self) -> (glib::GString, pango::AttrList, i32) {
104 unsafe {
105 let mut str = std::ptr::null_mut();
106 let mut attrs = std::ptr::null_mut();
107 let mut cursor_pos = std::mem::MaybeUninit::uninit();
108 ffi::gtk_im_context_get_preedit_string(
109 self.as_ref().to_glib_none().0,
110 &mut str,
111 &mut attrs,
112 cursor_pos.as_mut_ptr(),
113 );
114 (
115 from_glib_full(str),
116 from_glib_full(attrs),
117 cursor_pos.assume_init(),
118 )
119 }
120 }
121
122 #[cfg_attr(feature = "v4_2", deprecated = "Since 4.2")]
123 #[allow(deprecated)]
124 #[doc(alias = "gtk_im_context_get_surrounding")]
125 #[doc(alias = "get_surrounding")]
126 fn surrounding(&self) -> Option<(glib::GString, i32)> {
127 unsafe {
128 let mut text = std::ptr::null_mut();
129 let mut cursor_index = std::mem::MaybeUninit::uninit();
130 let ret = from_glib(ffi::gtk_im_context_get_surrounding(
131 self.as_ref().to_glib_none().0,
132 &mut text,
133 cursor_index.as_mut_ptr(),
134 ));
135 if ret {
136 Some((from_glib_full(text), cursor_index.assume_init()))
137 } else {
138 None
139 }
140 }
141 }
142
143 #[cfg(feature = "v4_2")]
144 #[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
145 #[doc(alias = "gtk_im_context_get_surrounding_with_selection")]
146 #[doc(alias = "get_surrounding_with_selection")]
147 fn surrounding_with_selection(&self) -> Option<(glib::GString, i32, i32)> {
148 unsafe {
149 let mut text = std::ptr::null_mut();
150 let mut cursor_index = std::mem::MaybeUninit::uninit();
151 let mut anchor_index = std::mem::MaybeUninit::uninit();
152 let ret = from_glib(ffi::gtk_im_context_get_surrounding_with_selection(
153 self.as_ref().to_glib_none().0,
154 &mut text,
155 cursor_index.as_mut_ptr(),
156 anchor_index.as_mut_ptr(),
157 ));
158 if ret {
159 Some((
160 from_glib_full(text),
161 cursor_index.assume_init(),
162 anchor_index.assume_init(),
163 ))
164 } else {
165 None
166 }
167 }
168 }
169
170 #[doc(alias = "gtk_im_context_reset")]
171 fn reset(&self) {
172 unsafe {
173 ffi::gtk_im_context_reset(self.as_ref().to_glib_none().0);
174 }
175 }
176
177 #[doc(alias = "gtk_im_context_set_client_widget")]
178 fn set_client_widget(&self, widget: Option<&impl IsA<Widget>>) {
179 unsafe {
180 ffi::gtk_im_context_set_client_widget(
181 self.as_ref().to_glib_none().0,
182 widget.map(|p| p.as_ref()).to_glib_none().0,
183 );
184 }
185 }
186
187 #[doc(alias = "gtk_im_context_set_cursor_location")]
188 fn set_cursor_location(&self, area: &gdk::Rectangle) {
189 unsafe {
190 ffi::gtk_im_context_set_cursor_location(
191 self.as_ref().to_glib_none().0,
192 area.to_glib_none().0,
193 );
194 }
195 }
196
197 #[cfg_attr(feature = "v4_2", deprecated = "Since 4.2")]
198 #[allow(deprecated)]
199 #[doc(alias = "gtk_im_context_set_surrounding")]
200 fn set_surrounding(&self, text: &str, cursor_index: i32) {
201 let len = text.len() as _;
202 unsafe {
203 ffi::gtk_im_context_set_surrounding(
204 self.as_ref().to_glib_none().0,
205 text.to_glib_none().0,
206 len,
207 cursor_index,
208 );
209 }
210 }
211
212 #[cfg(feature = "v4_2")]
213 #[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
214 #[doc(alias = "gtk_im_context_set_surrounding_with_selection")]
215 fn set_surrounding_with_selection(&self, text: &str, cursor_index: i32, anchor_index: i32) {
216 let len = text.len() as _;
217 unsafe {
218 ffi::gtk_im_context_set_surrounding_with_selection(
219 self.as_ref().to_glib_none().0,
220 text.to_glib_none().0,
221 len,
222 cursor_index,
223 anchor_index,
224 );
225 }
226 }
227
228 #[doc(alias = "gtk_im_context_set_use_preedit")]
229 fn set_use_preedit(&self, use_preedit: bool) {
230 unsafe {
231 ffi::gtk_im_context_set_use_preedit(
232 self.as_ref().to_glib_none().0,
233 use_preedit.into_glib(),
234 );
235 }
236 }
237
238 #[doc(alias = "input-hints")]
239 fn input_hints(&self) -> InputHints {
240 ObjectExt::property(self.as_ref(), "input-hints")
241 }
242
243 #[doc(alias = "input-hints")]
244 fn set_input_hints(&self, input_hints: InputHints) {
245 ObjectExt::set_property(self.as_ref(), "input-hints", input_hints)
246 }
247
248 #[doc(alias = "input-purpose")]
249 fn input_purpose(&self) -> InputPurpose {
250 ObjectExt::property(self.as_ref(), "input-purpose")
251 }
252
253 #[doc(alias = "input-purpose")]
254 fn set_input_purpose(&self, input_purpose: InputPurpose) {
255 ObjectExt::set_property(self.as_ref(), "input-purpose", input_purpose)
256 }
257
258 #[doc(alias = "commit")]
259 fn connect_commit<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId {
260 unsafe extern "C" fn commit_trampoline<P: IsA<IMContext>, F: Fn(&P, &str) + 'static>(
261 this: *mut ffi::GtkIMContext,
262 str: *mut std::ffi::c_char,
263 f: glib::ffi::gpointer,
264 ) {
265 let f: &F = &*(f as *const F);
266 f(
267 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
268 &glib::GString::from_glib_borrow(str),
269 )
270 }
271 unsafe {
272 let f: Box_<F> = Box_::new(f);
273 connect_raw(
274 self.as_ptr() as *mut _,
275 c"commit".as_ptr() as *const _,
276 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
277 commit_trampoline::<Self, F> as *const (),
278 )),
279 Box_::into_raw(f),
280 )
281 }
282 }
283
284 #[doc(alias = "delete-surrounding")]
285 fn connect_delete_surrounding<F: Fn(&Self, i32, i32) -> bool + 'static>(
286 &self,
287 f: F,
288 ) -> SignalHandlerId {
289 unsafe extern "C" fn delete_surrounding_trampoline<
290 P: IsA<IMContext>,
291 F: Fn(&P, i32, i32) -> bool + 'static,
292 >(
293 this: *mut ffi::GtkIMContext,
294 offset: std::ffi::c_int,
295 n_chars: std::ffi::c_int,
296 f: glib::ffi::gpointer,
297 ) -> glib::ffi::gboolean {
298 let f: &F = &*(f as *const F);
299 f(
300 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
301 offset,
302 n_chars,
303 )
304 .into_glib()
305 }
306 unsafe {
307 let f: Box_<F> = Box_::new(f);
308 connect_raw(
309 self.as_ptr() as *mut _,
310 c"delete-surrounding".as_ptr() as *const _,
311 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
312 delete_surrounding_trampoline::<Self, F> as *const (),
313 )),
314 Box_::into_raw(f),
315 )
316 }
317 }
318
319 #[doc(alias = "preedit-changed")]
320 fn connect_preedit_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
321 unsafe extern "C" fn preedit_changed_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
322 this: *mut ffi::GtkIMContext,
323 f: glib::ffi::gpointer,
324 ) {
325 let f: &F = &*(f as *const F);
326 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
327 }
328 unsafe {
329 let f: Box_<F> = Box_::new(f);
330 connect_raw(
331 self.as_ptr() as *mut _,
332 c"preedit-changed".as_ptr() as *const _,
333 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
334 preedit_changed_trampoline::<Self, F> as *const (),
335 )),
336 Box_::into_raw(f),
337 )
338 }
339 }
340
341 #[doc(alias = "preedit-end")]
342 fn connect_preedit_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
343 unsafe extern "C" fn preedit_end_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
344 this: *mut ffi::GtkIMContext,
345 f: glib::ffi::gpointer,
346 ) {
347 let f: &F = &*(f as *const F);
348 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
349 }
350 unsafe {
351 let f: Box_<F> = Box_::new(f);
352 connect_raw(
353 self.as_ptr() as *mut _,
354 c"preedit-end".as_ptr() as *const _,
355 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
356 preedit_end_trampoline::<Self, F> as *const (),
357 )),
358 Box_::into_raw(f),
359 )
360 }
361 }
362
363 #[doc(alias = "preedit-start")]
364 fn connect_preedit_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
365 unsafe extern "C" fn preedit_start_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
366 this: *mut ffi::GtkIMContext,
367 f: glib::ffi::gpointer,
368 ) {
369 let f: &F = &*(f as *const F);
370 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
371 }
372 unsafe {
373 let f: Box_<F> = Box_::new(f);
374 connect_raw(
375 self.as_ptr() as *mut _,
376 c"preedit-start".as_ptr() as *const _,
377 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
378 preedit_start_trampoline::<Self, F> as *const (),
379 )),
380 Box_::into_raw(f),
381 )
382 }
383 }
384
385 #[doc(alias = "retrieve-surrounding")]
386 fn connect_retrieve_surrounding<F: Fn(&Self) -> bool + 'static>(
387 &self,
388 f: F,
389 ) -> SignalHandlerId {
390 unsafe extern "C" fn retrieve_surrounding_trampoline<
391 P: IsA<IMContext>,
392 F: Fn(&P) -> bool + 'static,
393 >(
394 this: *mut ffi::GtkIMContext,
395 f: glib::ffi::gpointer,
396 ) -> glib::ffi::gboolean {
397 let f: &F = &*(f as *const F);
398 f(IMContext::from_glib_borrow(this).unsafe_cast_ref()).into_glib()
399 }
400 unsafe {
401 let f: Box_<F> = Box_::new(f);
402 connect_raw(
403 self.as_ptr() as *mut _,
404 c"retrieve-surrounding".as_ptr() as *const _,
405 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
406 retrieve_surrounding_trampoline::<Self, F> as *const (),
407 )),
408 Box_::into_raw(f),
409 )
410 }
411 }
412
413 #[doc(alias = "input-hints")]
414 fn connect_input_hints_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
415 unsafe extern "C" fn notify_input_hints_trampoline<
416 P: IsA<IMContext>,
417 F: Fn(&P) + 'static,
418 >(
419 this: *mut ffi::GtkIMContext,
420 _param_spec: glib::ffi::gpointer,
421 f: glib::ffi::gpointer,
422 ) {
423 let f: &F = &*(f as *const F);
424 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
425 }
426 unsafe {
427 let f: Box_<F> = Box_::new(f);
428 connect_raw(
429 self.as_ptr() as *mut _,
430 c"notify::input-hints".as_ptr() as *const _,
431 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
432 notify_input_hints_trampoline::<Self, F> as *const (),
433 )),
434 Box_::into_raw(f),
435 )
436 }
437 }
438
439 #[doc(alias = "input-purpose")]
440 fn connect_input_purpose_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
441 unsafe extern "C" fn notify_input_purpose_trampoline<
442 P: IsA<IMContext>,
443 F: Fn(&P) + 'static,
444 >(
445 this: *mut ffi::GtkIMContext,
446 _param_spec: glib::ffi::gpointer,
447 f: glib::ffi::gpointer,
448 ) {
449 let f: &F = &*(f as *const F);
450 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
451 }
452 unsafe {
453 let f: Box_<F> = Box_::new(f);
454 connect_raw(
455 self.as_ptr() as *mut _,
456 c"notify::input-purpose".as_ptr() as *const _,
457 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
458 notify_input_purpose_trampoline::<Self, F> as *const (),
459 )),
460 Box_::into_raw(f),
461 )
462 }
463 }
464}
465
466impl<O: IsA<IMContext>> IMContextExt for O {}