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.
This commit is contained in:
Vadim Zeitlin 2022-10-21 01:46:58 +01:00
parent 0850666ebd
commit 0a33da8058

View file

@ -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);
}