Audio plugin host https://kx.studio/carla
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.

607 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. FileBrowserComponent::FileBrowserComponent (int flags_,
  18. const File& initialFileOrDirectory,
  19. const FileFilter* fileFilter_,
  20. FilePreviewComponent* previewComp_)
  21. : FileFilter (String::empty),
  22. fileFilter (fileFilter_),
  23. flags (flags_),
  24. previewComp (previewComp_),
  25. currentPathBox ("path"),
  26. fileLabel ("f", TRANS ("file:")),
  27. thread ("Juce FileBrowser"),
  28. wasProcessActive (true)
  29. {
  30. // You need to specify one or other of the open/save flags..
  31. jassert ((flags & (saveMode | openMode)) != 0);
  32. jassert ((flags & (saveMode | openMode)) != (saveMode | openMode));
  33. // You need to specify at least one of these flags..
  34. jassert ((flags & (canSelectFiles | canSelectDirectories)) != 0);
  35. String filename;
  36. if (initialFileOrDirectory == File::nonexistent)
  37. {
  38. currentRoot = File::getCurrentWorkingDirectory();
  39. }
  40. else if (initialFileOrDirectory.isDirectory())
  41. {
  42. currentRoot = initialFileOrDirectory;
  43. }
  44. else
  45. {
  46. chosenFiles.add (initialFileOrDirectory);
  47. currentRoot = initialFileOrDirectory.getParentDirectory();
  48. filename = initialFileOrDirectory.getFileName();
  49. }
  50. fileList = new DirectoryContentsList (this, thread);
  51. if ((flags & useTreeView) != 0)
  52. {
  53. FileTreeComponent* const tree = new FileTreeComponent (*fileList);
  54. fileListComponent = tree;
  55. if ((flags & canSelectMultipleItems) != 0)
  56. tree->setMultiSelectEnabled (true);
  57. addAndMakeVisible (tree);
  58. }
  59. else
  60. {
  61. FileListComponent* const list = new FileListComponent (*fileList);
  62. fileListComponent = list;
  63. list->setOutlineThickness (1);
  64. if ((flags & canSelectMultipleItems) != 0)
  65. list->setMultipleSelectionEnabled (true);
  66. addAndMakeVisible (list);
  67. }
  68. fileListComponent->addListener (this);
  69. addAndMakeVisible (currentPathBox);
  70. currentPathBox.setEditableText (true);
  71. resetRecentPaths();
  72. currentPathBox.addListener (this);
  73. addAndMakeVisible (filenameBox);
  74. filenameBox.setMultiLine (false);
  75. filenameBox.setSelectAllWhenFocused (true);
  76. filenameBox.setText (filename, false);
  77. filenameBox.addListener (this);
  78. filenameBox.setReadOnly ((flags & (filenameBoxIsReadOnly | canSelectMultipleItems)) != 0);
  79. addAndMakeVisible (fileLabel);
  80. fileLabel.attachToComponent (&filenameBox, true);
  81. addAndMakeVisible (goUpButton = getLookAndFeel().createFileBrowserGoUpButton());
  82. goUpButton->addListener (this);
  83. goUpButton->setTooltip (TRANS ("Go up to parent directory"));
  84. if (previewComp != nullptr)
  85. addAndMakeVisible (previewComp);
  86. setRoot (currentRoot);
  87. thread.startThread (4);
  88. startTimer (2000);
  89. }
  90. FileBrowserComponent::~FileBrowserComponent()
  91. {
  92. fileListComponent = nullptr;
  93. fileList = nullptr;
  94. thread.stopThread (10000);
  95. }
  96. //==============================================================================
  97. void FileBrowserComponent::addListener (FileBrowserListener* const newListener)
  98. {
  99. listeners.add (newListener);
  100. }
  101. void FileBrowserComponent::removeListener (FileBrowserListener* const listener)
  102. {
  103. listeners.remove (listener);
  104. }
  105. //==============================================================================
  106. bool FileBrowserComponent::isSaveMode() const noexcept
  107. {
  108. return (flags & saveMode) != 0;
  109. }
  110. int FileBrowserComponent::getNumSelectedFiles() const noexcept
  111. {
  112. if (chosenFiles.size() == 0 && currentFileIsValid())
  113. return 1;
  114. return chosenFiles.size();
  115. }
  116. File FileBrowserComponent::getSelectedFile (int index) const noexcept
  117. {
  118. if ((flags & canSelectDirectories) != 0 && filenameBox.getText().isEmpty())
  119. return currentRoot;
  120. if (! filenameBox.isReadOnly())
  121. return currentRoot.getChildFile (filenameBox.getText());
  122. return chosenFiles[index];
  123. }
  124. bool FileBrowserComponent::currentFileIsValid() const
  125. {
  126. const File f (getSelectedFile (0));
  127. if (isSaveMode())
  128. return (flags & canSelectDirectories) != 0 || ! f.isDirectory();
  129. return f.exists();
  130. }
  131. File FileBrowserComponent::getHighlightedFile() const noexcept
  132. {
  133. return fileListComponent->getSelectedFile (0);
  134. }
  135. void FileBrowserComponent::deselectAllFiles()
  136. {
  137. fileListComponent->deselectAllFiles();
  138. }
  139. //==============================================================================
  140. bool FileBrowserComponent::isFileSuitable (const File& file) const
  141. {
  142. return (flags & canSelectFiles) != 0
  143. && (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. if (FileTreeComponent* tree = dynamic_cast<FileTreeComponent*> (fileListComponent.get()))
  192. tree->refresh();
  193. String currentRootName (currentRoot.getFullPathName());
  194. if (currentRootName.isEmpty())
  195. currentRootName = File::separatorString;
  196. currentPathBox.setText (currentRootName, dontSendNotification);
  197. goUpButton->setEnabled (currentRoot.getParentDirectory().isDirectory()
  198. && currentRoot.getParentDirectory() != currentRoot);
  199. if (callListeners)
  200. {
  201. Component::BailOutChecker checker (this);
  202. listeners.callChecked (checker, &FileBrowserListener::browserRootChanged, currentRoot);
  203. }
  204. }
  205. void FileBrowserComponent::setFileName (const String& newName)
  206. {
  207. filenameBox.setText (newName, true);
  208. fileListComponent->setSelectedFile (currentRoot.getChildFile (newName));
  209. }
  210. void FileBrowserComponent::resetRecentPaths()
  211. {
  212. currentPathBox.clear();
  213. StringArray rootNames, rootPaths;
  214. getRoots (rootNames, rootPaths);
  215. for (int i = 0; i < rootNames.size(); ++i)
  216. {
  217. if (rootNames[i].isEmpty())
  218. currentPathBox.addSeparator();
  219. else
  220. currentPathBox.addItem (rootNames[i], i + 1);
  221. }
  222. currentPathBox.addSeparator();
  223. }
  224. void FileBrowserComponent::goUp()
  225. {
  226. setRoot (getRoot().getParentDirectory());
  227. }
  228. void FileBrowserComponent::refresh()
  229. {
  230. fileList->refresh();
  231. }
  232. void FileBrowserComponent::setFileFilter (const FileFilter* const newFileFilter)
  233. {
  234. if (fileFilter != newFileFilter)
  235. {
  236. fileFilter = newFileFilter;
  237. refresh();
  238. }
  239. }
  240. String FileBrowserComponent::getActionVerb() const
  241. {
  242. return isSaveMode() ? ((flags & canSelectDirectories) != 0 ? TRANS("Choose")
  243. : TRANS("Save"))
  244. : TRANS("Open");
  245. }
  246. void FileBrowserComponent::setFilenameBoxLabel (const String& name)
  247. {
  248. fileLabel.setText (name, dontSendNotification);
  249. }
  250. FilePreviewComponent* FileBrowserComponent::getPreviewComponent() const noexcept
  251. {
  252. return previewComp;
  253. }
  254. DirectoryContentsDisplayComponent* FileBrowserComponent::getDisplayComponent() const noexcept
  255. {
  256. return fileListComponent;
  257. }
  258. //==============================================================================
  259. void FileBrowserComponent::resized()
  260. {
  261. getLookAndFeel()
  262. .layoutFileBrowserComponent (*this, fileListComponent, previewComp,
  263. &currentPathBox, &filenameBox, goUpButton);
  264. }
  265. //==============================================================================
  266. void FileBrowserComponent::sendListenerChangeMessage()
  267. {
  268. Component::BailOutChecker checker (this);
  269. if (previewComp != nullptr)
  270. previewComp->selectedFileChanged (getSelectedFile (0));
  271. // You shouldn't delete the browser when the file gets changed!
  272. jassert (! checker.shouldBailOut());
  273. listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
  274. }
  275. void FileBrowserComponent::selectionChanged()
  276. {
  277. StringArray newFilenames;
  278. bool resetChosenFiles = true;
  279. for (int i = 0; i < fileListComponent->getNumSelectedFiles(); ++i)
  280. {
  281. const File f (fileListComponent->getSelectedFile (i));
  282. if (isFileOrDirSuitable (f))
  283. {
  284. if (resetChosenFiles)
  285. {
  286. chosenFiles.clear();
  287. resetChosenFiles = false;
  288. }
  289. chosenFiles.add (f);
  290. newFilenames.add (f.getRelativePathFrom (getRoot()));
  291. }
  292. }
  293. if (newFilenames.size() > 0)
  294. filenameBox.setText (newFilenames.joinIntoString (", "), false);
  295. sendListenerChangeMessage();
  296. }
  297. void FileBrowserComponent::fileClicked (const File& f, const MouseEvent& e)
  298. {
  299. Component::BailOutChecker checker (this);
  300. listeners.callChecked (checker, &FileBrowserListener::fileClicked, f, e);
  301. }
  302. void FileBrowserComponent::fileDoubleClicked (const File& f)
  303. {
  304. if (f.isDirectory())
  305. {
  306. setRoot (f);
  307. if ((flags & canSelectDirectories) != 0 && (flags & doNotClearFileNameOnRootChange) == 0)
  308. filenameBox.setText (String::empty);
  309. }
  310. else
  311. {
  312. Component::BailOutChecker checker (this);
  313. listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, f);
  314. }
  315. }
  316. void FileBrowserComponent::browserRootChanged (const File&) {}
  317. bool FileBrowserComponent::keyPressed (const KeyPress& key)
  318. {
  319. #if JUCE_LINUX || JUCE_WINDOWS
  320. if (key.getModifiers().isCommandDown()
  321. && (key.getKeyCode() == 'H' || key.getKeyCode() == 'h'))
  322. {
  323. fileList->setIgnoresHiddenFiles (! fileList->ignoresHiddenFiles());
  324. fileList->refresh();
  325. return true;
  326. }
  327. #endif
  328. ignoreUnused (key);
  329. return false;
  330. }
  331. //==============================================================================
  332. void FileBrowserComponent::textEditorTextChanged (TextEditor&)
  333. {
  334. sendListenerChangeMessage();
  335. }
  336. void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&)
  337. {
  338. if (filenameBox.getText().containsChar (File::separator))
  339. {
  340. const File f (currentRoot.getChildFile (filenameBox.getText()));
  341. if (f.isDirectory())
  342. {
  343. setRoot (f);
  344. chosenFiles.clear();
  345. if ((flags & doNotClearFileNameOnRootChange) == 0)
  346. filenameBox.setText (String());
  347. }
  348. else
  349. {
  350. setRoot (f.getParentDirectory());
  351. chosenFiles.clear();
  352. chosenFiles.add (f);
  353. filenameBox.setText (f.getFileName());
  354. }
  355. }
  356. else
  357. {
  358. fileDoubleClicked (getSelectedFile (0));
  359. }
  360. }
  361. void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&)
  362. {
  363. }
  364. void FileBrowserComponent::textEditorFocusLost (TextEditor&)
  365. {
  366. if (! isSaveMode())
  367. selectionChanged();
  368. }
  369. //==============================================================================
  370. void FileBrowserComponent::buttonClicked (Button*)
  371. {
  372. goUp();
  373. }
  374. void FileBrowserComponent::comboBoxChanged (ComboBox*)
  375. {
  376. const String newText (currentPathBox.getText().trim().unquoted());
  377. if (newText.isNotEmpty())
  378. {
  379. const int index = currentPathBox.getSelectedId() - 1;
  380. StringArray rootNames, rootPaths;
  381. getRoots (rootNames, rootPaths);
  382. if (rootPaths [index].isNotEmpty())
  383. {
  384. setRoot (File (rootPaths [index]));
  385. }
  386. else
  387. {
  388. File f (newText);
  389. for (;;)
  390. {
  391. if (f.isDirectory())
  392. {
  393. setRoot (f);
  394. break;
  395. }
  396. if (f.getParentDirectory() == f)
  397. break;
  398. f = f.getParentDirectory();
  399. }
  400. }
  401. }
  402. }
  403. void FileBrowserComponent::getDefaultRoots (StringArray& rootNames, StringArray& rootPaths)
  404. {
  405. #if JUCE_WINDOWS
  406. Array<File> roots;
  407. File::findFileSystemRoots (roots);
  408. rootPaths.clear();
  409. for (int i = 0; i < roots.size(); ++i)
  410. {
  411. const File& drive = roots.getReference(i);
  412. String name (drive.getFullPathName());
  413. rootPaths.add (name);
  414. if (drive.isOnHardDisk())
  415. {
  416. String volume (drive.getVolumeLabel());
  417. if (volume.isEmpty())
  418. volume = TRANS("Hard Drive");
  419. name << " [" << volume << ']';
  420. }
  421. else if (drive.isOnCDRomDrive())
  422. {
  423. name << " [" << TRANS("CD/DVD drive") << ']';
  424. }
  425. rootNames.add (name);
  426. }
  427. rootPaths.add (String::empty);
  428. rootNames.add (String::empty);
  429. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  430. rootNames.add (TRANS("Documents"));
  431. rootPaths.add (File::getSpecialLocation (File::userMusicDirectory).getFullPathName());
  432. rootNames.add (TRANS("Music"));
  433. rootPaths.add (File::getSpecialLocation (File::userPicturesDirectory).getFullPathName());
  434. rootNames.add (TRANS("Pictures"));
  435. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  436. rootNames.add (TRANS("Desktop"));
  437. #elif JUCE_MAC
  438. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  439. rootNames.add (TRANS("Home folder"));
  440. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  441. rootNames.add (TRANS("Documents"));
  442. rootPaths.add (File::getSpecialLocation (File::userMusicDirectory).getFullPathName());
  443. rootNames.add (TRANS("Music"));
  444. rootPaths.add (File::getSpecialLocation (File::userPicturesDirectory).getFullPathName());
  445. rootNames.add (TRANS("Pictures"));
  446. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  447. rootNames.add (TRANS("Desktop"));
  448. rootPaths.add (String::empty);
  449. rootNames.add (String::empty);
  450. Array<File> volumes;
  451. File vol ("/Volumes");
  452. vol.findChildFiles (volumes, File::findDirectories, false);
  453. for (int i = 0; i < volumes.size(); ++i)
  454. {
  455. const File& volume = volumes.getReference(i);
  456. if (volume.isDirectory() && ! volume.getFileName().startsWithChar ('.'))
  457. {
  458. rootPaths.add (volume.getFullPathName());
  459. rootNames.add (volume.getFileName());
  460. }
  461. }
  462. #else
  463. rootPaths.add ("/");
  464. rootNames.add ("/");
  465. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  466. rootNames.add (TRANS("Home folder"));
  467. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  468. rootNames.add (TRANS("Desktop"));
  469. #endif
  470. }
  471. void FileBrowserComponent::getRoots (StringArray& rootNames, StringArray& rootPaths)
  472. {
  473. getDefaultRoots (rootNames, rootPaths);
  474. }
  475. void FileBrowserComponent::timerCallback()
  476. {
  477. const bool isProcessActive = Process::isForegroundProcess();
  478. if (wasProcessActive != isProcessActive)
  479. {
  480. wasProcessActive = isProcessActive;
  481. if (isProcessActive && fileList != nullptr)
  482. refresh();
  483. }
  484. }