[go: up one dir, main page]

gtk 0.1.0

Rust bindings for the GTK+ 3 library
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// Copyright 2015, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use std::cell::RefCell;
use std::mem::transmute;

use glib::translate::*;

use glib_ffi::{self, gboolean, gpointer};
use gdk::ModifierType;

use {
    CellEditable,
    CellRenderer,
    Continue,
    Rectangle,
    ScrollType,
    SpinButton,
    TreeIter,
    TreeModel,
    TreePath,
    TreeViewColumn,
};

/// Whether to propagate the signal to the default handler.
///
/// Don't inhibit default handlers without a reason, they're usually helpful.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Inhibit(pub bool);

#[doc(hidden)]
impl ToGlib for Inhibit {
    type GlibType = gboolean;

    #[inline]
    fn to_glib(&self) -> gboolean {
        self.0.to_glib()
    }
}

// idle_add and timeout_add fixed to the main thread

unsafe extern "C" fn trampoline(func: gpointer) -> gboolean {
    callback_guard!();
    let func: &RefCell<Box<FnMut() -> Continue + 'static>> = transmute(func);
    (&mut *func.borrow_mut())().to_glib()
}

unsafe extern "C" fn destroy_closure(ptr: gpointer) {
    callback_guard!();
    Box::<RefCell<Box<FnMut() -> Continue + 'static>>>::from_raw(ptr as *mut _);
}

fn into_raw<F: FnMut() -> Continue + 'static>(func: F) -> gpointer {
    let func: Box<RefCell<Box<FnMut() -> Continue + 'static>>> =
        Box::new(RefCell::new(Box::new(func)));
    Box::into_raw(func) as gpointer
}

/// Adds a closure to be called by the default main loop when it's idle.
///
/// `func` will be called repeatedly until it returns `Continue(false)`.
///
/// Similar to `glib::idle_add` but only callable from the main thread and
/// doesn't require `Send`.
pub fn idle_add<F>(func: F) -> u32
    where F: FnMut() -> Continue + 'static {
    assert_initialized_main_thread!();
    unsafe {
        glib_ffi::g_idle_add_full(glib_ffi::G_PRIORITY_DEFAULT_IDLE, Some(trampoline),
            into_raw(func), Some(destroy_closure))
    }
}

/// Adds a closure to be called by the default main loop at regular intervals
/// with millisecond granularity.
///
/// `func` will be called repeatedly every `interval` milliseconds until it
/// returns `Continue(false)`. Precise timing is not guaranteed, the timeout may
/// be delayed by other events. Prefer `timeout_add_seconds` when millisecond
/// precision is not necessary.
///
/// Similar to `glib::timeout_add` but only callable from the main thread and
/// doesn't require `Send`.
pub fn timeout_add<F>(interval: u32, func: F) -> u32
    where F: FnMut() -> Continue + 'static {
    assert_initialized_main_thread!();
    unsafe {
        glib_ffi::g_timeout_add_full(glib_ffi::G_PRIORITY_DEFAULT, interval, Some(trampoline),
            into_raw(func), Some(destroy_closure))
    }
}

/// Adds a closure to be called by the default main loop at regular intervals
/// with second granularity.
///
/// `func` will be called repeatedly every `interval` seconds until it
/// returns `Continue(false)`. Precise timing is not guaranteed, the timeout may
/// be delayed by other events.
///
/// Similar to `glib::timeout_add_seconds` but only callable from the main thread and
/// doesn't require `Send`.
pub fn timeout_add_seconds<F>(interval: u32, func: F) -> u32
    where F: FnMut() -> Continue + 'static {
    assert_initialized_main_thread!();
    unsafe {
        glib_ffi::g_timeout_add_seconds_full(glib_ffi::G_PRIORITY_DEFAULT, interval,
            Some(trampoline), into_raw(func), Some(destroy_closure))
    }
}

pub trait CellRendererToggleSignals {
    fn connect_toggled<F: Fn(&Self, &TreePath) + 'static>(&self, f: F) -> u64;
}

mod cell_renderer_toggle {
    use std::mem::transmute;
    use glib::signal::connect;
    use glib::translate::*;
    use libc::c_char;
    use ffi::{GtkCellRendererToggle, gtk_tree_path_new_from_string};
    use {CellRendererToggle, TreePath};

