2018-10-24 20:40:12 +02:00
|
|
|
use std::ffi::OsString;
|
|
|
|
|
use std::os::windows::ffi::OsStringExt;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
use std::{mem, ptr};
|
|
|
|
|
|
|
|
|
|
use winapi::ctypes::c_void;
|
|
|
|
|
use winapi::shared::guiddef::REFIID;
|
|
|
|
|
use winapi::shared::minwindef::{DWORD, MAX_PATH, UINT, ULONG};
|
|
|
|
|
use winapi::shared::windef::{HWND, POINTL};
|
|
|
|
|
use winapi::shared::winerror::S_OK;
|
|
|
|
|
use winapi::um::objidl::IDataObject;
|
2018-11-20 09:28:26 +01:00
|
|
|
use winapi::um::oleidl::{DROPEFFECT_COPY, DROPEFFECT_NONE, IDropTarget, IDropTargetVtbl};
|
2018-10-24 20:40:12 +02:00
|
|
|
use winapi::um::winnt::HRESULT;
|
|
|
|
|
use winapi::um::{shellapi, unknwnbase};
|
|
|
|
|
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::platform_impl::platform::WindowId;
|
2018-10-24 20:40:12 +02:00
|
|
|
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::Event;
|
|
|
|
|
use crate::window::WindowId as SuperWindowId;
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub struct FileDropHandlerData {
|
|
|
|
|
pub interface: IDropTarget,
|
|
|
|
|
refcount: AtomicUsize,
|
|
|
|
|
window: HWND,
|
2019-06-18 02:27:00 +08:00
|
|
|
send_event: Box<dyn Fn(Event<()>)>,
|
2018-11-20 09:28:26 +01:00
|
|
|
cursor_effect: DWORD,
|
|
|
|
|
hovered_is_valid: bool, // If the currently hovered item is not valid there must not be any `HoveredFileCancelled` emitted
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct FileDropHandler {
|
|
|
|
|
pub data: *mut FileDropHandlerData,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
impl FileDropHandler {
|
2019-06-18 02:27:00 +08:00
|
|
|
pub fn new(window: HWND, send_event: Box<dyn Fn(Event<()>)>) -> FileDropHandler {
|
2018-10-24 20:40:12 +02:00
|
|
|
let data = Box::new(FileDropHandlerData {
|
|
|
|
|
interface: IDropTarget {
|
|
|
|
|
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
|
|
|
|
|
},
|
|
|
|
|
refcount: AtomicUsize::new(1),
|
|
|
|
|
window,
|
2019-02-05 10:30:33 -05:00
|
|
|
send_event,
|
2018-11-20 09:28:26 +01:00
|
|
|
cursor_effect: DROPEFFECT_NONE,
|
|
|
|
|
hovered_is_valid: false,
|
2018-10-24 20:40:12 +02:00
|
|
|
});
|
|
|
|
|
FileDropHandler {
|
|
|
|
|
data: Box::into_raw(data),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement IUnknown
|
|
|
|
|
pub unsafe extern "system" fn QueryInterface(
|
|
|
|
|
_this: *mut unknwnbase::IUnknown,
|
|
|
|
|
_riid: REFIID,
|
|
|
|
|
_ppvObject: *mut *mut c_void,
|
|
|
|
|
) -> HRESULT {
|
|
|
|
|
// This function doesn't appear to be required for an `IDropTarget`.
|
|
|
|
|
// An implementation would be nice however.
|
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn AddRef(this: *mut unknwnbase::IUnknown) -> ULONG {
|
|
|
|
|
let drop_handler_data = Self::from_interface(this);
|
|
|
|
|
let count = drop_handler_data.refcount.fetch_add(1, Ordering::Release) + 1;
|
|
|
|
|
count as ULONG
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn Release(this: *mut unknwnbase::IUnknown) -> ULONG {
|
|
|
|
|
let drop_handler = Self::from_interface(this);
|
|
|
|
|
let count = drop_handler.refcount.fetch_sub(1, Ordering::Release) - 1;
|
|
|
|
|
if count == 0 {
|
|
|
|
|
// Destroy the underlying data
|
|
|
|
|
Box::from_raw(drop_handler as *mut FileDropHandlerData);
|
|
|
|
|
}
|
|
|
|
|
count as ULONG
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragEnter(
|
|
|
|
|
this: *mut IDropTarget,
|
|
|
|
|
pDataObj: *const IDataObject,
|
|
|
|
|
_grfKeyState: DWORD,
|
|
|
|
|
_pt: *const POINTL,
|
2018-11-20 09:28:26 +01:00
|
|
|
pdwEffect: *mut DWORD,
|
2018-10-24 20:40:12 +02:00
|
|
|
) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::HoveredFile;
|
2018-10-24 20:40:12 +02:00
|
|
|
let drop_handler = Self::from_interface(this);
|
2018-11-20 09:28:26 +01:00
|
|
|
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
|
2019-02-05 10:30:33 -05:00
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
2018-10-24 20:40:12 +02:00
|
|
|
window_id: SuperWindowId(WindowId(drop_handler.window)),
|
|
|
|
|
event: HoveredFile(filename),
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-11-20 09:28:26 +01:00
|
|
|
drop_handler.hovered_is_valid = hdrop.is_some();
|
|
|
|
|
drop_handler.cursor_effect = if drop_handler.hovered_is_valid {
|
|
|
|
|
DROPEFFECT_COPY
|
|
|
|
|
} else {
|
|
|
|
|
DROPEFFECT_NONE
|
|
|
|
|
};
|
|
|
|
|
*pdwEffect = drop_handler.cursor_effect;
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragOver(
|
2018-11-20 09:28:26 +01:00
|
|
|
this: *mut IDropTarget,
|
2018-10-24 20:40:12 +02:00
|
|
|
_grfKeyState: DWORD,
|
|
|
|
|
_pt: *const POINTL,
|
2018-11-20 09:28:26 +01:00
|
|
|
pdwEffect: *mut DWORD,
|
2018-10-24 20:40:12 +02:00
|
|
|
) -> HRESULT {
|
2018-11-20 09:28:26 +01:00
|
|
|
let drop_handler = Self::from_interface(this);
|
|
|
|
|
*pdwEffect = drop_handler.cursor_effect;
|
|
|
|
|
|
2018-10-24 20:40:12 +02:00
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragLeave(this: *mut IDropTarget) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::HoveredFileCancelled;
|
2018-10-24 20:40:12 +02:00
|
|
|
let drop_handler = Self::from_interface(this);
|
2018-11-20 09:28:26 +01:00
|
|
|
if drop_handler.hovered_is_valid {
|
2019-02-05 10:30:33 -05:00
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
2018-11-20 09:28:26 +01:00
|
|
|
window_id: SuperWindowId(WindowId(drop_handler.window)),
|
|
|
|
|
event: HoveredFileCancelled,
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn Drop(
|
|
|
|
|
this: *mut IDropTarget,
|
|
|
|
|
pDataObj: *const IDataObject,
|
|
|
|
|
_grfKeyState: DWORD,
|
|
|
|
|
_pt: *const POINTL,
|
|
|
|
|
_pdwEffect: *mut DWORD,
|
|
|
|
|
) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::DroppedFile;
|
2018-10-24 20:40:12 +02:00
|
|
|
let drop_handler = Self::from_interface(this);
|
|
|
|
|
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
|
2019-02-05 10:30:33 -05:00
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
2018-10-24 20:40:12 +02:00
|
|
|
window_id: SuperWindowId(WindowId(drop_handler.window)),
|
|
|
|
|
event: DroppedFile(filename),
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-11-20 09:28:26 +01:00
|
|
|
if let Some(hdrop) = hdrop {
|
|
|
|
|
shellapi::DragFinish(hdrop);
|
|
|
|
|
}
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn from_interface<'a, InterfaceT>(this: *mut InterfaceT) -> &'a mut FileDropHandlerData {
|
|
|
|
|
&mut *(this as *mut _)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 09:28:26 +01:00
|
|
|
unsafe fn iterate_filenames<F>(data_obj: *const IDataObject, callback: F) -> Option<shellapi::HDROP>
|
2018-10-24 20:40:12 +02:00
|
|
|
where
|
|
|
|
|
F: Fn(PathBuf),
|
|
|
|
|
{
|
|
|
|
|
use winapi::ctypes::wchar_t;
|
2018-11-20 09:28:26 +01:00
|
|
|
use winapi::shared::winerror::{SUCCEEDED, DV_E_FORMATETC};
|
2018-10-24 20:40:12 +02:00
|
|
|
use winapi::shared::wtypes::{CLIPFORMAT, DVASPECT_CONTENT};
|
|
|
|
|
use winapi::um::objidl::{FORMATETC, TYMED_HGLOBAL};
|
|
|
|
|
use winapi::um::shellapi::DragQueryFileW;
|
|
|
|
|
use winapi::um::winuser::CF_HDROP;
|
|
|
|
|
|
|
|
|
|
let mut drop_format = FORMATETC {
|
|
|
|
|
cfFormat: CF_HDROP as CLIPFORMAT,
|
|
|
|
|
ptd: ptr::null(),
|
|
|
|
|
dwAspect: DVASPECT_CONTENT,
|
|
|
|
|
lindex: -1,
|
|
|
|
|
tymed: TYMED_HGLOBAL,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut medium = mem::uninitialized();
|
2018-11-20 09:28:26 +01:00
|
|
|
let get_data_result = (*data_obj).GetData(&mut drop_format, &mut medium);
|
|
|
|
|
if SUCCEEDED(get_data_result) {
|
2018-10-24 20:40:12 +02:00
|
|
|
let hglobal = (*medium.u).hGlobal();
|
|
|
|
|
let hdrop = (*hglobal) as shellapi::HDROP;
|
|
|
|
|
|
|
|
|
|
// The second parameter (0xFFFFFFFF) instructs the function to return the item count
|
|
|
|
|
let item_count = DragQueryFileW(hdrop, 0xFFFFFFFF, ptr::null_mut(), 0);
|
|
|
|
|
|
|
|
|
|
let mut pathbuf: [wchar_t; MAX_PATH] = mem::uninitialized();
|
|
|
|
|
|
|
|
|
|
for i in 0..item_count {
|
|
|
|
|
let character_count =
|
|
|
|
|
DragQueryFileW(hdrop, i, pathbuf.as_mut_ptr(), MAX_PATH as UINT) as usize;
|
|
|
|
|
|
|
|
|
|
if character_count > 0 {
|
|
|
|
|
callback(OsString::from_wide(&pathbuf[0..character_count]).into());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 09:28:26 +01:00
|
|
|
return Some(hdrop);
|
|
|
|
|
} else if get_data_result == DV_E_FORMATETC {
|
|
|
|
|
// If the dropped item is not a file this error will occur.
|
|
|
|
|
// In this case it is OK to return without taking further action.
|
|
|
|
|
debug!("Error occured while processing dropped/hovered item: item is not a file.");
|
|
|
|
|
return None;
|
|
|
|
|
} else {
|
|
|
|
|
debug!("Unexpected error occured while processing dropped/hovered item.");
|
|
|
|
|
return None;
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl FileDropHandlerData {
|
|
|
|
|
fn send_event(&self, event: Event<()>) {
|
|
|
|
|
(self.send_event)(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 20:40:12 +02:00
|
|
|
impl Drop for FileDropHandler {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
FileDropHandler::Release(self.data as *mut unknwnbase::IUnknown);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DROP_TARGET_VTBL: IDropTargetVtbl = IDropTargetVtbl {
|
|
|
|
|
parent: unknwnbase::IUnknownVtbl {
|
|
|
|
|
QueryInterface: FileDropHandler::QueryInterface,
|
|
|
|
|
AddRef: FileDropHandler::AddRef,
|
|
|
|
|
Release: FileDropHandler::Release,
|
|
|
|
|
},
|
|
|
|
|
DragEnter: FileDropHandler::DragEnter,
|
|
|
|
|
DragOver: FileDropHandler::DragOver,
|
|
|
|
|
DragLeave: FileDropHandler::DragLeave,
|
|
|
|
|
Drop: FileDropHandler::Drop,
|
|
|
|
|
};
|