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.

564 lines
16KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. FileBrowserComponent::FileBrowserComponent (int flags_,
  21. const File& initialFileOrDirectory,
  22. const FileFilter* fileFilter_,
  23. FilePreviewComponent* previewComp_)
  24. : FileFilter (String::empty),
  25. fileFilter (fileFilter_),
  26. flags (flags_),
  27. previewComp (previewComp_),
  28. currentPathBox ("path"),
  29. fileLabel ("f", TRANS ("file:")),
  30. thread ("Juce FileBrowser")
  31. {
  32. // You need to specify one or other of the open/save flags..
  33. jassert ((flags & (saveMode | openMode)) != 0);
  34. jassert ((flags & (saveMode | openMode)) != (saveMode | openMode));
  35. // You need to specify at least one of these flags..
  36. jassert ((flags & (canSelectFiles | canSelectDirectories)) != 0);
  37. String filename;
  38. if (initialFileOrDirectory == File::nonexistent)
  39. {
  40. currentRoot = File::getCurrentWorkingDirectory();
  41. }
  42. else if (initialFileOrDirectory.isDirectory())
  43. {
  44. currentRoot = initialFileOrDirectory;
  45. }
  46. else
  47. {
  48. chosenFiles.add (initialFileOrDirectory);
  49. currentRoot = initialFileOrDirectory.getParentDirectory();
  50. filename = initialFileOrDirectory.getFileName();
  51. }
  52. fileList = new DirectoryContentsList (this, thread);
  53. if ((flags & useTreeView) != 0)
  54. {
  55. FileTreeComponent* const tree = new FileTreeComponent (*fileList);
  56. fileListComponent = tree;
  57. if ((flags & canSelectMultipleItems) != 0)
  58. tree->setMultiSelectEnabled (true);
  59. addAndMakeVisible (tree);
  60. }
  61. else
  62. {
  63. FileListComponent* const list = new FileListComponent (*fileList);
  64. fileListComponent = list;
  65. list->setOutlineThickness (1);
  66. if ((flags & canSelectMultipleItems) != 0)
  67. list->setMultipleSelectionEnabled (true);
  68. addAndMakeVisible (list);
  69. }
  70. fileListComponent->addListener (this);
  71. addAndMakeVisible (&currentPathBox);
  72. currentPathBox.setEditableText (true);
  73. resetRecentPaths();
  74. currentPathBox.addListener (this);
  75. addAndMakeVisible (&filenameBox);
  76. filenameBox.setMultiLine (false);
  77. filenameBox.setSelectAllWhenFocused (true);
  78. filenameBox.setText (filename, false);
  79. filenameBox.addListener (this);
  80. filenameBox.setReadOnly ((flags & (filenameBoxIsReadOnly | canSelectMultipleItems)) != 0);
  81. addAndMakeVisible (&fileLabel);
  82. fileLabel.attachToComponent (&filenameBox, true);
  83. addAndMakeVisible (goUpButton = getLookAndFeel().createFileBrowserGoUpButton());
  84. goUpButton->addListener (this);
  85. goUpButton->setTooltip (TRANS ("go up to parent directory"));
  86. if (previewComp != nullptr)
  87. addAndMakeVisible (previewComp);
  88. setRoot (currentRoot);
  89. thread.startThread (4);
  90. }
  91. FileBrowserComponent::~FileBrowserComponent()
  92. {
  93. fileListComponent = nullptr;
  94. fileList = nullptr;
  95. thread.stopThread (10000);
  96. }
  97. //==============================================================================
  98. void FileBrowserComponent::addListener (FileBrowserListener* const newListener)
  99. {
  100. listeners.add (newListener);
  101. }
  102. void FileBrowserComponent::removeListener (FileBrowserListener* const listener)
  103. {
  104. listeners.remove (listener);
  105. }
  106. //==============================================================================
  107. bool FileBrowserComponent::isSaveMode() const noexcept
  108. {
  109. return (flags & saveMode) != 0;
  110. }
  111. int FileBrowserComponent::getNumSelectedFiles() const noexcept
  112. {
  113. if (chosenFiles.size() == 0 && currentFileIsValid())
  114. return 1;
  115. return chosenFiles.size();
  116. }
  117. File FileBrowserComponent::getSelectedFile (int index) const noexcept
  118. {
  119. if ((flags & canSelectDirectories) != 0 && filenameBox.getText().isEmpty())
  120. return currentRoot;
  121. if (! filenameBox.isReadOnly())
  122. return currentRoot.getChildFile (filenameBox.getText());
  123. return chosenFiles[index];
  124. }
  125. bool FileBrowserComponent::currentFileIsValid() const
  126. {
  127. if (isSaveMode())
  128. return ! getSelectedFile (0).isDirectory();
  129. else
  130. return getSelectedFile (0).exists();
  131. }
  132. File FileBrowserComponent::getHighlightedFile() const noexcept
  133. {
  134. return fileListComponent->getSelectedFile (0);
  135. }
  136. void FileBrowserComponent::deselectAllFiles()
  137. {
  138. fileListComponent->deselectAllFiles();
  139. }
  140. //==============================================================================
  141. bool FileBrowserComponent::isFileSuitable (const File& file) const
  142. {
  143. return (flags & canSelectFiles) != 0 && (fileFilter == nullptr || fileFilter->isFileSuitable (file));
  144. }
  145. bool FileBrowserComponent::isDirectorySuitable (const File&) const
  146. {
  147. return true;
  148. }
  149. bool FileBrowserComponent::isFileOrDirSuitable (const File& f) const
  150. {
  151. if (f.isDirectory())
  152. return (flags & canSelectDirectories) != 0
  153. && (fileFilter == nullptr || fileFilter->isDirectorySuitable (f));
  154. return (flags & canSelectFiles) != 0 && f.exists()
  155. && (fileFilter == nullptr || fileFilter->isFileSuitable (f));
  156. }
  157. //==============================================================================
  158. const File& FileBrowserComponent::getRoot() const
  159. {
  160. return currentRoot;
  161. }
  162. void FileBrowserComponent::setRoot (const File& newRootDirectory)
  163. {
  164. bool callListeners = false;
  165. if (currentRoot != newRootDirectory)
  166. {
  167. callListeners = true;
  168. fileListComponent->scrollToTop();
  169. String path (newRootDirectory.getFullPathName());
  170. if (path.isEmpty())
  171. path = File::separatorString;
  172. StringArray rootNames, rootPaths;
  173. getRoots (rootNames, rootPaths);
  174. if (! rootPaths.contains (path, true))
  175. {
  176. bool alreadyListed = false;
  177. for (int i = currentPathBox.getNumItems(); --i >= 0;)
  178. {
  179. if (currentPathBox.getItemText (i).equalsIgnoreCase (path))
  180. {
  181. alreadyListed = true;
  182. break;
  183. }
  184. }
  185. if (! alreadyListed)
  186. currentPathBox.addItem (path, currentPathBox.getNumItems() + 2);
  187. }
  188. }
  189. currentRoot = newRootDirectory;
  190. fileList->setDirectory (currentRoot, true, true);
  191. String currentRootName (currentRoot.getFullPathName());
  192. if (currentRootName.isEmpty())
  193. currentRootName = File::separatorString;
  194. currentPathBox.setText (currentRootName, true);
  195. goUpButton->setEnabled (currentRoot.getParentDirectory().isDirectory()
  196. && currentRoot.getParentDirectory() != currentRoot);
  197. if (callListeners)
  198. {
  199. Component::BailOutChecker checker (this);
  200. listeners.callChecked (checker, &FileBrowserListener::browserRootChanged, currentRoot);
  201. }
  202. }
  203. void FileBrowserComponent::resetRecentPaths()
  204. {
  205. currentPathBox.clear();
  206. StringArray rootNames, rootPaths;
  207. getRoots (rootNames, rootPaths);
  208. for (int i = 0; i < rootNames.size(); ++i)
  209. {
  210. if (rootNames[i].isEmpty())
  211. currentPathBox.addSeparator();
  212. else
  213. currentPathBox.addItem (rootNames[i], i + 1);
  214. }
  215. currentPathBox.addSeparator();
  216. }
  217. void FileBrowserComponent::goUp()
  218. {
  219. setRoot (getRoot().getParentDirectory());
  220. }
  221. void FileBrowserComponent::refresh()
  222. {
  223. fileList->refresh();
  224. }
  225. void FileBrowserComponent::setFileFilter (const FileFilter* const newFileFilter)
  226. {
  227. if (fileFilter != newFileFilter)
  228. {
  229. fileFilter = newFileFilter;
  230. refresh();
  231. }
  232. }
  233. String FileBrowserComponent::getActionVerb() const
  234. {
  235. return isSaveMode() ? TRANS("Save") : TRANS("Open");
  236. }
  237. FilePreviewComponent* FileBrowserComponent::getPreviewComponent() const noexcept
  238. {
  239. return previewComp;
  240. }
  241. DirectoryContentsDisplayComponent* FileBrowserComponent::getDisplayComponent() const noexcept
  242. {
  243. return fileListComponent;
  244. }
  245. //==============================================================================
  246. void FileBrowserComponent::resized()
  247. {
  248. getLookAndFeel()
  249. .layoutFileBrowserComponent (*this, fileListComponent, previewComp,
  250. &currentPathBox, &filenameBox, goUpButton);
  251. }
  252. //==============================================================================
  253. void FileBrowserComponent::sendListenerChangeMessage()
  254. {
  255. Component::BailOutChecker checker (this);
  256. if (previewComp != nullptr)
  257. previewComp->selectedFileChanged (getSelectedFile (0));
  258. // You shouldn't delete the browser when the file gets changed!
  259. jassert (! checker.shouldBailOut());
  260. listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
  261. }
  262. void FileBrowserComponent::selectionChanged()
  263. {
  264. StringArray newFilenames;
  265. bool resetChosenFiles = true;
  266. for (int i = 0; i < fileListComponent->getNumSelectedFiles(); ++i)
  267. {
  268. const File f (fileListComponent->getSelectedFile (i));
  269. if (isFileOrDirSuitable (f))
  270. {
  271. if (resetChosenFiles)
  272. {
  273. chosenFiles.clear();
  274. resetChosenFiles = false;
  275. }
  276. chosenFiles.add (f);
  277. newFilenames.add (f.getRelativePathFrom (getRoot()));
  278. }
  279. }
  280. if (newFilenames.size() > 0)
  281. filenameBox.setText (newFilenames.joinIntoString (", "), false);
  282. sendListenerChangeMessage();
  283. }
  284. void FileBrowserComponent::fileClicked (const File& f, const MouseEvent& e)
  285. {
  286. Component::BailOutChecker checker (this);
  287. listeners.callChecked (checker, &FileBrowserListener::fileClicked, f, e);
  288. }
  289. void FileBrowserComponent::fileDoubleClicked (const File& f)
  290. {
  291. if (f.isDirectory())
  292. {
  293. setRoot (f);
  294. if ((flags & canSelectDirectories) != 0)
  295. filenameBox.setText (String::empty);
  296. }
  297. else
  298. {
  299. Component::BailOutChecker checker (this);
  300. listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, f);
  301. }
  302. }
  303. void FileBrowserComponent::browserRootChanged (const File&) {}
  304. bool FileBrowserComponent::keyPressed (const KeyPress& key)
  305. {
  306. (void) key;
  307. #if JUCE_LINUX || JUCE_WINDOWS
  308. if (key.getModifiers().isCommandDown()
  309. && (key.getKeyCode() == 'H' || key.getKeyCode() == 'h'))
  310. {
  311. fileList->setIgnoresHiddenFiles (! fileList->ignoresHiddenFiles());
  312. fileList->refresh();
  313. return true;
  314. }
  315. #endif
  316. return false;
  317. }
  318. //==============================================================================
  319. void FileBrowserComponent::textEditorTextChanged (TextEditor&)
  320. {
  321. sendListenerChangeMessage();
  322. }
  323. void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&)
  324. {
  325. if (filenameBox.getText().containsChar (File::separator))
  326. {
  327. const File f (currentRoot.getChildFile (filenameBox.getText()));
  328. if (f.isDirectory())
  329. {
  330. setRoot (f);
  331. chosenFiles.clear();
  332. filenameBox.setText (String::empty);
  333. }
  334. else
  335. {
  336. setRoot (f.getParentDirectory());
  337. chosenFiles.clear();
  338. chosenFiles.add (f);
  339. filenameBox.setText (f.getFileName());
  340. }
  341. }
  342. else
  343. {
  344. fileDoubleClicked (getSelectedFile (0));
  345. }
  346. }
  347. void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&)
  348. {
  349. }
  350. void FileBrowserComponent::textEditorFocusLost (TextEditor&)
  351. {
  352. if (! isSaveMode())
  353. selectionChanged();
  354. }
  355. //==============================================================================
  356. void FileBrowserComponent::buttonClicked (Button*)
  357. {
  358. goUp();
  359. }
  360. void FileBrowserComponent::comboBoxChanged (ComboBox*)
  361. {
  362. const String newText (currentPathBox.getText().trim().unquoted());
  363. if (newText.isNotEmpty())
  364. {
  365. const int index = currentPathBox.getSelectedId() - 1;
  366. StringArray rootNames, rootPaths;
  367. getRoots (rootNames, rootPaths);
  368. if (rootPaths [index].isNotEmpty())
  369. {
  370. setRoot (File (rootPaths [index]));
  371. }
  372. else
  373. {
  374. File f (newText);
  375. for (;;)
  376. {
  377. if (f.isDirectory())
  378. {
  379. setRoot (f);
  380. break;
  381. }
  382. if (f.getParentDirectory() == f)
  383. break;
  384. f = f.getParentDirectory();
  385. }
  386. }
  387. }
  388. }
  389. void FileBrowserComponent::getRoots (StringArray& rootNames, StringArray& rootPaths)
  390. {
  391. #if JUCE_WINDOWS
  392. Array<File> roots;
  393. File::findFileSystemRoots (roots);
  394. rootPaths.clear();
  395. for (int i = 0; i < roots.size(); ++i)
  396. {
  397. const File& drive = roots.getReference(i);
  398. String name (drive.getFullPathName());
  399. rootPaths.add (name);
  400. if (drive.isOnHardDisk())
  401. {
  402. String volume (drive.getVolumeLabel());
  403. if (volume.isEmpty())
  404. volume = TRANS("Hard Drive");
  405. name << " [" << volume << ']';
  406. }
  407. else if (drive.isOnCDRomDrive())
  408. {
  409. name << TRANS(" [CD/DVD drive]");
  410. }
  411. rootNames.add (name);
  412. }
  413. rootPaths.add (String::empty);
  414. rootNames.add (String::empty);
  415. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  416. rootNames.add ("Documents");
  417. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  418. rootNames.add ("Desktop");
  419. #elif JUCE_MAC
  420. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  421. rootNames.add ("Home folder");
  422. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  423. rootNames.add ("Documents");
  424. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  425. rootNames.add ("Desktop");
  426. rootPaths.add (String::empty);
  427. rootNames.add (String::empty);
  428. Array <File> volumes;
  429. File vol ("/Volumes");
  430. vol.findChildFiles (volumes, File::findDirectories, false);
  431. for (int i = 0; i < volumes.size(); ++i)
  432. {
  433. const File& volume = volumes.getReference(i);
  434. if (volume.isDirectory() && ! volume.getFileName().startsWithChar ('.'))
  435. {
  436. rootPaths.add (volume.getFullPathName());
  437. rootNames.add (volume.getFileName());
  438. }
  439. }
  440. #else
  441. rootPaths.add ("/");
  442. rootNames.add ("/");
  443. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  444. rootNames.add ("Home folder");
  445. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  446. rootNames.add ("Desktop");
  447. #endif
  448. }
  449. END_JUCE_NAMESPACE