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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. if (FileTreeComponent* tree = dynamic_cast<FileTreeComponent*> (fileListComponent.get()))
  190. tree->refresh();
  191. String currentRootName (currentRoot.getFullPathName());
  192. if (currentRootName.isEmpty())
  193. currentRootName = File::separatorString;
  194. currentPathBox.setText (currentRootName, dontSendNotification);
  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::setFileName (const String& newName)
  204. {
  205. filenameBox.setText (newName, true);
  206. fileListComponent->setSelectedFile (currentRoot.getChildFile (newName));
  207. }
  208. void FileBrowserComponent::resetRecentPaths()
  209. {
  210. currentPathBox.clear();
  211. StringArray rootNames, rootPaths;
  212. getRoots (rootNames, rootPaths);
  213. for (int i = 0; i < rootNames.size(); ++i)
  214. {
  215. if (rootNames[i].isEmpty())
  216. currentPathBox.addSeparator();
  217. else
  218. currentPathBox.addItem (rootNames[i], i + 1);
  219. }
  220. currentPathBox.addSeparator();
  221. }
  222. void FileBrowserComponent::goUp()
  223. {
  224. setRoot (getRoot().getParentDirectory());
  225. }
  226. void FileBrowserComponent::refresh()
  227. {
  228. fileList->refresh();
  229. }
  230. void FileBrowserComponent::setFileFilter (const FileFilter* const newFileFilter)
  231. {
  232. if (fileFilter != newFileFilter)
  233. {
  234. fileFilter = newFileFilter;
  235. refresh();
  236. }
  237. }
  238. String FileBrowserComponent::getActionVerb() const
  239. {
  240. return isSaveMode() ? ((flags & canSelectDirectories) != 0 ? TRANS("Choose")
  241. : TRANS("Save"))
  242. : TRANS("Open");
  243. }
  244. void FileBrowserComponent::setFilenameBoxLabel (const String& name)
  245. {
  246. fileLabel.setText (name, dontSendNotification);
  247. }
  248. FilePreviewComponent* FileBrowserComponent::getPreviewComponent() const noexcept
  249. {
  250. return previewComp;
  251. }
  252. DirectoryContentsDisplayComponent* FileBrowserComponent::getDisplayComponent() const noexcept
  253. {
  254. return fileListComponent;
  255. }
  256. //==============================================================================
  257. void FileBrowserComponent::resized()
  258. {
  259. getLookAndFeel()
  260. .layoutFileBrowserComponent (*this, fileListComponent, previewComp,
  261. &currentPathBox, &filenameBox, goUpButton);
  262. }
  263. //==============================================================================
  264. void FileBrowserComponent::sendListenerChangeMessage()
  265. {
  266. Component::BailOutChecker checker (this);
  267. if (previewComp != nullptr)
  268. previewComp->selectedFileChanged (getSelectedFile (0));
  269. // You shouldn't delete the browser when the file gets changed!
  270. jassert (! checker.shouldBailOut());
  271. listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
  272. }
  273. void FileBrowserComponent::selectionChanged()
  274. {
  275. StringArray newFilenames;
  276. bool resetChosenFiles = true;
  277. for (int i = 0; i < fileListComponent->getNumSelectedFiles(); ++i)
  278. {
  279. const File f (fileListComponent->getSelectedFile (i));
  280. if (isFileOrDirSuitable (f))
  281. {
  282. if (resetChosenFiles)
  283. {
  284. chosenFiles.clear();
  285. resetChosenFiles = false;
  286. }
  287. chosenFiles.add (f);
  288. newFilenames.add (f.getRelativePathFrom (getRoot()));
  289. }
  290. }
  291. if (newFilenames.size() > 0)
  292. filenameBox.setText (newFilenames.joinIntoString (", "), false);
  293. sendListenerChangeMessage();
  294. }
  295. void FileBrowserComponent::fileClicked (const File& f, const MouseEvent& e)
  296. {
  297. Component::BailOutChecker checker (this);
  298. listeners.callChecked (checker, &FileBrowserListener::fileClicked, f, e);
  299. }
  300. void FileBrowserComponent::fileDoubleClicked (const File& f)
  301. {
  302. if (f.isDirectory())
  303. {
  304. setRoot (f);
  305. if ((flags & canSelectDirectories) != 0 && (flags & doNotClearFileNameOnRootChange) == 0)
  306. filenameBox.setText (String::empty);
  307. }
  308. else
  309. {
  310. Component::BailOutChecker checker (this);
  311. listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, f);
  312. }
  313. }
  314. void FileBrowserComponent::browserRootChanged (const File&) {}
  315. bool FileBrowserComponent::keyPressed (const KeyPress& key)
  316. {
  317. (void) key;
  318. #if JUCE_LINUX || JUCE_WINDOWS
  319. if (key.getModifiers().isCommandDown()
  320. && (key.getKeyCode() == 'H' || key.getKeyCode() == 'h'))
  321. {
  322. fileList->setIgnoresHiddenFiles (! fileList->ignoresHiddenFiles());
  323. fileList->refresh();
  324. return true;
  325. }
  326. #endif
  327. return false;
  328. }
  329. //==============================================================================
  330. void FileBrowserComponent::textEditorTextChanged (TextEditor&)
  331. {
  332. sendListenerChangeMessage();
  333. }
  334. void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&)
  335. {
  336. if (filenameBox.getText().containsChar (File::separator))
  337. {
  338. const File f (currentRoot.getChildFile (filenameBox.getText()));
  339. if (f.isDirectory())
  340. {
  341. setRoot (f);
  342. chosenFiles.clear();
  343. if ((flags & doNotClearFileNameOnRootChange) == 0)
  344. filenameBox.setText (String());
  345. }
  346. else
  347. {
  348. setRoot (f.getParentDirectory());
  349. chosenFiles.clear();
  350. chosenFiles.add (f);
  351. filenameBox.setText (f.getFileName());
  352. }
  353. }
  354. else
  355. {
  356. fileDoubleClicked (getSelectedFile (0));
  357. }
  358. }
  359. void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&)
  360. {
  361. }
  362. void FileBrowserComponent::textEditorFocusLost (TextEditor&)
  363. {
  364. if (! isSaveMode())
  365. selectionChanged();
  366. }
  367. //==============================================================================
  368. void FileBrowserComponent::buttonClicked (Button*)
  369. {
  370. goUp();
  371. }
  372. void FileBrowserComponent::comboBoxChanged (ComboBox*)
  373. {
  374. const String newText (currentPathBox.getText().trim().unquoted());
  375. if (newText.isNotEmpty())
  376. {
  377. const int index = currentPathBox.getSelectedId() - 1;
  378. StringArray rootNames, rootPaths;
  379. getRoots (rootNames, rootPaths);
  380. if (rootPaths [index].isNotEmpty())
  381. {
  382. setRoot (File (rootPaths [index]));
  383. }
  384. else
  385. {
  386. File f (newText);
  387. for (;;)
  388. {
  389. if (f.isDirectory())
  390. {
  391. setRoot (f);
  392. break;
  393. }
  394. if (f.getParentDirectory() == f)
  395. break;
  396. f = f.getParentDirectory();
  397. }
  398. }
  399. }
  400. }
  401. void FileBrowserComponent::getDefaultRoots (StringArray& rootNames, StringArray& rootPaths)
  402. {
  403. #if JUCE_WINDOWS
  404. Array<File> roots;
  405. File::findFileSystemRoots (roots);
  406. rootPaths.clear();
  407. for (int i = 0; i < roots.size(); ++i)
  408. {
  409. const File& drive = roots.getReference(i);
  410. String name (drive.getFullPathName());
  411. rootPaths.add (name);
  412. if (drive.isOnHardDisk())
  413. {
  414. String volume (drive.getVolumeLabel());
  415. if (volume.isEmpty())
  416. volume = TRANS("Hard Drive");
  417. name << " [" << volume << ']';
  418. }
  419. else if (drive.isOnCDRomDrive())
  420. {
  421. name << " [" << TRANS("CD/DVD drive") << ']';
  422. }
  423. rootNames.add (name);
  424. }
  425. rootPaths.add (String::empty);
  426. rootNames.add (String::empty);
  427. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  428. rootNames.add (TRANS("Documents"));
  429. rootPaths.add (File::getSpecialLocation (File::userMusicDirectory).getFullPathName());
  430. rootNames.add (TRANS("Music"));
  431. rootPaths.add (File::getSpecialLocation (File::userPicturesDirectory).getFullPathName());
  432. rootNames.add (TRANS("Pictures"));
  433. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  434. rootNames.add (TRANS("Desktop"));
  435. #elif JUCE_MAC
  436. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  437. rootNames.add (TRANS("Home folder"));
  438. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  439. rootNames.add (TRANS("Documents"));
  440. rootPaths.add (File::getSpecialLocation (File::userMusicDirectory).getFullPathName());
  441. rootNames.add (TRANS("Music"));
  442. rootPaths.add (File::getSpecialLocation (File::userPicturesDirectory).getFullPathName());
  443. rootNames.add (TRANS("Pictures"));
  444. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  445. rootNames.add (TRANS("Desktop"));
  446. rootPaths.add (String::empty);
  447. rootNames.add (String::empty);
  448. Array<File> volumes;
  449. File vol ("/Volumes");
  450. vol.findChildFiles (volumes, File::findDirectories, false);
  451. for (int i = 0; i < volumes.size(); ++i)
  452. {
  453. const File& volume = volumes.getReference(i);
  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. }