use std::{fmt, convert::{TryFrom, TryInto}};
use ncursesw::{SCREEN, WINDOW};
use crate::{Screen, Origin, NCurseswWinError, gen::*};
pub struct RipoffWindow {
screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool
}
impl HasHandle<WINDOW> for RipoffWindow {
fn _from(screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool) -> Self {
assert!(screen.map_or_else(|| true, |screen| !screen.is_null()), "RipoffWindow::_from() : screen.is_null()");
assert!(!handle.is_null(), "RipoffWindow::_from() : handle.is_null()");
Self { screen, handle, free_on_drop }
}
fn _screen(&self) -> Option<SCREEN> {
self.screen
}
fn _handle(&self) -> WINDOW {
self.handle
}
}
impl IsWindow for RipoffWindow { }
impl BaseCanvas for RipoffWindow { }
impl Mouseable for RipoffWindow { }
impl HasXAxis for RipoffWindow { }
impl HasBackground for RipoffWindow { }
impl HasAttributes for RipoffWindow { }
impl HasAddFunctions for RipoffWindow { }
impl HasDelFunctions for RipoffWindow { }
impl HasInFunctions for RipoffWindow { }
impl HasInsFunctions for RipoffWindow { }
impl HasNonBlocking for RipoffWindow { }
impl HasGetFunctions for RipoffWindow { }
impl RipoffWindow {
pub fn screen(&self) -> Option<Screen> {
self.screen.map(|screen| Screen::_from(screen, false))
}
pub fn column(&self) -> result!(u16) {
Ok(u16::try_from(ncursesw::getcurx(self._handle())?)?)
}
pub fn set_column(&self, column: u16) -> result!(()) {
Ok(ncursesw::wmove(self._handle(), Origin { y: 0, x: column }.try_into()?)?)
}
}
impl Drop for RipoffWindow {
fn drop(&mut self) {
if self.free_on_drop {
if let Err(source) = ncursesw::delwin(self.handle) {
panic!("{} @ {:?}", source, self)
}
}
}
}
unsafe impl Send for RipoffWindow { } unsafe impl Sync for RipoffWindow { }
impl AsRef<RipoffWindow> for RipoffWindow {
fn as_ref(&self) -> &Self {
self
}
}
impl Clone for RipoffWindow {
fn clone(&self) -> Self {
Self { screen: self.screen.clone(), handle: self.handle.clone(), free_on_drop: false }
}
}
impl fmt::Debug for RipoffWindow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RipoffWindow {{ screen: {:?}, handle: {:p} }}", self.screen, self.handle)
}
}