animate/legacy/
text_buffer.rs

1use glib::{
2    object::{Cast, IsA},
3    signal::{connect_raw, SignalHandlerId},
4    translate::*,
5    GString,
6};
7use std::boxed::Box as Box_;
8use std::{fmt, mem::transmute};
9
10glib_wrapper! {
11    pub struct TextBuffer(Object<ffi::ClutterTextBuffer, ffi::ClutterTextBufferClass, TextBufferClass>);
12
13    match fn {
14        get_type => || ffi::clutter_text_buffer_get_type(),
15    }
16}
17
18impl TextBuffer {
19    /// Create a new TextBuffer object.
20    ///
21    /// # Returns
22    ///
23    /// A new TextBuffer object.
24    pub fn new() -> TextBuffer {
25        unsafe { from_glib_full(ffi::clutter_text_buffer_new()) }
26    }
27
28    // pub fn with_text(text: Option<&str>) -> TextBuffer {
29    //     let text_len = text.len() as isize;
30    //     unsafe {
31    //         from_glib_full(ffi::clutter_text_buffer_new_with_text(
32    //             text.to_glib_none().0,
33    //             text_len,
34    //         ))
35    //     }
36    // }
37}
38
39impl Default for TextBuffer {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45/// Trait containing all `TextBuffer` methods.
46///
47/// # Implementors
48///
49/// [`TextBuffer`](struct.TextBuffer.html)
50pub trait TextBufferExt: 'static {
51    /// Deletes a sequence of characters from the buffer. `n_chars` characters are
52    /// deleted starting at `position`. If `n_chars` is negative, then all characters
53    /// until the end of the text are deleted.
54    ///
55    /// If `position` or `n_chars` are out of bounds, then they are coerced to sane
56    /// values.
57    ///
58    /// Note that the positions are specified in characters, not bytes.
59    /// ## `position`
60    /// position at which to delete text
61    /// ## `n_chars`
62    /// number of characters to delete
63    ///
64    /// # Returns
65    ///
66    /// The number of characters deleted.
67    fn delete_text(&self, position: u32, n_chars: i32) -> u32;
68
69    /// Emits the `TextBuffer::deleted-text` signal on `self`.
70    ///
71    /// Used when subclassing `TextBuffer`
72    /// ## `position`
73    /// position at which text was deleted
74    /// ## `n_chars`
75    /// number of characters deleted
76    fn emit_deleted_text(&self, position: u32, n_chars: u32);
77
78    /// Emits the `TextBuffer::inserted-text` signal on `self`.
79    ///
80    /// Used when subclassing `TextBuffer`
81    /// ## `position`
82    /// position at which text was inserted
83    /// ## `chars`
84    /// text that was inserted
85    /// ## `n_chars`
86    /// number of characters inserted
87    fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32);
88
89    /// Retrieves the length in bytes of the buffer.
90    /// See `TextBufferExt::get_length`.
91    ///
92    /// # Returns
93    ///
94    /// The byte length of the buffer.
95    fn get_bytes(&self) -> usize;
96
97    /// Retrieves the length in characters of the buffer.
98    ///
99    /// # Returns
100    ///
101    /// The number of characters in the buffer.
102    fn get_length(&self) -> u32;
103
104    /// Retrieves the maximum allowed length of the text in
105    /// `self`. See `TextBufferExt::set_max_length`.
106    ///
107    /// # Returns
108    ///
109    /// the maximum allowed number of characters
110    ///  in `TextBuffer`, or 0 if there is no maximum.
111    fn get_max_length(&self) -> i32;
112
113    /// Retrieves the contents of the buffer.
114    ///
115    /// The memory pointer returned by this call will not change
116    /// unless this object emits a signal, or is finalized.
117    ///
118    /// # Returns
119    ///
120    /// a pointer to the contents of the widget as a
121    ///  string. This string points to internally allocated
122    ///  storage in the buffer and must not be freed, modified or
123    ///  stored.
124    fn get_text(&self) -> Option<GString>;
125
126    /// Inserts `n_chars` characters of `chars` into the contents of the
127    /// buffer, at position `position`.
128    ///
129    /// If `n_chars` is negative, then characters from chars will be inserted
130    /// until a null-terminator is found. If `position` or `n_chars` are out of
131    /// bounds, or the maximum buffer text length is exceeded, then they are
132    /// coerced to sane values.
133    ///
134    /// Note that the position and length are in characters, not in bytes.
135    /// ## `position`
136    /// the position at which to insert text.
137    /// ## `chars`
138    /// the text to insert into the buffer.
139    /// ## `n_chars`
140    /// the length of the text in characters, or -1
141    ///
142    /// # Returns
143    ///
144    /// The number of characters actually inserted.
145    fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32;
146
147    /// Sets the maximum allowed length of the contents of the buffer. If
148    /// the current contents are longer than the given length, then they
149    /// will be truncated to fit.
150    /// ## `max_length`
151    /// the maximum length of the entry buffer, or 0 for no maximum.
152    ///  (other than the maximum length of entries.) The value passed in will
153    ///  be clamped to the range [ 0, `TEXT_BUFFER_MAX_SIZE` ].
154    fn set_max_length(&self, max_length: i32);
155
156    /// Sets the text in the buffer.
157    ///
158    /// This is roughly equivalent to calling `TextBufferExt::delete_text`
159    /// and `TextBufferExt::insert_text`.
160    ///
161    /// Note that `n_chars` is in characters, not in bytes.
162    /// ## `chars`
163    /// the new text
164    /// ## `n_chars`
165    /// the number of characters in `text`, or -1
166    fn set_text(&self, chars: &str, n_chars: i32);
167
168    /// This signal is emitted after text is deleted from the buffer.
169    /// ## `position`
170    /// the position the text was deleted at.
171    /// ## `n_chars`
172    /// The number of characters that were deleted.
173    fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId;
174
175    /// This signal is emitted after text is inserted into the buffer.
176    /// ## `position`
177    /// the position the text was inserted at.
178    /// ## `chars`
179    /// The text that was inserted.
180    /// ## `n_chars`
181    /// The number of characters that were inserted.
182    fn connect_inserted_text<F: Fn(&Self, u32, &str, u32) + 'static>(
183        &self,
184        f: F,
185    ) -> SignalHandlerId;
186
187    fn connect_property_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
188
189    fn connect_property_max_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
190
191    fn connect_property_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
192}
193
194impl<O: IsA<TextBuffer>> TextBufferExt for O {
195    fn delete_text(&self, position: u32, n_chars: i32) -> u32 {
196        unsafe {
197            ffi::clutter_text_buffer_delete_text(self.as_ref().to_glib_none().0, position, n_chars)
198        }
199    }
200
201    fn emit_deleted_text(&self, position: u32, n_chars: u32) {
202        unsafe {
203            ffi::clutter_text_buffer_emit_deleted_text(
204                self.as_ref().to_glib_none().0,
205                position,
206                n_chars,
207            );
208        }
209    }
210
211    fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32) {
212        unsafe {
213            ffi::clutter_text_buffer_emit_inserted_text(
214                self.as_ref().to_glib_none().0,
215                position,
216                chars.to_glib_none().0,
217                n_chars,
218            );
219        }
220    }
221
222    fn get_bytes(&self) -> usize {
223        unsafe { ffi::clutter_text_buffer_get_bytes(self.as_ref().to_glib_none().0) }
224    }
225
226    fn get_length(&self) -> u32 {
227        unsafe { ffi::clutter_text_buffer_get_length(self.as_ref().to_glib_none().0) }
228    }
229
230    fn get_max_length(&self) -> i32 {
231        unsafe { ffi::clutter_text_buffer_get_max_length(self.as_ref().to_glib_none().0) }
232    }
233
234    fn get_text(&self) -> Option<GString> {
235        unsafe {
236            from_glib_none(ffi::clutter_text_buffer_get_text(
237                self.as_ref().to_glib_none().0,
238            ))
239        }
240    }
241
242    fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32 {
243        unsafe {
244            ffi::clutter_text_buffer_insert_text(
245                self.as_ref().to_glib_none().0,
246                position,
247                chars.to_glib_none().0,
248                n_chars,
249            )
250        }
251    }
252
253    fn set_max_length(&self, max_length: i32) {
254        unsafe {
255            ffi::clutter_text_buffer_set_max_length(self.as_ref().to_glib_none().0, max_length);
256        }
257    }
258
259    fn set_text(&self, chars: &str, n_chars: i32) {
260        unsafe {
261            ffi::clutter_text_buffer_set_text(
262                self.as_ref().to_glib_none().0,
263                chars.to_glib_none().0,
264                n_chars,
265            );
266        }
267    }
268
269    fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId {
270        unsafe extern "C" fn deleted_text_trampoline<P, F: Fn(&P, u32, u32) + 'static>(
271            this: *mut ffi::ClutterTextBuffer,
272            position: libc::c_uint,
273            n_chars: libc::c_uint,
274            f: glib_sys::gpointer,
275        ) where
276            P: IsA<TextBuffer>,
277        {
278            let f: &F = &*(f as *const F);
279            f(
280                &TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
281                position,
282                n_chars,
283            )
284        }
285        unsafe {
286            let f: Box_<F> = Box_::new(f);
287            connect_raw(
288                self.as_ptr() as *mut _,
289                b"deleted-text\0".as_ptr() as *const _,
290                Some(transmute::<_, unsafe extern "C" fn()>(
291                    deleted_text_trampoline::<Self, F> as *const (),
292                )),
293                Box_::into_raw(f),
294            )
295        }
296    }
297
298    fn connect_inserted_text<F: Fn(&Self, u32, &str, u32) + 'static>(
299        &self,
300        f: F,
301    ) -> SignalHandlerId {
302        unsafe extern "C" fn inserted_text_trampoline<P, F: Fn(&P, u32, &str, u32) + 'static>(
303            this: *mut ffi::ClutterTextBuffer,
304            position: libc::c_uint,
305            chars: *mut libc::c_char,
306            n_chars: libc::c_uint,
307            f: glib_sys::gpointer,
308        ) where
309            P: IsA<TextBuffer>,
310        {
311            let f: &F = &*(f as *const F);
312            f(
313                &TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
314                position,
315                &GString::from_glib_borrow(chars),
316                n_chars,
317            )
318        }
319        unsafe {
320            let f: Box_<F> = Box_::new(f);
321            connect_raw(
322                self.as_ptr() as *mut _,
323                b"inserted-text\0".as_ptr() as *const _,
324                Some(transmute::<_, unsafe extern "C" fn()>(
325                    inserted_text_trampoline::<Self, F> as *const (),
326                )),
327                Box_::into_raw(f),
328            )
329        }
330    }
331
332    fn connect_property_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
333        unsafe extern "C" fn notify_length_trampoline<P, F: Fn(&P) + 'static>(
334            this: *mut ffi::ClutterTextBuffer,
335            _param_spec: glib_sys::gpointer,
336            f: glib_sys::gpointer,
337        ) where
338            P: IsA<TextBuffer>,
339        {
340            let f: &F = &*(f as *const F);
341            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
342        }
343        unsafe {
344            let f: Box_<F> = Box_::new(f);
345            connect_raw(
346                self.as_ptr() as *mut _,
347                b"notify::length\0".as_ptr() as *const _,
348                Some(transmute::<_, unsafe extern "C" fn()>(
349                    notify_length_trampoline::<Self, F> as *const (),
350                )),
351                Box_::into_raw(f),
352            )
353        }
354    }
355
356    fn connect_property_max_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
357        unsafe extern "C" fn notify_max_length_trampoline<P, F: Fn(&P) + 'static>(
358            this: *mut ffi::ClutterTextBuffer,
359            _param_spec: glib_sys::gpointer,
360            f: glib_sys::gpointer,
361        ) where
362            P: IsA<TextBuffer>,
363        {
364            let f: &F = &*(f as *const F);
365            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
366        }
367        unsafe {
368            let f: Box_<F> = Box_::new(f);
369            connect_raw(
370                self.as_ptr() as *mut _,
371                b"notify::max-length\0".as_ptr() as *const _,
372                Some(transmute::<_, unsafe extern "C" fn()>(
373                    notify_max_length_trampoline::<Self, F> as *const (),
374                )),
375                Box_::into_raw(f),
376            )
377        }
378    }
379
380    fn connect_property_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
381        unsafe extern "C" fn notify_text_trampoline<P, F: Fn(&P) + 'static>(
382            this: *mut ffi::ClutterTextBuffer,
383            _param_spec: glib_sys::gpointer,
384            f: glib_sys::gpointer,
385        ) where
386            P: IsA<TextBuffer>,
387        {
388            let f: &F = &*(f as *const F);
389            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
390        }
391        unsafe {
392            let f: Box_<F> = Box_::new(f);
393            connect_raw(
394                self.as_ptr() as *mut _,
395                b"notify::text\0".as_ptr() as *const _,
396                Some(transmute::<_, unsafe extern "C" fn()>(
397                    notify_text_trampoline::<Self, F> as *const (),
398                )),
399                Box_::into_raw(f),
400            )
401        }
402    }
403}
404
405impl fmt::Display for TextBuffer {
406    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
407        write!(f, "TextBuffer")
408    }
409}