gdk4/auto/paintable.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, PaintableFlags, Snapshot};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// An interface for content that can be painted.
16 ///
17 /// The content of a [`Paintable`][crate::Paintable] can be painted anywhere at any size
18 /// without requiring any sort of layout. The interface is inspired by
19 /// similar concepts elsewhere, such as
20 /// [ClutterContent](https://developer.gnome.org/clutter/stable/ClutterContent.html),
21 /// [HTML/CSS Paint Sources](https://www.w3.org/TR/css-images-4/#paint-source),
22 /// or [SVG Paint Servers](https://www.w3.org/TR/SVG2/pservers.html).
23 ///
24 /// A [`Paintable`][crate::Paintable] can be snapshot at any time and size using
25 /// [`PaintableExt::snapshot()`][crate::prelude::PaintableExt::snapshot()]. How the paintable interprets that size and
26 /// if it scales or centers itself into the given rectangle is implementation
27 /// defined, though if you are implementing a [`Paintable`][crate::Paintable] and don't know what
28 /// to do, it is suggested that you scale your paintable ignoring any potential
29 /// aspect ratio.
30 ///
31 /// The contents that a [`Paintable`][crate::Paintable] produces may depend on the [`Snapshot`][crate::Snapshot]
32 /// passed to it. For example, paintables may decide to use more detailed images
33 /// on higher resolution screens or when OpenGL is available. A [`Paintable`][crate::Paintable]
34 /// will however always produce the same output for the same snapshot.
35 ///
36 /// A [`Paintable`][crate::Paintable] may change its contents, meaning that it will now produce
37 /// a different output with the same snapshot. Once that happens, it will call
38 /// [`PaintableExt::invalidate_contents()`][crate::prelude::PaintableExt::invalidate_contents()] which will emit the
39 /// [`invalidate-contents`][struct@crate::Paintable#invalidate-contents] signal. If a paintable is known
40 /// to never change its contents, it will set the [`PaintableFlags::CONTENTS`][crate::PaintableFlags::CONTENTS]
41 /// flag. If a consumer cannot deal with changing contents, it may call
42 /// [`PaintableExt::current_image()`][crate::prelude::PaintableExt::current_image()] which will return a static
43 /// paintable and use that.
44 ///
45 /// A paintable can report an intrinsic (or preferred) size or aspect ratio it
46 /// wishes to be rendered at, though it doesn't have to. Consumers of the interface
47 /// can use this information to layout thepaintable appropriately. Just like the
48 /// contents, the size of a paintable can change. A paintable will indicate this
49 /// by calling [`PaintableExt::invalidate_size()`][crate::prelude::PaintableExt::invalidate_size()] which will emit the
50 /// [`invalidate-size`][struct@crate::Paintable#invalidate-size] signal. And just like for contents,
51 /// if a paintable is known to never change its size, it will set the
52 /// [`PaintableFlags::SIZE`][crate::PaintableFlags::SIZE] flag.
53 ///
54 /// Besides API for applications, there are some functions that are only
55 /// useful for implementing subclasses and should not be used by applications:
56 /// [`PaintableExt::invalidate_contents()`][crate::prelude::PaintableExt::invalidate_contents()],
57 /// [`PaintableExt::invalidate_size()`][crate::prelude::PaintableExt::invalidate_size()],
58 /// [`new_empty()`][Self::new_empty()].
59 ///
60 /// ## Signals
61 ///
62 ///
63 /// #### `invalidate-contents`
64 /// Emitted when the contents of the @paintable change.
65 ///
66 /// Examples for such an event would be videos changing to the next frame or
67 /// the icon theme for an icon changing.
68 ///
69 ///
70 ///
71 ///
72 /// #### `invalidate-size`
73 /// Emitted when the intrinsic size of the @paintable changes.
74 ///
75 /// This means the values reported by at least one of
76 /// [`PaintableExt::intrinsic_width()`][crate::prelude::PaintableExt::intrinsic_width()],
77 /// [`PaintableExt::intrinsic_height()`][crate::prelude::PaintableExt::intrinsic_height()] or
78 /// [`PaintableExt::intrinsic_aspect_ratio()`][crate::prelude::PaintableExt::intrinsic_aspect_ratio()]
79 /// has changed.
80 ///
81 /// Examples for such an event would be a paintable displaying
82 /// the contents of a toplevel surface being resized.
83 ///
84 ///
85 ///
86 /// # Implements
87 ///
88 /// [`PaintableExt`][trait@crate::prelude::PaintableExt]
89 #[doc(alias = "GdkPaintable")]
90 pub struct Paintable(Interface<ffi::GdkPaintable, ffi::GdkPaintableInterface>);
91
92 match fn {
93 type_ => || ffi::gdk_paintable_get_type(),
94 }
95}
96
97impl Paintable {
98 pub const NONE: Option<&'static Paintable> = None;
99
100 /// Returns a paintable that has the given intrinsic size and draws nothing.
101 ///
102 /// This is often useful for implementing the
103 /// `vfunc::Gdk::Paintable::get_current_image` virtual function
104 /// when the paintable is in an incomplete state (like a
105 /// [GtkMediaStream](../gtk4/class.MediaStream.html) before receiving
106 /// the first frame).
107 /// ## `intrinsic_width`
108 /// The intrinsic width to report. Can be 0 for no width.
109 /// ## `intrinsic_height`
110 /// The intrinsic height to report. Can be 0 for no height.
111 ///
112 /// # Returns
113 ///
114 /// a [`Paintable`][crate::Paintable]
115 #[doc(alias = "gdk_paintable_new_empty")]
116 pub fn new_empty(intrinsic_width: i32, intrinsic_height: i32) -> Paintable {
117 assert_initialized_main_thread!();
118 unsafe {
119 from_glib_full(ffi::gdk_paintable_new_empty(
120 intrinsic_width,
121 intrinsic_height,
122 ))
123 }
124 }
125}
126
127/// Trait containing all [`struct@Paintable`] methods.
128///
129/// # Implementors
130///
131/// [`DmabufTexture`][struct@crate::DmabufTexture], [`GLTexture`][struct@crate::GLTexture], [`MemoryTexture`][struct@crate::MemoryTexture], [`Paintable`][struct@crate::Paintable], [`Texture`][struct@crate::Texture]
132pub trait PaintableExt: IsA<Paintable> + 'static {
133 /// Compute a concrete size for the [`Paintable`][crate::Paintable].
134 ///
135 /// Applies the sizing algorithm outlined in the
136 /// [CSS Image spec](https://drafts.csswg.org/css-images-3/#default-sizing)
137 /// to the given @self. See that link for more details.
138 ///
139 /// It is not necessary to call this function when both @specified_width
140 /// and @specified_height are known, but it is useful to call this
141 /// function in GtkWidget:measure implementations to compute the
142 /// other dimension when only one dimension is given.
143 /// ## `specified_width`
144 /// the width @self could be drawn into or
145 /// 0.0 if unknown
146 /// ## `specified_height`
147 /// the height @self could be drawn into or
148 /// 0.0 if unknown
149 /// ## `default_width`
150 /// the width @self would be drawn into if
151 /// no other constraints were given
152 /// ## `default_height`
153 /// the height @self would be drawn into if
154 /// no other constraints were given
155 ///
156 /// # Returns
157 ///
158 ///
159 /// ## `concrete_width`
160 /// will be set to the concrete width computed
161 ///
162 /// ## `concrete_height`
163 /// will be set to the concrete height computed
164 #[doc(alias = "gdk_paintable_compute_concrete_size")]
165 fn compute_concrete_size(
166 &self,
167 specified_width: f64,
168 specified_height: f64,
169 default_width: f64,
170 default_height: f64,
171 ) -> (f64, f64) {
172 unsafe {
173 let mut concrete_width = std::mem::MaybeUninit::uninit();
174 let mut concrete_height = std::mem::MaybeUninit::uninit();
175 ffi::gdk_paintable_compute_concrete_size(
176 self.as_ref().to_glib_none().0,
177 specified_width,
178 specified_height,
179 default_width,
180 default_height,
181 concrete_width.as_mut_ptr(),
182 concrete_height.as_mut_ptr(),
183 );
184 (concrete_width.assume_init(), concrete_height.assume_init())
185 }
186 }
187
188 /// Gets an immutable paintable for the current contents displayed by @self.
189 ///
190 /// This is useful when you want to retain the current state of an animation,
191 /// for example to take a screenshot of a running animation.
192 ///
193 /// If the @self is already immutable, it will return itself.
194 ///
195 /// # Returns
196 ///
197 /// An immutable paintable for the current
198 /// contents of @self
199 #[doc(alias = "gdk_paintable_get_current_image")]
200 #[doc(alias = "get_current_image")]
201 #[must_use]
202 fn current_image(&self) -> Paintable {
203 unsafe {
204 from_glib_full(ffi::gdk_paintable_get_current_image(
205 self.as_ref().to_glib_none().0,
206 ))
207 }
208 }
209
210 /// Get flags for the paintable.
211 ///
212 /// This is oftentimes useful for optimizations.
213 ///
214 /// See [`PaintableFlags`][crate::PaintableFlags] for the flags and what they mean.
215 ///
216 /// # Returns
217 ///
218 /// The [`PaintableFlags`][crate::PaintableFlags] for this paintable
219 #[doc(alias = "gdk_paintable_get_flags")]
220 #[doc(alias = "get_flags")]
221 fn flags(&self) -> PaintableFlags {
222 unsafe { from_glib(ffi::gdk_paintable_get_flags(self.as_ref().to_glib_none().0)) }
223 }
224
225 /// Gets the preferred aspect ratio the @self would like to be displayed at.
226 ///
227 /// The aspect ratio is the width divided by the height, so a value of 0.5
228 /// means that the @self prefers to be displayed twice as high as it
229 /// is wide. Consumers of this interface can use this to preserve aspect
230 /// ratio when displaying the paintable.
231 ///
232 /// This is a purely informational value and does not in any way limit the
233 /// values that may be passed to [`snapshot()`][Self::snapshot()].
234 ///
235 /// Usually when a @self returns nonzero values from
236 /// [`intrinsic_width()`][Self::intrinsic_width()] and
237 /// [`intrinsic_height()`][Self::intrinsic_height()] the aspect ratio
238 /// should conform to those values, though that is not required.
239 ///
240 /// If the @self does not have a preferred aspect ratio,
241 /// it returns 0. Negative values are never returned.
242 ///
243 /// # Returns
244 ///
245 /// the intrinsic aspect ratio of @self or 0 if none.
246 #[doc(alias = "gdk_paintable_get_intrinsic_aspect_ratio")]
247 #[doc(alias = "get_intrinsic_aspect_ratio")]
248 fn intrinsic_aspect_ratio(&self) -> f64 {
249 unsafe { ffi::gdk_paintable_get_intrinsic_aspect_ratio(self.as_ref().to_glib_none().0) }
250 }
251
252 /// Gets the preferred height the @self would like to be displayed at.
253 ///
254 /// Consumers of this interface can use this to reserve enough space to draw
255 /// the paintable.
256 ///
257 /// This is a purely informational value and does not in any way limit the
258 /// values that may be passed to [`snapshot()`][Self::snapshot()].
259 ///
260 /// If the @self does not have a preferred height, it returns 0.
261 /// Negative values are never returned.
262 ///
263 /// # Returns
264 ///
265 /// the intrinsic height of @self or 0 if none.
266 #[doc(alias = "gdk_paintable_get_intrinsic_height")]
267 #[doc(alias = "get_intrinsic_height")]
268 fn intrinsic_height(&self) -> i32 {
269 unsafe { ffi::gdk_paintable_get_intrinsic_height(self.as_ref().to_glib_none().0) }
270 }
271
272 /// Gets the preferred width the @self would like to be displayed at.
273 ///
274 /// Consumers of this interface can use this to reserve enough space to draw
275 /// the paintable.
276 ///
277 /// This is a purely informational value and does not in any way limit the
278 /// values that may be passed to [`snapshot()`][Self::snapshot()].
279 ///
280 /// If the @self does not have a preferred width, it returns 0.
281 /// Negative values are never returned.
282 ///
283 /// # Returns
284 ///
285 /// the intrinsic width of @self or 0 if none.
286 #[doc(alias = "gdk_paintable_get_intrinsic_width")]
287 #[doc(alias = "get_intrinsic_width")]
288 fn intrinsic_width(&self) -> i32 {
289 unsafe { ffi::gdk_paintable_get_intrinsic_width(self.as_ref().to_glib_none().0) }
290 }
291
292 /// Called by implementations of [`Paintable`][crate::Paintable] to invalidate their contents.
293 ///
294 /// Unless the contents are invalidated, implementations must guarantee that
295 /// multiple calls of [`snapshot()`][Self::snapshot()] produce the same output.
296 ///
297 /// This function will emit the [`invalidate-contents`][struct@crate::Paintable#invalidate-contents]
298 /// signal.
299 ///
300 /// If a @self reports the [`PaintableFlags::CONTENTS`][crate::PaintableFlags::CONTENTS] flag,
301 /// it must not call this function.
302 #[doc(alias = "gdk_paintable_invalidate_contents")]
303 fn invalidate_contents(&self) {
304 unsafe {
305 ffi::gdk_paintable_invalidate_contents(self.as_ref().to_glib_none().0);
306 }
307 }
308
309 /// Called by implementations of [`Paintable`][crate::Paintable] to invalidate their size.
310 ///
311 /// As long as the size is not invalidated, @self must return the same
312 /// values for its intrinsic width, height and aspect ratio.
313 ///
314 /// This function will emit the [`invalidate-size`][struct@crate::Paintable#invalidate-size]
315 /// signal.
316 ///
317 /// If a @self reports the [`PaintableFlags::SIZE`][crate::PaintableFlags::SIZE] flag,
318 /// it must not call this function.
319 #[doc(alias = "gdk_paintable_invalidate_size")]
320 fn invalidate_size(&self) {
321 unsafe {
322 ffi::gdk_paintable_invalidate_size(self.as_ref().to_glib_none().0);
323 }
324 }
325
326 /// Snapshots the given paintable with the given @width and @height.
327 ///
328 /// The paintable is drawn at the current (0,0) offset of the @snapshot.
329 /// If @width and @height are not larger than zero, this function will
330 /// do nothing.
331 /// ## `snapshot`
332 /// a [`Snapshot`][crate::Snapshot] to snapshot to
333 /// ## `width`
334 /// width to snapshot in
335 /// ## `height`
336 /// height to snapshot in
337 #[doc(alias = "gdk_paintable_snapshot")]
338 fn snapshot(&self, snapshot: &impl IsA<Snapshot>, width: f64, height: f64) {
339 unsafe {
340 ffi::gdk_paintable_snapshot(
341 self.as_ref().to_glib_none().0,
342 snapshot.as_ref().to_glib_none().0,
343 width,
344 height,
345 );
346 }
347 }
348
349 /// Emitted when the contents of the @paintable change.
350 ///
351 /// Examples for such an event would be videos changing to the next frame or
352 /// the icon theme for an icon changing.
353 #[doc(alias = "invalidate-contents")]
354 fn connect_invalidate_contents<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
355 unsafe extern "C" fn invalidate_contents_trampoline<
356 P: IsA<Paintable>,
357 F: Fn(&P) + 'static,
358 >(
359 this: *mut ffi::GdkPaintable,
360 f: glib::ffi::gpointer,
361 ) {
362 let f: &F = &*(f as *const F);
363 f(Paintable::from_glib_borrow(this).unsafe_cast_ref())
364 }
365 unsafe {
366 let f: Box_<F> = Box_::new(f);
367 connect_raw(
368 self.as_ptr() as *mut _,
369 c"invalidate-contents".as_ptr() as *const _,
370 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
371 invalidate_contents_trampoline::<Self, F> as *const (),
372 )),
373 Box_::into_raw(f),
374 )
375 }
376 }
377
378 /// Emitted when the intrinsic size of the @paintable changes.
379 ///
380 /// This means the values reported by at least one of
381 /// [`intrinsic_width()`][Self::intrinsic_width()],
382 /// [`intrinsic_height()`][Self::intrinsic_height()] or
383 /// [`intrinsic_aspect_ratio()`][Self::intrinsic_aspect_ratio()]
384 /// has changed.
385 ///
386 /// Examples for such an event would be a paintable displaying
387 /// the contents of a toplevel surface being resized.
388 #[doc(alias = "invalidate-size")]
389 fn connect_invalidate_size<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
390 unsafe extern "C" fn invalidate_size_trampoline<P: IsA<Paintable>, F: Fn(&P) + 'static>(
391 this: *mut ffi::GdkPaintable,
392 f: glib::ffi::gpointer,
393 ) {
394 let f: &F = &*(f as *const F);
395 f(Paintable::from_glib_borrow(this).unsafe_cast_ref())
396 }
397 unsafe {
398 let f: Box_<F> = Box_::new(f);
399 connect_raw(
400 self.as_ptr() as *mut _,
401 c"invalidate-size".as_ptr() as *const _,
402 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
403 invalidate_size_trampoline::<Self, F> as *const (),
404 )),
405 Box_::into_raw(f),
406 )
407 }
408 }
409}
410
411impl<O: IsA<Paintable>> PaintableExt for O {}