Przeglądaj źródła

Merge pull request #1605 from Azaezel/fileFilterFix2

corrects native file dialogue return values
Areloch 9 lat temu
rodzic
commit
3262c004a5
1 zmienionych plików z 51 dodań i 19 usunięć
  1. 51 19
      Engine/source/platform/nativeDialogs/fileDialog.cpp

+ 51 - 19
Engine/source/platform/nativeDialogs/fileDialog.cpp

@@ -184,40 +184,63 @@ static const U32 convertUTF16toUTF8DoubleNULL(const UTF16 *unistring, UTF8  *out
 //
 bool FileDialog::Execute()
 {
-   String suffix;
+   String strippedFilters;
 
    U32 filtersCount = StringUnit::getUnitCount(mData.mFilters, "|");
 
    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
-      const char *filter = StringUnit::getUnit(mData.mFilters, i, "|");
+      String filter = StringUnit::getUnit(mData.mFilters, i, "|");
 
-      if (!dStrcmp(filter, "*.*"))
+      if (!dStrcmp(filter.c_str(), "*.*"))
          continue;
 
-      U32 c = 2;
-      const char* tmpchr = &filter[c];
-      String tString = String(tmpchr);
-      tString.ToLower(tString);
-      suffix += tString;
-      suffix += String(",");
-      suffix += tString.ToUpper(tString);
+      U32 subFilterCount = StringUnit::getUnitCount(filter, ";");
+
+      //if we have a 'super filter', break it down to sub-options as well
+      if (subFilterCount > 1)
+      {
+         String suffixFilter;
+         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;
-      if (i < filtersCount-2)
-         suffix += String(";");
+      if (i < filtersCount - 2)
+         strippedFilters += String(";");
    }
-   String strippedFilters = suffix;
-   strippedFilters.replace(";",",");
-      strippedFilters += String(";") + suffix;
+
+   //strip the last character, if it's unneeded
+   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
    // done its craziness and messed with it.
    StringTableEntry cwd = Platform::getCurrentDirectory();
    if (mData.mDefaultPath == StringTable->lookup("") || !Platform::isDirectory(mData.mDefaultPath))
       mData.mDefaultPath = cwd;
-
+   String rootDir = String(cwd);
    // Execute Dialog (Blocking Call)
    nfdchar_t *outPath = NULL;
    nfdpathset_t pathSet;
@@ -226,6 +249,7 @@ bool FileDialog::Execute()
    String defaultPath = String(mData.mDefaultPath);
 #if defined(TORQUE_OS_WIN)
    defaultPath.replace("/", "\\");
+   rootDir.replace("/", "\\");
 #endif
 
    if (mData.mStyle & FileDialogData::FDS_OPEN)
@@ -235,6 +259,15 @@ bool FileDialog::Execute()
    else if (mData.mStyle & FileDialogData::FDS_MULTIPLEFILES)
       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?
    if (result != NFD_OKAY)
    {
@@ -245,7 +278,7 @@ bool FileDialog::Execute()
    if (mData.mStyle & FileDialogData::FDS_OPEN || mData.mStyle & FileDialogData::FDS_SAVE)
    {
       // 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)
    {
@@ -265,14 +298,13 @@ bool FileDialog::Execute()
       else
       {
          //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");
       }
    }
 
    // Return success.
    return true;
-
 }
 
 DefineEngineMethod(FileDialog, Execute, bool, (), ,