The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

270 lines
7.8KB

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