Skip to content

Commit

Permalink
Integrates the nativeFileDialog library to enable native file dialogs…
Browse files Browse the repository at this point in the history
… on the major platforms. It is activated with SDL.
  • Loading branch information
Areloch committed Apr 30, 2016
1 parent bab55d4 commit ec6f9c0
Show file tree
Hide file tree
Showing 15 changed files with 2,489 additions and 354 deletions.
16 changes: 16 additions & 0 deletions Engine/lib/nativeFileDialogs/LICENSE
@@ -0,0 +1,16 @@
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

137 changes: 137 additions & 0 deletions Engine/lib/nativeFileDialogs/README.md
@@ -0,0 +1,137 @@
# Native File Dialog #

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](SDL2) on all platforms, for the game developers out there.

# Example Usage #

```C
#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](src/include/nfd.h) for more options.
# Screenshots #
![Windows 8 rendering an IFileOpenDialog](screens/open_win8.png?raw=true)
![GTK3 on Linux](screens/open_gtk3.png?raw=true)
![Cocoa on Yosemite](screens/open_cocoa.png?raw=true)
## Building ##
NFD uses [SCons](http://www.scons.org) 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:
1. Add all header files in `src/` and `src/include` to your project.
2. Add `src/include` to your include search path or copy it into your existing search path.
3. Add `src/nfd_common.c` to your project.
4. Add `src/nfd_<platform>` to your project, where `<platform>` is the NFD backend for the platform you are fixing to build.
5. On Visual Studio, define `_CRT_SECURE_NO_WARNINGS` to avoid warnings.
### Compiling Your Programs ###
1. Add `src/include` to your include search path.
2. Add `nfd.lib` to the list of list of static libraries to link against.
3. 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`.
## Usage ##
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.
## File Filter Syntax ##
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.
### Separators ###
- `;` Begin a new filter.
- `,` Add a separate type to the filter.
#### Examples ####
`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.
## Iterating Over PathSets ##
See [test_opendialogmultiple.c](test/test_opendialogmultiple.c).
# Known Limitations #
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 and Credit #
Copyright &copy; 2014 [Frogtoss Games](http://www.frogtoss.com), Inc.
File [LICENSE](LICENSE) covers all files in this repo.
Native File Dialog by Michael Labbe
<mike@frogtoss.com>
Tomasz Konojacki for [microutf8](http://puszcza.gnu.org.ua/software/microutf8/)
## Support ##
Directed support for this work is available from the original author under a paid agreement.
[Contact Frogtoss Games](http://www.frogtoss.com/pages/contact.html).
99 changes: 99 additions & 0 deletions Engine/lib/nativeFileDialogs/SConstruct
@@ -0,0 +1,99 @@
#
# Native File Dialog
#
# Scons build script -- GCC, Clang, Visual Studio
# Does not build test


import os


# target arch is build arch -- extend here for OS cross compiling
target_os=str(Platform())

# Corresponds to TARGET_ARCH set to environ.
target_arch = ARGUMENTS.get('target_arch', None)

# visual studio does not import from environment
if target_os != 'win32':
IMPORT_FROM_ENV =['CC', 'CXX', 'CFLAGS', 'CXXFLAGS', 'ARFLAGS']
else:
IMPORT_FROM_ENV =[]


debug = int(ARGUMENTS.get( 'debug', 0 ))

nfd_files = ['nfd_common.c']

# Due to a Scons limitation, TARGET_ARCH cannot be appended to an existing environment.
if target_arch != None:
nfd_env = Environment( TARGET_ARCH=target_arch )
else:
nfd_env = Environment()

# import specific environment variables from the command line, overriding
# Scons environment defaults
for env_key in IMPORT_FROM_ENV:
if env_key in os.environ:
print "Making %s => %s" % ( env_key, os.environ[env_key] )
nfd_env[env_key] = os.environ[env_key]

# Windows runtime library types
win_rtl = {'debug': '/MDd',
'release': '/MD'}

def set_debug(env):
if target_os == 'win32':
env.Append( CCFLAGS=['/Z7', # obj contains full symbols
win_rtl['debug']
])
else:
env.Append( CFLAGS=['-g'] )


def set_release(env):
if target_os == 'win32':
env.Append( CCFLAGS=[win_rtl['release'],
'/O2'] )
else:
env.Append( CFLAGS=['-O3'] )


def set_warnings(env):
if target_os == 'win32':
env.Append( CCFLAGS=['/W3'],
CPPDEFINES=['_CRT_SECURE_NO_WARNINGS'] )
else:
env.Append( CFLAGS=['-Wall', '-pedantic'] )


def get_lib_name(base, is_debug):
if is_debug:
return base + '_d'
else:
return base


# Cocoa OS X builds - clang
if target_os == 'darwin':
nfd_files.append('nfd_cocoa.m')
nfd_env.CC='clang -fcolor-diagnostics'

# Linux GTK+ 3 builds - GCC
elif target_os == 'posix':
nfd_files.append('nfd_gtk.c')
nfd_env.ParseConfig( 'pkg-config --cflags gtk+-3.0' )

# Windows builds - Visual Studio
elif target_os == 'win32':
nfd_files.append('nfd_win.cpp')

if debug:
set_debug(nfd_env)
else:
set_release(nfd_env)

set_warnings(nfd_env)

nfd_env.Append( CPPPATH=['.','./include'] )
nfd_env.StaticLibrary( get_lib_name('nfd', debug), nfd_files )
21 changes: 21 additions & 0 deletions Engine/lib/nativeFileDialogs/common.h
@@ -0,0 +1,21 @@
/*
Native File Dialog
Internal, common across platforms
http://www.frogtoss.com/labs
*/


#ifndef _NFD_COMMON_H
#define _NFD_COMMON_H

#define NFD_MAX_STRLEN 256
#define _NFD_UNUSED(x) ((void)x)

void *NFDi_Malloc( size_t bytes );
void NFDi_Free( void *ptr );
void NFDi_SetError( const char *msg );
void NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy );

#endif
69 changes: 69 additions & 0 deletions Engine/lib/nativeFileDialogs/include/nfd.h
@@ -0,0 +1,69 @@
/*
Native File Dialog
User API
http://www.frogtoss.com/labs
*/


#ifndef _NFD_H
#define _NFD_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

/* denotes UTF-8 char */
typedef char nfdchar_t;

/* opaque data structure -- see NFD_PathSet_* */
typedef struct {
nfdchar_t *buf;
size_t *indices; /* byte offsets into buf */
size_t count; /* number of indices into buf */
}nfdpathset_t;

typedef enum {
NFD_ERROR, /* programmatic error */
NFD_OKAY, /* user pressed okay, or successful return */
NFD_CANCEL /* user pressed cancel */
}nfdresult_t;


/* nfd_<targetplatform>.c */

/* single file open dialog */
nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath );

/* multiple file open dialog */
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
const nfdchar_t *defaultPath,
nfdpathset_t *outPaths );

/* save dialog */
nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath );

/* nfd_common.c */

/* get last error -- set when nfdresult_t returns NFD_ERROR */
const char *NFD_GetError( void );
/* get the number of entries stored in pathSet */
size_t NFD_PathSet_GetCount( const nfdpathset_t *pathSet );
/* Get the UTF-8 path at offset index */
nfdchar_t *NFD_PathSet_GetPath( const nfdpathset_t *pathSet, size_t index );
/* Free the pathSet */
void NFD_PathSet_Free( nfdpathset_t *pathSet );


#ifdef __cplusplus
}
#endif

#endif

0 comments on commit ec6f9c0

Please sign in to comment.