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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::TextBuffer;
use crate::TextChildAnchor;
use crate::TextIter;
use crate::TextTag;
use glib::object::{Cast, IsA};
use glib::signal::{connect_raw, SignalHandlerId};
use glib::translate::*;
use libc::{c_char, c_int};
use std::boxed::Box as Box_;
use std::mem::transmute;
use std::{slice, str};

pub trait TextBufferExtManual: 'static {
    /// The ::apply-tag signal is emitted to apply a tag to a
    /// range of text in a [`TextBuffer`][crate::TextBuffer].
    /// Applying actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler it must not
    /// invalidate the `start` and `end` iters (or has to revalidate them).
    ///
    /// See also:
    /// [`TextBufferExt::apply_tag()`][crate::prelude::TextBufferExt::apply_tag()],
    /// `gtk_text_buffer_insert_with_tags()`,
    /// [`TextBufferExt::insert_range()`][crate::prelude::TextBufferExt::insert_range()].
    /// ## `tag`
    /// the applied tag
    /// ## `start`
    /// the start of the range the tag is applied to
    /// ## `end`
    /// the end of the range the tag is applied to
    fn connect_apply_tag<F: Fn(&Self, &TextTag, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::delete-range signal is emitted to delete a range
    /// from a [`TextBuffer`][crate::TextBuffer].
    ///
    /// Note that if your handler runs before the default handler it must not
    /// invalidate the `start` and `end` iters (or has to revalidate them).
    /// The default signal handler revalidates the `start` and `end` iters to
    /// both point to the location where text was deleted. Handlers
    /// which run after the default handler (see `g_signal_connect_after()`)
    /// do not have access to the deleted text.
    ///
    /// See also: [`TextBufferExt::delete()`][crate::prelude::TextBufferExt::delete()].
    /// ## `start`
    /// the start of the range to be deleted
    /// ## `end`
    /// the end of the range to be deleted
    fn connect_delete_range<F: Fn(&Self, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::insert-child-anchor signal is emitted to insert a
    /// [`TextChildAnchor`][crate::TextChildAnchor] in a [`TextBuffer`][crate::TextBuffer].
    /// Insertion actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler it must
    /// not invalidate the `location` iter (or has to revalidate it).
    /// The default signal handler revalidates it to be placed after the
    /// inserted `anchor`.
    ///
    /// See also: [`TextBufferExt::insert_child_anchor()`][crate::prelude::TextBufferExt::insert_child_anchor()].
    /// ## `location`
    /// position to insert `anchor` in `textbuffer`
    /// ## `anchor`
    /// the [`TextChildAnchor`][crate::TextChildAnchor] to be inserted
    fn connect_insert_child_anchor<F: Fn(&Self, &mut TextIter, &TextChildAnchor) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::insert-pixbuf signal is emitted to insert a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
    /// in a [`TextBuffer`][crate::TextBuffer]. Insertion actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler it must not
    /// invalidate the `location` iter (or has to revalidate it).
    /// The default signal handler revalidates it to be placed after the
    /// inserted `pixbuf`.
    ///
    /// See also: [`TextBufferExt::insert_pixbuf()`][crate::prelude::TextBufferExt::insert_pixbuf()].
    /// ## `location`
    /// position to insert `pixbuf` in `textbuffer`
    /// ## `pixbuf`
    /// the [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf] to be inserted
    fn connect_insert_pixbuf<F: Fn(&Self, &mut TextIter, &gdk_pixbuf::Pixbuf) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::insert-text signal is emitted to insert text in a [`TextBuffer`][crate::TextBuffer].
    /// Insertion actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler it must not
    /// invalidate the `location` iter (or has to revalidate it).
    /// The default signal handler revalidates it to point to the end of the
    /// inserted text.
    ///
    /// See also:
    /// [`TextBufferExt::insert()`][crate::prelude::TextBufferExt::insert()],
    /// [`TextBufferExt::insert_range()`][crate::prelude::TextBufferExt::insert_range()].
    /// ## `location`
    /// position to insert `text` in `textbuffer`
    /// ## `text`
    /// the UTF-8 text to be inserted
    /// ## `len`
    /// length of the inserted text in bytes
    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// The ::remove-tag signal is emitted to remove all occurrences of `tag` from
    /// a range of text in a [`TextBuffer`][crate::TextBuffer].
    /// Removal actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler it must not
    /// invalidate the `start` and `end` iters (or has to revalidate them).
    ///
    /// See also:
    /// [`TextBufferExt::remove_tag()`][crate::prelude::TextBufferExt::remove_tag()].
    /// ## `tag`
    /// the tag to be removed
    /// ## `start`
    /// the start of the range the tag is removed from
    /// ## `end`
    /// the end of the range the tag is removed from
    fn connect_remove_tag<F: Fn(&Self, &TextTag, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<TextBuffer>> TextBufferExtManual for O {
    fn connect_apply_tag<F: Fn(&Self, &TextTag, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn apply_tag_trampoline<
            P,
            F: Fn(&P, &TextTag, &mut TextIter, &mut TextIter) + 'static,
        >(
            this: *mut ffi::GtkTextBuffer,
            tag: *mut ffi::GtkTextTag,
            start: *mut ffi::GtkTextIter,
            end: *mut ffi::GtkTextIter,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut start_copy = from_glib_none(start);
            let mut end_copy = from_glib_none(end);

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(tag),
                &mut start_copy,
                &mut end_copy,
            );

            *start = *start_copy.to_glib_none().0;
            *end = *end_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"apply-tag\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    apply_tag_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_delete_range<F: Fn(&Self, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn delete_range_trampoline<
            P,
            F: Fn(&P, &mut TextIter, &mut TextIter) + 'static,
        >(
            this: *mut ffi::GtkTextBuffer,
            start: *mut ffi::GtkTextIter,
            end: *mut ffi::GtkTextIter,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut start_copy = from_glib_none(start);
            let mut end_copy = from_glib_none(end);

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &mut start_copy,
                &mut end_copy,
            );

            *start = *start_copy.to_glib_none().0;
            *end = *end_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"delete-range\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    delete_range_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_insert_child_anchor<F: Fn(&Self, &mut TextIter, &TextChildAnchor) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn insert_child_anchor_trampoline<
            P,
            F: Fn(&P, &mut TextIter, &TextChildAnchor) + 'static,
        >(
            this: *mut ffi::GtkTextBuffer,
            location: *mut ffi::GtkTextIter,
            anchor: *mut ffi::GtkTextChildAnchor,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut location_copy = from_glib_none(location);

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &mut location_copy,
                &from_glib_borrow(anchor),
            );

            *location = *location_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"insert-child-anchor\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    insert_child_anchor_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_insert_pixbuf<F: Fn(&Self, &mut TextIter, &gdk_pixbuf::Pixbuf) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn insert_pixbuf_trampoline<
            P,
            F: Fn(&P, &mut TextIter, &gdk_pixbuf::Pixbuf) + 'static,
        >(
            this: *mut ffi::GtkTextBuffer,
            location: *mut ffi::GtkTextIter,
            pixbuf: *mut gdk_pixbuf::ffi::GdkPixbuf,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut location_copy = from_glib_none(location);

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &mut location_copy,
                &from_glib_borrow(pixbuf),
            );

            *location = *location_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"insert-pixbuf\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    insert_pixbuf_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &mut TextIter, &str) + 'static>(
            this: *mut ffi::GtkTextBuffer,
            location: *mut ffi::GtkTextIter,
            text: *mut c_char,
            len: c_int,
            f: glib::ffi::gpointer,
        ) where
            T: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut location_copy = from_glib_none(location);

            let text = if len <= 0 {
                &[]
            } else {
                slice::from_raw_parts(text as *const u8, len as usize)
            };

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &mut location_copy,
                str::from_utf8(text).unwrap(),
            );

            *location = *location_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.to_glib_none().0 as *mut _,
                b"insert-text\0".as_ptr() as *mut _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    insert_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_remove_tag<F: Fn(&Self, &TextTag, &mut TextIter, &mut TextIter) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn remove_tag_trampoline<
            P,
            F: Fn(&P, &TextTag, &mut TextIter, &mut TextIter) + 'static,
        >(
            this: *mut ffi::GtkTextBuffer,
            tag: *mut ffi::GtkTextTag,
            start: *mut ffi::GtkTextIter,
            end: *mut ffi::GtkTextIter,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<TextBuffer>,
        {
            let f: &F = &*(f as *const F);
            let mut start_copy = from_glib_none(start);
            let mut end_copy = from_glib_none(end);

            f(
                TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(tag),
                &mut start_copy,
                &mut end_copy,
            );

            *start = *start_copy.to_glib_none().0;
            *end = *end_copy.to_glib_none().0;
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"remove-tag\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    remove_tag_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}