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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
extern crate std;
extern crate newt_sys;

use std::ops::Drop;
use std::os::unix::io::RawFd;
use std::ptr;

use newt_sys::*;
use crate::Component;
use crate::callbacks::HelpCallback;
use crate::widgets::VerticalScrollbar;

mod exit_reason;
pub use self::exit_reason::ExitReason;

#[allow(non_camel_case_types)]
type newtExitReason = newtExitStruct__bindgen_ty_1;
const NEWT_EXIT_HOTKEY: newtExitReason    = newtExitStruct_NEWT_EXIT_HOTKEY;
const NEWT_EXIT_COMPONENT: newtExitReason = newtExitStruct_NEWT_EXIT_COMPONENT;
const NEWT_EXIT_FDREADY: newtExitReason   = newtExitStruct_NEWT_EXIT_FDREADY;
const NEWT_EXIT_TIMER: newtExitReason     = newtExitStruct_NEWT_EXIT_TIMER;
const NEWT_EXIT_ERROR: newtExitReason     = newtExitStruct_NEWT_EXIT_ERROR;

#[allow(non_camel_case_types)]
type newtExitStructUnion = newtExitStruct__bindgen_ty_2;

///
/// File descriptor flags for the [`Form.watch_fd()`][watch_fd] function.
///
/// [watch_fd]: ../form/struct.Form.html#method.watch_fd
///
#[repr(C)]
pub enum FDFlags {
    /// Exit when the file descriptor is ready for reading.
    Read   = NEWT_FD_READ as isize,
    /// Exit when the file descriptor is ready for writing.
    Write  = NEWT_FD_WRITE as isize,
    /// Exit when an exception has occurred on the file descriptor.
    Except = NEWT_FD_EXCEPT as isize
}

#[derive(Component)]
struct BaseComponent {
    co: newtComponent,
    added_to_parent: bool
}

///
/// Displays `Component`s and accepts user input.
///
#[derive(Component)]
pub struct Form
{
    co: newtComponent,
    added_to_parent: bool
}

impl Drop for Form
{
    fn drop(&mut self) {
        if !self.added_to_parent {
            unsafe { newtFormDestroy(self.co); }
        }
    }
}

impl Form
{
    ///
    /// Creates a new `Form`.
    ///
    pub fn new(scrollbar: Option<&VerticalScrollbar>, flags: i32) -> Form {
        let scrollbar = if let Some(scrollbar) = scrollbar {
            scrollbar.co()
        } else {
            ptr::null_mut()
        };

        Form {
            co: unsafe { newtForm(scrollbar, ptr::null_mut(), flags) },
            added_to_parent: false
        }
    }

    ///
    /// Creates a new `Form` with an associated help `HelpCallback`. See
    /// [HelpCallback][help_cb] for additional information.
    ///
    /// [help_cb]: ../../callbacks/struct.HelpCallback.html#method.new
    ///
    pub fn new_with_help_callback<FN, T>
      (scrollbar: Option<&VerticalScrollbar>, flags: i32, function: FN, data: Option<T>)
        -> (Form, Box<HelpCallback<FN, T>>)
        where FN: Fn(&Form, Option<&T>)
    {
        HelpCallback::new(scrollbar, flags, function, data)
    }

    pub(crate) fn new_co(co: newtComponent) -> Form {
        Form { co, added_to_parent: false }
    }

    ///
    /// Add a `Component` to the `Form` to be displayed when the `Form` is run.
    ///
    pub fn add_component(&mut self, component: &mut dyn Component)
      -> Result<(), &'static str> {
        component.add_to_parent(false)?;
        unsafe { newtFormAddComponent(self.co, component.co()); }
        Ok(())
    }

    ///
    /// Add multiple `Component`s to the `Form`.
    ///
    pub fn add_components(&mut self, components: &mut [&mut dyn Component])
            -> Result<(), &'static str> {
        for component in components.iter_mut() {
            self.add_component(*component)?;
        }
        Ok(())
    }

    ///
    /// Set the height of the `Form`.
    ///
    pub fn set_height(&mut self, height: i32) {
        unsafe { newtFormSetHeight(self.co, height); }
    }

    ///
    /// Set the width of the `Form`.
    ///
    pub fn set_width(&mut self, width: i32) {
        unsafe { newtFormSetWidth(self.co, width); }
    }

    ///
    /// Tell the `Form` to resize itself.
    ///
    pub fn set_size(&mut self) {
        unsafe { newtFormSetSize(self.co); }
    }

    ///
    /// Add an exit hot key to the `Form`. The `Form` will stop running
    /// when the key is pressed.
    ///
    pub fn add_hot_key(&mut self, key: i32) {
        unsafe { newtFormAddHotKey(self.co, key); }
    }

    ///
    /// Add an exit timer to the `Form`. The `Form` will stop running
    /// when the timer times out.
    ///
    /// * `millisecs` - The timer countdown in milliseconds.
    ///
    pub fn set_timer(&mut self, millisecs: i32) {
        unsafe { newtFormSetTimer(self.co, millisecs); }
    }

    ///
    /// Add a file descriptor for the `Form` to watch. The `Form` will stop
    /// running when the specified activity occurs on the file descriptor.
    ///
    /// * `fd` - The file descriptor to watch.
    /// * `flags` - Flags specifying the activity to watch for.
    ///
    pub fn watch_fd(&mut self, fd: RawFd, flags: FDFlags) {
        unsafe { newtFormWatchFd(self.co, fd, flags as i32); }
    }

    ///
    /// Get the `Form`'s currently focused `Component`.
    ///
    pub fn get_current(&self) -> Box<dyn Component> {
        Box::new(BaseComponent {
            co: unsafe { newtFormGetCurrent(self.co) },
            added_to_parent: true
        })
    }

    ///
    /// Set the `Form`'s currently focused `Component`.
    ///
    pub fn set_current(&mut self, subcomponent: &dyn Component) {
        unsafe { newtFormSetCurrent(self.co, subcomponent.co()); }
    }

    ///
    /// Set the `Form`'s background color.
    ///
    pub fn set_background(&mut self, color: i32) {
        unsafe { newtFormSetBackground(self.co, color); }
    }

    pub fn get_scroll_position(&self) -> i32 {
        unsafe { newtFormGetScrollPosition(self.co) }
    }

    pub fn set_scroll_position(&mut self, position: i32) {
        unsafe { newtFormSetScrollPosition(self.co, position); }
    }

    ///
    /// Run the form displaying all added components and accepting
    /// input from the user.
    ///
    pub fn run(&self) -> Result<ExitReason, ()> {
        use self::ExitReason::{HotKey,Component,FDReady,Timer};

        let mut es = newtExitStruct {
            reason: NEWT_EXIT_HOTKEY,
            u: newtExitStructUnion { watch: 0 }
        };

        unsafe {
            newtFormRun(self.co, &mut es);
            match es.reason {
                NEWT_EXIT_HOTKEY => Ok(HotKey(es.u.key)),
                NEWT_EXIT_COMPONENT => Ok(
                    Component(Box::new(BaseComponent {
                                       co: es.u.co,
                                       added_to_parent: true
                    }))
                ),
                NEWT_EXIT_FDREADY => Ok(FDReady(es.u.watch)),
                NEWT_EXIT_TIMER => Ok(Timer),
                NEWT_EXIT_ERROR => Err(()),
                _ => panic!("Unexpected newt exit reason.")
            }
        }
    }

    ///
    /// Redraw the `Form`.
    ///
    pub fn draw(&self) {
        unsafe { newtDrawForm(self.co); }
    }
}