juce_FileChooser.h 8.8 KB

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