fileDialog.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/nativeDialogs/fileDialog.h"
  23. #include "console/consoleTypes.h"
  24. #include "fileDialog_ScriptBinding.h"
  25. IMPLEMENT_CONOBJECT(FileDialog);
  26. IMPLEMENT_CONOBJECT(OpenFileDialog);
  27. IMPLEMENT_CONOBJECT(SaveFileDialog);
  28. IMPLEMENT_CONOBJECT(OpenFolderDialog);
  29. //-----------------------------------------------------------------------------
  30. FileDialogData::FileDialogData()
  31. {
  32. mDefaultPath = StringTable->insert("");
  33. mFilters = StringTable->insert("");
  34. mFile = StringTable->insert("");
  35. mTitle = StringTable->insert("");
  36. mStyle = 0;
  37. mDefaultPath = StringTable->insert(Con::getVariable("Tools::FileDialogs::LastFilePath"));
  38. if (mDefaultPath == StringTable->EmptyString || !Platform::isDirectory(mDefaultPath))
  39. mDefaultPath = Platform::getCurrentDirectory();
  40. }
  41. //-----------------------------------------------------------------------------
  42. FileDialogData::~FileDialogData()
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. FileDialog::FileDialog() : mData()
  47. {
  48. // Default to File Must Exist Open Dialog style
  49. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_MUSTEXIST;
  50. mChangePath = false;
  51. }
  52. //-----------------------------------------------------------------------------
  53. FileDialog::~FileDialog()
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. void FileDialog::initPersistFields()
  58. {
  59. addProtectedField("DefaultPath", TypeString, Offset(mData.mDefaultPath, FileDialog), &setDefaultPath, &defaultProtectedGetFn, "Default Path when Dialog is shown");
  60. addProtectedField("DefaultFile", TypeString, Offset(mData.mDefaultFile, FileDialog), &setDefaultFile, &defaultProtectedGetFn, "Default File when Dialog is shown");
  61. addProtectedField("FileName", TypeString, Offset(mData.mFile, FileDialog), &setFile, &defaultProtectedGetFn, "Default File when Dialog is shown");
  62. addProtectedField("Filters", TypeString, Offset(mData.mFilters, FileDialog), &setFilters, &defaultProtectedGetFn, "Default File when Dialog is shown");
  63. addField("Title", TypeString, Offset(mData.mTitle, FileDialog), "Default File when Dialog is shown");
  64. addProtectedField("ChangePath", TypeBool, Offset(mChangePath, FileDialog), &setChangePath, &getChangePath, "True/False whether to set the working directory to the directory returned by the dialog");
  65. Parent::initPersistFields();
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Dialog Filters
  69. bool FileDialog::setFilters(void* obj, const char* data)
  70. {
  71. // Will do validate on write at some point.
  72. if (!data)
  73. return true;
  74. return true;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // ChangePath Property - Change working path on successful file selection
  78. bool FileDialog::setChangePath(void* obj, const char* data)
  79. {
  80. bool bMustExist = dAtob(data);
  81. FileDialog *pDlg = static_cast<FileDialog*>(obj);
  82. if (bMustExist)
  83. pDlg->mData.mStyle |= FileDialogData::FDS_CHANGEPATH;
  84. else
  85. pDlg->mData.mStyle &= ~FileDialogData::FDS_CHANGEPATH;
  86. return true;
  87. }
  88. //-----------------------------------------------------------------------------
  89. // ChangePath Property - Get the path change
  90. const char* FileDialog::getChangePath(void* obj, const char* data)
  91. {
  92. FileDialog *pDlg = static_cast<FileDialog*>(obj);
  93. if (pDlg->mData.mStyle & FileDialogData::FDS_CHANGEPATH)
  94. return StringTable->insert("true");
  95. else
  96. return StringTable->insert("false");
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Dead function. Let's clean this up in the next platform sweep
  100. bool FileDialog::setFile(void* obj, const char* data)
  101. {
  102. return false;
  103. }
  104. //-----------------------------------------------------------------------------
  105. // OpenFileDialog Constructor
  106. OpenFileDialog::OpenFileDialog()
  107. {
  108. // Default File Must Exist
  109. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_MUSTEXIST;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // OpenFileDialog Destructor
  113. OpenFileDialog::~OpenFileDialog()
  114. {
  115. mMustExist = true;
  116. mMultipleFiles = false;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Console Properties
  120. void OpenFileDialog::initPersistFields()
  121. {
  122. addProtectedField("MustExist", TypeBool, Offset(mMustExist, OpenFileDialog), &setMustExist, &getMustExist, "True/False whether the file returned must exist or not");
  123. addProtectedField("MultipleFiles", TypeBool, Offset(mMultipleFiles, OpenFileDialog), &setMultipleFiles, &getMultipleFiles, "True/False whether multiple files may be selected and returned or not");
  124. Parent::initPersistFields();
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Set the mData.mStyle to use the FDS_MUSTEXIST flag
  128. bool OpenFileDialog::setMustExist(void* obj, const char* data)
  129. {
  130. bool bMustExist = dAtob(data);
  131. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  132. if (bMustExist)
  133. pDlg->mData.mStyle |= FileDialogData::FDS_MUSTEXIST;
  134. else
  135. pDlg->mData.mStyle &= ~FileDialogData::FDS_MUSTEXIST;
  136. return true;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Check if the mData.mStyle has the FDS_MUSTEXIST flag
  140. const char* OpenFileDialog::getMustExist(void* obj, const char* data)
  141. {
  142. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  143. if (pDlg->mData.mStyle & FileDialogData::FDS_MUSTEXIST)
  144. return StringTable->insert("true");
  145. else
  146. return StringTable->insert("false");
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Set the the mData.mStyle property to use the FDS_MULTIPLEFILES flag
  150. bool OpenFileDialog::setMultipleFiles(void* obj, const char* data)
  151. {
  152. bool bMustExist = dAtob(data);
  153. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  154. if (bMustExist)
  155. pDlg->mData.mStyle |= FileDialogData::FDS_MULTIPLEFILES;
  156. else
  157. pDlg->mData.mStyle &= ~FileDialogData::FDS_MULTIPLEFILES;
  158. return true;
  159. };
  160. //-----------------------------------------------------------------------------
  161. // Check if the mData.mStyle has the FDS_MULTIPLEFILES flag
  162. const char* OpenFileDialog::getMultipleFiles(void* obj, const char* data)
  163. {
  164. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  165. if (pDlg->mData.mStyle & FileDialogData::FDS_MULTIPLEFILES)
  166. return StringTable->insert("true");
  167. else
  168. return StringTable->insert("false");
  169. }
  170. //-----------------------------------------------------------------------------
  171. // SaveFileDialog Constructor
  172. SaveFileDialog::SaveFileDialog()
  173. {
  174. // Default File Must Exist
  175. mData.mStyle = FileDialogData::FDS_SAVE | FileDialogData::FDS_OVERWRITEPROMPT;
  176. mOverwritePrompt = true;
  177. }
  178. //-----------------------------------------------------------------------------
  179. // SaveFileDialog Destructor
  180. SaveFileDialog::~SaveFileDialog()
  181. {
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Console Properties
  185. void SaveFileDialog::initPersistFields()
  186. {
  187. addProtectedField("OverwritePrompt", TypeBool, Offset(mOverwritePrompt, SaveFileDialog), &setOverwritePrompt, &getOverwritePrompt, "True/False whether the dialog should prompt before accepting an existing file name");
  188. Parent::initPersistFields();
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Set the mData.mStyle property to use the FDS_OVERWRITEPROMPT flag
  192. bool SaveFileDialog::setOverwritePrompt(void* obj, const char* data)
  193. {
  194. bool bMustExist = dAtob(data);
  195. SaveFileDialog *pDlg = static_cast<SaveFileDialog*>(obj);
  196. if (bMustExist)
  197. pDlg->mData.mStyle |= FileDialogData::FDS_OVERWRITEPROMPT;
  198. else
  199. pDlg->mData.mStyle &= ~FileDialogData::FDS_OVERWRITEPROMPT;
  200. return true;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Check if the mData.mStyle property uses the FDS_OVERWRITEPROMPT flag
  204. const char* SaveFileDialog::getOverwritePrompt(void* obj, const char* data)
  205. {
  206. SaveFileDialog *pDlg = static_cast<SaveFileDialog*>(obj);
  207. if (pDlg->mData.mStyle & FileDialogData::FDS_OVERWRITEPROMPT)
  208. return StringTable->insert("true");
  209. else
  210. return StringTable->insert("false");
  211. }
  212. //-----------------------------------------------------------------------------
  213. // OpenFolderDialog Constructor
  214. OpenFolderDialog::OpenFolderDialog()
  215. {
  216. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_OVERWRITEPROMPT | FileDialogData::FDS_BROWSEFOLDER;
  217. mMustExistInDir = "";
  218. }
  219. //-----------------------------------------------------------------------------
  220. // OpenFolderDialog Destructor
  221. OpenFolderDialog::~OpenFolderDialog()
  222. {
  223. }
  224. //-----------------------------------------------------------------------------
  225. // OpenFolderDialog Console Properties
  226. void OpenFolderDialog::initPersistFields()
  227. {
  228. addField("fileMustExist", TypeFilename, Offset(mMustExistInDir, OpenFolderDialog), "File that must in selected folder for it to be valid");
  229. Parent::initPersistFields();
  230. }