From 580ffdf4c03b47e37aaede43985ebb3a5b399162 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 15 Apr 2012 23:18:12 +0000 Subject: [PATCH] Use new style directory selection dialog under Vista and later. Use IFileDialog in wxDirDialog implementation if possible, it shows a much more user-friendly dialog than the one used by SHBrowseForFolder(). Closes #11401. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71201 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/msw/dirdlg.h | 5 + src/msw/dirdlg.cpp | 266 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 266 insertions(+), 6 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 06c51e1538..4060433d9f 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -516,6 +516,7 @@ GTK: MSW: - Fixed regression with initial focus in the dialogs in 2.9.3. +- Use new style directory selection dialog under Vista and later (PB). - Added support for wxEXEC_MAKE_GROUP_LEADER to wxExecute (tteras). - Set wxMenu being closed in wxEVT_MENU_CLOSE events (Marcin Malich). - Fix coordinates and Z-position for joystick events (Markus Juergens). diff --git a/include/wx/msw/dirdlg.h b/include/wx/msw/dirdlg.h index 9c6be1fef5..d8fbd9bc7b 100644 --- a/include/wx/msw/dirdlg.h +++ b/include/wx/msw/dirdlg.h @@ -28,6 +28,11 @@ public: virtual int ShowModal(); private: + // The real implementations of ShowModal(), used for Windows versions + // before and since Vista. + int ShowSHBrowseForFolder(WXHWND owner); + int ShowIFileDialog(WXHWND owner); + DECLARE_DYNAMIC_CLASS_NO_COPY(wxDirDialog) }; diff --git a/src/msw/dirdlg.cpp b/src/msw/dirdlg.cpp index 9d3b472058..cdc1022464 100644 --- a/src/msw/dirdlg.cpp +++ b/src/msw/dirdlg.cpp @@ -40,6 +40,105 @@ #include "wx/msw/private.h" #include "wx/msw/wrapshl.h" +#include "wx/msw/private/comptr.h" +#include "wx/dynlib.h" + +#include + +// We can only use IFileDialog under desktop Windows and we need +// wxDynamicLibrary for it. +#if wxUSE_DYNLIB_CLASS && !defined(__WXWINCE__) + #define wxUSE_IFILEDIALOG 1 +#else + #define wxUSE_IFILEDIALOG +#endif + +#if wxUSE_IFILEDIALOG +// IFileDialog related declarations missing from some compilers headers. + +// IShellItem +#ifndef __IShellItem_INTERFACE_DEFINED__ + +#ifndef SIGDN_FILESYSPATH + #define SIGDN_FILESYSPATH 0x80058000 +#endif + +struct IShellItem : public IUnknown +{ + virtual HRESULT wxSTDCALL BindToHandler(IBindCtx*, REFGUID, REFIID, void**) = 0; + virtual HRESULT wxSTDCALL GetParent(IShellItem**) = 0; + virtual HRESULT wxSTDCALL GetDisplayName(DWORD, LPWSTR*) = 0; + virtual HRESULT wxSTDCALL GetAttributes(ULONG, ULONG*) = 0; + virtual HRESULT wxSTDCALL Compare(IShellItem*, DWORD, int*) = 0; +}; + +DEFINE_GUID(IID_IShellItem, + 0x43826D1E, 0xE718, 0x42EE, 0xBC, 0x55, 0xA1, 0xE2, 0x61, 0xC3, 0x7B, 0xFE); + +#endif // #ifndef __IShellItem_INTERFACE_DEFINED__ + +struct IShellItemFilter; +struct IFileDialogEvents; + +// IModalWindow +#ifndef __IModalWindow_INTERFACE_DEFINED__ + +struct IModalWindow : public IUnknown +{ + virtual HRESULT wxSTDCALL Show(HWND) = 0; +}; + +#endif // #ifndef __IModalWindow_INTERFACE_DEFINED__ + +// IFileDialog +#ifndef __IFileDialog_INTERFACE_DEFINED__ + +#ifndef FOS_PICKFOLDERS + #define FOS_PICKFOLDERS 0x20 +#endif + +#ifndef FOS_FORCEFILESYSTEM + #define FOS_FORCEFILESYSTEM 0x40 +#endif + +struct COMDLG_FILTERSPEC; + +struct IFileDialog : public IModalWindow +{ + virtual HRESULT wxSTDCALL SetFileTypes(UINT, const COMDLG_FILTERSPEC*) = 0; + virtual HRESULT wxSTDCALL SetFileTypeIndex(UINT) = 0; + virtual HRESULT wxSTDCALL GetFileTypeIndex(UINT*) = 0; + virtual HRESULT wxSTDCALL Advise(IFileDialogEvents*, DWORD*) = 0; + virtual HRESULT wxSTDCALL Unadvise(DWORD) = 0; + virtual HRESULT wxSTDCALL SetOptions(DWORD) = 0; + virtual HRESULT wxSTDCALL GetOptions(DWORD*) = 0; + virtual HRESULT wxSTDCALL SetDefaultFolder(IShellItem*) = 0; + virtual HRESULT wxSTDCALL SetFolder(IShellItem*) = 0; + virtual HRESULT wxSTDCALL GetFolder(IShellItem**) = 0; + virtual HRESULT wxSTDCALL GetCurrentSelection(IShellItem**) = 0; + virtual HRESULT wxSTDCALL SetFileName(LPCWSTR) = 0; + virtual HRESULT wxSTDCALL GetFileName(LPWSTR*) = 0; + virtual HRESULT wxSTDCALL SetTitle(LPCWSTR) = 0; + virtual HRESULT wxSTDCALL SetOkButtonLabel(LPCWSTR) = 0; + virtual HRESULT wxSTDCALL SetFileNameLabel(LPCWSTR) = 0; + virtual HRESULT wxSTDCALL GetResult(IShellItem**) = 0; + virtual HRESULT wxSTDCALL AddPlace(IShellItem*, DWORD) = 0; + virtual HRESULT wxSTDCALL SetDefaultExtension(LPCWSTR) = 0; + virtual HRESULT wxSTDCALL Close(HRESULT) = 0; + virtual HRESULT wxSTDCALL SetClientGuid(REFGUID) = 0; + virtual HRESULT wxSTDCALL ClearClientData() = 0; + virtual HRESULT wxSTDCALL SetFilter(IShellItemFilter*) = 0; +}; + +DEFINE_GUID(CLSID_FileOpenDialog, + 0xDC1C5A9C, 0xE88A, 0x4dde, 0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7); + +DEFINE_GUID(IID_IFileDialog, + 0x42F85136, 0xDB7E, 0x439C, 0x85, 0xF1, 0xE4, 0x07, 0x5D, 0x13, 0x5F, 0xC8); + +#endif // #ifndef __IFileDialog_INTERFACE_DEFINED__ + +#endif // wxUSE_IFILEDIALOG // ---------------------------------------------------------------------------- // constants @@ -117,10 +216,38 @@ void wxDirDialog::SetPath(const wxString& path) int wxDirDialog::ShowModal() { - wxWindow *parent = GetParent(); + wxWindow* const parent = GetParent(); + WXHWND hWndParent = parent ? GetHwndOf(parent) : NULL; + + // Use IFileDialog under new enough Windows, it's more user-friendly. + int rc; +#if wxUSE_IFILEDIALOG + if ( wxGetWinVersion() >= wxWinVersion_Vista ) + { + rc = ShowIFileDialog(hWndParent); + } + else + { + rc = wxID_NONE; + } + + if ( rc == wxID_NONE ) +#endif // wxUSE_IFILEDIALOG + { + rc = ShowSHBrowseForFolder(hWndParent); + } + + // change current working directory if asked so + if ( rc == wxID_OK && HasFlag(wxDD_CHANGE_DIR) ) + wxSetWorkingDirectory(m_path); + return rc; +} + +int wxDirDialog::ShowSHBrowseForFolder(WXHWND owner) +{ BROWSEINFO bi; - bi.hwndOwner = parent ? GetHwndOf(parent) : NULL; + bi.hwndOwner = owner; bi.pidlRoot = NULL; bi.pszDisplayName = NULL; // Please don't change this without checking it compiles @@ -181,13 +308,140 @@ int wxDirDialog::ShowModal() m_path = pidl.GetPath(); - // change current working directory if asked so - if (HasFlag(wxDD_CHANGE_DIR)) - wxSetWorkingDirectory(m_path); - return m_path.empty() ? wxID_CANCEL : wxID_OK; } +// Function for obtaining folder name on Vista and newer. +// +// Returns wxID_OK on success, wxID_CANCEL if cancelled by user or wxID_NONE if +// an error occurred and we should fall back onto the old dialog. +#if wxUSE_IFILEDIALOG + +int wxDirDialog::ShowIFileDialog(WXHWND owner) +{ + HRESULT hr; + wxCOMPtr fileDialog; + + hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, + wxIID_PPV_ARGS(IFileDialog, &fileDialog)); + if ( FAILED(hr) ) + { + wxLogApiError(wxS("CoCreateInstance(CLSID_FileOpenDialog)"), hr); + return wxID_NONE; + } + + // allow user to select only a file system folder + hr = fileDialog->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM); + if ( FAILED(hr) ) + { + wxLogApiError(wxS("IFileDialog::SetOptions"), hr); + return wxID_NONE; + } + + hr = fileDialog->SetTitle(m_message.wc_str()); + if ( FAILED(hr) ) + { + // This error is not serious, let's just log it and continue even + // without the title set. + wxLogApiError(wxS("IFileDialog::SetTitle"), hr); + } + + // set the initial path + if ( !m_path.empty() ) + { + // We need to link SHCreateItemFromParsingName() dynamically as it's + // not available on pre-Vista systems. + typedef HRESULT + (WINAPI *SHCreateItemFromParsingName_t)(PCWSTR, + IBindCtx*, + REFIID, + void**); + + SHCreateItemFromParsingName_t s_pfnSHCreateItemFromParsingName = NULL; + wxDynamicLibrary dllShell32; + if ( dllShell32.Load(wxS("shell32.dll"), wxDL_VERBATIM | wxDL_QUIET) ) + { + wxDL_INIT_FUNC(s_pfn, SHCreateItemFromParsingName, dllShell32); + } + + if ( !s_pfnSHCreateItemFromParsingName ) + { + wxLogLastError(wxS("SHCreateItemFromParsingName() not found")); + return wxID_NONE; + } + + wxCOMPtr folder; + hr = s_pfnSHCreateItemFromParsingName(m_path.wc_str(), + NULL, + wxIID_PPV_ARGS(IShellItem, + &folder)); + if ( FAILED(hr) ) + { + wxLogApiError(wxS("SHCreateItemFromParsingName"), hr); + return wxID_NONE; + } + + hr = fileDialog->SetFolder(folder); + if ( FAILED(hr) ) + { + wxLogApiError(wxS("IFileDialog::SetFolder"), hr); + return wxID_NONE; + } + } + + + wxString path; + + hr = fileDialog->Show(owner); + if ( SUCCEEDED(hr) ) + { + wxCOMPtr folder; + + hr = fileDialog->GetResult(&folder); + if ( SUCCEEDED(hr) ) + { + LPOLESTR pathOLE = NULL; + + hr = folder->GetDisplayName(SIGDN_FILESYSPATH, &pathOLE); + if ( SUCCEEDED(hr) ) + { + path = pathOLE; + CoTaskMemFree(pathOLE); + } + else + { + wxLogApiError(wxS("IShellItem::GetDisplayName"), hr); + } + } + else + { + wxLogApiError(wxS("IFileDialog::GetResult"), hr); + } + } + else if ( hr == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) + { + return wxID_CANCEL; // the user cancelled the dialog + } + else + { + wxLogApiError(wxS("IFileDialog::Show"), hr); + } + + if ( path.empty() ) + { + // the user didn't cancel the dialog and yet the path is empty + // it means there was an error, already logged by wxLogApiError() + // now report the error to the user and return + wxLogSysError(_("Couldn't obtain folder name"), hr); + return wxID_CANCEL; + } + + m_path = path; + return wxID_OK; +} + +#endif // wxUSE_IFILEDIALOG + // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- -- 2.45.2