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.

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