Audio plugin host https://kx.studio/carla
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.

330 lines
16KB

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