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 2020-01-04 09:34:20 -08:00
parent 46464d2722
commit 97ac18ceee
2 changed files with 72 additions and 75 deletions

View file

@ -4,6 +4,7 @@
#include "Core/MemMap.h" #include "Core/MemMap.h"
#include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/MIPS/JitCommon/JitCommon.h"
#include "Windows/W32Util/Misc.h" #include "Windows/W32Util/Misc.h"
#include "Windows/W32Util/ShellUtil.h"
#include "Windows/MainWindow.h" #include "Windows/MainWindow.h"
#include "Windows/InputBox.h" #include "Windows/InputBox.h"
@ -1323,47 +1324,31 @@ std::string CtrlDisAsmView::disassembleRange(u32 start, u32 size)
return result; return result;
} }
void CtrlDisAsmView::disassembleToFile() void CtrlDisAsmView::disassembleToFile() {
{
wchar_t fileName[MAX_PATH];
u32 size;
// get size // get size
if (executeExpressionWindow(wnd,debugger,size) == false) return; u32 size;
if (size == 0 || size > 10*1024*1024) if (executeExpressionWindow(wnd,debugger,size) == false)
{ return;
if (size == 0 || size > 10*1024*1024) {
MessageBox(wnd,L"Invalid size!",L"Error",MB_OK); MessageBox(wnd,L"Invalid size!",L"Error",MB_OK);
return; return;
} }
// get file name std::string filename;
OPENFILENAME ofn; if (W32Util::BrowseForFileName(false, nullptr, L"Save Disassembly As...", nullptr, L"All Files\0*.*\0\0", nullptr, filename)) {
ZeroMemory( &ofn , sizeof( ofn)); std::wstring fileName = ConvertUTF8ToWString(filename);
ofn.lStructSize = sizeof ( ofn ); FILE *output = _wfopen(fileName.c_str(), L"wb");
ofn.hwndOwner = NULL ; if (output == nullptr) {
ofn.lpstrFile = fileName ; MessageBox(wnd, L"Could not open file!", L"Error", MB_OK);
ofn.lpstrFile[0] = '\0'; return;
ofn.nMaxFile = sizeof( fileName ); }
ofn.lpstrFilter = L"All Files\0*.*\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir = NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn) == false) return; std::string disassembly = disassembleRange(curAddress, size);
fprintf(output, "%s", disassembly.c_str());
FILE* output = _wfopen(fileName, L"wb"); fclose(output);
if (output == NULL) { MessageBox(wnd, L"Finished!", L"Done", MB_OK);
MessageBox(wnd,L"Could not open file!",L"Error",MB_OK);
return;
} }
std::string disassembly = disassembleRange(curAddress,size);
fprintf(output,"%s",disassembly.c_str());
fclose(output);
MessageBox(wnd,L"Finished!",L"Done",MB_OK);
} }
void CtrlDisAsmView::getOpcodeText(u32 address, char* dest, int bufsize) void CtrlDisAsmView::getOpcodeText(u32 address, char* dest, int bufsize)

View file

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