Skip to content

Commit 388549a

Browse files
authored
Windows: Add NFD_OVERRIDE_RECENT_WITH_DEFAULT build option (#152)
This build option makes the Windows backend call `IFileDialog::SetFolder` instead of `IFileDialog::SetDefaultFolder` on Windows.
1 parent 79ebd33 commit 388549a

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
![GitHub Actions](https://github.com/btzy/nativefiledialog-extended/workflows/build/badge.svg)
55

6-
A small C library with that portably invokes native file open, folder select and file save dialogs. Write dialog code once and have it pop up native dialogs on all supported platforms. Avoid linking large dependencies like wxWidgets and Qt.
6+
A small C library that portably invokes native file open, folder select and file save dialogs. Write dialog code once and have it pop up native dialogs on all supported platforms. Avoid linking large dependencies like wxWidgets and Qt.
77

88
This library is based on Michael Labbe's Native File Dialog ([mlabbe/nativefiledialog](https://github.com/mlabbe/nativefiledialog)).
99

@@ -223,7 +223,7 @@ typedef struct {
223223
```
224224

225225
- `filterList` and `filterCount`: Set these to customize the file filter (it appears as a dropdown menu on Windows and Linux, but simply hides files on macOS). Set `filterList` to a pointer to the start of the array of filter items and `filterCount` to the number of filter items in that array. See the "File Filter Syntax" section below for details.
226-
- `defaultPath`: Set this to the default folder that the dialog should open to (on Windows, if there is a recently used folder, it opens to that folder instead of the folder you pass).
226+
- `defaultPath`: Set this to the default folder that the dialog should open to (on Windows, if there is a recently used folder, it opens to that folder instead of the folder you pass, unless the `NFD_OVERRIDE_RECENT_WITH_DEFAULT` build option is set to ON).
227227
- `defaultName`: (For SaveDialog only) Set this to the file name that should be pre-filled on the dialog.
228228
- `parentWindow`: Set this to the native window handle of the parent of this dialog. See the "Usage with a Platform Abstraction Framework" section for details. It is also possible to pass a handle even if you do not use a platform abstraction framework.
229229

@@ -257,7 +257,7 @@ A wildcard filter is always added to every dialog.
257257
258258
*Note 3: On Linux, the file extension is appended (if missing) when the user presses down the "Save" button. The appended file extension will remain visible to the user, even if an overwrite prompt is shown and the user then presses "Cancel".*
259259
260-
*Note 4: On Windows, the default folder parameter is only used if there is no recently used folder available. Otherwise, the default folder will be the folder that was last used. Internally, the Windows implementation calls [IFileDialog::SetDefaultFolder(IShellItem)](https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setdefaultfolder). This is usual Windows behaviour and users expect it.*
260+
*Note 4: On Windows, the default folder parameter is only used if there is no recently used folder available, unless the `NFD_OVERRIDE_RECENT_WITH_DEFAULT` build option is set to ON. Otherwise, the default folder will be the folder that was last used. Internally, the Windows implementation calls [IFileDialog::SetDefaultFolder(IShellItem)](https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setdefaultfolder). This is usual Windows behaviour and users expect it.*
261261
262262
## Iterating Over PathSets
263263

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ if (NFD_INSTALL)
138138
FILE ${TARGET_NAME}-config.cmake
139139
)
140140
endif()
141+
142+
option(NFD_OVERRIDE_RECENT_WITH_DEFAULT "Use defaultPath instead of recent folder on Windows" OFF)
143+
if (NFD_OVERRIDE_RECENT_WITH_DEFAULT)
144+
target_compile_definitions(${TARGET_NAME} PRIVATE NFD_OVERRIDE_RECENT_WITH_DEFAULT)
145+
endif()

src/nfd_win.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,20 @@ nfdresult_t SetDefaultPath(IFileDialog* dialog, const nfdnchar_t* defaultPath) {
250250

251251
Release_Guard<IShellItem> folderGuard(folder);
252252

253-
// SetDefaultFolder() might use another recently used folder if available, so the user doesn't
254-
// need to keep navigating back to the default folder (recommended by Windows). change to
255-
// SetFolder() if you always want to use the default folder
253+
#ifdef NFD_OVERRIDE_RECENT_WITH_DEFAULT
254+
// Use SetFolder() if you always want to use the default folder
255+
if (!SUCCEEDED(dialog->SetFolder(folder))) {
256+
NFDi_SetError("Failed to set default path.");
257+
return NFD_ERROR;
258+
}
259+
#else
260+
// SetDefaultFolder() might use another recently used folder if available, so the user
261+
// doesn't need to keep navigating back to the default folder (recommended by Windows).
256262
if (!SUCCEEDED(dialog->SetDefaultFolder(folder))) {
257263
NFDi_SetError("Failed to set default path.");
258264
return NFD_ERROR;
259265
}
266+
#endif
260267

261268
return NFD_OKAY;
262269
}

0 commit comments

Comments
 (0)