1#![allow(deprecated)]
5
6use crate::{InputHints, InputPurpose, 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 #[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 unsafe {
266 let f: &F = &*(f as *const F);
267 f(
268 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
269 &glib::GString::from_glib_borrow(str),
270 )
271 }
272 }
273 unsafe {
274 let f: Box_<F> = Box_::new(f);
275 connect_raw(
276 self.as_ptr() as *mut _,
277 c"commit".as_ptr(),
278 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
279 commit_trampoline::<Self, F> as *const (),
280 )),
281 Box_::into_raw(f),
282 )
283 }
284 }
285
286 #[doc(alias = "delete-surrounding")]
287 fn connect_delete_surrounding<F: Fn(&Self, i32, i32) -> bool + 'static>(
288 &self,
289 f: F,
290 ) -> SignalHandlerId {
291 unsafe extern "C" fn delete_surrounding_trampoline<
292 P: IsA<IMContext>,
293 F: Fn(&P, i32, i32) -> bool + 'static,
294 >(
295 this: *mut ffi::GtkIMContext,
296 offset: std::ffi::c_int,
297 n_chars: std::ffi::c_int,
298 f: glib::ffi::gpointer,
299 ) -> glib::ffi::gboolean {
300 unsafe {
301 let f: &F = &*(f as *const F);
302 f(
303 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
304 offset,
305 n_chars,
306 )
307 .into_glib()
308 }
309 }
310 unsafe {
311 let f: Box_<F> = Box_::new(f);
312 connect_raw(
313 self.as_ptr() as *mut _,
314 c"delete-surrounding".as_ptr(),
315 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
316 delete_surrounding_trampoline::<Self, F> as *const (),
317 )),
318 Box_::into_raw(f),
319 )
320 }
321 }
322
323 #[cfg(feature = "v4_22")]
324 #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
325 #[doc(alias = "invalid-composition")]
326 fn connect_invalid_composition<F: Fn(&Self, &str) -> bool + 'static>(
327 &self,
328 f: F,
329 ) -> SignalHandlerId {
330 unsafe extern "C" fn invalid_composition_trampoline<
331 P: IsA<IMContext>,
332 F: Fn(&P, &str) -> bool + 'static,
333 >(
334 this: *mut ffi::GtkIMContext,
335 str: *mut std::ffi::c_char,
336 f: glib::ffi::gpointer,
337 ) -> glib::ffi::gboolean {
338 unsafe {
339 let f: &F = &*(f as *const F);
340 f(
341 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
342 &glib::GString::from_glib_borrow(str),
343 )
344 .into_glib()
345 }
346 }
347 unsafe {
348 let f: Box_<F> = Box_::new(f);
349 connect_raw(
350 self.as_ptr() as *mut _,
351 c"invalid-composition".as_ptr(),
352 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
353 invalid_composition_trampoline::<Self, F> as *const (),
354 )),
355 Box_::into_raw(f),
356 )
357 }
358 }
359
360 #[doc(alias = "preedit-changed")]
361 fn connect_preedit_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
362 unsafe extern "C" fn preedit_changed_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
363 this: *mut ffi::GtkIMContext,
364 f: glib::ffi::gpointer,
365 ) {
366 unsafe {
367 let f: &F = &*(f as *const F);
368 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
369 }
370 }
371 unsafe {
372 let f: Box_<F> = Box_::new(f);
373 connect_raw(
374 self.as_ptr() as *mut _,
375 c"preedit-changed".as_ptr(),
376 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
377 preedit_changed_trampoline::<Self, F> as *const (),
378 )),
379 Box_::into_raw(f),
380 )
381 }
382 }
383
384 #[doc(alias = "preedit-end")]
385 fn connect_preedit_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
386 unsafe extern "C" fn preedit_end_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
387 this: *mut ffi::GtkIMContext,
388 f: glib::ffi::gpointer,
389 ) {
390 unsafe {
391 let f: &F = &*(f as *const F);
392 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
393 }
394 }
395 unsafe {
396 let f: Box_<F> = Box_::new(f);
397 connect_raw(
398 self.as_ptr() as *mut _,
399 c"preedit-end".as_ptr(),
400 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
401 preedit_end_trampoline::<Self, F> as *const (),
402 )),
403 Box_::into_raw(f),
404 )
405 }
406 }
407
408 #[doc(alias = "preedit-start")]
409 fn connect_preedit_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
410 unsafe extern "C" fn preedit_start_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
411 this: *mut ffi::GtkIMContext,
412 f: glib::ffi::gpointer,
413 ) {
414 unsafe {
415 let f: &F = &*(f as *const F);
416 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
417 }
418 }
419 unsafe {
420 let f: Box_<F> = Box_::new(f);
421 connect_raw(
422 self.as_ptr() as *mut _,
423 c"preedit-start".as_ptr(),
424 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
425 preedit_start_trampoline::<Self, F> as *const (),
426 )),
427 Box_::into_raw(f),
428 )
429 }
430 }
431
432 #[doc(alias = "retrieve-surrounding")]
433 fn connect_retrieve_surrounding<F: Fn(&Self) -> bool + 'static>(
434 &self,
435 f: F,
436 ) -> SignalHandlerId {
437 unsafe extern "C" fn retrieve_surrounding_trampoline<
438 P: IsA<IMContext>,
439 F: Fn(&P) -> bool + 'static,
440 >(
441 this: *mut ffi::GtkIMContext,
442 f: glib::ffi::gpointer,
443 ) -> glib::ffi::gboolean {
444 unsafe {
445 let f: &F = &*(f as *const F);
446 f(IMContext::from_glib_borrow(this).unsafe_cast_ref()).into_glib()
447 }
448 }
449 unsafe {
450 let f: Box_<F> = Box_::new(f);
451 connect_raw(
452 self.as_ptr() as *mut _,
453 c"retrieve-surrounding".as_ptr(),
454 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
455 retrieve_surrounding_trampoline::<Self, F> as *const (),
456 )),
457 Box_::into_raw(f),
458 )
459 }
460 }
461
462 #[doc(alias = "input-hints")]
463 fn connect_input_hints_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
464 unsafe extern "C" fn notify_input_hints_trampoline<
465 P: IsA<IMContext>,
466 F: Fn(&P) + 'static,
467 >(
468 this: *mut ffi::GtkIMContext,
469 _param_spec: glib::ffi::gpointer,
470 f: glib::ffi::gpointer,
471 ) {
472 unsafe {
473 let f: &F = &*(f as *const F);
474 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
475 }
476 }
477 unsafe {
478 let f: Box_<F> = Box_::new(f);
479 connect_raw(
480 self.as_ptr() as *mut _,
481 c"notify::input-hints".as_ptr(),
482 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
483 notify_input_hints_trampoline::<Self, F> as *const (),
484 )),
485 Box_::into_raw(f),
486 )
487 }
488 }
489
490 #[doc(alias = "input-purpose")]
491 fn connect_input_purpose_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
492 unsafe extern "C" fn notify_input_purpose_trampoline<
493 P: IsA<IMContext>,
494 F: Fn(&P) + 'static,
495 >(
496 this: *mut ffi::GtkIMContext,
497 _param_spec: glib::ffi::gpointer,
498 f: glib::ffi::gpointer,
499 ) {
500 unsafe {
501 let f: &F = &*(f as *const F);
502 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
503 }
504 }
505 unsafe {
506 let f: Box_<F> = Box_::new(f);
507 connect_raw(
508 self.as_ptr() as *mut _,
509 c"notify::input-purpose".as_ptr(),
510 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
511 notify_input_purpose_trampoline::<Self, F> as *const (),
512 )),
513 Box_::into_raw(f),
514 )
515 }
516 }
517}
518
519impl<O: IsA<IMContext>> IMContextExt for O {}