    impl super::CellRendererToggleSignals for CellRendererToggle {
        fn connect_toggled<F: Fn(&Self, &TreePath) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreePath) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "toggled",
                    transmute(string_path_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn string_path_trampoline(this: *mut GtkCellRendererToggle, c_str_path: *const c_char,
            f: &Box<Fn(&CellRendererToggle, &TreePath) + 'static>) {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(c_str_path));
        f(&from_glib_none(this), &path);
    }
}

pub trait TreeViewSignals {
    fn connect_columns_changed<F: Fn(&Self) + 'static>(&self, f: F) -> u64;
    fn connect_cursor_changed<F: Fn(&Self) + 'static>(&self, f: F) -> u64;
    fn connect_expand_collapse_cursor_row<F: Fn(&Self, bool, bool, bool) -> bool + 'static>(&self, f: F)
        -> u64;
    fn connect_row_activated<F: Fn(&Self, &TreePath, &TreeViewColumn) + 'static>(&self, f: F) -> u64;
    fn connect_row_collapsed<F: Fn(&Self, &TreeIter, &TreePath) + 'static>(&self, f: F) -> u64;
    fn connect_row_expanded<F: Fn(&Self, &TreeIter, &TreePath) + 'static>(&self, f: F) -> u64;
    fn connect_select_all<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64;
    fn connect_select_cursor_parent<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64;
    fn connect_select_cursor_row<F: Fn(&Self, bool) -> bool + 'static>(&self, f: F) -> u64;
    fn connect_start_interactive_search<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64;
    fn connect_test_collapse_row<F: Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>(&self, f: F)
        -> u64;
    fn connect_test_expand_row<F: Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>(&self, f: F)
        -> u64;
    fn connect_toggle_cursor_row<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64;
    fn connect_unselect_all<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64;
}

mod tree_view {
    use std::mem::transmute;
    use glib::signal::connect;
    use glib::translate::*;
    use glib_ffi::gboolean;
    use ffi::{GtkTreeIter, GtkTreePath, GtkTreeView, GtkTreeViewColumn};
    use super::Inhibit;
    use {TreeIter, TreePath, TreeView, TreeViewColumn};

