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.

269 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. FilenameComponent::FilenameComponent (const String& name,
  21. const File& currentFile,
  22. bool canEditFilename,
  23. bool isDirectory,
  24. bool isForSaving,
  25. const String& fileBrowserWildcard,
  26. const String& suffix,
  27. const String& textWhenNothingSelected)
  28. : Component (name),
  29. isDir (isDirectory),
  30. isSaving (isForSaving),
  31. wildcard (fileBrowserWildcard),
  32. enforcedSuffix (suffix)
  33. {
  34. addAndMakeVisible (filenameBox);
  35. filenameBox.setEditableText (canEditFilename);
  36. filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
  37. filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
  38. filenameBox.onChange = [this] { setCurrentFile (getCurrentFile(), true); };
  39. setBrowseButtonText ("...");
  40. setCurrentFile (currentFile, true, dontSendNotification);
  41. }
  42. FilenameComponent::~FilenameComponent()
  43. {
  44. }
  45. //==============================================================================
  46. void FilenameComponent::paintOverChildren (Graphics& g)
  47. {
  48. if (isFileDragOver)
  49. {
  50. g.setColour (Colours::red.withAlpha (0.2f));
  51. g.drawRect (getLocalBounds(), 3);
  52. }
  53. }
  54. void FilenameComponent::resized()
  55. {
  56. getLookAndFeel().layoutFilenameComponent (*this, &filenameBox, browseButton.get());
  57. }
  58. std::unique_ptr<ComponentTraverser> FilenameComponent::createKeyboardFocusTraverser()
  59. {
  60. // This prevents the sub-components from grabbing focus if the
  61. // FilenameComponent has been set to refuse focus.
  62. return getWantsKeyboardFocus() ? Component::createKeyboardFocusTraverser() : nullptr;
  63. }
  64. void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
  65. {
  66. browseButtonText = newBrowseButtonText;
  67. lookAndFeelChanged();
  68. }
  69. void FilenameComponent::lookAndFeelChanged()
  70. {
  71. browseButton.reset();
  72. browseButton.reset (getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  73. addAndMakeVisible (browseButton.get());
  74. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  75. browseButton->onClick = [this] { showChooser(); };
  76. resized();
  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. if (lastFilename.isEmpty() && defaultBrowseFile != File())
  90. return defaultBrowseFile;
  91. return getCurrentFile();
  92. }
  93. void FilenameComponent::showChooser()
  94. {
  95. chooser = std::make_unique<FileChooser> (isDir ? TRANS ("Choose a new directory")
  96. : TRANS ("Choose a new file"),
  97. getLocationToBrowse(),
  98. wildcard);
  99. auto chooserFlags = isDir ? FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories
  100. : FileBrowserComponent::canSelectFiles | (isSaving ? FileBrowserComponent::saveMode
  101. : FileBrowserComponent::openMode);
  102. chooser->launchAsync (chooserFlags, [this] (const FileChooser&)
  103. {
  104. if (chooser->getResult() == File{})
  105. return;
  106. setCurrentFile (chooser->getResult(), true);
  107. });
  108. }
  109. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  110. {
  111. return true;
  112. }
  113. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  114. {
  115. isFileDragOver = false;
  116. repaint();
  117. const File f (filenames[0]);
  118. if (f.exists() && (f.isDirectory() == isDir))
  119. setCurrentFile (f, true);
  120. }
  121. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  122. {
  123. isFileDragOver = true;
  124. repaint();
  125. }
  126. void FilenameComponent::fileDragExit (const StringArray&)
  127. {
  128. isFileDragOver = false;
  129. repaint();
  130. }
  131. //==============================================================================
  132. String FilenameComponent::getCurrentFileText() const
  133. {
  134. return filenameBox.getText();
  135. }
  136. File FilenameComponent::getCurrentFile() const
  137. {
  138. auto f = File::getCurrentWorkingDirectory().getChildFile (getCurrentFileText());
  139. if (enforcedSuffix.isNotEmpty())
  140. f = f.withFileExtension (enforcedSuffix);
  141. return f;
  142. }
  143. void FilenameComponent::setCurrentFile (File newFile,
  144. const bool addToRecentlyUsedList,
  145. NotificationType notification)
  146. {
  147. if (enforcedSuffix.isNotEmpty())
  148. newFile = newFile.withFileExtension (enforcedSuffix);
  149. if (newFile.getFullPathName() != lastFilename)
  150. {
  151. lastFilename = newFile.getFullPathName();
  152. if (addToRecentlyUsedList)
  153. addRecentlyUsedFile (newFile);
  154. filenameBox.setText (lastFilename, dontSendNotification);
  155. if (notification != dontSendNotification)
  156. {
  157. triggerAsyncUpdate();
  158. if (notification == sendNotificationSync)
  159. handleUpdateNowIfNeeded();
  160. }
  161. }
  162. }
  163. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  164. {
  165. filenameBox.setEditableText (shouldBeEditable);
  166. }
  167. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  168. {
  169. StringArray names;
  170. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  171. names.add (filenameBox.getItemText (i));
  172. return names;
  173. }
  174. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  175. {
  176. if (filenames != getRecentlyUsedFilenames())
  177. {
  178. filenameBox.clear();
  179. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  180. filenameBox.addItem (filenames[i], i + 1);
  181. }
  182. }
  183. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  184. {
  185. maxRecentFiles = jmax (1, newMaximum);
  186. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  187. }
  188. void FilenameComponent::addRecentlyUsedFile (const File& file)
  189. {
  190. auto files = getRecentlyUsedFilenames();
  191. if (file.getFullPathName().isNotEmpty())
  192. {
  193. files.removeString (file.getFullPathName(), true);
  194. files.insert (0, file.getFullPathName());
  195. setRecentlyUsedFilenames (files);
  196. }
  197. }
  198. //==============================================================================
  199. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  200. {
  201. listeners.add (listener);
  202. }
  203. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  204. {
  205. listeners.remove (listener);
  206. }
  207. void FilenameComponent::handleAsyncUpdate()
  208. {
  209. Component::BailOutChecker checker (this);
  210. listeners.callChecked (checker, [this] (FilenameComponentListener& l) { l.filenameComponentChanged (this); });
  211. }
  212. } // namespace juce