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.

606 lines
18KB

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