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.5KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. FilenameComponent::FilenameComponent (const String& name,
  21. const File& currentFile,
  22. const bool canEditFilename,
  23. const bool isDirectory,
  24. const bool isForSaving,
  25. const String& fileBrowserWildcard,
  26. const String& enforcedSuffix_,
  27. const String& textWhenNothingSelected)
  28. : Component (name),
  29. maxRecentFiles (30),
  30. isDir (isDirectory),
  31. isSaving (isForSaving),
  32. isFileDragOver (false),
  33. wildcard (fileBrowserWildcard),
  34. enforcedSuffix (enforcedSuffix_)
  35. {
  36. addAndMakeVisible (&filenameBox);
  37. filenameBox.setEditableText (canEditFilename);
  38. filenameBox.addListener (this);
  39. filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
  40. filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
  41. setBrowseButtonText ("...");
  42. setCurrentFile (currentFile, true);
  43. }
  44. FilenameComponent::~FilenameComponent()
  45. {
  46. }
  47. //==============================================================================
  48. void FilenameComponent::paintOverChildren (Graphics& g)
  49. {
  50. if (isFileDragOver)
  51. {
  52. g.setColour (Colours::red.withAlpha (0.2f));
  53. g.drawRect (0, 0, getWidth(), getHeight(), 3);
  54. }
  55. }
  56. void FilenameComponent::resized()
  57. {
  58. getLookAndFeel().layoutFilenameComponent (*this, &filenameBox, browseButton);
  59. }
  60. void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
  61. {
  62. browseButtonText = newBrowseButtonText;
  63. lookAndFeelChanged();
  64. }
  65. void FilenameComponent::lookAndFeelChanged()
  66. {
  67. browseButton = nullptr;
  68. addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
  69. browseButton->setConnectedEdges (Button::ConnectedOnLeft);
  70. resized();
  71. browseButton->addListener (this);
  72. }
  73. void FilenameComponent::setTooltip (const String& newTooltip)
  74. {
  75. SettableTooltipClient::setTooltip (newTooltip);
  76. filenameBox.setTooltip (newTooltip);
  77. }
  78. void FilenameComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  79. {
  80. defaultBrowseFile = newDefaultDirectory;
  81. }
  82. void FilenameComponent::buttonClicked (Button*)
  83. {
  84. #if JUCE_MODAL_LOOPS_PERMITTED
  85. FileChooser fc (TRANS("Choose a new file"),
  86. getCurrentFile() == File::nonexistent ? defaultBrowseFile
  87. : getCurrentFile(),
  88. wildcard);
  89. if (isDir ? fc.browseForDirectory()
  90. : (isSaving ? fc.browseForFileToSave (false)
  91. : fc.browseForFileToOpen()))
  92. {
  93. setCurrentFile (fc.getResult(), true);
  94. }
  95. #else
  96. jassertfalse; // needs rewriting to deal with non-modal environments
  97. #endif
  98. }
  99. void FilenameComponent::comboBoxChanged (ComboBox*)
  100. {
  101. setCurrentFile (getCurrentFile(), true);
  102. }
  103. bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
  104. {
  105. return true;
  106. }
  107. void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
  108. {
  109. isFileDragOver = false;
  110. repaint();
  111. const File f (filenames[0]);
  112. if (f.exists() && (f.isDirectory() == isDir))
  113. setCurrentFile (f, true);
  114. }
  115. void FilenameComponent::fileDragEnter (const StringArray&, int, int)
  116. {
  117. isFileDragOver = true;
  118. repaint();
  119. }
  120. void FilenameComponent::fileDragExit (const StringArray&)
  121. {
  122. isFileDragOver = false;
  123. repaint();
  124. }
  125. //==============================================================================
  126. File FilenameComponent::getCurrentFile() const
  127. {
  128. File f (filenameBox.getText());
  129. if (enforcedSuffix.isNotEmpty())
  130. f = f.withFileExtension (enforcedSuffix);
  131. return f;
  132. }
  133. void FilenameComponent::setCurrentFile (File newFile,
  134. const bool addToRecentlyUsedList,
  135. const bool sendChangeNotification)
  136. {
  137. if (enforcedSuffix.isNotEmpty())
  138. newFile = newFile.withFileExtension (enforcedSuffix);
  139. if (newFile.getFullPathName() != lastFilename)
  140. {
  141. lastFilename = newFile.getFullPathName();
  142. if (addToRecentlyUsedList)
  143. addRecentlyUsedFile (newFile);
  144. filenameBox.setText (lastFilename, true);
  145. if (sendChangeNotification)
  146. triggerAsyncUpdate();
  147. }
  148. }
  149. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  150. {
  151. filenameBox.setEditableText (shouldBeEditable);
  152. }
  153. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  154. {
  155. StringArray names;
  156. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  157. names.add (filenameBox.getItemText (i));
  158. return names;
  159. }
  160. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  161. {
  162. if (filenames != getRecentlyUsedFilenames())
  163. {
  164. filenameBox.clear();
  165. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  166. filenameBox.addItem (filenames[i], i + 1);
  167. }
  168. }
  169. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  170. {
  171. maxRecentFiles = jmax (1, newMaximum);
  172. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  173. }
  174. void FilenameComponent::addRecentlyUsedFile (const File& file)
  175. {
  176. StringArray files (getRecentlyUsedFilenames());
  177. if (file.getFullPathName().isNotEmpty())
  178. {
  179. files.removeString (file.getFullPathName(), true);
  180. files.insert (0, file.getFullPathName());
  181. setRecentlyUsedFilenames (files);
  182. }
  183. }
  184. //==============================================================================
  185. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  186. {
  187. listeners.add (listener);
  188. }
  189. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  190. {
  191. listeners.remove (listener);
  192. }
  193. void FilenameComponent::handleAsyncUpdate()
  194. {
  195. Component::BailOutChecker checker (this);
  196. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  197. }
  198. END_JUCE_NAMESPACE