use std::{ptr, fmt, hash::{Hash, Hasher}};
use ncursesw::{SCREEN, WINDOW};
use crate::{Screen, gen::*};
pub struct Pad {
screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool }
impl HasHandle<WINDOW> for Pad {
fn _from(screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool) -> Self {
assert!(screen.map_or_else(|| true, |screen| !screen.is_null()), "Pad::_from() : screen.is_null()");
assert!(!handle.is_null(), "Pad::_from() : handle.is_null()");
Self { screen, handle, free_on_drop }
}
fn _screen(&self) -> Option<SCREEN> {
self.screen
}
fn _handle(&self) -> WINDOW {
self.handle
}
}
impl Pad {
pub fn screen(&self) -> Option<Screen> {
self.screen.map(|screen| Screen::_from(screen, false))
}
}
impl IsPad for Pad { }
impl BaseCanvas for Pad { }
impl Mouseable for Pad { }
impl Scrollable for Pad { }
impl HasYAxis for Pad { }
impl HasYXAxis for Pad { }
impl HasXAxis for Pad { }
impl GraphicsTransform for Pad { }
impl HasGraphicFunctions for Pad { }
impl HasBackground for Pad { }
impl HasAttributes for Pad { }
impl HasMvAttributes for Pad { }
impl HasAddFunctions for Pad { }
impl HasMvAddFunctions for Pad { }
impl HasDelFunctions for Pad { }
impl HasMvDelFunctions for Pad { }
impl HasInFunctions for Pad { }
impl HasMvInFunctions for Pad { }
impl HasInsFunctions for Pad { }
impl HasMvInsFunctions for Pad { }
impl HasNonBlocking for Pad { }
impl HasGetFunctions for Pad { }
impl HasMvGetFunctions for Pad { }
impl Drop for Pad {
fn drop(&mut self) {
if self.free_on_drop {
if let Err(source) = ncursesw::delwin(self.handle) {
panic!("{} @ {:?}", source, self)
}
}
}
}
unsafe impl Send for Pad { } unsafe impl Sync for Pad { }
impl PartialEq for Pad {
fn eq(&self, rhs: &Self) -> bool {
ptr::eq(self.handle, rhs.handle)
}
}
impl Eq for Pad { }
impl Hash for Pad {
fn hash<H: Hasher>(&self, state: &mut H) {
self.handle.hash(state);
}
}
impl AsRef<Pad> for Pad {
fn as_ref(&self) -> &Self {
self
}
}
impl Clone for Pad {
fn clone(&self) -> Self {
Self::_from(self.screen.clone(), self.handle.clone(), false)
}
}
impl fmt::Debug for Pad {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Pad {{ screen: {:?}, handle: {:p}, free_on_drop: {} }}", self.screen, self.handle, self.free_on_drop)
}
}