Merge pull request #1605 from Azaezel/fileFilterFix2

corrects native file dialogue return values
This commit is contained in:
Areloch 2016-05-17 12:49:14 -05:00
commit 3262c004a5

View file

@ -184,40 +184,63 @@ static const U32 convertUTF16toUTF8DoubleNULL(const UTF16 *unistring, UTF8 *out
// //
bool FileDialog::Execute() bool FileDialog::Execute()
{ {
String suffix; String strippedFilters;
U32 filtersCount = StringUnit::getUnitCount(mData.mFilters, "|"); U32 filtersCount = StringUnit::getUnitCount(mData.mFilters, "|");
for (U32 i = 1; i < filtersCount; ++i) for (U32 i = 1; i < filtersCount; ++i)
{ {
//The first of each pair is the name, which we'll skip because NFD doesn't support named filters atm //The first of each pair is the name, which we'll skip because NFD doesn't support named filters atm
const char *filter = StringUnit::getUnit(mData.mFilters, i, "|"); String filter = StringUnit::getUnit(mData.mFilters, i, "|");
if (!dStrcmp(filter, "*.*")) if (!dStrcmp(filter.c_str(), "*.*"))
continue; continue;
U32 c = 2; U32 subFilterCount = StringUnit::getUnitCount(filter, ";");
const char* tmpchr = &filter[c];
String tString = String(tmpchr); //if we have a 'super filter', break it down to sub-options as well
tString.ToLower(tString); if (subFilterCount > 1)
suffix += tString; {
suffix += String(","); String suffixFilter;
suffix += tString.ToUpper(tString); String subFilters;
for (U32 f = 0; f < subFilterCount; ++f)
{
String subFilter = StringUnit::getUnit(filter, f, ";");
suffixFilter += String::ToLower(subFilter) + "," + String::ToUpper(subFilter) + ",";
subFilters += String::ToLower(subFilter) + "," + String::ToUpper(subFilter) + ";";
}
suffixFilter = suffixFilter.substr(0, suffixFilter.length() - 1);
suffixFilter += ";";
strippedFilters += suffixFilter + subFilters;
}
else //otherwise, just add the filter
{
strippedFilters += String::ToLower(filter) + "," + String::ToUpper(filter) + ";";
}
++i; ++i;
if (i < filtersCount-2) if (i < filtersCount - 2)
suffix += String(";"); strippedFilters += String(";");
} }
String strippedFilters = suffix;
strippedFilters.replace(";",","); //strip the last character, if it's unneeded
strippedFilters += String(";") + suffix; if (strippedFilters.endsWith(";"))
{
strippedFilters = strippedFilters.substr(0, strippedFilters.length() - 1);
}
strippedFilters.replace("*.", "");
// Get the current working directory, so we can back up to it once Windows has // Get the current working directory, so we can back up to it once Windows has
// done its craziness and messed with it. // done its craziness and messed with it.
StringTableEntry cwd = Platform::getCurrentDirectory(); StringTableEntry cwd = Platform::getCurrentDirectory();
if (mData.mDefaultPath == StringTable->lookup("") || !Platform::isDirectory(mData.mDefaultPath)) if (mData.mDefaultPath == StringTable->lookup("") || !Platform::isDirectory(mData.mDefaultPath))
mData.mDefaultPath = cwd; mData.mDefaultPath = cwd;
String rootDir = String(cwd);
// Execute Dialog (Blocking Call) // Execute Dialog (Blocking Call)
nfdchar_t *outPath = NULL; nfdchar_t *outPath = NULL;
nfdpathset_t pathSet; nfdpathset_t pathSet;
@ -226,6 +249,7 @@ bool FileDialog::Execute()
String defaultPath = String(mData.mDefaultPath); String defaultPath = String(mData.mDefaultPath);
#if defined(TORQUE_OS_WIN) #if defined(TORQUE_OS_WIN)
defaultPath.replace("/", "\\"); defaultPath.replace("/", "\\");
rootDir.replace("/", "\\");
#endif #endif
if (mData.mStyle & FileDialogData::FDS_OPEN) if (mData.mStyle & FileDialogData::FDS_OPEN)
@ -235,6 +259,15 @@ bool FileDialog::Execute()
else if (mData.mStyle & FileDialogData::FDS_MULTIPLEFILES) else if (mData.mStyle & FileDialogData::FDS_MULTIPLEFILES)
result = NFD_OpenDialogMultiple(strippedFilters.c_str(), defaultPath.c_str(), &pathSet); result = NFD_OpenDialogMultiple(strippedFilters.c_str(), defaultPath.c_str(), &pathSet);
if (result == NFD_CANCEL)
{
return false;
}
String resultPath = String(outPath).replace(rootDir, String(""));
resultPath = resultPath.replace(0, 1, String("")).c_str(); //kill '\\' prefix
resultPath = resultPath.replace(String("\\"), String("/"));
// Did we select a file? // Did we select a file?
if (result != NFD_OKAY) if (result != NFD_OKAY)
{ {
@ -245,7 +278,7 @@ bool FileDialog::Execute()
if (mData.mStyle & FileDialogData::FDS_OPEN || mData.mStyle & FileDialogData::FDS_SAVE) if (mData.mStyle & FileDialogData::FDS_OPEN || mData.mStyle & FileDialogData::FDS_SAVE)
{ {
// Single file selection, do it the easy way // Single file selection, do it the easy way
mData.mFile = StringTable->insert(outPath); mData.mFile = Platform::makeRelativePathName(resultPath.c_str(), NULL);
} }
else if (mData.mStyle & FileDialogData::FDS_MULTIPLEFILES) else if (mData.mStyle & FileDialogData::FDS_MULTIPLEFILES)
{ {
@ -265,14 +298,13 @@ bool FileDialog::Execute()
else else
{ {
//nope, just one file, so set it as normal //nope, just one file, so set it as normal
setDataField(StringTable->insert("files"), "0", outPath); setDataField(StringTable->insert("files"), "0", Platform::makeRelativePathName(resultPath.c_str(), NULL));
setDataField(StringTable->insert("fileCount"), NULL, "1"); setDataField(StringTable->insert("fileCount"), NULL, "1");
} }
} }
// Return success. // Return success.
return true; return true;
} }
DefineEngineMethod(FileDialog, Execute, bool, (), , DefineEngineMethod(FileDialog, Execute, bool, (), ,