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.

194 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILECHOOSER_JUCEHEADER__
  19. #define __JUCE_FILECHOOSER_JUCEHEADER__
  20. #include "juce_FilePreviewComponent.h"
  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 when
  52. the dialog box opens. If this parameter is set to
  53. File::nonexistent, a sensible default directory
  54. will be used instead.
  55. @param filePatternsAllowed a set of file patterns to specify which files can be
  56. selected - each pattern should be separated by a
  57. comma or semi-colon, e.g. "*" or "*.jpg;*.gif". An
  58. empty string means that all files are allowed
  59. @param useOSNativeDialogBox if true, then a native dialog box will be used if
  60. possible; if false, then a Juce-based browser dialog
  61. box will always be used
  62. @see browseForFileToOpen, browseForFileToSave, browseForDirectory
  63. */
  64. FileChooser (const String& dialogBoxTitle,
  65. const File& initialFileOrDirectory = File::nonexistent,
  66. const String& filePatternsAllowed = String::empty,
  67. bool useOSNativeDialogBox = true);
  68. /** Destructor. */
  69. ~FileChooser();
  70. //==============================================================================
  71. /** Shows a dialog box to choose a file to open.
  72. This will display the dialog box modally, using an "open file" mode, so that
  73. it won't allow non-existent files or directories to be chosen.
  74. @param previewComponent an optional component to display inside the dialog
  75. box to show special info about the files that the user
  76. is browsing. The component will not be deleted by this
  77. object, so the caller must take care of it.
  78. @returns true if the user selected a file, in which case, use the getResult()
  79. method to find out what it was. Returns false if they cancelled instead.
  80. @see browseForFileToSave, browseForDirectory
  81. */
  82. bool browseForFileToOpen (FilePreviewComponent* previewComponent = 0);
  83. /** Same as browseForFileToOpen, but allows the user to select multiple files.
  84. The files that are returned can be obtained by calling getResults(). See
  85. browseForFileToOpen() for more info about the behaviour of this method.
  86. */
  87. bool browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent = 0);
  88. /** Shows a dialog box to choose a file to save.
  89. This will display the dialog box modally, using an "save file" mode, so it
  90. will allow non-existent files to be chosen, but not directories.
  91. @param warnAboutOverwritingExistingFiles if true, the dialog box will ask
  92. the user if they're sure they want to overwrite a file that already
  93. exists
  94. @returns true if the user chose a file and pressed 'ok', in which case, use
  95. the getResult() method to find out what the file was. Returns false
  96. if they cancelled instead.
  97. @see browseForFileToOpen, browseForDirectory
  98. */
  99. bool browseForFileToSave (bool warnAboutOverwritingExistingFiles);
  100. /** Shows a dialog box to choose a directory.
  101. This will display the dialog box modally, using an "open directory" mode, so it
  102. will only allow directories to be returned, not files.
  103. @returns true if the user chose a directory and pressed 'ok', in which case, use
  104. the getResult() method to find out what they chose. Returns false
  105. if they cancelled instead.
  106. @see browseForFileToOpen, browseForFileToSave
  107. */
  108. bool browseForDirectory();
  109. /** Same as browseForFileToOpen, but allows the user to select multiple files and directories.
  110. The files that are returned can be obtained by calling getResults(). See
  111. browseForFileToOpen() for more info about the behaviour of this method.
  112. */
  113. bool browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComponent = 0);
  114. //==============================================================================
  115. /** Returns the last file that was chosen by one of the browseFor methods.
  116. After calling the appropriate browseFor... method, this method lets you
  117. find out what file or directory they chose.
  118. Note that the file returned is only valid if the browse method returned true (i.e.
  119. if the user pressed 'ok' rather than cancelling).
  120. If you're using a multiple-file select, then use the getResults() method instead,
  121. to obtain the list of all files chosen.
  122. @see getResults
  123. */
  124. File getResult() const;
  125. /** Returns a list of all the files that were chosen during the last call to a
  126. browse method.
  127. This array may be empty if no files were chosen, or can contain multiple entries
  128. if multiple files were chosen.
  129. @see getResult
  130. */
  131. const Array<File>& getResults() const;
  132. private:
  133. //==============================================================================
  134. String title, filters;
  135. File startingFile;
  136. Array<File> results;
  137. bool useNativeDialogBox;
  138. bool showDialog (bool selectsDirectories, bool selectsFiles, bool isSave,
  139. bool warnAboutOverwritingExistingFiles, bool selectMultipleFiles,
  140. FilePreviewComponent* previewComponent);
  141. static void showPlatformDialog (Array<File>& results, const String& title, const File& file,
  142. const String& filters, bool selectsDirectories, bool selectsFiles,
  143. bool isSave, bool warnAboutOverwritingExistingFiles, bool selectMultipleFiles,
  144. FilePreviewComponent* previewComponent);
  145. static bool isPlatformDialogAvailable();
  146. JUCE_LEAK_DETECTOR (FileChooser);
  147. };
  148. #endif // __JUCE_FILECHOOSER_JUCEHEADER__