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.

271 lines
7.8KB

  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. FilenameComponent::FilenameComponent (const String& name,
  20. const File& currentFile,
  21. const bool canEditFilename,
  22. const bool isDirectory,
  23. const bool isForSaving,
  24. const String& fileBrowserWildcard,
  25. const String& suffix,
  26. const String& textWhenNothingSelected)
  27. : Component (name),
  28. maxRecentFiles (30),
  29. isDir (isDirectory),
  30. isSaving (isForSaving),
  31. isFileDragOver (false),
  32. wildcard (fileBrowserWildcard),
  33. enforcedSuffix (suffix)
  34. {
  35. addAndMakeVisible (filenameBox);
  36. filenameBox.setEditableText (canEditFilename);
  37. filenameBox.addListener (this);
  38. filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
  39. filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
  40. setBrowseButtonText ("...");
  41. setCurrentFile (currentFile, true, dontSendNotification);
  42. }
  43. FilenameComponent::~FilenameComponent()
  44. {
  45. }
  46. //==============================================================================
  47. void FilenameComponent::paintOverChildren (Graphics& g)
  48. {
  49. if (isFileDragOver)
  50. {
  51. g.setColour (Colours::red.withAlpha (0.2f));
  52. g.drawRect (getLocalBounds(), 3);
  53. }
  54. }
  55. void FilenameComponent::resized()
  56. {
  57. getLookAndFeel().layoutFilenameComponent (*this, &filenameBox, browseButton);
  58. }
  59. KeyboardFocusTraverser* FilenameComponent::createFocusTraverser()
  60. {
  61. // This prevents the sub-components from grabbing focus if the
  62. // FilenameComponent has been set to refuse focus.
  63. return getWantsKeyboardFocus() ? Component::createFocusTraverser() : nullptr;
  64. }
  65. void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
  66. {
  67. browseButtonText = newBrowseButtonText;
  68. lookAndFeelChanged();
  69. }
  70. void FilenameComponent::lookAndFeelChanged()
  71. {
  72. browseButton = nullptr;
  73. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  74. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  75. resized();
  76. browseButton->addListener (this);
  77. }
  78. void FilenameComponent::setTooltip (const String& newTooltip)
  79. {
  80. SettableTooltipClient::setTooltip (newTooltip);
  81. filenameBox.setTooltip (newTooltip);
  82. }
  83. void FilenameComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  84. {
  85. defaultBrowseFile = newDefaultDirectory;
  86. }
  87. File FilenameComponent::getLocationToBrowse()
  88. {
  89. return getCurrentFile() == File() ? defaultBrowseFile
  90. : getCurrentFile();
  91. }
  92. void FilenameComponent::buttonClicked (Button*)
  93. {
  94. #if JUCE_MODAL_LOOPS_PERMITTED
  95. FileChooser fc (isDir ? TRANS ("Choose a new directory")
  96. : TRANS ("Choose a new file"),
  97. getLocationToBrowse(),
  98. wildcard);
  99. if (isDir ? fc.browseForDirectory()
  100. : (isSaving ? fc.browseForFileToSave (false)
  101. : fc.browseForFileToOpen()))
  102. {
  103. setCurrentFile (fc.getResult(), true);
  104. }
  105. #else
  106. ignoreUnused (isSaving);
  107. jassertfalse; // needs rewriting to deal with non-modal environments
  108. #endif
  109. }
  110. void FilenameComponent::comboBoxChanged (ComboBox*)
  111. {
  112. setCurrentFile (getCurrentFile(), true);
  113. }
  114. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  115. {
  116. return true;
  117. }
  118. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  119. {
  120. isFileDragOver = false;
  121. repaint();
  122. const File f (filenames[0]);
  123. if (f.exists() && (f.isDirectory() == isDir))
  124. setCurrentFile (f, true);
  125. }
  126. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  127. {
  128. isFileDragOver = true;
  129. repaint();
  130. }
  131. void FilenameComponent::fileDragExit (const StringArray&)
  132. {
  133. isFileDragOver = false;
  134. repaint();
  135. }
  136. //==============================================================================
  137. String FilenameComponent::getCurrentFileText() const
  138. {
  139. return filenameBox.getText();
  140. }
  141. File FilenameComponent::getCurrentFile() const
  142. {
  143. File f (File::getCurrentWorkingDirectory().getChildFile (getCurrentFileText()));
  144. if (enforcedSuffix.isNotEmpty())
  145. f = f.withFileExtension (enforcedSuffix);
  146. return f;
  147. }
  148. void FilenameComponent::setCurrentFile (File newFile,
  149. const bool addToRecentlyUsedList,
  150. NotificationType notification)
  151. {
  152. if (enforcedSuffix.isNotEmpty())
  153. newFile = newFile.withFileExtension (enforcedSuffix);
  154. if (newFile.getFullPathName() != lastFilename)
  155. {
  156. lastFilename = newFile.getFullPathName();
  157. if (addToRecentlyUsedList)
  158. addRecentlyUsedFile (newFile);
  159. filenameBox.setText (lastFilename, dontSendNotification);
  160. if (notification != dontSendNotification)
  161. {
  162. triggerAsyncUpdate();
  163. if (notification == sendNotificationSync)
  164. handleUpdateNowIfNeeded();
  165. }
  166. }
  167. }
  168. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  169. {
  170. filenameBox.setEditableText (shouldBeEditable);
  171. }
  172. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  173. {
  174. StringArray names;
  175. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  176. names.add (filenameBox.getItemText (i));
  177. return names;
  178. }
  179. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  180. {
  181. if (filenames != getRecentlyUsedFilenames())
  182. {
  183. filenameBox.clear();
  184. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  185. filenameBox.addItem (filenames[i], i + 1);
  186. }
  187. }
  188. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  189. {
  190. maxRecentFiles = jmax (1, newMaximum);
  191. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  192. }
  193. void FilenameComponent::addRecentlyUsedFile (const File& file)
  194. {
  195. StringArray files (getRecentlyUsedFilenames());
  196. if (file.getFullPathName().isNotEmpty())
  197. {
  198. files.removeString (file.getFullPathName(), true);
  199. files.insert (0, file.getFullPathName());
  200. setRecentlyUsedFilenames (files);
  201. }
  202. }
  203. //==============================================================================
  204. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  205. {
  206. listeners.add (listener);
  207. }
  208. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  209. {
  210. listeners.remove (listener);
  211. }
  212. void FilenameComponent::handleAsyncUpdate()
  213. {
  214. Component::BailOutChecker checker (this);
  215. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  216. }