use std::ops::{Deref, DerefMut};
use crate::decl::*;
use crate::kernel::privs::*;
use crate::macros::*;
use crate::prelude::*;
use crate::user::ffi;
pub struct CloseClipboardGuard {
hwnd: HWND,
hclip: HCLIPBOARD,
}
impl Drop for CloseClipboardGuard {
fn drop(&mut self) {
unsafe { ffi::CloseClipboard() }; }
}
impl Deref for CloseClipboardGuard {
type Target = HCLIPBOARD;
fn deref(&self) -> &Self::Target {
&self.hclip
}
}
impl DerefMut for CloseClipboardGuard {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hclip
}
}
impl CloseClipboardGuard {
#[must_use]
pub unsafe fn new(hwnd: &HWND, hclip: HCLIPBOARD) -> Self {
Self { hwnd: unsafe { hwnd.raw_copy() }, hclip }
}
#[must_use]
pub const fn hwnd(&self) -> &HWND {
&self.hwnd
}
}
handle_guard! { CloseDesktopGuard: HDESK;
ffi::CloseDesktop;
}
handle_guard! { DestroyAcceleratorTableGuard: HACCEL;
ffi::DestroyAcceleratorTable;
}
handle_guard! { DestroyCursorGuard: HCURSOR;
ffi::DestroyCursor;
}
handle_guard! { DestroyIconGuard: HICON;
ffi::DestroyIcon;
}
handle_guard! { DestroyMenuGuard: HMENU;
ffi::DestroyMenu;
}
handle_guard! { EndDeferWindowPosGuard: HDWP;
ffi::EndDeferWindowPos;
}
pub struct EndPaintGuard {
hwnd: HWND,
hdc: HDC,
ps: PAINTSTRUCT,
}
impl Drop for EndPaintGuard {
fn drop(&mut self) {
unsafe { ffi::EndPaint(self.hwnd.ptr(), pcvoid(&self.ps)) };
}
}
impl Deref for EndPaintGuard {
type Target = HDC;
fn deref(&self) -> &Self::Target {
&self.hdc
}
}
impl DerefMut for EndPaintGuard {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hdc
}
}
impl EndPaintGuard {
#[must_use]
pub unsafe fn new(hwnd: &HWND, hdc: HDC, ps: PAINTSTRUCT) -> Self {
Self {
hwnd: unsafe { hwnd.raw_copy() },
hdc,
ps,
}
}
#[must_use]
pub const fn paintstruct(&self) -> &PAINTSTRUCT {
&self.ps
}
#[must_use]
pub const fn hwnd(&self) -> &HWND {
&self.hwnd
}
}
pub struct ReleaseCaptureGuard {
hwnd: HWND,
hwnd_prev: Option<HWND>,
}
impl Drop for ReleaseCaptureGuard {
fn drop(&mut self) {
unsafe { ffi::ReleaseCapture() }; }
}
impl ReleaseCaptureGuard {
#[must_use]
pub unsafe fn new(hwnd: &HWND, hwnd_prev: Option<HWND>) -> Self {
Self {
hwnd: unsafe { hwnd.raw_copy() },
hwnd_prev,
}
}
#[must_use]
pub const fn hwnd(&self) -> &HWND {
return &self.hwnd;
}
#[must_use]
pub const fn prev_hwnd(&self) -> Option<&HWND> {
self.hwnd_prev.as_ref()
}
}
pub struct ReleaseDCGuard {
hwnd: HWND,
hdc: HDC,
}
impl Drop for ReleaseDCGuard {
fn drop(&mut self) {
if let Some(h) = self.hwnd.as_opt() {
if let Some(dc) = self.hdc.as_opt() {
unsafe { ffi::ReleaseDC(h.ptr(), dc.ptr()) }; }
}
}
}
impl Deref for ReleaseDCGuard {
type Target = HDC;
fn deref(&self) -> &Self::Target {
&self.hdc
}
}
impl DerefMut for ReleaseDCGuard {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hdc
}
}
impl ReleaseDCGuard {
#[must_use]
pub unsafe fn new(hwnd: &HWND, hdc: HDC) -> Self {
Self { hwnd: unsafe { hwnd.raw_copy() }, hdc }
}
#[must_use]
pub fn leak(&mut self) -> HDC {
std::mem::replace(&mut self.hdc, HDC::INVALID)
}
#[must_use]
pub const fn hwnd(&self) -> &HWND {
&self.hwnd
}
}