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