1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::CoordType;
#[cfg(any(feature = "v2_32", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
use crate::ScrollType;
use crate::TextBoundary;
use crate::TextClipType;
use crate::TextGranularity;
use crate::TextRange;
use crate::TextRectangle;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem;
use std::mem::transmute;

glib::wrapper! {
    /// [`Text`][crate::Text] should be implemented by `AtkObjects` on behalf of widgets
    /// that have text content which is either attributed or otherwise
    /// non-trivial. `AtkObjects` whose text content is simple,
    /// unattributed, and very brief may expose that content via
    /// `atk_object_get_name` instead; however if the text is editable,
    /// multi-line, typically longer than three or four words, attributed,
    /// selectable, or if the object already uses the 'name' ATK property
    /// for other information, the [`Text`][crate::Text] interface should be used to
    /// expose the text content. In the case of editable text content,
    /// [`EditableText`][crate::EditableText] (a subtype of the [`Text`][crate::Text] interface) should be
    /// implemented instead.
    ///
    ///  [`Text`][crate::Text] provides not only traversal facilities and change
    /// notification for text content, but also caret tracking and glyph
    /// bounding box calculations. Note that the text strings are exposed
    /// as UTF-8, and are therefore potentially multi-byte, and
    /// caret-to-byte offset mapping makes no assumptions about the
    /// character length; also bounding box glyph-to-offset mapping may be
    /// complex for languages which use ligatures.
    ///
    /// # Implements
    ///
    /// [`TextExt`][trait@crate::prelude::TextExt]
    #[doc(alias = "AtkText")]
    pub struct Text(Interface<ffi::AtkText, ffi::AtkTextIface>);

    match fn {
        type_ => || ffi::atk_text_get_type(),
    }
}

impl Text {
    pub const NONE: Option<&'static Text> = None;
}

