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.

251 lines
7.4KB

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