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.

juce_FilenameComponent.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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