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

// rustdoc-stripper-ignore-next
//! Traits intended for implementing the [`ColorChooser`](crate::ColorChooser)
//! interface.

use gdk::RGBA;
use glib::translate::*;

use crate::{prelude::*, subclass::prelude::*, ColorChooser, Orientation};

#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub trait ColorChooserImpl: ObjectImpl {
    fn add_palette(&self, orientation: Orientation, colors_per_line: i32, colors: &[RGBA]) {
        self.parent_add_palette(orientation, colors_per_line, colors);
    }

    fn color_activated(&self, rgba: RGBA) {
        self.parent_color_activated(rgba);
    }

    #[doc(alias = "get_rgba")]
    fn rgba(&self) -> RGBA;
    fn set_rgba(&self, rgba: RGBA);
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::ColorChooserImplExt> Sealed for T {}
}

#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub trait ColorChooserImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_add_palette(&self, orientation: Orientation, colors_per_line: i32, colors: &[RGBA]) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ColorChooser>()
                as *const ffi::GtkColorChooserInterface;

            if let Some(func) = (*parent_iface).add_palette {
                let colors_ptr: Vec<gdk::ffi::GdkRGBA> =
                    colors.iter().map(|c| *c.to_glib_none().0).collect();

                func(
                    self.obj()
                        .unsafe_cast_ref::<ColorChooser>()
                        .to_glib_none()
                        .0,
                    orientation.into_glib(),
                    colors_per_line,
                    colors.len() as i32,
                    mut_override(colors_ptr.as_ptr()),
                )
            }
        }
    }

    fn parent_color_activated(&self, rgba: RGBA) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ColorChooser>()
                as *const ffi::GtkColorChooserInterface;

            if let Some(func) = (*parent_iface).color_activated {
                func(
                    self.obj()
                        .unsafe_cast_ref::<ColorChooser>()
                        .to_glib_none()
                        .0,
                    rgba.to_glib_none().0,
                )
            }
        }
    }

    fn parent_rgba(&self) -> RGBA {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ColorChooser>()
                as *const ffi::GtkColorChooserInterface;

            let func = (*parent_iface)
                .get_rgba
                .expect("no parent \"get_rgba\" implementation");
            let rgba = std::ptr::null_mut();
            func(
                self.obj()
                    .unsafe_cast_ref::<ColorChooser>()
                    .to_glib_none()
                    .0,
                rgba,
            );
            from_glib_none(rgba)
        }
    }

    fn parent_set_rgba(&self, rgba: RGBA) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ColorChooser>()
                as *const ffi::GtkColorChooserInterface;

            if let Some(func) = (*parent_iface).set_rgba {
                func(
                    self.obj()
                        .unsafe_cast_ref::<ColorChooser>()
                        .to_glib_none()
                        .0,
                    rgba.to_glib_none().0,
                )
            }
        }
    }
}

impl<T: ColorChooserImpl> ColorChooserImplExt for T {}

unsafe impl<T: ColorChooserImpl> IsImplementable<T> for ColorChooser {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        assert_initialized_main_thread!();

        iface.add_palette = Some(color_chooser_add_palette::<T>);
        iface.color_activated = Some(color_chooser_color_activated::<T>);
        iface.get_rgba = Some(color_chooser_get_rgba::<T>);
        iface.set_rgba = Some(color_chooser_set_rgba::<T>);
    }
}

unsafe extern "C" fn color_chooser_add_palette<T: ColorChooserImpl>(
    color_chooser: *mut ffi::GtkColorChooser,
    orientation: ffi::GtkOrientation,
    colors_per_line: i32,
    total: i32,
    colorsptr: *mut gdk::ffi::GdkRGBA,
) {
    let instance = &*(color_chooser as *mut T::Instance);
    let imp = instance.imp();

    let colors = if total == 0 {
        &[]
    } else {
        std::slice::from_raw_parts(colorsptr as *const RGBA, total as usize)
    };
    imp.add_palette(from_glib(orientation), colors_per_line, colors);
}

unsafe extern "C" fn color_chooser_color_activated<T: ColorChooserImpl>(
    color_chooser: *mut ffi::GtkColorChooser,
    rgba: *const gdk::ffi::GdkRGBA,
) {
    let instance = &*(color_chooser as *mut T::Instance);
    let imp = instance.imp();

    imp.color_activated(from_glib_none(rgba))
}

unsafe extern "C" fn color_chooser_get_rgba<T: ColorChooserImpl>(
    color_chooser: *mut ffi::GtkColorChooser,
    rgbaptr: *const gdk::ffi::GdkRGBA,
) {
    let instance = &*(color_chooser as *mut T::Instance);
    let imp = instance.imp();

    let rgba = imp.rgba();
    *(rgbaptr as *mut gdk::ffi::GdkRGBA) = *rgba.to_glib_none().0;
}

unsafe extern "C" fn color_chooser_set_rgba<T: ColorChooserImpl>(
    color_chooser: *mut ffi::GtkColorChooser,
    rgba: *const gdk::ffi::GdkRGBA,
) {
    let instance = &*(color_chooser as *mut T::Instance);
    let imp = instance.imp();

    imp.set_rgba(from_glib_none(rgba))
}