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.

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