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.

252 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. FilenameComponent::FilenameComponent (const String& name,
  19. const File& currentFile,
  20. const bool canEditFilename,
  21. const bool isDirectory,
  22. const bool isForSaving,
  23. const String& fileBrowserWildcard,
  24. const String& enforcedSuffix_,
  25. const String& textWhenNothingSelected)
  26. : Component (name),
  27. maxRecentFiles (30),
  28. isDir (isDirectory),
  29. isSaving (isForSaving),
  30. isFileDragOver (false),
  31. wildcard (fileBrowserWildcard),
  32. enforcedSuffix (enforcedSuffix_)
  33. {
  34. addAndMakeVisible (&filenameBox);
  35. filenameBox.setEditableText (canEditFilename);
  36. filenameBox.addListener (this);
  37. filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
  38. filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
  39. setBrowseButtonText ("...");
  40. setCurrentFile (currentFile, true, dontSendNotification);
  41. }
  42. FilenameComponent::~FilenameComponent()
  43. {
  44. }
  45. //==============================================================================
  46. void FilenameComponent::paintOverChildren (Graphics& g)
  47. {
  48. if (isFileDragOver)
  49. {
  50. g.setColour (Colours::red.withAlpha (0.2f));
  51. g.drawRect (0, 0, getWidth(), getHeight(), 3);
  52. }
  53. }
  54. void FilenameComponent::resized()
  55. {
  56. getLookAndFeel().layoutFilenameComponent (*this, &filenameBox, browseButton);
  57. }
  58. void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
  59. {
  60. browseButtonText = newBrowseButtonText;
  61. lookAndFeelChanged();
  62. }
  63. void FilenameComponent::lookAndFeelChanged()
  64. {
  65. browseButton = nullptr;
  66. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  67. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  68. resized();
  69. browseButton->addListener (this);
  70. }
  71. void FilenameComponent::setTooltip (const String& newTooltip)
  72. {
  73. SettableTooltipClient::setTooltip (newTooltip);
  74. filenameBox.setTooltip (newTooltip);
  75. }
  76. void FilenameComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  77. {
  78. defaultBrowseFile = newDefaultDirectory;
  79. }
  80. void FilenameComponent::buttonClicked (Button*)
  81. {
  82. #if JUCE_MODAL_LOOPS_PERMITTED
  83. FileChooser fc (isDir ? TRANS ("Choose a new directory")
  84. : TRANS ("Choose a new file"),
  85. getCurrentFile() == File::nonexistent ? defaultBrowseFile
  86. : getCurrentFile(),
  87. wildcard);
  88. if (isDir ? fc.browseForDirectory()
  89. : (isSaving ? fc.browseForFileToSave (false)
  90. : fc.browseForFileToOpen()))
  91. {
  92. setCurrentFile (fc.getResult(), true);
  93. }
  94. #else
  95. jassertfalse; // needs rewriting to deal with non-modal environments
  96. #endif
  97. }
  98. void FilenameComponent::comboBoxChanged (ComboBox*)
  99. {
  100. setCurrentFile (getCurrentFile(), true);
  101. }
  102. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  103. {
  104. return true;
  105. }
  106. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  107. {
  108. isFileDragOver = false;
  109. repaint();
  110. const File f (filenames[0]);
  111. if (f.exists() && (f.isDirectory() == isDir))
  112. setCurrentFile (f, true);
  113. }
  114. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  115. {
  116. isFileDragOver = true;
  117. repaint();
  118. }
  119. void FilenameComponent::fileDragExit (const StringArray&)
  120. {
  121. isFileDragOver = false;
  122. repaint();
  123. }
  124. //==============================================================================
  125. File FilenameComponent::getCurrentFile() const
  126. {
  127. File f (filenameBox.getText());
  128. if (enforcedSuffix.isNotEmpty())
  129. f = f.withFileExtension (enforcedSuffix);
  130. return f;
  131. }
  132. void FilenameComponent::setCurrentFile (File newFile,
  133. const bool addToRecentlyUsedList,
  134. NotificationType notification)
  135. {
  136. if (enforcedSuffix.isNotEmpty())
  137. newFile = newFile.withFileExtension (enforcedSuffix);
  138. if (newFile.getFullPathName() != lastFilename)
  139. {
  140. lastFilename = newFile.getFullPathName();
  141. if (addToRecentlyUsedList)
  142. addRecentlyUsedFile (newFile);
  143. filenameBox.setText (lastFilename, true);
  144. if (notification != dontSendNotification)
  145. {
  146. triggerAsyncUpdate();
  147. if (notification == sendNotificationSync)
  148. handleUpdateNowIfNeeded();
  149. }
  150. }
  151. }
  152. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  153. {
  154. filenameBox.setEditableText (shouldBeEditable);
  155. }
  156. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  157. {
  158. StringArray names;
  159. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  160. names.add (filenameBox.getItemText (i));
  161. return names;
  162. }
  163. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  164. {
  165. if (filenames != getRecentlyUsedFilenames())
  166. {
  167. filenameBox.clear();
  168. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  169. filenameBox.addItem (filenames[i], i + 1);
  170. }
  171. }
  172. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  173. {
  174. maxRecentFiles = jmax (1, newMaximum);
  175. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  176. }
  177. void FilenameComponent::addRecentlyUsedFile (const File& file)
  178. {
  179. StringArray files (getRecentlyUsedFilenames());
  180. if (file.getFullPathName().isNotEmpty())
  181. {
  182. files.removeString (file.getFullPathName(), true);
  183. files.insert (0, file.getFullPathName());
  184. setRecentlyUsedFilenames (files);
  185. }
  186. }
  187. //==============================================================================
  188. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  189. {
  190. listeners.add (listener);
  191. }
  192. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  193. {
  194. listeners.remove (listener);
  195. }
  196. void FilenameComponent::handleAsyncUpdate()
  197. {
  198. Component::BailOutChecker checker (this);
  199. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  200. }