The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Creates a dialog box to choose a file or directory to load or save.
  23. e.g. @code
  24. void loadMooseFile()
  25. {
  26. myChooser = std::make_unique<FileChooser> ("Please select the moose you want to load...",
  27. File::getSpecialLocation (File::userHomeDirectory),
  28. "*.moose");
  29. auto folderChooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories;
  30. myChooser->launchAsync (folderChooserFlags, [this] (const FileChooser& chooser)
  31. {
  32. File mooseFile (chooser.getResult());
  33. loadMoose (mooseFile);
  34. }
  35. }
  36. @endcode
  37. @tags{GUI}
  38. */
  39. class JUCE_API FileChooser
  40. {
  41. public:
  42. //==============================================================================
  43. /** Creates a FileChooser.
  44. After creating one of these, use one of the browseFor... methods to display it.
  45. @param dialogBoxTitle a text string to display in the dialog box to
  46. tell the user what's going on
  47. @param initialFileOrDirectory the file or directory that should be selected
  48. when the dialog box opens. If this parameter is
  49. set to File(), a sensible default directory will
  50. be used instead. When using native dialogs, not
  51. all platforms will actually select the file. For
  52. example, on macOS, when initialFileOrDirectory is
  53. a file, only the parent directory of
  54. initialFileOrDirectory will be used as the initial
  55. directory of the native file chooser.
  56. Note: On iOS when saving a file, a user will not
  57. be able to change a file name, so it may be a good
  58. idea to include at least a valid file name in
  59. initialFileOrDirectory. When no filename is found,
  60. "Untitled" will be used.
  61. Also, if you pass an already existing file on iOS,
  62. that file will be automatically copied to the
  63. destination chosen by user and if it can be previewed,
  64. its preview will be presented in the dialog too. You
  65. will still be able to write into this file copy, since
  66. its URL will be returned by getURLResult(). This can be
  67. useful when you want to save e.g. an image, so that
  68. you can pass a (temporary) file with low quality
  69. preview and after the user picks the destination,
  70. you can write a high quality image into the copied
  71. file. If you create such a temporary file, you need
  72. to delete it yourself, once it is not needed anymore.
  73. @param filePatternsAllowed a set of file patterns to specify which files can be
  74. selected - each pattern should be separated by a comma or
  75. semi-colon, e.g. "*" or "*.jpg;*.gif". The native MacOS
  76. file browser only supports wildcard that specify
  77. extensions, so "*.jpg" is OK but "myfilename*" will not
  78. work. An empty string means that all files are allowed
  79. @param useOSNativeDialogBox if true, then a native dialog box will be used
  80. if possible; if false, then a Juce-based
  81. browser dialog box will always be used
  82. @param treatFilePackagesAsDirectories if true, then the file chooser will allow the
  83. selection of files inside packages when
  84. invoked on OS X and when using native dialog
  85. boxes.
  86. @param parentComponent An optional component which should be the parent
  87. for the file chooser. If this is a nullptr then the
  88. FileChooser will be a top-level window. AUv3s on iOS
  89. must specify this parameter as opening a top-level window
  90. in an AUv3 is forbidden due to sandbox restrictions.
  91. @see browseForFileToOpen, browseForFileToSave, browseForDirectory
  92. */
  93. FileChooser (const String& dialogBoxTitle,
  94. const File& initialFileOrDirectory = File(),
  95. const String& filePatternsAllowed = String(),
  96. bool useOSNativeDialogBox = true,
  97. bool treatFilePackagesAsDirectories = false,
  98. Component* parentComponent = nullptr);
  99. /** Destructor. */
  100. ~FileChooser();
  101. //==============================================================================
  102. #if JUCE_MODAL_LOOPS_PERMITTED
  103. /** Shows a dialog box to choose a file to open.
  104. This will display the dialog box modally, using an "open file" mode, so that
  105. it won't allow non-existent files or directories to be chosen.
  106. @param previewComponent an optional component to display inside the dialog
  107. box to show special info about the files that the user
  108. is browsing. The component will not be deleted by this
  109. object, so the caller must take care of it.
  110. @returns true if the user selected a file, in which case, use the getResult()
  111. method to find out what it was. Returns false if they cancelled instead.
  112. @see browseForFileToSave, browseForDirectory
  113. */
  114. bool browseForFileToOpen (FilePreviewComponent* previewComponent = nullptr);
  115. /** Same as browseForFileToOpen, but allows the user to select multiple files.
  116. The files that are returned can be obtained by calling getResults(). See
  117. browseForFileToOpen() for more info about the behaviour of this method.
  118. */
  119. bool browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent = nullptr);
  120. /** Shows a dialog box to choose a file to save.
  121. This will display the dialog box modally, using an "save file" mode, so it
  122. will allow non-existent files to be chosen, but not directories.
  123. @param warnAboutOverwritingExistingFiles if true, the dialog box will ask
  124. the user if they're sure they want to overwrite a file that already
  125. exists
  126. @returns true if the user chose a file and pressed 'ok', in which case, use
  127. the getResult() method to find out what the file was. Returns false
  128. if they cancelled instead.
  129. @see browseForFileToOpen, browseForDirectory
  130. */
  131. bool browseForFileToSave (bool warnAboutOverwritingExistingFiles);
  132. /** Shows a dialog box to choose a directory.
  133. This will display the dialog box modally, using an "open directory" mode, so it
  134. will only allow directories to be returned, not files.
  135. @returns true if the user chose a directory and pressed 'ok', in which case, use
  136. the getResult() method to find out what they chose. Returns false
  137. if they cancelled instead.
  138. @see browseForFileToOpen, browseForFileToSave
  139. */
  140. bool browseForDirectory();
  141. /** Same as browseForFileToOpen, but allows the user to select multiple files and directories.
  142. The files that are returned can be obtained by calling getResults(). See
  143. browseForFileToOpen() for more info about the behaviour of this method.
  144. */
  145. bool browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComponent = nullptr);
  146. //==============================================================================
  147. /** Runs a dialog box for the given set of option flags.
  148. The flag values used are those in FileBrowserComponent::FileChooserFlags.
  149. @returns true if the user chose a directory and pressed 'ok', in which case, use
  150. the getResult() method to find out what they chose. Returns false
  151. if they cancelled instead.
  152. @see FileBrowserComponent::FileChooserFlags
  153. */
  154. bool showDialog (int flags, FilePreviewComponent* previewComponent);
  155. #endif
  156. /** Use this method to launch the file browser window asynchronously.
  157. This will create a file browser dialog based on the settings in this
  158. structure and will launch it modally, returning immediately.
  159. You must specify a callback which is called when the file browser is
  160. cancelled or a file is selected. To abort the file selection, simply
  161. delete the FileChooser object.
  162. You must ensure that the lifetime of the callback object is longer than
  163. the lifetime of the file-chooser.
  164. */
  165. void launchAsync (int flags,
  166. std::function<void (const FileChooser&)>,
  167. FilePreviewComponent* previewComponent = nullptr);
  168. //==============================================================================
  169. /** Returns the last file that was chosen by one of the browseFor methods.
  170. After calling the appropriate browseFor... method, this method lets you
  171. find out what file or directory they chose.
  172. Note that the file returned is only valid if the browse method returned true (i.e.
  173. if the user pressed 'ok' rather than cancelling).
  174. On mobile platforms, the file browser may return a URL instead of a local file.
  175. Therefore, on mobile platforms, you should call getURLResult() instead.
  176. If you're using a multiple-file select, then use the getResults() method instead,
  177. to obtain the list of all files chosen.
  178. @see getURLResult, getResults
  179. */
  180. File getResult() const;
  181. /** Returns a list of all the files that were chosen during the last call to a
  182. browse method.
  183. On mobile platforms, the file browser may return a URL instead of a local file.
  184. Therefore, on mobile platforms, you should call getURLResults() instead.
  185. This array may be empty if no files were chosen, or can contain multiple entries
  186. if multiple files were chosen.
  187. @see getURLResults, getResult
  188. */
  189. Array<File> getResults() const noexcept;
  190. //==============================================================================
  191. /** Returns the last document that was chosen by one of the browseFor methods.
  192. Use this method if you are using the FileChooser on a mobile platform which
  193. may return a URL to a remote document. If a local file is chosen then you can
  194. convert this file to a JUCE File class via the URL::getLocalFile method.
  195. Note: On iOS you must use the returned URL object directly (you are also
  196. allowed to copy- or move-construct another URL from the returned URL), rather
  197. than just storing the path as a String and then creating a new URL from that
  198. String. This is because the returned URL contains internally a security
  199. bookmark that is required to access the files pointed by it. Then, once you stop
  200. dealing with the file pointed by the URL, you should dispose that URL object,
  201. so that the security bookmark can be released by the system (only a limited
  202. number of such URLs is allowed).
  203. @see getResult, URL::getLocalFile
  204. */
  205. URL getURLResult() const;
  206. /** Returns a list of all the files that were chosen during the last call to a
  207. browse method.
  208. Use this method if you are using the FileChooser on a mobile platform which
  209. may return a URL to a remote document. If a local file is chosen then you can
  210. convert this file to a JUCE File class via the URL::getLocalFile method.
  211. This array may be empty if no files were chosen, or can contain multiple entries
  212. if multiple files were chosen.
  213. Note: On iOS you must use the returned URL object directly (you are also
  214. allowed to copy- or move-construct another URL from the returned URL), rather
  215. than just storing the path as a String and then creating a new URL from that
  216. String. This is because the returned URL contains internally a security
  217. bookmark that is required to access the files pointed by it. Then, once you stop
  218. dealing with the file pointed by the URL, you should dispose that URL object,
  219. so that the security bookmark can be released by the system (only a limited
  220. number of such URLs is allowed).
  221. @see getResults, URL::getLocalFile
  222. */
  223. const Array<URL>& getURLResults() const noexcept { return results; }
  224. //==============================================================================
  225. /** Returns if a native filechooser is currently available on this platform.
  226. Note: On iOS this will only return true if you have iCloud permissions
  227. and code-signing enabled in the Projucer and have added iCloud containers
  228. to your app in Apple's online developer portal. Additionally, the user must
  229. have installed the iCloud app on their device and used the app at least once.
  230. */
  231. static bool isPlatformDialogAvailable();
  232. //==============================================================================
  233. #ifndef DOXYGEN
  234. class Native;
  235. #endif
  236. private:
  237. //==============================================================================
  238. String title, filters;
  239. File startingFile;
  240. Component* parent;
  241. Array<URL> results;
  242. const bool useNativeDialogBox;
  243. const bool treatFilePackagesAsDirs;
  244. std::function<void (const FileChooser&)> asyncCallback;
  245. //==============================================================================
  246. void finished (const Array<URL>&);
  247. //==============================================================================
  248. struct Pimpl
  249. {
  250. virtual ~Pimpl() = default;
  251. virtual void launch() = 0;
  252. virtual void runModally() = 0;
  253. };
  254. std::shared_ptr<Pimpl> pimpl;
  255. //==============================================================================
  256. std::shared_ptr<Pimpl> createPimpl (int, FilePreviewComponent*);
  257. static std::shared_ptr<Pimpl> showPlatformDialog (FileChooser&, int, FilePreviewComponent*);
  258. class NonNative;
  259. friend class NonNative;
  260. friend class Native;
  261. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileChooser)
  262. };
  263. } // namespace juce