X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/106d80ad373817594dba9aeacef76b663eda2d0b..fd0fca1a25931e51d7e56695875fba5030bf3e07:/src/msw/filedlg.cpp?ds=sidebyside diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index a31464cb56..f71cfb69c8 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "filedlg.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -210,6 +206,30 @@ void wxFileDialog::DoMoveWindow(int x, int y, int WXUNUSED(width), int WXUNUSED( */ } +// helper used below in ShowModal(): style is used to determine whether to show +// the "Save file" dialog (if it contains wxSAVE bit) or "Open file" one; +// returns true on success or false on failure in which case err is filled with +// the CDERR_XXX constant +static bool DoShowCommFileDialog(OPENFILENAME *of, long style, DWORD *err) +{ + if ( style & wxSAVE ? GetSaveFileName(of) : GetOpenFileName(of) ) + return true; + + if ( err ) + { +#ifdef __WXWINCE__ + // according to MSDN, CommDlgExtendedError() should work under CE as + // well but apparently in practice it doesn't (anybody has more + // details?) + *err = GetLastError(); +#else + *err = CommDlgExtendedError(); +#endif + } + + return false; +} + int wxFileDialog::ShowModal() { HWND hWnd = 0; @@ -261,7 +281,8 @@ int wxFileDialog::ShowModal() } // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the - // standard dialog does by default + // standard dialog does by default (notice that under NT it does it anyhow, + // OFN_NOCHANGEDIR or not, see below) if ( !(m_dialogStyle & wxCHANGE_DIR) ) { msw_flags |= OFN_NOCHANGEDIR; @@ -279,7 +300,7 @@ int wxFileDialog::ShowModal() // comcdlg32.dll, but as we don't use the extended fields anyhow, set // the struct size to the old value - otherwise, the programs compiled // with new headers will not work with the old libraries -#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500) +#if !defined(__WXWINCE__) && defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500) of.lStructSize = sizeof(OPENFILENAME) - (sizeof(void *) + 2*sizeof(DWORD)); #else // old headers @@ -391,35 +412,45 @@ int wxFileDialog::ShowModal() } } - //== Execute FileDialog >>================================================= + // store off before the standard windows dialog can possibly change it + const wxString cwdOrig = wxGetCwd(); - bool success = (m_dialogStyle & wxSAVE ? GetSaveFileName(&of) - : GetOpenFileName(&of)) != 0; + //== Execute FileDialog >>================================================= - DWORD errCode = CommDlgExtendedError(); + DWORD errCode; + bool success = DoShowCommFileDialog(&of, m_dialogStyle, &errCode); -#ifdef __WIN32__ - if (!success && (errCode == CDERR_STRUCTSIZE)) + // sometimes we may have a mismatch between the headers used to compile the + // library and the run-time version of comdlg32.dll, try to account for it +#ifndef __WXWINCE__ + if ( !success && errCode == CDERR_STRUCTSIZE ) { // The struct size has changed so try a smaller or bigger size + const int oldStructSize = of.lStructSize; + of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD)); + success = DoShowCommFileDialog(&of, m_dialogStyle, &errCode); - int oldStructSize = of.lStructSize; - of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD)); - success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0) - : (GetOpenFileName(&of) != 0); - errCode = CommDlgExtendedError(); - - if (!success && (errCode == CDERR_STRUCTSIZE)) + if ( !success && (errCode == CDERR_STRUCTSIZE) ) { - of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD)); - success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0) - : (GetOpenFileName(&of) != 0); + // try to adjust in the other direction + of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD)); + success = DoShowCommFileDialog(&of, m_dialogStyle, &errCode); } } -#endif // __WIN32__ +#endif // !__WXWINCE__ if ( success ) { + // 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 + // wxCHANGE_DIR let's restore the current working directory to what it + // was before the dialog was shown. + if ( msw_flags & OFN_NOCHANGEDIR ) + { + wxSetWorkingDirectory(cwdOrig); + } + m_fileNames.Empty(); if ( ( m_dialogStyle & wxMULTIPLE ) && @@ -486,20 +517,19 @@ int wxFileDialog::ShowModal() m_dir = wxPathOnly(fileNameBuffer); } } +#ifdef __WXDEBUG__ else { // common dialog failed - why? -#ifdef __WXDEBUG__ - DWORD dwErr = CommDlgExtendedError(); - if ( dwErr != 0 ) + if ( errCode != 0 ) { - // this msg is only for developers + // this msg is only for developers so don't translate it wxLogError(wxT("Common dialog failed with error code %0lx."), - dwErr); + errCode); } //else: it was just cancelled -#endif } +#endif // __WXDEBUG__ return success ? wxID_OK : wxID_CANCEL;