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.

384 lines
12KB

  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. //==================================================================================
  25. class BrowserColumn : public BasicFileBrowser,
  26. public DragAndDropContainer,
  27. public ChangeBroadcaster
  28. {
  29. public:
  30. //==================================================================================
  31. BrowserColumn (WildcardFileFilter* filesToDisplay_)
  32. : BasicFileBrowser (BasicFileBrowser::openMode
  33. + BasicFileBrowser::canSelectFiles
  34. + BasicFileBrowser::canSelectDirectories
  35. + BasicFileBrowser::canSelectMultipleItems,
  36. File::getSpecialLocation (File::userMusicDirectory),
  37. filesToDisplay_),
  38. fileDragEnabled (false)
  39. {
  40. addMouseListener (this, true);
  41. }
  42. ~BrowserColumn()
  43. {
  44. }
  45. void mouseDrag (const MouseEvent& /*e*/)
  46. {
  47. if (fileDragEnabled)
  48. {
  49. StringArray files;
  50. for (int i = 0; i < getNumSelectedFiles(); i++)
  51. {
  52. File file (getSelectedFile (i));
  53. if (file.existsAsFile())
  54. files.add (file.getFullPathName());
  55. }
  56. fileDragEnabled = false;
  57. performExternalDragDropOfFiles (files, false);
  58. }
  59. }
  60. void mouseUp (const MouseEvent& /*e*/)
  61. {
  62. fileDragEnabled = false;
  63. }
  64. void selectionChanged()
  65. {
  66. BasicFileBrowser::selectionChanged();
  67. sendSynchronousChangeMessage();
  68. }
  69. void fileClicked (const File& /*file*/, const MouseEvent& /*e*/)
  70. {
  71. fileDragEnabled = true;
  72. }
  73. void fileDoubleClicked (const File& /*f*/) {}
  74. private:
  75. //==================================================================================
  76. bool fileDragEnabled;
  77. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BrowserColumn);
  78. };
  79. //==================================================================================
  80. class ColumnFileBrowserContents : public Component,
  81. public FileBrowserListener,
  82. public ChangeListener,
  83. public ComponentListener
  84. {
  85. public:
  86. //==================================================================================
  87. ColumnFileBrowserContents (WildcardFileFilter* filesToDisplay_, Viewport* parentViewport);
  88. ~ColumnFileBrowserContents();
  89. void resized();
  90. void selectionChanged () {}
  91. void fileClicked (const File& /*file*/, const MouseEvent& /*e*/) {}
  92. void fileDoubleClicked (const File& /*file*/) {}
  93. void browserRootChanged (const File& /*newRoot*/) {}
  94. void selectedFileChanged (const File& file);
  95. bool addColumn (const File& rootDirectory);
  96. void removeColumn (int numColumns = 1);
  97. void changeListenerCallback (ChangeBroadcaster* changedComponent);
  98. void componentMovedOrResized (Component &component, bool wasMoved, bool wasResized);
  99. bool keyPressed (const KeyPress& key);
  100. private:
  101. //==================================================================================
  102. WildcardFileFilter* filesToDisplay;
  103. Viewport* viewport;
  104. OwnedArray <BrowserColumn> columns;
  105. int activeColumn;
  106. friend class ColumnFileBrowser;
  107. ScopedPointer<LookAndFeel> activeLookAndFeel;
  108. ScopedPointer<LookAndFeel> inactiveLookAndFeel;
  109. //==================================================================================
  110. int getNumValidChildFiles (const File& sourceFile);
  111. //==================================================================================
  112. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColumnFileBrowserContents);
  113. };
  114. //==================================================================================
  115. ColumnFileBrowserContents::ColumnFileBrowserContents (WildcardFileFilter* filesToDisplay_, Viewport* parentViewport)
  116. : filesToDisplay (filesToDisplay_),
  117. viewport (parentViewport)
  118. {
  119. activeLookAndFeel = new ColumnFileBrowserLookAndFeel();
  120. activeLookAndFeel->setColour (DirectoryContentsDisplayComponent::highlightColourId,
  121. Colours::darkorange);
  122. inactiveLookAndFeel = new ColumnFileBrowserLookAndFeel();
  123. columns.add (new BrowserColumn (filesToDisplay_));
  124. addAndMakeVisible (columns[0]);
  125. columns[0]->setSize (300, 50);
  126. columns[0]->addListener (this);
  127. columns[0]->addChangeListener (this);
  128. columns[0]->addComponentListener (this);
  129. columns[0]->setLookAndFeel (activeLookAndFeel);
  130. activeColumn = 0;
  131. }
  132. ColumnFileBrowserContents::~ColumnFileBrowserContents()
  133. {
  134. }
  135. void ColumnFileBrowserContents::resized()
  136. {
  137. int width = 0;
  138. const int height = getHeight();
  139. for (int i = 0; i < columns.size(); i++)
  140. {
  141. columns[i]->setBounds (width, 0, columns[i]->getWidth(), height);
  142. width += columns[i]->getWidth();
  143. }
  144. setSize (width, height);
  145. }
  146. void ColumnFileBrowserContents::selectedFileChanged (const File& file)
  147. {
  148. // if last column clicked add new column
  149. if (columns[activeColumn] == columns.getLast())
  150. {
  151. addColumn (file);
  152. }
  153. else // otherwise remove uneeded columns and change last
  154. {
  155. for (int i = 0; i < columns.size(); i++)
  156. {
  157. if (columns[activeColumn] == columns[i])
  158. {
  159. const int numColumnsToRemove = columns.size() - i - (file.isDirectory() ? 1 : 0);
  160. removeColumn (numColumnsToRemove);
  161. if (file.isDirectory())
  162. columns.getLast()->setRoot (file);
  163. break;
  164. }
  165. }
  166. resized();
  167. }
  168. // stick to edges of viewport
  169. if (getWidth() < viewport->getWidth())
  170. {
  171. viewport->setViewPosition (0, 0);
  172. }
  173. else if (file.exists() || (getRight() < viewport->getRight()))
  174. {
  175. const int ammountToSubtract = viewport->getRight() - getRight();
  176. viewport->setViewPosition (viewport->getViewPositionX() - ammountToSubtract, 0);
  177. }
  178. }
  179. bool ColumnFileBrowserContents::addColumn (const File& rootDirectory)
  180. {
  181. if (rootDirectory.isDirectory() && rootDirectory.exists())
  182. {
  183. const int startingWidth = columns.getLast()->getWidth();
  184. BrowserColumn* newColumn = new BrowserColumn (filesToDisplay);
  185. newColumn->setLookAndFeel (inactiveLookAndFeel);
  186. newColumn->setRoot (rootDirectory);
  187. newColumn->setSize (startingWidth, 50);
  188. newColumn->addListener (this);
  189. newColumn->addChangeListener (this);
  190. newColumn->addComponentListener (this);
  191. columns.add (newColumn);
  192. addAndMakeVisible (newColumn);
  193. resized();
  194. return true;
  195. }
  196. return false;
  197. }
  198. void ColumnFileBrowserContents::removeColumn (int numColumns)
  199. {
  200. for (int i = numColumns; i <= 0; i--)
  201. {
  202. columns[i]->removeListener (this);
  203. columns[i]->removeChangeListener (this);
  204. columns[i]->removeComponentListener (this);
  205. }
  206. columns.removeLast (numColumns - 1);
  207. }
  208. void ColumnFileBrowserContents::changeListenerCallback (ChangeBroadcaster* changedComponent)
  209. {
  210. BrowserColumn* changedColumn = static_cast<BrowserColumn*> (changedComponent);
  211. if (changedColumn->getHighlightedFile().getFileName().isNotEmpty())
  212. {
  213. columns[activeColumn]->setLookAndFeel (inactiveLookAndFeel);
  214. activeColumn = columns.indexOf (changedColumn);
  215. columns[activeColumn]->setLookAndFeel (activeLookAndFeel);
  216. columns[activeColumn]->repaint();
  217. selectedFileChanged (changedColumn->getHighlightedFile());
  218. }
  219. }
  220. void ColumnFileBrowserContents::componentMovedOrResized (Component& /*component*/, bool /*wasMoved*/, bool wasResized)
  221. {
  222. if (wasResized)
  223. resized();
  224. }
  225. bool ColumnFileBrowserContents::keyPressed (const KeyPress& key)
  226. {
  227. if (key.isKeyCode (KeyPress::leftKey))
  228. {
  229. if (activeColumn != 0)
  230. {
  231. FileListComponent* list = dynamic_cast<FileListComponent*> (columns[activeColumn]->getDisplayComponent());
  232. list->deselectAllRows();
  233. int newActiveColumn = jmax (0, activeColumn - 1);
  234. columns[newActiveColumn]->selectionChanged();
  235. columns[newActiveColumn]->grabKeyboardFocus();
  236. }
  237. return true;
  238. }
  239. else if (key.isKeyCode (KeyPress::rightKey))
  240. {
  241. if (columns[activeColumn]->getNumSelectedFiles() == 1
  242. && columns[activeColumn]->getSelectedFile (0).isDirectory()
  243. && getNumValidChildFiles (columns[activeColumn]->getSelectedFile (0)) > 0)
  244. {
  245. int newActiveColumn = activeColumn + 1;
  246. addColumn (columns[activeColumn]->getSelectedFile (0));
  247. FileListComponent* list = dynamic_cast<FileListComponent*> (columns[newActiveColumn]->getDisplayComponent());
  248. if (list != nullptr)
  249. {
  250. ListBoxModel* model = list->getModel();
  251. if (model != nullptr)
  252. {
  253. if (model->getNumRows() > 0)
  254. {
  255. columns[newActiveColumn]->grabKeyboardFocus();
  256. list->selectRow (0);
  257. }
  258. }
  259. }
  260. }
  261. return true;
  262. }
  263. return false;
  264. }
  265. //==================================================================================
  266. int ColumnFileBrowserContents::getNumValidChildFiles (const File& sourceFile)
  267. {
  268. const int numChildFiles = sourceFile.getNumberOfChildFiles (File::findFilesAndDirectories +
  269. File::ignoreHiddenFiles,
  270. filesToDisplay == nullptr ? "*" : filesToDisplay->getDescription().fromFirstOccurrenceOf ("(", false, false).upToFirstOccurrenceOf (")", false, false));
  271. return numChildFiles;
  272. }
  273. //==================================================================================
  274. ColumnFileBrowser::ColumnFileBrowser (WildcardFileFilter* filesToDisplay_)
  275. : Viewport ("ColumnFileBrowser"),
  276. wildcard (filesToDisplay_)
  277. {
  278. addMouseListener (this, true);
  279. setWantsKeyboardFocus (false);
  280. setScrollBarsShown (false, true);
  281. setScrollBarThickness (10);
  282. fileBrowser = new ColumnFileBrowserContents (filesToDisplay_, this);
  283. setViewedComponent (fileBrowser);
  284. }
  285. ColumnFileBrowser::~ColumnFileBrowser()
  286. {
  287. }
  288. void ColumnFileBrowser::setActiveColumHighlightColour (const Colour& colour)
  289. {
  290. fileBrowser->activeLookAndFeel->setColour (DirectoryContentsDisplayComponent::highlightColourId,
  291. colour);
  292. }
  293. void ColumnFileBrowser::resized()
  294. {
  295. Viewport::resized();
  296. fileBrowser->setSize (fileBrowser->getWidth(), getMaximumVisibleHeight());
  297. }
  298. void ColumnFileBrowser::visibleAreaChanged (const Rectangle<int>& /*newVisibleArea*/)
  299. {
  300. resized();
  301. }
  302. void ColumnFileBrowser::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  303. {
  304. if (! (e.mods.isAltDown() || e.mods.isCtrlDown()))
  305. {
  306. if (e.eventComponent != this && e.eventComponent != &getHorizontalScrollBar())
  307. if (wheel.deltaX != 0.0f)
  308. Viewport::useMouseWheelMoveIfNeeded (e, wheel);
  309. }
  310. }