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.

See #22898.

(cherry picked from commit 0a33da8058150df7e0ee4acf30385c3034029e97)
This commit is contained in:
Vadim Zeitlin 2022-10-21 01:46:58 +01:00
parent 7835005985
commit f0ee875c22
2 changed files with 12 additions and 1 deletions

View File

@ -264,6 +264,7 @@ wxMSW:
- Fix return value of wxZoomGestureEvent::GetZoomFactor() (s-murphree, #22818).
- Update top level icon resolution on DPI change (#22807).
- Remove "Printing" from title when printing to PDF (Blake-Madden, #22836).
- Fix sometimes missing overvwrite prompt in "Save" file dialog (#22898).
wxOSX:

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