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.

200 lines
8.9KB

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