1use crate::{Pixbuf, PixbufAnimation, PixbufFormat, ffi};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GdkPixbufLoader")]
16 pub struct PixbufLoader(Object<ffi::GdkPixbufLoader, ffi::GdkPixbufLoaderClass>);
17
18 match fn {
19 type_ => || ffi::gdk_pixbuf_loader_get_type(),
20 }
21}
22
23impl PixbufLoader {
24 pub const NONE: Option<&'static PixbufLoader> = None;
25
26 #[doc(alias = "gdk_pixbuf_loader_new")]
27 pub fn new() -> PixbufLoader {
28 unsafe { from_glib_full(ffi::gdk_pixbuf_loader_new()) }
29 }
30
31 #[doc(alias = "gdk_pixbuf_loader_new_with_mime_type")]
32 #[doc(alias = "new_with_mime_type")]
33 pub fn with_mime_type(mime_type: &str) -> Result<PixbufLoader, glib::Error> {
34 unsafe {
35 let mut error = std::ptr::null_mut();
36 let ret =
37 ffi::gdk_pixbuf_loader_new_with_mime_type(mime_type.to_glib_none().0, &mut error);
38 if error.is_null() {
39 Ok(from_glib_full(ret))
40 } else {
41 Err(from_glib_full(error))
42 }
43 }
44 }
45
46 #[doc(alias = "gdk_pixbuf_loader_new_with_type")]
47 #[doc(alias = "new_with_type")]
48 pub fn with_type(image_type: &str) -> Result<PixbufLoader, glib::Error> {
49 unsafe {
50 let mut error = std::ptr::null_mut();
51 let ret = ffi::gdk_pixbuf_loader_new_with_type(image_type.to_glib_none().0, &mut error);
52 if error.is_null() {
53 Ok(from_glib_full(ret))
54 } else {
55 Err(from_glib_full(error))
56 }
57 }
58 }
59}
60
61impl Default for PixbufLoader {
62 fn default() -> Self {
63 Self::new()
64 }
65}
66
67pub trait PixbufLoaderExt: IsA<PixbufLoader> + 'static {
68 #[doc(alias = "gdk_pixbuf_loader_close")]
69 fn close(&self) -> Result<(), glib::Error> {
70 unsafe {
71 let mut error = std::ptr::null_mut();
72 let is_ok = ffi::gdk_pixbuf_loader_close(self.as_ref().to_glib_none().0, &mut error);
73 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
74 if error.is_null() {
75 Ok(())
76 } else {
77 Err(from_glib_full(error))
78 }
79 }
80 }
81
82 #[doc(alias = "gdk_pixbuf_loader_get_animation")]
83 #[doc(alias = "get_animation")]
84 fn animation(&self) -> Option<PixbufAnimation> {
85 unsafe {
86 from_glib_none(ffi::gdk_pixbuf_loader_get_animation(
87 self.as_ref().to_glib_none().0,
88 ))
89 }
90 }
91
92 #[doc(alias = "gdk_pixbuf_loader_get_format")]
93 #[doc(alias = "get_format")]
94 fn format(&self) -> Option<PixbufFormat> {
95 unsafe {
96 from_glib_none(ffi::gdk_pixbuf_loader_get_format(
97 self.as_ref().to_glib_none().0,
98 ))
99 }
100 }
101
102 #[doc(alias = "gdk_pixbuf_loader_get_pixbuf")]
103 #[doc(alias = "get_pixbuf")]
104 fn pixbuf(&self) -> Option<Pixbuf> {
105 unsafe {
106 from_glib_none(ffi::gdk_pixbuf_loader_get_pixbuf(
107 self.as_ref().to_glib_none().0,
108 ))
109 }
110 }
111
112 #[doc(alias = "gdk_pixbuf_loader_set_size")]
113 fn set_size(&self, width: i32, height: i32) {
114 unsafe {
115 ffi::gdk_pixbuf_loader_set_size(self.as_ref().to_glib_none().0, width, height);
116 }
117 }
118
119 #[doc(alias = "gdk_pixbuf_loader_write")]
120 fn write(&self, buf: &[u8]) -> Result<(), glib::Error> {
121 let count = buf.len() as _;
122 unsafe {
123 let mut error = std::ptr::null_mut();
124 let is_ok = ffi::gdk_pixbuf_loader_write(
125 self.as_ref().to_glib_none().0,
126 buf.to_glib_none().0,
127 count,
128 &mut error,
129 );
130 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
131 if error.is_null() {
132 Ok(())
133 } else {
134 Err(from_glib_full(error))
135 }
136 }
137 }
138
139 #[doc(alias = "gdk_pixbuf_loader_write_bytes")]
140 fn write_bytes(&self, buffer: &glib::Bytes) -> Result<(), glib::Error> {
141 unsafe {
142 let mut error = std::ptr::null_mut();
143 let is_ok = ffi::gdk_pixbuf_loader_write_bytes(
144 self.as_ref().to_glib_none().0,
145 buffer.to_glib_none().0,
146 &mut error,
147 );
148 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
149 if error.is_null() {
150 Ok(())
151 } else {
152 Err(from_glib_full(error))
153 }
154 }
155 }
156
157 #[doc(alias = "area-prepared")]
158 fn connect_area_prepared<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
159 unsafe extern "C" fn area_prepared_trampoline<P: IsA<PixbufLoader>, F: Fn(&P) + 'static>(
160 this: *mut ffi::GdkPixbufLoader,
161 f: glib::ffi::gpointer,
162 ) {
163 unsafe {
164 let f: &F = &*(f as *const F);
165 f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
166 }
167 }
168 unsafe {
169 let f: Box_<F> = Box_::new(f);
170 connect_raw(
171 self.as_ptr() as *mut _,
172 c"area-prepared".as_ptr(),
173 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
174 area_prepared_trampoline::<Self, F> as *const (),
175 )),
176 Box_::into_raw(f),
177 )
178 }
179 }
180
181 #[doc(alias = "area-updated")]
182 fn connect_area_updated<F: Fn(&Self, i32, i32, i32, i32) + 'static>(
183 &self,
184 f: F,
185 ) -> SignalHandlerId {
186 unsafe extern "C" fn area_updated_trampoline<
187 P: IsA<PixbufLoader>,
188 F: Fn(&P, i32, i32, i32, i32) + 'static,
189 >(
190 this: *mut ffi::GdkPixbufLoader,
191 x: std::ffi::c_int,
192 y: std::ffi::c_int,
193 width: std::ffi::c_int,
194 height: std::ffi::c_int,
195 f: glib::ffi::gpointer,
196 ) {
197 unsafe {
198 let f: &F = &*(f as *const F);
199 f(
200 PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
201 x,
202 y,
203 width,
204 height,
205 )
206 }
207 }
208 unsafe {
209 let f: Box_<F> = Box_::new(f);
210 connect_raw(
211 self.as_ptr() as *mut _,
212 c"area-updated".as_ptr(),
213 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
214 area_updated_trampoline::<Self, F> as *const (),
215 )),
216 Box_::into_raw(f),
217 )
218 }
219 }
220
221 #[doc(alias = "closed")]
222 fn connect_closed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
223 unsafe extern "C" fn closed_trampoline<P: IsA<PixbufLoader>, F: Fn(&P) + 'static>(
224 this: *mut ffi::GdkPixbufLoader,
225 f: glib::ffi::gpointer,
226 ) {
227 unsafe {
228 let f: &F = &*(f as *const F);
229 f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
230 }
231 }
232 unsafe {
233 let f: Box_<F> = Box_::new(f);
234 connect_raw(
235 self.as_ptr() as *mut _,
236 c"closed".as_ptr(),
237 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
238 closed_trampoline::<Self, F> as *const (),
239 )),
240 Box_::into_raw(f),
241 )
242 }
243 }
244
245 #[doc(alias = "size-prepared")]
246 fn connect_size_prepared<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
247 unsafe extern "C" fn size_prepared_trampoline<
248 P: IsA<PixbufLoader>,
249 F: Fn(&P, i32, i32) + 'static,
250 >(
251 this: *mut ffi::GdkPixbufLoader,
252 width: std::ffi::c_int,
253 height: std::ffi::c_int,
254 f: glib::ffi::gpointer,
255 ) {
256 unsafe {
257 let f: &F = &*(f as *const F);
258 f(
259 PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
260 width,
261 height,
262 )
263 }
264 }
265 unsafe {
266 let f: Box_<F> = Box_::new(f);
267 connect_raw(
268 self.as_ptr() as *mut _,
269 c"size-prepared".as_ptr(),
270 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
271 size_prepared_trampoline::<Self, F> as *const (),
272 )),
273 Box_::into_raw(f),
274 )
275 }
276 }
277}
278
279impl<O: IsA<PixbufLoader>> PixbufLoaderExt for O {}