use crate::co;
use crate::decl::*;
use crate::gui::privs::*;
use crate::msg::*;
use crate::prelude::*;
pub struct MsgError {
src_msg: WndMsg,
source: Box<dyn std::error::Error + Send + Sync>,
}
impl std::error::Error for MsgError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.source.as_ref())
}
}
impl std::fmt::Display for MsgError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "WM {} - {}", self.src_msg.msg_id, self.source.to_string())
}
}
impl std::fmt::Debug for MsgError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl MsgError {
#[must_use]
pub const fn new(
src_msg: WndMsg,
source: Box<dyn std::error::Error + Send + Sync>,
) -> MsgError {
Self { src_msg, source }
}
#[must_use]
pub const fn src_msg(&self) -> WndMsg {
self.src_msg
}
}
pub fn dpi_x(x_val: i32) -> i32 {
initial_gui_setup();
MulDiv(x_val.into(), unsafe { DPI }.0, 96)
}
pub fn dpi_y(x_val: i32) -> i32 {
initial_gui_setup();
MulDiv(x_val.into(), unsafe { DPI }.0, 96)
}
pub fn dpi(x_val: i32, y_val: i32) -> (i32, i32) {
(dpi_x(x_val), dpi_y(y_val))
}
pub enum Brush {
Color(co::COLOR),
Handle(HBRUSH),
None,
}
impl Brush {
#[must_use]
pub fn as_hbrush(&self) -> HBRUSH {
use Brush::*;
match self {
Color(c) => HBRUSH::from_sys_color(*c),
Handle(h) => unsafe { h.raw_copy() },
None => HBRUSH::NULL,
}
}
}
pub enum Cursor {
Handle(HCURSOR),
Id(u16),
Idc(co::IDC),
None,
Str(WString),
}
impl Cursor {
#[must_use]
pub fn as_hcursor(&self, hinst: &HINSTANCE) -> SysResult<HCURSOR> {
unsafe {
use Cursor::*;
Ok(match self {
Handle(h) => h.raw_copy(),
Id(id) => hinst.LoadCursor(IdIdcStr::Id(*id))?.leak(),
Idc(idc) => HINSTANCE::NULL.LoadCursor(IdIdcStr::Idc(*idc))?.leak(),
None => HCURSOR::NULL,
Str(s) => hinst.LoadCursor(IdIdcStr::Str(s.clone()))?.leak(),
})
}
}
}
pub enum Icon {
Handle(HICON),
Id(u16),
Idi(co::IDI),
None,
Str(WString),
}
impl Icon {
#[must_use]
pub fn as_hicon(&self, hinst: &HINSTANCE) -> SysResult<HICON> {
unsafe {
use Icon::*;
Ok(match self {
Handle(h) => h.raw_copy(),
Id(id) => hinst.LoadIcon(IdIdiStr::Id(*id))?.leak(),
Idi(idi) => HINSTANCE::NULL.LoadIcon(IdIdiStr::Idi(*idi))?.leak(),
None => HICON::NULL,
Str(s) => hinst.LoadIcon(IdIdiStr::Str(s.clone()))?.leak(),
})
}
}
}