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