Class NativeFileDialog
Usage
File Filter Syntax
Files can be filtered by file extension groups:
nfdfilteritem_t filterItem[2] = { { "Source code", "c,cpp,cc" }, { "Headers", "h,hpp" } };
A file filter is a pair of strings comprising the friendly name and the specification (multiple file extensions are comma-separated).
A list of file filters can be passed as an argument when invoking the library.
A wildcard filter is always added to every dialog.
Note: On MacOS, the file dialogs do not have friendly names and there is no way to switch between filters, so the filter specifications are combined (e.g. "c,cpp,cc,h,hpp"). The filter specification is also never explicitly shown to the user. This is usual MacOS behaviour and users expect it.
Note 2: You must ensure that the specification string is non-empty and that every file extension has at least one character. Otherwise, bad things might ensue (i.e. undefined behaviour).
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)
. This is usual Windows
behaviour and users expect it.
Iterating Over PathSets
A file open dialog that supports multiple selection produces a PathSet
, which is a thin abstraction over the platform-specific collection.
There are two ways to iterate over a PathSet
:
Accessing by index
This method does array-like access on the PathSet
, and is the easiest to use. However, on certain platforms (Linux, and possibly Windows), it
takes O(N2) time in total to iterate the entire PathSet
, because the underlying platform-specific implementation uses a linked list.
Using an enumerator
This method uses an enumerator object to iterate the paths in the PathSet
. It is guaranteed to take O(N) time in total to iterate the entire
PathSet
.
Usage with a Platform Abstraction Framework
NFDe is known to work with SDL2 and GLFW, and should also work with other platform abstraction framworks. However, you should initialize NFDe after
initializing the framework, and probably should deinitialize NFDe before deinitializing the framework. This is because some frameworks expect to be
initialized on a "clean slate", and they may configure the system in a different way from NFDe. Init
is generally very careful not to disrupt the
existing configuration unless necessary, and Quit
restores the configuration back exactly to what it was before initialization.
Known Limitations
- No support for Windows XP's legacy dialogs such as
GetOpenFileName
. - GTK dialogs don't set the existing window as parent, so if users click the existing window while the dialog is open then the dialog will go behind
it. GTK writes a warning to
stdout
orstderr
about this. - This library does not explicitly dispatch calls to the UI thread. This may lead to crashes if you call functions from other threads when the platform does not support it (e.g. MacOS). Users are generally expected to call NFDe from an appropriate UI thread (i.e. the thread performing the UI event loop).
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Result values.static final int
Result values.static final int
Result values. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Clear the errorstatic void
NFD_FreePath
(long filePath) Free a file path that was returned by the dialogs.static void
NFD_FreePath
(ByteBuffer filePath) Free a file path that was returned by the dialogs.static String
Get last error -- set whennfdresult_t
returnsERROR
.static int
NFD_Init()
Initialize NFD - call this for every thread that might use NFD, before calling any other NFD functions on that thread.static int
NFD_OpenDialog
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, CharSequence defaultPath) Single file open dialog.static int
NFD_OpenDialog
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, ByteBuffer defaultPath) Single file open dialog.static int
NFD_OpenDialogMultiple
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, CharSequence defaultPath) Multiple file open dialog.static int
NFD_OpenDialogMultiple
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, ByteBuffer defaultPath) Multiple file open dialog.static int
NFD_PathSet_EnumNext
(NFDPathSetEnum enumerator, PointerBuffer outPath) Gets the next item from the path set enumerator.static void
NFD_PathSet_Free
(long pathSet) Free thepathSet
.static void
NFD_PathSet_FreeEnum
(NFDPathSetEnum enumerator) Frees an enumerator of the path set.static void
NFD_PathSet_FreePath
(long filePath) Free the path gotten byPathSet_GetPath
.static void
NFD_PathSet_FreePath
(ByteBuffer filePath) Free the path gotten byPathSet_GetPath
.static int
NFD_PathSet_GetCount
(long pathSet, int[] count) Array version of:PathSet_GetCount
static int
NFD_PathSet_GetCount
(long pathSet, IntBuffer count) Gets the number of entries stored inpathSet
.static int
NFD_PathSet_GetEnum
(long pathSet, NFDPathSetEnum outEnumerator) Gets an enumerator of the path set.static int
NFD_PathSet_GetPath
(long pathSet, int index, PointerBuffer outPath) Gets the UTF-8 path at offset index.static int
NFD_PickFolder
(PointerBuffer outPath, CharSequence defaultPath) Select folder dialog.static int
NFD_PickFolder
(PointerBuffer outPath, ByteBuffer defaultPath) Select folder dialog.static void
NFD_Quit()
static int
NFD_SaveDialog
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, CharSequence defaultPath, CharSequence defaultName) Save dialog.static int
NFD_SaveDialog
(PointerBuffer outPath, NFDFilterItem.Buffer filterList, ByteBuffer defaultPath, ByteBuffer defaultName) Save dialog.static void
nNFD_FreePath
(long filePath) Unsafe version of:FreePath
static long
Unsafe version of:GetError
static int
nNFD_OpenDialog
(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialog
static int
nNFD_OpenDialogMultiple
(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialogMultiple
static int
nNFD_PathSet_EnumNext
(long enumerator, long outPath) Unsafe version of:PathSet_EnumNext
static void
nNFD_PathSet_Free
(long pathSet) Unsafe version of:PathSet_Free
static void
nNFD_PathSet_FreeEnum
(long enumerator) Unsafe version of:PathSet_FreeEnum
static void
nNFD_PathSet_FreePath
(long filePath) Unsafe version of:PathSet_FreePath
static int
nNFD_PathSet_GetCount
(long pathSet, int[] count) Array version of:nNFD_PathSet_GetCount(long, long)
static int
nNFD_PathSet_GetCount
(long pathSet, long count) Unsafe version of:PathSet_GetCount
static int
nNFD_PathSet_GetEnum
(long pathSet, long outEnumerator) Unsafe version of:PathSet_GetEnum
static int
nNFD_PathSet_GetPath
(long pathSet, int index, long outPath) Unsafe version of:PathSet_GetPath
static int
nNFD_PickFolder
(long outPath, long defaultPath) Unsafe version of:PickFolder
static int
nNFD_SaveDialog
(long outPath, long filterList, int filterCount, long defaultPath, long defaultName) Unsafe version of:SaveDialog
-
Field Details
-
NFD_ERROR
public static final int NFD_ERRORResult values. (nfdresult_t
)Enum values:
- See Also:
-
NFD_OKAY
public static final int NFD_OKAYResult values. (nfdresult_t
)Enum values:
- See Also:
-
NFD_CANCEL
public static final int NFD_CANCELResult values. (nfdresult_t
)Enum values:
- See Also:
-
-
Method Details
-
nNFD_FreePath
public static void nNFD_FreePath(long filePath) Unsafe version of:FreePath
-
NFD_FreePath
Free a file path that was returned by the dialogs. -
NFD_FreePath
public static void NFD_FreePath(long filePath) Free a file path that was returned by the dialogs. -
NFD_Init
public static int NFD_Init()Initialize NFD - call this for every thread that might use NFD, before calling any other NFD functions on that thread. -
NFD_Quit
public static void NFD_Quit() -
nNFD_OpenDialog
public static int nNFD_OpenDialog(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialog
- Parameters:
filterCount
- if zero,filterList
is ignored (you can useNULL
)
-
NFD_OpenDialog
public static int NFD_OpenDialog(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Single file open dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
NFD_OpenDialog
public static int NFD_OpenDialog(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Single file open dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
nNFD_OpenDialogMultiple
public static int nNFD_OpenDialogMultiple(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialogMultiple
- Parameters:
filterCount
- if zero,filterList
is ignored (you can useNULL
)
-
NFD_OpenDialogMultiple
public static int NFD_OpenDialogMultiple(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Multiple file open dialog.It is the caller's responsibility to free
outPaths
viaPathSet_Free
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
NFD_OpenDialogMultiple
public static int NFD_OpenDialogMultiple(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Multiple file open dialog.It is the caller's responsibility to free
outPaths
viaPathSet_Free
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
nNFD_SaveDialog
public static int nNFD_SaveDialog(long outPath, long filterList, int filterCount, long defaultPath, long defaultName) Unsafe version of:SaveDialog
- Parameters:
filterCount
- if zero,filterList
is ignored (you can useNULL
)
-
NFD_SaveDialog
public static int NFD_SaveDialog(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath, @Nullable ByteBuffer defaultName) Save dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
NFD_SaveDialog
public static int NFD_SaveDialog(PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath, @Nullable CharSequence defaultName) Save dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
nNFD_PickFolder
public static int nNFD_PickFolder(long outPath, long defaultPath) Unsafe version of:PickFolder
-
NFD_PickFolder
Select folder dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
NFD_PickFolder
Select folder dialog.It is the caller's responsibility to free
outPath
viaFreePath
if this function returnsOKAY
.- Parameters:
defaultPath
- ifNULL
, the operating system will decide
-
nNFD_GetError
public static long nNFD_GetError()Unsafe version of:GetError
-
NFD_GetError
Get last error -- set whennfdresult_t
returnsERROR
.Returns the last error that was set, or
NULL
if there is no error. The memory is owned by NFD and should not be freed by user code. This is always ASCII printable characters, so it can be interpreted as UTF-8 without any conversion. -
NFD_ClearError
public static void NFD_ClearError()Clear the error -
nNFD_PathSet_GetCount
public static int nNFD_PathSet_GetCount(long pathSet, long count) Unsafe version of:PathSet_GetCount
-
NFD_PathSet_GetCount
Gets the number of entries stored inpathSet
.Note that some paths might be invalid (
ERROR
will be returned byPathSet_GetPath
), so we might not actually have this number of usable paths. -
nNFD_PathSet_GetPath
public static int nNFD_PathSet_GetPath(long pathSet, int index, long outPath) Unsafe version of:PathSet_GetPath
-
NFD_PathSet_GetPath
Gets the UTF-8 path at offset index.It is the caller's responsibility to free
outPath
viaPathSet_FreePath
if this function returnsOKAY
. -
nNFD_PathSet_FreePath
public static void nNFD_PathSet_FreePath(long filePath) Unsafe version of:PathSet_FreePath
-
NFD_PathSet_FreePath
Free the path gotten byPathSet_GetPath
. -
NFD_PathSet_FreePath
public static void NFD_PathSet_FreePath(long filePath) Free the path gotten byPathSet_GetPath
. -
nNFD_PathSet_GetEnum
public static int nNFD_PathSet_GetEnum(long pathSet, long outEnumerator) Unsafe version of:PathSet_GetEnum
-
NFD_PathSet_GetEnum
Gets an enumerator of the path set.It is the caller's responsibility to free
enumerator
viaPathSet_FreeEnum
if this function returnsOKAY
, and it should be freed before freeing the pathset. -
nNFD_PathSet_FreeEnum
public static void nNFD_PathSet_FreeEnum(long enumerator) Unsafe version of:PathSet_FreeEnum
-
NFD_PathSet_FreeEnum
Frees an enumerator of the path set. -
nNFD_PathSet_EnumNext
public static int nNFD_PathSet_EnumNext(long enumerator, long outPath) Unsafe version of:PathSet_EnumNext
-
NFD_PathSet_EnumNext
Gets the next item from the path set enumerator.If there are no more items, then
*outPaths
will be set toNULL
.It is the caller's responsibility to free
*outPath
viaPathSet_FreePath
if this function returnsOKAY
and*outPath
is not null. -
nNFD_PathSet_Free
public static void nNFD_PathSet_Free(long pathSet) Unsafe version of:PathSet_Free
-
NFD_PathSet_Free
public static void NFD_PathSet_Free(long pathSet) Free thepathSet
. -
nNFD_PathSet_GetCount
public static int nNFD_PathSet_GetCount(long pathSet, int[] count) Array version of:nNFD_PathSet_GetCount(long, long)
-
NFD_PathSet_GetCount
public static int NFD_PathSet_GetCount(long pathSet, int[] count) Array version of:PathSet_GetCount
-