1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    src/panels.rs

    Copyright (c) 2019 Stephen Whittle  All rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom
    the Software is furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
*/

use ncurseswerror::NCurseswError;
use origin::Origin;
use shims::ncurses;
use shims::npanels;
use shims::constants::{OK, ERR};

type WINDOW = ncurses::WINDOW;

pub type PANEL = npanels::PANEL;
pub type PANEL_USERPTR = *const libc::c_void;

/// allocates a PANEL structure, associates it with win, places the panel on the top of the stack (causes it to be displayed above any other panel) and returns a pointer to the new panel.
pub fn new_panel(window_handle: WINDOW) -> result!(PANEL) {
    match npanels::new_panel(window_handle) {
        None         => Err(panels_function_error!("new_panel")),
        Some(handle) => Ok(handle)
    }
} 

/// puts panel at the bottom of all panels.
pub fn bottom_panel(handle: PANEL) -> result!(()) {
    match npanels::bottom_panel(handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("bottom_panel", rc))
    }
}

/// puts the given visible panel on top of all panels in the stack.
pub fn top_panel(handle: PANEL) -> result!(()) {
    match npanels::top_panel(handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("top_panel", rc))
    }
}

/// makes a hidden panel visible by placing it on top of the panels in the panel stack.
pub fn show_panel(handle: PANEL) -> result!(()) {
    match npanels::show_panel(handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("show_panel", rc))
    }
}

/// refreshes the virtual screen to reflect the relations between the panels in the stack.
///
/// Does not call doupdate() to refresh the physical screen. Use this function and not wrefresh() or wnoutrefresh().
/// update_panels() may be called more than once before a call to doupdate(), but doupdate() is the function
/// responsible for updating the physical screen.
pub fn update_panels() {
    npanels::update_panels();
} 

/// removes the given panel from the panel stack and thus hides it from view. The PANEL structure is not lost, merely removed from the stack.
pub fn hide_panel(handle: PANEL) -> result!(()) {
    match npanels::hide_panel(handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("hide_panel", rc))
    }
}

/// returns a pointer to the window of the given panel.
pub fn panel_window(handle: PANEL) -> result!(WINDOW) {
    match npanels::panel_window(handle) {
        None                => Err(panels_function_error!("panel_window")),
        Some(window_handle) => Ok(window_handle)
    }
} 

/// replaces the current window of panel with window (useful, for example if you want to resize a panel; if you're using ncurses, you can call replace_panel on the output of wresize(3x)). It does not change the position of the panel in the stack.
pub fn replace_panel(handle: PANEL, window_handle: WINDOW) -> result!(()) {
    match npanels::replace_panel(handle, window_handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("replace_panel", rc))
    }
} 

/// moves the given panel window so that its upper-left corner is at origin.y, origin.x. It does not change the position of the panel in the stack. Be sure to use this function, not mvwin(), to move a panel window.
pub fn move_panel(handle: PANEL, origin: Origin) -> result!(()) {
    match npanels::move_panel(handle, origin.y, origin.x) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("move_panel", rc))
    }
} 

/// returns true if the panel is in the panel stack, false if it is not.
pub fn panel_hidden(handle: PANEL) -> result!(bool) {
    match npanels::panel_hidden(handle) {
        None    => Err(panels_function_error!("panel_hidden")),
        Some(v) => Ok(v)
    }
}

/// returns a pointer to the panel above pan. If the panel argument is None, it returns a pointer to the bottom panel in the stack.
pub fn panel_above(handle: Option<PANEL>) -> result!(PANEL) {
    match npanels::panel_above(handle) {
        None    => Err(panels_function_error!("panel_above")),
        Some(p) => Ok(p)
    }
} 

/// returns a pointer to the panel just below pan. If the panel argument is None, it returns a pointer to the top panel in the stack.
pub fn panel_below(handle: Option<PANEL>) -> result!(PANEL) {
    match npanels::panel_below(handle) {
        None    => Err(panels_function_error!("panel_below")),
        Some(p) => Ok(p)
    }
} 

/// sets the panel's user pointer.
pub fn set_panel_userptr(handle: PANEL, ptr: Option<PANEL_USERPTR>) -> result!(()) {
    match npanels::set_panel_userptr(handle, ptr) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("set_panel_userptr", rc))
    }
}

/// returns the user pointer for a given panel.
pub fn panel_userptr(handle: PANEL) -> Option<PANEL_USERPTR> {
    npanels::panel_userptr(handle)
}

/// removes the given panel from the stack and deallocates the PANEL structure (but not its associated window).
pub fn del_panel(handle: PANEL) -> result!(()) {
    match npanels::del_panel(handle) {
        OK => Ok(()),
        rc => Err(panels_function_error_with_rc!("del_panel", rc))
    }
}