gtk/auto/im_context.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::{InputHints, InputPurpose, 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 ///
16 /// GtkIMContext * im_module_create(const gchar *context_id);
17 /// ]|
18 /// This function should return a pointer to a newly created instance of the
19 /// [`IMContext`][crate::IMContext] subclass identified by `context_id`. The context ID is the same
20 /// as specified in the `GtkIMContextInfo` array returned by `im_module_list()`.
21 ///
22 /// After a new loadable input method module has been installed on the system,
23 /// the configuration file `gtk.immodules` needs to be
24 /// regenerated by [gtk-query-immodules-3.0][gtk-query-immodules-3.0],
25 /// in order for the new input method to become available to GTK+ applications.
26 ///
27 /// This is an Abstract Base Class, you cannot instantiate it.
28 ///
29 /// ## Properties
30 ///
31 ///
32 /// #### `input-hints`
33 /// Readable | Writable
34 ///
35 ///
36 /// #### `input-purpose`
37 /// Readable | Writable
38 ///
39 /// ## Signals
40 ///
41 ///
42 /// #### `commit`
43 /// The ::commit signal is emitted when a complete input sequence
44 /// has been entered by the user. This can be a single character
45 /// immediately after a key press or the final result of preediting.
46 ///
47 ///
48 ///
49 ///
50 /// #### `delete-surrounding`
51 /// The ::delete-surrounding signal is emitted when the input method
52 /// needs to delete all or part of the context surrounding the cursor.
53 ///
54 ///
55 ///
56 ///
57 /// #### `preedit-changed`
58 /// The ::preedit-changed signal is emitted whenever the preedit sequence
59 /// currently being entered has changed. It is also emitted at the end of
60 /// a preedit sequence, in which case
61 /// [`IMContextExt::preedit_string()`][crate::prelude::IMContextExt::preedit_string()] returns the empty string.
62 ///
63 ///
64 ///
65 ///
66 /// #### `preedit-end`
67 /// The ::preedit-end signal is emitted when a preediting sequence
68 /// has been completed or canceled.
69 ///
70 ///
71 ///
72 ///
73 /// #### `preedit-start`
74 /// The ::preedit-start signal is emitted when a new preediting sequence
75 /// starts.
76 ///
77 ///
78 ///
79 ///
80 /// #### `retrieve-surrounding`
81 /// The ::retrieve-surrounding signal is emitted when the input method
82 /// requires the context surrounding the cursor. The callback should set
83 /// the input method surrounding context by calling the
84 /// [`IMContextExt::set_surrounding()`][crate::prelude::IMContextExt::set_surrounding()] method.
85 ///
86 ///
87 ///
88 /// # Implements
89 ///
90 /// [`IMContextExt`][trait@crate::prelude::IMContextExt], [`trait@glib::ObjectExt`]
91 #[doc(alias = "GtkIMContext")]
92 pub struct IMContext(Object<ffi::GtkIMContext, ffi::GtkIMContextClass>);
93
94 match fn {
95 type_ => || ffi::gtk_im_context_get_type(),
96 }
97}
98
99impl IMContext {
100 pub const NONE: Option<&'static IMContext> = None;
101}
102
103/// Trait containing all [`struct@IMContext`] methods.
104///
105/// # Implementors
106///
107/// [`IMContextSimple`][struct@crate::IMContextSimple], [`IMContext`][struct@crate::IMContext], [`IMMulticontext`][struct@crate::IMMulticontext]
108pub trait IMContextExt: IsA<IMContext> + 'static {
109 /// Asks the widget that the input context is attached to to delete
110 /// characters around the cursor position by emitting the
111 /// GtkIMContext::delete_surrounding signal. Note that `offset` and `n_chars`
112 /// are in characters not in bytes which differs from the usage other
113 /// places in [`IMContext`][crate::IMContext].
114 ///
115 /// In order to use this function, you should first call
116 /// [`surrounding()`][Self::surrounding()] to get the current context, and
117 /// call this function immediately afterwards to make sure that you
118 /// know what you are deleting. You should also account for the fact
119 /// that even if the signal was handled, the input context might not
120 /// have deleted all the characters that were requested to be deleted.
121 ///
122 /// This function is used by an input method that wants to make
123 /// subsitutions in the existing text in response to new input. It is
124 /// not useful for applications.
125 /// ## `offset`
126 /// offset from cursor position in chars;
127 /// a negative value means start before the cursor.
128 /// ## `n_chars`
129 /// number of characters to delete.
130 ///
131 /// # Returns
132 ///
133 /// [`true`] if the signal was handled.
134 #[doc(alias = "gtk_im_context_delete_surrounding")]
135 fn delete_surrounding(&self, offset: i32, n_chars: i32) -> bool {
136 unsafe {
137 from_glib(ffi::gtk_im_context_delete_surrounding(
138 self.as_ref().to_glib_none().0,
139 offset,
140 n_chars,
141 ))
142 }
143 }
144
145 /// Allow an input method to internally handle key press and release
146 /// events. If this function returns [`true`], then no further processing
147 /// should be done for this key event.
148 /// ## `event`
149 /// the key event
150 ///
151 /// # Returns
152 ///
153 /// [`true`] if the input method handled the key event.
154 #[doc(alias = "gtk_im_context_filter_keypress")]
155 fn filter_keypress(&self, event: &gdk::EventKey) -> bool {
156 unsafe {
157 from_glib(ffi::gtk_im_context_filter_keypress(
158 self.as_ref().to_glib_none().0,
159 mut_override(event.to_glib_none().0),
160 ))
161 }
162 }
163
164 /// Notify the input method that the widget to which this
165 /// input context corresponds has gained focus. The input method
166 /// may, for example, change the displayed feedback to reflect
167 /// this change.
168 #[doc(alias = "gtk_im_context_focus_in")]
169 fn focus_in(&self) {
170 unsafe {
171 ffi::gtk_im_context_focus_in(self.as_ref().to_glib_none().0);
172 }
173 }
174
175 /// Notify the input method that the widget to which this
176 /// input context corresponds has lost focus. The input method
177 /// may, for example, change the displayed feedback or reset the contexts
178 /// state to reflect this change.
179 #[doc(alias = "gtk_im_context_focus_out")]
180 fn focus_out(&self) {
181 unsafe {
182 ffi::gtk_im_context_focus_out(self.as_ref().to_glib_none().0);
183 }
184 }
185
186 /// Retrieve the current preedit string for the input context,
187 /// and a list of attributes to apply to the string.
188 /// This string should be displayed inserted at the insertion
189 /// point.
190 ///
191 /// # Returns
192 ///
193 ///
194 /// ## `str`
195 /// location to store the retrieved
196 /// string. The string retrieved must be freed with `g_free()`.
197 ///
198 /// ## `attrs`
199 /// location to store the retrieved
200 /// attribute list. When you are done with this list, you
201 /// must unreference it with `pango_attr_list_unref()`.
202 ///
203 /// ## `cursor_pos`
204 /// location to store position of cursor (in characters)
205 /// within the preedit string.
206 #[doc(alias = "gtk_im_context_get_preedit_string")]
207 #[doc(alias = "get_preedit_string")]
208 fn preedit_string(&self) -> (glib::GString, pango::AttrList, i32) {
209 unsafe {
210 let mut str = std::ptr::null_mut();
211 let mut attrs = std::ptr::null_mut();
212 let mut cursor_pos = std::mem::MaybeUninit::uninit();
213 ffi::gtk_im_context_get_preedit_string(
214 self.as_ref().to_glib_none().0,
215 &mut str,
216 &mut attrs,
217 cursor_pos.as_mut_ptr(),
218 );
219 (
220 from_glib_full(str),
221 from_glib_full(attrs),
222 cursor_pos.assume_init(),
223 )
224 }
225 }
226
227 /// Retrieves context around the insertion point. Input methods
228 /// typically want context in order to constrain input text based on
229 /// existing text; this is important for languages such as Thai where
230 /// only some sequences of characters are allowed.
231 ///
232 /// This function is implemented by emitting the
233 /// GtkIMContext::retrieve_surrounding signal on the input method; in
234 /// response to this signal, a widget should provide as much context as
235 /// is available, up to an entire paragraph, by calling
236 /// [`set_surrounding()`][Self::set_surrounding()]. Note that there is no obligation
237 /// for a widget to respond to the ::retrieve_surrounding signal, so input
238 /// methods must be prepared to function without context.
239 ///
240 /// # Returns
241 ///
242 /// [`true`] if surrounding text was provided; in this case
243 /// you must free the result stored in *text.
244 ///
245 /// ## `text`
246 /// location to store a UTF-8 encoded
247 /// string of text holding context around the insertion point.
248 /// If the function returns [`true`], then you must free the result
249 /// stored in this location with `g_free()`.
250 ///
251 /// ## `cursor_index`
252 /// location to store byte index of the insertion
253 /// cursor within `text`.
254 #[doc(alias = "gtk_im_context_get_surrounding")]
255 #[doc(alias = "get_surrounding")]
256 fn surrounding(&self) -> Option<(glib::GString, i32)> {
257 unsafe {
258 let mut text = std::ptr::null_mut();
259 let mut cursor_index = std::mem::MaybeUninit::uninit();
260 let ret = from_glib(ffi::gtk_im_context_get_surrounding(
261 self.as_ref().to_glib_none().0,
262 &mut text,
263 cursor_index.as_mut_ptr(),
264 ));
265 if ret {
266 Some((from_glib_full(text), cursor_index.assume_init()))
267 } else {
268 None
269 }
270 }
271 }
272
273 /// Notify the input method that a change such as a change in cursor
274 /// position has been made. This will typically cause the input
275 /// method to clear the preedit state.
276 #[doc(alias = "gtk_im_context_reset")]
277 fn reset(&self) {
278 unsafe {
279 ffi::gtk_im_context_reset(self.as_ref().to_glib_none().0);
280 }
281 }
282
283 /// Set the client window for the input context; this is the
284 /// [`gdk::Window`][crate::gdk::Window] in which the input appears. This window is
285 /// used in order to correctly position status windows, and may
286 /// also be used for purposes internal to the input method.
287 /// ## `window`
288 /// the client window. This may be [`None`] to indicate
289 /// that the previous client window no longer exists.
290 #[doc(alias = "gtk_im_context_set_client_window")]
291 fn set_client_window(&self, window: Option<&gdk::Window>) {
292 unsafe {
293 ffi::gtk_im_context_set_client_window(
294 self.as_ref().to_glib_none().0,
295 window.to_glib_none().0,
296 );
297 }
298 }
299
300 /// Notify the input method that a change in cursor
301 /// position has been made. The location is relative to the client
302 /// window.
303 /// ## `area`
304 /// new location
305 #[doc(alias = "gtk_im_context_set_cursor_location")]
306 fn set_cursor_location(&self, area: &gdk::Rectangle) {
307 unsafe {
308 ffi::gtk_im_context_set_cursor_location(
309 self.as_ref().to_glib_none().0,
310 area.to_glib_none().0,
311 );
312 }
313 }
314
315 /// Sets surrounding context around the insertion point and preedit
316 /// string. This function is expected to be called in response to the
317 /// GtkIMContext::retrieve_surrounding signal, and will likely have no
318 /// effect if called at other times.
319 /// ## `text`
320 /// text surrounding the insertion point, as UTF-8.
321 /// the preedit string should not be included within
322 /// `text`.
323 /// ## `len`
324 /// the length of `text`, or -1 if `text` is nul-terminated
325 /// ## `cursor_index`
326 /// the byte index of the insertion cursor within `text`.
327 #[doc(alias = "gtk_im_context_set_surrounding")]
328 fn set_surrounding(&self, text: &str, cursor_index: i32) {
329 let len = text.len() as _;
330 unsafe {
331 ffi::gtk_im_context_set_surrounding(
332 self.as_ref().to_glib_none().0,
333 text.to_glib_none().0,
334 len,
335 cursor_index,
336 );
337 }
338 }
339
340 /// Sets whether the IM context should use the preedit string
341 /// to display feedback. If `use_preedit` is FALSE (default
342 /// is TRUE), then the IM context may use some other method to display
343 /// feedback, such as displaying it in a child of the root window.
344 /// ## `use_preedit`
345 /// whether the IM context should use the preedit string.
346 #[doc(alias = "gtk_im_context_set_use_preedit")]
347 fn set_use_preedit(&self, use_preedit: bool) {
348 unsafe {
349 ffi::gtk_im_context_set_use_preedit(
350 self.as_ref().to_glib_none().0,
351 use_preedit.into_glib(),
352 );
353 }
354 }
355
356 #[doc(alias = "input-hints")]
357 fn input_hints(&self) -> InputHints {
358 ObjectExt::property(self.as_ref(), "input-hints")
359 }
360
361 #[doc(alias = "input-hints")]
362 fn set_input_hints(&self, input_hints: InputHints) {
363 ObjectExt::set_property(self.as_ref(), "input-hints", input_hints)
364 }
365
366 #[doc(alias = "input-purpose")]
367 fn input_purpose(&self) -> InputPurpose {
368 ObjectExt::property(self.as_ref(), "input-purpose")
369 }
370
371 #[doc(alias = "input-purpose")]
372 fn set_input_purpose(&self, input_purpose: InputPurpose) {
373 ObjectExt::set_property(self.as_ref(), "input-purpose", input_purpose)
374 }
375
376 /// The ::commit signal is emitted when a complete input sequence
377 /// has been entered by the user. This can be a single character
378 /// immediately after a key press or the final result of preediting.
379 /// ## `str`
380 /// the completed character(s) entered by the user
381 #[doc(alias = "commit")]
382 fn connect_commit<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId {
383 unsafe extern "C" fn commit_trampoline<P: IsA<IMContext>, F: Fn(&P, &str) + 'static>(
384 this: *mut ffi::GtkIMContext,
385 str: *mut std::ffi::c_char,
386 f: glib::ffi::gpointer,
387 ) {
388 unsafe {
389 let f: &F = &*(f as *const F);
390 f(
391 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
392 &glib::GString::from_glib_borrow(str),
393 )
394 }
395 }
396 unsafe {
397 let f: Box_<F> = Box_::new(f);
398 connect_raw(
399 self.as_ptr() as *mut _,
400 c"commit".as_ptr(),
401 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
402 commit_trampoline::<Self, F> as *const (),
403 )),
404 Box_::into_raw(f),
405 )
406 }
407 }
408
409 /// The ::delete-surrounding signal is emitted when the input method
410 /// needs to delete all or part of the context surrounding the cursor.
411 /// ## `offset`
412 /// the character offset from the cursor position of the text
413 /// to be deleted. A negative value indicates a position before
414 /// the cursor.
415 /// ## `n_chars`
416 /// the number of characters to be deleted
417 ///
418 /// # Returns
419 ///
420 /// [`true`] if the signal was handled.
421 #[doc(alias = "delete-surrounding")]
422 fn connect_delete_surrounding<F: Fn(&Self, i32, i32) -> bool + 'static>(
423 &self,
424 f: F,
425 ) -> SignalHandlerId {
426 unsafe extern "C" fn delete_surrounding_trampoline<
427 P: IsA<IMContext>,
428 F: Fn(&P, i32, i32) -> bool + 'static,
429 >(
430 this: *mut ffi::GtkIMContext,
431 offset: std::ffi::c_int,
432 n_chars: std::ffi::c_int,
433 f: glib::ffi::gpointer,
434 ) -> glib::ffi::gboolean {
435 unsafe {
436 let f: &F = &*(f as *const F);
437 f(
438 IMContext::from_glib_borrow(this).unsafe_cast_ref(),
439 offset,
440 n_chars,
441 )
442 .into_glib()
443 }
444 }
445 unsafe {
446 let f: Box_<F> = Box_::new(f);
447 connect_raw(
448 self.as_ptr() as *mut _,
449 c"delete-surrounding".as_ptr(),
450 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
451 delete_surrounding_trampoline::<Self, F> as *const (),
452 )),
453 Box_::into_raw(f),
454 )
455 }
456 }
457
458 /// The ::preedit-changed signal is emitted whenever the preedit sequence
459 /// currently being entered has changed. It is also emitted at the end of
460 /// a preedit sequence, in which case
461 /// [`preedit_string()`][Self::preedit_string()] returns the empty string.
462 #[doc(alias = "preedit-changed")]
463 fn connect_preedit_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
464 unsafe extern "C" fn preedit_changed_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
465 this: *mut ffi::GtkIMContext,
466 f: glib::ffi::gpointer,
467 ) {
468 unsafe {
469 let f: &F = &*(f as *const F);
470 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
471 }
472 }
473 unsafe {
474 let f: Box_<F> = Box_::new(f);
475 connect_raw(
476 self.as_ptr() as *mut _,
477 c"preedit-changed".as_ptr(),
478 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
479 preedit_changed_trampoline::<Self, F> as *const (),
480 )),
481 Box_::into_raw(f),
482 )
483 }
484 }
485
486 /// The ::preedit-end signal is emitted when a preediting sequence
487 /// has been completed or canceled.
488 #[doc(alias = "preedit-end")]
489 fn connect_preedit_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
490 unsafe extern "C" fn preedit_end_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
491 this: *mut ffi::GtkIMContext,
492 f: glib::ffi::gpointer,
493 ) {
494 unsafe {
495 let f: &F = &*(f as *const F);
496 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
497 }
498 }
499 unsafe {
500 let f: Box_<F> = Box_::new(f);
501 connect_raw(
502 self.as_ptr() as *mut _,
503 c"preedit-end".as_ptr(),
504 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
505 preedit_end_trampoline::<Self, F> as *const (),
506 )),
507 Box_::into_raw(f),
508 )
509 }
510 }
511
512 /// The ::preedit-start signal is emitted when a new preediting sequence
513 /// starts.
514 #[doc(alias = "preedit-start")]
515 fn connect_preedit_start<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
516 unsafe extern "C" fn preedit_start_trampoline<P: IsA<IMContext>, F: Fn(&P) + 'static>(
517 this: *mut ffi::GtkIMContext,
518 f: glib::ffi::gpointer,
519 ) {
520 unsafe {
521 let f: &F = &*(f as *const F);
522 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
523 }
524 }
525 unsafe {
526 let f: Box_<F> = Box_::new(f);
527 connect_raw(
528 self.as_ptr() as *mut _,
529 c"preedit-start".as_ptr(),
530 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
531 preedit_start_trampoline::<Self, F> as *const (),
532 )),
533 Box_::into_raw(f),
534 )
535 }
536 }
537
538 /// The ::retrieve-surrounding signal is emitted when the input method
539 /// requires the context surrounding the cursor. The callback should set
540 /// the input method surrounding context by calling the
541 /// [`set_surrounding()`][Self::set_surrounding()] method.
542 ///
543 /// # Returns
544 ///
545 /// [`true`] if the signal was handled.
546 #[doc(alias = "retrieve-surrounding")]
547 fn connect_retrieve_surrounding<F: Fn(&Self) -> bool + 'static>(
548 &self,
549 f: F,
550 ) -> SignalHandlerId {
551 unsafe extern "C" fn retrieve_surrounding_trampoline<
552 P: IsA<IMContext>,
553 F: Fn(&P) -> bool + 'static,
554 >(
555 this: *mut ffi::GtkIMContext,
556 f: glib::ffi::gpointer,
557 ) -> glib::ffi::gboolean {
558 unsafe {
559 let f: &F = &*(f as *const F);
560 f(IMContext::from_glib_borrow(this).unsafe_cast_ref()).into_glib()
561 }
562 }
563 unsafe {
564 let f: Box_<F> = Box_::new(f);
565 connect_raw(
566 self.as_ptr() as *mut _,
567 c"retrieve-surrounding".as_ptr(),
568 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
569 retrieve_surrounding_trampoline::<Self, F> as *const (),
570 )),
571 Box_::into_raw(f),
572 )
573 }
574 }
575
576 #[doc(alias = "input-hints")]
577 fn connect_input_hints_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
578 unsafe extern "C" fn notify_input_hints_trampoline<
579 P: IsA<IMContext>,
580 F: Fn(&P) + 'static,
581 >(
582 this: *mut ffi::GtkIMContext,
583 _param_spec: glib::ffi::gpointer,
584 f: glib::ffi::gpointer,
585 ) {
586 unsafe {
587 let f: &F = &*(f as *const F);
588 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
589 }
590 }
591 unsafe {
592 let f: Box_<F> = Box_::new(f);
593 connect_raw(
594 self.as_ptr() as *mut _,
595 c"notify::input-hints".as_ptr(),
596 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
597 notify_input_hints_trampoline::<Self, F> as *const (),
598 )),
599 Box_::into_raw(f),
600 )
601 }
602 }
603
604 #[doc(alias = "input-purpose")]
605 fn connect_input_purpose_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
606 unsafe extern "C" fn notify_input_purpose_trampoline<
607 P: IsA<IMContext>,
608 F: Fn(&P) + 'static,
609 >(
610 this: *mut ffi::GtkIMContext,
611 _param_spec: glib::ffi::gpointer,
612 f: glib::ffi::gpointer,
613 ) {
614 unsafe {
615 let f: &F = &*(f as *const F);
616 f(IMContext::from_glib_borrow(this).unsafe_cast_ref())
617 }
618 }
619 unsafe {
620 let f: Box_<F> = Box_::new(f);
621 connect_raw(
622 self.as_ptr() as *mut _,
623 c"notify::input-purpose".as_ptr(),
624 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
625 notify_input_purpose_trampoline::<Self, F> as *const (),
626 )),
627 Box_::into_raw(f),
628 )
629 }
630 }
631}
632
633impl<O: IsA<IMContext>> IMContextExt for O {}