Skip to content

Commit

Permalink
Улучшен класс CSaveFileDialog.
Browse files Browse the repository at this point in the history
Запрещен метод CFileDialog::GetPathName, т.к. не поддерживает длинные пути. Вместо него добавлен метод GetFilePath.
Больше не гадаем, сколько памяти может понадобиться.
  • Loading branch information
v0lt committed Sep 14, 2024
1 parent 5db5523 commit db4367f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/apps/mplayerc/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6042,11 +6042,14 @@ void CMainFrame::OnFileSaveAs()
OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT,
ext_list, GetModalParent());

if (fd.DoModal() != IDOK || !in.CompareNoCase(fd.GetPathName())) {
if (fd.DoModal() != IDOK) {
return;
}
CStringW savedFileName(fd.GetFilePath());
if (in.CompareNoCase(fd.GetFilePath()) == 0) {
return;
}

CStringW savedFileName(fd.GetPathName());
if (ext.GetLength() && GetFileExt(savedFileName).IsEmpty()) {
savedFileName.Append(ext);
}
Expand Down Expand Up @@ -6570,7 +6573,7 @@ void CMainFrame::OnFileSaveImage()
s.iThumbLevelPNG = std::clamp(fd.m_PngCompression, 1, 9);
s.bSnapShotSubtitles = fd.m_bDrawSubtitles;

CStringW pdst = fd.GetPathName();
CStringW pdst = fd.GetFilePath();
if (GetFileExt(pdst).MakeLower() != s.strSnapShotExt) {
RenameFileExt(pdst, s.strSnapShotExt);
}
Expand Down Expand Up @@ -6665,7 +6668,7 @@ void CMainFrame::OnFileSaveThumbnails()
s.iThumbLevelPNG = std::clamp(fd.m_PngCompression, 1, 9);
s.bSnapShotSubtitles = fd.m_bDrawSubtitles;

CStringW pdst = fd.GetPathName();
CStringW pdst = fd.GetFilePath();
if (GetFileExt(pdst).MakeLower() != s.strSnapShotExt) {
RenameFileExt(pdst, s.strSnapShotExt);
}
Expand Down Expand Up @@ -6840,7 +6843,7 @@ void CMainFrame::OnFileSaveSubtitle()

if (fd.DoModal() == IDOK) {
CAutoLock cAutoLock(&m_csSubLock);
pVSF->Save(fd.GetPathName());
pVSF->Save(fd.GetFilePath());
}

return;
Expand Down Expand Up @@ -6874,7 +6877,7 @@ void CMainFrame::OnFileSaveSubtitle()
s.bSubSaveExternalStyleFile = !!fd.GetSaveExternalStyleFile();

CAutoLock cAutoLock(&m_csSubLock);
pRTS->SaveAs(fd.GetPathName(), types[fd.m_ofn.nFilterIndex - 1], m_pCAP->GetFPS(), m_pCAP->GetSubtitleDelay(), fd.GetEncoding(), s.bSubSaveExternalStyleFile);
pRTS->SaveAs(fd.GetFilePath(), types[fd.m_ofn.nFilterIndex - 1], m_pCAP->GetFPS(), m_pCAP->GetSubtitleDelay(), fd.GetEncoding(), s.bSubSaveExternalStyleFile);
}

return;
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mplayerc/PPageFileInfoRes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void CPPageFileInfoRes::OnSaveAs()
ext_list, this);
if (fd.DoModal() == IDOK) {
FILE* f = nullptr;
if (_wfopen_s(&f, fd.GetPathName(), L"wb") == 0) {
if (_wfopen_s(&f, fd.GetFilePath(), L"wb") == 0) {
fwrite((*it).data.data(), 1, (*it).data.size(), f);
fclose(f);
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mplayerc/PPageFileInfoSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void CPPageFileInfoSheet::OnSaveAs()

if (filedlg.DoModal() == IDOK) {
CFile mFile;
if (mFile.Open(filedlg.GetPathName(), CFile::modeCreate | CFile::modeWrite)) {
if (mFile.Open(filedlg.GetFilePath(), CFile::modeCreate | CFile::modeWrite)) {
const WCHAR bom = 0xFEFF;
mFile.Write(&bom, sizeof(WCHAR));
mFile.Write(LPCWSTR(m_mi.MI_Text), m_mi.MI_Text.GetLength() * sizeof(WCHAR));
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mplayerc/PlayerCaptureDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ void CPlayerCaptureDialog::OnOpenFile()
L"Media files (*.avi,*.mkv,*.dsm)|*.avi;*.mkv;*.dsm|", this);

if (fd.DoModal() == IDOK) {
CString str = fd.GetPathName();
CString str = fd.GetFilePath();

CString ext = str.Mid(str.ReverseFind('.')+1).MakeLower();
if (ext == L"avi") {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mplayerc/PlayerPlaylistBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3683,7 +3683,7 @@ void CPlayerPlaylistBar::OnContextMenu(CWnd* /*pWnd*/, CPoint p)

int idx = fd.m_pOFN->nFilterIndex;

CStringW path(fd.GetPathName());
CStringW path(fd.GetFilePath());
CStringW base = GetFolderPath(path);
s.strLastSavedPlaylistDir = GetAddSlash(base);

Expand Down
29 changes: 23 additions & 6 deletions src/apps/mplayerc/SaveFileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,32 @@ CSaveFileDialog::CSaveFileDialog(
: CFileDialog(FALSE, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
CStringW dir = ::GetFolderPath(lpszFileName);
int size = std::max(MAX_PATH, dir.GetLength() + 1);
size_t size = dir.GetLength() + 1;

m_pstrInitialDir.reset(new WCHAR[size]);
memset(m_pstrInitialDir.get(), 0, size * sizeof(WCHAR));
wcscpy_s(m_pstrInitialDir.get(), size, dir.GetString());

m_pOFN->lpstrInitialDir = m_pstrInitialDir.get();
}

CStringW CSaveFileDialog::GetFilePath()
{
CStringW filepath;

CComPtr<IFileSaveDialog> pFileSaveDialog = GetIFileSaveDialog();
if (pFileSaveDialog) {
CComPtr<IShellItem> pShellItem;
HRESULT hr = pFileSaveDialog->GetResult(&pShellItem);
if (SUCCEEDED(hr)) {
LPWSTR pszName = nullptr;
hr = pShellItem->GetDisplayName(SIGDN_FILESYSPATH, &pszName);
if (SUCCEEDED(hr)) {
filepath = pszName;
CoTaskMemFree(pszName);
}
}
}

size += 500;
m_pstrFile.reset(new WCHAR[size]);
memset(m_pstrFile.get(), 0, size * sizeof(WCHAR));
m_pOFN->lpstrFile = m_pstrFile.get();
m_pOFN->nMaxFile = size;
return filepath;
}
7 changes: 6 additions & 1 deletion src/apps/mplayerc/SaveFileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class CSaveFileDialog : public CFileDialog
DECLARE_DYNAMIC(CSaveFileDialog)

private:
// CFileDialog::GetPathName does not work for long paths, use GetFilePath instead.
CFileDialog::GetPathName;

std::unique_ptr<WCHAR[]> m_pstrInitialDir;
std::unique_ptr<WCHAR[]> m_pstrFile;

public:
CSaveFileDialog(
Expand All @@ -40,4 +42,7 @@ class CSaveFileDialog : public CFileDialog
LPCWSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
virtual ~CSaveFileDialog() = default;

// Returns the file path selected for saving. Long paths are supported.
CStringW GetFilePath();
};

0 comments on commit db4367f

Please sign in to comment.