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.

342 lines
17KB

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