A tiny, neat C library that portably invokes native file open and 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.
Features:
- Lean C API, static library -- no ObjC, no C++, no STL.
- Zlib licensed.
- Consistent UTF-8 support on all platforms.
- Simple universal file filter syntax.
- Paid support available.
- Multiple file selection support.
- 64-bit and 32-bit friendly.
- GCC, Clang and Visual Studio supported.
- No third party dependencies.
- Support for Vista's modern
IFileDialog
on Windows. - Support for non-deprecated Cocoa APIs on OS X.
- GTK+3 dialog on Linux.
- Tested, works alongside http://www.libsdl.org on all platforms, for the game developers out there.
#include <nfd.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
nfdchar_t *outPath = NULL;
nfdresult_t result = NFD_OpenDialog( NULL, NULL, &outPath );
if ( result == NFD_OKAY ) {
puts("Success!");
puts(outPath);
free(outPath);
}
else if ( result == NFD_CANCEL ) {
puts("User pressed cancel.");
}
else {
printf("Error: %s\n", NFD_GetError() );
}
return 0;
}
See NFD.h for more options.
NFD uses SCons for cross-platform builds. After installing SCons, build it with:
cd src
scons debug=[0,1]
Alternatively, you can avoid Scons by just including NFD files to your existing project:
- Add all header files in
src/
andsrc/include
to your project. - Add
src/include
to your include search path or copy it into your existing search path. - Add
src/nfd_common.c
to your project. - Add
src/nfd_<platform>
to your project, where<platform>
is the NFD backend for the platform you are fixing to build. - On Visual Studio, define
_CRT_SECURE_NO_WARNINGS
to avoid warnings.
- Add
src/include
to your include search path. - Add
nfd.lib
to the list of list of static libraries to link against. - Add
src/
to the library search path.
On Linux, you must compile and link against GTK+. Recommend use of pkg-config --cflags --libs gtk+-3.0
.
On Mac OS X, add AppKit
to the list of frameworks.
On Windows, ensure you are building against comctl32.lib
.
See NFD.h
for API calls. See tests/*.c
for example code.
See tests/SConstruct
for a working build script that compiles on all platforms.
There is a form of file filtering in every file dialog, but no consistent means of supporting it. NFD provides support for filtering files by groups of extensions, providing its own descriptions (where applicable) for the extensions.
A wildcard filter is always added to every dialog.
;
Begin a new filter.,
Add a separate type to the filter.
txt
The default filter is for text files. There is a wildcard option in a dropdown.
png,jpg;psd
The default filter is for png and jpg files. A second filter is available for psd files. There is a wildcard option in a dropdown.
NULL
Wildcard only.
See test_opendialogmultiple.c.
I accept quality code patches, or will resolve these and other matters through support.
- No support for Windows XP's legacy dialogs such as
GetOpenFileName
. - No support for file filter names -- ex: "Image Files" (*.png, *.jpg). Nameless filters are supported, though.
- No support for selecting folders instead of files.
- On Linux, GTK+ cannot be uninitialized to save memory. Launching a file dialog costs memory. I am open to accepting an alternative
nfd_zenity.c
implementation which uses Zenity and pipes.
Copyright © 2014 Frogtoss Games, Inc. File LICENSE covers all files in this repo.
Native File Dialog by Michael Labbe [email protected]
Tomasz Konojacki for microutf8
Directed support for this work is available from the original author under a paid agreement.