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.

220 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_BASICFILEBROWSER_H__
  25. #define __DROWAUDIO_BASICFILEBROWSER_H__
  26. //==================================================================================
  27. /** A BasicFileBrowser with an optional corner resizer.
  28. This is very similar to a FileBrowserComponent expect it does not have the file
  29. list box, go up button etc.
  30. */
  31. class BasicFileBrowser : public Component,
  32. private FileBrowserListener,
  33. private FileFilter
  34. {
  35. public:
  36. //==============================================================================
  37. /** Various options for the browser.
  38. A combination of these is passed into the FileBrowserComponent constructor.
  39. */
  40. enum FileChooserFlags
  41. {
  42. openMode = 1, /**< specifies that the component should allow the user to
  43. choose an existing file with the intention of opening it. */
  44. saveMode = 2, /**< specifies that the component should allow the user to specify
  45. the name of a file that will be used to save something. */
  46. canSelectFiles = 4, /**< specifies that the user can select files (can be used in
  47. conjunction with canSelectDirectories). */
  48. canSelectDirectories = 8, /**< specifies that the user can select directories (can be used in
  49. conjuction with canSelectFiles). */
  50. canSelectMultipleItems = 16, /**< specifies that the user can select multiple items. */
  51. useTreeView = 32, /**< specifies that a tree-view should be shown instead of a file list. */
  52. filenameBoxIsReadOnly = 64 /**< specifies that the user can't type directly into the filename box. */
  53. };
  54. //==============================================================================
  55. /** Creates a BasicFileBrowser.
  56. @param browserMode The intended purpose for the browser - see the
  57. FileChooserMode enum for the various options
  58. @param initialFileOrDirectory The file or directory that should be selected when
  59. the component begins. If this is File(),
  60. a default directory will be chosen.
  61. @param fileFilter an optional filter to use to determine which files
  62. are shown. If this is 0 then all files are displayed. Note
  63. that a pointer is kept internally to this object, so
  64. make sure that it is not deleted before the browser object
  65. is deleted.
  66. */
  67. BasicFileBrowser (int flags,
  68. const File& initialFileOrDirectory,
  69. const FileFilter* fileFilter);
  70. /** Destructor. */
  71. ~BasicFileBrowser();
  72. //==============================================================================
  73. /** Returns the number of files that the user has got selected.
  74. If multiple select isn't active, this will only be 0 or 1. To get the complete
  75. list of files they've chosen, pass an index to getCurrentFile().
  76. */
  77. int getNumSelectedFiles() const noexcept;
  78. /** Returns one of the files that the user has chosen.
  79. If the box has multi-select enabled, the index lets you specify which of the files
  80. to get - see getNumSelectedFiles() to find out how many files were chosen.
  81. @see getHighlightedFile
  82. */
  83. File getSelectedFile (int index) const noexcept;
  84. /** Deselects any files that are currently selected.
  85. */
  86. void deselectAllFiles();
  87. /** Returns true if the currently selected file(s) are usable.
  88. This can be used to decide whether the user can press "ok" for the
  89. current file. What it does depends on the mode, so for example in an "open"
  90. mode, this only returns true if a file has been selected and if it exists.
  91. In a "save" mode, a non-existent file would also be valid.
  92. */
  93. bool currentFileIsValid() const;
  94. /** This returns the last item in the view that the user has highlighted.
  95. This may be different from getCurrentFile(), which returns the value
  96. that is shown in the filename box, and if there are multiple selections,
  97. this will only return one of them.
  98. @see getSelectedFile
  99. */
  100. File getHighlightedFile() const noexcept;
  101. //==============================================================================
  102. /** Returns the directory whose contents are currently being shown in the listbox. */
  103. const File& getRoot() const;
  104. /** Changes the directory that's being shown in the listbox. */
  105. void setRoot (const File& newRootDirectory);
  106. /** Refreshes the directory that's currently being listed. */
  107. void refresh();
  108. /** Changes the filter that's being used to sift the files. */
  109. void setFileFilter (const FileFilter* newFileFilter);
  110. /** Returns a verb to describe what should happen when the file is accepted.
  111. E.g. if browsing in "load file" mode, this will be "Open", if in "save file"
  112. mode, it'll be "Save", etc.
  113. */
  114. virtual String getActionVerb() const;
  115. /** Returns true if the saveMode flag was set when this component was created.
  116. */
  117. bool isSaveMode() const noexcept;
  118. //==============================================================================
  119. /** Adds a listener to be told when the user selects and clicks on files.
  120. @see removeListener
  121. */
  122. void addListener (FileBrowserListener* listener);
  123. /** Removes a listener.
  124. @see addListener
  125. */
  126. void removeListener (FileBrowserListener* listener);
  127. //==============================================================================
  128. /** Enables the column resizer.
  129. */
  130. void setResizeEnable(bool enableResize)
  131. {
  132. showResizer = enableResize;
  133. }
  134. /** Returns true if the resizer is enabled.
  135. */
  136. bool getResizeEnabled() { return showResizer; }
  137. /** Returns the width for the longest item in the list.
  138. */
  139. int getLongestWidth();
  140. //==============================================================================
  141. /** @internal */
  142. void resized();
  143. /** @internal */
  144. bool keyPressed (const KeyPress& key);
  145. /** @internal */
  146. void mouseDoubleClick (const MouseEvent &e);
  147. /** @internal */
  148. void selectionChanged();
  149. /** @internal */
  150. void fileClicked (const File& f, const MouseEvent& e);
  151. /** @internal */
  152. void fileDoubleClicked (const File& f);
  153. /** @internal */
  154. void browserRootChanged (const File&);
  155. /** @internal */
  156. bool isFileSuitable (const File&) const;
  157. /** @internal */
  158. bool isDirectorySuitable (const File&) const;
  159. /** @internal */
  160. DirectoryContentsDisplayComponent* getDisplayComponent() const noexcept;
  161. private:
  162. //==============================================================================
  163. ScopedPointer <DirectoryContentsList> fileList;
  164. const FileFilter* fileFilter;
  165. int flags;
  166. File currentRoot;
  167. Array<File> chosenFiles;
  168. ListenerList <FileBrowserListener> listeners;
  169. ScopedPointer<DirectoryContentsDisplayComponent> fileListComponent;
  170. TimeSliceThread thread;
  171. void sendListenerChangeMessage();
  172. bool isFileOrDirSuitable (const File& f) const;
  173. bool showResizer;
  174. ScopedPointer<ResizableCornerComponent> resizer;
  175. ComponentBoundsConstrainer resizeLimits;
  176. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BasicFileBrowser);
  177. };
  178. #endif //__DROWAUDIO_BASICFILEBROWSER_H__