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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. KeyboardFocusTraverser* FilenameComponent::createFocusTraverser()
  59. {
  60. // This prevents the sub-components from grabbing focus if the
  61. // FilenameComponent has been set to refuse focus.
  62. return getWantsKeyboardFocus() ? Component::createFocusTraverser() : 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. #if JUCE_MODAL_LOOPS_PERMITTED
  96. FileChooser fc (isDir ? TRANS ("Choose a new directory")
  97. : TRANS ("Choose a new file"),
  98. getLocationToBrowse(),
  99. wildcard);
  100. if (isDir ? fc.browseForDirectory()
  101. : (isSaving ? fc.browseForFileToSave (false)
  102. : fc.browseForFileToOpen()))
  103. {
  104. setCurrentFile (fc.getResult(), true);
  105. }
  106. #else
  107. ignoreUnused (isSaving);
  108. jassertfalse; // needs rewriting to deal with non-modal environments
  109. #endif
  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. auto 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. auto 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, [this] (FilenameComponentListener& l) { l.filenameComponentChanged (this); });
  213. }
  214. } // namespace juce