fileDialog.cc 11 KB

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