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.

juce_FileChooser.h 17KB

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