1#[cfg(feature = "v2_32")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v2_32")))]
7use crate::ScrollType;
8use crate::{CoordType, TextBoundary, TextClipType, TextGranularity, TextRange, TextRectangle};
9use glib::{
10 prelude::*,
11 signal::{connect_raw, SignalHandlerId},
12 translate::*,
13};
14use std::{boxed::Box as Box_, fmt, mem, mem::transmute};
15
16glib::wrapper! {
17 #[doc(alias = "AtkText")]
18 pub struct Text(Interface<ffi::AtkText, ffi::AtkTextIface>);
19
20 match fn {
21 type_ => || ffi::atk_text_get_type(),
22 }
23}
24
25impl Text {
26 pub const NONE: Option<&'static Text> = None;
27}
28
29mod sealed {
30 pub trait Sealed {}
31 impl<T: super::IsA<super::Text>> Sealed for T {}
32}
33
34pub trait TextExt: IsA<Text> + sealed::Sealed + 'static {
35 #[doc(alias = "atk_text_add_selection")]
36 fn add_selection(&self, start_offset: i32, end_offset: i32) -> bool {
37 unsafe {
38 from_glib(ffi::atk_text_add_selection(
39 self.as_ref().to_glib_none().0,
40 start_offset,
41 end_offset,
42 ))
43 }
44 }
45
46 #[doc(alias = "atk_text_get_bounded_ranges")]
47 #[doc(alias = "get_bounded_ranges")]
48 fn bounded_ranges(
49 &self,
50 rect: &mut TextRectangle,
51 coord_type: CoordType,
52 x_clip_type: TextClipType,
53 y_clip_type: TextClipType,
54 ) -> Vec<TextRange> {
55 unsafe {
56 FromGlibPtrContainer::from_glib_full(ffi::atk_text_get_bounded_ranges(
57 self.as_ref().to_glib_none().0,
58 rect.to_glib_none_mut().0,
59 coord_type.into_glib(),
60 x_clip_type.into_glib(),
61 y_clip_type.into_glib(),
62 ))
63 }
64 }
65
66 #[doc(alias = "atk_text_get_caret_offset")]
67 #[doc(alias = "get_caret_offset")]
68 fn caret_offset(&self) -> i32 {
69 unsafe { ffi::atk_text_get_caret_offset(self.as_ref().to_glib_none().0) }
70 }
71
72 #[doc(alias = "atk_text_get_character_at_offset")]
73 #[doc(alias = "get_character_at_offset")]
74 fn character_at_offset(&self, offset: i32) -> char {
75 unsafe {
76 std::convert::TryFrom::try_from(ffi::atk_text_get_character_at_offset(
77 self.as_ref().to_glib_none().0,
78 offset,
79 ))
80 .expect("conversion from an invalid Unicode value attempted")
81 }
82 }
83
84 #[doc(alias = "atk_text_get_character_count")]
85 #[doc(alias = "get_character_count")]
86 fn character_count(&self) -> i32 {
87 unsafe { ffi::atk_text_get_character_count(self.as_ref().to_glib_none().0) }
88 }
89
90 #[doc(alias = "atk_text_get_character_extents")]
91 #[doc(alias = "get_character_extents")]
92 fn character_extents(&self, offset: i32, coords: CoordType) -> (i32, i32, i32, i32) {
93 unsafe {
94 let mut x = mem::MaybeUninit::uninit();
95 let mut y = mem::MaybeUninit::uninit();
96 let mut width = mem::MaybeUninit::uninit();
97 let mut height = mem::MaybeUninit::uninit();
98 ffi::atk_text_get_character_extents(
99 self.as_ref().to_glib_none().0,
100 offset,
101 x.as_mut_ptr(),
102 y.as_mut_ptr(),
103 width.as_mut_ptr(),
104 height.as_mut_ptr(),
105 coords.into_glib(),
106 );
107 (
108 x.assume_init(),
109 y.assume_init(),
110 width.assume_init(),
111 height.assume_init(),
112 )
113 }
114 }
115
116 #[doc(alias = "atk_text_get_n_selections")]
123 #[doc(alias = "get_n_selections")]
124 fn n_selections(&self) -> i32 {
125 unsafe { ffi::atk_text_get_n_selections(self.as_ref().to_glib_none().0) }
126 }
127
128 #[doc(alias = "atk_text_get_offset_at_point")]
129 #[doc(alias = "get_offset_at_point")]
130 fn offset_at_point(&self, x: i32, y: i32, coords: CoordType) -> i32 {
131 unsafe {
132 ffi::atk_text_get_offset_at_point(
133 self.as_ref().to_glib_none().0,
134 x,
135 y,
136 coords.into_glib(),
137 )
138 }
139 }
140
141 #[doc(alias = "atk_text_get_range_extents")]
142 #[doc(alias = "get_range_extents")]
143 fn range_extents(
144 &self,
145 start_offset: i32,
146 end_offset: i32,
147 coord_type: CoordType,
148 ) -> TextRectangle {
149 unsafe {
150 let mut rect = TextRectangle::uninitialized();
151 ffi::atk_text_get_range_extents(
152 self.as_ref().to_glib_none().0,
153 start_offset,
154 end_offset,
155 coord_type.into_glib(),
156 rect.to_glib_none_mut().0,
157 );
158 rect
159 }
160 }
161
162 #[doc(alias = "atk_text_get_selection")]
169 #[doc(alias = "get_selection")]
170 fn selection(&self, selection_num: i32) -> (glib::GString, i32, i32) {
171 unsafe {
172 let mut start_offset = mem::MaybeUninit::uninit();
173 let mut end_offset = mem::MaybeUninit::uninit();
174 let ret = from_glib_full(ffi::atk_text_get_selection(
175 self.as_ref().to_glib_none().0,
176 selection_num,
177 start_offset.as_mut_ptr(),
178 end_offset.as_mut_ptr(),
179 ));
180 (ret, start_offset.assume_init(), end_offset.assume_init())
181 }
182 }
183
184 #[doc(alias = "atk_text_get_string_at_offset")]
185 #[doc(alias = "get_string_at_offset")]
186 fn string_at_offset(
187 &self,
188 offset: i32,
189 granularity: TextGranularity,
190 ) -> (Option<glib::GString>, i32, i32) {
191 unsafe {
192 let mut start_offset = mem::MaybeUninit::uninit();
193 let mut end_offset = mem::MaybeUninit::uninit();
194 let ret = from_glib_full(ffi::atk_text_get_string_at_offset(
195 self.as_ref().to_glib_none().0,
196 offset,
197 granularity.into_glib(),
198 start_offset.as_mut_ptr(),
199 end_offset.as_mut_ptr(),
200 ));
201 (ret, start_offset.assume_init(), end_offset.assume_init())
202 }
203 }
204
205 #[doc(alias = "atk_text_get_text")]
206 #[doc(alias = "get_text")]
207 fn text(&self, start_offset: i32, end_offset: i32) -> Option<glib::GString> {
208 unsafe {
209 from_glib_full(ffi::atk_text_get_text(
210 self.as_ref().to_glib_none().0,
211 start_offset,
212 end_offset,
213 ))
214 }
215 }
216
217 #[doc(alias = "atk_text_get_text_at_offset")]
218 #[doc(alias = "get_text_at_offset")]
219 fn text_at_offset(
220 &self,
221 offset: i32,
222 boundary_type: TextBoundary,
223 ) -> (glib::GString, i32, i32) {
224 unsafe {
225 let mut start_offset = mem::MaybeUninit::uninit();
226 let mut end_offset = mem::MaybeUninit::uninit();
227 let ret = from_glib_full(ffi::atk_text_get_text_at_offset(
228 self.as_ref().to_glib_none().0,
229 offset,
230 boundary_type.into_glib(),
231 start_offset.as_mut_ptr(),
232 end_offset.as_mut_ptr(),
233 ));
234 (ret, start_offset.assume_init(), end_offset.assume_init())
235 }
236 }
237
238 #[doc(alias = "atk_text_remove_selection")]
239 fn remove_selection(&self, selection_num: i32) -> bool {
240 unsafe {
241 from_glib(ffi::atk_text_remove_selection(
242 self.as_ref().to_glib_none().0,
243 selection_num,
244 ))
245 }
246 }
247
248 #[cfg(feature = "v2_32")]
249 #[cfg_attr(docsrs, doc(cfg(feature = "v2_32")))]
250 #[doc(alias = "atk_text_scroll_substring_to")]
251 fn scroll_substring_to(&self, start_offset: i32, end_offset: i32, type_: ScrollType) -> bool {
252 unsafe {
253 from_glib(ffi::atk_text_scroll_substring_to(
254 self.as_ref().to_glib_none().0,
255 start_offset,
256 end_offset,
257 type_.into_glib(),
258 ))
259 }
260 }
261
262 #[cfg(feature = "v2_32")]
263 #[cfg_attr(docsrs, doc(cfg(feature = "v2_32")))]
264 #[doc(alias = "atk_text_scroll_substring_to_point")]
265 fn scroll_substring_to_point(
266 &self,
267 start_offset: i32,
268 end_offset: i32,
269 coords: CoordType,
270 x: i32,
271 y: i32,
272 ) -> bool {
273 unsafe {
274 from_glib(ffi::atk_text_scroll_substring_to_point(
275 self.as_ref().to_glib_none().0,
276 start_offset,
277 end_offset,
278 coords.into_glib(),
279 x,
280 y,
281 ))
282 }
283 }
284
285 #[doc(alias = "atk_text_set_caret_offset")]
286 fn set_caret_offset(&self, offset: i32) -> bool {
287 unsafe {
288 from_glib(ffi::atk_text_set_caret_offset(
289 self.as_ref().to_glib_none().0,
290 offset,
291 ))
292 }
293 }
294
295 #[doc(alias = "atk_text_set_selection")]
296 fn set_selection(&self, selection_num: i32, start_offset: i32, end_offset: i32) -> bool {
297 unsafe {
298 from_glib(ffi::atk_text_set_selection(
299 self.as_ref().to_glib_none().0,
300 selection_num,
301 start_offset,
302 end_offset,
303 ))
304 }
305 }
306
307 #[doc(alias = "text-attributes-changed")]
308 fn connect_text_attributes_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
309 unsafe extern "C" fn text_attributes_changed_trampoline<
310 P: IsA<Text>,
311 F: Fn(&P) + 'static,
312 >(
313 this: *mut ffi::AtkText,
314 f: glib::ffi::gpointer,
315 ) {
316 let f: &F = &*(f as *const F);
317 f(Text::from_glib_borrow(this).unsafe_cast_ref())
318 }
319 unsafe {
320 let f: Box_<F> = Box_::new(f);
321 connect_raw(
322 self.as_ptr() as *mut _,
323 b"text-attributes-changed\0".as_ptr() as *const _,
324 Some(transmute::<_, unsafe extern "C" fn()>(
325 text_attributes_changed_trampoline::<Self, F> as *const (),
326 )),
327 Box_::into_raw(f),
328 )
329 }
330 }
331
332 #[doc(alias = "text-caret-moved")]
333 fn connect_text_caret_moved<F: Fn(&Self, i32) + 'static>(&self, f: F) -> SignalHandlerId {
334 unsafe extern "C" fn text_caret_moved_trampoline<P: IsA<Text>, F: Fn(&P, i32) + 'static>(
335 this: *mut ffi::AtkText,
336 arg1: libc::c_int,
337 f: glib::ffi::gpointer,
338 ) {
339 let f: &F = &*(f as *const F);
340 f(Text::from_glib_borrow(this).unsafe_cast_ref(), arg1)
341 }
342 unsafe {
343 let f: Box_<F> = Box_::new(f);
344 connect_raw(
345 self.as_ptr() as *mut _,
346 b"text-caret-moved\0".as_ptr() as *const _,
347 Some(transmute::<_, unsafe extern "C" fn()>(
348 text_caret_moved_trampoline::<Self, F> as *const (),
349 )),
350 Box_::into_raw(f),
351 )
352 }
353 }
354
355 #[doc(alias = "text-insert")]
356 fn connect_text_insert<F: Fn(&Self, i32, i32, &str) + 'static>(
357 &self,
358 detail: Option<&str>,
359 f: F,
360 ) -> SignalHandlerId {
361 unsafe extern "C" fn text_insert_trampoline<
362 P: IsA<Text>,
363 F: Fn(&P, i32, i32, &str) + 'static,
364 >(
365 this: *mut ffi::AtkText,
366 arg1: libc::c_int,
367 arg2: libc::c_int,
368 arg3: *mut libc::c_char,
369 f: glib::ffi::gpointer,
370 ) {
371 let f: &F = &*(f as *const F);
372 f(
373 Text::from_glib_borrow(this).unsafe_cast_ref(),
374 arg1,
375 arg2,
376 &glib::GString::from_glib_borrow(arg3),
377 )
378 }
379 unsafe {
380 let f: Box_<F> = Box_::new(f);
381 let detailed_signal_name = detail.map(|name| format!("text-insert::{name}\0"));
382 let signal_name: &[u8] = detailed_signal_name
383 .as_ref()
384 .map_or(&b"text-insert\0"[..], |n| n.as_bytes());
385 connect_raw(
386 self.as_ptr() as *mut _,
387 signal_name.as_ptr() as *const _,
388 Some(transmute::<_, unsafe extern "C" fn()>(
389 text_insert_trampoline::<Self, F> as *const (),
390 )),
391 Box_::into_raw(f),
392 )
393 }
394 }
395
396 #[doc(alias = "text-remove")]
397 fn connect_text_remove<F: Fn(&Self, i32, i32, &str) + 'static>(
398 &self,
399 detail: Option<&str>,
400 f: F,
401 ) -> SignalHandlerId {
402 unsafe extern "C" fn text_remove_trampoline<
403 P: IsA<Text>,
404 F: Fn(&P, i32, i32, &str) + 'static,
405 >(
406 this: *mut ffi::AtkText,
407 arg1: libc::c_int,
408 arg2: libc::c_int,
409 arg3: *mut libc::c_char,
410 f: glib::ffi::gpointer,
411 ) {
412 let f: &F = &*(f as *const F);
413 f(
414 Text::from_glib_borrow(this).unsafe_cast_ref(),
415 arg1,
416 arg2,
417 &glib::GString::from_glib_borrow(arg3),
418 )
419 }
420 unsafe {
421 let f: Box_<F> = Box_::new(f);
422 let detailed_signal_name = detail.map(|name| format!("text-remove::{name}\0"));
423 let signal_name: &[u8] = detailed_signal_name
424 .as_ref()
425 .map_or(&b"text-remove\0"[..], |n| n.as_bytes());
426 connect_raw(
427 self.as_ptr() as *mut _,
428 signal_name.as_ptr() as *const _,
429 Some(transmute::<_, unsafe extern "C" fn()>(
430 text_remove_trampoline::<Self, F> as *const (),
431 )),
432 Box_::into_raw(f),
433 )
434 }
435 }
436
437 #[doc(alias = "text-selection-changed")]
438 fn connect_text_selection_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
439 unsafe extern "C" fn text_selection_changed_trampoline<
440 P: IsA<Text>,
441 F: Fn(&P) + 'static,
442 >(
443 this: *mut ffi::AtkText,
444 f: glib::ffi::gpointer,
445 ) {
446 let f: &F = &*(f as *const F);
447 f(Text::from_glib_borrow(this).unsafe_cast_ref())
448 }
449 unsafe {
450 let f: Box_<F> = Box_::new(f);
451 connect_raw(
452 self.as_ptr() as *mut _,
453 b"text-selection-changed\0".as_ptr() as *const _,
454 Some(transmute::<_, unsafe extern "C" fn()>(
455 text_selection_changed_trampoline::<Self, F> as *const (),
456 )),
457 Box_::into_raw(f),
458 )
459 }
460 }
461}
462
463impl<O: IsA<Text>> TextExt for O {}
464
465impl fmt::Display for Text {
466 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
467 f.write_str("Text")
468 }
469}