gdk_pixbuf/
animation.rs

1// Copyright 2013-2015, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use super::Pixbuf;
6use gdk_pixbuf_sys;
7use glib::object::IsA;
8use glib::translate::*;
9use glib::{Error, TimeVal};
10use std::path::Path;
11use std::ptr;
12
13glib_wrapper! {
14    pub struct PixbufAnimationIter(Object<gdk_pixbuf_sys::GdkPixbufAnimationIter, PixbufAnimationIterClass>);
15
16    match fn {
17        get_type => || gdk_pixbuf_sys::gdk_pixbuf_animation_iter_get_type(),
18    }
19}
20
21impl PixbufAnimationIter {
22    pub fn advance(&self, start_time: TimeVal) -> bool {
23        unsafe {
24            from_glib(gdk_pixbuf_sys::gdk_pixbuf_animation_iter_advance(
25                self.to_glib_none().0,
26                &start_time as *const _,
27            ))
28        }
29    }
30
31    pub fn get_pixbuf(&self) -> Pixbuf {
32        unsafe {
33            from_glib_none(gdk_pixbuf_sys::gdk_pixbuf_animation_iter_get_pixbuf(
34                self.to_glib_none().0,
35            ))
36        }
37    }
38
39    pub fn get_delay_time(&self) -> i32 {
40        unsafe { gdk_pixbuf_sys::gdk_pixbuf_animation_iter_get_delay_time(self.to_glib_none().0) }
41    }
42
43    pub fn on_currently_loading_frame(&self) -> bool {
44        unsafe {
45            from_glib(
46                gdk_pixbuf_sys::gdk_pixbuf_animation_iter_on_currently_loading_frame(
47                    self.to_glib_none().0,
48                ),
49            )
50        }
51    }
52}
53
54glib_wrapper! {
55    pub struct PixbufAnimation(Object<gdk_pixbuf_sys::GdkPixbufAnimation, PixbufAnimationClass>);
56
57    match fn {
58        get_type => || gdk_pixbuf_sys::gdk_pixbuf_animation_get_type(),
59    }
60}
61
62impl PixbufAnimation {
63    pub fn new_from_file<T: AsRef<Path>>(file: T) -> Result<PixbufAnimation, Error> {
64        #[cfg(not(windows))]
65        use gdk_pixbuf_sys::gdk_pixbuf_animation_new_from_file;
66        #[cfg(windows)]
67        use gdk_pixbuf_sys::gdk_pixbuf_animation_new_from_file_utf8 as gdk_pixbuf_animation_new_from_file;
68
69        unsafe {
70            let mut error = ptr::null_mut();
71            let ptr =
72                gdk_pixbuf_animation_new_from_file(file.as_ref().to_glib_none().0, &mut error);
73            if error.is_null() {
74                Ok(from_glib_full(ptr))
75            } else {
76                Err(from_glib_full(error))
77            }
78        }
79    }
80
81    pub fn new_from_resource(resource_path: &str) -> Result<PixbufAnimation, Error> {
82        unsafe {
83            let mut error = ptr::null_mut();
84            let ptr = gdk_pixbuf_sys::gdk_pixbuf_animation_new_from_resource(
85                resource_path.to_glib_none().0,
86                &mut error,
87            );
88            if error.is_null() {
89                Ok(from_glib_full(ptr))
90            } else {
91                Err(from_glib_full(error))
92            }
93        }
94    }
95}
96
97pub trait PixbufAnimationExt {
98    fn get_width(&self) -> i32;
99    fn get_height(&self) -> i32;
100    fn get_iter(&self, start_time: TimeVal) -> PixbufAnimationIter;
101    fn is_static_image(&self) -> bool;
102    fn get_static_image(&self) -> Option<Pixbuf>;
103}
104
105impl<T: IsA<PixbufAnimation>> PixbufAnimationExt for T {
106    fn get_width(&self) -> i32 {
107        unsafe { gdk_pixbuf_sys::gdk_pixbuf_animation_get_width(self.as_ref().to_glib_none().0) }
108    }
109
110    fn get_height(&self) -> i32 {
111        unsafe { gdk_pixbuf_sys::gdk_pixbuf_animation_get_height(self.as_ref().to_glib_none().0) }
112    }
113
114    fn get_iter(&self, start_time: TimeVal) -> PixbufAnimationIter {
115        unsafe {
116            from_glib_full(gdk_pixbuf_sys::gdk_pixbuf_animation_get_iter(
117                self.as_ref().to_glib_none().0,
118                &start_time as *const _,
119            ))
120        }
121    }
122
123    fn is_static_image(&self) -> bool {
124        unsafe {
125            from_glib(gdk_pixbuf_sys::gdk_pixbuf_animation_is_static_image(
126                self.as_ref().to_glib_none().0,
127            ))
128        }
129    }
130
131    fn get_static_image(&self) -> Option<Pixbuf> {
132        unsafe {
133            from_glib_none(gdk_pixbuf_sys::gdk_pixbuf_animation_get_static_image(
134                self.as_ref().to_glib_none().0,
135            ))
136        }
137    }
138}