Skip to main content

gtk/subclass/
socket.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::object::IsA;
4use glib::subclass::prelude::*;
5
6use glib::object::Cast;
7use glib::translate::*;
8
9use super::container::ContainerImpl;
10
11use crate::{Socket, ffi};
12
13pub trait SocketImpl: ContainerImpl + ObjectSubclass<Type: IsA<Socket>> {
14    fn plug_added(&self) {
15        self.parent_plug_added()
16    }
17
18    fn plug_removed(&self) -> glib::Propagation {
19        self.parent_plug_removed()
20    }
21}
22
23pub trait SocketImplExt: SocketImpl {
24    fn parent_plug_added(&self) {
25        unsafe {
26            let data = Self::type_data();
27            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkSocketClass;
28            if let Some(f) = (*parent_class).plug_added {
29                f(self.obj().unsafe_cast_ref::<Socket>().to_glib_none().0)
30            }
31        }
32    }
33    fn parent_plug_removed(&self) -> glib::Propagation {
34        unsafe {
35            let data = Self::type_data();
36            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkSocketClass;
37            if let Some(f) = (*parent_class).plug_removed {
38                glib::Propagation::from_glib(f(self
39                    .obj()
40                    .unsafe_cast_ref::<Socket>()
41                    .to_glib_none()
42                    .0))
43            } else {
44                glib::Propagation::Proceed
45            }
46        }
47    }
48}
49
50impl<T: SocketImpl> SocketImplExt for T {}
51
52unsafe impl<T: SocketImpl> IsSubclassable<T> for Socket {
53    fn class_init(class: &mut ::glib::Class<Self>) {
54        Self::parent_class_init::<T>(class);
55
56        if !crate::rt::is_initialized() {
57            panic!("GTK has to be initialized first");
58        }
59
60        let klass = class.as_mut();
61        klass.plug_added = Some(socket_plug_added::<T>);
62        klass.plug_removed = Some(socket_plug_removed::<T>);
63    }
64}
65
66unsafe extern "C" fn socket_plug_added<T: SocketImpl>(ptr: *mut ffi::GtkSocket) {
67    unsafe {
68        let instance = &*(ptr as *mut T::Instance);
69        let imp = instance.imp();
70
71        imp.plug_added()
72    }
73}
74
75unsafe extern "C" fn socket_plug_removed<T: SocketImpl>(
76    ptr: *mut ffi::GtkSocket,
77) -> glib::ffi::gboolean {
78    unsafe {
79        let instance = &*(ptr as *mut T::Instance);
80        let imp = instance.imp();
81
82        imp.plug_removed().into_glib()
83    }
84}