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
use crate::image::Image;
pub use crate::prelude::*;
use fltk_sys::widget::*;
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw;
/// An abstract type, shouldn't be instantiated in user code
#[derive(WidgetExt, Debug)]
pub struct Widget {
_inner: *mut Fl_Widget,
_tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}
// /// A conversion function for internal use
// impl<W: WidgetExt> From<W> for Widget {
// fn from(s: W) -> Self {
// let widg: *mut Fl_Widget = s.as_widget_ptr();
// Widget { _inner: widg }
// }
// }
impl Widget {
/// Initialize a Widget base from a pointer
/// # Safety
/// The pointer must be valid
pub unsafe fn from_raw(ptr: *mut Fl_Widget) -> Self {
assert!(!ptr.is_null());
let tracker = fltk_sys::fl::Fl_Widget_Tracker_new(ptr as *mut fltk_sys::fl::Fl_Widget);
assert!(!tracker.is_null());
Widget {
_inner: ptr,
_tracker: tracker,
}
}
/// Returns the inner pointer
/// # Safety
/// Can return multiple mutable pointers to the widget
pub unsafe fn as_ptr(&self) -> *mut Fl_Widget {
self._inner
}
/// Transform Widget base to another Widget
/// # Safety
/// Can be if used to downcast to an incorrect widget type
pub unsafe fn into<W: WidgetExt>(self) -> W {
assert!(!self.was_deleted());
W::from_widget_ptr(self._inner)
}
/// Deletes a widget
pub fn delete<W: WidgetExt>(mut wid: W) {
wid.delete()
}
}