use winapi::shared::windef::HWND;
use winapi::shared::minwindef::{WPARAM, LPARAM};
use winapi::um::winuser::{LBS_MULTIPLESEL, LBS_NOSEL, WS_VISIBLE, WS_DISABLED, WS_TABSTOP};
use crate::win32::window_helper as wh;
use crate::win32::base_helper::{to_utf16, from_utf16, check_hwnd};
use crate::{Font, NwgError};
use super::{ControlBase, ControlHandle};
use std::cell::{Ref, RefMut, RefCell};
use std::fmt::Display;
use std::ops::Range;
use std::mem;
const NOT_BOUND: &'static str = "ListBox is not yet bound to a winapi object";
const BAD_HANDLE: &'static str = "INTERNAL ERROR: ListBox handle is not HWND!";
bitflags! {
pub struct ListBoxFlags: u32 {
const NONE = 0;
const VISIBLE = WS_VISIBLE;
const DISABLED = WS_DISABLED;
const MULTI_SELECT = LBS_MULTIPLESEL;
const NO_SELECT = LBS_NOSEL;
const TAB_STOP = WS_TABSTOP;
}
}
#[derive(Default)]
pub struct ListBox<D: Display+Default> {
pub handle: ControlHandle,
collection: RefCell<Vec<D>>
}
impl<D: Display+Default> ListBox<D> {
pub fn builder<'a>() -> ListBoxBuilder<'a, D> {
ListBoxBuilder {
size: (100, 300),
position: (0, 0),
enabled: true,
focus: false,
flags: None,
font: None,
collection: None,
selected_index: None,
multi_selection: Vec::new(),
parent: None
}
}
pub fn push(&self, item: D) {
use winapi::um::winuser::LB_ADDSTRING;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let display = format!("{}", item);
let display_os = to_utf16(&display);
unsafe {
wh::send_message(handle, LB_ADDSTRING, 0, mem::transmute(display_os.as_ptr()));
}
self.collection.borrow_mut().push(item);
}
pub fn insert(&self, index: usize, item: D) {
use winapi::um::winuser::LB_INSERTSTRING;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let display = format!("{}", item);
let display_os = to_utf16(&display);
let mut col = self.collection.borrow_mut();
if index == std::usize::MAX {
col.push(item);
} else {
col.insert(index, item);
}
unsafe {
wh::send_message(handle, LB_INSERTSTRING, index, mem::transmute(display_os.as_ptr()));
}
}
pub fn remove(&self, index: usize) -> D {
use winapi::um::winuser::LB_DELETESTRING;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_DELETESTRING, index as WPARAM, 0);
let mut col_ref = self.collection.borrow_mut();
col_ref.remove(index)
}
pub fn selection(&self) -> Option<usize> {
use winapi::um::winuser::{LB_GETCURSEL, LB_ERR};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let index = wh::send_message(handle, LB_GETCURSEL , 0, 0);
if index == LB_ERR { None }
else { Some(index as usize) }
}
pub fn multi_selection_len(&self) -> usize {
use winapi::um::winuser::{LB_GETSELCOUNT, LB_ERR};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
match wh::send_message(handle, LB_GETSELCOUNT, 0, 0) {
LB_ERR => 0,
value => value as usize
}
}
pub fn multi_selection(&self) -> Vec<usize> {
use winapi::um::winuser::{LB_GETSELCOUNT, LB_GETSELITEMS, LB_ERR};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let select_count = match wh::send_message(handle, LB_GETSELCOUNT, 0, 0) {
LB_ERR => usize::max_value(),
value => value as usize
};
if select_count == usize::max_value() || usize::max_value() == 0 {
return Vec::new();
}
let mut indices_buffer: Vec<u32> = Vec::with_capacity(select_count);
unsafe { indices_buffer.set_len(select_count) };
wh::send_message(
handle,
LB_GETSELITEMS,
select_count as WPARAM,
indices_buffer.as_mut_ptr() as LPARAM
);
indices_buffer.into_iter().map(|i| i as usize).collect()
}
pub fn selection_string(&self) -> Option<String> {
use winapi::um::winuser::{LB_GETCURSEL, LB_GETTEXTLEN, LB_GETTEXT, LB_ERR};
use winapi::shared::ntdef::WCHAR;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let index = wh::send_message(handle, LB_GETCURSEL, 0, 0);
if index == LB_ERR { None }
else {
let index = index as usize;
let length = (wh::send_message(handle, LB_GETTEXTLEN, index, 0) as usize) + 1;
let mut buffer: Vec<WCHAR> = Vec::with_capacity(length);
unsafe {
buffer.set_len(length);
wh::send_message(handle, LB_GETTEXT, index, mem::transmute(buffer.as_ptr()));
}
Some(from_utf16(&buffer))
}
}
pub fn set_selection(&self, index: Option<usize>) {
use winapi::um::winuser::LB_SETCURSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let index = index.unwrap_or(-1isize as usize);
wh::send_message(handle, LB_SETCURSEL, index, 0);
}
pub fn multi_add_selection(&self, index: usize) {
use winapi::um::winuser::LB_SETSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_SETSEL, 1, index as LPARAM);
}
pub fn multi_remove_selection(&self, index: usize) {
use winapi::um::winuser::LB_SETSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_SETSEL, 0, index as LPARAM);
}
pub fn unselect_all(&self) {
use winapi::um::winuser::LB_SETSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_SETSEL, 0, -1);
}
pub fn select_all(&self) {
use winapi::um::winuser::LB_SETSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_SETSEL, 1, -1);
}
pub fn multi_select_range(&self, range: Range<usize>) {
use winapi::um::winuser::LB_SELITEMRANGEEX;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let start = range.start as WPARAM;
let end = range.end as LPARAM;
wh::send_message(handle, LB_SELITEMRANGEEX, start, end);
}
pub fn multi_unselect_range(&self, range: Range<usize>) {
use winapi::um::winuser::LB_SELITEMRANGEEX;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let start = range.start as LPARAM;
let end = range.end as WPARAM;
wh::send_message(handle, LB_SELITEMRANGEEX, end, start);
}
pub fn set_selection_string(&self, value: &str) -> Option<usize> {
use winapi::um::winuser::{LB_SELECTSTRING, LB_ERR};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let os_string = to_utf16(value);
unsafe {
let index = wh::send_message(handle, LB_SELECTSTRING, 0, mem::transmute(os_string.as_ptr()));
if index == LB_ERR {
None
} else {
Some(index as usize)
}
}
}
pub fn selected(&self, index: usize) -> bool {
use winapi::um::winuser::LB_GETSEL;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_GETSEL, index as WPARAM, 0) > 0
}
pub fn sync(&self) {
use winapi::um::winuser::{LB_ADDSTRING, LB_INITSTORAGE};
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
self.clear_inner(handle);
let item_count = self.collection.borrow().len();
wh::send_message(handle, LB_INITSTORAGE, item_count as WPARAM, (10*item_count) as LPARAM);
for item in self.collection.borrow().iter() {
let display = format!("{}", item);
let display_os = to_utf16(&display);
unsafe {
wh::send_message(handle, LB_ADDSTRING, 0, mem::transmute(display_os.as_ptr()));
}
}
}
pub fn set_collection(&self, mut col: Vec<D>) -> Vec<D> {
use winapi::um::winuser::LB_ADDSTRING;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
self.clear_inner(handle);
for item in col.iter() {
let display = format!("{}", item);
let display_os = to_utf16(&display);
unsafe {
wh::send_message(handle, LB_ADDSTRING, 0, mem::transmute(display_os.as_ptr()));
}
}
let mut col_ref = self.collection.borrow_mut();
mem::swap::<Vec<D>>(&mut col_ref, &mut col);
col
}
pub fn clear(&self) {
self.set_collection(Vec::new());
}
pub fn len(&self) -> usize {
use winapi::um::winuser::LB_GETCOUNT;
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
wh::send_message(handle, LB_GETCOUNT, 0, 0) as usize
}
pub fn font(&self) -> Option<Font> {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
let font_handle = wh::get_window_font(handle);
if font_handle.is_null() {
None
} else {
Some(Font { handle: font_handle })
}
}
pub fn set_font(&self, font: Option<&Font>) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_window_font(handle, font.map(|f| f.handle), true); }
}
pub fn focus(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::get_focus(handle) }
}
pub fn set_focus(&self) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_focus(handle); }
}
pub fn enabled(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::get_window_enabled(handle) }
}
pub fn set_enabled(&self, v: bool) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_window_enabled(handle, v) }
}
pub fn visible(&self) -> bool {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::get_window_visibility(handle) }
}
pub fn set_visible(&self, v: bool) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_window_visibility(handle, v) }
}
pub fn size(&self) -> (u32, u32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::get_window_size(handle) }
}
pub fn set_size(&self, x: u32, y: u32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_window_size(handle, x, y, false) }
}
pub fn position(&self) -> (i32, i32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::get_window_position(handle) }
}
pub fn set_position(&self, x: i32, y: i32) {
let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
unsafe { wh::set_window_position(handle, x, y) }
}
pub fn collection(&self) -> Ref<Vec<D>> {
self.collection.borrow()
}
pub fn collection_mut(&self) -> RefMut<Vec<D>> {
self.collection.borrow_mut()
}
pub fn class_name(&self) -> &'static str {
"ListBox"
}
pub fn flags(&self) -> u32 {
WS_VISIBLE | WS_TABSTOP
}
pub fn forced_flags(&self) -> u32 {
use winapi::um::winuser::{LBS_HASSTRINGS, WS_BORDER, WS_VSCROLL, LBS_NOTIFY, WS_CHILD};
LBS_HASSTRINGS | LBS_NOTIFY | WS_BORDER | WS_CHILD | WS_VSCROLL
}
fn clear_inner(&self, handle: HWND) {
use winapi::um::winuser::LB_RESETCONTENT;
wh::send_message(handle, LB_RESETCONTENT, 0, 0);
}
}
impl<D: Display+Default> Drop for ListBox<D> {
fn drop(&mut self) {
self.handle.destroy();
}
}
pub struct ListBoxBuilder<'a, D: Display+Default> {
size: (i32, i32),
position: (i32, i32),
enabled: bool,
focus: bool,
flags: Option<ListBoxFlags>,
font: Option<&'a Font>,
collection: Option<Vec<D>>,
selected_index: Option<usize>,
multi_selection: Vec<usize>,
parent: Option<ControlHandle>
}
impl<'a, D: Display+Default> ListBoxBuilder<'a, D> {
pub fn flags(mut self, flags: ListBoxFlags) -> ListBoxBuilder<'a, D> {
self.flags = Some(flags);
self
}
pub fn size(mut self, size: (i32, i32)) -> ListBoxBuilder<'a, D> {
self.size = size;
self
}
pub fn position(mut self, pos: (i32, i32)) -> ListBoxBuilder<'a, D> {
self.position = pos;
self
}
pub fn font(mut self, font: Option<&'a Font>) -> ListBoxBuilder<'a, D> {
self.font = font;
self
}
pub fn parent<C: Into<ControlHandle>>(mut self, p: C) -> ListBoxBuilder<'a, D> {
self.parent = Some(p.into());
self
}
pub fn collection(mut self, collection: Vec<D>) -> ListBoxBuilder<'a, D> {
self.collection = Some(collection);
self
}
pub fn selected_index(mut self, index: Option<usize>) -> ListBoxBuilder<'a, D> {
self.selected_index = index;
self
}
pub fn multi_selection(mut self, select: Vec<usize>) -> ListBoxBuilder<'a, D> {
self.multi_selection = select;
self
}
pub fn enabled(mut self, enabled: bool) -> ListBoxBuilder<'a, D> {
self.enabled = enabled;
self
}
pub fn focus(mut self, focus: bool) -> ListBoxBuilder<'a, D> {
self.focus = focus;
self
}
pub fn build(self, out: &mut ListBox<D>) -> Result<(), NwgError> {
let flags = self.flags.map(|f| f.bits()).unwrap_or(out.flags());
let parent = match self.parent {
Some(p) => Ok(p),
None => Err(NwgError::no_parent("ListBox"))
}?;
out.handle = ControlBase::build_hwnd()
.class_name(out.class_name())
.forced_flags(out.forced_flags())
.flags(flags)
.size(self.size)
.position(self.position)
.parent(Some(parent))
.build()?;
if self.font.is_some() {
out.set_font(self.font);
} else {
out.set_font(Font::global_default().as_ref());
}
if let Some(col) = self.collection {
out.set_collection(col);
}
if flags & LBS_MULTIPLESEL == LBS_MULTIPLESEL {
for i in self.multi_selection {
out.multi_add_selection(i);
}
} else {
out.set_selection(self.selected_index);
}
if self.focus {
out.set_focus();
}
Ok(())
}
}
impl<D: Display+Default> PartialEq for ListBox<D> {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}