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.

253 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__
  19. #define __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__
  20. #include "juce_DirectoryContentsDisplayComponent.h"
  21. #include "juce_FilePreviewComponent.h"
  22. #include "../widgets/juce_TextEditor.h"
  23. #include "../widgets/juce_ComboBox.h"
  24. #include "../buttons/juce_DrawableButton.h"
  25. //==============================================================================
  26. /**
  27. A component for browsing and selecting a file or directory to open or save.
  28. This contains a FileListComponent and adds various boxes and controls for
  29. navigating and selecting a file. It can work in different modes so that it can
  30. be used for loading or saving a file, or for choosing a directory.
  31. @see FileChooserDialogBox, FileChooser, FileListComponent
  32. */
  33. class JUCE_API FileBrowserComponent : public Component,
  34. private FileBrowserListener,
  35. private TextEditorListener,
  36. private ButtonListener,
  37. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  38. private FileFilter
  39. {
  40. public:
  41. //==============================================================================
  42. /** Various options for the browser.
  43. A combination of these is passed into the FileBrowserComponent constructor.
  44. */
  45. enum FileChooserFlags
  46. {
  47. openMode = 1, /**< specifies that the component should allow the user to
  48. choose an existing file with the intention of opening it. */
  49. saveMode = 2, /**< specifies that the component should allow the user to specify
  50. the name of a file that will be used to save something. */
  51. canSelectFiles = 4, /**< specifies that the user can select files (can be used in
  52. conjunction with canSelectDirectories). */
  53. canSelectDirectories = 8, /**< specifies that the user can select directories (can be used in
  54. conjuction with canSelectFiles). */
  55. canSelectMultipleItems = 16, /**< specifies that the user can select multiple items. */
  56. useTreeView = 32, /**< specifies that a tree-view should be shown instead of a file list. */
  57. filenameBoxIsReadOnly = 64 /**< specifies that the user can't type directly into the filename box. */
  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. //==============================================================================
  144. /** @internal */
  145. void resized();
  146. /** @internal */
  147. void buttonClicked (Button*);
  148. /** @internal */
  149. void comboBoxChanged (ComboBox*);
  150. /** @internal */
  151. void textEditorTextChanged (TextEditor&);
  152. /** @internal */
  153. void textEditorReturnKeyPressed (TextEditor&);
  154. /** @internal */
  155. void textEditorEscapeKeyPressed (TextEditor&);
  156. /** @internal */
  157. void textEditorFocusLost (TextEditor&);
  158. /** @internal */
  159. bool keyPressed (const KeyPress&);
  160. /** @internal */
  161. void selectionChanged();
  162. /** @internal */
  163. void fileClicked (const File&, const MouseEvent&);
  164. /** @internal */
  165. void fileDoubleClicked (const File&);
  166. /** @internal */
  167. void browserRootChanged (const File&);
  168. /** @internal */
  169. bool isFileSuitable (const File&) const;
  170. /** @internal */
  171. bool isDirectorySuitable (const File&) const;
  172. /** @internal */
  173. FilePreviewComponent* getPreviewComponent() const noexcept;
  174. /** @internal */
  175. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  176. protected:
  177. /** Returns a list of names and paths for the default places the user might want to look.
  178. Use an empty string to indicate a section break.
  179. */
  180. virtual void getRoots (StringArray& rootNames, StringArray& rootPaths);
  181. /** Updates the items in the dropdown list of recent paths with the values from getRoots(). */
  182. void resetRecentPaths();
  183. private:
  184. //==============================================================================
  185. ScopedPointer <DirectoryContentsList> fileList;
  186. const FileFilter* fileFilter;
  187. int flags;
  188. File currentRoot;
  189. Array<File> chosenFiles;
  190. ListenerList <FileBrowserListener> listeners;
  191. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  192. FilePreviewComponent* previewComp;
  193. ComboBox currentPathBox;
  194. TextEditor filenameBox;
  195. Label fileLabel;
  196. ScopedPointer<Button> goUpButton;
  197. TimeSliceThread thread;
  198. void sendListenerChangeMessage();
  199. bool isFileOrDirSuitable (const File& f) const;
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent);
  201. };
  202. #endif // __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__