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 trait TextBufferExt: 'static {
51 fn delete_text(&self, position: u32, n_chars: i32) -> u32;
68
69 fn emit_deleted_text(&self, position: u32, n_chars: u32);
77
78 fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32);
88
89 fn get_bytes(&self) -> usize;
96
97 fn get_length(&self) -> u32;
103
104 fn get_max_length(&self) -> i32;
112
113 fn get_text(&self) -> Option<GString>;
125
126 fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32;
146
147 fn set_max_length(&self, max_length: i32);
155
156 fn set_text(&self, chars: &str, n_chars: i32);
167
168 fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId;
174
175 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}