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.9KB

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