From 0a33da8058150df7e0ee4acf30385c3034029e97 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 21 Oct 2022 01:46:58 +0100 Subject: [PATCH] Fix MSW "Save" dialog overwrite prompt for files without ext We need to call IFileDialog::SetDefaultExtension() to ensure that the native dialog itself appends the extension itself to the files entered without extension it, otherwise it doesn't do it at all, in spite of SetFileTypes() being already called, and so doesn't show "Confirm overwrite" prompt for them, while wxMSW own code does append the extension later, resulting in the existing files being overwritten without any confirmation. It would probably be a good idea to stop appending the extension on our own and just use the one appended by the dialog to ensure that we don't have similar problems in the future, but don't change this yet. Closes #22898. --- src/msw/filedlg.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index 9c240aafcc..556b3ef9a3 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -1597,6 +1597,16 @@ int wxFileDialog::ShowIFileDialog(WXHWND hWndParent) hr = fileDialog->SetFileTypeIndex(m_filterIndex + 1); if ( FAILED(hr) ) wxLogApiError(wxS("IFileDialog::SetFileTypeIndex"), hr); + + // We need to call SetDefaultExtension() to make the file dialog append + // the selected extension by default. It will append the correct + // extension depending on the current file type choice if we call this + // function, but won't do anything at all without it, so find the first + // extension associated with the selected filter and use it here. + wxString defExt = + wildFilters[m_filterIndex].BeforeFirst(';').AfterFirst('.'); + if ( !defExt.empty() && defExt != wxS("*") ) + fileDialog->SetDefaultExtension(defExt.wc_str()); } if ( !m_dir.empty() ) @@ -1608,7 +1618,7 @@ int wxFileDialog::ShowIFileDialog(WXHWND hWndParent) { hr = fileDialog->SetFileName(m_fileName.wc_str()); if ( FAILED(hr) ) - wxLogApiError(wxS("IFileDialog::SetDefaultExtension"), hr); + wxLogApiError(wxS("IFileDialog::SetFileName"), hr); }