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 {
pub fn new() -> TextBuffer {
unsafe { from_glib_full(ffi::clutter_text_buffer_new()) }
}
}
impl Default for TextBuffer {
fn default() -> Self {
Self::new()
}
}
pub trait TextBufferExt: 'static {
fn delete_text(&self, position: u32, n_chars: i32) -> u32;
fn emit_deleted_text(&self, position: u32, n_chars: u32);
fn emit_inserted_text(&self, position: u32, chars: &str, n_chars: u32);
fn get_bytes(&self) -> usize;
fn get_length(&self) -> u32;
fn get_max_length(&self) -> i32;
fn get_text(&self) -> Option<GString>;
fn insert_text(&self, position: u32, chars: &str, n_chars: i32) -> u32;
fn set_max_length(&self, max_length: i32);
fn set_text(&self, chars: &str, n_chars: i32);
fn connect_deleted_text<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId;
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")
}
}