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