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.

juce_FileBrowserComponent.cpp 16KB

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