#![cfg_attr(feature = "asm", feature(llvm_asm))]
#[macro_use]
extern crate newt_proc_macros;
extern crate newt_sys;
use std::ffi::{CStr,CString};
use std::os::raw::{c_char,c_int};
use std::ptr;
#[macro_use]
mod intern;
pub mod callbacks;
pub mod component;
pub mod constants;
pub mod prelude;
pub mod widgets;
pub mod windows;
#[doc(no_inline)]
pub use self::component::Component;
#[cfg(feature = "asm")]
pub mod grid;
#[doc(no_inline)]
pub use self::callbacks::Callback;
#[doc(no_inline)]
pub use self::windows::win_message;
#[doc(no_inline)]
pub use self::windows::win_choice;
#[doc(no_inline)]
pub use self::windows::win_ternary;
#[doc(no_inline)]
#[cfg(feature = "asm")]
pub use self::windows::win_menu;
#[doc(no_inline)]
#[cfg(feature = "asm")]
pub use self::windows::win_entries;
use newt_sys::*;
#[derive(Clone,Debug)]
pub struct Colors<'a> {
pub root_fg: &'a str, pub root_bg: &'a str,
pub border_fg: &'a str, pub border_bg: &'a str,
pub window_fg: &'a str, pub window_bg: &'a str,
pub shadow_fg: &'a str, pub shadow_bg: &'a str,
pub title_fg: &'a str, pub title_bg: &'a str,
pub button_fg: &'a str, pub button_bg: &'a str,
pub act_button_fg: &'a str, pub act_button_bg: &'a str,
pub checkbox_fg: &'a str, pub checkbox_bg: &'a str,
pub act_checkbox_fg: &'a str, pub act_checkbox_bg: &'a str,
pub entry_fg: &'a str, pub entry_bg: &'a str,
pub label_fg: &'a str, pub label_bg: &'a str,
pub listbox_fg: &'a str, pub listbox_bg: &'a str,
pub act_listbox_fg: &'a str, pub act_listbox_bg: &'a str,
pub textbox_fg: &'a str, pub textbox_bg: &'a str,
pub act_textbox_fg: &'a str, pub act_textbox_bg: &'a str,
pub help_line_fg: &'a str, pub help_line_bg: &'a str,
pub root_text_fg: &'a str, pub root_text_bg: &'a str,
pub empty_scale: &'a str, pub full_scale: &'a str,
pub disabled_entry_fg: &'a str, pub disabled_entry_bg: &'a str,
pub compact_button_fg: &'a str, pub compact_button_bg: &'a str,
pub act_sel_listbox_fg: &'a str, pub act_sel_listbox_bg: &'a str,
pub sel_listbox_fg: &'a str, pub sel_listbox_bg: &'a str
}
macro_rules! cstr {
( $c_str:expr ) => { CStr::from_ptr($c_str).to_str().unwrap() }
}
impl<'a> Default for Colors<'a> {
fn default() -> Self {
unsafe {
let c = &newtDefaultColorPalette;
Colors {
root_fg: cstr!(c.rootFg), root_bg: cstr!(c.rootBg),
border_fg: cstr!(c.borderFg), border_bg: cstr!(c.borderBg),
window_fg: cstr!(c.windowFg), window_bg: cstr!(c.windowBg),
shadow_fg: cstr!(c.shadowFg), shadow_bg: cstr!(c.shadowBg),
title_fg: cstr!(c.titleFg), title_bg: cstr!(c.titleBg),
button_fg: cstr!(c.buttonFg), button_bg: cstr!(c.buttonBg),
act_button_fg: cstr!(c.actButtonFg), act_button_bg: cstr!(c.actButtonBg),
checkbox_fg: cstr!(c.checkboxFg), checkbox_bg: cstr!(c.checkboxBg),
act_checkbox_fg: cstr!(c.actCheckboxFg), act_checkbox_bg: cstr!(c.actCheckboxBg),
entry_fg: cstr!(c.entryFg), entry_bg: cstr!(c.entryBg),
label_fg: cstr!(c.labelFg), label_bg: cstr!(c.labelBg),
listbox_fg: cstr!(c.listboxFg), listbox_bg: cstr!(c.listboxBg),
act_listbox_fg: cstr!(c.actListboxFg), act_listbox_bg: cstr!(c.actListboxBg),
textbox_fg: cstr!(c.textboxFg), textbox_bg: cstr!(c.textboxBg),
act_textbox_fg: cstr!(c.actTextboxFg), act_textbox_bg: cstr!(c.actTextboxBg),
help_line_fg: cstr!(c.helpLineFg), help_line_bg: cstr!(c.helpLineBg),
root_text_fg: cstr!(c.rootTextFg), root_text_bg: cstr!(c.rootTextBg),
empty_scale: cstr!(c.emptyScale), full_scale: cstr!(c.fullScale),
disabled_entry_fg: cstr!(c.disabledEntryFg), disabled_entry_bg: cstr!(c.disabledEntryBg),
compact_button_fg: cstr!(c.compactButtonFg), compact_button_bg: cstr!(c.compactButtonBg),
act_sel_listbox_fg: cstr!(c.actSelListboxFg), act_sel_listbox_bg: cstr!(c.actSelListboxBg),
sel_listbox_fg: cstr!(c.selListboxFg), sel_listbox_bg: cstr!(c.selListboxBg)
}
}
}
}
pub fn init() -> Result<(), ()> {
let rv = unsafe { newtInit() };
if rv == 0 { Ok(()) } else { Err(()) }
}
pub fn finished() {
unsafe { newtFinished(); }
}
pub fn cls() {
unsafe { newtCls(); }
}
pub fn resize_screen(redraw: i32) {
unsafe { newtResizeScreen(redraw); }
}
pub fn wait_for_key() {
unsafe { newtWaitForKey(); }
}
pub fn clear_key_buffer() {
unsafe { newtClearKeyBuffer(); }
}
pub fn delay(usecs: u32) {
unsafe { newtDelay(usecs); }
}
pub fn open_window(left: i32, top: i32, width: u32, height: u32,
title: Option<&str>) -> Result<(), ()> {
let c_str: CString;
let c_ptr = match title {
Some(title) => {
c_str = CString::new(title).unwrap();
c_str.as_ptr()
},
None => ptr::null()
};
let rv = unsafe { newtOpenWindow(left, top, width, height, c_ptr) };
if rv == 0 { Ok(()) } else { Err(()) }
}
pub fn centered_window(width: u32, height: u32, title: Option<&str>)
-> Result<(), ()> {
let c_str: CString;
let c_ptr = match title {
Some(title) => {
c_str = CString::new(title).unwrap();
c_str.as_ptr()
},
None => ptr::null()
};
let rv = unsafe { newtCenteredWindow(width, height, c_ptr) };
if rv == 0 { Ok(()) } else { Err(()) }
}
pub fn pop_window() {
unsafe { newtPopWindow(); }
}
pub fn pop_window_no_refresh() {
unsafe { newtPopWindowNoRefresh(); }
}
pub fn set_colors(colors: &Colors) {
let root_fg = CString::new(colors.root_fg).unwrap();
let root_bg = CString::new(colors.root_bg).unwrap();
let border_fg = CString::new(colors.border_fg).unwrap();
let border_bg = CString::new(colors.border_bg).unwrap();
let window_fg = CString::new(colors.window_fg).unwrap();
let window_bg = CString::new(colors.window_bg).unwrap();
let shadow_fg = CString::new(colors.shadow_fg).unwrap();
let shadow_bg = CString::new(colors.shadow_bg).unwrap();
let title_fg = CString::new(colors.title_fg).unwrap();
let title_bg = CString::new(colors.title_bg).unwrap();
let button_fg = CString::new(colors.button_fg).unwrap();
let button_bg = CString::new(colors.button_bg).unwrap();
let act_button_fg = CString::new(colors.act_button_fg).unwrap();
let act_button_bg = CString::new(colors.act_button_bg).unwrap();
let checkbox_fg = CString::new(colors.checkbox_fg).unwrap();
let checkbox_bg = CString::new(colors.checkbox_bg).unwrap();
let act_checkbox_fg = CString::new(colors.act_checkbox_fg).unwrap();
let act_checkbox_bg = CString::new(colors.act_checkbox_bg).unwrap();
let entry_fg = CString::new(colors.entry_fg).unwrap();
let entry_bg = CString::new(colors.entry_bg).unwrap();
let label_fg = CString::new(colors.label_fg).unwrap();
let label_bg = CString::new(colors.label_bg).unwrap();
let listbox_fg = CString::new(colors.listbox_fg).unwrap();
let listbox_bg = CString::new(colors.listbox_bg).unwrap();
let act_listbox_fg = CString::new(colors.act_listbox_fg).unwrap();
let act_listbox_bg = CString::new(colors.act_listbox_bg).unwrap();
let textbox_fg = CString::new(colors.textbox_fg).unwrap();
let textbox_bg = CString::new(colors.textbox_bg).unwrap();
let act_textbox_fg = CString::new(colors.act_textbox_fg).unwrap();
let act_textbox_bg = CString::new(colors.act_textbox_bg).unwrap();
let help_line_fg = CString::new(colors.help_line_fg).unwrap();
let help_line_bg = CString::new(colors.help_line_bg).unwrap();
let root_text_fg = CString::new(colors.root_text_fg).unwrap();
let root_text_bg = CString::new(colors.root_text_bg).unwrap();
let empty_scale = CString::new(colors.empty_scale).unwrap();
let full_scale = CString::new(colors.full_scale).unwrap();
let disabled_entry_fg = CString::new(colors.disabled_entry_fg).unwrap();
let disabled_entry_bg = CString::new(colors.disabled_entry_bg).unwrap();
let compact_button_fg = CString::new(colors.compact_button_fg).unwrap();
let compact_button_bg = CString::new(colors.compact_button_bg).unwrap();
let act_sel_listbox_fg = CString::new(colors.act_sel_listbox_fg).unwrap();
let act_sel_listbox_bg = CString::new(colors.act_sel_listbox_bg).unwrap();
let sel_listbox_fg = CString::new(colors.sel_listbox_fg).unwrap();
let sel_listbox_bg = CString::new(colors.sel_listbox_bg).unwrap();
let c_colors = newtColors {
rootFg: root_fg.as_ptr() as *mut c_char,
rootBg: root_bg.as_ptr() as *mut c_char,
borderFg: border_fg.as_ptr() as *mut c_char,
borderBg: border_bg.as_ptr() as *mut c_char,
windowFg: window_fg.as_ptr() as *mut c_char,
windowBg: window_bg.as_ptr() as *mut c_char,
shadowFg: shadow_fg.as_ptr() as *mut c_char,
shadowBg: shadow_bg.as_ptr() as *mut c_char,
titleFg: title_fg.as_ptr() as *mut c_char,
titleBg: title_bg.as_ptr() as *mut c_char,
buttonFg: button_fg.as_ptr() as *mut c_char,
buttonBg: button_bg.as_ptr() as *mut c_char,
actButtonFg: act_button_fg.as_ptr() as *mut c_char,
actButtonBg: act_button_bg.as_ptr() as *mut c_char,
checkboxFg: checkbox_fg.as_ptr() as *mut c_char,
checkboxBg: checkbox_bg.as_ptr() as *mut c_char,
actCheckboxFg: act_checkbox_fg.as_ptr() as *mut c_char,
actCheckboxBg: act_checkbox_bg.as_ptr() as *mut c_char,
entryFg: entry_fg.as_ptr() as *mut c_char,
entryBg: entry_bg.as_ptr() as *mut c_char,
labelFg: label_fg.as_ptr() as *mut c_char,
labelBg: label_bg.as_ptr() as *mut c_char,
listboxFg: listbox_fg.as_ptr() as *mut c_char,
listboxBg: listbox_bg.as_ptr() as *mut c_char,
actListboxFg: act_listbox_fg.as_ptr() as *mut c_char,
actListboxBg: act_listbox_bg.as_ptr() as *mut c_char,
textboxFg: textbox_fg.as_ptr() as *mut c_char,
textboxBg: textbox_bg.as_ptr() as *mut c_char,
actTextboxFg: act_textbox_fg.as_ptr() as *mut c_char,
actTextboxBg: act_textbox_bg.as_ptr() as *mut c_char,
helpLineFg: help_line_fg.as_ptr() as *mut c_char,
helpLineBg: help_line_bg.as_ptr() as *mut c_char,
rootTextFg: root_text_fg.as_ptr() as *mut c_char,
rootTextBg: root_text_bg.as_ptr() as *mut c_char,
emptyScale: empty_scale.as_ptr() as *mut c_char,
fullScale: full_scale.as_ptr() as *mut c_char,
disabledEntryFg: disabled_entry_fg.as_ptr() as *mut c_char,
disabledEntryBg: disabled_entry_bg.as_ptr() as *mut c_char,
compactButtonFg: compact_button_fg.as_ptr() as *mut c_char,
compactButtonBg: compact_button_bg.as_ptr() as *mut c_char,
actSelListboxFg: act_sel_listbox_fg.as_ptr() as *mut c_char,
actSelListboxBg: act_sel_listbox_bg.as_ptr() as *mut c_char,
selListboxFg: sel_listbox_fg.as_ptr() as *mut c_char,
selListboxBg: sel_listbox_bg.as_ptr() as *mut c_char
};
unsafe { newtSetColors(c_colors); }
}
pub fn set_color(colorset: i32, fg: &str, bg: &str) {
let c_fg = CString::new(fg).unwrap();
let c_bg = CString::new(bg).unwrap();
unsafe {
newtSetColor(colorset,
c_fg.as_ptr() as *mut c_char,
c_bg.as_ptr() as *mut c_char);
}
}
pub fn refresh() {
unsafe { newtRefresh(); }
}
pub fn suspend() {
unsafe { newtSuspend(); }
}
pub fn resume() {
unsafe { newtResume(); }
}
pub fn push_help_line(text: &str) {
let c_str = CString::new(text).unwrap();
unsafe { newtPushHelpLine(c_str.as_ptr()); }
}
pub fn redraw_help_line() {
unsafe { newtRedrawHelpLine(); }
}
pub fn pop_help_line() {
unsafe { newtPopHelpLine(); }
}
pub fn draw_root_text(col: i32, row: i32, text: &str) {
let c_str = CString::new(text).unwrap();
unsafe { newtDrawRootText(col, row, c_str.as_ptr()); }
}
pub fn bell() {
unsafe { newtBell(); }
}
pub fn cursor_off() {
unsafe { newtCursorOff(); }
}
pub fn cursor_on() {
unsafe { newtCursorOn(); }
}
pub fn get_screen_size() -> (i32, i32) {
let mut cols: c_int = 0;
let mut rows: c_int = 0;
unsafe { newtGetScreenSize(&mut cols, &mut rows); }
(cols, rows)
}
pub fn reflow_text(text: &str, width: i32, flex_down: i32, flex_up: i32)
-> (String, i32, i32) {
let c_str = CString::new(text).unwrap();
let mut actual_width: c_int = 0;
let mut actual_height: c_int = 0;
unsafe {
let rstr = newtReflowText(c_str.as_ptr() as *mut c_char,
width, flex_down, flex_up, &mut actual_width,
&mut actual_height);
let c_str = CStr::from_ptr(rstr).to_string_lossy().into_owned();
(c_str, actual_width, actual_height)
}
}