2012-11-01 15:19:01 +00:00
|
|
|
// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003.
|
|
|
|
|
2015-09-17 20:02:15 +00:00
|
|
|
#pragma warning(disable:4091) // workaround bug in VS2015 headers
|
|
|
|
|
2020-09-30 06:25:22 +00:00
|
|
|
#include "Windows/stdafx.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2016-10-12 09:32:24 +00:00
|
|
|
#include <functional>
|
2017-02-27 19:51:36 +00:00
|
|
|
#include <thread>
|
2016-10-12 09:32:24 +00:00
|
|
|
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "ShellUtil.h"
|
|
|
|
|
2012-11-17 22:44:29 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <commdlg.h>
|
2020-01-04 17:34:20 +00:00
|
|
|
#include <cderr.h>
|
2012-11-17 22:44:29 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
namespace W32Util
|
|
|
|
{
|
2013-10-15 11:28:09 +00:00
|
|
|
std::string BrowseForFolder(HWND parent, const char *title)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-09-07 20:32:29 +00:00
|
|
|
std::wstring titleString = ConvertUTF8ToWString(title);
|
2014-01-20 02:44:41 +00:00
|
|
|
return BrowseForFolder(parent, titleString.c_str());
|
|
|
|
}
|
2013-09-07 20:32:29 +00:00
|
|
|
|
2014-01-20 02:44:41 +00:00
|
|
|
std::string BrowseForFolder(HWND parent, const wchar_t *title)
|
|
|
|
{
|
2019-06-27 01:48:00 +00:00
|
|
|
BROWSEINFO info{};
|
2012-11-01 15:19:01 +00:00
|
|
|
info.hwndOwner = parent;
|
2014-01-20 02:44:41 +00:00
|
|
|
info.lpszTitle = title;
|
2012-11-01 15:19:01 +00:00
|
|
|
info.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
|
|
|
|
|
|
|
|
//info.pszDisplayName
|
2019-06-27 01:48:00 +00:00
|
|
|
auto idList = SHBrowseForFolder(&info);
|
2020-01-04 18:33:19 +00:00
|
|
|
HMODULE shell32 = GetModuleHandle(L"shell32.dll");
|
|
|
|
typedef BOOL (WINAPI *SHGetPathFromIDListEx_f)(PCIDLIST_ABSOLUTE pidl, PWSTR pszPath, DWORD cchPath, GPFIDL_FLAGS uOpts);
|
2020-01-04 18:57:23 +00:00
|
|
|
SHGetPathFromIDListEx_f SHGetPathFromIDListEx_ = (SHGetPathFromIDListEx_f)GetProcAddress(shell32, "SHGetPathFromIDListEx");
|
2020-01-04 18:33:19 +00:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
if (SHGetPathFromIDListEx_) {
|
|
|
|
std::wstring temp;
|
|
|
|
do {
|
|
|
|
// Assume it's failing if it goes on too long.
|
|
|
|
if (temp.size() > 32768 * 10) {
|
|
|
|
temp.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
temp.resize(temp.size() + MAX_PATH);
|
|
|
|
} while (SHGetPathFromIDListEx_(idList, &temp[0], (DWORD)temp.size(), GPFIDL_DEFAULT) == 0);
|
|
|
|
result = ConvertWStringToUTF8(temp);
|
|
|
|
} else {
|
|
|
|
wchar_t temp[MAX_PATH]{};
|
|
|
|
SHGetPathFromIDList(idList, temp);
|
|
|
|
result = ConvertWStringToUTF8(temp);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2019-06-27 01:48:00 +00:00
|
|
|
CoTaskMemFree(idList);
|
2020-01-04 18:33:19 +00:00
|
|
|
return result;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------
|
|
|
|
// function WinBrowseForFileName
|
|
|
|
//---------------------------------------------------------------------------------------------------
|
2020-01-04 17:34:20 +00:00
|
|
|
bool BrowseForFileName(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
|
|
|
|
const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension,
|
|
|
|
std::string &_strFileName) {
|
|
|
|
// Let's hope this is large enough, don't want to trigger the dialog twice...
|
|
|
|
std::wstring filenameBuffer(32768 * 10, '\0');
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2019-06-27 01:48:00 +00:00
|
|
|
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-01-04 17:34:20 +00:00
|
|
|
auto resetFileBuffer = [&] {
|
|
|
|
ofn.nMaxFile = (DWORD)filenameBuffer.size();
|
|
|
|
ofn.lpstrFile = &filenameBuffer[0];
|
|
|
|
if (!_strFileName.empty())
|
|
|
|
wcsncpy(ofn.lpstrFile, ConvertUTF8ToWString(_strFileName).c_str(), filenameBuffer.size() - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
resetFileBuffer();
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lpstrInitialDir = _pInitialFolder;
|
|
|
|
ofn.lpstrFilter = _pFilter;
|
2020-01-04 17:34:20 +00:00
|
|
|
ofn.lpstrFileTitle = nullptr;
|
|
|
|
ofn.nMaxFileTitle = 0;
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lpstrDefExt = _pExtension;
|
|
|
|
ofn.hwndOwner = _hParent;
|
2020-01-04 17:35:00 +00:00
|
|
|
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER;
|
|
|
|
if (!_bLoad)
|
|
|
|
ofn.Flags |= OFN_HIDEREADONLY;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-01-04 17:34:20 +00:00
|
|
|
int success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
|
|
|
|
if (success == 0 && CommDlgExtendedError() == FNERR_BUFFERTOOSMALL) {
|
|
|
|
size_t sz = *(unsigned short *)&filenameBuffer[0];
|
|
|
|
// Documentation is unclear if this is WCHARs to CHARs.
|
|
|
|
filenameBuffer.resize(filenameBuffer.size() + sz * 2);
|
|
|
|
resetFileBuffer();
|
|
|
|
success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-01-04 17:34:20 +00:00
|
|
|
if (success) {
|
2013-08-26 17:00:16 +00:00
|
|
|
_strFileName = ConvertWStringToUTF8(ofn.lpstrFile);
|
2012-11-01 15:19:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-04 17:34:20 +00:00
|
|
|
return false;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 19:32:05 +00:00
|
|
|
std::vector<std::string> BrowseForFileNameMultiSelect(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
|
2020-01-04 17:34:20 +00:00
|
|
|
const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension) {
|
|
|
|
// Let's hope this is large enough, don't want to trigger the dialog twice...
|
|
|
|
std::wstring filenameBuffer(32768 * 10, '\0');
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2019-06-27 01:48:00 +00:00
|
|
|
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-01-04 17:34:20 +00:00
|
|
|
auto resetFileBuffer = [&] {
|
|
|
|
ofn.nMaxFile = (DWORD)filenameBuffer.size();
|
|
|
|
ofn.lpstrFile = &filenameBuffer[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
resetFileBuffer();
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lpstrInitialDir = _pInitialFolder;
|
|
|
|
ofn.lpstrFilter = _pFilter;
|
2020-01-04 17:34:20 +00:00
|
|
|
ofn.lpstrFileTitle = nullptr;
|
|
|
|
ofn.nMaxFileTitle = 0;
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lpstrDefExt = _pExtension;
|
|
|
|
ofn.hwndOwner = _hParent;
|
2020-01-04 17:35:00 +00:00
|
|
|
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_ALLOWMULTISELECT;
|
|
|
|
if (!_bLoad)
|
|
|
|
ofn.Flags |= OFN_HIDEREADONLY;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
std::vector<std::string> files;
|
2020-01-04 17:34:20 +00:00
|
|
|
int success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
|
|
|
|
if (success == 0 && CommDlgExtendedError() == FNERR_BUFFERTOOSMALL) {
|
|
|
|
size_t sz = *(unsigned short *)&filenameBuffer[0];
|
|
|
|
// Documentation is unclear if this is WCHARs to CHARs.
|
|
|
|
filenameBuffer.resize(filenameBuffer.size() + sz * 2);
|
|
|
|
resetFileBuffer();
|
|
|
|
success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-01-04 17:34:20 +00:00
|
|
|
if (success) {
|
2013-08-26 17:00:16 +00:00
|
|
|
std::string directory = ConvertWStringToUTF8(ofn.lpstrFile);
|
|
|
|
wchar_t *temp = ofn.lpstrFile;
|
2020-01-04 17:34:20 +00:00
|
|
|
temp += wcslen(temp) + 1;
|
|
|
|
if (*temp == 0) {
|
2012-11-01 15:19:01 +00:00
|
|
|
//we only got one file
|
2020-01-04 17:34:20 +00:00
|
|
|
files.push_back(directory);
|
|
|
|
} else {
|
|
|
|
while (*temp) {
|
|
|
|
files.push_back(directory + "\\" + ConvertWStringToUTF8(temp));
|
|
|
|
temp += wcslen(temp) + 1;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 17:34:20 +00:00
|
|
|
return files;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2014-01-20 02:44:41 +00:00
|
|
|
|
2020-01-04 18:57:23 +00:00
|
|
|
std::string UserDocumentsPath() {
|
|
|
|
std::string result;
|
|
|
|
HMODULE shell32 = GetModuleHandle(L"shell32.dll");
|
|
|
|
typedef HRESULT(WINAPI *SHGetKnownFolderPath_f)(REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
|
|
|
|
SHGetKnownFolderPath_f SHGetKnownFolderPath_ = (SHGetKnownFolderPath_f)GetProcAddress(shell32, "SHGetKnownFolderPath");
|
|
|
|
if (SHGetKnownFolderPath_) {
|
|
|
|
PWSTR path = nullptr;
|
|
|
|
if (SHGetKnownFolderPath_(FOLDERID_Documents, 0, nullptr, &path) == S_OK) {
|
|
|
|
result = ConvertWStringToUTF8(path);
|
|
|
|
}
|
|
|
|
if (path)
|
|
|
|
CoTaskMemFree(path);
|
|
|
|
} else {
|
|
|
|
wchar_t path[MAX_PATH];
|
|
|
|
if (SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path) == S_OK) {
|
|
|
|
result = ConvertWStringToUTF8(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-01-20 02:44:41 +00:00
|
|
|
AsyncBrowseDialog::AsyncBrowseDialog(HWND parent, UINT completeMsg, std::wstring title)
|
2015-01-18 02:56:55 +00:00
|
|
|
: type_(DIR), parent_(parent), completeMsg_(completeMsg), title_(title), complete_(false), result_(false) {
|
2014-01-20 02:44:41 +00:00
|
|
|
thread_ = new std::thread(std::bind(&AsyncBrowseDialog::Execute, this));
|
|
|
|
thread_->detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncBrowseDialog::AsyncBrowseDialog(Type type, HWND parent, UINT completeMsg, std::wstring title, std::wstring initialFolder, std::wstring filter, std::wstring extension)
|
2015-01-18 02:56:55 +00:00
|
|
|
: type_(type), parent_(parent), completeMsg_(completeMsg), title_(title), initialFolder_(initialFolder), filter_(filter), extension_(extension), complete_(false), result_(false) {
|
2014-01-20 02:44:41 +00:00
|
|
|
thread_ = new std::thread(std::bind(&AsyncBrowseDialog::Execute, this));
|
|
|
|
thread_->detach();
|
|
|
|
}
|
|
|
|
|
2014-02-15 05:08:24 +00:00
|
|
|
AsyncBrowseDialog::~AsyncBrowseDialog() {
|
|
|
|
delete thread_;
|
|
|
|
}
|
|
|
|
|
2014-01-20 02:44:41 +00:00
|
|
|
bool AsyncBrowseDialog::GetResult(std::string &filename) {
|
|
|
|
filename = filename_;
|
|
|
|
return result_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncBrowseDialog::Execute() {
|
|
|
|
switch (type_) {
|
|
|
|
case DIR:
|
|
|
|
filename_ = BrowseForFolder(parent_, title_.c_str());
|
|
|
|
result_ = filename_ != "";
|
|
|
|
complete_ = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPEN:
|
|
|
|
case SAVE:
|
|
|
|
result_ = BrowseForFileName(type_ == OPEN, parent_, title_.c_str(), initialFolder_.size() ? initialFolder_.c_str() : 0, filter_.c_str(), extension_.c_str(), filename_);
|
|
|
|
complete_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostMessage(parent_, completeMsg_, 0, 0);
|
|
|
|
}
|
2016-10-12 09:32:24 +00:00
|
|
|
}
|