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.

289 lines
14KB

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