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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
  58. {
  59. browseButtonText = newBrowseButtonText;
  60. lookAndFeelChanged();
  61. }
  62. void FilenameComponent::lookAndFeelChanged()
  63. {
  64. browseButton = nullptr;
  65. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  66. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  67. resized();
  68. browseButton->addListener (this);
  69. }
  70. void FilenameComponent::setTooltip (const String& newTooltip)
  71. {
  72. SettableTooltipClient::setTooltip (newTooltip);
  73. filenameBox.setTooltip (newTooltip);
  74. }
  75. void FilenameComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  76. {
  77. defaultBrowseFile = newDefaultDirectory;
  78. }
  79. File FilenameComponent::getLocationToBrowse()
  80. {
  81. return getCurrentFile() == File::nonexistent ? defaultBrowseFile
  82. : getCurrentFile();
  83. }
  84. void FilenameComponent::buttonClicked (Button*)
  85. {
  86. #if JUCE_MODAL_LOOPS_PERMITTED
  87. FileChooser fc (isDir ? TRANS ("Choose a new directory")
  88. : TRANS ("Choose a new file"),
  89. getLocationToBrowse(),
  90. wildcard);
  91. if (isDir ? fc.browseForDirectory()
  92. : (isSaving ? fc.browseForFileToSave (false)
  93. : fc.browseForFileToOpen()))
  94. {
  95. setCurrentFile (fc.getResult(), true);
  96. }
  97. #else
  98. jassertfalse; // needs rewriting to deal with non-modal environments
  99. #endif
  100. }
  101. void FilenameComponent::comboBoxChanged (ComboBox*)
  102. {
  103. setCurrentFile (getCurrentFile(), true);
  104. }
  105. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  106. {
  107. return true;
  108. }
  109. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  110. {
  111. isFileDragOver = false;
  112. repaint();
  113. const File f (filenames[0]);
  114. if (f.exists() && (f.isDirectory() == isDir))
  115. setCurrentFile (f, true);
  116. }
  117. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  118. {
  119. isFileDragOver = true;
  120. repaint();
  121. }
  122. void FilenameComponent::fileDragExit (const StringArray&)
  123. {
  124. isFileDragOver = false;
  125. repaint();
  126. }
  127. //==============================================================================
  128. File FilenameComponent::getCurrentFile() const
  129. {
  130. File f (File::getCurrentWorkingDirectory().getChildFile (filenameBox.getText()));
  131. if (enforcedSuffix.isNotEmpty())
  132. f = f.withFileExtension (enforcedSuffix);
  133. return f;
  134. }
  135. void FilenameComponent::setCurrentFile (File newFile,
  136. const bool addToRecentlyUsedList,
  137. NotificationType notification)
  138. {
  139. if (enforcedSuffix.isNotEmpty())
  140. newFile = newFile.withFileExtension (enforcedSuffix);
  141. if (newFile.getFullPathName() != lastFilename)
  142. {
  143. lastFilename = newFile.getFullPathName();
  144. if (addToRecentlyUsedList)
  145. addRecentlyUsedFile (newFile);
  146. filenameBox.setText (lastFilename, dontSendNotification);
  147. if (notification != dontSendNotification)
  148. {
  149. triggerAsyncUpdate();
  150. if (notification == sendNotificationSync)
  151. handleUpdateNowIfNeeded();
  152. }
  153. }
  154. }
  155. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  156. {
  157. filenameBox.setEditableText (shouldBeEditable);
  158. }
  159. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  160. {
  161. StringArray names;
  162. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  163. names.add (filenameBox.getItemText (i));
  164. return names;
  165. }
  166. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  167. {
  168. if (filenames != getRecentlyUsedFilenames())
  169. {
  170. filenameBox.clear();
  171. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  172. filenameBox.addItem (filenames[i], i + 1);
  173. }
  174. }
  175. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  176. {
  177. maxRecentFiles = jmax (1, newMaximum);
  178. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  179. }
  180. void FilenameComponent::addRecentlyUsedFile (const File& file)
  181. {
  182. StringArray files (getRecentlyUsedFilenames());
  183. if (file.getFullPathName().isNotEmpty())
  184. {
  185. files.removeString (file.getFullPathName(), true);
  186. files.insert (0, file.getFullPathName());
  187. setRecentlyUsedFilenames (files);
  188. }
  189. }
  190. //==============================================================================
  191. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  192. {
  193. listeners.add (listener);
  194. }
  195. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  196. {
  197. listeners.remove (listener);
  198. }
  199. void FilenameComponent::handleAsyncUpdate()
  200. {
  201. Component::BailOutChecker checker (this);
  202. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  203. }