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.

268 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);
  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. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  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. return getCurrentFile() == File() ? defaultBrowseFile
  90. : getCurrentFile();
  91. }
  92. void FilenameComponent::showChooser()
  93. {
  94. #if JUCE_MODAL_LOOPS_PERMITTED
  95. FileChooser fc (isDir ? TRANS ("Choose a new directory")
  96. : TRANS ("Choose a new file"),
  97. getLocationToBrowse(),
  98. wildcard);
  99. if (isDir ? fc.browseForDirectory()
  100. : (isSaving ? fc.browseForFileToSave (false)
  101. : fc.browseForFileToOpen()))
  102. {
  103. setCurrentFile (fc.getResult(), true);
  104. }
  105. #else
  106. ignoreUnused (isSaving);
  107. jassertfalse; // needs rewriting to deal with non-modal environments
  108. #endif
  109. }
  110. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  111. {
  112. return true;
  113. }
  114. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  115. {
  116. isFileDragOver = false;
  117. repaint();
  118. const File f (filenames[0]);
  119. if (f.exists() && (f.isDirectory() == isDir))
  120. setCurrentFile (f, true);
  121. }
  122. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  123. {
  124. isFileDragOver = true;
  125. repaint();
  126. }
  127. void FilenameComponent::fileDragExit (const StringArray&)
  128. {
  129. isFileDragOver = false;
  130. repaint();
  131. }
  132. //==============================================================================
  133. String FilenameComponent::getCurrentFileText() const
  134. {
  135. return filenameBox.getText();
  136. }
  137. File FilenameComponent::getCurrentFile() const
  138. {
  139. auto f = File::getCurrentWorkingDirectory().getChildFile (getCurrentFileText());
  140. if (enforcedSuffix.isNotEmpty())
  141. f = f.withFileExtension (enforcedSuffix);
  142. return f;
  143. }
  144. void FilenameComponent::setCurrentFile (File newFile,
  145. const bool addToRecentlyUsedList,
  146. NotificationType notification)
  147. {
  148. if (enforcedSuffix.isNotEmpty())
  149. newFile = newFile.withFileExtension (enforcedSuffix);
  150. if (newFile.getFullPathName() != lastFilename)
  151. {
  152. lastFilename = newFile.getFullPathName();
  153. if (addToRecentlyUsedList)
  154. addRecentlyUsedFile (newFile);
  155. filenameBox.setText (lastFilename, dontSendNotification);
  156. if (notification != dontSendNotification)
  157. {
  158. triggerAsyncUpdate();
  159. if (notification == sendNotificationSync)
  160. handleUpdateNowIfNeeded();
  161. }
  162. }
  163. }
  164. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  165. {
  166. filenameBox.setEditableText (shouldBeEditable);
  167. }
  168. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  169. {
  170. StringArray names;
  171. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  172. names.add (filenameBox.getItemText (i));
  173. return names;
  174. }
  175. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  176. {
  177. if (filenames != getRecentlyUsedFilenames())
  178. {
  179. filenameBox.clear();
  180. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  181. filenameBox.addItem (filenames[i], i + 1);
  182. }
  183. }
  184. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  185. {
  186. maxRecentFiles = jmax (1, newMaximum);
  187. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  188. }
  189. void FilenameComponent::addRecentlyUsedFile (const File& file)
  190. {
  191. auto files = getRecentlyUsedFilenames();
  192. if (file.getFullPathName().isNotEmpty())
  193. {
  194. files.removeString (file.getFullPathName(), true);
  195. files.insert (0, file.getFullPathName());
  196. setRecentlyUsedFilenames (files);
  197. }
  198. }
  199. //==============================================================================
  200. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  201. {
  202. listeners.add (listener);
  203. }
  204. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  205. {
  206. listeners.remove (listener);
  207. }
  208. void FilenameComponent::handleAsyncUpdate()
  209. {
  210. Component::BailOutChecker checker (this);
  211. listeners.callChecked (checker, [this] (FilenameComponentListener& l) { l.filenameComponentChanged (this); });
  212. }
  213. } // namespace juce