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.

575 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. FileBrowserComponent::FileBrowserComponent (int flags_,
  19. const File& initialFileOrDirectory,
  20. const FileFilter* fileFilter_,
  21. FilePreviewComponent* previewComp_)
  22. : FileFilter (String::empty),
  23. fileFilter (fileFilter_),
  24. flags (flags_),
  25. previewComp (previewComp_),
  26. currentPathBox ("path"),
  27. fileLabel ("f", TRANS ("file:")),
  28. thread ("Juce FileBrowser")
  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. }
  89. FileBrowserComponent::~FileBrowserComponent()
  90. {
  91. fileListComponent = nullptr;
  92. fileList = nullptr;
  93. thread.stopThread (10000);
  94. }
  95. //==============================================================================
  96. void FileBrowserComponent::addListener (FileBrowserListener* const newListener)
  97. {
  98. listeners.add (newListener);
  99. }
  100. void FileBrowserComponent::removeListener (FileBrowserListener* const listener)
  101. {
  102. listeners.remove (listener);
  103. }
  104. //==============================================================================
  105. bool FileBrowserComponent::isSaveMode() const noexcept
  106. {
  107. return (flags & saveMode) != 0;
  108. }
  109. int FileBrowserComponent::getNumSelectedFiles() const noexcept
  110. {
  111. if (chosenFiles.size() == 0 && currentFileIsValid())
  112. return 1;
  113. return chosenFiles.size();
  114. }
  115. File FileBrowserComponent::getSelectedFile (int index) const noexcept
  116. {
  117. if ((flags & canSelectDirectories) != 0 && filenameBox.getText().isEmpty())
  118. return currentRoot;
  119. if (! filenameBox.isReadOnly())
  120. return currentRoot.getChildFile (filenameBox.getText());
  121. return chosenFiles[index];
  122. }
  123. bool FileBrowserComponent::currentFileIsValid() const
  124. {
  125. const File f (getSelectedFile (0));
  126. if (isSaveMode())
  127. return (flags & canSelectDirectories) != 0 || ! f.isDirectory();
  128. return f.exists();
  129. }
  130. File FileBrowserComponent::getHighlightedFile() const noexcept
  131. {
  132. return fileListComponent->getSelectedFile (0);
  133. }
  134. void FileBrowserComponent::deselectAllFiles()
  135. {
  136. fileListComponent->deselectAllFiles();
  137. }
  138. //==============================================================================
  139. bool FileBrowserComponent::isFileSuitable (const File& file) const
  140. {
  141. return (flags & canSelectFiles) != 0
  142. && (fileFilter == nullptr || fileFilter->isFileSuitable (file));
  143. }
  144. bool FileBrowserComponent::isDirectorySuitable (const File&) const
  145. {
  146. return true;
  147. }
  148. bool FileBrowserComponent::isFileOrDirSuitable (const File& f) const
  149. {
  150. if (f.isDirectory())
  151. return (flags & canSelectDirectories) != 0
  152. && (fileFilter == nullptr || fileFilter->isDirectorySuitable (f));
  153. return (flags & canSelectFiles) != 0 && f.exists()
  154. && (fileFilter == nullptr || fileFilter->isFileSuitable (f));
  155. }
  156. //==============================================================================
  157. const File& FileBrowserComponent::getRoot() const
  158. {
  159. return currentRoot;
  160. }
  161. void FileBrowserComponent::setRoot (const File& newRootDirectory)
  162. {
  163. bool callListeners = false;
  164. if (currentRoot != newRootDirectory)
  165. {
  166. callListeners = true;
  167. fileListComponent->scrollToTop();
  168. String path (newRootDirectory.getFullPathName());
  169. if (path.isEmpty())
  170. path = File::separatorString;
  171. StringArray rootNames, rootPaths;
  172. getRoots (rootNames, rootPaths);
  173. if (! rootPaths.contains (path, true))
  174. {
  175. bool alreadyListed = false;
  176. for (int i = currentPathBox.getNumItems(); --i >= 0;)
  177. {
  178. if (currentPathBox.getItemText (i).equalsIgnoreCase (path))
  179. {
  180. alreadyListed = true;
  181. break;
  182. }
  183. }
  184. if (! alreadyListed)
  185. currentPathBox.addItem (path, currentPathBox.getNumItems() + 2);
  186. }
  187. }
  188. currentRoot = newRootDirectory;
  189. fileList->setDirectory (currentRoot, true, true);
  190. String currentRootName (currentRoot.getFullPathName());
  191. if (currentRootName.isEmpty())
  192. currentRootName = File::separatorString;
  193. currentPathBox.setText (currentRootName, true);
  194. goUpButton->setEnabled (currentRoot.getParentDirectory().isDirectory()
  195. && currentRoot.getParentDirectory() != currentRoot);
  196. if (callListeners)
  197. {
  198. Component::BailOutChecker checker (this);
  199. listeners.callChecked (checker, &FileBrowserListener::browserRootChanged, currentRoot);
  200. }
  201. }
  202. void FileBrowserComponent::setFileName (const String& newName)
  203. {
  204. filenameBox.setText (newName, true);
  205. fileListComponent->setSelectedFile (currentRoot.getChildFile (newName));
  206. }
  207. void FileBrowserComponent::resetRecentPaths()
  208. {
  209. currentPathBox.clear();
  210. StringArray rootNames, rootPaths;
  211. getRoots (rootNames, rootPaths);
  212. for (int i = 0; i < rootNames.size(); ++i)
  213. {
  214. if (rootNames[i].isEmpty())
  215. currentPathBox.addSeparator();
  216. else
  217. currentPathBox.addItem (rootNames[i], i + 1);
  218. }
  219. currentPathBox.addSeparator();
  220. }
  221. void FileBrowserComponent::goUp()
  222. {
  223. setRoot (getRoot().getParentDirectory());
  224. }
  225. void FileBrowserComponent::refresh()
  226. {
  227. fileList->refresh();
  228. }
  229. void FileBrowserComponent::setFileFilter (const FileFilter* const newFileFilter)
  230. {
  231. if (fileFilter != newFileFilter)
  232. {
  233. fileFilter = newFileFilter;
  234. refresh();
  235. }
  236. }
  237. String FileBrowserComponent::getActionVerb() const
  238. {
  239. return isSaveMode() ? ((flags & canSelectDirectories) != 0 ? TRANS("Choose")
  240. : TRANS("Save"))
  241. : TRANS("Open");
  242. }
  243. void FileBrowserComponent::setFilenameBoxLabel (const String& name)
  244. {
  245. fileLabel.setText (name, false);
  246. }
  247. FilePreviewComponent* FileBrowserComponent::getPreviewComponent() const noexcept
  248. {
  249. return previewComp;
  250. }
  251. DirectoryContentsDisplayComponent* FileBrowserComponent::getDisplayComponent() const noexcept
  252. {
  253. return fileListComponent;
  254. }
  255. //==============================================================================
  256. void FileBrowserComponent::resized()
  257. {
  258. getLookAndFeel()
  259. .layoutFileBrowserComponent (*this, fileListComponent, previewComp,
  260. &currentPathBox, &filenameBox, goUpButton);
  261. }
  262. //==============================================================================
  263. void FileBrowserComponent::sendListenerChangeMessage()
  264. {
  265. Component::BailOutChecker checker (this);
  266. if (previewComp != nullptr)
  267. previewComp->selectedFileChanged (getSelectedFile (0));
  268. // You shouldn't delete the browser when the file gets changed!
  269. jassert (! checker.shouldBailOut());
  270. listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
  271. }
  272. void FileBrowserComponent::selectionChanged()
  273. {
  274. StringArray newFilenames;
  275. bool resetChosenFiles = true;
  276. for (int i = 0; i < fileListComponent->getNumSelectedFiles(); ++i)
  277. {
  278. const File f (fileListComponent->getSelectedFile (i));
  279. if (isFileOrDirSuitable (f))
  280. {
  281. if (resetChosenFiles)
  282. {
  283. chosenFiles.clear();
  284. resetChosenFiles = false;
  285. }
  286. chosenFiles.add (f);
  287. newFilenames.add (f.getRelativePathFrom (getRoot()));
  288. }
  289. }
  290. if (newFilenames.size() > 0)
  291. filenameBox.setText (newFilenames.joinIntoString (", "), false);
  292. sendListenerChangeMessage();
  293. }
  294. void FileBrowserComponent::fileClicked (const File& f, const MouseEvent& e)
  295. {
  296. Component::BailOutChecker checker (this);
  297. listeners.callChecked (checker, &FileBrowserListener::fileClicked, f, e);
  298. }
  299. void FileBrowserComponent::fileDoubleClicked (const File& f)
  300. {
  301. if (f.isDirectory())
  302. {
  303. setRoot (f);
  304. if ((flags & canSelectDirectories) != 0)
  305. filenameBox.setText (String::empty);
  306. }
  307. else
  308. {
  309. Component::BailOutChecker checker (this);
  310. listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, f);
  311. }
  312. }
  313. void FileBrowserComponent::browserRootChanged (const File&) {}
  314. bool FileBrowserComponent::keyPressed (const KeyPress& key)
  315. {
  316. (void) key;
  317. #if JUCE_LINUX || JUCE_WINDOWS
  318. if (key.getModifiers().isCommandDown()
  319. && (key.getKeyCode() == 'H' || key.getKeyCode() == 'h'))
  320. {
  321. fileList->setIgnoresHiddenFiles (! fileList->ignoresHiddenFiles());
  322. fileList->refresh();
  323. return true;
  324. }
  325. #endif
  326. return false;
  327. }
  328. //==============================================================================
  329. void FileBrowserComponent::textEditorTextChanged (TextEditor&)
  330. {
  331. sendListenerChangeMessage();
  332. }
  333. void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&)
  334. {
  335. if (filenameBox.getText().containsChar (File::separator))
  336. {
  337. const File f (currentRoot.getChildFile (filenameBox.getText()));
  338. if (f.isDirectory())
  339. {
  340. setRoot (f);
  341. chosenFiles.clear();
  342. filenameBox.setText (String::empty);
  343. }
  344. else
  345. {
  346. setRoot (f.getParentDirectory());
  347. chosenFiles.clear();
  348. chosenFiles.add (f);
  349. filenameBox.setText (f.getFileName());
  350. }
  351. }
  352. else
  353. {
  354. fileDoubleClicked (getSelectedFile (0));
  355. }
  356. }
  357. void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&)
  358. {
  359. }
  360. void FileBrowserComponent::textEditorFocusLost (TextEditor&)
  361. {
  362. if (! isSaveMode())
  363. selectionChanged();
  364. }
  365. //==============================================================================
  366. void FileBrowserComponent::buttonClicked (Button*)
  367. {
  368. goUp();
  369. }
  370. void FileBrowserComponent::comboBoxChanged (ComboBox*)
  371. {
  372. const String newText (currentPathBox.getText().trim().unquoted());
  373. if (newText.isNotEmpty())
  374. {
  375. const int index = currentPathBox.getSelectedId() - 1;
  376. StringArray rootNames, rootPaths;
  377. getRoots (rootNames, rootPaths);
  378. if (rootPaths [index].isNotEmpty())
  379. {
  380. setRoot (File (rootPaths [index]));
  381. }
  382. else
  383. {
  384. File f (newText);
  385. for (;;)
  386. {
  387. if (f.isDirectory())
  388. {
  389. setRoot (f);
  390. break;
  391. }
  392. if (f.getParentDirectory() == f)
  393. break;
  394. f = f.getParentDirectory();
  395. }
  396. }
  397. }
  398. }
  399. void FileBrowserComponent::getRoots (StringArray& rootNames, StringArray& rootPaths)
  400. {
  401. #if JUCE_WINDOWS
  402. Array<File> roots;
  403. File::findFileSystemRoots (roots);
  404. rootPaths.clear();
  405. for (int i = 0; i < roots.size(); ++i)
  406. {
  407. const File& drive = roots.getReference(i);
  408. String name (drive.getFullPathName());
  409. rootPaths.add (name);
  410. if (drive.isOnHardDisk())
  411. {
  412. String volume (drive.getVolumeLabel());
  413. if (volume.isEmpty())
  414. volume = TRANS("Hard Drive");
  415. name << " [" << volume << ']';
  416. }
  417. else if (drive.isOnCDRomDrive())
  418. {
  419. name << TRANS(" [CD/DVD drive]");
  420. }
  421. rootNames.add (name);
  422. }
  423. rootPaths.add (String::empty);
  424. rootNames.add (String::empty);
  425. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  426. rootNames.add ("Documents");
  427. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  428. rootNames.add ("Desktop");
  429. #elif JUCE_MAC
  430. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  431. rootNames.add ("Home folder");
  432. rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
  433. rootNames.add ("Documents");
  434. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  435. rootNames.add ("Desktop");
  436. rootPaths.add (String::empty);
  437. rootNames.add (String::empty);
  438. Array <File> volumes;
  439. File vol ("/Volumes");
  440. vol.findChildFiles (volumes, File::findDirectories, false);
  441. for (int i = 0; i < volumes.size(); ++i)
  442. {
  443. const File& volume = volumes.getReference(i);
  444. if (volume.isDirectory() && ! volume.getFileName().startsWithChar ('.'))
  445. {
  446. rootPaths.add (volume.getFullPathName());
  447. rootNames.add (volume.getFileName());
  448. }
  449. }
  450. #else
  451. rootPaths.add ("/");
  452. rootNames.add ("/");
  453. rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  454. rootNames.add ("Home folder");
  455. rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
  456. rootNames.add ("Desktop");
  457. #endif
  458. }