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
use glib::{
    object::{Cast, IsA},
    signal::{connect_raw, SignalHandlerId},
    translate::*,
    GString,
};
use std::boxed::Box as Box_;
use std::{fmt, mem::transmute};

glib_wrapper! {
    pub struct TextBuffer(Object<ffi::ClutterTextBuffer, ffi::ClutterTextBufferClass, TextBufferClass>);

    match fn {
        get_type => || ffi::clutter_text_buffer_get_type(),
    }
}

impl TextBuffer {
    /// Create a new TextBuffer object.
    ///
    /// # Returns
    ///
    /// A new TextBuffer object.
    pub fn new() -> TextBuffer {
        unsafe { from_glib_full(ffi::clutter_text_buffer_new()) }
    }

    // pub fn with_text(text: Option<&str>) -> TextBuffer {
    //     let text_len = text.len() as isize;
    //     unsafe {
    //         from_glib_full(ffi::clutter_text_buffer_new_with_text(
    //             text.to_glib_none().0,
    //             text_len,
    //         ))
    //     }
    // }
}

impl Default for TextBuffer {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait containing all `TextBuffer` methods.
///
/// # Implementors
///
/// [`TextBuffer`](struct.TextBuffer.html)
pub trait TextBufferExt: 'static {
    /// Deletes a sequence of characters from the buffer. `n_chars` characters are
    /// deleted starting at `position`. If `n_chars` is negative, then all characters
    /// until the end of the text are deleted.
    ///
    /// If `position` or `n_chars` are out of bounds, then they are coerced to sane
    /// values.
    ///
    /// Note that the positions are specified in characters, not bytes.
    /// ## `position`
    /// position at which to delete text
    /// ## `n_chars`
    /// number of characters to delete
    ///
    /// # Returns
    ///
    /// The number of characters deleted.
    fn delete_text(&self, position: u32, n_chars: i32) -> u32;

    /// Emits the `TextBuffer::deleted-text` signal on `self`.
    ///
    /// Used when subclassing `TextBuffer`
    /// ## `position`
    /// position at which text was deleted
    /// ## `n_chars`
    /// number of characters deleted
    fn emit_deleted_text(&self, position: u32, n_chars: u32);

