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.

236 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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 "../../../io/files/juce_File.h"
  23. #include "../../../containers/juce_BitArray.h"
  24. #include "../controls/juce_TextEditor.h"
  25. #include "../controls/juce_ComboBox.h"
  26. #include "../buttons/juce_DrawableButton.h"
  27. //==============================================================================
  28. /**
  29. A component for browsing and selecting a file or directory to open or save.
  30. This contains a FileListComponent and adds various boxes and controls for
  31. navigating and selecting a file. It can work in different modes so that it can
  32. be used for loading or saving a file, or for choosing a directory.
  33. @see FileChooserDialogBox, FileChooser, FileListComponent
  34. */
  35. class JUCE_API FileBrowserComponent : public Component,
  36. public ChangeBroadcaster,
  37. private FileBrowserListener,
  38. private TextEditorListener,
  39. private ButtonListener,
  40. private ComboBoxListener,
  41. private FileFilter
  42. {
  43. public:
  44. //==============================================================================
  45. /** Various options for the browser.
  46. A combination of these is passed into the FileBrowserComponent constructor.
  47. */
  48. enum FileChooserFlags
  49. {
  50. openMode = 1, /**< specifies that the component should allow the user to
  51. choose an existing file with the intention of opening it. */
  52. saveMode = 2, /**< specifies that the component should allow the user to specify
  53. the name of a file that will be used to save something. */
  54. canSelectFiles = 4, /**< specifies that the user can select files (can be used in
  55. conjunction with canSelectDirectories). */
  56. canSelectDirectories = 8, /**< specifies that the user can select directories (can be used in
  57. conjuction with canSelectFiles). */
  58. canSelectMultipleItems = 16, /**< specifies that the user can select multiple items. */
  59. useTreeView = 32, /**< specifies that a tree-view should be shown instead of a file list. */
  60. filenameBoxIsReadOnly = 64 /**< specifies that the user can't type directly into the filename box. */
  61. };
  62. //==============================================================================
  63. /** Creates a FileBrowserComponent.
  64. @param flags A combination of flags from the FileChooserFlags enumeration,
  65. used to specify the component's behaviour. The flags must contain
  66. either openMode or saveMode, and canSelectFiles and/or
  67. canSelectDirectories.
  68. @param initialFileOrDirectory The file or directory that should be selected when
  69. the component begins. If this is File::nonexistent,
  70. a default directory will be chosen.
  71. @param fileFilter an optional filter to use to determine which files
  72. are shown. If this is 0 then all files are displayed. Note
  73. that a pointer is kept internally to this object, so
  74. make sure that it is not deleted before the browser object
  75. is deleted.
  76. @param previewComp an optional preview component that will be used to
  77. show previews of files that the user selects
  78. */
  79. FileBrowserComponent (int flags,
  80. const File& initialFileOrDirectory,
  81. const FileFilter* fileFilter,
  82. FilePreviewComponent* previewComp);
  83. /** Destructor. */
  84. ~FileBrowserComponent();
  85. //==============================================================================
  86. /** Returns the number of files that the user has got selected.
  87. If multiple select isn't active, this will only be 0 or 1. To get the complete
  88. list of files they've chosen, pass an index to getCurrentFile().
  89. */
  90. int getNumSelectedFiles() const throw();
  91. /** Returns one of the files that the user has chosen.
  92. If the box has multi-select enabled, the index lets you specify which of the files
  93. to get - see getNumSelectedFiles() to find out how many files were chosen.
  94. @see getHighlightedFile
  95. */
  96. const File getSelectedFile (int index) const throw();
  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 getCurrentFile
  109. */
  110. const File getHighlightedFile() const throw();
  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. /** Returns a verb to describe what should happen when the file is accepted.
  121. E.g. if browsing in "load file" mode, this will be "Open", if in "save file"
  122. mode, it'll be "Save", etc.
  123. */
  124. virtual const String getActionVerb() const;
  125. /** Returns true if the saveMode flag was set when this component was created.
  126. */
  127. bool isSaveMode() const throw();
  128. //==============================================================================
  129. /** Adds a listener to be told when the user selects and clicks on files.
  130. @see removeListener
  131. */
  132. void addListener (FileBrowserListener* const listener) throw();
  133. /** Removes a listener.
  134. @see addListener
  135. */
  136. void removeListener (FileBrowserListener* const listener) throw();
  137. //==============================================================================
  138. /** @internal */
  139. void resized();
  140. /** @internal */
  141. void buttonClicked (Button* b);
  142. /** @internal */
  143. void comboBoxChanged (ComboBox*);
  144. /** @internal */
  145. void textEditorTextChanged (TextEditor& editor);
  146. /** @internal */
  147. void textEditorReturnKeyPressed (TextEditor& editor);
  148. /** @internal */
  149. void textEditorEscapeKeyPressed (TextEditor& editor);
  150. /** @internal */
  151. void textEditorFocusLost (TextEditor& editor);
  152. /** @internal */
  153. bool keyPressed (const KeyPress& key);
  154. /** @internal */
  155. void selectionChanged();
  156. /** @internal */
  157. void fileClicked (const File& f, const MouseEvent& e);
  158. /** @internal */
  159. void fileDoubleClicked (const File& f);
  160. /** @internal */
  161. bool isFileSuitable (const File& file) const;
  162. /** @internal */
  163. bool isDirectorySuitable (const File&) const;
  164. /** @internal */
  165. FilePreviewComponent* getPreviewComponent() const throw();
  166. juce_UseDebuggingNewOperator
  167. protected:
  168. virtual const BitArray getRoots (StringArray& rootNames, StringArray& rootPaths);
  169. private:
  170. //==============================================================================
  171. ScopedPointer <DirectoryContentsList> fileList;
  172. const FileFilter* fileFilter;
  173. int flags;
  174. File currentRoot;
  175. OwnedArray <File> chosenFiles;
  176. SortedSet <void*> listeners;
  177. DirectoryContentsDisplayComponent* fileListComponent;
  178. FilePreviewComponent* previewComp;
  179. ComboBox* currentPathBox;
  180. TextEditor* filenameBox;
  181. Button* goUpButton;
  182. TimeSliceThread thread;
  183. void sendListenerChangeMessage();
  184. bool isFileOrDirSuitable (const File& f) const;
  185. FileBrowserComponent (const FileBrowserComponent&);
  186. const FileBrowserComponent& operator= (const FileBrowserComponent&);
  187. };
  188. #endif // __JUCE_FILEBROWSERCOMPONENT_JUCEHEADER__