From deffd99503324c7fe308936e017fce9fefba7fd1 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Fri, 24 Jun 2022 03:11:16 -0500 Subject: [PATCH] pass filter to NFD - DOES NOT BUILD I am so lazy to adapt the macOS part --- extern/nfd-modified/src/include/nfd.h | 8 ++- extern/nfd-modified/src/nfd_cocoa.mm | 10 +-- extern/nfd-modified/src/nfd_win.cpp | 89 ++++++--------------------- src/gui/fileDialog.cpp | 4 +- 4 files changed, 31 insertions(+), 80 deletions(-) diff --git a/extern/nfd-modified/src/include/nfd.h b/extern/nfd-modified/src/include/nfd.h index 4e4ddcf7..7f630167 100644 --- a/extern/nfd-modified/src/include/nfd.h +++ b/extern/nfd-modified/src/include/nfd.h @@ -12,6 +12,8 @@ #include #include +#include +#include /* denotes UTF-8 char */ typedef char nfdchar_t; @@ -35,19 +37,19 @@ typedef enum { /* nfd_.c */ /* single file open dialog */ -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, +nfdresult_t NFD_OpenDialog( const std::vector& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback = NULL ); /* multiple file open dialog */ -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, +nfdresult_t NFD_OpenDialogMultiple( const std::vector& filterList, const nfdchar_t *defaultPath, nfdpathset_t *outPaths, nfdselcallback_t selCallback = NULL ); /* save dialog */ -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, +nfdresult_t NFD_SaveDialog( const std::vector& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback = NULL ); diff --git a/extern/nfd-modified/src/nfd_cocoa.mm b/extern/nfd-modified/src/nfd_cocoa.mm index 8eac6f79..2f08a3a2 100644 --- a/extern/nfd-modified/src/nfd_cocoa.mm +++ b/extern/nfd-modified/src/nfd_cocoa.mm @@ -14,7 +14,7 @@ // // might as well make Objective-Ruswift++... -static NSArray *BuildAllowedFileTypes( const char *filterList ) +static NSArray *BuildAllowedFileTypes( const std::vector& filterList ) { // Commas and semicolons are the same thing on this platform @@ -56,7 +56,7 @@ static NSArray *BuildAllowedFileTypes( const char *filterList ) return returnArray; } -static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList ) +static void AddFilterListToDialog( NSSavePanel *dialog, const std::vector& filterList ) { if ( !filterList || strlen(filterList) == 0 ) return; @@ -130,7 +130,7 @@ static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset ) /* public */ -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, +nfdresult_t NFD_OpenDialog( const std::vector& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback ) @@ -173,7 +173,7 @@ nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, } -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, +nfdresult_t NFD_OpenDialogMultiple( const std::vector& filterList, const nfdchar_t *defaultPath, nfdpathset_t *outPaths, nfdselcallback_t selCallback ) @@ -218,7 +218,7 @@ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, } -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, +nfdresult_t NFD_SaveDialog( const std::vector& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback ) diff --git a/extern/nfd-modified/src/nfd_win.cpp b/extern/nfd-modified/src/nfd_win.cpp index ea18d72b..2e7f4000 100644 --- a/extern/nfd-modified/src/nfd_win.cpp +++ b/extern/nfd-modified/src/nfd_win.cpp @@ -197,44 +197,19 @@ static void CopyNFDCharToWChar( const nfdchar_t *inStr, wchar_t **outStr ) #endif } - -/* ext is in format "jpg", no wildcards or separators */ -static int AppendExtensionToSpecBuf( const char *ext, char *specBuf, size_t specBufLen ) -{ - const char SEP[] = ";"; - assert( specBufLen > strlen(ext)+3 ); - - if ( strlen(specBuf) > 0 ) - { - strncat( specBuf, SEP, specBufLen - strlen(specBuf) - 1 ); - specBufLen += strlen(SEP); - } - - char extWildcard[NFD_MAX_STRLEN]; - int bytesWritten = sprintf_s( extWildcard, NFD_MAX_STRLEN, "*.%s", ext ); - assert( bytesWritten == (int)(strlen(ext)+2) ); - _NFD_UNUSED(bytesWritten); - - strncat( specBuf, extWildcard, specBufLen - strlen(specBuf) - 1 ); - - return NFD_OKAY; -} - -static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char *filterList ) +static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const std::vector& filterList ) { const wchar_t WILDCARD[] = L"*.*"; - if ( !filterList || strlen(filterList) == 0 ) + if (filterList.empty()) return NFD_OKAY; + // list size has to be an even number (name/filter) + if (filterList.size()&1) + return NFD_ERROR; + // Count rows to alloc - UINT filterCount = 1; /* guaranteed to have one filter on a correct, non-empty parse */ - const char *p_filterList; - for ( p_filterList = filterList; *p_filterList; ++p_filterList ) - { - if ( *p_filterList == ';' ) - ++filterCount; - } + UINT filterCount = filterList.size()>>1; /* guaranteed to have one filter on a correct, non-empty parse */ assert(filterCount); if ( !filterCount ) @@ -256,43 +231,17 @@ static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char } size_t specIdx = 0; - p_filterList = filterList; - char typebuf[NFD_MAX_STRLEN] = {0}; /* one per comma or semicolon */ - char *p_typebuf = typebuf; - char specbuf[NFD_MAX_STRLEN] = {0}; /* one per semicolon */ + for (size_t i=0; i& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback ) @@ -558,7 +507,7 @@ end: return nfdResult; } -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, +nfdresult_t NFD_OpenDialogMultiple( const std::vector& filterList, const nfdchar_t *defaultPath, nfdpathset_t *outPaths, nfdselcallback_t selCallback ) @@ -653,7 +602,7 @@ end: return nfdResult; } -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, +nfdresult_t NFD_SaveDialog( const std::vector& filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath, nfdselcallback_t selCallback ) diff --git a/src/gui/fileDialog.cpp b/src/gui/fileDialog.cpp index 64f9e989..f9d4ac4d 100644 --- a/src/gui/fileDialog.cpp +++ b/src/gui/fileDialog.cpp @@ -30,9 +30,9 @@ void _nfdThread(const NFDState state, std::atomic* ok, String* result) { nfdresult_t ret=NFD_CANCEL; if (state.isSave) { - ret=NFD_SaveDialog(NULL,state.path.c_str(),&out,state.clickCallback); + ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback); } else { - ret=NFD_OpenDialog(NULL,state.path.c_str(),&out,state.clickCallback); + ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback); } switch (ret) {