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.
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "shlobj.h"
|
|
|
|
|
2014-01-20 02:44:41 +00:00
|
|
|
#include "base/functional.h"
|
|
|
|
#include "thread/thread.h"
|
2013-08-26 17:00:16 +00:00
|
|
|
#include "util/text/utf8.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "ShellUtil.h"
|
2013-08-26 17:00:16 +00:00
|
|
|
#include "CommDlg.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-17 22:44:29 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <commdlg.h>
|
|
|
|
|
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)
|
|
|
|
{
|
2012-11-01 15:19:01 +00:00
|
|
|
BROWSEINFO info;
|
|
|
|
memset(&info,0,sizeof(info));
|
|
|
|
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
|
|
|
|
LPCITEMIDLIST idList = SHBrowseForFolder(&info);
|
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
wchar_t temp[MAX_PATH];
|
2012-11-01 15:19:01 +00:00
|
|
|
SHGetPathFromIDList(idList, temp);
|
2013-08-26 17:00:16 +00:00
|
|
|
if (wcslen(temp))
|
|
|
|
return ConvertWStringToUTF8(temp);
|
2012-11-01 15:19:01 +00:00
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------
|
|
|
|
// function WinBrowseForFileName
|
|
|
|
//---------------------------------------------------------------------------------------------------
|
2013-08-26 19:32:05 +00:00
|
|
|
bool BrowseForFileName (bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
|
|
|
|
const wchar_t *_pInitialFolder,const wchar_t *_pFilter,const wchar_t *_pExtension,
|
2012-11-01 15:19:01 +00:00
|
|
|
std::string& _strFileName)
|
|
|
|
{
|
2013-08-26 17:00:16 +00:00
|
|
|
wchar_t szFile [MAX_PATH+1] = {0};
|
|
|
|
wchar_t szFileTitle [MAX_PATH+1] = {0};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
ZeroMemory (&ofn,sizeof (ofn));
|
|
|
|
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lStructSize = sizeof (OPENFILENAME);
|
|
|
|
ofn.lpstrInitialDir = _pInitialFolder;
|
|
|
|
ofn.lpstrFilter = _pFilter;
|
|
|
|
ofn.nMaxFile = sizeof (szFile);
|
|
|
|
ofn.lpstrFile = szFile;
|
|
|
|
ofn.lpstrFileTitle = szFileTitle;
|
|
|
|
ofn.nMaxFileTitle = sizeof (szFileTitle);
|
|
|
|
ofn.lpstrDefExt = _pExtension;
|
|
|
|
ofn.hwndOwner = _hParent;
|
|
|
|
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_HIDEREADONLY;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-18 21:16:34 +00:00
|
|
|
if (!_strFileName.empty())
|
|
|
|
wcsncpy(ofn.lpstrFile, ConvertUTF8ToWString(_strFileName).c_str(), MAX_PATH);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
if (((_bLoad) ? GetOpenFileName(&ofn) : GetSaveFileName (&ofn)))
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-08-26 17:00:16 +00:00
|
|
|
_strFileName = ConvertWStringToUTF8(ofn.lpstrFile);
|
2012-11-01 15:19:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-26 19:32:05 +00:00
|
|
|
std::vector<std::string> BrowseForFileNameMultiSelect(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
|
|
|
|
const wchar_t *_pInitialFolder,const wchar_t *_pFilter,const wchar_t *_pExtension)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-08-26 17:00:16 +00:00
|
|
|
wchar_t szFile [MAX_PATH+1+2048*2] = {0};
|
|
|
|
wchar_t szFileTitle [MAX_PATH+1] = {0};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
OPENFILENAME ofn;
|
|
|
|
|
|
|
|
ZeroMemory (&ofn,sizeof (ofn));
|
|
|
|
|
2015-09-02 02:13:51 +00:00
|
|
|
ofn.lStructSize = sizeof (OPENFILENAME);
|
|
|
|
ofn.lpstrInitialDir = _pInitialFolder;
|
|
|
|
ofn.lpstrFilter = _pFilter;
|
|
|
|
ofn.nMaxFile = sizeof (szFile);
|
|
|
|
ofn.lpstrFile = szFile;
|
|
|
|
ofn.lpstrFileTitle = szFileTitle;
|
|
|
|
ofn.nMaxFileTitle = sizeof (szFileTitle);
|
|
|
|
ofn.lpstrDefExt = _pExtension;
|
|
|
|
ofn.hwndOwner = _hParent;
|
|
|
|
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
std::vector<std::string> files;
|
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
if (((_bLoad) ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn)))
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-08-26 17:00:16 +00:00
|
|
|
std::string directory = ConvertWStringToUTF8(ofn.lpstrFile);
|
|
|
|
wchar_t *temp = ofn.lpstrFile;
|
|
|
|
wchar_t *oldtemp = temp;
|
|
|
|
temp += wcslen(temp)+1;
|
2012-11-01 15:19:01 +00:00
|
|
|
if (*temp==0)
|
|
|
|
{
|
|
|
|
//we only got one file
|
2013-08-26 17:00:16 +00:00
|
|
|
files.push_back(ConvertWStringToUTF8(oldtemp));
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*temp)
|
|
|
|
{
|
2013-08-26 17:00:16 +00:00
|
|
|
files.push_back(directory+"\\"+ConvertWStringToUTF8(temp));
|
|
|
|
temp += wcslen(temp)+1;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::vector<std::string>(); // empty vector;
|
|
|
|
}
|
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);
|
|
|
|
}
|
2015-09-02 02:13:51 +00:00
|
|
|
}
|