From ceaa840d7b1d91ee61db77e3030df42a186ac5ea Mon Sep 17 00:00:00 2001 From: Steve Browne Date: Thu, 31 Mar 2016 18:32:07 -0400 Subject: [PATCH] Restore cwd even if wxFileDialog is cancelled in wxMSW Sometimes the file dialog changes the current directory even if it is cancelled, so restore it in any case, not only after successful return. Closes https://github.com/wxWidgets/wxWidgets/pull/263 --- src/msw/filedlg.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index be2df982c6..1c16505ce5 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -654,23 +654,30 @@ int wxFileDialog::ShowModal() } } + // Create a temporary struct to restore the CWD when we exit this function // store off before the standard windows dialog can possibly change it - const wxString cwdOrig = wxGetCwd(); - - //== Execute FileDialog >>================================================= - - if ( !ShowCommFileDialog(&of, m_windowStyle) ) - return wxID_CANCEL; + struct CwdRestore + { + wxString value; + ~CwdRestore() + { + if (!value.empty()) + wxSetWorkingDirectory(value); + } + } cwdOrig; // GetOpenFileName will always change the current working directory on // (according to MSDN) "Windows NT 4.0/2000/XP" because the flag // OFN_NOCHANGEDIR has no effect. If the user did not specify // wxFD_CHANGE_DIR let's restore the current working directory to what it // was before the dialog was shown. - if ( msw_flags & OFN_NOCHANGEDIR ) - { - wxSetWorkingDirectory(cwdOrig); - } + if (msw_flags & OFN_NOCHANGEDIR) + cwdOrig.value = wxGetCwd(); + + //== Execute FileDialog >>================================================= + + if ( !ShowCommFileDialog(&of, m_windowStyle) ) + return wxID_CANCEL; m_fileNames.Empty();