juce_FileBrowserComponent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_H_INCLUDED
  18. #define JUCE_FILEBROWSERCOMPONENT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A component for browsing and selecting a file or directory to open or save.
  22. This contains a FileListComponent and adds various boxes and controls for
  23. navigating and selecting a file. It can work in different modes so that it can
  24. be used for loading or saving a file, or for choosing a directory.
  25. @see FileChooserDialogBox, FileChooser, FileListComponent
  26. */
  27. class JUCE_API FileBrowserComponent : public Component,
  28. private FileBrowserListener,
  29. private TextEditorListener,
  30. private ButtonListener,
  31. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  32. private FileFilter
  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. conjuction 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. };
  54. //==============================================================================
  55. /** Creates a FileBrowserComponent.
  56. @param flags A combination of flags from the FileChooserFlags enumeration, used to
  57. specify the component's behaviour. The flags must contain either openMode
  58. or saveMode, and canSelectFiles and/or canSelectDirectories.
  59. @param initialFileOrDirectory The file or directory that should be selected when the component begins.
  60. If this is File::nonexistent, a default directory will be chosen.
  61. @param fileFilter an optional filter to use to determine which files are shown.
  62. If this is nullptr then all files are displayed. Note that a pointer
  63. is kept internally to this object, so make sure that it is not deleted
  64. before the FileBrowserComponent object is deleted.
  65. @param previewComp an optional preview component that will be used to show previews of
  66. files that the user selects
  67. */
  68. FileBrowserComponent (int flags,
  69. const File& initialFileOrDirectory,
  70. const FileFilter* fileFilter,
  71. FilePreviewComponent* previewComp);
  72. /** Destructor. */
  73. ~FileBrowserComponent();
  74. //==============================================================================
  75. /** Returns the number of files that the user has got selected.
  76. If multiple select isn't active, this will only be 0 or 1. To get the complete
  77. list of files they've chosen, pass an index to getCurrentFile().
  78. */
  79. int getNumSelectedFiles() const noexcept;
  80. /** Returns one of the files that the user has chosen.
  81. If the box has multi-select enabled, the index lets you specify which of the files
  82. to get - see getNumSelectedFiles() to find out how many files were chosen.
  83. @see getHighlightedFile
  84. */
  85. File getSelectedFile (int index) const noexcept;
  86. /** Deselects any files that are currently selected.
  87. */
  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. */
  123. bool isSaveMode() const noexcept;
  124. /** Sets the label that will be displayed next to the filename entry box.
  125. By default this is just "file", but you might want to change it to something more
  126. appropriate for your app.
  127. */
  128. void setFilenameBoxLabel (const String& name);
  129. //==============================================================================
  130. /** Adds a listener to be told when the user selects and clicks on files.
  131. @see removeListener
  132. */
  133. void addListener (FileBrowserListener* listener);
  134. /** Removes a listener.
  135. @see addListener
  136. */
  137. void removeListener (FileBrowserListener* listener);
  138. /** Returns a platform-specific list of names and paths for some suggested places the user
  139. might want to use as root folders.
  140. The list returned contains empty strings to indicate section breaks.
  141. @see getRoots()
  142. */
  143. static void getDefaultRoots (StringArray& rootNames, StringArray& rootPaths);
  144. //==============================================================================
  145. /** This abstract base class is implemented by LookAndFeel classes to provide
  146. various file-browser layout and drawing methods.
  147. */
  148. struct JUCE_API LookAndFeelMethods
  149. {
  150. virtual ~LookAndFeelMethods() {}
  151. // These return a pointer to an internally cached drawable - make sure you don't keep
  152. // a copy of this pointer anywhere, as it may become invalid in the future.
  153. virtual const Drawable* getDefaultFolderImage() = 0;
  154. virtual const Drawable* getDefaultDocumentFileImage() = 0;
  155. virtual AttributedString createFileChooserHeaderText (const String& title,
  156. const String& instructions) = 0;
  157. virtual void drawFileBrowserRow (Graphics&, int width, int height,
  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. //==============================================================================
  175. /** @internal */
  176. void resized() override;
  177. /** @internal */
  178. void buttonClicked (Button*) override;
  179. /** @internal */
  180. void comboBoxChanged (ComboBox*) override;
  181. /** @internal */
  182. void textEditorTextChanged (TextEditor&) override;
  183. /** @internal */
  184. void textEditorReturnKeyPressed (TextEditor&) override;
  185. /** @internal */
  186. void textEditorEscapeKeyPressed (TextEditor&) override;
  187. /** @internal */
  188. void textEditorFocusLost (TextEditor&) override;
  189. /** @internal */
  190. bool keyPressed (const KeyPress&) override;
  191. /** @internal */
  192. void selectionChanged() override;
  193. /** @internal */
  194. void fileClicked (const File&, const MouseEvent&) override;
  195. /** @internal */
  196. void fileDoubleClicked (const File&) override;
  197. /** @internal */
  198. void browserRootChanged (const File&) override;
  199. /** @internal */
  200. bool isFileSuitable (const File&) const override;
  201. /** @internal */
  202. bool isDirectorySuitable (const File&) const override;
  203. /** @internal */
  204. FilePreviewComponent* getPreviewComponent() const noexcept;
  205. /** @internal */
  206. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  207. protected:
  208. /** Returns a list of names and paths for the default places the user might want to look.
  209. By default this just calls getDefaultRoots(), but you may want to override it to
  210. return a custom list.
  211. */
  212. virtual void getRoots (StringArray& rootNames, StringArray& rootPaths);
  213. /** Updates the items in the dropdown list of recent paths with the values from getRoots(). */
  214. void resetRecentPaths();
  215. private:
  216. //==============================================================================
  217. ScopedPointer <DirectoryContentsList> fileList;
  218. const FileFilter* fileFilter;
  219. int flags;
  220. File currentRoot;
  221. Array<File> chosenFiles;
  222. ListenerList <FileBrowserListener> listeners;
  223. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  224. FilePreviewComponent* previewComp;
  225. ComboBox currentPathBox;
  226. TextEditor filenameBox;
  227. Label fileLabel;
  228. ScopedPointer<Button> goUpButton;
  229. TimeSliceThread thread;
  230. void sendListenerChangeMessage();
  231. bool isFileOrDirSuitable (const File& f) const;
  232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent)
  233. };
  234. #endif // JUCE_FILEBROWSERCOMPONENT_H_INCLUDED