    impl super::TreeViewSignals for TreeView {
        fn connect_columns_changed<F: Fn(&Self) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "columns-changed",
                    transmute(void_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_cursor_changed<F: Fn(&Self) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "cursor-changed",
                    transmute(void_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_expand_collapse_cursor_row<F: Fn(&Self, bool, bool, bool) -> bool + 'static>(&self,
                f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, bool, bool, bool) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "expand-collapse-cursor-row",
                    transmute(bool3_bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_row_activated<F: Fn(&Self, &TreePath, &TreeViewColumn) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreePath, &TreeViewColumn) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "row-activated",
                    transmute(path_column_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_row_collapsed<F: Fn(&Self, &TreeIter, &TreePath) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreeIter, &TreePath) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "row-collapsed",
                    transmute(iter_path_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_row_expanded<F: Fn(&Self, &TreeIter, &TreePath) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreeIter, &TreePath) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "row-expanded",
                    transmute(iter_path_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_select_all<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "select-all",
                    transmute(bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_select_cursor_parent<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "select-cursor-parent",
                    transmute(bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_select_cursor_row<F: Fn(&Self, bool) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, bool) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "select-cursor-row",
                    transmute(bool_bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_start_interactive_search<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "start-interactive-search",
                    transmute(bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_test_collapse_row<F: Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>(&self, f: F)
                -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>> =
                    Box::new(Box::new(f));
                connect(self.to_glib_none().0, "test-collapse-row",
                    transmute(iter_path_inhibit_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_test_expand_row<F: Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>(&self, f: F)
                -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreeIter, &TreePath) -> Inhibit + 'static>> =
                    Box::new(Box::new(f));
                connect(self.to_glib_none().0, "test-expand-row",
                    transmute(iter_path_inhibit_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_toggle_cursor_row<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "toggle-cursor-row",
                    transmute(bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_unselect_all<F: Fn(&Self) -> bool + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self) -> bool + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "unselect-all",
                    transmute(bool_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn void_trampoline(this: *mut GtkTreeView, f: &Box<Fn(&TreeView) + 'static>) {
        callback_guard!();
        f(&from_glib_none(this));
    }

    unsafe extern "C" fn bool_trampoline(this: *mut GtkTreeView, f: &Box<Fn(&TreeView) -> bool + 'static>)
            -> gboolean {
        callback_guard!();
        f(&from_glib_none(this)).to_glib()
    }

    unsafe extern "C" fn bool_bool_trampoline(this: *mut GtkTreeView, arg1: gboolean,
            f: &Box<Fn(&TreeView, bool) -> bool + 'static>) -> gboolean {
        callback_guard!();
        f(&from_glib_none(this), from_glib(arg1)).to_glib()
    }

    unsafe extern "C" fn bool3_bool_trampoline(this: *mut GtkTreeView, arg1: gboolean, arg2: gboolean,
            arg3: gboolean, f: &Box<Fn(&TreeView, bool, bool, bool) -> bool + 'static>) -> gboolean {
        callback_guard!();
        f(&from_glib_none(this), from_glib(arg1), from_glib(arg2),
            from_glib(arg3)).to_glib()
    }

    unsafe extern "C" fn path_column_trampoline(this: *mut GtkTreeView, path: *mut GtkTreePath,
            column: *mut GtkTreeViewColumn,
            f: &Box<Fn(&TreeView, &TreePath, &TreeViewColumn) + 'static>) {
        callback_guard!();
        f(&from_glib_none(this), &from_glib_borrow(path), &from_glib_none(column));
    }

    unsafe extern "C" fn iter_path_trampoline(this: *mut GtkTreeView, iter: *mut GtkTreeIter,
            path: *mut GtkTreePath, f: &Box<Fn(&TreeView, &TreeIter, &TreePath) + 'static>) {
        callback_guard!();
        f(&from_glib_none(this), &from_glib_borrow(iter), &from_glib_borrow(path));
    }

    unsafe extern "C" fn iter_path_inhibit_trampoline(this: *mut GtkTreeView, iter: *mut GtkTreeIter,
            path: *mut GtkTreePath,
            f: &Box<Fn(&TreeView, &TreeIter, &TreePath) -> Inhibit + 'static>) -> gboolean {
        callback_guard!();
        f(&from_glib_none(this), &from_glib_borrow(iter), &from_glib_borrow(path)).to_glib()
    }
}

pub trait CellAreaSignals {
    fn connect_add_editable<F>(&self, add_editable_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, &CellEditable, &Rectangle, TreePath) + 'static;
    fn connect_apply_attributes<F>(&self, apply_attributes_func: F) -> u64
        where F: Fn(&Self, &TreeModel, &TreeIter, bool, bool) + 'static;
    fn connect_focus_changed<F>(&self, focus_changed_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, TreePath) + 'static;
    fn connect_remove_editable<F>(&self, remove_editable_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, &CellEditable) + 'static;
}

mod cell_area {
    use CellArea;
    use CellEditable;
    use CellRenderer;
    use TreeIter;
    use TreeModel;
    use TreePath;
    use Rectangle;
    use std::mem::transmute;
    use ffi::{GtkCellArea, GtkCellEditable, GtkCellRenderer, GtkTreeIter, GtkTreeModel};
    use ffi::gtk_tree_path_new_from_string;
    use gdk_ffi::GdkRectangle;
    use glib_ffi::gboolean;
    use glib::object::Downcast;
    use glib::signal::connect;
    use glib::translate::*;
    use IsA;
    use libc::c_char;

    impl super::CellAreaSignals for CellArea {
        fn connect_add_editable<F>(&self, add_editable_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, &CellEditable, &Rectangle, TreePath) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &CellRenderer, &CellEditable, &Rectangle, TreePath) + 'static>> =
                    Box::new(Box::new(add_editable_func));
                connect(self.to_glib_none().0, "add-editable",
                    transmute(add_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_apply_attributes<F>(&self, apply_attributes_func: F) -> u64
        where F: Fn(&Self, &TreeModel, &TreeIter, bool, bool) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreeModel, &TreeIter, bool, bool) + 'static>> =
                    Box::new(Box::new(apply_attributes_func));
                connect(self.to_glib_none().0, "apply-attributes",
                    transmute(apply_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_focus_changed<F>(&self, focus_changed_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, TreePath) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &CellRenderer, TreePath) + 'static>> =
                    Box::new(Box::new(focus_changed_func));
                connect(self.to_glib_none().0, "focus-changed",
                    transmute(focus_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_remove_editable<F>(&self, remove_editable_func: F) -> u64
        where F: Fn(&Self, &CellRenderer, &CellEditable) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &CellRenderer, &CellEditable) + 'static>> =
                    Box::new(Box::new(remove_editable_func));
                connect(self.to_glib_none().0, "remove-editable",
                    transmute(remove_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn add_trampoline<T>(this: *mut GtkCellArea,
                                           renderer: *mut GtkCellRenderer,
                                           editable: *mut GtkCellEditable,
                                           cell_area: *mut GdkRectangle,
                                           path: *const c_char,
                                           f: &Box<Fn(&T, &CellRenderer, &CellEditable, &Rectangle, TreePath) + 'static>)
    where T: IsA<CellArea> {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellArea::from_glib_none(this).downcast_unchecked(),
          &CellRenderer::from_glib_none(renderer),
          &CellEditable::from_glib_none(editable),
          &Rectangle { x: (*cell_area).x, y: (*cell_area).y, width: (*cell_area).width, height: (*cell_area).height },
          path);
    }

    unsafe extern "C" fn apply_trampoline<T>(this: *mut GtkCellArea,
                                             model: *mut GtkTreeModel,
                                             iter: *mut GtkTreeIter,
                                             is_expander: gboolean,
                                             is_expanded: gboolean,
                                             f: &Box<Fn(&T, &TreeModel, &TreeIter, bool, bool) + 'static>)
    where T: IsA<CellArea> {
        callback_guard!();
        f(&CellArea::from_glib_none(this).downcast_unchecked(),
          &TreeModel::from_glib_none(model),
          &TreeIter::from_glib_borrow(iter),
          from_glib(is_expander),
          from_glib(is_expanded));
    }

    unsafe extern "C" fn focus_trampoline<T>(this: *mut GtkCellArea,
                                             renderer: *mut GtkCellRenderer,
                                             path: *const c_char,
                                             f: &Box<Fn(&T, &CellRenderer, TreePath) + 'static>)
    where T: IsA<CellArea> {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellArea::from_glib_none(this).downcast_unchecked(),
          &CellRenderer::from_glib_none(renderer),
          path);
    }

    unsafe extern "C" fn remove_trampoline<T>(this: *mut GtkCellArea,
                                              renderer: *mut GtkCellRenderer,
                                              editable: *mut GtkCellEditable,
                                              f: &Box<Fn(&T, &CellRenderer, &CellEditable) + 'static>)
    where T: IsA<CellArea> {
        callback_guard!();
        f(&CellArea::from_glib_none(this).downcast_unchecked(),
          &CellRenderer::from_glib_none(renderer),
          &CellEditable::from_glib_none(editable));
    }
}

pub trait CellRendererAccelSignals {
    fn connect_accel_cleared<F>(&self, accel_cleared_func: F) -> u64
        where F: Fn(&Self, TreePath) + 'static;
    fn connect_accel_edited<F>(&self, accel_edited_func: F) -> u64
        where F: Fn(&Self, TreePath, u32, ModifierType, u32) + 'static;
}

mod cell_renderer_accel {
    use CellRendererAccel;
    use TreePath;
    use std::mem::transmute;
    use ffi::GtkCellRendererAccel;
    use glib::signal::connect;
    use glib::translate::*;
    use gdk::ModifierType;
    use ffi::gtk_tree_path_new_from_string;
    use gdk_ffi::GdkModifierType;
    use libc::{c_char, c_uint};

    impl super::CellRendererAccelSignals for CellRendererAccel {
        fn connect_accel_cleared<F>(&self, accel_cleared_func: F) -> u64
        where F: Fn(&Self, TreePath) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, TreePath) + 'static>> =
                    Box::new(Box::new(accel_cleared_func));
                connect(self.to_glib_none().0, "accel-cleared",
                    transmute(trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_accel_edited<F>(&self, accel_edited_func: F) -> u64
        where F: Fn(&Self, TreePath, u32, ModifierType, u32) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, TreePath, u32, ModifierType, u32) + 'static>> =
                    Box::new(Box::new(accel_edited_func));
                connect(self.to_glib_none().0, "accel-edited",
                    transmute(e_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn trampoline(this: *mut GtkCellRendererAccel,
                                    path: *const c_char,
                                    f: &Box<Fn(&CellRendererAccel, TreePath) + 'static>) {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellRendererAccel::from_glib_none(this), path);
    }

    unsafe extern "C" fn e_trampoline(this: *mut GtkCellRendererAccel,
                                      path: *const c_char,
                                      accel_key: c_uint,
                                      accel_mods: GdkModifierType,
                                      hardware_keycode: c_uint,
                                      f: &Box<Fn(&CellRendererAccel, TreePath, u32, ModifierType, u32) + 'static>) {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellRendererAccel::from_glib_none(this),
           path,
           accel_key,
           from_glib(accel_mods),
           hardware_keycode);
    }
}

pub trait CellRendererComboSignals {
    fn connect_changed<F>(&self, changed_func: F) -> u64
        where F: Fn(&Self, TreePath, &TreeIter) + 'static;
}

mod cell_renderer_combo {
    use CellRendererCombo;
    use TreeIter;
    use TreePath;
    use std::mem::transmute;
    use ffi::{GtkCellRendererCombo, GtkTreeIter};
    use glib::signal::connect;
    use glib::translate::*;
    use ffi::gtk_tree_path_new_from_string;
    use libc::c_char;

    impl super::CellRendererComboSignals for CellRendererCombo {
        fn connect_changed<F>(&self, accel_cleared_func: F) -> u64
        where F: Fn(&Self, TreePath, &TreeIter) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, TreePath, &TreeIter) + 'static>> =
                    Box::new(Box::new(accel_cleared_func));
                connect(self.to_glib_none().0, "changed",
                    transmute(trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn trampoline(this: *mut GtkCellRendererCombo,
                                    path: *const c_char,
                                    new_iter: *mut GtkTreeIter,
                                    f: &Box<Fn(&CellRendererCombo, TreePath, &TreeIter) + 'static>) {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellRendererCombo::from_glib_none(this), path, &from_glib_borrow(new_iter));
    }
}

pub trait CellRendererSignals {
    fn connect_editing_canceled<F>(&self, editing_canceled_func: F) -> u64
        where F: Fn(&Self) + 'static;
    fn connect_editing_started<F>(&self, editing_started_func: F) -> u64
        where F: Fn(&Self, &CellEditable, TreePath) + 'static;
}

mod cell_renderer {
    use CellEditable;
    use CellRenderer;
    use Object;
    use TreePath;
    use std::mem::transmute;
    use ffi::{GtkCellEditable, GtkCellRenderer};
    use glib::object::Downcast;
    use glib::signal::connect;
    use glib::translate::*;
    use IsA;
    use ffi::gtk_tree_path_new_from_string;
    use libc::c_char;

    impl<T: IsA<CellRenderer> + IsA<Object>> super::CellRendererSignals for T {
        fn connect_editing_canceled<F>(&self, editing_canceled_func: F) -> u64
        where F: Fn(&Self) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self) + 'static>> =
                    Box::new(Box::new(editing_canceled_func));
                connect(self.to_glib_none().0, "editing-canceled",
                    transmute(trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_editing_started<F>(&self, editing_started_func: F) -> u64
        where F: Fn(&Self, &CellEditable, TreePath) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &CellEditable, TreePath) + 'static>> =
                    Box::new(Box::new(editing_started_func));
                connect(self.to_glib_none().0, "editing-started",
                    transmute(start_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn trampoline<T>(this: *mut GtkCellRenderer,
                                       f: &Box<Fn(&T) + 'static>)
    where T: IsA<CellRenderer> {
        callback_guard!();
        f(&CellRenderer::from_glib_none(this).downcast_unchecked());
    }

    unsafe extern "C" fn start_trampoline<T>(this: *mut GtkCellRenderer,
                                             editable: *mut GtkCellEditable,
                                             path: *const c_char,
                                             f: &Box<Fn(&T, &CellEditable, TreePath) + 'static>)
    where T: IsA<CellRenderer> {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        f(&CellRenderer::from_glib_none(this).downcast_unchecked(),
          &CellEditable::from_glib_none(editable),
          path);
    }
}

pub trait CellRendererTextSignals {
    fn connect_edited<F: Fn(&Self, &TreePath, &str) + 'static>(&self, f: F) -> u64;
}

mod cell_renderer_text {
    use Object;
    use std::mem::transmute;
    use std::str;
    use glib::signal::connect;
    use glib::translate::*;
    use libc::c_char;
    use std::ffi::CStr;
    use glib::object::Downcast;
    use IsA;
    use ffi::{GtkCellRendererText, gtk_tree_path_new_from_string};
    use {CellRendererText, TreePath};

    impl<T: IsA<CellRendererText> + IsA<Object>> super::CellRendererTextSignals for T {
        fn connect_edited<F: Fn(&Self, &TreePath, &str) + 'static>(&self, f: F) -> u64 {
            unsafe {
                let f: Box<Box<Fn(&Self, &TreePath, &str) + 'static>> = Box::new(Box::new(f));
                connect(self.to_glib_none().0, "edited",
                    transmute(trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn trampoline<T>(this: *mut GtkCellRendererText, path: *const c_char,
        new_text: *const c_char, f: &Box<Fn(&T, &TreePath, &str) + 'static>)
    where T: IsA<CellRendererText> {
        callback_guard!();
        let path = from_glib_full(gtk_tree_path_new_from_string(path));
        let buf = CStr::from_ptr(new_text).to_bytes();
        let new_text = str::from_utf8(buf).unwrap();
        f(&CellRendererText::from_glib_none(this).downcast_unchecked(), &path, new_text);
    }
}

pub trait EditableSignals {
    fn connect_changed<F>(&self, changed_func: F) -> u64
        where F: Fn(&Self) + 'static;
    fn connect_delete_text<F>(&self, delete_text_func: F) -> u64
        where F: Fn(&Self, i32, i32) + 'static;
    fn connect_insert_text<F>(&self, insert_text_func: F) -> u64
        where F: Fn(&Self, &str, &mut i32) + 'static;
}

mod editable {
    use Editable;
    use Object;
    use std::mem::transmute;
    use ffi::GtkEditable;
    use glib::signal::connect;
    use glib::translate::*;
    use IsA;
    use libc::{c_char, c_int, c_uchar};
    use std::ffi::CStr;
    use std::str;
    use glib::object::Downcast;
    use std::slice;

    impl<T: IsA<Editable> + IsA<Object>> super::EditableSignals for T {
        fn connect_changed<F>(&self, changed_func: F) -> u64
        where F: Fn(&Self) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self) + 'static>> =
                    Box::new(Box::new(changed_func));
                connect(self.to_glib_none().0, "changed",
                    transmute(trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_delete_text<F>(&self, delete_text_func: F) -> u64
        where F: Fn(&Self, i32, i32) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, i32, i32) + 'static>> =
                    Box::new(Box::new(delete_text_func));
                connect(self.to_glib_none().0, "delete-text",
                    transmute(delete_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_insert_text<F>(&self, insert_text_func: F) -> u64
        where F: Fn(&Self, &str, &mut i32) + 'static {
            unsafe {
                let f: Box<Box<Fn(&Self, &str, &mut i32) + 'static>> =
                    Box::new(Box::new(insert_text_func));
                connect(self.to_glib_none().0, "insert-text",
                    transmute(insert_trampoline::<Self> as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn trampoline<T>(this: *mut GtkEditable,
                                       f: &Box<Fn(&T) + 'static>)
    where T: IsA<Editable> {
        callback_guard!();
        f(&Editable::from_glib_none(this).downcast_unchecked());
    }

    unsafe extern "C" fn delete_trampoline<T>(this: *mut GtkEditable,
                                              start_pos: c_int,
                                              end_pos: c_int,
                                              f: &Box<Fn(&T, i32, i32) + 'static>)
    where T: IsA<Editable> {
        callback_guard!();
        f(&Editable::from_glib_none(this).downcast_unchecked(), start_pos, end_pos);
    }

    unsafe extern "C" fn insert_trampoline<T>(this: *mut GtkEditable,
                                              new_text: *mut c_char,
                                              new_text_length: c_int,
                                              position: *mut c_int,
                                              f: &Box<Fn(&T, &str, &mut i32) + 'static>)
    where T: IsA<Editable> {
        callback_guard!();
        let buf = if new_text_length != -1 {
            slice::from_raw_parts(new_text as *mut c_uchar,
                                  new_text_length as usize)
        } else {
            CStr::from_ptr(new_text).to_bytes()
        };
        let string = str::from_utf8(buf).unwrap();
        f(&Editable::from_glib_none(this).downcast_unchecked(),
          string,
          transmute(position));
    }
}

pub trait SpinButtonSignals {
    fn connect_change_value<F>(&self, change_value_func: F) -> u64
        where F: Fn(&SpinButton, ScrollType) + 'static;
    fn connect_input<F>(&self, input_func: F) -> u64
        where F: Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static;
    fn connect_output<F>(&self, output_func: F) -> u64
        where F: Fn(&SpinButton) -> Inhibit + 'static;
    fn connect_value_changed<F>(&self, value_changed_func: F) -> u64
        where F: Fn(&SpinButton) + 'static;
    fn connect_wrapped<F>(&self, wrapped_func: F) -> u64
        where F: Fn(&SpinButton) + 'static;
}

mod spin_button {
    use Inhibit;
    use SpinButton;
    use ScrollType;
    use ffi::{GTK_INPUT_ERROR, GtkSpinButton};
    use glib::signal::connect;
    use glib::translate::*;
    use glib_ffi::{GTRUE, GFALSE};
    use libc::{c_int, c_double};
    use std::boxed::Box as Box_;
    use std::mem::transmute;
    use glib_ffi::gboolean;

    impl ::SpinButtonSignals for SpinButton {
        fn connect_change_value<F>(&self, change_value_func: F) -> u64
        where F: Fn(&SpinButton, ScrollType) + 'static {
            unsafe {
                let f: Box<Box<Fn(&SpinButton, ScrollType) + 'static>> =
                    Box::new(Box::new(change_value_func));
                connect(self.to_glib_none().0, "change_value",
                        transmute(change_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_input<F>(&self, f: F) -> u64
        where F: Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static {
            unsafe {
                let f: Box_<Box_<Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static>> = Box_::new(Box_::new(f));
                connect(self.to_glib_none().0, "input",
                        transmute(input_trampoline as usize), Box_::into_raw(f) as *mut _)
            }
        }

        fn connect_output<F>(&self, output_func: F) -> u64
        where F: Fn(&SpinButton) -> Inhibit + 'static {
            unsafe {
                let f: Box<Box<Fn(&SpinButton) -> Inhibit + 'static>> =
                    Box::new(Box::new(output_func));
                connect(self.to_glib_none().0, "output",
                        transmute(output_trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_value_changed<F>(&self, value_changed_func: F) -> u64
        where F: Fn(&SpinButton) + 'static {
            unsafe {
                let f: Box<Box<Fn(&SpinButton) + 'static>> =
                    Box::new(Box::new(value_changed_func));
                connect(self.to_glib_none().0, "value-changed",
                        transmute(trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }

        fn connect_wrapped<F>(&self, wrapped_func: F) -> u64
        where F: Fn(&SpinButton) + 'static {
            unsafe {
                let f: Box<Box<Fn(&SpinButton) + 'static>> =
                    Box::new(Box::new(wrapped_func));
                connect(self.to_glib_none().0, "wrapped",
                        transmute(trampoline as usize), Box::into_raw(f) as *mut _)
            }
        }
    }

    unsafe extern "C" fn change_trampoline(this: *mut GtkSpinButton,
                                           scroll: ScrollType,
                                           f: &Box<Fn(&SpinButton, ScrollType) + 'static>) {
        callback_guard!();
        f(&from_glib_none(this), scroll)
    }

    unsafe extern "C" fn input_trampoline(this: *mut GtkSpinButton,
                                          new_value: *mut c_double,
                                          f: &Box_<Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static>)
                                          -> c_int {
        callback_guard!();
        match f(&from_glib_none(this)) {
            Some(Ok(v)) => {
                *new_value = v;
                GTRUE
            }
            Some(Err(_)) => GTK_INPUT_ERROR,
            None => GFALSE,
        }
    }

    unsafe extern "C" fn output_trampoline(this: *mut GtkSpinButton,
                                           f: &Box<Fn(&SpinButton) -> Inhibit + 'static>)
                                           -> gboolean {
        callback_guard!();
        f(&from_glib_none(this)).to_glib()
    }

    unsafe extern "C" fn trampoline(this: *mut GtkSpinButton,
                                    f: &Box<Fn(&SpinButton) + 'static>) {
        callback_guard!();
        f(&from_glib_none(this))
    }
}