BR: FileChooser::browseForFileToSave does not set default extension post Vista

In the FileChooser pre-Vista, if you browse to save a file it adds the extension if the user just types a name. Post Vista this doesn’t happen. The developer can manually add the extension afterwards, however it’s still bad UX since if the file already exists with the extension the overwrite prompt won’t happen.

I reused the code from the pre-vista dialog and added it to post-vista dialog:

diff --git a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp
index 220c0f5aa..bbd132eeb 100644
--- a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp
+++ b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp
@@ -210,7 +210,18 @@ private:
         if (FAILED (dialog.SetFileName (filename.toWideCharPointer())))
             return false;

-        const auto extension = filename.fromLastOccurrenceOf (".", false, false);
+        auto extension = filename.fromLastOccurrenceOf (".", false, false);
+        
+        if (isSave && extension.isEmpty())
+        {
+            StringArray tokens;
+            tokens.addTokens (filtersString, ";,", "\"'");
+            tokens.trim();
+            tokens.removeEmptyStrings();
+
+            if (tokens.size() == 1 && tokens[0].removeCharacters ("*.").isNotEmpty())
+                extension = tokens[0].fromFirstOccurrenceOf (".", false, false);
+        }

         if (extension.isNotEmpty() && FAILED (dialog.SetDefaultExtension (extension.toWideCharPointer())))
             return false;

Thanks for the report. This should be fixed up on develop now.

1 Like