X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d43803715268f5396741b3f0eb4b94d1408c9f9d..ee3de647ffd5891138864d032bc0f2bd5a130ef0:/src/msw/filedlg.cpp diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index b788f8a7cf..7327c7bdd7 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -4,7 +4,6 @@ // Author: Julian Smart // Modified by: // Created: 01/02/97 -// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -43,8 +42,11 @@ #include #include +#include "wx/dynlib.h" #include "wx/filename.h" +#include "wx/scopeguard.h" #include "wx/tokenzr.h" +#include "wx/modalhook.h" // ---------------------------------------------------------------------------- // constants @@ -65,7 +67,7 @@ // ---------------------------------------------------------------------------- // standard dialog size for the old Windows systems where the dialog wasn't -// resizeable +// resizable static wxRect gs_rectDialog(0, 0, 428, 266); // ============================================================================ @@ -74,6 +76,78 @@ static wxRect gs_rectDialog(0, 0, 428, 266); IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) +// ---------------------------------------------------------------------------- + +namespace +{ + +#if wxUSE_DYNLIB_CLASS + +typedef BOOL (WINAPI *GetProcessUserModeExceptionPolicy_t)(LPDWORD); +typedef BOOL (WINAPI *SetProcessUserModeExceptionPolicy_t)(DWORD); + +GetProcessUserModeExceptionPolicy_t gs_pfnGetProcessUserModeExceptionPolicy + = (GetProcessUserModeExceptionPolicy_t) -1; + +SetProcessUserModeExceptionPolicy_t gs_pfnSetProcessUserModeExceptionPolicy + = (SetProcessUserModeExceptionPolicy_t) -1; + +DWORD gs_oldExceptionPolicyFlags = 0; + +bool gs_changedPolicy = false; + +#endif // #if wxUSE_DYNLIB_CLASS + +/* +Since Windows 7 by default (callback) exceptions aren't swallowed anymore +with native x64 applications. Exceptions can occur in a file dialog when +using the hook procedure in combination with third-party utilities. +Since Windows 7 SP1 the swallowing of exceptions can be enabled again +by using SetProcessUserModeExceptionPolicy. +*/ +void ChangeExceptionPolicy() +{ +#if wxUSE_DYNLIB_CLASS + gs_changedPolicy = false; + + wxLoadedDLL dllKernel32(wxT("kernel32.dll")); + + if ( gs_pfnGetProcessUserModeExceptionPolicy + == (GetProcessUserModeExceptionPolicy_t) -1) + { + wxDL_INIT_FUNC(gs_pfn, GetProcessUserModeExceptionPolicy, dllKernel32); + wxDL_INIT_FUNC(gs_pfn, SetProcessUserModeExceptionPolicy, dllKernel32); + } + + if ( !gs_pfnGetProcessUserModeExceptionPolicy + || !gs_pfnSetProcessUserModeExceptionPolicy + || !gs_pfnGetProcessUserModeExceptionPolicy(&gs_oldExceptionPolicyFlags) ) + { + return; + } + + if ( gs_pfnSetProcessUserModeExceptionPolicy(gs_oldExceptionPolicyFlags + | 0x1 /* PROCESS_CALLBACK_FILTER_ENABLED */ ) ) + { + gs_changedPolicy = true; + } + +#endif // wxUSE_DYNLIB_CLASS +} + +void RestoreExceptionPolicy() +{ +#if wxUSE_DYNLIB_CLASS + if (gs_changedPolicy) + { + gs_changedPolicy = false; + (void) gs_pfnSetProcessUserModeExceptionPolicy(gs_oldExceptionPolicyFlags); + } +#endif // wxUSE_DYNLIB_CLASS +} + +} // unnamed namespace + // ---------------------------------------------------------------------------- // hook function for moving the dialog // ---------------------------------------------------------------------------- @@ -86,15 +160,35 @@ wxFileDialogHookFunction(HWND hDlg, { switch ( iMsg ) { +#ifndef __WXWINCE__ + case WM_INITDIALOG: + { + OPENFILENAME* ofn = reinterpret_cast(lParam); + reinterpret_cast(ofn->lCustData) + ->MSWOnInitDialogHook((WXHWND)hDlg); + } + break; +#endif // __WXWINCE__ + case WM_NOTIFY: { - OFNOTIFY *pNotifyCode = wx_reinterpret_cast(OFNOTIFY *, lParam); - if ( pNotifyCode->hdr.code == CDN_INITDONE ) + OFNOTIFY* const + pNotifyCode = reinterpret_cast(lParam); + wxFileDialog* const + dialog = reinterpret_cast( + pNotifyCode->lpOFN->lCustData + ); + + switch ( pNotifyCode->hdr.code ) { - wx_reinterpret_cast(wxFileDialog *, - pNotifyCode->lpOFN->lCustData) - ->MSWOnInitDone((WXHWND)hDlg); - } + case CDN_INITDONE: + dialog->MSWOnInitDone((WXHWND)hDlg); + break; + + case CDN_SELCHANGE: + dialog->MSWOnSelChange((WXHWND)hDlg); + break; + } } break; @@ -146,8 +240,8 @@ void wxFileDialog::GetPaths(wxArrayString& paths) const paths.Empty(); wxString dir(m_dir); - if ( m_dir.Last() != _T('\\') ) - dir += _T('\\'); + if ( m_dir.empty() || m_dir.Last() != wxT('\\') ) + dir += wxT('\\'); size_t count = m_fileNames.GetCount(); for ( size_t n = 0; n < count; n++ ) @@ -164,14 +258,6 @@ void wxFileDialog::GetFilenames(wxArrayString& files) const files = m_fileNames; } -void wxFileDialog::SetPath(const wxString& path) -{ - wxString ext; - wxSplitPath(path, &m_dir, &m_fileName, &ext); - if ( !ext.empty() ) - m_fileName << _T('.') << ext; -} - void wxFileDialog::DoGetPosition(int *x, int *y) const { if ( x ) @@ -223,7 +309,7 @@ void wxFileDialog::DoCentre(int dir) void wxFileDialog::MSWOnInitDone(WXHWND hDlg) { - // note the the dialog is the parent window: hDlg is a child of it when + // note the dialog is the parent window: hDlg is a child of it when // OFN_EXPLORER is used HWND hFileDlg = ::GetParent((HWND)hDlg); @@ -246,10 +332,29 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg) SetPosition(gs_rectDialog.GetPosition()); } + // Call selection change handler so that update handler will be + // called once with no selection. + MSWOnSelChange(hDlg); + // we shouldn't destroy this HWND SetHWND(NULL); } +void wxFileDialog::MSWOnSelChange(WXHWND hDlg) +{ + TCHAR buf[MAX_PATH]; + LRESULT len = SendMessage(::GetParent(hDlg), CDM_GETFILEPATH, + MAX_PATH, reinterpret_cast(buf)); + + if ( len > 0 ) + m_currentlySelectedFilename = buf; + else + m_currentlySelectedFilename.clear(); + + if ( m_extraControl ) + m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE); +} + // helper used below in ShowCommFileDialog(): style is used to determine // whether to show the "Save file" dialog (if it contains wxFD_SAVE bit) or // "Open file" one; returns true on success or false on failure in which case @@ -341,7 +446,7 @@ static bool ShowCommFileDialog(OPENFILENAME *of, long style) { // this can happen if the default file name is invalid, try without it // now - of->lpstrFile[0] = _T('\0'); + of->lpstrFile[0] = wxT('\0'); success = DoShowCommFileDialog(of, style, &errCode); } @@ -360,8 +465,21 @@ static bool ShowCommFileDialog(OPENFILENAME *of, long style) return true; } +#ifndef __WXWINCE__ +void wxFileDialog::MSWOnInitDialogHook(WXHWND hwnd) +{ + SetHWND(hwnd); + + CreateExtraControl(); + + SetHWND(NULL); +} +#endif // __WXWINCE__ + int wxFileDialog::ShowModal() { + WX_HOOK_MODAL_DIALOG(); + HWND hWnd = 0; if (m_parent) hWnd = (HWND) m_parent->GetHWND(); if (!hWnd && wxTheApp->GetTopWindow()) @@ -385,14 +503,17 @@ int wxFileDialog::ShowModal() in the upper left of the frame, it does not center automatically. */ - if (m_bMovedWindow) // we need these flags. + if (m_bMovedWindow || HasExtraControlCreator()) // we need these flags. { + ChangeExceptionPolicy(); msw_flags |= OFN_EXPLORER|OFN_ENABLEHOOK; #ifndef __WXWINCE__ msw_flags |= OFN_ENABLESIZING; #endif } + wxON_BLOCK_EXIT0(RestoreExceptionPolicy); + if ( HasFdFlag(wxFD_MULTIPLE) ) { // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT @@ -417,10 +538,40 @@ int wxFileDialog::ShowModal() of.lStructSize = gs_ofStructSize; of.hwndOwner = hWnd; - of.lpstrTitle = m_message.wx_str(); + of.lpstrTitle = m_message.t_str(); of.lpstrFileTitle = titleBuffer; of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; +#ifndef __WXWINCE__ + GlobalPtr hgbl; + if ( HasExtraControlCreator() ) + { + msw_flags |= OFN_ENABLETEMPLATEHANDLE; + + hgbl.Init(256, GMEM_ZEROINIT); + GlobalPtrLock hgblLock(hgbl); + LPDLGTEMPLATE lpdt = static_cast(hgblLock.Get()); + + // Define a dialog box. + + lpdt->style = DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS; + lpdt->cdit = 0; // Number of controls + lpdt->x = 0; + lpdt->y = 0; + + // convert the size of the extra controls to the dialog units + const wxSize extraSize = GetExtraControlSize(); + const LONG baseUnits = ::GetDialogBaseUnits(); + lpdt->cx = ::MulDiv(extraSize.x, 4, LOWORD(baseUnits)); + lpdt->cy = ::MulDiv(extraSize.y, 8, HIWORD(baseUnits)); + + // after the DLGTEMPLATE there are 3 additional WORDs for dialog menu, + // class and title, all three set to zeros. + + of.hInstance = (HINSTANCE)lpdt; + } +#endif // __WXWINCE__ + // Convert forward slashes to backslashes (file selector doesn't like // forward slashes) and also squeeze multiple consecutive slashes into one // as it doesn't like two backslashes in a row neither @@ -433,17 +584,17 @@ int wxFileDialog::ShowModal() wxChar ch = m_dir[i]; switch ( ch ) { - case _T('/'): + case wxT('/'): // convert to backslash - ch = _T('\\'); + ch = wxT('\\'); // fall through - case _T('\\'): + case wxT('\\'): while ( i < len - 1 ) { wxChar chNext = m_dir[i + 1]; - if ( chNext != _T('\\') && chNext != _T('/') ) + if ( chNext != wxT('\\') && chNext != wxT('/') ) break; // ignore the next one, unless it is at the start of a UNC path @@ -470,7 +621,7 @@ int wxFileDialog::ShowModal() size_t items = wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters); - wxASSERT_MSG( items > 0 , _T("empty wildcard list") ); + wxASSERT_MSG( items > 0 , wxT("empty wildcard list") ); wxString filterBuffer; @@ -489,13 +640,12 @@ int wxFileDialog::ShowModal() } } - of.lpstrFilter = (LPTSTR)filterBuffer.wx_str(); + of.lpstrFilter = filterBuffer.t_str(); of.nFilterIndex = m_filterIndex + 1; //=== Setting defaultFileName >>========================================= - wxStrncpy(fileNameBuffer, m_fileName, wxMAXPATH-1); - fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0'); + wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer)); of.lpstrFile = fileNameBuffer; // holds returned filename of.nMaxFile = wxMAXPATH; @@ -507,7 +657,7 @@ int wxFileDialog::ShowModal() wxString defextBuffer; // we need it to be alive until GetSaveFileName()! if (HasFdFlag(wxFD_SAVE)) { - const wxChar* extension = filterBuffer.wx_str(); + const wxChar* extension = filterBuffer.t_str(); int maxFilter = (int)(of.nFilterIndex*2L) - 1; for( int i = 0; i < maxFilter; i++ ) // get extension @@ -563,7 +713,7 @@ int wxFileDialog::ShowModal() i += wxStrlen(&fileNameBuffer[i]) + 1; } #else - wxStringTokenizer toke(fileNameBuffer, _T(" \t\r\n")); + wxStringTokenizer toke(fileNameBuffer, wxT(" \t\r\n")); m_dir = toke.GetNextToken(); m_fileName = toke.GetNextToken(); m_fileNames.Add(m_fileName); @@ -573,8 +723,8 @@ int wxFileDialog::ShowModal() #endif // OFN_EXPLORER wxString dir(m_dir); - if ( m_dir.Last() != _T('\\') ) - dir += _T('\\'); + if ( m_dir.Last() != wxT('\\') ) + dir += wxT('\\'); m_path = dir + m_fileName; m_filterIndex = (int)of.nFilterIndex - 1; @@ -589,15 +739,14 @@ int wxFileDialog::ShowModal() (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) ) { // User has typed a filename without an extension: - const wxChar* extension = filterBuffer.wx_str(); + const wxChar* extension = filterBuffer.t_str(); int maxFilter = (int)(of.nFilterIndex*2L) - 1; for( int i = 0; i < maxFilter; i++ ) // get extension extension = extension + wxStrlen( extension ) + 1; m_fileName = AppendExtension(fileNameBuffer, extension); - wxStrncpy(fileNameBuffer, m_fileName.c_str(), wxMin(m_fileName.length(), wxMAXPATH-1)); - fileNameBuffer[wxMin(m_fileName.length(), wxMAXPATH-1)] = wxT('\0'); + wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer)); } m_path = fileNameBuffer;