use crate::utils::{ScreenPoint, ScreenRect};
use crate::window::{MoveableWindow, Window};
pub struct SubWindow {
this: ncurses::WINDOW,
}
pub enum SubWindowError {
CoordinateError(ScreenRect),
OtherError,
}
impl SubWindow {
pub fn init(rect: ScreenRect) -> Result<Self, SubWindowError> {
let win = ncurses::newwin(
rect.offset.y as i32,
rect.offset.x as i32,
rect.start.y as i32,
rect.start.x as i32,
);
if win == 0 as *mut i8 {
Err(SubWindowError::CoordinateError(rect))
} else {
Ok(SubWindow { this: win })
}
}
}
impl Window for SubWindow {
fn move_print(&mut self, point: ScreenPoint, text: &str) {
ncurses::mvwaddstr(self.this, point.y as i32, point.x as i32, text);
}
fn print(&mut self, text: &str) {
ncurses::waddstr(self.this, text);
}
fn refresh(&mut self) {
ncurses::wrefresh(self.this);
}
fn get_char(&mut self) -> char {
ncurses::wgetch(self.this) as u8 as char
}
}
impl MoveableWindow for SubWindow {
fn move_window(&mut self, point: ScreenPoint) {
ncurses::mvwin(self.this, point.y as i32, point.x as i32);
}
}
impl Drop for SubWindow {
fn drop(&mut self) {
ncurses::delwin(self.this);
}
}