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 pub fn new() -> TextBuffer {
25 unsafe { from_glib_full(ffi::clutter_text_buffer_new()) }
26 }
27
28 }
38
39impl Default for TextBuffer {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45pub const NONE_TEXT_BUFFER: Option<&TextBuffer> = None;
46
47pub trait TextBufferExt: 'static {
53 fn delete_text(&self, position: u32, n_chars: i32) -> u32;
70
71 fn emit_deleted_text(&self, position: u32, n_chars: u32);
79
80 fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32);
90
91 fn get_bytes(&self) -> usize;
98
99 fn get_length(&self) -> u32;
105
106 fn get_max_length(&self) -> i32;
114
115 fn get_text(&self) -> Option<GString>;
127
128 fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32;
148
149 fn set_max_length(&self, max_length: i32);
157
158 fn set_text(&self, chars: &str, n_chars: i32);
169
170 fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId;
176
177 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}