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
128mod sealed {
129 pub trait Sealed {}
130 impl<T: super::IsA<super::Paintable>> Sealed for T {}
131}
132
133/// Trait containing all [`struct@Paintable`] methods.
134///
135/// # Implementors
136///
137/// [`DmabufTexture`][struct@crate::DmabufTexture], [`GLTexture`][struct@crate::GLTexture], [`MemoryTexture`][struct@crate::MemoryTexture], [`Paintable`][struct@crate::Paintable], [`Texture`][struct@crate::Texture]
138pub trait PaintableExt: IsA<Paintable> + sealed::Sealed + 'static {
139 /// Compute a concrete size for the [`Paintable`][crate::Paintable].
140 ///
141 /// Applies the sizing algorithm outlined in the
142 /// [CSS Image spec](https://drafts.csswg.org/css-images-3/#default-sizing)
143 /// to the given @self. See that link for more details.
144 ///
145 /// It is not necessary to call this function when both @specified_width
146 /// and @specified_height are known, but it is useful to call this
147 /// function in GtkWidget:measure implementations to compute the
148 /// other dimension when only one dimension is given.
149 /// ## `specified_width`
150 /// the width @self could be drawn into or
151 /// 0.0 if unknown
152 /// ## `specified_height`
153 /// the height @self could be drawn into or
154 /// 0.0 if unknown
155 /// ## `default_width`
156 /// the width @self would be drawn into if
157 /// no other constraints were given
158 /// ## `default_height`
159 /// the height @self would be drawn into if
160 /// no other constraints were given
161 ///
162 /// # Returns
163 ///
164 ///
165 /// ## `concrete_width`
166 /// will be set to the concrete width computed
167 ///
168 /// ## `concrete_height`
169 /// will be set to the concrete height computed
170 #[doc(alias = "gdk_paintable_compute_concrete_size")]
171 fn compute_concrete_size(
172 &self,
173 specified_width: f64,
174 specified_height: f64,
175 default_width: f64,
176 default_height: f64,
177 ) -> (f64, f64) {
178 unsafe {
179 let mut concrete_width = std::mem::MaybeUninit::uninit();
180 let mut concrete_height = std::mem::MaybeUninit::uninit();
181 ffi::gdk_paintable_compute_concrete_size(
182 self.as_ref().to_glib_none().0,
183 specified_width,
184 specified_height,
185 default_width,
186 default_height,
187 concrete_width.as_mut_ptr(),
188 concrete_height.as_mut_ptr(),
189 );
190 (concrete_width.assume_init(), concrete_height.assume_init())
191 }
192 }
193
194 /// Gets an immutable paintable for the current contents displayed by @self.
195 ///
196 /// This is useful when you want to retain the current state of an animation,
197 /// for example to take a screenshot of a running animation.
198 ///
199 /// If the @self is already immutable, it will return itself.
200 ///
201 /// # Returns
202 ///
203 /// An immutable paintable for the current
204 /// contents of @self
205 #[doc(alias = "gdk_paintable_get_current_image")]
206 #[doc(alias = "get_current_image")]
207 #[must_use]
208 fn current_image(&self) -> Paintable {
209 unsafe {
210 from_glib_full(ffi::gdk_paintable_get_current_image(
211 self.as_ref().to_glib_none().0,
212 ))
213 }
214 }
215
216 /// Get flags for the paintable.
217 ///
218 /// This is oftentimes useful for optimizations.
219 ///
220 /// See [`PaintableFlags`][crate::PaintableFlags] for the flags and what they mean.
221 ///
222 /// # Returns
223 ///
224 /// The [`PaintableFlags`][crate::PaintableFlags] for this paintable
225 #[doc(alias = "gdk_paintable_get_flags")]
226 #[doc(alias = "get_flags")]
227 fn flags(&self) -> PaintableFlags {
228 unsafe { from_glib(ffi::gdk_paintable_get_flags(self.as_ref().to_glib_none().0)) }
229 }
230
231 /// Gets the preferred aspect ratio the @self would like to be displayed at.
232 ///
233 /// The aspect ratio is the width divided by the height, so a value of 0.5
234 /// means that the @self prefers to be displayed twice as high as it
235 /// is wide. Consumers of this interface can use this to preserve aspect
236 /// ratio when displaying the paintable.
237 ///
238 /// This is a purely informational value and does not in any way limit the
239 /// values that may be passed to [`snapshot()`][Self::snapshot()].
240 ///
241 /// Usually when a @self returns nonzero values from
242 /// [`intrinsic_width()`][Self::intrinsic_width()] and
243 /// [`intrinsic_height()`][Self::intrinsic_height()] the aspect ratio
244 /// should conform to those values, though that is not required.
245 ///
246 /// If the @self does not have a preferred aspect ratio,
247 /// it returns 0. Negative values are never returned.
248 ///
249 /// # Returns
250 ///
251 /// the intrinsic aspect ratio of @self or 0 if none.
252 #[doc(alias = "gdk_paintable_get_intrinsic_aspect_ratio")]
253 #[doc(alias = "get_intrinsic_aspect_ratio")]
254 fn intrinsic_aspect_ratio(&self) -> f64 {
255 unsafe { ffi::gdk_paintable_get_intrinsic_aspect_ratio(self.as_ref().to_glib_none().0) }
256 }
257
258 /// Gets the preferred height the @self would like to be displayed at.
259 ///
260 /// Consumers of this interface can use this to reserve enough space to draw
261 /// the paintable.
262 ///
263 /// This is a purely informational value and does not in any way limit the
264 /// values that may be passed to [`snapshot()`][Self::snapshot()].
265 ///
266 /// If the @self does not have a preferred height, it returns 0.
267 /// Negative values are never returned.
268 ///
269 /// # Returns
270 ///
271 /// the intrinsic height of @self or 0 if none.
272 #[doc(alias = "gdk_paintable_get_intrinsic_height")]
273 #[doc(alias = "get_intrinsic_height")]
274 fn intrinsic_height(&self) -> i32 {
275 unsafe { ffi::gdk_paintable_get_intrinsic_height(self.as_ref().to_glib_none().0) }
276 }
277
278 /// Gets the preferred width the @self would like to be displayed at.
279 ///
280 /// Consumers of this interface can use this to reserve enough space to draw
281 /// the paintable.
282 ///
283 /// This is a purely informational value and does not in any way limit the
284 /// values that may be passed to [`snapshot()`][Self::snapshot()].
285 ///
286 /// If the @self does not have a preferred width, it returns 0.
287 /// Negative values are never returned.
288 ///
289 /// # Returns
290 ///
291 /// the intrinsic width of @self or 0 if none.
292 #[doc(alias = "gdk_paintable_get_intrinsic_width")]
293 #[doc(alias = "get_intrinsic_width")]
294 fn intrinsic_width(&self) -> i32 {
295 unsafe { ffi::gdk_paintable_get_intrinsic_width(self.as_ref().to_glib_none().0) }
296 }
297
298 /// Called by implementations of [`Paintable`][crate::Paintable] to invalidate their contents.
299 ///
300 /// Unless the contents are invalidated, implementations must guarantee that
301 /// multiple calls of [`snapshot()`][Self::snapshot()] produce the same output.
302 ///
303 /// This function will emit the [`invalidate-contents`][struct@crate::Paintable#invalidate-contents]
304 /// signal.
305 ///
306 /// If a @self reports the [`PaintableFlags::CONTENTS`][crate::PaintableFlags::CONTENTS] flag,
307 /// it must not call this function.
308 #[doc(alias = "gdk_paintable_invalidate_contents")]
309 fn invalidate_contents(&self) {
310 unsafe {
311 ffi::gdk_paintable_invalidate_contents(self.as_ref().to_glib_none().0);
312 }
313 }
314
315 /// Called by implementations of [`Paintable`][crate::Paintable] to invalidate their size.
316 ///
317 /// As long as the size is not invalidated, @self must return the same
318 /// values for its intrinsic width, height and aspect ratio.
319 ///
320 /// This function will emit the [`invalidate-size`][struct@crate::Paintable#invalidate-size]
321 /// signal.
322 ///
323 /// If a @self reports the [`PaintableFlags::SIZE`][crate::PaintableFlags::SIZE] flag,
324 /// it must not call this function.
325 #[doc(alias = "gdk_paintable_invalidate_size")]
326 fn invalidate_size(&self) {
327 unsafe {
328 ffi::gdk_paintable_invalidate_size(self.as_ref().to_glib_none().0);
329 }
330 }
331
332 /// Snapshots the given paintable with the given @width and @height.
333 ///
334 /// The paintable is drawn at the current (0,0) offset of the @snapshot.
335 /// If @width and @height are not larger than zero, this function will
336 /// do nothing.
337 /// ## `snapshot`
338 /// a [`Snapshot`][crate::Snapshot] to snapshot to
339 /// ## `width`
340 /// width to snapshot in
341 /// ## `height`
342 /// height to snapshot in
343 #[doc(alias = "gdk_paintable_snapshot")]
344 fn snapshot(&self, snapshot: &impl IsA<Snapshot>, width: f64, height: f64) {
345 unsafe {
346 ffi::gdk_paintable_snapshot(
347 self.as_ref().to_glib_none().0,
348 snapshot.as_ref().to_glib_none().0,
349 width,
350 height,
351 );
352 }
353 }
354
355 /// Emitted when the contents of the @paintable change.
356 ///
357 /// Examples for such an event would be videos changing to the next frame or
358 /// the icon theme for an icon changing.
359 #[doc(alias = "invalidate-contents")]
360 fn connect_invalidate_contents<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
361 unsafe extern "C" fn invalidate_contents_trampoline<
362 P: IsA<Paintable>,
363 F: Fn(&P) + 'static,
364 >(
365 this: *mut ffi::GdkPaintable,
366 f: glib::ffi::gpointer,
367 ) {
368 let f: &F = &*(f as *const F);
369 f(Paintable::from_glib_borrow(this).unsafe_cast_ref())
370 }
371 unsafe {
372 let f: Box_<F> = Box_::new(f);
373 connect_raw(
374 self.as_ptr() as *mut _,
375 b"invalidate-contents\0".as_ptr() as *const _,
376 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
377 invalidate_contents_trampoline::<Self, F> as *const (),
378 )),
379 Box_::into_raw(f),
380 )
381 }
382 }
383
384 /// Emitted when the intrinsic size of the @paintable changes.
385 ///
386 /// This means the values reported by at least one of
387 /// [`intrinsic_width()`][Self::intrinsic_width()],
388 /// [`intrinsic_height()`][Self::intrinsic_height()] or
389 /// [`intrinsic_aspect_ratio()`][Self::intrinsic_aspect_ratio()]
390 /// has changed.
391 ///
392 /// Examples for such an event would be a paintable displaying
393 /// the contents of a toplevel surface being resized.
394 #[doc(alias = "invalidate-size")]
395 fn connect_invalidate_size<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
396 unsafe extern "C" fn invalidate_size_trampoline<P: IsA<Paintable>, F: Fn(&P) + 'static>(
397 this: *mut ffi::GdkPaintable,
398 f: glib::ffi::gpointer,
399 ) {
400 let f: &F = &*(f as *const F);
401 f(Paintable::from_glib_borrow(this).unsafe_cast_ref())
402 }
403 unsafe {
404 let f: Box_<F> = Box_::new(f);
405 connect_raw(
406 self.as_ptr() as *mut _,
407 b"invalidate-size\0".as_ptr() as *const _,
408 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
409 invalidate_size_trampoline::<Self, F> as *const (),
410 )),
411 Box_::into_raw(f),
412 )
413 }
414 }
415}
416
417impl<O: IsA<Paintable>> PaintableExt for O {}