fileDialog.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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->EmptyString;
  33. mDefaultFile = StringTable->EmptyString;
  34. mFilters = StringTable->EmptyString;
  35. mFile = StringTable->EmptyString;
  36. mTitle = StringTable->EmptyString;
  37. mStyle = 0;
  38. mDefaultPath = StringTable->insert(Con::getVariable("Tools::FileDialogs::LastFilePath"));
  39. if (mDefaultPath == StringTable->EmptyString || !Platform::isDirectory(mDefaultPath))
  40. mDefaultPath = Platform::getCurrentDirectory();
  41. }
  42. //-----------------------------------------------------------------------------
  43. FileDialogData::~FileDialogData()
  44. {
  45. }
  46. //-----------------------------------------------------------------------------
  47. FileDialog::FileDialog() : mData()
  48. {
  49. // Default to File Must Exist Open Dialog style
  50. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_MUSTEXIST;
  51. mChangePath = false;
  52. }
  53. //-----------------------------------------------------------------------------
  54. FileDialog::~FileDialog()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void FileDialog::initPersistFields()
  59. {
  60. addProtectedField("DefaultPath", TypeString, Offset(mData.mDefaultPath, FileDialog), &setDefaultPath, &defaultProtectedGetFn, "Default Path when Dialog is shown");
  61. addProtectedField("DefaultFile", TypeString, Offset(mData.mDefaultFile, FileDialog), &setDefaultFile, &defaultProtectedGetFn, "Default File when Dialog is shown");
  62. addProtectedField("FileName", TypeString, Offset(mData.mFile, FileDialog), &setFile, &defaultProtectedGetFn, "Default File when Dialog is shown");
  63. addProtectedField("Filters", TypeString, Offset(mData.mFilters, FileDialog), &setFilters, &defaultProtectedGetFn, "Default File when Dialog is shown");
  64. addField("Title", TypeString, Offset(mData.mTitle, FileDialog), "Default File when Dialog is shown");
  65. addProtectedField("ChangePath", TypeBool, Offset(mChangePath, FileDialog), &setChangePath, &getChangePath, "True/False whether to set the working directory to the directory returned by the dialog");
  66. Parent::initPersistFields();
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Dialog Filters
  70. bool FileDialog::setFilters(void* obj, const char* data)
  71. {
  72. // Will do validate on write at some point.
  73. if (!data)
  74. return true;
  75. return true;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // ChangePath Property - Change working path on successful file selection
  79. bool FileDialog::setChangePath(void* obj, const char* data)
  80. {
  81. bool bMustExist = dAtob(data);
  82. FileDialog *pDlg = static_cast<FileDialog*>(obj);
  83. if (bMustExist)
  84. pDlg->mData.mStyle |= FileDialogData::FDS_CHANGEPATH;
  85. else
  86. pDlg->mData.mStyle &= ~FileDialogData::FDS_CHANGEPATH;
  87. return true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // ChangePath Property - Get the path change
  91. const char* FileDialog::getChangePath(void* obj, const char* data)
  92. {
  93. FileDialog *pDlg = static_cast<FileDialog*>(obj);
  94. if (pDlg->mData.mStyle & FileDialogData::FDS_CHANGEPATH)
  95. return StringTable->insert("true");
  96. else
  97. return StringTable->insert("false");
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Dead function. Let's clean this up in the next platform sweep
  101. bool FileDialog::setFile(void* obj, const char* data)
  102. {
  103. return false;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // OpenFileDialog Constructor
  107. OpenFileDialog::OpenFileDialog()
  108. {
  109. // Default File Must Exist
  110. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_MUSTEXIST;
  111. }
  112. //-----------------------------------------------------------------------------
  113. // OpenFileDialog Destructor
  114. OpenFileDialog::~OpenFileDialog()
  115. {
  116. mMustExist = true;
  117. mMultipleFiles = false;
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Console Properties
  121. void OpenFileDialog::initPersistFields()
  122. {
  123. addProtectedField("MustExist", TypeBool, Offset(mMustExist, OpenFileDialog), &setMustExist, &getMustExist, "True/False whether the file returned must exist or not");
  124. addProtectedField("MultipleFiles", TypeBool, Offset(mMultipleFiles, OpenFileDialog), &setMultipleFiles, &getMultipleFiles, "True/False whether multiple files may be selected and returned or not");
  125. Parent::initPersistFields();
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Set the mData.mStyle to use the FDS_MUSTEXIST flag
  129. bool OpenFileDialog::setMustExist(void* obj, const char* data)
  130. {
  131. bool bMustExist = dAtob(data);
  132. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  133. if (bMustExist)
  134. pDlg->mData.mStyle |= FileDialogData::FDS_MUSTEXIST;
  135. else
  136. pDlg->mData.mStyle &= ~FileDialogData::FDS_MUSTEXIST;
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Check if the mData.mStyle has the FDS_MUSTEXIST flag
  141. const char* OpenFileDialog::getMustExist(void* obj, const char* data)
  142. {
  143. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  144. if (pDlg->mData.mStyle & FileDialogData::FDS_MUSTEXIST)
  145. return StringTable->insert("true");
  146. else
  147. return StringTable->insert("false");
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Set the the mData.mStyle property to use the FDS_MULTIPLEFILES flag
  151. bool OpenFileDialog::setMultipleFiles(void* obj, const char* data)
  152. {
  153. bool bMustExist = dAtob(data);
  154. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  155. if (bMustExist)
  156. pDlg->mData.mStyle |= FileDialogData::FDS_MULTIPLEFILES;
  157. else
  158. pDlg->mData.mStyle &= ~FileDialogData::FDS_MULTIPLEFILES;
  159. return true;
  160. };
  161. //-----------------------------------------------------------------------------
  162. // Check if the mData.mStyle has the FDS_MULTIPLEFILES flag
  163. const char* OpenFileDialog::getMultipleFiles(void* obj, const char* data)
  164. {
  165. OpenFileDialog *pDlg = static_cast<OpenFileDialog*>(obj);
  166. if (pDlg->mData.mStyle & FileDialogData::FDS_MULTIPLEFILES)
  167. return StringTable->insert("true");
  168. else
  169. return StringTable->insert("false");
  170. }
  171. //-----------------------------------------------------------------------------
  172. // SaveFileDialog Constructor
  173. SaveFileDialog::SaveFileDialog()
  174. {
  175. // Default File Must Exist
  176. mData.mStyle = FileDialogData::FDS_SAVE | FileDialogData::FDS_OVERWRITEPROMPT;
  177. mOverwritePrompt = true;
  178. }
  179. //-----------------------------------------------------------------------------
  180. // SaveFileDialog Destructor
  181. SaveFileDialog::~SaveFileDialog()
  182. {
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Console Properties
  186. void SaveFileDialog::initPersistFields()
  187. {
  188. addProtectedField("OverwritePrompt", TypeBool, Offset(mOverwritePrompt, SaveFileDialog), &setOverwritePrompt, &getOverwritePrompt, "True/False whether the dialog should prompt before accepting an existing file name");
  189. Parent::initPersistFields();
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Set the mData.mStyle property to use the FDS_OVERWRITEPROMPT flag
  193. bool SaveFileDialog::setOverwritePrompt(void* obj, const char* data)
  194. {
  195. bool bMustExist = dAtob(data);
  196. SaveFileDialog *pDlg = static_cast<SaveFileDialog*>(obj);
  197. if (bMustExist)
  198. pDlg->mData.mStyle |= FileDialogData::FDS_OVERWRITEPROMPT;
  199. else
  200. pDlg->mData.mStyle &= ~FileDialogData::FDS_OVERWRITEPROMPT;
  201. return true;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Check if the mData.mStyle property uses the FDS_OVERWRITEPROMPT flag
  205. const char* SaveFileDialog::getOverwritePrompt(void* obj, const char* data)
  206. {
  207. SaveFileDialog *pDlg = static_cast<SaveFileDialog*>(obj);
  208. if (pDlg->mData.mStyle & FileDialogData::FDS_OVERWRITEPROMPT)
  209. return StringTable->insert("true");
  210. else
  211. return StringTable->insert("false");
  212. }
  213. //-----------------------------------------------------------------------------
  214. // OpenFolderDialog Constructor
  215. OpenFolderDialog::OpenFolderDialog()
  216. {
  217. mData.mStyle = FileDialogData::FDS_OPEN | FileDialogData::FDS_OVERWRITEPROMPT | FileDialogData::FDS_BROWSEFOLDER;
  218. mMustExistInDir = "";
  219. }
  220. //-----------------------------------------------------------------------------
  221. // OpenFolderDialog Destructor
  222. OpenFolderDialog::~OpenFolderDialog()
  223. {
  224. }
  225. //-----------------------------------------------------------------------------
  226. // OpenFolderDialog Console Properties
  227. void OpenFolderDialog::initPersistFields()
  228. {
  229. addField("fileMustExist", TypeFilename, Offset(mMustExistInDir, OpenFolderDialog), "File that must in selected folder for it to be valid");
  230. Parent::initPersistFields();
  231. }