1 parent
4b5bcd2983
commit
07ed4cf724
5 files changed
+165
-10
No files matched your search
@@ -486,6 +486,77 @@ Clip textClip(const std::wstring &text)
|
||||
memcpy(f.data.data(), text.c_str(), f.data.size());
|
||||
return Clip{{std::move(f)}};
|
||||
}
|
||||
|
||||
Clip fileDropClip(std::span<const std::filesystem::path> paths)
|
||||
{
|
||||
std::wstring names;
|
||||
for(const auto &path:paths)
|
||||
{
|
||||
if(!path.is_absolute() || path.native().find(L'\0')!=std::wstring::npos)
|
||||
throw std::invalid_argument("A file clipboard path must be absolute and contain no nulls.");
|
||||
names+=path.native();
|
||||
names+=L'\0';
|
||||
}
|
||||
if(paths.empty()) names+=L'\0';
|
||||
names+=L'\0';
|
||||
DROPFILES header{};
|
||||
header.pFiles=sizeof(header);
|
||||
header.fWide=TRUE;
|
||||
Format files{CF_HDROP,{},Bytes(sizeof(header)+names.size()*sizeof(wchar_t))};
|
||||
memcpy(files.data.data(),&header,sizeof(header));
|
||||
memcpy(files.data.data()+sizeof(header),names.data(),names.size()*sizeof(wchar_t));
|
||||
const DWORD effect=DROPEFFECT_COPY;
|
||||
Format preferred{0,CFSTR_PREFERREDDROPEFFECT,Bytes(sizeof(effect))};
|
||||
memcpy(preferred.data.data(),&effect,sizeof(effect));
|
||||
return Clip{{std::move(files),std::move(preferred)}};
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> fileDropPaths(HANDLE medium)
|
||||
{
|
||||
const auto size=medium?GlobalSize(medium):0;
|
||||
if(size<sizeof(DROPFILES)) return {};
|
||||
const auto bytes=static_cast<const unsigned char *>(GlobalLock(medium));
|
||||
if(!bytes) return {};
|
||||
DROPFILES header{};
|
||||
memcpy(&header,bytes,sizeof(header));
|
||||
bool terminated=false;
|
||||
if(header.pFiles>=sizeof(header) && header.pFiles<size)
|
||||
{
|
||||
const auto start=bytes+header.pFiles;
|
||||
if(!header.fWide)
|
||||
terminated=std::search_n(start,bytes+size,2,static_cast<unsigned char>(0))!=bytes+size;
|
||||
else if(header.pFiles%alignof(wchar_t)==0)
|
||||
{
|
||||
const auto names=reinterpret_cast<const wchar_t *>(start);
|
||||
const auto end=names+(size-header.pFiles)/sizeof(wchar_t);
|
||||
terminated=std::search_n(names,end,2,L'\0')!=end;
|
||||
}
|
||||
}
|
||||
GlobalUnlock(medium);
|
||||
if(!terminated) return {};
|
||||
const auto drop=reinterpret_cast<HDROP>(medium);
|
||||
const UINT count=DragQueryFileW(drop,UINT(-1),nullptr,0);
|
||||
std::vector<std::filesystem::path> paths;
|
||||
std::set<std::filesystem::path> seen;
|
||||
for(UINT index=0;index<count;++index)
|
||||
{
|
||||
const UINT length=DragQueryFileW(drop,index,nullptr,0);
|
||||
if(!length || length>32767) continue;
|
||||
std::wstring name(length+1,L'\0');
|
||||
if(DragQueryFileW(drop,index,name.data(),length+1)!=length) continue;
|
||||
name.resize(length);
|
||||
auto path=std::filesystem::path(std::move(name)).lexically_normal();
|
||||
if(path.is_absolute() && seen.insert(path).second) paths.push_back(std::move(path));
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> readClipboardFilePaths(HWND window)
|
||||
{
|
||||
ClipboardLock lock(window);
|
||||
return fileDropPaths(GetClipboardData(CF_HDROP));
|
||||
}
|
||||
|
||||
std::wstring Clip::text() const
|
||||
{
|
||||
for (const auto &f : formats)
|
||||
|
||||
Reference in new issue
Block a user