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.

296 lines
14KB

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