use std::ops::{Deref, DerefMut};
use crate::decl::*;
use crate::kernel::privs::*;
use crate::prelude::*;
use crate::user::ffi;
pub struct CloseClipboardGuard<'a> {
_hwnd: &'a HWND,
hclip: HCLIPBOARD,
}
impl<'a> Drop for CloseClipboardGuard<'a> {
fn drop(&mut self) {
unsafe {
ffi::CloseClipboard(); }
}
}
impl<'a> Deref for CloseClipboardGuard<'a> {
type Target = HCLIPBOARD;
fn deref(&self) -> &Self::Target {
&self.hclip
}
}
impl<'a> DerefMut for CloseClipboardGuard<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hclip
}
}
impl<'a> CloseClipboardGuard<'a> {
#[must_use]
pub const unsafe fn new(hwnd: &'a HWND, hclip: HCLIPBOARD) -> Self {
Self { _hwnd: hwnd, hclip }
}
}
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<'a> {
hwnd: &'a HWND,
hdc: HDC,
ps: PAINTSTRUCT,
}
impl<'a> Drop for EndPaintGuard<'a> {
fn drop(&mut self) {
unsafe {
ffi::EndPaint(self.hwnd.ptr(), pcvoid(&self.ps));
}
}
}
impl<'a> Deref for EndPaintGuard<'a> {
type Target = HDC;
fn deref(&self) -> &Self::Target {
&self.hdc
}
}
impl<'a> DerefMut for EndPaintGuard<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hdc
}
}
impl<'a> EndPaintGuard<'a> {
#[must_use]
pub const unsafe fn new(hwnd: &'a HWND, hdc: HDC, ps: PAINTSTRUCT) -> Self {
Self { hwnd, hdc, ps }
}
#[must_use]
pub const fn paintstruct(&self) -> &PAINTSTRUCT {
&self.ps
}
}
pub struct ReleaseCaptureGuard<'a> {
_hwnd: &'a HWND,
hwnd_prev: Option<HWND>,
}
impl<'a> Drop for ReleaseCaptureGuard<'a> {
fn drop(&mut self) {
unsafe {
ffi::ReleaseCapture(); }
}
}
impl<'a> ReleaseCaptureGuard<'a> {
#[must_use]
pub const unsafe fn new(hwnd: &'a HWND, hwnd_prev: Option<HWND>) -> Self {
Self { _hwnd: hwnd, hwnd_prev }
}
#[must_use]
pub const fn prev_hwnd(&self) -> Option<&HWND> {
self.hwnd_prev.as_ref()
}
}
pub struct ReleaseDCGuard<'a> {
hwnd: &'a HWND,
hdc: HDC,
}
impl<'a> Drop for ReleaseDCGuard<'a> {
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<'a> Deref for ReleaseDCGuard<'a> {
type Target = HDC;
fn deref(&self) -> &Self::Target {
&self.hdc
}
}
impl<'a> DerefMut for ReleaseDCGuard<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hdc
}
}
impl<'a> ReleaseDCGuard<'a> {
#[must_use]
pub const unsafe fn new(hwnd: &'a HWND, hdc: HDC) -> Self {
Self { hwnd, hdc }
}
#[must_use]
pub fn leak(&mut self) -> HDC {
std::mem::replace(&mut self.hdc, HDC::INVALID)
}
}