Skip to main content

oxidized_curses/window/
sub_window.rs

1use crate::utils::{ScreenPoint, ScreenRect};
2use crate::window::{MoveableWindow, Window};
3
4/// A window inside the main window
5pub struct SubWindow {
6    // the identifier in ncurses for the window
7    this: ncurses::WINDOW,
8}
9
10/// Subwindow Creation Error
11pub enum SubWindowError {
12    CoordinateError(ScreenRect),
13    OtherError,
14}
15
16impl SubWindow {
17    /// Create a new subwindow from the rectangle given
18    pub fn init(rect: ScreenRect) -> Result<Self, SubWindowError> {
19        let win = ncurses::newwin(
20            rect.offset.y as i32,
21            rect.offset.x as i32,
22            rect.start.y as i32,
23            rect.start.x as i32,
24        );
25
26        if win == 0 as *mut i8 {
27            Err(SubWindowError::CoordinateError(rect))
28        } else {
29            Ok(SubWindow { this: win })
30        }
31    }
32}
33
34impl Window for SubWindow {
35    /// Move the cursor and print a string at specific coordinates on the subwindow
36    fn move_print(&mut self, point: ScreenPoint, text: &str) {
37        ncurses::mvwaddstr(self.this, point.y as i32, point.x as i32, text);
38    }
39
40    /// Print a string at the curent subwindow cursor position
41    fn print(&mut self, text: &str) {
42        ncurses::waddstr(self.this, text);
43    }
44
45    fn refresh(&mut self) {
46        ncurses::wrefresh(self.this);
47    }
48
49    fn get_char(&mut self) -> char {
50        ncurses::wgetch(self.this) as u8 as char
51    }
52}
53
54impl MoveableWindow for SubWindow {
55    fn move_window(&mut self, point: ScreenPoint) {
56        ncurses::mvwin(self.this, point.y as i32, point.x as i32);
57    }
58}
59
60impl Drop for SubWindow {
61    /// Free the internal subwindow pointer
62    fn drop(&mut self) {
63        ncurses::delwin(self.this);
64    }
65}