use std::pin::Pin;
use std::sync::Arc;
use crate::co;
use crate::decl::*;
use crate::gui::{privs::*, *};
use crate::prelude::*;
struct PropSheetObj {
title: String,
page_titles: Vec<WString>,
pages: Vec<PropSheetPage>,
apply_button: bool,
context_help: bool,
margin: bool,
}
#[derive(Clone)]
pub struct PropSheet(Pin<Arc<PropSheetObj>>);
unsafe impl Send for PropSheet {}
impl PropSheet {
#[must_use]
pub fn new(opts: PropSheetOpts) -> Self {
if opts.pages.is_empty() {
panic!("Property sheet has no pages.");
}
let (page_titles, pages): (Vec<WString>, Vec<PropSheetPage>) = opts
.pages
.into_iter()
.map(|(page_title, page)| (WString::from_str(*page_title), page.clone()))
.unzip();
Self(Arc::pin(PropSheetObj {
title: opts.title.to_owned(),
page_titles,
pages,
apply_button: opts.apply_button,
context_help: opts.context_help,
margin: opts.margin,
}))
}
pub fn show(&self, parent: Option<&impl GuiParent>) -> AnyResult<()> {
let ps_pages = self
.0
.pages
.iter()
.zip(&self.0.page_titles)
.map(|(page, page_title)| {
let mut ps_page = page.generate_propsheetpage();
ps_page.pszTitle = page_title.as_ptr();
ps_page
})
.collect::<Vec<_>>();
let mut psh = PROPSHEETHEADER::default();
psh.dwFlags = co::PSH::PROPSHEETPAGE;
if !self.0.apply_button {
psh.dwFlags |= co::PSH::NOAPPLYNOW;
}
if !self.0.context_help {
psh.dwFlags |= co::PSH::NOCONTEXTHELP;
}
if !self.0.margin {
psh.dwFlags |= co::PSH::NOMARGIN;
}
match parent {
Some(parent) => {
psh.hInstance = parent.hwnd().hinstance();
psh.hwndParent = unsafe { parent.hwnd().raw_copy() };
},
None => {
psh.hInstance = HINSTANCE::GetModuleHandle(None).expect(DONTFAIL);
},
}
let mut wcaption = WString::from_str(&self.0.title);
psh.set_pszCaption(Some(&mut wcaption));
psh.nPages = ps_pages.len() as _;
psh.set_ppsp(&ps_pages);
unsafe {
PropertySheet(&psh)?;
}
Ok(())
}
}
pub struct PropSheetOpts<'a> {
pub title: &'a str,
pub pages: &'a [(&'a str, PropSheetPage)],
pub apply_button: bool,
pub context_help: bool,
pub margin: bool,
}
impl<'a> Default for PropSheetOpts<'a> {
fn default() -> Self {
Self {
title: "",
pages: &[],
apply_button: true,
context_help: false,
margin: true,
}
}
}