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

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