diff --git a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp index 80a82ebaeb..8eb19721cc 100644 --- a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp +++ b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp @@ -245,10 +245,42 @@ private: JUCE_COMRESULT OnFolderChanging (IFileDialog* d, IShellItem*) override { return updateHwnd (d); } JUCE_COMRESULT OnFileOk (IFileDialog* d) override { return updateHwnd (d); } JUCE_COMRESULT OnFolderChange (IFileDialog* d) override { return updateHwnd (d); } - JUCE_COMRESULT OnSelectionChange (IFileDialog* d) override { return updateHwnd (d); } + JUCE_COMRESULT OnSelectionChange (IFileDialog* d) override { focusWorkaround(); return updateHwnd (d); } JUCE_COMRESULT OnShareViolation (IFileDialog* d, IShellItem*, FDE_SHAREVIOLATION_RESPONSE*) override { return updateHwnd (d); } JUCE_COMRESULT OnOverwrite (IFileDialog* d, IShellItem*, FDE_OVERWRITE_RESPONSE*) override { return updateHwnd (d); } + /** Workaround for a bug in Vista+, OpenFileDialog will truncate the initialFile text. + Moving the caret along the full length of the text and back will reveal the full string. + */ + void focusWorkaround() + { + if (! defaultFileNameTextCaretMoved) + { + auto makeKeyInput = [] (WORD vk, bool pressed) + { + INPUT i; + + ZeroMemory (&i, sizeof (INPUT)); + + i.type = INPUT_KEYBOARD; + i.ki.wVk = vk; + i.ki.dwFlags = pressed ? 0 : KEYEVENTF_KEYUP; + + return i; + }; + + INPUT inputs[] = { + makeKeyInput (VK_HOME, true), + makeKeyInput (VK_HOME, false), + makeKeyInput (VK_END, true), + makeKeyInput (VK_END, false), + }; + + SendInput ((UINT) std::size (inputs), inputs, sizeof (INPUT)); + defaultFileNameTextCaretMoved = true; + } + } + JUCE_COMRESULT updateHwnd (IFileDialog* d) { HWND hwnd = nullptr; @@ -266,6 +298,7 @@ private: return S_OK; } + bool defaultFileNameTextCaretMoved = false; Win32NativeFileChooser& owner; };