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.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. return getCurrentFile() == File() ? defaultBrowseFile
  91. : 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