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