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.

206 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_FILECHOOSER_H_INCLUDED
  18. #define JUCE_FILECHOOSER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Creates a dialog box to choose a file or directory to load or save.
  22. To use a FileChooser:
  23. - create one (as a local stack variable is the neatest way)
  24. - call one of its browseFor.. methods
  25. - if this returns true, the user has selected a file, so you can retrieve it
  26. with the getResult() method.
  27. e.g. @code
  28. void loadMooseFile()
  29. {
  30. FileChooser myChooser ("Please select the moose you want to load...",
  31. File::getSpecialLocation (File::userHomeDirectory),
  32. "*.moose");
  33. if (myChooser.browseForFileToOpen())
  34. {
  35. File mooseFile (myChooser.getResult());
  36. loadMoose (mooseFile);
  37. }
  38. }
  39. @endcode
  40. */
  41. class JUCE_API FileChooser
  42. {
  43. public:
  44. //==============================================================================
  45. /** Creates a FileChooser.
  46. After creating one of these, use one of the browseFor... methods to display it.
  47. @param dialogBoxTitle a text string to display in the dialog box to
  48. tell the user what's going on
  49. @param initialFileOrDirectory the file or directory that should be selected
  50. when the dialog box opens. If this parameter is
  51. set to File::nonexistent, a sensible default
  52. directory will be used instead.
  53. @param filePatternsAllowed a set of file patterns to specify which files
  54. can be selected - each pattern should be
  55. separated by a comma or semi-colon, e.g. "*" or
  56. "*.jpg;*.gif". An empty string means that all
  57. files are allowed
  58. @param useOSNativeDialogBox if true, then a native dialog box will be used
  59. if possible; if false, then a Juce-based
  60. browser dialog box will always be used
  61. @param treatFilePackagesAsDirectories if true, then the file chooser will allow the
  62. selection of files inside packages when
  63. invoked on OS X and when using native dialog
  64. boxes.
  65. @see browseForFileToOpen, browseForFileToSave, browseForDirectory
  66. */
  67. FileChooser (const String& dialogBoxTitle,
  68. const File& initialFileOrDirectory = File::nonexistent,
  69. const String& filePatternsAllowed = String::empty,
  70. bool useOSNativeDialogBox = true,
  71. bool treatFilePackagesAsDirectories = false);
  72. /** Destructor. */
  73. ~FileChooser();
  74. //==============================================================================
  75. /** Shows a dialog box to choose a file to open.
  76. This will display the dialog box modally, using an "open file" mode, so that
  77. it won't allow non-existent files or directories to be chosen.
  78. @param previewComponent an optional component to display inside the dialog
  79. box to show special info about the files that the user
  80. is browsing. The component will not be deleted by this
  81. object, so the caller must take care of it.
  82. @returns true if the user selected a file, in which case, use the getResult()
  83. method to find out what it was. Returns false if they cancelled instead.
  84. @see browseForFileToSave, browseForDirectory
  85. */
  86. bool browseForFileToOpen (FilePreviewComponent* previewComponent = nullptr);
  87. /** Same as browseForFileToOpen, but allows the user to select multiple files.
  88. The files that are returned can be obtained by calling getResults(). See
  89. browseForFileToOpen() for more info about the behaviour of this method.
  90. */
  91. bool browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent = nullptr);
  92. /** Shows a dialog box to choose a file to save.
  93. This will display the dialog box modally, using an "save file" mode, so it
  94. will allow non-existent files to be chosen, but not directories.
  95. @param warnAboutOverwritingExistingFiles if true, the dialog box will ask
  96. the user if they're sure they want to overwrite a file that already
  97. exists
  98. @returns true if the user chose a file and pressed 'ok', in which case, use
  99. the getResult() method to find out what the file was. Returns false
  100. if they cancelled instead.
  101. @see browseForFileToOpen, browseForDirectory
  102. */
  103. bool browseForFileToSave (bool warnAboutOverwritingExistingFiles);
  104. /** Shows a dialog box to choose a directory.
  105. This will display the dialog box modally, using an "open directory" mode, so it
  106. will only allow directories to be returned, not files.
  107. @returns true if the user chose a directory and pressed 'ok', in which case, use
  108. the getResult() method to find out what they chose. Returns false
  109. if they cancelled instead.
  110. @see browseForFileToOpen, browseForFileToSave
  111. */
  112. bool browseForDirectory();
  113. /** Same as browseForFileToOpen, but allows the user to select multiple files and directories.
  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 browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComponent = nullptr);
  118. //==============================================================================
  119. /** Runs a dialog box for the given set of option flags.
  120. The flag values used are those in FileBrowserComponent::FileChooserFlags.
  121. @returns true if the user chose a directory and pressed 'ok', in which case, use
  122. the getResult() method to find out what they chose. Returns false
  123. if they cancelled instead.
  124. @see FileBrowserComponent::FileChooserFlags
  125. */
  126. bool showDialog (int flags, FilePreviewComponent* previewComponent);
  127. //==============================================================================
  128. /** Returns the last file that was chosen by one of the browseFor methods.
  129. After calling the appropriate browseFor... method, this method lets you
  130. find out what file or directory they chose.
  131. Note that the file returned is only valid if the browse method returned true (i.e.
  132. if the user pressed 'ok' rather than cancelling).
  133. If you're using a multiple-file select, then use the getResults() method instead,
  134. to obtain the list of all files chosen.
  135. @see getResults
  136. */
  137. File getResult() const;
  138. /** Returns a list of all the files that were chosen during the last call to a
  139. browse method.
  140. This array may be empty if no files were chosen, or can contain multiple entries
  141. if multiple files were chosen.
  142. @see getResult
  143. */
  144. const Array<File>& getResults() const noexcept { return results; }
  145. private:
  146. //==============================================================================
  147. String title, filters;
  148. const File startingFile;
  149. Array<File> results;
  150. const bool useNativeDialogBox;
  151. const bool treatFilePackagesAsDirs;
  152. static void showPlatformDialog (Array<File>& results, const String& title, const File& file,
  153. const String& filters, bool selectsDirectories, bool selectsFiles,
  154. bool isSave, bool warnAboutOverwritingExistingFiles, bool selectMultipleFiles,
  155. bool treatFilePackagesAsDirs, FilePreviewComponent* previewComponent);
  156. static bool isPlatformDialogAvailable();
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileChooser)
  158. };
  159. #endif // JUCE_FILECHOOSER_H_INCLUDED