From cb7748a7b064e1782f9d41ea6dc6b9fb87bdb0d8 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 10 Feb 2019 11:37:21 +0100 Subject: Add all widgets --- src/widgets/prompt.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/widgets/prompt.rs (limited to 'src/widgets/prompt.rs') diff --git a/src/widgets/prompt.rs b/src/widgets/prompt.rs new file mode 100644 index 0000000..0690840 --- /dev/null +++ b/src/widgets/prompt.rs @@ -0,0 +1,68 @@ +use std::ptr; + +use crate::bindings; +use crate::util; + +pub struct Prompt { + ptr: *mut bindings::Promptwidget, +} + +pub struct PromptBuilder<'a> { + lefttop: (u32, u32), + width: u32, + title: Option<&'a str>, +} + +impl<'a> PromptBuilder<'a> { + pub fn add_title<'s: 'a>(&'a mut self, title: &'s str) + -> &'a mut PromptBuilder<'a> { + self.title = Some(title); + self + } + + pub fn create(&self) -> Prompt { + Prompt { + ptr: unsafe { + bindings::prw_make( + self.lefttop.0 as i32, self.lefttop.1 as i32, + self.width as i32, + match self.title { + None => ptr::null(), + Some(s) => s.as_ptr() as *const i8, + }) + } + } + } +} + +impl Prompt { + pub fn new<'a>(lefttop: (u32, u32), width: u32) -> PromptBuilder<'a> { + PromptBuilder { + lefttop, width, + title: None, + } + } + + pub fn redraw(&self) { + unsafe { bindings::prw_redraw(self.ptr); } + } + + pub fn handle_key(&mut self, key: i32) -> Option { + let ptr = unsafe { bindings::prw_handlekey(self.ptr, key) }; + if ptr.is_null() { + None + } else { + unsafe { Some(util::string_from_utf8_charp(ptr)) } + } + } + + pub fn change_title(&mut self, title: &str) { + unsafe { bindings::prw_changetitle(self.ptr, title.as_ptr() as *const i8); } + } +} + +impl Drop for Prompt { + fn drop(&mut self) { + unsafe { bindings::prw_destroy(self.ptr); } + } +} -- cgit v1.2.3-70-g09d2