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.

249 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,
  62. used to specify the component's behaviour. The flags must contain
  63. either openMode or saveMode, and canSelectFiles and/or
  64. canSelectDirectories.
  65. @param initialFileOrDirectory The file or directory that should be selected when
  66. the component begins. If this is File::nonexistent,
  67. a default directory will be chosen.
  68. @param fileFilter an optional filter to use to determine which files
  69. are shown. If this is 0 then all files are displayed. Note
  70. that a pointer is kept internally to this object, so
  71. make sure that it is not deleted before the browser object
  72. is deleted.
  73. @param previewComp an optional preview component that will be used to
  74. show previews of files that the user selects
  75. */
  76. FileBrowserComponent (int flags,
  77. const File& initialFileOrDirectory,
  78. const FileFilter* fileFilter,
  79. FilePreviewComponent* previewComp);
  80. /** Destructor. */
  81. ~FileBrowserComponent();
  82. //==============================================================================
  83. /** Returns the number of files that the user has got selected.
  84. If multiple select isn't active, this will only be 0 or 1. To get the complete
  85. list of files they've chosen, pass an index to getCurrentFile().
  86. */
  87. int getNumSelectedFiles() const noexcept;
  88. /** Returns one of the files that the user has chosen.
  89. If the box has multi-select enabled, the index lets you specify which of the files
  90. to get - see getNumSelectedFiles() to find out how many files were chosen.
  91. @see getHighlightedFile
  92. */
  93. File getSelectedFile (int index) const noexcept;
  94. /** Deselects any files that are currently selected.
  95. */
  96. void deselectAllFiles();
  97. /** Returns true if the currently selected file(s) are usable.
  98. This can be used to decide whether the user can press "ok" for the
  99. current file. What it does depends on the mode, so for example in an "open"
  100. mode, this only returns true if a file has been selected and if it exists.
  101. In a "save" mode, a non-existent file would also be valid.
  102. */
  103. bool currentFileIsValid() const;
  104. /** This returns the last item in the view that the user has highlighted.
  105. This may be different from getCurrentFile(), which returns the value
  106. that is shown in the filename box, and if there are multiple selections,
  107. this will only return one of them.
  108. @see getSelectedFile
  109. */
  110. File getHighlightedFile() const noexcept;
  111. //==============================================================================
  112. /** Returns the directory whose contents are currently being shown in the listbox. */
  113. const File& getRoot() const;
  114. /** Changes the directory that's being shown in the listbox. */
  115. void setRoot (const File& newRootDirectory);
  116. /** Equivalent to pressing the "up" button to browse the parent directory. */
  117. void goUp();
  118. /** Refreshes the directory that's currently being listed. */
  119. void refresh();
  120. /** Changes the filter that's being used to sift the files. */
  121. void setFileFilter (const FileFilter* newFileFilter);
  122. /** Returns a verb to describe what should happen when the file is accepted.
  123. E.g. if browsing in "load file" mode, this will be "Open", if in "save file"
  124. mode, it'll be "Save", etc.
  125. */
  126. virtual String getActionVerb() const;
  127. /** Returns true if the saveMode flag was set when this component was created.
  128. */
  129. bool isSaveMode() const noexcept;
  130. //==============================================================================
  131. /** Adds a listener to be told when the user selects and clicks on files.
  132. @see removeListener
  133. */
  134. void addListener (FileBrowserListener* listener);
  135. /** Removes a listener.
  136. @see addListener
  137. */
  138. void removeListener (FileBrowserListener* listener);
  139. //==============================================================================
  140. /** @internal */
  141. void resized();
  142. /** @internal */
  143. void buttonClicked (Button*);
  144. /** @internal */
  145. void comboBoxChanged (ComboBox*);
  146. /** @internal */
  147. void textEditorTextChanged (TextEditor&);
  148. /** @internal */
  149. void textEditorReturnKeyPressed (TextEditor&);
  150. /** @internal */
  151. void textEditorEscapeKeyPressed (TextEditor&);
  152. /** @internal */
  153. void textEditorFocusLost (TextEditor&);
  154. /** @internal */
  155. bool keyPressed (const KeyPress&);
  156. /** @internal */
  157. void selectionChanged();
  158. /** @internal */
  159. void fileClicked (const File&, const MouseEvent&);
  160. /** @internal */
  161. void fileDoubleClicked (const File&);
  162. /** @internal */
  163. void browserRootChanged (const File&);
  164. /** @internal */
  165. bool isFileSuitable (const File&) const;
  166. /** @internal */
  167. bool isDirectorySuitable (const File&) const;
  168. /** @internal */
  169. FilePreviewComponent* getPreviewComponent() const noexcept;
  170. /** @internal */
  171. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  172. protected:
  173. /** Returns a list of names and paths for the default places the user might want to look.
  174. Use an empty string to indicate a section break.
  175. */
  176. virtual void getRoots (StringArray& rootNames, StringArray& rootPaths);
  177. /** Updates the items in the dropdown list of recent paths with the values from getRoots(). */
  178. void resetRecentPaths();
  179. private:
  180. //==============================================================================
  181. ScopedPointer <DirectoryContentsList> fileList;
  182. const FileFilter* fileFilter;
  183. int flags;
  184. File currentRoot;
  185. Array<File> chosenFiles;
  186. ListenerList <FileBrowserListener> listeners;
  187. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  188. FilePreviewComponent* previewComp;
  189. ComboBox currentPathBox;
  190. TextEditor filenameBox;
  191. Label fileLabel;
  192. ScopedPointer<Button> goUpButton;
  193. TimeSliceThread thread;
  194. void sendListenerChangeMessage();
  195. bool isFileOrDirSuitable (const File& f) const;
  196. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent);
  197. };
  198. #endif // __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__