    /// Emits the `TextBuffer::inserted-text` signal on `self`.
    ///
    /// Used when subclassing `TextBuffer`
    /// ## `position`
    /// position at which text was inserted
    /// ## `chars`
    /// text that was inserted
    /// ## `n_chars`
    /// number of characters inserted
    fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32);

    /// Retrieves the length in bytes of the buffer.
    /// See `TextBufferExt::get_length`.
    ///
    /// # Returns
    ///
    /// The byte length of the buffer.
    fn get_bytes(&self) -> usize;

    /// Retrieves the length in characters of the buffer.
    ///
    /// # Returns
    ///
    /// The number of characters in the buffer.
    fn get_length(&self) -> u32;

    /// Retrieves the maximum allowed length of the text in
    /// `self`. See `TextBufferExt::set_max_length`.
    ///
    /// # Returns
    ///
    /// the maximum allowed number of characters
    ///  in `TextBuffer`, or 0 if there is no maximum.
    fn get_max_length(&self) -> i32;

    /// Retrieves the contents of the buffer.
    ///
    /// The memory pointer returned by this call will not change
    /// unless this object emits a signal, or is finalized.
    ///
    /// # Returns
    ///
    /// a pointer to the contents of the widget as a
    ///  string. This string points to internally allocated
    ///  storage in the buffer and must not be freed, modified or
    ///  stored.
    fn get_text(&self) -> Option<GString>;

    /// Inserts `n_chars` characters of `chars` into the contents of the
    /// buffer, at position `position`.
    ///
    /// If `n_chars` is negative, then characters from chars will be inserted
    /// until a null-terminator is found. If `position` or `n_chars` are out of
    /// bounds, or the maximum buffer text length is exceeded, then they are
    /// coerced to sane values.
    ///
    /// Note that the position and length are in characters, not in bytes.
    /// ## `position`
    /// the position at which to insert text.
    /// ## `chars`
    /// the text to insert into the buffer.
    /// ## `n_chars`
    /// the length of the text in characters, or -1
    ///
    /// # Returns
    ///
    /// The number of characters actually inserted.
    fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32;

    /// Sets the maximum allowed length of the contents of the buffer. If
    /// the current contents are longer than the given length, then they
    /// will be truncated to fit.
    /// ## `max_length`
    /// the maximum length of the entry buffer, or 0 for no maximum.
    ///  (other than the maximum length of entries.) The value passed in will
    ///  be clamped to the range [ 0, `TEXT_BUFFER_MAX_SIZE` ].
    fn set_max_length(&self, max_length: i32);

    /// Sets the text in the buffer.
    ///
    /// This is roughly equivalent to calling `TextBufferExt::delete_text`
    /// and `TextBufferExt::insert_text`.
    ///
    /// Note that `n_chars` is in characters, not in bytes.
    /// ## `chars`
    /// the new text
    /// ## `n_chars`
    /// the number of characters in `text`, or -1
    fn set_text(&self, chars: &str, n_chars: i32);

    /// This signal is emitted after text is deleted from the buffer.
    /// ## `position`
    /// the position the text was deleted at.
    /// ## `n_chars`
    /// The number of characters that were deleted.
    fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId;

    /// This signal is emitted after text is inserted into the buffer.
    /// ## `position`
    /// the position the text was inserted at.
    /// ## `chars`
    /// The text that was inserted.
    /// ## `n_chars`
    /// The number of characters that were inserted.
    fn connect_inserted_text<F: Fn(&Self, u32, &str, u32) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_max_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<TextBuffer>> TextBufferExt for O {
    fn delete_text(&self, position: u32, n_chars: i32) -> u32 {
        unsafe {
            ffi::clutter_text_buffer_delete_text(self.as_ref().to_glib_none().0, position, n_chars)
        }
    }

    fn emit_deleted_text(&self, position: u32, n_chars: u32) {
        unsafe {
            ffi::clutter_text_buffer_emit_deleted_text(
                self.as_ref().to_glib_none().0,
                position,
                n_chars,
            );
        }
    }

    fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32) {
        unsafe {
            ffi::clutter_text_buffer_emit_inserted_text(
                self.as_ref().to_glib_none().0,
                position,
                chars.to_glib_none().0,
                n_chars,
            );
        }
    }

    fn get_bytes(&self) -> usize {
        unsafe { ffi::clutter_text_buffer_get_bytes(self.as_ref().to_glib_none().0) }
    }

    fn get_length(&self) -> u32 {
        unsafe { ffi::clutter_text_buffer_get_length(self.as_ref().to_glib_none().0) }
    }

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

    fn get_text(&self) -> Option<GString> {
        unsafe {
            from_glib_none(ffi::clutter_text_buffer_get_text(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32 {
        unsafe {
            ffi::clutter_text_buffer_insert_text(
                self.as_ref().to_glib_none().0,
                position,
                chars.to_glib_none().0,
                n_chars,
            )
        }
    }

    fn set_max_length(&self, max_length: i32) {
        unsafe {
            ffi::clutter_text_buffer_set_max_length(self.as_ref().to_glib_none().0, max_length);
        }
    }

    fn set_text(&self, chars: &str, n_chars: i32) {
        unsafe {
            ffi::clutter_text_buffer_set_text(
                self.as_ref().to_glib_none().0,
                chars.to_glib_none().0,
                n_chars,
            );
        }
    }

    fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn deleted_text_trampoline<P, F: Fn(&P, u32, u32) + 'static>(
            this: *mut ffi::ClutterTextBuffer,
            position: libc::c_uint,
            n_chars: libc::c_uint,
            f: glib_sys::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            f(
                &TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                position,
                n_chars,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"deleted-text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    deleted_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_inserted_text<F: Fn(&Self, u32, &str, u32) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn inserted_text_trampoline<P, F: Fn(&P, u32, &str, u32) + 'static>(
            this: *mut ffi::ClutterTextBuffer,
            position: libc::c_uint,
            chars: *mut libc::c_char,
            n_chars: libc::c_uint,
            f: glib_sys::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            f(
                &TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                position,
                &GString::from_glib_borrow(chars),
                n_chars,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"inserted-text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    inserted_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_length_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterTextBuffer,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::length\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_length_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_max_length_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_max_length_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterTextBuffer,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::max-length\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_max_length_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_text_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterTextBuffer,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            f(&TextBuffer::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for TextBuffer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TextBuffer")
    }
}