Skip to content

Commit

Permalink
Add overrideRecentWithDefault flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dbierek committed Sep 20, 2024
1 parent 79ebd33 commit a5daec7
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 124 deletions.
10 changes: 7 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 @@ -200,6 +200,7 @@ typedef struct {
nfdfiltersize_t filterCount;
const nfdu8char_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdopendialogu8args_t;
```

Expand All @@ -211,6 +212,7 @@ typedef struct {
const nfdu8char_t* defaultPath;
const nfdu8char_t* defaultName;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdsavedialogu8args_t;
```

Expand All @@ -219,13 +221,15 @@ typedef struct {
typedef struct {
const nfdu8char_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdpickfolderu8args_t;
```

- `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 `overrideRecentWithDefault `value is set to 1).
- `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.
- `overrideRecentWithDefault`: Set this to 1 to use the `defaultPath` instead of the most recent folder on Windows.

## Examples

Expand Down Expand Up @@ -257,7 +261,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 overrideRecentWithDefault is set to 1. 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
68 changes: 48 additions & 20 deletions src/include/nfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ typedef struct {
nfdfiltersize_t filterCount;
const nfdu8char_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdopendialogu8args_t;

#ifdef _WIN32
Expand All @@ -129,6 +130,7 @@ typedef struct {
nfdfiltersize_t filterCount;
const nfdnchar_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdopendialognargs_t;
#else
typedef nfdopendialogu8args_t nfdopendialognargs_t;
Expand All @@ -140,6 +142,7 @@ typedef struct {
const nfdu8char_t* defaultPath;
const nfdu8char_t* defaultName;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdsavedialogu8args_t;

#ifdef _WIN32
Expand All @@ -149,6 +152,7 @@ typedef struct {
const nfdnchar_t* defaultPath;
const nfdnchar_t* defaultName;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdsavedialognargs_t;
#else
typedef nfdsavedialogu8args_t nfdsavedialognargs_t;
Expand All @@ -157,12 +161,14 @@ typedef nfdsavedialogu8args_t nfdsavedialognargs_t;
typedef struct {
const nfdu8char_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdpickfolderu8args_t;

#ifdef _WIN32
typedef struct {
const nfdnchar_t* defaultPath;
nfdwindowhandle_t parentWindow;
int overrideRecentWithDefault;
} nfdpickfoldernargs_t;
#else
typedef nfdpickfolderu8args_t nfdpickfoldernargs_t;
Expand Down Expand Up @@ -196,23 +202,27 @@ NFD_API void NFD_Quit(void);
* NFD_OKAY.
* @param[out] outPath
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_OpenDialogN(nfdnchar_t** outPath,
const nfdnfilteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdnchar_t* defaultPath);
const nfdnchar_t* defaultPath,
int overrideRecentWithDefault);

/** Single file open dialog
*
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
* returns NFD_OKAY.
* @param[out] outPath
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_OpenDialogU8(nfdu8char_t** outPath,
const nfdu8filteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdu8char_t* defaultPath);
const nfdu8char_t* defaultPath,
int overrideRecentWithDefault);

/** This function is a library implementation detail. Please use NFD_OpenDialogN_With() instead. */
NFD_API nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,
Expand Down Expand Up @@ -249,23 +259,27 @@ NFD_INLINE nfdresult_t NFD_OpenDialogU8_With(nfdu8char_t** outPath,
* returns NFD_OKAY.
* @param[out] outPaths
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_OpenDialogMultipleN(const nfdpathset_t** outPaths,
const nfdnfilteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdnchar_t* defaultPath);
const nfdnchar_t* defaultPath,
int overrideRecentWithDefault);

/** Multiple file open dialog
*
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeU8() if this function
* returns NFD_OKAY.
* @param[out] outPaths
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_OpenDialogMultipleU8(const nfdpathset_t** outPaths,
const nfdu8filteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdu8char_t* defaultPath);
const nfdu8char_t* defaultPath,
int overrideRecentWithDefault);

/** This function is a library implementation detail. Please use NFD_OpenDialogMultipleN_With()
* instead. */
Expand Down Expand Up @@ -303,25 +317,29 @@ NFD_INLINE nfdresult_t NFD_OpenDialogMultipleU8_With(const nfdpathset_t** outPat
* NFD_OKAY.
* @param[out] outPath
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_SaveDialogN(nfdnchar_t** outPath,
const nfdnfilteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdnchar_t* defaultPath,
const nfdnchar_t* defaultName);
const nfdnchar_t* defaultName,
int overrideRecentWithDefault);

/** Save dialog
*
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
* returns NFD_OKAY.
* @param[out] outPath
* @param filterCount If zero, filterList is ignored (you can use null).
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_SaveDialogU8(nfdu8char_t** outPath,
const nfdu8filteritem_t* filterList,
nfdfiltersize_t filterCount,
const nfdu8char_t* defaultPath,
const nfdu8char_t* defaultName);
const nfdu8char_t* defaultName,
int overrideRecentWithDefault);

/** This function is a library implementation detail. Please use NFD_SaveDialogN_With() instead. */
NFD_API nfdresult_t NFD_SaveDialogN_With_Impl(nfdversion_t version,
Expand Down Expand Up @@ -357,16 +375,22 @@ NFD_INLINE nfdresult_t NFD_SaveDialogU8_With(nfdu8char_t** outPath,
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function returns
* NFD_OKAY.
* @param[out] outPath
* @param defaultPath If null, the operating system will decide. */
NFD_API nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath);
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath,
const nfdnchar_t* defaultPath,
int overrideRecentWithDefault);

/** Select single folder dialog
*
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
* returns NFD_OKAY.
* @param[out] outPath
* @param defaultPath If null, the operating system will decide. */
NFD_API nfdresult_t NFD_PickFolderU8(nfdu8char_t** outPath, const nfdu8char_t* defaultPath);
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_PickFolderU8(nfdu8char_t** outPath,
const nfdu8char_t* defaultPath,
int overrideRecentWithDefault);

/** This function is a library implementation detail. Please use NFD_PickFolderN_With() instead. */
NFD_API nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version,
Expand Down Expand Up @@ -402,18 +426,22 @@ NFD_INLINE nfdresult_t NFD_PickFolderU8_With(nfdu8char_t** outPath,
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeN() if this function
* returns NFD_OKAY.
* @param[out] outPaths
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_PickFolderMultipleN(const nfdpathset_t** outPaths,
const nfdnchar_t* defaultPath);
const nfdnchar_t* defaultPath,
int overrideRecentWithDefault);

/** Select multiple folder dialog
*
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeU8() if this function
* returns NFD_OKAY.
* @param[out] outPaths
* @param defaultPath If null, the operating system will decide. */
* @param defaultPath If null, the operating system will decide.
* @param overrideRecentWithDefault If 1, use defaultPath instead of the recent path on Windows. */
NFD_API nfdresult_t NFD_PickFolderMultipleU8(const nfdpathset_t** outPaths,
const nfdu8char_t* defaultPath);
const nfdu8char_t* defaultPath,
int overrideRecentWithDefault);

/** This function is a library implementation detail. Please use NFD_PickFolderMultipleN_With()
* instead. */
Expand Down
Loading

0 comments on commit a5daec7

Please sign in to comment.