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