clutter/auto/
image.rs

1use crate::Content;
2use glib::{object::IsA, translate::*};
3use std::{fmt, ptr};
4
5glib_wrapper! {
6    pub struct Image(Object<ffi::ClutterImage, ffi::ClutterImageClass, ImageClass>) @implements Content;
7
8    match fn {
9        get_type => || ffi::clutter_image_get_type(),
10    }
11}
12
13impl Image {
14    /// Creates a new `Image` instance.
15    ///
16    /// # Returns
17    ///
18    /// the newly created `Image` instance.
19    ///  Use `gobject::ObjectExt::unref` when done.
20    pub fn new() -> Option<Content> {
21        unsafe { from_glib_full(ffi::clutter_image_new()) }
22    }
23}
24
25// impl Default for Image {
26//     fn default() -> Self {
27//         Self::new()
28//     }
29// }
30
31pub const NONE_IMAGE: Option<&Image> = None;
32
33/// Trait containing all `Image` methods.
34///
35/// # Implementors
36///
37/// [`Image`](struct.Image.html)
38pub trait ImageExt: 'static {
39    //fn set_area(&self, data: &[u8], pixel_format: cogl::PixelFormat, rect: &cairo::RectangleInt, row_stride: u32) -> Result<(), glib::Error>;
40
41    /// Sets the image data stored inside a `glib::Bytes` to be displayed by `self`.
42    ///
43    /// If the image data was successfully loaded, the `self` will be invalidated.
44    ///
45    /// In case of error, the `error` value will be set, and this function will
46    /// return `false`.
47    ///
48    /// The image data contained inside the `glib::Bytes` is copied in texture memory,
49    /// and no additional reference is acquired on the `data`.
50    /// ## `data`
51    /// the image data, as a `glib::Bytes`
52    /// ## `pixel_format`
53    /// the Cogl pixel format of the image data
54    /// ## `width`
55    /// the width of the image data
56    /// ## `height`
57    /// the height of the image data
58    /// ## `row_stride`
59    /// the length of each row inside `data`
60    ///
61    /// # Returns
62    ///
63    /// `true` if the image data was successfully loaded,
64    ///  and `false` otherwise.
65    fn set_bytes(
66        &self,
67        data: &glib::Bytes,
68        pixel_format: cogl::PixelFormat,
69        width: u32,
70        height: u32,
71        row_stride: u32,
72    ) -> Result<(), glib::Error>;
73
74    //fn set_data(&self, data: &[u8], pixel_format: cogl::PixelFormat, width: u32, height: u32, row_stride: u32) -> Result<(), glib::Error>;
75}
76
77impl<O: IsA<Image>> ImageExt for O {
78    //fn set_area(&self, data: &[u8], pixel_format: cogl::PixelFormat, rect: &cairo::RectangleInt, row_stride: u32) -> Result<(), glib::Error> {
79    //    unsafe { TODO: call clutter_sys:clutter_image_set_area() }
80    //}
81
82    fn set_bytes(
83        &self,
84        data: &glib::Bytes,
85        pixel_format: cogl::PixelFormat,
86        width: u32,
87        height: u32,
88        row_stride: u32,
89    ) -> Result<(), glib::Error> {
90        unsafe {
91            let mut error = ptr::null_mut();
92            let _ = ffi::clutter_image_set_bytes(
93                self.as_ref().to_glib_none().0,
94                data.to_glib_none().0,
95                pixel_format.to_glib(),
96                width,
97                height,
98                row_stride,
99                &mut error,
100            );
101            if error.is_null() {
102                Ok(())
103            } else {
104                Err(from_glib_full(error))
105            }
106        }
107    }
108
109    //fn set_data(&self, data: &[u8], pixel_format: cogl::PixelFormat, width: u32, height: u32, row_stride: u32) -> Result<(), glib::Error> {
110    //    unsafe { TODO: call clutter_sys:clutter_image_set_data() }
111    //}
112}
113
114impl fmt::Display for Image {
115    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116        write!(f, "Image")
117    }
118}