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.

261 lines
11KB

  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_FILEBROWSERCOMPONENT_JUCEHEADER__
  18. #define __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__
  19. #include "juce_DirectoryContentsDisplayComponent.h"
  20. #include "juce_FilePreviewComponent.h"
  21. #include "../widgets/juce_TextEditor.h"
  22. #include "../widgets/juce_ComboBox.h"
  23. #include "../buttons/juce_DrawableButton.h"
  24. //==============================================================================
  25. /**
  26. A component for browsing and selecting a file or directory to open or save.
  27. This contains a FileListComponent and adds various boxes and controls for
  28. navigating and selecting a file. It can work in different modes so that it can
  29. be used for loading or saving a file, or for choosing a directory.
  30. @see FileChooserDialogBox, FileChooser, FileListComponent
  31. */
  32. class JUCE_API FileBrowserComponent : public Component,
  33. private FileBrowserListener,
  34. private TextEditorListener,
  35. private ButtonListener,
  36. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  37. private FileFilter
  38. {
  39. public:
  40. //==============================================================================
  41. /** Various options for the browser.
  42. A combination of these is passed into the FileBrowserComponent constructor.
  43. */
  44. enum FileChooserFlags
  45. {
  46. openMode = 1, /**< specifies that the component should allow the user to
  47. choose an existing file with the intention of opening it. */
  48. saveMode = 2, /**< specifies that the component should allow the user to specify
  49. the name of a file that will be used to save something. */
  50. canSelectFiles = 4, /**< specifies that the user can select files (can be used in
  51. conjunction with canSelectDirectories). */
  52. canSelectDirectories = 8, /**< specifies that the user can select directories (can be used in
  53. conjuction with canSelectFiles). */
  54. canSelectMultipleItems = 16, /**< specifies that the user can select multiple items. */
  55. useTreeView = 32, /**< specifies that a tree-view should be shown instead of a file list. */
  56. filenameBoxIsReadOnly = 64, /**< specifies that the user can't type directly into the filename box. */
  57. warnAboutOverwriting = 128 /**< specifies that the dialog should warn about overwriting existing files (if possible). */
  58. };
  59. //==============================================================================
  60. /** Creates a FileBrowserComponent.
  61. @param flags A combination of flags from the FileChooserFlags enumeration, used to
  62. specify the component's behaviour. The flags must contain either openMode
  63. or saveMode, and canSelectFiles and/or canSelectDirectories.
  64. @param initialFileOrDirectory The file or directory that should be selected when the component begins.
  65. If this is File::nonexistent, a default directory will be chosen.
  66. @param fileFilter an optional filter to use to determine which files are shown.
  67. If this is nullptr then all files are displayed. Note that a pointer
  68. is kept internally to this object, so make sure that it is not deleted
  69. before the FileBrowserComponent object is deleted.
  70. @param previewComp an optional preview component that will be used to show previews of
  71. files that the user selects
  72. */
  73. FileBrowserComponent (int flags,
  74. const File& initialFileOrDirectory,
  75. const FileFilter* fileFilter,
  76. FilePreviewComponent* previewComp);
  77. /** Destructor. */
  78. ~FileBrowserComponent();
  79. //==============================================================================
  80. /** Returns the number of files that the user has got selected.
  81. If multiple select isn't active, this will only be 0 or 1. To get the complete
  82. list of files they've chosen, pass an index to getCurrentFile().
  83. */
  84. int getNumSelectedFiles() const noexcept;
  85. /** Returns one of the files that the user has chosen.
  86. If the box has multi-select enabled, the index lets you specify which of the files
  87. to get - see getNumSelectedFiles() to find out how many files were chosen.
  88. @see getHighlightedFile
  89. */
  90. File getSelectedFile (int index) const noexcept;
  91. /** Deselects any files that are currently selected.
  92. */
  93. void deselectAllFiles();
  94. /** Returns true if the currently selected file(s) are usable.
  95. This can be used to decide whether the user can press "ok" for the
  96. current file. What it does depends on the mode, so for example in an "open"
  97. mode, this only returns true if a file has been selected and if it exists.
  98. In a "save" mode, a non-existent file would also be valid.
  99. */
  100. bool currentFileIsValid() const;
  101. /** This returns the last item in the view that the user has highlighted.
  102. This may be different from getCurrentFile(), which returns the value
  103. that is shown in the filename box, and if there are multiple selections,
  104. this will only return one of them.
  105. @see getSelectedFile
  106. */
  107. File getHighlightedFile() const noexcept;
  108. //==============================================================================
  109. /** Returns the directory whose contents are currently being shown in the listbox. */
  110. const File& getRoot() const;
  111. /** Changes the directory that's being shown in the listbox. */
  112. void setRoot (const File& newRootDirectory);
  113. /** Changes the name that is currently shown in the filename box. */
  114. void setFileName (const String& newName);
  115. /** Equivalent to pressing the "up" button to browse the parent directory. */
  116. void goUp();
  117. /** Refreshes the directory that's currently being listed. */
  118. void refresh();
  119. /** Changes the filter that's being used to sift the files. */
  120. void setFileFilter (const FileFilter* newFileFilter);
  121. /** Returns a verb to describe what should happen when the file is accepted.
  122. E.g. if browsing in "load file" mode, this will be "Open", if in "save file"
  123. mode, it'll be "Save", etc.
  124. */
  125. virtual String getActionVerb() const;
  126. /** Returns true if the saveMode flag was set when this component was created.
  127. */
  128. bool isSaveMode() const noexcept;
  129. /** Sets the label that will be displayed next to the filename entry box.
  130. By default this is just "file", but you might want to change it to something more
  131. appropriate for your app.
  132. */
  133. void setFilenameBoxLabel (const String& name);
  134. //==============================================================================
  135. /** Adds a listener to be told when the user selects and clicks on files.
  136. @see removeListener
  137. */
  138. void addListener (FileBrowserListener* listener);
  139. /** Removes a listener.
  140. @see addListener
  141. */
  142. void removeListener (FileBrowserListener* listener);
  143. /** Returns a platform-specific list of names and paths for some suggested places the user
  144. might want to use as root folders.
  145. The list returned contains empty strings to indicate section breaks.
  146. @see getRoots()
  147. */
  148. static void getDefaultRoots (StringArray& rootNames, StringArray& rootPaths);
  149. //==============================================================================
  150. /** @internal */
  151. void resized() override;
  152. /** @internal */
  153. void buttonClicked (Button*) override;
  154. /** @internal */
  155. void comboBoxChanged (ComboBox*) override;
  156. /** @internal */
  157. void textEditorTextChanged (TextEditor&) override;
  158. /** @internal */
  159. void textEditorReturnKeyPressed (TextEditor&) override;
  160. /** @internal */
  161. void textEditorEscapeKeyPressed (TextEditor&) override;
  162. /** @internal */
  163. void textEditorFocusLost (TextEditor&) override;
  164. /** @internal */
  165. bool keyPressed (const KeyPress&) override;
  166. /** @internal */
  167. void selectionChanged() override;
  168. /** @internal */
  169. void fileClicked (const File&, const MouseEvent&) override;
  170. /** @internal */
  171. void fileDoubleClicked (const File&) override;
  172. /** @internal */
  173. void browserRootChanged (const File&) override;
  174. /** @internal */
  175. bool isFileSuitable (const File&) const override;
  176. /** @internal */
  177. bool isDirectorySuitable (const File&) const override;
  178. /** @internal */
  179. FilePreviewComponent* getPreviewComponent() const noexcept;
  180. /** @internal */
  181. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  182. protected:
  183. /** Returns a list of names and paths for the default places the user might want to look.
  184. By default this just calls getDefaultRoots(), but you may want to override it to
  185. return a custom list.
  186. */
  187. virtual void getRoots (StringArray& rootNames, StringArray& rootPaths);
  188. /** Updates the items in the dropdown list of recent paths with the values from getRoots(). */
  189. void resetRecentPaths();
  190. private:
  191. //==============================================================================
  192. ScopedPointer <DirectoryContentsList> fileList;
  193. const FileFilter* fileFilter;
  194. int flags;
  195. File currentRoot;
  196. Array<File> chosenFiles;
  197. ListenerList <FileBrowserListener> listeners;
  198. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  199. FilePreviewComponent* previewComp;
  200. ComboBox currentPathBox;
  201. TextEditor filenameBox;
  202. Label fileLabel;
  203. ScopedPointer<Button> goUpButton;
  204. TimeSliceThread thread;
  205. void sendListenerChangeMessage();
  206. bool isFileOrDirSuitable (const File& f) const;
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent)
  208. };
  209. #endif // __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__