gtk4/auto/
widget_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, Widget};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// [`WidgetPaintable`][crate::WidgetPaintable] is a [`gdk::Paintable`][crate::gdk::Paintable] that displays the contents
15    /// of a widget.
16    ///
17    /// [`WidgetPaintable`][crate::WidgetPaintable] will also take care of the widget not being in a
18    /// state where it can be drawn (like when it isn't shown) and just draw
19    /// nothing or where it does not have a size (like when it is hidden) and
20    /// report no size in that case.
21    ///
22    /// Of course, [`WidgetPaintable`][crate::WidgetPaintable] allows you to monitor widgets for size
23    /// changes by emitting the [`invalidate-size`][struct@crate::gdk::Paintable#invalidate-size] signal
24    /// whenever the size of the widget changes as well as for visual changes by
25    /// emitting the [`invalidate-contents`][struct@crate::gdk::Paintable#invalidate-contents] signal whenever
26    /// the widget changes.
27    ///
28    /// You can use a [`WidgetPaintable`][crate::WidgetPaintable] everywhere a [`gdk::Paintable`][crate::gdk::Paintable] is allowed,
29    /// including using it on a [`Picture`][crate::Picture] (or one of its parents) that it was
30    /// set on itself via gtk_picture_set_paintable(). The paintable will take care
31    /// of recursion when this happens. If you do this however, ensure that the
32    /// [`can-shrink`][struct@crate::Picture#can-shrink] property is set to [`true`] or you might
33    /// end up with an infinitely growing widget.
34    ///
35    /// ## Properties
36    ///
37    ///
38    /// #### `widget`
39    ///  The observed widget or [`None`] if none.
40    ///
41    /// Readable | Writeable
42    ///
43    /// # Implements
44    ///
45    /// [`trait@glib::ObjectExt`], [`trait@gdk::prelude::PaintableExt`]
46    #[doc(alias = "GtkWidgetPaintable")]
47    pub struct WidgetPaintable(Object<ffi::GtkWidgetPaintable, ffi::GtkWidgetPaintableClass>) @implements gdk::Paintable;
48
49    match fn {
50        type_ => || ffi::gtk_widget_paintable_get_type(),
51    }
52}
53
54impl WidgetPaintable {
55    /// Creates a new widget paintable observing the given widget.
56    /// ## `widget`
57    /// a [`Widget`][crate::Widget]
58    ///
59    /// # Returns
60    ///
61    /// a new [`WidgetPaintable`][crate::WidgetPaintable]
62    #[doc(alias = "gtk_widget_paintable_new")]
63    pub fn new(widget: Option<&impl IsA<Widget>>) -> WidgetPaintable {
64        assert_initialized_main_thread!();
65        unsafe {
66            from_glib_full(ffi::gtk_widget_paintable_new(
67                widget.map(|p| p.as_ref()).to_glib_none().0,
68            ))
69        }
70    }
71
72    /// Returns the widget that is observed or [`None`] if none.
73    ///
74    /// # Returns
75    ///
76    /// the observed widget.
77    #[doc(alias = "gtk_widget_paintable_get_widget")]
78    #[doc(alias = "get_widget")]
79    pub fn widget(&self) -> Option<Widget> {
80        unsafe { from_glib_none(ffi::gtk_widget_paintable_get_widget(self.to_glib_none().0)) }
81    }
82
83    /// Sets the widget that should be observed.
84    /// ## `widget`
85    /// the widget to observe
86    #[doc(alias = "gtk_widget_paintable_set_widget")]
87    #[doc(alias = "widget")]
88    pub fn set_widget(&self, widget: Option<&impl IsA<Widget>>) {
89        unsafe {
90            ffi::gtk_widget_paintable_set_widget(
91                self.to_glib_none().0,
92                widget.map(|p| p.as_ref()).to_glib_none().0,
93            );
94        }
95    }
96
97    #[doc(alias = "widget")]
98    pub fn connect_widget_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
99        unsafe extern "C" fn notify_widget_trampoline<F: Fn(&WidgetPaintable) + 'static>(
100            this: *mut ffi::GtkWidgetPaintable,
101            _param_spec: glib::ffi::gpointer,
102            f: glib::ffi::gpointer,
103        ) {
104            let f: &F = &*(f as *const F);
105            f(&from_glib_borrow(this))
106        }
107        unsafe {
108            let f: Box_<F> = Box_::new(f);
109            connect_raw(
110                self.as_ptr() as *mut _,
111                b"notify::widget\0".as_ptr() as *const _,
112                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
113                    notify_widget_trampoline::<F> as *const (),
114                )),
115                Box_::into_raw(f),
116            )
117        }
118    }
119}