gtk4/
drawing_area.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{cell::RefCell, ptr};
4
5use glib::translate::*;
6
7use crate::{ffi, prelude::*, DrawingArea};
8
9mod sealed {
10    pub trait Sealed {}
11    impl<T: super::IsA<super::DrawingArea>> Sealed for T {}
12}
13
14// rustdoc-stripper-ignore-next
15/// Trait containing manually implemented methods of
16/// [`DrawingArea`](crate::DrawingArea).
17pub trait DrawingAreaExtManual: sealed::Sealed + IsA<DrawingArea> + 'static {
18    #[doc(alias = "gtk_drawing_area_set_draw_func")]
19    #[doc(alias = "set_draw_func")]
20    fn unset_draw_func(&self) {
21        unsafe {
22            ffi::gtk_drawing_area_set_draw_func(
23                self.as_ref().to_glib_none().0,
24                None,
25                ptr::null_mut(),
26                None,
27            )
28        }
29    }
30
31    /// Setting a draw function is the main thing you want to do when using
32    /// a drawing area.
33    ///
34    /// The draw function is called whenever GTK needs to draw the contents
35    /// of the drawing area to the screen.
36    ///
37    /// The draw function will be called during the drawing stage of GTK.
38    /// In the drawing stage it is not allowed to change properties of any
39    /// GTK widgets or call any functions that would cause any properties
40    /// to be changed. You should restrict yourself exclusively to drawing
41    /// your contents in the draw function.
42    ///
43    /// If what you are drawing does change, call [`WidgetExt::queue_draw()`][crate::prelude::WidgetExt::queue_draw()]
44    /// on the drawing area. This will cause a redraw and will call @draw_func again.
45    /// ## `draw_func`
46    /// callback
47    ///   that lets you draw the drawing area's contents
48    #[doc(alias = "gtk_drawing_area_set_draw_func")]
49    fn set_draw_func<P: FnMut(&DrawingArea, &cairo::Context, i32, i32) + 'static>(
50        &self,
51        draw_func: P,
52    ) {
53        unsafe extern "C" fn draw_func_func<
54            P: FnMut(&DrawingArea, &cairo::Context, i32, i32) + 'static,
55        >(
56            drawing_area: *mut ffi::GtkDrawingArea,
57            cr: *mut cairo::ffi::cairo_t,
58            width: libc::c_int,
59            height: libc::c_int,
60            user_data: glib::ffi::gpointer,
61        ) {
62            let drawing_area = from_glib_borrow(drawing_area);
63            let cr = from_glib_borrow(cr);
64            let callback: &RefCell<P> = &*(user_data as *mut _);
65            (callback.borrow_mut())(&drawing_area, &cr, width, height);
66        }
67
68        unsafe extern "C" fn destroy_func<
69            P: FnMut(&DrawingArea, &cairo::Context, i32, i32) + 'static,
70        >(
71            data: glib::ffi::gpointer,
72        ) {
73            let _callback: Box<RefCell<P>> = Box::from_raw(data as *mut _);
74        }
75
76        let callback: Box<RefCell<P>> = Box::new(RefCell::new(draw_func));
77        unsafe {
78            ffi::gtk_drawing_area_set_draw_func(
79                self.as_ref().to_glib_none().0,
80                Some(draw_func_func::<P> as _),
81                Box::into_raw(callback) as *mut _,
82                Some(destroy_func::<P> as _),
83            );
84        }
85    }
86}
87impl<O: IsA<DrawingArea>> DrawingAreaExtManual for O {}