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.

247 lines
7.4KB

  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);
  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. const bool sendChangeNotification)
  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 (sendChangeNotification)
  145. triggerAsyncUpdate();
  146. }
  147. }
  148. void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
  149. {
  150. filenameBox.setEditableText (shouldBeEditable);
  151. }
  152. StringArray FilenameComponent::getRecentlyUsedFilenames() const
  153. {
  154. StringArray names;
  155. for (int i = 0; i < filenameBox.getNumItems(); ++i)
  156. names.add (filenameBox.getItemText (i));
  157. return names;
  158. }
  159. void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
  160. {
  161. if (filenames != getRecentlyUsedFilenames())
  162. {
  163. filenameBox.clear();
  164. for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
  165. filenameBox.addItem (filenames[i], i + 1);
  166. }
  167. }
  168. void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
  169. {
  170. maxRecentFiles = jmax (1, newMaximum);
  171. setRecentlyUsedFilenames (getRecentlyUsedFilenames());
  172. }
  173. void FilenameComponent::addRecentlyUsedFile (const File& file)
  174. {
  175. StringArray files (getRecentlyUsedFilenames());
  176. if (file.getFullPathName().isNotEmpty())
  177. {
  178. files.removeString (file.getFullPathName(), true);
  179. files.insert (0, file.getFullPathName());
  180. setRecentlyUsedFilenames (files);
  181. }
  182. }
  183. //==============================================================================
  184. void FilenameComponent::addListener (FilenameComponentListener* const listener)
  185. {
  186. listeners.add (listener);
  187. }
  188. void FilenameComponent::removeListener (FilenameComponentListener* const listener)
  189. {
  190. listeners.remove (listener);
  191. }
  192. void FilenameComponent::handleAsyncUpdate()
  193. {
  194. Component::BailOutChecker checker (this);
  195. listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
  196. }