1#![allow(deprecated)]
5
6#[cfg(feature = "v4_16")]
7#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
8use crate::ColorState;
9#[cfg(feature = "v4_10")]
10#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
11use crate::MemoryFormat;
12use crate::{ffi, Paintable};
13use glib::{prelude::*, translate::*};
14
15glib::wrapper! {
16 #[doc(alias = "GdkTexture")]
17 pub struct Texture(Object<ffi::GdkTexture, ffi::GdkTextureClass>) @implements Paintable, gio::Icon, gio::LoadableIcon;
18
19 match fn {
20 type_ => || ffi::gdk_texture_get_type(),
21 }
22}
23
24impl Texture {
25 pub const NONE: Option<&'static Texture> = None;
26
27 #[cfg_attr(feature = "v4_20", deprecated = "Since 4.20")]
28 #[allow(deprecated)]
29 #[doc(alias = "gdk_texture_new_for_pixbuf")]
30 #[doc(alias = "new_for_pixbuf")]
31 pub fn for_pixbuf(pixbuf: &gdk_pixbuf::Pixbuf) -> Texture {
32 assert_initialized_main_thread!();
33 unsafe { from_glib_full(ffi::gdk_texture_new_for_pixbuf(pixbuf.to_glib_none().0)) }
34 }
35
36 #[cfg(feature = "v4_6")]
37 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
38 #[doc(alias = "gdk_texture_new_from_bytes")]
39 #[doc(alias = "new_from_bytes")]
40 pub fn from_bytes(bytes: &glib::Bytes) -> Result<Texture, glib::Error> {
41 assert_initialized_main_thread!();
42 unsafe {
43 let mut error = std::ptr::null_mut();
44 let ret = ffi::gdk_texture_new_from_bytes(bytes.to_glib_none().0, &mut error);
45 if error.is_null() {
46 Ok(from_glib_full(ret))
47 } else {
48 Err(from_glib_full(error))
49 }
50 }
51 }
52
53 #[doc(alias = "gdk_texture_new_from_file")]
54 #[doc(alias = "new_from_file")]
55 pub fn from_file(file: &impl IsA<gio::File>) -> Result<Texture, glib::Error> {
56 assert_initialized_main_thread!();
57 unsafe {
58 let mut error = std::ptr::null_mut();
59 let ret = ffi::gdk_texture_new_from_file(file.as_ref().to_glib_none().0, &mut error);
60 if error.is_null() {
61 Ok(from_glib_full(ret))
62 } else {
63 Err(from_glib_full(error))
64 }
65 }
66 }
67
68 #[cfg(feature = "v4_6")]
69 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
70 #[doc(alias = "gdk_texture_new_from_filename")]
71 #[doc(alias = "new_from_filename")]
72 pub fn from_filename(path: impl AsRef<std::path::Path>) -> Result<Texture, glib::Error> {
73 assert_initialized_main_thread!();
74 unsafe {
75 let mut error = std::ptr::null_mut();
76 let ret =
77 ffi::gdk_texture_new_from_filename(path.as_ref().to_glib_none().0, &mut error);
78 if error.is_null() {
79 Ok(from_glib_full(ret))
80 } else {
81 Err(from_glib_full(error))
82 }
83 }
84 }
85
86 #[doc(alias = "gdk_texture_new_from_resource")]
87 #[doc(alias = "new_from_resource")]
88 pub fn from_resource(resource_path: &str) -> Texture {
89 assert_initialized_main_thread!();
90 unsafe {
91 from_glib_full(ffi::gdk_texture_new_from_resource(
92 resource_path.to_glib_none().0,
93 ))
94 }
95 }
96}
97
98unsafe impl Send for Texture {}
99unsafe impl Sync for Texture {}
100
101pub trait TextureExt: IsA<Texture> + 'static {
102 #[cfg(feature = "v4_16")]
103 #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
104 #[doc(alias = "gdk_texture_get_color_state")]
105 #[doc(alias = "get_color_state")]
106 #[doc(alias = "color-state")]
107 fn color_state(&self) -> ColorState {
108 unsafe {
109 from_glib_none(ffi::gdk_texture_get_color_state(
110 self.as_ref().to_glib_none().0,
111 ))
112 }
113 }
114
115 #[cfg(feature = "v4_10")]
116 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
117 #[doc(alias = "gdk_texture_get_format")]
118 #[doc(alias = "get_format")]
119 fn format(&self) -> MemoryFormat {
120 unsafe { from_glib(ffi::gdk_texture_get_format(self.as_ref().to_glib_none().0)) }
121 }
122
123 #[doc(alias = "gdk_texture_get_height")]
124 #[doc(alias = "get_height")]
125 fn height(&self) -> i32 {
126 unsafe { ffi::gdk_texture_get_height(self.as_ref().to_glib_none().0) }
127 }
128
129 #[doc(alias = "gdk_texture_get_width")]
130 #[doc(alias = "get_width")]
131 fn width(&self) -> i32 {
132 unsafe { ffi::gdk_texture_get_width(self.as_ref().to_glib_none().0) }
133 }
134
135 #[doc(alias = "gdk_texture_save_to_png")]
136 fn save_to_png(
137 &self,
138 filename: impl AsRef<std::path::Path>,
139 ) -> Result<(), glib::error::BoolError> {
140 unsafe {
141 glib::result_from_gboolean!(
142 ffi::gdk_texture_save_to_png(
143 self.as_ref().to_glib_none().0,
144 filename.as_ref().to_glib_none().0
145 ),
146 "Failed to save the texture as png"
147 )
148 }
149 }
150
151 #[cfg(feature = "v4_6")]
152 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
153 #[doc(alias = "gdk_texture_save_to_png_bytes")]
154 fn save_to_png_bytes(&self) -> glib::Bytes {
155 unsafe {
156 from_glib_full(ffi::gdk_texture_save_to_png_bytes(
157 self.as_ref().to_glib_none().0,
158 ))
159 }
160 }
161
162 #[cfg(feature = "v4_6")]
163 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
164 #[doc(alias = "gdk_texture_save_to_tiff")]
165 fn save_to_tiff(
166 &self,
167 filename: impl AsRef<std::path::Path>,
168 ) -> Result<(), glib::error::BoolError> {
169 unsafe {
170 glib::result_from_gboolean!(
171 ffi::gdk_texture_save_to_tiff(
172 self.as_ref().to_glib_none().0,
173 filename.as_ref().to_glib_none().0
174 ),
175 "Failed to save the texture as tiff"
176 )
177 }
178 }
179
180 #[cfg(feature = "v4_6")]
181 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
182 #[doc(alias = "gdk_texture_save_to_tiff_bytes")]
183 fn save_to_tiff_bytes(&self) -> glib::Bytes {
184 unsafe {
185 from_glib_full(ffi::gdk_texture_save_to_tiff_bytes(
186 self.as_ref().to_glib_none().0,
187 ))
188 }
189 }
190}
191
192impl<O: IsA<Texture>> TextureExt for O {}