fix Windows file picker filter not working at all

This commit is contained in:
tildearrow 2022-06-27 03:23:13 -05:00
parent 01057ced3a
commit 5507fd9f33

View file

@ -211,7 +211,8 @@ static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const std:
// Count rows to alloc // Count rows to alloc
UINT filterCount = filterList.size()>>1; /* guaranteed to have one filter on a correct, non-empty parse */ UINT filterCount = filterList.size()>>1; /* guaranteed to have one filter on a correct, non-empty parse */
assert(filterCount); if (filterCount==0) filterCount=1;
if ( !filterCount ) if ( !filterCount )
{ {
NFDi_SetError("Error parsing filters."); NFDi_SetError("Error parsing filters.");
@ -219,12 +220,12 @@ static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const std:
} }
/* filterCount plus 1 because we hardcode the *.* wildcard after the while loop */ /* filterCount plus 1 because we hardcode the *.* wildcard after the while loop */
COMDLG_FILTERSPEC *specList = (COMDLG_FILTERSPEC*)NFDi_Malloc( sizeof(COMDLG_FILTERSPEC) * ((size_t)filterCount + 1) ); COMDLG_FILTERSPEC *specList = (COMDLG_FILTERSPEC*)NFDi_Malloc( sizeof(COMDLG_FILTERSPEC) * ((size_t)filterCount) );
if ( !specList ) if ( !specList )
{ {
return NFD_ERROR; return NFD_ERROR;
} }
for (UINT i = 0; i < filterCount+1; ++i ) for (UINT i = 0; i < filterCount; ++i )
{ {
specList[i].pszName = NULL; specList[i].pszName = NULL;
specList[i].pszSpec = NULL; specList[i].pszSpec = NULL;
@ -236,19 +237,22 @@ static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const std:
String name=filterList[i]; String name=filterList[i];
String spec=filterList[i+1]; String spec=filterList[i+1];
for (char& i: spec) { for (char& i: spec) {
if (i==' ') i=','; if (i==' ') i=';';
} }
if (spec==".*") spec="*.*";
CopyNFDCharToWChar( name.c_str(), (wchar_t**)&specList[specIdx].pszName ); CopyNFDCharToWChar( name.c_str(), (wchar_t**)&specList[specIdx].pszName );
CopyNFDCharToWChar( spec.c_str(), (wchar_t**)&specList[specIdx].pszSpec ); CopyNFDCharToWChar( spec.c_str(), (wchar_t**)&specList[specIdx].pszSpec );
++specIdx; ++specIdx;
} }
/* Add wildcard */ /* Add wildcard if specIdx is 0 */
specList[specIdx].pszSpec = WILDCARD; if (specIdx==0) {
specList[specIdx].pszName = WILDCARD; specList[specIdx].pszSpec = WILDCARD;
specList[specIdx].pszName = WILDCARD;
}
fileOpenDialog->SetFileTypes( filterCount+1, specList ); fileOpenDialog->SetFileTypes( filterCount, specList );
/* free speclist */ /* free speclist */
for ( size_t i = 0; i < filterCount; ++i ) for ( size_t i = 0; i < filterCount; ++i )