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
stdoutorstderrabout 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
FieldsModifier and TypeFieldDescriptionstatic final intResult values.static final intResult values.static final intResult values.static final intThe native window handle type.static final intThe native window handle type.static final intThe native window handle type.static final intThe native window handle type. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClear the errorstatic voidNFD_FreePath(long filePath) Free a file path that was returned by the dialogs.static voidNFD_FreePath(ByteBuffer filePath) Free a file path that was returned by the dialogs.static @Nullable StringGet last error -- set whennfdresult_treturnsERROR.static intNFD_Init()Initialize NFD - call this for every thread that might use NFD, before calling any other NFD functions on that thread.static intNFD_OpenDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Single file open dialog.static intNFD_OpenDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Single file open dialog.static intNFD_OpenDialog_With(org.lwjgl.PointerBuffer outPath, NFDOpenDialogArgs args) Single file open dialog, with additional parameters.static intNFD_OpenDialogMultiple(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Multiple file open dialog.static intNFD_OpenDialogMultiple(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Multiple file open dialog.static intNFD_OpenDialogMultiple_With(org.lwjgl.PointerBuffer outPath, NFDOpenDialogArgs args) Multiple file open dialog, with additional parameters.static intNFD_PathSet_EnumNext(NFDPathSetEnum enumerator, org.lwjgl.PointerBuffer outPath) Gets the next item from the path set enumerator.static voidNFD_PathSet_Free(long pathSet) Free thepathSet.static voidNFD_PathSet_FreeEnum(NFDPathSetEnum enumerator) Frees an enumerator of the path set.static voidNFD_PathSet_FreePath(long filePath) Free the path gotten byPathSet_GetPath.static voidNFD_PathSet_FreePath(ByteBuffer filePath) Free the path gotten byPathSet_GetPath.static intNFD_PathSet_GetCount(long pathSet, int[] count) Array version of:PathSet_GetCountstatic intNFD_PathSet_GetCount(long pathSet, IntBuffer count) Gets the number of entries stored inpathSet.static intNFD_PathSet_GetEnum(long pathSet, NFDPathSetEnum outEnumerator) Gets an enumerator of the path set.static intNFD_PathSet_GetPath(long pathSet, int index, org.lwjgl.PointerBuffer outPath) Gets the UTF-8 path at offset index.static intNFD_PickFolder(org.lwjgl.PointerBuffer outPath, @Nullable CharSequence defaultPath) Select folder dialog.static intNFD_PickFolder(org.lwjgl.PointerBuffer outPath, @Nullable ByteBuffer defaultPath) Select folder dialog.static intNFD_PickFolder_With(org.lwjgl.PointerBuffer outPath, NFDPickFolderArgs args) Select folder dialog, with additional parameters.static intNFD_PickFolderMultiple(org.lwjgl.PointerBuffer outPaths, @Nullable CharSequence defaultPath) Select multiple folder dialog.static intNFD_PickFolderMultiple(org.lwjgl.PointerBuffer outPaths, @Nullable ByteBuffer defaultPath) Select multiple folder dialog.static intNFD_PickFolderMultiple_With(org.lwjgl.PointerBuffer outPaths, NFDPickFolderArgs args) Select multiple folder dialog, with additional parameters.static voidNFD_Quit()static intNFD_SaveDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath, @Nullable CharSequence defaultName) Save dialog.static intNFD_SaveDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath, @Nullable ByteBuffer defaultName) Save dialog.static intNFD_SaveDialog_With(org.lwjgl.PointerBuffer outPath, NFDSaveDialogArgs args) Save dialog, with additional parameters.static voidnNFD_FreePath(long filePath) Unsafe version of:FreePathstatic longUnsafe version of:GetErrorstatic intnNFD_OpenDialog(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialogstatic intnNFD_OpenDialog_With(long outPath, long args) Unsafe version of:OpenDialog_Withstatic intnNFD_OpenDialogMultiple(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialogMultiplestatic intnNFD_OpenDialogMultiple_With(long outPath, long args) Unsafe version of:OpenDialogMultiple_Withstatic intnNFD_PathSet_EnumNext(long enumerator, long outPath) Unsafe version of:PathSet_EnumNextstatic voidnNFD_PathSet_Free(long pathSet) Unsafe version of:PathSet_Freestatic voidnNFD_PathSet_FreeEnum(long enumerator) Unsafe version of:PathSet_FreeEnumstatic voidnNFD_PathSet_FreePath(long filePath) Unsafe version of:PathSet_FreePathstatic intnNFD_PathSet_GetCount(long pathSet, int[] count) Array version of:nNFD_PathSet_GetCount(long, long)static intnNFD_PathSet_GetCount(long pathSet, long count) Unsafe version of:PathSet_GetCountstatic intnNFD_PathSet_GetEnum(long pathSet, long outEnumerator) Unsafe version of:PathSet_GetEnumstatic intnNFD_PathSet_GetPath(long pathSet, int index, long outPath) Unsafe version of:PathSet_GetPathstatic intnNFD_PickFolder(long outPath, long defaultPath) Unsafe version of:PickFolderstatic intnNFD_PickFolder_With(long outPath, long args) Unsafe version of:PickFolder_Withstatic intnNFD_PickFolderMultiple(long outPaths, long defaultPath) Unsafe version of:PickFolderMultiplestatic intnNFD_PickFolderMultiple_With(long outPaths, long args) Unsafe version of:PickFolderMultiple_Withstatic intnNFD_SaveDialog(long outPath, long filterList, int filterCount, long defaultPath, long defaultName) Unsafe version of:SaveDialogstatic intnNFD_SaveDialog_With(long outPath, long args) Unsafe version of:SaveDialog_With
-
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:
-
NFD_WINDOW_HANDLE_TYPE_UNSET
public static final int NFD_WINDOW_HANDLE_TYPE_UNSETThe native window handle type.Enum values:
WINDOW_HANDLE_TYPE_UNSETWINDOW_HANDLE_TYPE_WINDOWS- Windows: handle isHWND(the Windows API typedefs this tovoid*)WINDOW_HANDLE_TYPE_COCOA- Cocoa: handle isNSWindow*WINDOW_HANDLE_TYPE_X11- X11: handle isWindow
- See Also:
-
NFD_WINDOW_HANDLE_TYPE_WINDOWS
public static final int NFD_WINDOW_HANDLE_TYPE_WINDOWSThe native window handle type.Enum values:
WINDOW_HANDLE_TYPE_UNSETWINDOW_HANDLE_TYPE_WINDOWS- Windows: handle isHWND(the Windows API typedefs this tovoid*)WINDOW_HANDLE_TYPE_COCOA- Cocoa: handle isNSWindow*WINDOW_HANDLE_TYPE_X11- X11: handle isWindow
- See Also:
-
NFD_WINDOW_HANDLE_TYPE_COCOA
public static final int NFD_WINDOW_HANDLE_TYPE_COCOAThe native window handle type.Enum values:
WINDOW_HANDLE_TYPE_UNSETWINDOW_HANDLE_TYPE_WINDOWS- Windows: handle isHWND(the Windows API typedefs this tovoid*)WINDOW_HANDLE_TYPE_COCOA- Cocoa: handle isNSWindow*WINDOW_HANDLE_TYPE_X11- X11: handle isWindow
- See Also:
-
NFD_WINDOW_HANDLE_TYPE_X11
public static final int NFD_WINDOW_HANDLE_TYPE_X11The native window handle type.Enum values:
WINDOW_HANDLE_TYPE_UNSETWINDOW_HANDLE_TYPE_WINDOWS- Windows: handle isHWND(the Windows API typedefs this tovoid*)WINDOW_HANDLE_TYPE_COCOA- Cocoa: handle isNSWindow*WINDOW_HANDLE_TYPE_X11- X11: handle isWindow
- 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,filterListis ignored (you can useNULL)
-
NFD_OpenDialog
public static int NFD_OpenDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Single file open dialog.It is the caller's responsibility to free
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
NFD_OpenDialog
public static int NFD_OpenDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Single file open dialog.It is the caller's responsibility to free
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
nNFD_OpenDialog_With
public static int nNFD_OpenDialog_With(long outPath, long args) Unsafe version of:OpenDialog_With -
NFD_OpenDialog_With
-
nNFD_OpenDialogMultiple
public static int nNFD_OpenDialogMultiple(long outPath, long filterList, int filterCount, long defaultPath) Unsafe version of:OpenDialogMultiple- Parameters:
filterCount- if zero,filterListis ignored (you can useNULL)
-
NFD_OpenDialogMultiple
public static int NFD_OpenDialogMultiple(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath) Multiple file open dialog.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
NFD_OpenDialogMultiple
public static int NFD_OpenDialogMultiple(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath) Multiple file open dialog.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
nNFD_OpenDialogMultiple_With
public static int nNFD_OpenDialogMultiple_With(long outPath, long args) Unsafe version of:OpenDialogMultiple_With -
NFD_OpenDialogMultiple_With
public static int NFD_OpenDialogMultiple_With(org.lwjgl.PointerBuffer outPath, NFDOpenDialogArgs args) Multiple file open dialog, with additional parameters.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY. -
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,filterListis ignored (you can useNULL)
-
NFD_SaveDialog
public static int NFD_SaveDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable ByteBuffer defaultPath, @Nullable ByteBuffer defaultName) Save dialog.It is the caller's responsibility to free
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
NFD_SaveDialog
public static int NFD_SaveDialog(org.lwjgl.PointerBuffer outPath, @Nullable NFDFilterItem.Buffer filterList, @Nullable CharSequence defaultPath, @Nullable CharSequence defaultName) Save dialog.It is the caller's responsibility to free
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
nNFD_SaveDialog_With
public static int nNFD_SaveDialog_With(long outPath, long args) Unsafe version of:SaveDialog_With -
NFD_SaveDialog_With
-
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
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
NFD_PickFolder
public static int NFD_PickFolder(org.lwjgl.PointerBuffer outPath, @Nullable CharSequence defaultPath) Select folder dialog.It is the caller's responsibility to free
outPathviaFreePathif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
nNFD_PickFolder_With
public static int nNFD_PickFolder_With(long outPath, long args) Unsafe version of:PickFolder_With -
NFD_PickFolder_With
-
nNFD_PickFolderMultiple
public static int nNFD_PickFolderMultiple(long outPaths, long defaultPath) Unsafe version of:PickFolderMultiple -
NFD_PickFolderMultiple
public static int NFD_PickFolderMultiple(org.lwjgl.PointerBuffer outPaths, @Nullable ByteBuffer defaultPath) Select multiple folder dialog.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
NFD_PickFolderMultiple
public static int NFD_PickFolderMultiple(org.lwjgl.PointerBuffer outPaths, @Nullable CharSequence defaultPath) Select multiple folder dialog.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY.- Parameters:
defaultPath- ifNULL, the operating system will decide
-
nNFD_PickFolderMultiple_With
public static int nNFD_PickFolderMultiple_With(long outPaths, long args) Unsafe version of:PickFolderMultiple_With -
NFD_PickFolderMultiple_With
public static int NFD_PickFolderMultiple_With(org.lwjgl.PointerBuffer outPaths, NFDPickFolderArgs args) Select multiple folder dialog, with additional parameters.It is the caller's responsibility to free
outPathsviaPathSet_Freeif this function returnsOKAY. -
nNFD_GetError
public static long nNFD_GetError()Unsafe version of:GetError -
NFD_GetError
Get last error -- set whennfdresult_treturnsERROR.Returns the last error that was set, or
NULLif 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 (
ERRORwill 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
public static int NFD_PathSet_GetPath(long pathSet, int index, org.lwjgl.PointerBuffer outPath) Gets the UTF-8 path at offset index.It is the caller's responsibility to free
outPathviaPathSet_FreePathif 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
enumeratorviaPathSet_FreeEnumif 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
*outPathswill be set toNULL.It is the caller's responsibility to free
*outPathviaPathSet_FreePathif this function returnsOKAYand*outPathis 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
-