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
use crate::subclass::prelude::*;
use crate::{ColorChooser, Orientation};
use gdk::RGBA;
use glib::translate::*;
use glib::Cast;
pub trait ColorChooserImpl: ObjectImpl {
fn add_palette(
&self,
color_chooser: &Self::Type,
orientation: Orientation,
colors_per_line: i32,
colors: &[RGBA],
) {
self.parent_add_palette(color_chooser, orientation, colors_per_line, colors);
}
fn color_activated(&self, color_chooser: &Self::Type, rgba: RGBA) {
self.parent_color_activated(color_chooser, rgba);
}
#[doc(alias = "get_rgba")]
fn rgba(&self, color_chooser: &Self::Type) -> RGBA;
fn set_rgba(&self, color_chooser: &Self::Type, rgba: RGBA);
}
pub trait ColorChooserImplExt: ObjectSubclass {
fn parent_add_palette(
&self,
color_chooser: &Self::Type,
orientation: Orientation,
colors_per_line: i32,
colors: &[RGBA],
);
fn parent_color_activated(&self, color_chooser: &Self::Type, rgba: RGBA);
fn parent_rgba(&self, color_chooser: &Self::Type) -> RGBA;
fn parent_set_rgba(&self, color_chooser: &Self::Type, rgba: RGBA);
}
impl<T: ColorChooserImpl> ColorChooserImplExt for T {
fn parent_add_palette(
&self,
color_chooser: &Self::Type,
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(
color_chooser
.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, color_chooser: &Self::Type, 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(
color_chooser
.unsafe_cast_ref::<ColorChooser>()
.to_glib_none()
.0,
rgba.to_glib_none().0,
)
}
}
}
fn parent_rgba(&self, color_chooser: &Self::Type) -> 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(
color_chooser
.unsafe_cast_ref::<ColorChooser>()
.to_glib_none()
.0,
rgba,
);
from_glib_none(rgba)
}
}
fn parent_set_rgba(&self, color_chooser: &Self::Type, 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(
color_chooser
.unsafe_cast_ref::<ColorChooser>()
.to_glib_none()
.0,
rgba.to_glib_none().0,
)
}
}
}
}
unsafe impl<T: ColorChooserImpl> IsImplementable<T> for ColorChooser {
fn interface_init(iface: &mut glib::Interface<Self>) {
let iface = iface.as_mut();
if !crate::rt::is_initialized() {
panic!("GTK has to be initialized first");
}
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>);
}
fn instance_init(_instance: &mut glib::subclass::InitializingObject<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.impl_();
let colors = std::slice::from_raw_parts(colorsptr as *const RGBA, total as usize);
imp.add_palette(
from_glib_borrow::<_, ColorChooser>(color_chooser).unsafe_cast_ref(),
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.impl_();
imp.color_activated(
from_glib_borrow::<_, ColorChooser>(color_chooser).unsafe_cast_ref(),
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.impl_();
let rgba = imp.rgba(from_glib_borrow::<_, ColorChooser>(color_chooser).unsafe_cast_ref());
*(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.impl_();
imp.set_rgba(
from_glib_borrow::<_, ColorChooser>(color_chooser).unsafe_cast_ref(),
from_glib_none(rgba),
)
}