Skip to content

Commit

Permalink
Windows: Add NFD_OVERRIDE_RECENT_WITH_DEFAULT build option (#152)
Browse files Browse the repository at this point in the history
This build option makes the Windows backend call `IFileDialog::SetFolder` instead of `IFileDialog::SetDefaultFolder` on Windows.
  • Loading branch information
dbierek authored Oct 3, 2024
1 parent 79ebd33 commit 388549a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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.
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.

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

Expand Down Expand Up @@ -223,7 +223,7 @@ typedef struct {
```

- `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.
- `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).
- `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).
- `defaultName`: (For SaveDialog only) Set this to the file name that should be pre-filled on the dialog.
- `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.

Expand Down Expand Up @@ -257,7 +257,7 @@ A wildcard filter is always added to every dialog.
*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".*
*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.*
*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.*
## Iterating Over PathSets
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ if (NFD_INSTALL)
FILE ${TARGET_NAME}-config.cmake
)
endif()

option(NFD_OVERRIDE_RECENT_WITH_DEFAULT "Use defaultPath instead of recent folder on Windows" OFF)
if (NFD_OVERRIDE_RECENT_WITH_DEFAULT)
target_compile_definitions(${TARGET_NAME} PRIVATE NFD_OVERRIDE_RECENT_WITH_DEFAULT)
endif()
13 changes: 10 additions & 3 deletions src/nfd_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,20 @@ nfdresult_t SetDefaultPath(IFileDialog* dialog, const nfdnchar_t* defaultPath) {

Release_Guard<IShellItem> folderGuard(folder);

// SetDefaultFolder() might use another recently used folder if available, so the user doesn't
// need to keep navigating back to the default folder (recommended by Windows). change to
// SetFolder() if you always want to use the default folder
#ifdef NFD_OVERRIDE_RECENT_WITH_DEFAULT
// Use SetFolder() if you always want to use the default folder
if (!SUCCEEDED(dialog->SetFolder(folder))) {
NFDi_SetError("Failed to set default path.");
return NFD_ERROR;
}
#else
// SetDefaultFolder() might use another recently used folder if available, so the user
// doesn't need to keep navigating back to the default folder (recommended by Windows).
if (!SUCCEEDED(dialog->SetDefaultFolder(folder))) {
NFDi_SetError("Failed to set default path.");
return NFD_ERROR;
}
#endif

return NFD_OKAY;
}
Expand Down

0 comments on commit 388549a

Please sign in to comment.