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 16KB

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