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.

202 lines
9.2KB

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