diff --git a/README.md b/README.md index 218eacd..d1a1ffe 100644 --- a/README.md +++ b/README.md @@ -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)). @@ -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. @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 740b484..3dcd160 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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() diff --git a/src/nfd_gtk.cpp b/src/nfd_gtk.cpp index 86aa797..bdb6292 100644 --- a/src/nfd_gtk.cpp +++ b/src/nfd_gtk.cpp @@ -447,8 +447,7 @@ void RealizedSignalHandler(GtkWidget* window, void* userdata) { } struct NativeWindowParenter { - NativeWindowParenter(GtkWidget* widget, const nfdwindowhandle_t& parentWindow) noexcept - : widget(widget) { + NativeWindowParenter(GtkWidget* w, const nfdwindowhandle_t& parentWindow) noexcept : widget(w) { parent = GetAllocNativeWindowHandle(parentWindow); if (parent) { diff --git a/src/nfd_portal.cpp b/src/nfd_portal.cpp index 77fd5d5..684c502 100644 --- a/src/nfd_portal.cpp +++ b/src/nfd_portal.cpp @@ -1605,17 +1605,17 @@ nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version, (void)version; { - dbus_uint32_t version; - const nfdresult_t res = NFD_DBus_GetVersion(version); + dbus_uint32_t portal_version; + const nfdresult_t res = NFD_DBus_GetVersion(portal_version); if (res != NFD_OKAY) { return res; } - if (version < 3) { + if (portal_version < 3) { NFDi_SetFormattedError( "The xdg-desktop-portal installed on this system does not support a folder picker; " "at least version 3 of the org.freedesktop.portal.FileChooser interface is " "required but the installed interface version is %u.", - version); + portal_version); return NFD_ERROR; } } @@ -1662,17 +1662,17 @@ nfdresult_t NFD_PickFolderMultipleN_With_Impl(nfdversion_t version, (void)version; { - dbus_uint32_t version; - const nfdresult_t res = NFD_DBus_GetVersion(version); + dbus_uint32_t portal_version; + const nfdresult_t res = NFD_DBus_GetVersion(portal_version); if (res != NFD_OKAY) { return res; } - if (version < 3) { + if (portal_version < 3) { NFDi_SetFormattedError( "The xdg-desktop-portal installed on this system does not support a folder picker; " "at least version 3 of the org.freedesktop.portal.FileChooser interface is " "required but the installed interface version is %u.", - version); + portal_version); return NFD_ERROR; } } diff --git a/src/nfd_win.cpp b/src/nfd_win.cpp index 4bf07e2..0170b02 100644 --- a/src/nfd_win.cpp +++ b/src/nfd_win.cpp @@ -250,13 +250,20 @@ nfdresult_t SetDefaultPath(IFileDialog* dialog, const nfdnchar_t* defaultPath) { Release_Guard 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; }