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.

268 lines
7.9KB

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