Windows: Make open/save dialogs long path aware.

At least, in most cases.  If the filename is super long, it'll open twice,
but we try to avoid.
This commit is contained in:
Unknown W. Brackets committed 2020-01-04 09:34:20 -08:00
1 parent 46464d2722
commit 97ac18ceee
2 files changed
+72 -75

No files matched your search

+54 -42
View File
@@ -9,10 +9,10 @@
#include "util/text/utf8.h"
#include "ShellUtil.h"
#include "CommDlg.h"
#include <shlobj.h>
#include <commdlg.h>
#include <cderr.h>
namespace W32Util
{
@@ -44,80 +44,92 @@ namespace W32Util
//---------------------------------------------------------------------------------------------------
// function WinBrowseForFileName
//---------------------------------------------------------------------------------------------------
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)
{
wchar_t szFile [MAX_PATH+1] = {0};
wchar_t szFileTitle [MAX_PATH+1] = {0};
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');
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
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();
ofn.lpstrInitialDir = _pInitialFolder;
ofn.lpstrFilter = _pFilter;
ofn.nMaxFile = ARRAY_SIZE(szFile);
ofn.lpstrFile = szFile;
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = ARRAY_SIZE(szFileTitle);
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrDefExt = _pExtension;
ofn.hwndOwner = _hParent;
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_HIDEREADONLY;
if (!_strFileName.empty())
wcsncpy(ofn.lpstrFile, ConvertUTF8ToWString(_strFileName).c_str(), MAX_PATH);
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);
}
if (((_bLoad) ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn)))
{
if (success) {
_strFileName = ConvertWStringToUTF8(ofn.lpstrFile);
return true;
}
else
return false;
return false;
}
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)
{
wchar_t szFile [MAX_PATH+1+2048*2] = {0};
wchar_t szFileTitle [MAX_PATH+1] = {0};
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');
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
auto resetFileBuffer = [&] {
ofn.nMaxFile = (DWORD)filenameBuffer.size();
ofn.lpstrFile = &filenameBuffer[0];
};
resetFileBuffer();
ofn.lpstrInitialDir = _pInitialFolder;
ofn.lpstrFilter = _pFilter;
ofn.nMaxFile = ARRAY_SIZE(szFile);
ofn.lpstrFile = szFile;
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = ARRAY_SIZE(szFileTitle);
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrDefExt = _pExtension;
ofn.hwndOwner = _hParent;
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
std::vector<std::string> files;
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);
}
if (((_bLoad) ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn)))
{
if (success) {
std::string directory = ConvertWStringToUTF8(ofn.lpstrFile);
wchar_t *temp = ofn.lpstrFile;
wchar_t *oldtemp = temp;
temp += wcslen(temp)+1;
if (*temp==0)
{
temp += wcslen(temp) + 1;
if (*temp == 0) {
//we only got one file
files.push_back(ConvertWStringToUTF8(oldtemp));
}
else
{
while (*temp)
{
files.push_back(directory+"\\"+ConvertWStringToUTF8(temp));
temp += wcslen(temp)+1;
files.push_back(directory);
} else {
while (*temp) {
files.push_back(directory + "\\" + ConvertWStringToUTF8(temp));
temp += wcslen(temp) + 1;
}
}
return files;
}
else
return std::vector<std::string>(); // empty vector;
return files;
}
AsyncBrowseDialog::AsyncBrowseDialog(HWND parent, UINT completeMsg, std::wstring title)