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