From 41ec486dd0f51e75d7a03342c24204d5665696ac Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 14 Dec 2020 13:26:49 +0000 Subject: [PATCH] FileChooser: Show desktop folder if requested folder does not exist This change affects the PostVista version of the Windows FileChooser. --- .../native/juce_win32_FileChooser.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp index a1a554ef9c..8a4b34f64e 100644 --- a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp +++ b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp @@ -167,6 +167,11 @@ private: Atomic nativeDialogRef; Atomic shouldCancel; + struct FreeLPWSTR + { + void operator() (LPWSTR ptr) const noexcept { CoTaskMemFree (ptr); } + }; + bool showDialog (IFileDialog& dialog, bool async) const { FILEOPENDIALOGOPTIONS flags = {}; @@ -193,7 +198,17 @@ private: PIDLIST_ABSOLUTE pidl = {}; if (FAILED (SHParseDisplayName (initialPath.toWideCharPointer(), nullptr, &pidl, SFGAO_FOLDER, nullptr))) - return false; + { + LPWSTR ptr = nullptr; + auto result = SHGetKnownFolderPath (FOLDERID_Desktop, 0, nullptr, &ptr); + std::unique_ptr desktopPath (ptr); + + if (FAILED (result)) + return false; + + if (FAILED (SHParseDisplayName (desktopPath.get(), nullptr, &pidl, SFGAO_FOLDER, nullptr))) + return false; + } const auto item = [&] { @@ -228,17 +243,12 @@ private: { const auto getUrl = [] (IShellItem& item) { - struct Free - { - void operator() (LPWSTR ptr) const noexcept { CoTaskMemFree (ptr); } - }; - LPWSTR ptr = nullptr; if (item.GetDisplayName (SIGDN_FILESYSPATH, &ptr) != S_OK) return URL(); - const auto path = std::unique_ptr { ptr }; + const auto path = std::unique_ptr { ptr }; return URL (File (String (path.get()))); };