/// Trait containing all [`struct@Text`] methods.
///
/// # Implementors
///
/// [`NoOpObject`][struct@crate::NoOpObject], [`Text`][struct@crate::Text]
pub trait TextExt: 'static {
    /// Adds a selection bounded by the specified offsets.
    /// ## `start_offset`
    /// the starting character offset of the selected region
    /// ## `end_offset`
    /// the offset of the first character after the selected region.
    ///
    /// # Returns
    ///
    /// [`true`] if successful, [`false`] otherwise
    #[doc(alias = "atk_text_add_selection")]
    fn add_selection(&self, start_offset: i32, end_offset: i32) -> bool;

    /// Get the ranges of text in the specified bounding box.
    /// ## `rect`
    /// An AtkTextRectangle giving the dimensions of the bounding box.
    /// ## `coord_type`
    /// Specify whether coordinates are relative to the screen or widget window.
    /// ## `x_clip_type`
    /// Specify the horizontal clip type.
    /// ## `y_clip_type`
    /// Specify the vertical clip type.
    ///
    /// # Returns
    ///
    /// Array of AtkTextRange. The last
    ///  element of the array returned by this function will be NULL.
    #[doc(alias = "atk_text_get_bounded_ranges")]
    #[doc(alias = "get_bounded_ranges")]
    fn bounded_ranges(
        &self,
        rect: &mut TextRectangle,
        coord_type: CoordType,
        x_clip_type: TextClipType,
        y_clip_type: TextClipType,
    ) -> Vec<TextRange>;

    /// Gets the offset of the position of the caret (cursor).
    ///
    /// # Returns
    ///
    /// the character offset of the position of the caret or -1 if
    ///  the caret is not located inside the element or in the case of
    ///  any other failure.
    #[doc(alias = "atk_text_get_caret_offset")]
    #[doc(alias = "get_caret_offset")]
    fn caret_offset(&self) -> i32;

    /// Gets the specified text.
    /// ## `offset`
    /// a character offset within `self`
    ///
    /// # Returns
    ///
    /// the character at `offset` or 0 in the case of failure.
    #[doc(alias = "atk_text_get_character_at_offset")]
    #[doc(alias = "get_character_at_offset")]
    fn character_at_offset(&self, offset: i32) -> char;

    /// Gets the character count.
    ///
    /// # Returns
    ///
    /// the number of characters or -1 in case of failure.
    #[doc(alias = "atk_text_get_character_count")]
    #[doc(alias = "get_character_count")]
    fn character_count(&self) -> i32;

    /// If the extent can not be obtained (e.g. missing support), all of x, y, width,
    /// height are set to -1.
    ///
    /// Get the bounding box containing the glyph representing the character at
    ///  a particular text offset.
    /// ## `offset`
    /// The offset of the text character for which bounding information is required.
    /// ## `coords`
    /// specify whether coordinates are relative to the screen or widget window
    ///
    /// # Returns
    ///
    ///
    /// ## `x`
    /// Pointer for the x coordinate of the bounding box
    ///
    /// ## `y`
    /// Pointer for the y coordinate of the bounding box
    ///
    /// ## `width`
    /// Pointer for the width of the bounding box
    ///
    /// ## `height`
    /// Pointer for the height of the bounding box
    #[doc(alias = "atk_text_get_character_extents")]
    #[doc(alias = "get_character_extents")]
    fn character_extents(&self, offset: i32, coords: CoordType) -> (i32, i32, i32, i32);

    //#[doc(alias = "atk_text_get_default_attributes")]
    //#[doc(alias = "get_default_attributes")]
    //fn default_attributes(&self) -> /*Ignored*/Option<AttributeSet>;

    /// Gets the number of selected regions.
    ///
    /// # Returns
    ///
    /// The number of selected regions, or -1 in the case of failure.
    #[doc(alias = "atk_text_get_n_selections")]
    #[doc(alias = "get_n_selections")]
    fn n_selections(&self) -> i32;

    /// Gets the offset of the character located at coordinates `x` and `y`. `x` and `y`
    /// are interpreted as being relative to the screen or this widget's window
    /// depending on `coords`.
    /// ## `x`
    /// screen x-position of character
    /// ## `y`
    /// screen y-position of character
    /// ## `coords`
    /// specify whether coordinates are relative to the screen or
    /// widget window
    ///
    /// # Returns
    ///
    /// the offset to the character which is located at the specified
    ///  `x` and `y` coordinates of -1 in case of failure.
    #[doc(alias = "atk_text_get_offset_at_point")]
    #[doc(alias = "get_offset_at_point")]
    fn offset_at_point(&self, x: i32, y: i32, coords: CoordType) -> i32;

    /// Get the bounding box for text within the specified range.
    ///
    /// If the extents can not be obtained (e.g. or missing support), the rectangle
    /// fields are set to -1.
    /// ## `start_offset`
    /// The offset of the first text character for which boundary
    ///  information is required.
    /// ## `end_offset`
    /// The offset of the text character after the last character
    ///  for which boundary information is required.
    /// ## `coord_type`
    /// Specify whether coordinates are relative to the screen or widget window.
    ///
    /// # Returns
    ///
    ///
    /// ## `rect`
    /// A pointer to a AtkTextRectangle which is filled in by this function.
    #[doc(alias = "atk_text_get_range_extents")]
    #[doc(alias = "get_range_extents")]
    fn range_extents(
        &self,
        start_offset: i32,
        end_offset: i32,
        coord_type: CoordType,
    ) -> TextRectangle;

    //#[doc(alias = "atk_text_get_run_attributes")]
    //#[doc(alias = "get_run_attributes")]
    //fn run_attributes(&self, offset: i32) -> (/*Ignored*/AttributeSet, i32, i32);

    /// Gets the text from the specified selection.
    /// ## `selection_num`
    /// The selection number. The selected regions are
    /// assigned numbers that correspond to how far the region is from the
    /// start of the text. The selected region closest to the beginning
    /// of the text region is assigned the number 0, etc. Note that adding,
    /// moving or deleting a selected region can change the numbering.
    ///
    /// # Returns
    ///
    /// a newly allocated string containing the selected text. Use `g_free()`
    ///  to free the returned string.
    ///
    /// ## `start_offset`
    /// passes back the starting character offset of the selected region
    ///
    /// ## `end_offset`
    /// passes back the ending character offset (offset immediately past)
    /// of the selected region
    #[doc(alias = "atk_text_get_selection")]
    #[doc(alias = "get_selection")]
    fn selection(&self, selection_num: i32) -> (glib::GString, i32, i32);

    /// Gets a portion of the text exposed through an [`Text`][crate::Text] according to a given `offset`
    /// and a specific `granularity`, along with the start and end offsets defining the
    /// boundaries of such a portion of text.
    ///
    /// If `granularity` is ATK_TEXT_GRANULARITY_CHAR the character at the
    /// offset is returned.
    ///
    /// If `granularity` is ATK_TEXT_GRANULARITY_WORD the returned string
    /// is from the word start at or before the offset to the word start after
    /// the offset.
    ///
    /// The returned string will contain the word at the offset if the offset
    /// is inside a word and will contain the word before the offset if the
    /// offset is not inside a word.
    ///
    /// If `granularity` is ATK_TEXT_GRANULARITY_SENTENCE the returned string
    /// is from the sentence start at or before the offset to the sentence
    /// start after the offset.
    ///
    /// The returned string will contain the sentence at the offset if the offset
    /// is inside a sentence and will contain the sentence before the offset
    /// if the offset is not inside a sentence.
    ///
    /// If `granularity` is ATK_TEXT_GRANULARITY_LINE the returned string
    /// is from the line start at or before the offset to the line
    /// start after the offset.
    ///
    /// If `granularity` is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
    /// is from the start of the paragraph at or before the offset to the start
    /// of the following paragraph after the offset.
    /// ## `offset`
    /// position
    /// ## `granularity`
    /// An [`TextGranularity`][crate::TextGranularity]
    ///
    /// # Returns
    ///
    /// a newly allocated string containing the text at
    ///  the `offset` bounded by the specified `granularity`. Use `g_free()`
    ///  to free the returned string. Returns [`None`] if the offset is invalid
    ///  or no implementation is available.
    ///
    /// ## `start_offset`
    /// the starting character offset of the returned string, or -1
    ///  in the case of error (e.g. invalid offset, not implemented)
    ///
    /// ## `end_offset`
    /// the offset of the first character after the returned string,
    ///  or -1 in the case of error (e.g. invalid offset, not implemented)
    #[doc(alias = "atk_text_get_string_at_offset")]
    #[doc(alias = "get_string_at_offset")]
    fn string_at_offset(
        &self,
        offset: i32,
        granularity: TextGranularity,
    ) -> (Option<glib::GString>, i32, i32);

    /// Gets the specified text.
    /// ## `start_offset`
    /// a starting character offset within `self`
    /// ## `end_offset`
    /// an ending character offset within `self`, or -1 for the end of the string.
    ///
    /// # Returns
    ///
    /// a newly allocated string containing the text from `start_offset` up
    ///  to, but not including `end_offset`. Use `g_free()` to free the returned
    ///  string.
    #[doc(alias = "atk_text_get_text")]
    #[doc(alias = "get_text")]
    fn text(&self, start_offset: i32, end_offset: i32) -> Option<glib::GString>;

    /// Gets the specified text.
    ///
    /// If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
    /// offset is returned.
    ///
    /// If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
    /// is from the word start at or before the offset to the word start after
    /// the offset.
    ///
    /// The returned string will contain the word at the offset if the offset
    /// is inside a word and will contain the word before the offset if the
    /// offset is not inside a word.
    ///
    /// If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
    /// string is from the sentence start at or before the offset to the sentence
    /// start after the offset.
    ///
    /// The returned string will contain the sentence at the offset if the offset
    /// is inside a sentence and will contain the sentence before the offset
    /// if the offset is not inside a sentence.
    ///
    /// If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
    /// string is from the line start at or before the offset to the line
    /// start after the offset.
    ///
    /// # Deprecated
    ///
    /// This method is deprecated since ATK version
    /// 2.9.4. Please use [`string_at_offset()`][Self::string_at_offset()] instead.
    /// ## `offset`
    /// position
    /// ## `boundary_type`
    /// An [`TextBoundary`][crate::TextBoundary]
    ///
    /// # Returns
    ///
    /// a newly allocated string containing the text at `offset` bounded
    ///  by the specified `boundary_type`. Use `g_free()` to free the returned
    ///  string.
    ///
    /// ## `start_offset`
    /// the starting character offset of the returned string
    ///
    /// ## `end_offset`
    /// the offset of the first character after the
    ///  returned substring
    #[doc(alias = "atk_text_get_text_at_offset")]
    #[doc(alias = "get_text_at_offset")]
    fn text_at_offset(&self, offset: i32, boundary_type: TextBoundary)
        -> (glib::GString, i32, i32);

    /// Removes the specified selection.
    /// ## `selection_num`
    /// The selection number. The selected regions are
    /// assigned numbers that correspond to how far the region is from the
    /// start of the text. The selected region closest to the beginning
    /// of the text region is assigned the number 0, etc. Note that adding,
    /// moving or deleting a selected region can change the numbering.
    ///
    /// # Returns
    ///
    /// [`true`] if successful, [`false`] otherwise
    #[doc(alias = "atk_text_remove_selection")]
    fn remove_selection(&self, selection_num: i32) -> bool;

    /// Makes a substring of `self` visible on the screen by scrolling all necessary parents.
    /// ## `start_offset`
    /// start offset in the `self`
    /// ## `end_offset`
    /// end offset in the `self`, or -1 for the end of the text.
    /// ## `type_`
    /// specify where the object should be made visible.
    ///
    /// # Returns
    ///
    /// whether scrolling was successful.
    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    #[doc(alias = "atk_text_scroll_substring_to")]
    fn scroll_substring_to(&self, start_offset: i32, end_offset: i32, type_: ScrollType) -> bool;

    /// Move the top-left of a substring of `self` to a given position of the screen
    /// by scrolling all necessary parents.
    /// ## `start_offset`
    /// start offset in the `self`
    /// ## `end_offset`
    /// end offset in the `self`, or -1 for the end of the text.
    /// ## `coords`
    /// specify whether coordinates are relative to the screen or to the
    /// parent object.
    /// ## `x`
    /// x-position where to scroll to
    /// ## `y`
    /// y-position where to scroll to
    ///
    /// # Returns
    ///
    /// whether scrolling was successful.
    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    #[doc(alias = "atk_text_scroll_substring_to_point")]
    fn scroll_substring_to_point(
        &self,
        start_offset: i32,
        end_offset: i32,
        coords: CoordType,
        x: i32,
        y: i32,
    ) -> bool;

    /// Sets the caret (cursor) position to the specified `offset`.
    ///
    /// In the case of rich-text content, this method should either grab focus
    /// or move the sequential focus navigation starting point (if the application
    /// supports this concept) as if the user had clicked on the new caret position.
    /// Typically, this means that the target of this operation is the node containing
    /// the new caret position or one of its ancestors. In other words, after this
    /// method is called, if the user advances focus, it should move to the first
    /// focusable node following the new caret position.
    ///
    /// Calling this method should also scroll the application viewport in a way
    /// that matches the behavior of the application's typical caret motion or tab
    /// navigation as closely as possible. This also means that if the application's
    /// caret motion or focus navigation does not trigger a scroll operation, this
    /// method should not trigger one either. If the application does not have a caret
    /// motion or focus navigation operation, this method should try to scroll the new
    /// caret position into view while minimizing unnecessary scroll motion.
    /// ## `offset`
    /// the character offset of the new caret position
    ///
    /// # Returns
    ///
    /// [`true`] if successful, [`false`] otherwise.
    #[doc(alias = "atk_text_set_caret_offset")]
    fn set_caret_offset(&self, offset: i32) -> bool;

    /// Changes the start and end offset of the specified selection.
    /// ## `selection_num`
    /// The selection number. The selected regions are
    /// assigned numbers that correspond to how far the region is from the
    /// start of the text. The selected region closest to the beginning
    /// of the text region is assigned the number 0, etc. Note that adding,
    /// moving or deleting a selected region can change the numbering.
    /// ## `start_offset`
    /// the new starting character offset of the selection
    /// ## `end_offset`
    /// the new end position of (e.g. offset immediately past)
    /// the selection
    ///
    /// # Returns
    ///
    /// [`true`] if successful, [`false`] otherwise
    #[doc(alias = "atk_text_set_selection")]
    fn set_selection(&self, selection_num: i32, start_offset: i32, end_offset: i32) -> bool;

    /// The "text-attributes-changed" signal is emitted when the text
    /// attributes of the text of an object which implements AtkText
    /// changes.
    #[doc(alias = "text-attributes-changed")]
    fn connect_text_attributes_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The "text-caret-moved" signal is emitted when the caret
    /// position of the text of an object which implements AtkText
    /// changes.
    /// ## `arg1`
    /// The new position of the text caret.
    #[doc(alias = "text-caret-moved")]
    fn connect_text_caret_moved<F: Fn(&Self, i32) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The "text-insert" signal is emitted when a new text is
    /// inserted. If the signal was not triggered by the user
    /// (e.g. typing or pasting text), the "system" detail should be
    /// included.
    /// ## `arg1`
    /// The position (character offset) of the insertion.
    /// ## `arg2`
    /// The length (in characters) of text inserted.
    /// ## `arg3`
    /// The new text inserted
    #[doc(alias = "text-insert")]
    fn connect_text_insert<F: Fn(&Self, i32, i32, &str) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId;

    /// The "text-remove" signal is emitted when a new text is
    /// removed. If the signal was not triggered by the user
    /// (e.g. typing or pasting text), the "system" detail should be
    /// included.
    /// ## `arg1`
    /// The position (character offset) of the removal.
    /// ## `arg2`
    /// The length (in characters) of text removed.
    /// ## `arg3`
    /// The old text removed
    #[doc(alias = "text-remove")]
    fn connect_text_remove<F: Fn(&Self, i32, i32, &str) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId;

    /// The "text-selection-changed" signal is emitted when the
    /// selected text of an object which implements AtkText changes.
    #[doc(alias = "text-selection-changed")]
    fn connect_text_selection_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Text>> TextExt for O {
    fn add_selection(&self, start_offset: i32, end_offset: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_text_add_selection(
                self.as_ref().to_glib_none().0,
                start_offset,
                end_offset,
            ))
        }
    }

    fn bounded_ranges(
        &self,
        rect: &mut TextRectangle,
        coord_type: CoordType,
        x_clip_type: TextClipType,
        y_clip_type: TextClipType,
    ) -> Vec<TextRange> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::atk_text_get_bounded_ranges(
                self.as_ref().to_glib_none().0,
                rect.to_glib_none_mut().0,
                coord_type.into_glib(),
                x_clip_type.into_glib(),
                y_clip_type.into_glib(),
            ))
        }
    }

    fn caret_offset(&self) -> i32 {
        unsafe { ffi::atk_text_get_caret_offset(self.as_ref().to_glib_none().0) }
    }

    fn character_at_offset(&self, offset: i32) -> char {
        unsafe {
            std::convert::TryFrom::try_from(ffi::atk_text_get_character_at_offset(
                self.as_ref().to_glib_none().0,
                offset,
            ))
            .expect("conversion from an invalid Unicode value attempted")
        }
    }

    fn character_count(&self) -> i32 {
        unsafe { ffi::atk_text_get_character_count(self.as_ref().to_glib_none().0) }
    }

    fn character_extents(&self, offset: i32, coords: CoordType) -> (i32, i32, i32, i32) {
        unsafe {
            let mut x = mem::MaybeUninit::uninit();
            let mut y = mem::MaybeUninit::uninit();
            let mut width = mem::MaybeUninit::uninit();
            let mut height = mem::MaybeUninit::uninit();
            ffi::atk_text_get_character_extents(
                self.as_ref().to_glib_none().0,
                offset,
                x.as_mut_ptr(),
                y.as_mut_ptr(),
                width.as_mut_ptr(),
                height.as_mut_ptr(),
                coords.into_glib(),
            );
            let x = x.assume_init();
            let y = y.assume_init();
            let width = width.assume_init();
            let height = height.assume_init();
            (x, y, width, height)
        }
    }

    //fn default_attributes(&self) -> /*Ignored*/Option<AttributeSet> {
    //    unsafe { TODO: call ffi:atk_text_get_default_attributes() }
    //}

    fn n_selections(&self) -> i32 {
        unsafe { ffi::atk_text_get_n_selections(self.as_ref().to_glib_none().0) }
    }

    fn offset_at_point(&self, x: i32, y: i32, coords: CoordType) -> i32 {
        unsafe {
            ffi::atk_text_get_offset_at_point(
                self.as_ref().to_glib_none().0,
                x,
                y,
                coords.into_glib(),
            )
        }
    }

    fn range_extents(
        &self,
        start_offset: i32,
        end_offset: i32,
        coord_type: CoordType,
    ) -> TextRectangle {
        unsafe {
            let mut rect = TextRectangle::uninitialized();
            ffi::atk_text_get_range_extents(
                self.as_ref().to_glib_none().0,
                start_offset,
                end_offset,
                coord_type.into_glib(),
                rect.to_glib_none_mut().0,
            );
            rect
        }
    }

    //fn run_attributes(&self, offset: i32) -> (/*Ignored*/AttributeSet, i32, i32) {
    //    unsafe { TODO: call ffi:atk_text_get_run_attributes() }
    //}

    fn selection(&self, selection_num: i32) -> (glib::GString, i32, i32) {
        unsafe {
            let mut start_offset = mem::MaybeUninit::uninit();
            let mut end_offset = mem::MaybeUninit::uninit();
            let ret = from_glib_full(ffi::atk_text_get_selection(
                self.as_ref().to_glib_none().0,
                selection_num,
                start_offset.as_mut_ptr(),
                end_offset.as_mut_ptr(),
            ));
            let start_offset = start_offset.assume_init();
            let end_offset = end_offset.assume_init();
            (ret, start_offset, end_offset)
        }
    }

    fn string_at_offset(
        &self,
        offset: i32,
        granularity: TextGranularity,
    ) -> (Option<glib::GString>, i32, i32) {
        unsafe {
            let mut start_offset = mem::MaybeUninit::uninit();
            let mut end_offset = mem::MaybeUninit::uninit();
            let ret = from_glib_full(ffi::atk_text_get_string_at_offset(
                self.as_ref().to_glib_none().0,
                offset,
                granularity.into_glib(),
                start_offset.as_mut_ptr(),
                end_offset.as_mut_ptr(),
            ));
            let start_offset = start_offset.assume_init();
            let end_offset = end_offset.assume_init();
            (ret, start_offset, end_offset)
        }
    }

    fn text(&self, start_offset: i32, end_offset: i32) -> Option<glib::GString> {
        unsafe {
            from_glib_full(ffi::atk_text_get_text(
                self.as_ref().to_glib_none().0,
                start_offset,
                end_offset,
            ))
        }
    }

    fn text_at_offset(
        &self,
        offset: i32,
        boundary_type: TextBoundary,
    ) -> (glib::GString, i32, i32) {
        unsafe {
            let mut start_offset = mem::MaybeUninit::uninit();
            let mut end_offset = mem::MaybeUninit::uninit();
            let ret = from_glib_full(ffi::atk_text_get_text_at_offset(
                self.as_ref().to_glib_none().0,
                offset,
                boundary_type.into_glib(),
                start_offset.as_mut_ptr(),
                end_offset.as_mut_ptr(),
            ));
            let start_offset = start_offset.assume_init();
            let end_offset = end_offset.assume_init();
            (ret, start_offset, end_offset)
        }
    }

    fn remove_selection(&self, selection_num: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_text_remove_selection(
                self.as_ref().to_glib_none().0,
                selection_num,
            ))
        }
    }

    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    fn scroll_substring_to(&self, start_offset: i32, end_offset: i32, type_: ScrollType) -> bool {
        unsafe {
            from_glib(ffi::atk_text_scroll_substring_to(
                self.as_ref().to_glib_none().0,
                start_offset,
                end_offset,
                type_.into_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    fn scroll_substring_to_point(
        &self,
        start_offset: i32,
        end_offset: i32,
        coords: CoordType,
        x: i32,
        y: i32,
    ) -> bool {
        unsafe {
            from_glib(ffi::atk_text_scroll_substring_to_point(
                self.as_ref().to_glib_none().0,
                start_offset,
                end_offset,
                coords.into_glib(),
                x,
                y,
            ))
        }
    }

    fn set_caret_offset(&self, offset: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_text_set_caret_offset(
                self.as_ref().to_glib_none().0,
                offset,
            ))
        }
    }

    fn set_selection(&self, selection_num: i32, start_offset: i32, end_offset: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_text_set_selection(
                self.as_ref().to_glib_none().0,
                selection_num,
                start_offset,
                end_offset,
            ))
        }
    }

    fn connect_text_attributes_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn text_attributes_changed_trampoline<
            P: IsA<Text>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::AtkText,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Text::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"text-attributes-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    text_attributes_changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_text_caret_moved<F: Fn(&Self, i32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn text_caret_moved_trampoline<P: IsA<Text>, F: Fn(&P, i32) + 'static>(
            this: *mut ffi::AtkText,
            arg1: libc::c_int,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Text::from_glib_borrow(this).unsafe_cast_ref(), arg1)
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"text-caret-moved\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    text_caret_moved_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_text_insert<F: Fn(&Self, i32, i32, &str) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn text_insert_trampoline<
            P: IsA<Text>,
            F: Fn(&P, i32, i32, &str) + 'static,
        >(
            this: *mut ffi::AtkText,
            arg1: libc::c_int,
            arg2: libc::c_int,
            arg3: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Text::from_glib_borrow(this).unsafe_cast_ref(),
                arg1,
                arg2,
                &glib::GString::from_glib_borrow(arg3),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            let detailed_signal_name = detail.map(|name| format!("text-insert::{}\0", name));
            let signal_name: &[u8] = detailed_signal_name
                .as_ref()
                .map_or(&b"text-insert\0"[..], |n| n.as_bytes());
            connect_raw(
                self.as_ptr() as *mut _,
                signal_name.as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    text_insert_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_text_remove<F: Fn(&Self, i32, i32, &str) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn text_remove_trampoline<
            P: IsA<Text>,
            F: Fn(&P, i32, i32, &str) + 'static,
        >(
            this: *mut ffi::AtkText,
            arg1: libc::c_int,
            arg2: libc::c_int,
            arg3: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Text::from_glib_borrow(this).unsafe_cast_ref(),
                arg1,
                arg2,
                &glib::GString::from_glib_borrow(arg3),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            let detailed_signal_name = detail.map(|name| format!("text-remove::{}\0", name));
            let signal_name: &[u8] = detailed_signal_name
                .as_ref()
                .map_or(&b"text-remove\0"[..], |n| n.as_bytes());
            connect_raw(
                self.as_ptr() as *mut _,
                signal_name.as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    text_remove_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_text_selection_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn text_selection_changed_trampoline<
            P: IsA<Text>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::AtkText,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Text::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"text-selection-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    text_selection_changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Text {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Text")
    }
}