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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::Paintable;
use glib::object::IsA;
use glib::translate::*;
use std::fmt;
use std::ptr;

glib::wrapper! {
    /// [`Texture`][crate::Texture] is the basic element used to refer to pixel data.
    ///
    /// It is primarily meant for pixel data that will not change over
    /// multiple frames, and will be used for a long time.
    ///
    /// There are various ways to create [`Texture`][crate::Texture] objects from a
    /// [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf], or a Cairo surface, or other pixel data.
    ///
    /// The ownership of the pixel data is transferred to the [`Texture`][crate::Texture]
    /// instance; you can only make a copy of it, via
    /// [``TextureExtManual::download()``][crate::prelude::`TextureExtManual::download()`].
    ///
    /// [`Texture`][crate::Texture] is an immutable object: That means you cannot change
    /// anything about it other than increasing the reference count via
    /// `g_object_ref()`.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`TextureExt`][trait@crate::prelude::TextureExt], [`PaintableExt`][trait@crate::prelude::PaintableExt], [`TextureExtManual`][trait@crate::prelude::TextureExtManual]
    #[doc(alias = "GdkTexture")]
    pub struct Texture(Object<ffi::GdkTexture, ffi::GdkTextureClass>) @implements Paintable;

    match fn {
        type_ => || ffi::gdk_texture_get_type(),
    }
}

impl Texture {
    /// Creates a new texture object representing the [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
    /// ## `pixbuf`
    /// a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
    ///
    /// # Returns
    ///
    /// a new [`Texture`][crate::Texture]
    #[doc(alias = "gdk_texture_new_for_pixbuf")]
    #[doc(alias = "new_for_pixbuf")]
    pub fn for_pixbuf(pixbuf: &gdk_pixbuf::Pixbuf) -> Texture {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gdk_texture_new_for_pixbuf(pixbuf.to_glib_none().0)) }
    }

    /// Creates a new texture by loading an image from a file.
    ///
    /// The file format is detected automatically. The supported formats
    /// are PNG and JPEG, though more formats might be available.
    ///
    /// If [`None`] is returned, then `error` will be set.
    /// ## `file`
    /// `GFile` to load
    ///
    /// # Returns
    ///
    /// A newly-created [`Texture`][crate::Texture]
    #[doc(alias = "gdk_texture_new_from_file")]
    #[doc(alias = "new_from_file")]
    pub fn from_file<P: IsA<gio::File>>(file: &P) -> Result<Texture, glib::Error> {
        assert_initialized_main_thread!();
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::gdk_texture_new_from_file(file.as_ref().to_glib_none().0, &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Creates a new texture by loading an image from a resource.
    ///
    /// The file format is detected automatically. The supported formats
    /// are PNG and JPEG, though more formats might be available.
    ///
    /// It is a fatal error if `resource_path` does not specify a valid
    /// image resource and the program will abort if that happens.
    /// If you are unsure about the validity of a resource, use
    /// [``from_file()``][`Self::from_file()`] to load it.
    /// ## `resource_path`
    /// the path of the resource file
    ///
    /// # Returns
    ///
    /// A newly-created [`Texture`][crate::Texture]
    #[doc(alias = "gdk_texture_new_from_resource")]
    #[doc(alias = "new_from_resource")]
    pub fn from_resource(resource_path: &str) -> Texture {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gdk_texture_new_from_resource(
                resource_path.to_glib_none().0,
            ))
        }
    }
}

pub const NONE_TEXTURE: Option<&Texture> = None;

/// Trait containing all [`struct@Texture`] methods.
///
/// # Implementors
///
/// [`GLTexture`][struct@crate::GLTexture], [`MemoryTexture`][struct@crate::MemoryTexture], [`Texture`][struct@crate::Texture]
pub trait TextureExt: 'static {
    /// Returns the height of the `self`, in pixels.
    ///
    /// # Returns
    ///
    /// the height of the [`Texture`][crate::Texture]
    #[doc(alias = "gdk_texture_get_height")]
    #[doc(alias = "get_height")]
    fn height(&self) -> i32;

    /// Returns the width of `self`, in pixels.
    ///
    /// # Returns
    ///
    /// the width of the [`Texture`][crate::Texture]
    #[doc(alias = "gdk_texture_get_width")]
    #[doc(alias = "get_width")]
    fn width(&self) -> i32;

    /// Store the given `self` to the `filename` as a PNG file.
    ///
    /// This is a utility function intended for debugging and testing.
    /// If you want more control over formats, proper error handling or
    /// want to store to a `GFile` or other location, you might want to
    /// look into using the gdk-pixbuf library.
    /// ## `filename`
    /// the filename to store to
    ///
    /// # Returns
    ///
    /// [`true`] if saving succeeded, [`false`] on failure.
    #[doc(alias = "gdk_texture_save_to_png")]
    fn save_to_png<P: AsRef<std::path::Path>>(&self, filename: P) -> bool;
}

impl<O: IsA<Texture>> TextureExt for O {
    fn height(&self) -> i32 {
        unsafe { ffi::gdk_texture_get_height(self.as_ref().to_glib_none().0) }
    }

    fn width(&self) -> i32 {
        unsafe { ffi::gdk_texture_get_width(self.as_ref().to_glib_none().0) }
    }

    fn save_to_png<P: AsRef<std::path::Path>>(&self, filename: P) -> bool {
        unsafe {
            from_glib(ffi::gdk_texture_save_to_png(
                self.as_ref().to_glib_none().0,
                filename.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for Texture {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Texture")
    }
}