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. FilenameComponent::FilenameComponent (const String& name,
  20. const File& currentFile,
  21. const bool canEditFilename,
  22. const bool isDirectory,
  23. const bool isForSaving,
  24. const String& fileBrowserWildcard,
  25. const String& suffix,
  26. const String& textWhenNothingSelected)
  27. : Component (name),
  28. maxRecentFiles (30),
  29. isDir (isDirectory),
  30. isSaving (isForSaving),
  31. isFileDragOver (false),
  32. wildcard (fileBrowserWildcard),
  33. enforcedSuffix (suffix)
  34. {
  35. addAndMakeVisible (filenameBox);
  36. filenameBox.setEditableText (canEditFilename);
  37. filenameBox.addListener (this);
  38. filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
  39. filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
  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 = nullptr;
  73. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  74. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  75. resized();
  76. browseButton->addListener (this);
  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::buttonClicked (Button*)
  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. jassertfalse; // needs rewriting to deal with non-modal environments
  107. #endif
  108. }
  109. void FilenameComponent::comboBoxChanged (ComboBox*)
  110. {
  111. setCurrentFile (getCurrentFile(), true);
  112. }
  113. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  114. {
  115. return true;
  116. }
  117. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  118. {
  119. isFileDragOver = false;
  120. repaint();
  121. const File f (filenames[0]);
  122. if (f.exists() && (f.isDirectory() == isDir))
  123. setCurrentFile (f, true);
  124. }
  125. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  126. {
  127. isFileDragOver = true;
  128. repaint();
  129. }
  130. void FilenameComponent::fileDragExit (const StringArray&)
  131. {
  132. isFileDragOver = false;
  133. repaint();
  134. }
  135. //==============================================================================
  136. String FilenameComponent::getCurrentFileText() const
  137. {
  138. return filenameBox.getText();
  139. }
  140. File FilenameComponent::getCurrentFile() const
  141. {
  142. File f (File::getCurrentWorkingDirectory().getChildFile (getCurrentFileText()));
  143. if (enforcedSuffix.isNotEmpty())
  144. f = f.withFileExtension (enforcedSuffix);
  145. return f;
  146. }
  147. void FilenameComponent::setCurrentFile (File newFile,
  148. const bool addToRecentlyUsedList,
  149. NotificationType notification)
  150. {
  151. if (enforcedSuffix.isNotEmpty())
  152. newFile = newFile.withFileExtension (enforcedSuffix);
  153. if (newFile.getFullPathName() != lastFilename)
  154. {
  155. lastFilename = newFile.getFullPathName();
  156. if (addToRecentlyUsedList)
  157. addRecentlyUsedFile (newFile);
  158. filenameBox.setText (lastFilename, dontSendNotification);
  159. if (notification != dontSendNotification)
  160. {
  161. triggerAsyncUpdate();
  162. if (notification == sendNotificationSync)
  163. handleUpdateNowIfNeeded();
  164. }
  165. }
  166. }
  167. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  168. {
  169. filenameBox.setEditableText (shouldBeEditable);
  170. }
  171. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  172. {
  173. StringArray names;
  174. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  175. names.add (filenameBox.getItemText (i));
  176. return names;
  177. }
  178. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  179. {
  180. if (filenames != getRecentlyUsedFilenames())
  181. {
  182. filenameBox.clear();
  183. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  184. filenameBox.addItem (filenames[i], i + 1);
  185. }
  186. }
  187. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  188. {
  189. maxRecentFiles = jmax (1, newMaximum);
  190. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  191. }
  192. void FilenameComponent::addRecentlyUsedFile (const File& file)
  193. {
  194. StringArray files (getRecentlyUsedFilenames());
  195. if (file.getFullPathName().isNotEmpty())
  196. {
  197. files.removeString (file.getFullPathName(), true);
  198. files.insert (0, file.getFullPathName());
  199. setRecentlyUsedFilenames (files);
  200. }
  201. }
  202. //==============================================================================
  203. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  204. {
  205. listeners.add (listener);
  206. }
  207. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  208. {
  209. listeners.remove (listener);
  210. }
  211. void FilenameComponent::handleAsyncUpdate()
  212. {
  213. Component::BailOutChecker checker (this);
  214. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  215. }