fileDialog.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef _FILEDIALOG_H_
  23. #define _FILEDIALOG_H_
  24. #include "sim/simBase.h"
  25. // [03/14/07] The file dialogs need refactoring, and will be refactored in Jugg.
  26. // Things that might need to change:
  27. // - The interface is not in fact platform agnotsic, it is win32 oriented.
  28. // - Filter format is highly windows specific, and is a little fragile, both for
  29. // win32 and for other platforms.
  30. // - Platform specific path strings are exposed to the console, because the
  31. // protected validators save them as such.
  32. // - Several of the FDS_XXX values are not options we want to give the user, such
  33. // as NOT warning on file overwrite. The values FDS_OVERWRITEPROMPT,
  34. // FDS_MUSTEXIST, and FDS_CHANGEPATH are not good things to give the user.
  35. // - The Execute method is virtual for good reason. It should be implemented for
  36. // each subclass. If common behavior is needed for Execute(), it can be
  37. // factored out in hidden platform specific code.
  38. /// @defgroup SystemDialogs Using System Dialogs
  39. /// @ingroup SystemDialogs
  40. /// FileDialogOpaqueData is both defined and implemented on a platform specific
  41. /// basis.
  42. class FileDialogOpaqueData;
  43. /// @ingroup SystemDialogs
  44. /// @internal
  45. /// Platform Agnostic Structure for holding information about a file dialog.
  46. struct FileDialogData
  47. {
  48. public:
  49. FileDialogData();
  50. ~FileDialogData();
  51. enum DialogStyle
  52. {
  53. FDS_OPEN = BIT(0),///< This is an open dialog.
  54. FDS_SAVE = BIT(1),///< This is a save dialog.
  55. FDS_OVERWRITEPROMPT = BIT(2),///< Can only be used in conjunction with style SaveDialog: prompt for a confirmation if a file will be overwritten.
  56. FDS_MUSTEXIST = BIT(3),///< The user may only select files that actually exist.
  57. FDS_MULTIPLEFILES = BIT(4),///< Can only be used in conjunction with style OpenDialog: allows selecting multiple files.
  58. FDS_CHANGEPATH = BIT(5),///< Change the current working path to the directory where the file(s) chosen by the user are.
  59. FDS_BROWSEFOLDER = BIT(6) ///< Select folders instead of files
  60. };
  61. U8 mStyle; ///< Specifies the Style of the File Dialog @see DialogStyle
  62. StringTableEntry mFilters; ///< List of Filters pipe separated e.g. "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg"
  63. //StringTableEntry mFiles; // this is never used ///< Should only be referenced when using dialogStyle OpenDialog AND MultipleFiles: List of Files returned pipe separated
  64. StringTableEntry mFile; ///< Should be referenced when dialogStyle MultipleFiles is NOT used: the file path of the user selected file.
  65. StringTableEntry mDefaultPath; ///< Default path of dialog
  66. StringTableEntry mDefaultFile; ///< Default selected file of dialog
  67. StringTableEntry mTitle; ///< Title to display in file dialog
  68. FileDialogOpaqueData *mOpaqueData; ///< Stores platform specific info about the dialog
  69. };
  70. /// @ingroup SystemDialogs
  71. /// FileDialog is a platform agnostic dialog interface for querying the user for
  72. /// file locations. It is designed to be used through the exposed
  73. /// scripting interface.
  74. ///
  75. /// FileDialog is the base class for Native File Dialog controls in Torque. It provides these
  76. /// basic areas of functionality:
  77. ///
  78. /// - Inherits from SimObject and is exposed to the scripting interface
  79. /// - Provides blocking interface to allow instant return to script execution
  80. /// - Simple object configuration makes practical use easy and effective
  81. ///
  82. /// @attention
  83. /// FileDialog is *NOT* intended to be used directly in script and is only exposed to script
  84. /// to expose generic file dialog attributes.
  85. /// @see OpenFileDialog for a practical example on opening a file
  86. /// @see SaveFileDialog for a practical example of saving a file
  87. ///
  88. ///
  89. /// @{
  90. class FileDialog : public SimObject
  91. {
  92. typedef SimObject Parent;
  93. protected:
  94. FileDialogData mData; ///< Stores platform agnostic information about the dialogs properties
  95. bool mChangePath; ///< Exposed ChangePath Property
  96. bool mBoolTranslator; ///< Internally used to translate boolean values into their respective bits of dialog style
  97. bool mPrevNativeCursorState; ///< Was the native cursor active before the dialog was opened?
  98. public:
  99. FileDialog();
  100. virtual ~FileDialog();
  101. DECLARE_CONOBJECT(FileDialog);
  102. static void initPersistFields();
  103. virtual void preExecute();
  104. virtual bool Execute();
  105. virtual void postExecute();
  106. FileDialogData &getData() { return mData; };
  107. protected:
  108. /// @name FileDialog Properties
  109. /// @{
  110. /// @@property DefaultPath (String) : <i>Path to use as the default when the dialog is shown.</i>
  111. /// @code %fd.DefaultPath = "/source/myGameProject/data/images"; @endcode
  112. ///
  113. /// @li @b ChangePath (bool) : <c>Will change the working path of the tools to the selected path when not canceled</c>
  114. /// @code %fd.ChangePath = true; // Change Working Path on Success @endcode
  115. /// @internal
  116. static bool setDefaultPath(void* obj, const char* data);
  117. static bool setDefaultFile(void* obj, const char* data);
  118. static bool setFilters(void* obj, const char* data);
  119. static bool setChangePath(void* obj, const char* data);
  120. static const char* getChangePath(void* obj, const char* data);
  121. ///
  122. /// @}
  123. static bool setFile(void* obj, const char* data);
  124. };
  125. /// @}
  126. class OpenFileDialog : public FileDialog
  127. {
  128. typedef FileDialog Parent;
  129. /// Field Values
  130. /// @{
  131. /// @internal
  132. bool mMustExist; ///< Corresponds to FDS_MUSTEXIST flag on the PlatformFileDlgData structure
  133. bool mMultipleFiles; ///< Corresponds to the FDS_MULTIPLEFILES flag on the PlatformFileDlgData structure
  134. /// @}
  135. public:
  136. OpenFileDialog();
  137. virtual ~OpenFileDialog();
  138. DECLARE_CONOBJECT(OpenFileDialog); /// @internal
  139. static void initPersistFields();
  140. protected:
  141. ///
  142. /// @}
  143. /// Must Exist Property
  144. static bool setMustExist(void* obj, const char* data);
  145. static const char*getMustExist(void* obj, const char* data);
  146. /// Multiple Files Property
  147. static bool setMultipleFiles(void* obj, const char* data);
  148. static const char* getMultipleFiles(void* obj, const char* data);
  149. };
  150. class OpenFolderDialog : public OpenFileDialog
  151. {
  152. typedef OpenFileDialog Parent;
  153. public:
  154. StringTableEntry mMustExistInDir;
  155. OpenFolderDialog();
  156. virtual ~OpenFolderDialog();
  157. DECLARE_CONOBJECT(OpenFolderDialog);
  158. static void initPersistFields();
  159. };
  160. class SaveFileDialog : public FileDialog
  161. {
  162. typedef FileDialog Parent;
  163. public:
  164. SaveFileDialog();
  165. virtual ~SaveFileDialog();
  166. DECLARE_CONOBJECT(SaveFileDialog);
  167. bool mOverwritePrompt;
  168. static void initPersistFields();
  169. protected:
  170. // Overwrite Prompt Property
  171. static bool setOverwritePrompt(void* obj, const char* data);
  172. static const char* getOverwritePrompt(void* obj, const char* data);
  173. };
  174. #endif // _FILEDIALOG_H_