use crate::image::RgbImage;
pub use crate::prelude::*;
use fltk_sys::draw::*;
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw;
#[derive(Debug)]
pub struct Region {
_inner: *mut raw::c_void,
}
unsafe impl Sync for Region {}
unsafe impl Send for Region {}
#[derive(Debug)]
pub struct Offscreen {
_inner: *mut raw::c_void,
}
unsafe impl Sync for Offscreen {}
unsafe impl Send for Offscreen {}
impl Offscreen {
pub fn new(w: i32, h: i32) -> Option<Offscreen> {
unsafe {
let x = cfl_create_offscreen(w, h);
if x.is_null() {
None
} else {
Some(Offscreen { _inner: x })
}
}
}
pub unsafe fn uninit() -> Offscreen {
Offscreen {
_inner: std::ptr::null_mut(),
}
}
pub fn begin(&self) {
assert!(!self._inner.is_null());
unsafe { cfl_begin_offscreen(self._inner) }
}
pub fn end(&self) {
assert!(!self._inner.is_null());
unsafe { cfl_end_offscreen() }
}
pub fn copy(&self, x: i32, y: i32, w: i32, h: i32, srcx: i32, srcy: i32) {
assert!(!self._inner.is_null());
unsafe { cfl_copy_offscreen(x, y, w, h, self._inner, srcx, srcy) }
}
pub fn rescale(&mut self) {
assert!(!self._inner.is_null());
unsafe { cfl_rescale_offscreen(self._inner) }
}
pub fn is_valid(&self) -> bool {
assert!(!self._inner.is_null());
if self._inner.is_null() {
false
} else {
true
}
}
pub unsafe fn memcpy(&self) -> Offscreen {
assert!(!self._inner.is_null());
Offscreen {
_inner: self._inner,
}
}
}
impl Drop for Offscreen {
fn drop(&mut self) {
unsafe { cfl_delete_offscreen(self._inner) }
}
}
pub fn show_colormap(old_color: Color) -> Color {
unsafe { mem::transmute(Fl_show_colormap(old_color as u32)) }
}
pub fn set_color_rgb(r: u8, g: u8, b: u8) {
unsafe { cfl_set_color_rgb(r, g, b) }
}
pub fn get_color() -> Color {
unsafe { mem::transmute(cfl_get_color()) }
}
pub fn draw_line(x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe {
cfl_line(x1, y1, x2, y2);
}
}
pub fn draw_point(x: i32, y: i32) {
unsafe { cfl_point(x, y) }
}
pub fn draw_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_rect(x, y, w, h) }
}
pub fn draw_rect_with_color(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { cfl_rect_with_color(x, y, w, h, color as u32) }
}
pub fn draw_loop(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
unsafe {
cfl_loop(x1, y1, x2, y2, x3, y3);
}
}
pub fn draw_rect_fill(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { cfl_rectf_with_color(x, y, w, h, color as u32) }
}
pub fn draw_focus_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_focus_rect(x, y, w, h) }
}
pub fn set_draw_color(color: Color) {
unsafe { cfl_set_color_int(color.to_u32()) }
}
pub fn draw_circle(x: f64, y: f64, r: f64) {
unsafe {
cfl_circle(x, y, r);
}
}
pub fn draw_arc(x: i32, y: i32, w: i32, h: i32, a: f64, b: f64) {
unsafe {
cfl_arc(x, y, w, h, a, b);
}
}
pub fn draw_pie(x: i32, y: i32, w: i32, h: i32, a: f64, b: f64) {
unsafe {
cfl_pie(x, y, w, h, a, b);
}
}
pub fn set_line_style(style: LineStyle, width: i32) {
unsafe {
cfl_line_style(
style as i32,
width,
std::ptr::null_mut() as *mut std::os::raw::c_char,
);
}
}
pub fn push_clip(x: i32, y: i32, w: i32, h: i32) {
unsafe {
cfl_push_clip(x, y, w, h);
}
}
pub fn pop_clip() {
unsafe {
cfl_pop_clip();
}
}
pub fn set_clip_region(r: &Region) {
unsafe { cfl_set_clip_region(r._inner) }
}
pub fn clip_region() -> Option<Region> {
unsafe {
let x = cfl_clip_region();
if x.is_null() {
None
} else {
Some(Region { _inner: x })
}
}
}
pub fn push_no_clip() {
unsafe { cfl_push_no_clip() }
}
pub fn not_clipped(x: i32, y: i32, w: i32, h: i32) -> bool {
unsafe {
match cfl_not_clipped(x, y, w, h) {
0 => false,
_ => true,
}
}
}
pub fn restore_clip() {
unsafe { cfl_restore_clip() }
}
#[allow(dead_code)]
fn copy_offscreen(x: i32, y: i32, w: i32, h: i32, pixmap: &Offscreen, srcx: i32, srcy: i32) {
unsafe { cfl_copy_offscreen(x, y, w, h, pixmap._inner, srcx, srcy) }
}
pub fn create_offscreen(w: i32, h: i32) -> Offscreen {
unsafe {
let x = cfl_create_offscreen(w, h);
assert!(!x.is_null());
Offscreen { _inner: x }
}
}
#[allow(dead_code)]
fn begin_offscreen(b: &Offscreen) {
unsafe { cfl_begin_offscreen(b._inner) }
}
pub fn end_offscreen() {
unsafe { cfl_end_offscreen() }
}
#[allow(dead_code)]
fn delete_offscreen(bitmap: &mut Offscreen) {
unsafe { cfl_delete_offscreen(bitmap._inner) }
}
#[allow(dead_code)]
fn rescale_offscreen(ctx: &mut Offscreen) {
unsafe { cfl_rescale_offscreen(ctx._inner) }
}
pub fn transform_x(x: f64, y: f64) -> f64 {
unsafe { cfl_transform_x(x, y) }
}
pub fn transform_y(x: f64, y: f64) -> f64 {
unsafe { cfl_transform_y(x, y) }
}
pub fn transform_dx(x: f64, y: f64) -> f64 {
unsafe { cfl_transform_dx(x, y) }
}
pub fn transform_dy(x: f64, y: f64) -> f64 {
unsafe { cfl_transform_dy(x, y) }
}
pub fn transformed_vertex(xf: f64, yf: f64) {
unsafe { cfl_transformed_vertex(xf, yf) }
}
pub fn draw_rectf(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_rectf(x, y, w, h) }
}
pub fn draw_rectf_with_rgb(x: i32, y: i32, w: i32, h: i32, r: u8, g: u8, b: u8) {
unsafe { cfl_rectf_with_rgb(x, y, w, h, r, g, b) }
}
pub fn draw_line2(x: i32, y: i32, x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe { cfl_line2(x, y, x1, y1, x2, y2) }
}
pub fn draw_loop2(x: i32, y: i32, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
unsafe { cfl_loop2(x, y, x1, y1, x2, y2, x3, y3) }
}
pub fn draw_polygon(x: i32, y: i32, x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe { cfl_polygon(x, y, x1, y1, x2, y2) }
}
pub fn draw_polygon2(x: i32, y: i32, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
unsafe { cfl_polygon2(x, y, x1, y1, x2, y2, x3, y3) }
}
pub fn draw_curve(x0: f64, y0: f64, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
unsafe { cfl_curve(x0, y0, x1, y1, x2, y2, x3, y3) }
}
pub fn draw_arc2(x: f64, y: f64, r: f64, start: f64, end: f64) {
unsafe { cfl_arc2(x, y, r, start, end) }
}
pub fn draw_xyline(x: i32, y: i32, x1: i32) {
unsafe { cfl_xyline(x, y, x1) }
}
pub fn draw_xyline2(x: i32, y: i32, x1: i32, y2: i32) {
unsafe { cfl_xyline2(x, y, x1, y2) }
}
pub fn draw_xyline3(x: i32, y: i32, x1: i32, y2: i32, x3: i32) {
unsafe { cfl_xyline3(x, y, x1, y2, x3) }
}
pub fn draw_yxline(x: i32, y: i32, y1: i32) {
unsafe { cfl_yxline(x, y, y1) }
}
pub fn draw_yxline2(x: i32, y: i32, y1: i32, x2: i32) {
unsafe { cfl_yxline2(x, y, y1, x2) }
}
pub fn draw_yxline3(x: i32, y: i32, y1: i32, x2: i32, y3: i32) {
unsafe { cfl_yxline3(x, y, y1, x2, y3) }
}
pub fn push_matrix() {
unsafe { cfl_push_matrix() }
}
pub fn pop_matrix() {
unsafe { cfl_pop_matrix() }
}
pub fn scale_xy(x: f64, y: f64) {
unsafe { cfl_scale(x, y) }
}
pub fn scale_x(x: f64) {
unsafe { cfl_scale2(x) }
}
pub fn translate(x: f64, y: f64) {
unsafe { cfl_translate(x, y) }
}
pub fn rotate(d: f64) {
unsafe { cfl_rotate(d) }
}
pub fn mult_matrix(a: f64, b: f64, c: f64, d: f64, x: f64, y: f64) {
unsafe { cfl_mult_matrix(a, b, c, d, x, y) }
}
pub fn begin_points() {
unsafe { cfl_begin_points() }
}
pub fn begin_line() {
unsafe { cfl_begin_line() }
}
pub fn begin_loop() {
unsafe { cfl_begin_loop() }
}
pub fn begin_polygon() {
unsafe { cfl_begin_polygon() }
}
pub fn vertex(x: f64, y: f64) {
unsafe { cfl_vertex(x, y) }
}
pub fn end_points() {
unsafe { cfl_end_points() }
}
pub fn end_line() {
unsafe { cfl_end_line() }
}
pub fn end_loop() {
unsafe { cfl_end_loop() }
}
pub fn end_polygon() {
unsafe { cfl_end_polygon() }
}
pub fn begin_complex_polygon() {
unsafe { cfl_begin_complex_polygon() }
}
pub fn gap() {
unsafe { cfl_gap() }
}
pub fn end_complex_polygon() {
unsafe { cfl_end_complex_polygon() }
}
pub fn set_font(face: Font, fsize: u32) {
unsafe { cfl_set_font(face as i32, fsize as i32) }
}
pub fn font() -> Font {
unsafe { mem::transmute(cfl_font()) }
}
pub fn size() -> u32 {
unsafe { cfl_size() as u32 }
}
pub fn height() -> i32 {
unsafe { cfl_height() }
}
pub fn set_height(font: Font, size: u32) {
unsafe {
cfl_set_height(font as i32, size as i32);
}
}
pub fn descent() -> i32 {
unsafe { cfl_descent() }
}
pub fn width(txt: &str) -> f64 {
let txt = CString::new(txt).unwrap();
unsafe { cfl_width(txt.as_ptr()) }
}
pub fn width2(txt: &str, n: i32) -> f64 {
let txt = CString::new(txt).unwrap();
unsafe { cfl_width2(txt.as_ptr(), n) }
}
pub fn char_width(c: char) -> f64 {
unsafe { cfl_width3(c as u32) }
}
pub fn latin1_to_local(txt: &str, n: i32) -> String {
let txt = CString::new(txt).unwrap();
unsafe {
let x = cfl_latin1_to_local(txt.as_ptr(), n);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn local_to_latin1(txt: &str, n: i32) -> String {
let txt = CString::new(txt).unwrap();
unsafe {
let x = cfl_local_to_latin1(txt.as_ptr(), n);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn draw_text(txt: &str, x: i32, y: i32) {
let txt = CString::new(txt).unwrap();
unsafe { cfl_draw(txt.as_ptr(), x, y) }
}
pub fn draw_text2(
s: &str,
x: i32,
y: i32,
w: i32,
h: i32,
align: Align,
) {
let s = CString::new(s).unwrap();
unsafe {
cfl_draw_text2(s.as_ptr(), x, y, w, h, align as i32)
}
}
pub fn draw_text_angled(angle: i32, txt: &str, x: i32, y: i32) {
let txt = CString::new(txt).unwrap();
unsafe { cfl_draw2(angle, txt.as_ptr(), x, y) }
}
pub fn rtl_draw(txt: &str, x: i32, y: i32) {
let n = txt.len() as i32;
let txt = CString::new(txt).unwrap();
unsafe { cfl_rtl_draw(txt.as_ptr(), n, x, y) }
}
pub fn draw_frame(s: &str, x: i32, y: i32, w: i32, h: i32) {
let s = CString::new(s).unwrap();
unsafe { cfl_frame(s.as_ptr(), x, y, w, h) }
}
pub fn draw_frame2(s: &str, x: i32, y: i32, w: i32, h: i32) {
let s = CString::new(s).unwrap();
unsafe { cfl_frame2(s.as_ptr(), x, y, w, h) }
}
pub fn draw_box(box_type: FrameType, x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { cfl_draw_box(box_type as i32, x, y, w, h, color as u32) }
}
pub fn can_do_alpha_blending() -> bool {
unsafe {
match cfl_can_do_alpha_blending() {
0 => false,
_ => true,
}
}
}
pub fn shortcut_label(shortcut: Shortcut) -> String {
unsafe {
let x = cfl_shortcut_label(shortcut as u32);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn overlay_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_overlay_rect(x, y, w, h) }
}
pub fn overlay_clear() {
unsafe { cfl_overlay_clear() }
}
pub fn set_cursor(cursor: Cursor) {
unsafe { cfl_set_cursor(cursor as i32) }
}
pub fn set_cursor_with_color(cursor: Cursor, fg: Color, bg: Color) {
unsafe { cfl_set_cursor2(cursor as i32, fg as i32, bg as i32) }
}
pub fn set_status(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_set_status(x, y, w, h) }
}
pub fn set_spot<Win: WindowExt>(font: Font, size: u32, x: i32, y: i32, w: i32, h: i32, win: &Win) {
unsafe {
assert!(!win.was_deleted());
cfl_set_spot(
font as i32,
size as i32,
x,
y,
w,
h,
win.as_widget_ptr() as *mut raw::c_void,
)
}
}
pub fn reset_spot() {
unsafe { cfl_reset_spot() }
}
pub fn capture_window<Win: WindowExt>(win: &mut Win) -> Result<RgbImage, FltkError> {
assert!(!win.was_deleted());
let cp = win.width() as u32 * win.height() as u32 * 3;
win.show();
unsafe {
let x = cfl_read_image(std::ptr::null_mut(), 0, 0, win.width(), win.height(), 0);
if x.is_null() {
Err(FltkError::Internal(FltkErrorKind::FailedOperation))
} else {
let x = std::slice::from_raw_parts(x, cp as usize).to_vec();
Ok(RgbImage::new(&x, win.width() as u32, win.height() as u32, 3)?)
}
}
}
pub fn write_to_png_file<I: ImageExt>(image: &I, path: &std::path::Path) -> Result<(), FltkError> {
assert!(std::any::type_name::<I>() != std::any::type_name::<crate::image::SvgImage>(), "SVG images are not supported!");
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_png(*image.to_raw_data() as *mut u8, path.as_ptr(), image.data_w() as i32, image.data_h() as i32) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}
pub fn write_to_jpg_file<I: ImageExt>(image: &I, path: &std::path::Path) -> Result<(), FltkError> {
assert!(std::any::type_name::<I>() != std::any::type_name::<crate::image::SvgImage>(), "SVG images are not supported!");
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_jpg(*image.to_raw_data() as *mut u8, path.as_ptr(), image.data_w() as i32, image.data_h() as i32) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}
pub fn write_to_bmp_file<I: ImageExt>(image: &I, path: &std::path::Path) -> Result<(), FltkError> {
assert!(std::any::type_name::<I>() != std::any::type_name::<crate::image::SvgImage>(), "SVG images are not supported!");
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_bmp(*image.to_raw_data() as *mut u8, path.as_ptr(), image.data_w() as i32, image.data_h() as i32) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}