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.

628 lines
18KB

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