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

use crate::{MemoryFormat, Texture};
use glib::{prelude::*, translate::*};
use std::mem;

glib::wrapper! {
    /// The [`TextureDownloader`][crate::TextureDownloader] is used to download the contents of a
    /// [`Texture`][crate::Texture].
    ///
    /// It is intended to be created as a short-term object for a single download,
    /// but can be used for multipe downloads of different textures or with different
    /// settings.
    ///
    /// [`TextureDownloader`][crate::TextureDownloader] can be used to convert data between different formats.
    /// Create a [`Texture`][crate::Texture] for the existing format and then download it in a
    /// different format.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct TextureDownloader(Boxed<ffi::GdkTextureDownloader>);

    match fn {
        copy => |ptr| ffi::gdk_texture_downloader_copy(ptr),
        free => |ptr| ffi::gdk_texture_downloader_free(ptr),
        type_ => || ffi::gdk_texture_downloader_get_type(),
    }
}

impl TextureDownloader {
    /// Creates a new texture downloader for @texture.
    /// ## `texture`
    /// texture to download
    ///
    /// # Returns
    ///
    /// A new texture downloader
    #[doc(alias = "gdk_texture_downloader_new")]
    pub fn new(texture: &impl IsA<Texture>) -> TextureDownloader {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::gdk_texture_downloader_new(
                texture.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Downloads the given texture pixels into a `GBytes`. The rowstride will
    /// be stored in the stride value.
    ///
    /// This function will abort if it tries to download a large texture and
    /// fails to allocate memory. If you think that may happen, you should handle
    /// memory allocation yourself and use `Gdk::TextureDownloader::download_into()`
    /// once allocation succeeded.
    ///
    /// # Returns
    ///
    /// The downloaded pixels
    ///
    /// ## `out_stride`
    /// The stride of the resulting data in bytes
    #[doc(alias = "gdk_texture_downloader_download_bytes")]
    pub fn download_bytes(&self) -> (glib::Bytes, usize) {
        unsafe {
            let mut out_stride = mem::MaybeUninit::uninit();
            let ret = from_glib_full(ffi::gdk_texture_downloader_download_bytes(
                self.to_glib_none().0,
                out_stride.as_mut_ptr(),
            ));
            (ret, out_stride.assume_init())
        }
    }

    //#[doc(alias = "gdk_texture_downloader_download_into")]
    //pub fn download_into(&self, data: &[u8], stride: usize) {
    //    unsafe { TODO: call ffi:gdk_texture_downloader_download_into() }
    //}

    /// Gets the format that the data will be downloaded in.
    ///
    /// # Returns
    ///
    /// The format of the download
    #[doc(alias = "gdk_texture_downloader_get_format")]
    #[doc(alias = "get_format")]
    pub fn format(&self) -> MemoryFormat {
        unsafe {
            from_glib(ffi::gdk_texture_downloader_get_format(
                self.to_glib_none().0,
            ))
        }
    }

    /// Gets the texture that the downloader will download.
    ///
    /// # Returns
    ///
    /// The texture to download
    #[doc(alias = "gdk_texture_downloader_get_texture")]
    #[doc(alias = "get_texture")]
    pub fn texture(&self) -> Texture {
        unsafe {
            from_glib_none(ffi::gdk_texture_downloader_get_texture(
                self.to_glib_none().0,
            ))
        }
    }

    /// Sets the format the downloader will download.
    ///
    /// By default, GDK_MEMORY_DEFAULT is set.
    /// ## `format`
    /// the format to use
    #[doc(alias = "gdk_texture_downloader_set_format")]
    pub fn set_format(&mut self, format: MemoryFormat) {
        unsafe {
            ffi::gdk_texture_downloader_set_format(self.to_glib_none_mut().0, format.into_glib());
        }
    }

    /// Changes the texture the downloader will download.
    /// ## `texture`
    /// the new texture to download
    #[doc(alias = "gdk_texture_downloader_set_texture")]
    pub fn set_texture(&mut self, texture: &impl IsA<Texture>) {
        unsafe {
            ffi::gdk_texture_downloader_set_texture(
                self.to_glib_none_mut().0,
                texture.as_ref().to_glib_none().0,
            );
        }
    }
}