gtk/
text_buffer.rs

1// Copyright 2013-2016, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use glib::object::{Cast, IsA};
6use glib::signal::{connect_raw, SignalHandlerId};
7use glib::translate::*;
8use glib_sys;
9use gtk_sys;
10use libc::{c_char, c_int};
11use std::boxed::Box as Box_;
12use std::mem::transmute;
13use std::{slice, str};
14use TextBuffer;
15use TextIter;
16
17pub trait TextBufferExtManual: 'static {
18    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
19        &self,
20        f: F,
21    ) -> SignalHandlerId;
22}
23
24impl<O: IsA<TextBuffer>> TextBufferExtManual for O {
25    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
26        &self,
27        f: F,
28    ) -> SignalHandlerId {
29        unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &mut TextIter, &str) + 'static>(
30            this: *mut gtk_sys::GtkTextBuffer,
31            location: *mut gtk_sys::GtkTextIter,
32            text: *mut c_char,
33            len: c_int,
34            f: glib_sys::gpointer,
35        ) where
36            T: IsA<TextBuffer>,
37        {
38            let f: &F = &*(f as *const F);
39            f(
40                &TextBuffer::from_glib_borrow(this).unsafe_cast(),
41                &mut from_glib_borrow(location),
42                str::from_utf8(slice::from_raw_parts(text as *const u8, len as usize)).unwrap(),
43            )
44        }
45        unsafe {
46            let f: Box_<F> = Box_::new(f);
47            connect_raw(
48                self.to_glib_none().0 as *mut _,
49                b"insert-text\0".as_ptr() as *mut _,
50                Some(transmute(insert_text_trampoline::<Self, F> as usize)),
51                Box_::into_raw(f),
52            )
53        }
54    }
55}