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.3KB

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