use super::*;
use crate::platform::win32::GetForegroundWindow;
pub(super) const GWL_STYLE: i32 = -16;
pub(super) const GWL_EXSTYLE: i32 = -20;
pub(super) const GWLP_HWNDPARENT: i32 = -8;
pub(super) const GW_OWNER: u32 = 4;
pub(super) const GA_ROOT: u32 = 2;
pub(super) const WS_POPUP: isize = 0x8000_0000u32 as i32 as isize;
pub(super) const WS_CHILD: isize = 0x4000_0000;
pub(super) const WS_CLIPSIBLINGS: isize = 0x0400_0000;
pub(super) const WS_CAPTION: isize = 0x00C0_0000;
pub(super) const WS_THICKFRAME: isize = 0x0004_0000;
pub(super) const WS_SYSMENU: isize = 0x0008_0000;
pub(super) const WS_MINIMIZEBOX: isize = 0x0002_0000;
pub(super) const WS_MAXIMIZEBOX: isize = 0x0001_0000;
pub(super) const WS_EX_DLGMODALFRAME: isize = 0x0000_0001;
pub(super) const WS_EX_WINDOWEDGE: isize = 0x0000_0100;
pub(super) const WS_EX_CLIENTEDGE: isize = 0x0000_0200;
pub(super) const WS_EX_TOOLWINDOW: isize = 0x0000_0080;
pub(super) const WS_EX_APPWINDOW: isize = 0x0004_0000;
pub(super) const WS_EX_STATICEDGE: isize = 0x0002_0000;
pub(super) const WS_EX_NOACTIVATE: isize = 0x0800_0000;
pub(super) const SWP_NOZORDER: u32 = 0x0004;
pub(super) const SWP_NOACTIVATE: u32 = 0x0010;
pub(super) const SWP_FRAMECHANGED: u32 = 0x0020;
pub(super) const SWP_ASYNCWINDOWPOS: u32 = 0x4000;
pub(super) const SW_HIDE: i32 = 0;
pub(super) const SW_SHOWNA: i32 = 8;
pub(super) const SW_RESTORE: i32 = 9;
pub(super) const WM_CLOSE: u32 = 0x0010;
pub(super) const RDW_INVALIDATE: u32 = 0x0001;
pub(super) const RDW_ERASE: u32 = 0x0004;
pub(super) const RDW_FRAME: u32 = 0x0400;
pub(super) const RDW_ALLCHILDREN: u32 = 0x0080;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
Child,
Owned,
}
pub struct Window {
pub hwnd: Hwnd,
parent: Hwnd,
mode: Mode,
}
impl Window {
pub fn adopt(hwnd: Hwnd, parent: Hwnd, mode: Mode) -> Window {
if mode == Mode::Owned {
return Self::own(hwnd, parent);
}
unsafe {
if IsZoomed(hwnd) != 0 {
ShowWindow(hwnd, SW_RESTORE);
}
let style = GetWindowLongPtrW(hwnd, GWL_STYLE);
let style = style & !(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP);
SetWindowLongPtrW(hwnd, GWL_STYLE, style | WS_CHILD | WS_CLIPSIBLINGS);
let ex = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
let ex =
ex & !(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_APPWINDOW | WS_EX_STATICEDGE);
SetWindowLongPtrW(hwnd, GWL_EXSTYLE, ex);
SetParent(hwnd, parent);
SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | 0x0001 | 0x0002);
}
unsafe {
log::debug!(
"adopted {hwnd:#x} into {parent:#x}: parent now {:#x}, style {:#x}",
GetParent(hwnd),
GetWindowLongPtrW(hwnd, GWL_STYLE)
);
}
Window { hwnd, parent, mode: Mode::Child }
}
fn own(hwnd: Hwnd, parent: Hwnd) -> Window {
unsafe {
if IsZoomed(hwnd) != 0 {
ShowWindow(hwnd, SW_RESTORE);
}
let style = GetWindowLongPtrW(hwnd, GWL_STYLE);
let style = style & !(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CHILD);
SetWindowLongPtrW(hwnd, GWL_STYLE, style | WS_POPUP);
let ex = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
let ex =
ex & !(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_APPWINDOW | WS_EX_STATICEDGE);
SetWindowLongPtrW(hwnd, GWL_EXSTYLE, ex | WS_EX_TOOLWINDOW);
SetWindowLongPtrW(hwnd, GWLP_HWNDPARENT, parent);
SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | 0x0001 | 0x0002);
}
Window { hwnd, parent, mode: Mode::Owned }
}
pub fn screen_origin(&self) -> [i32; 2] {
if self.mode == Mode::Child {
return [0, 0];
}
let mut at = [0, 0];
unsafe { ClientToScreen(self.parent, &mut at) };
at
}
pub fn alive(&self) -> bool {
unsafe {
IsWindow(self.hwnd) != 0
&& (self.mode == Mode::Owned || GetAncestor(self.hwnd, GA_ROOT) == GetAncestor(self.parent, GA_ROOT))
}
}
pub fn place(&self, [x, y, w, h]: [i32; 4]) {
unsafe {
let [ox, oy] = self.screen_origin();
SetWindowPos(
self.hwnd,
0,
x + ox,
y + oy,
w.max(1),
h.max(1),
SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS,
);
SetWindowRgn(self.hwnd, CreateRectRgn(0, 0, w.max(1), h.max(1)), 0);
RedrawWindow(self.hwnd, std::ptr::null(), 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN);
}
}
pub fn bounds(&self) -> Option<[i32; 4]> {
let r = rect_of(self.hwnd)?;
let mut corners = [r.left, r.top, r.right, r.bottom];
unsafe { MapWindowPoints(0, self.parent, corners.as_mut_ptr(), 2) };
Some([corners[0], corners[1], corners[2] - corners[0], corners[3] - corners[1]])
}
pub fn contains(&self, x: i32, y: i32) -> bool {
rect_of(self.hwnd).is_some_and(|r| x >= r.left && x < r.right && y >= r.top && y < r.bottom)
}
pub fn show(&self, show: bool) {
unsafe { ShowWindow(self.hwnd, if show { SW_SHOWNA } else { SW_HIDE }) };
}
pub fn focus(&self) {
unsafe {
if self.mode == Mode::Owned {
SetForegroundWindow(self.hwnd);
} else {
SetFocus(self.hwnd);
}
}
}
pub fn give_back(&self) {
unsafe {
if self.mode == Mode::Owned {
SetForegroundWindow(self.parent);
} else {
SetFocus(self.parent);
}
}
}
pub fn has_focus(&self) -> bool {
unsafe {
if self.mode == Mode::Owned {
let mut at = GetForegroundWindow();
for _ in 0..4 {
if at == 0 {
return false;
}
if at == self.hwnd {
return true;
}
at = GetWindow(at, GW_OWNER);
}
return false;
}
let at = GetFocus();
at != 0 && (at == self.hwnd || IsChild(self.hwnd, at) != 0)
}
}
pub fn close(&self) {
unsafe { PostMessageW(self.hwnd, WM_CLOSE, 0, 0) };
}
}