Audio plugin host https://kx.studio/carla
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.

juce_FileBrowserComponent.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A component for browsing and selecting a file or directory to open or save.
  24. This contains a FileListComponent and adds various boxes and controls for
  25. navigating and selecting a file. It can work in different modes so that it can
  26. be used for loading or saving a file, or for choosing a directory.
  27. @see FileChooserDialogBox, FileChooser, FileListComponent
  28. */
  29. class JUCE_API FileBrowserComponent : public Component,
  30. private FileBrowserListener,
  31. private TextEditor::Listener,
  32. private Button::Listener,
  33. private ComboBox::Listener,
  34. private FileFilter,
  35. private Timer
  36. {
  37. public:
  38. //==============================================================================
  39. /** Various options for the browser.
  40. A combination of these is passed into the FileBrowserComponent constructor.
  41. */
  42. enum FileChooserFlags
  43. {
  44. openMode = 1, /**< specifies that the component should allow the user to
  45. choose an existing file with the intention of opening it. */
  46. saveMode = 2, /**< specifies that the component should allow the user to specify
  47. the name of a file that will be used to save something. */
  48. canSelectFiles = 4, /**< specifies that the user can select files (can be used in
  49. conjunction with canSelectDirectories). */
  50. canSelectDirectories = 8, /**< specifies that the user can select directories (can be used in
  51. conjunction with canSelectFiles). */
  52. canSelectMultipleItems = 16, /**< specifies that the user can select multiple items. */
  53. useTreeView = 32, /**< specifies that a tree-view should be shown instead of a file list. */
  54. filenameBoxIsReadOnly = 64, /**< specifies that the user can't type directly into the filename box. */
  55. warnAboutOverwriting = 128, /**< specifies that the dialog should warn about overwriting existing files (if possible). */
  56. doNotClearFileNameOnRootChange = 256 /**< specifies that the file name should not be cleared upon root change. */
  57. };
  58. //==============================================================================
  59. /** Creates a FileBrowserComponent.
  60. @param flags A combination of flags from the FileChooserFlags enumeration, used to
  61. specify the component's behaviour. The flags must contain either openMode
  62. or saveMode, and canSelectFiles and/or canSelectDirectories.
  63. @param initialFileOrDirectory The file or directory that should be selected when the component begins.
  64. If this is File(), a default directory will be chosen.
  65. @param fileFilter an optional filter to use to determine which files are shown.
  66. If this is nullptr then all files are displayed. Note that a pointer
  67. is kept internally to this object, so make sure that it is not deleted
  68. before the FileBrowserComponent object is deleted.
  69. @param previewComp an optional preview component that will be used to show previews of
  70. files that the user selects
  71. */
  72. FileBrowserComponent (int flags,
  73. const File& initialFileOrDirectory,
  74. const FileFilter* fileFilter,
  75. FilePreviewComponent* previewComp);
  76. /** Destructor. */
  77. ~FileBrowserComponent();
  78. //==============================================================================
  79. /** Returns the number of files that the user has got selected.
  80. If multiple select isn't active, this will only be 0 or 1. To get the complete
  81. list of files they've chosen, pass an index to getCurrentFile().
  82. */
  83. int getNumSelectedFiles() const noexcept;
  84. /** Returns one of the files that the user has chosen.
  85. If the box has multi-select enabled, the index lets you specify which of the files
  86. to get - see getNumSelectedFiles() to find out how many files were chosen.
  87. @see getHighlightedFile
  88. */
  89. File getSelectedFile (int index) const noexcept;
  90. /** Deselects any files that are currently selected. */
  91. void deselectAllFiles();
  92. /** Returns true if the currently selected file(s) are usable.
  93. This can be used to decide whether the user can press "ok" for the
  94. current file. What it does depends on the mode, so for example in an "open"
  95. mode, this only returns true if a file has been selected and if it exists.
  96. In a "save" mode, a non-existent file would also be valid.
  97. */
  98. bool currentFileIsValid() const;
  99. /** This returns the last item in the view that the user has highlighted.
  100. This may be different from getCurrentFile(), which returns the value
  101. that is shown in the filename box, and if there are multiple selections,
  102. this will only return one of them.
  103. @see getSelectedFile
  104. */
  105. File getHighlightedFile() const noexcept;
  106. //==============================================================================
  107. /** Returns the directory whose contents are currently being shown in the listbox. */
  108. const File& getRoot() const;
  109. /** Changes the directory that's being shown in the listbox. */
  110. void setRoot (const File& newRootDirectory);
  111. /** Changes the name that is currently shown in the filename box. */
  112. void setFileName (const String& newName);
  113. /** Equivalent to pressing the "up" button to browse the parent directory. */
  114. void goUp();
  115. /** Refreshes the directory that's currently being listed. */
  116. void refresh();
  117. /** Changes the filter that's being used to sift the files. */
  118. void setFileFilter (const FileFilter* newFileFilter);
  119. /** Returns a verb to describe what should happen when the file is accepted.
  120. E.g. if browsing in "load file" mode, this will be "Open", if in "save file"
  121. mode, it'll be "Save", etc.
  122. */
  123. virtual String getActionVerb() const;
  124. /** Returns true if the saveMode flag was set when this component was created. */
  125. bool isSaveMode() const noexcept;
  126. /** Sets the label that will be displayed next to the filename entry box.
  127. By default this is just "file", but you might want to change it to something more
  128. appropriate for your app.
  129. */
  130. void setFilenameBoxLabel (const String& name);
  131. //==============================================================================
  132. /** Adds a listener to be told when the user selects and clicks on files.
  133. @see removeListener
  134. */
  135. void addListener (FileBrowserListener* listener);
  136. /** Removes a listener.
  137. @see addListener
  138. */
  139. void removeListener (FileBrowserListener* listener);
  140. /** Returns a platform-specific list of names and paths for some suggested places the user
  141. might want to use as root folders.
  142. The list returned contains empty strings to indicate section breaks.
  143. @see getRoots()
  144. */
  145. static void getDefaultRoots (StringArray& rootNames, StringArray& rootPaths);
  146. //==============================================================================
  147. /** This abstract base class is implemented by LookAndFeel classes to provide
  148. various file-browser layout and drawing methods.
  149. */
  150. struct JUCE_API LookAndFeelMethods
  151. {
  152. virtual ~LookAndFeelMethods() {}
  153. // These return a pointer to an internally cached drawable - make sure you don't keep
  154. // a copy of this pointer anywhere, as it may become invalid in the future.
  155. virtual const Drawable* getDefaultFolderImage() = 0;
  156. virtual const Drawable* getDefaultDocumentFileImage() = 0;
  157. virtual AttributedString createFileChooserHeaderText (const String& title,
  158. const String& instructions) = 0;
  159. virtual void drawFileBrowserRow (Graphics&, int width, int height,
  160. const File& file,
  161. const String& filename,
  162. Image* optionalIcon,
  163. const String& fileSizeDescription,
  164. const String& fileTimeDescription,
  165. bool isDirectory,
  166. bool isItemSelected,
  167. int itemIndex,
  168. DirectoryContentsDisplayComponent&) = 0;
  169. virtual Button* createFileBrowserGoUpButton() = 0;
  170. virtual void layoutFileBrowserComponent (FileBrowserComponent& browserComp,
  171. DirectoryContentsDisplayComponent* fileListComponent,
  172. FilePreviewComponent* previewComp,
  173. ComboBox* currentPathBox,
  174. TextEditor* filenameBox,
  175. Button* goUpButton) = 0;
  176. };
  177. //==============================================================================
  178. /** @internal */
  179. void resized() override;
  180. /** @internal */
  181. void buttonClicked (Button*) override;
  182. /** @internal */
  183. void comboBoxChanged (ComboBox*) override;
  184. /** @internal */
  185. void textEditorTextChanged (TextEditor&) override;
  186. /** @internal */
  187. void textEditorReturnKeyPressed (TextEditor&) override;
  188. /** @internal */
  189. void textEditorEscapeKeyPressed (TextEditor&) override;
  190. /** @internal */
  191. void textEditorFocusLost (TextEditor&) override;
  192. /** @internal */
  193. bool keyPressed (const KeyPress&) override;
  194. /** @internal */
  195. void selectionChanged() override;
  196. /** @internal */
  197. void fileClicked (const File&, const MouseEvent&) override;
  198. /** @internal */
  199. void fileDoubleClicked (const File&) override;
  200. /** @internal */
  201. void browserRootChanged (const File&) override;
  202. /** @internal */
  203. bool isFileSuitable (const File&) const override;
  204. /** @internal */
  205. bool isDirectorySuitable (const File&) const override;
  206. /** @internal */
  207. FilePreviewComponent* getPreviewComponent() const noexcept;
  208. /** @internal */
  209. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  210. protected:
  211. /** Returns a list of names and paths for the default places the user might want to look.
  212. By default this just calls getDefaultRoots(), but you may want to override it to
  213. return a custom list.
  214. */
  215. virtual void getRoots (StringArray& rootNames, StringArray& rootPaths);
  216. /** Updates the items in the dropdown list of recent paths with the values from getRoots(). */
  217. void resetRecentPaths();
  218. private:
  219. //==============================================================================
  220. ScopedPointer<DirectoryContentsList> fileList;
  221. const FileFilter* fileFilter;
  222. int flags;
  223. File currentRoot;
  224. Array<File> chosenFiles;
  225. ListenerList<FileBrowserListener> listeners;
  226. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  227. FilePreviewComponent* previewComp;
  228. ComboBox currentPathBox;
  229. TextEditor filenameBox;
  230. Label fileLabel;
  231. ScopedPointer<Button> goUpButton;
  232. TimeSliceThread thread;
  233. bool wasProcessActive;
  234. void timerCallback() override;
  235. void sendListenerChangeMessage();
  236. bool isFileOrDirSuitable (const File&) const;
  237. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent)
  238. };
  239. } // namespace juce