clutter/auto/
clone.rs

1// Scriptable
2use crate::{Actor, Animatable, Container};
3use glib::{
4    object as gobject,
5    object::{Cast, IsA},
6    signal::{connect_raw, SignalHandlerId},
7    translate::*,
8};
9use std::boxed::Box as Box_;
10use std::{fmt, mem::transmute};
11
12// TODO: implements atk::ImplementorIface, Scriptable
13glib_wrapper! {
14    pub struct Clone(Object<ffi::ClutterClone, ffi::ClutterCloneClass, CloneClass>) @extends Actor, gobject::InitiallyUnowned, @implements Animatable, Container;
15
16    match fn {
17        get_type => || ffi::clutter_clone_get_type(),
18    }
19}
20
21impl Clone {
22    /// Creates a new `Actor` which clones `source`/
23    /// ## `source`
24    /// a `Actor`, or `None`
25    ///
26    /// # Returns
27    ///
28    /// the newly created `Clone`
29    pub fn new<P: IsA<Actor>>(source: &P) -> Clone {
30        unsafe {
31            Actor::from_glib_none(ffi::clutter_clone_new(source.as_ref().to_glib_none().0))
32                .unsafe_cast()
33        }
34    }
35}
36
37pub const NONE_CLONE: Option<&Clone> = None;
38
39/// Trait containing all `Clone` methods.
40///
41/// # Implementors
42///
43/// [`Clone`](struct.Clone.html)
44pub trait CloneExt: 'static {
45    /// Retrieves the source `Actor` being cloned by `self`.
46    ///
47    /// # Returns
48    ///
49    /// the actor source for the clone
50    fn get_source(&self) -> Option<Actor>;
51
52    /// Sets `source` as the source actor to be cloned by `self`.
53    /// ## `source`
54    /// a `Actor`, or `None`
55    fn set_source<P: IsA<Actor>>(&self, source: Option<&P>);
56
57    fn connect_property_source_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
58}
59
60impl<O: IsA<Clone>> CloneExt for O {
61    fn get_source(&self) -> Option<Actor> {
62        unsafe {
63            from_glib_none(ffi::clutter_clone_get_source(
64                self.as_ref().to_glib_none().0,
65            ))
66        }
67    }
68
69    fn set_source<P: IsA<Actor>>(&self, source: Option<&P>) {
70        unsafe {
71            ffi::clutter_clone_set_source(
72                self.as_ref().to_glib_none().0,
73                source.map(|p| p.as_ref()).to_glib_none().0,
74            );
75        }
76    }
77
78    fn connect_property_source_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
79        unsafe extern "C" fn notify_source_trampoline<P, F: Fn(&P) + 'static>(
80            this: *mut ffi::ClutterClone,
81            _param_spec: glib_sys::gpointer,
82            f: glib_sys::gpointer,
83        ) where
84            P: IsA<Clone>,
85        {
86            let f: &F = &*(f as *const F);
87            f(&Clone::from_glib_borrow(this).unsafe_cast_ref())
88        }
89        unsafe {
90            let f: Box_<F> = Box_::new(f);
91            connect_raw(
92                self.as_ptr() as *mut _,
93                b"notify::source\0".as_ptr() as *const _,
94                Some(transmute::<_, unsafe extern "C" fn()>(
95                    notify_source_trampoline::<Self, F> as *const (),
96                )),
97                Box_::into_raw(f),
98            )
99        }
100    }
101}
102
103impl fmt::Display for Clone {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        write!(f, "Clone")
106    }
107}