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