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.

258 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. Image juce_createIconForFile (const File& file);
  20. //==============================================================================
  21. FileListComponent::FileListComponent (DirectoryContentsList& listToShow)
  22. : ListBox ({}, nullptr),
  23. DirectoryContentsDisplayComponent (listToShow)
  24. {
  25. setModel (this);
  26. directoryContentsList.addChangeListener (this);
  27. }
  28. FileListComponent::~FileListComponent()
  29. {
  30. directoryContentsList.removeChangeListener (this);
  31. }
  32. int FileListComponent::getNumSelectedFiles() const
  33. {
  34. return getNumSelectedRows();
  35. }
  36. File FileListComponent::getSelectedFile (int index) const
  37. {
  38. return directoryContentsList.getFile (getSelectedRow (index));
  39. }
  40. void FileListComponent::deselectAllFiles()
  41. {
  42. deselectAllRows();
  43. }
  44. void FileListComponent::scrollToTop()
  45. {
  46. getVerticalScrollBar()->setCurrentRangeStart (0);
  47. }
  48. void FileListComponent::setSelectedFile (const File& f)
  49. {
  50. for (int i = directoryContentsList.getNumFiles(); --i >= 0;)
  51. {
  52. if (directoryContentsList.getFile(i) == f)
  53. {
  54. selectRow (i);
  55. return;
  56. }
  57. }
  58. deselectAllRows();
  59. }
  60. //==============================================================================
  61. void FileListComponent::changeListenerCallback (ChangeBroadcaster*)
  62. {
  63. updateContent();
  64. if (lastDirectory != directoryContentsList.getDirectory())
  65. {
  66. lastDirectory = directoryContentsList.getDirectory();
  67. deselectAllRows();
  68. }
  69. }
  70. //==============================================================================
  71. class FileListComponent::ItemComponent : public Component,
  72. private TimeSliceClient,
  73. private AsyncUpdater
  74. {
  75. public:
  76. ItemComponent (FileListComponent& fc, TimeSliceThread& t)
  77. : owner (fc), thread (t)
  78. {
  79. }
  80. ~ItemComponent()
  81. {
  82. thread.removeTimeSliceClient (this);
  83. }
  84. //==============================================================================
  85. void paint (Graphics& g) override
  86. {
  87. getLookAndFeel().drawFileBrowserRow (g, getWidth(), getHeight(),
  88. file, file.getFileName(),
  89. &icon, fileSize, modTime,
  90. isDirectory, highlighted,
  91. index, owner);
  92. }
  93. void mouseDown (const MouseEvent& e) override
  94. {
  95. owner.selectRowsBasedOnModifierKeys (index, e.mods, true);
  96. owner.sendMouseClickMessage (file, e);
  97. }
  98. void mouseDoubleClick (const MouseEvent&) override
  99. {
  100. owner.sendDoubleClickMessage (file);
  101. }
  102. void update (const File& root, const DirectoryContentsList::FileInfo* fileInfo,
  103. int newIndex, bool nowHighlighted)
  104. {
  105. thread.removeTimeSliceClient (this);
  106. if (nowHighlighted != highlighted || newIndex != index)
  107. {
  108. index = newIndex;
  109. highlighted = nowHighlighted;
  110. repaint();
  111. }
  112. File newFile;
  113. String newFileSize, newModTime;
  114. if (fileInfo != nullptr)
  115. {
  116. newFile = root.getChildFile (fileInfo->filename);
  117. newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
  118. newModTime = fileInfo->modificationTime.formatted ("%d %b '%y %H:%M");
  119. }
  120. if (newFile != file
  121. || fileSize != newFileSize
  122. || modTime != newModTime)
  123. {
  124. file = newFile;
  125. fileSize = newFileSize;
  126. modTime = newModTime;
  127. icon = Image();
  128. isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
  129. repaint();
  130. }
  131. if (file != File() && icon.isNull() && ! isDirectory)
  132. {
  133. updateIcon (true);
  134. if (! icon.isValid())
  135. thread.addTimeSliceClient (this);
  136. }
  137. }
  138. int useTimeSlice() override
  139. {
  140. updateIcon (false);
  141. return -1;
  142. }
  143. void handleAsyncUpdate() override
  144. {
  145. repaint();
  146. }
  147. private:
  148. //==============================================================================
  149. FileListComponent& owner;
  150. TimeSliceThread& thread;
  151. File file;
  152. String fileSize, modTime;
  153. Image icon;
  154. int index = 0;
  155. bool highlighted = false, isDirectory = false;
  156. void updateIcon (const bool onlyUpdateIfCached)
  157. {
  158. if (icon.isNull())
  159. {
  160. auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
  161. auto im = ImageCache::getFromHashCode (hashCode);
  162. if (im.isNull() && ! onlyUpdateIfCached)
  163. {
  164. im = juce_createIconForFile (file);
  165. if (im.isValid())
  166. ImageCache::addImageToCache (im, hashCode);
  167. }
  168. if (im.isValid())
  169. {
  170. icon = im;
  171. triggerAsyncUpdate();
  172. }
  173. }
  174. }
  175. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  176. };
  177. //==============================================================================
  178. int FileListComponent::getNumRows()
  179. {
  180. return directoryContentsList.getNumFiles();
  181. }
  182. void FileListComponent::paintListBoxItem (int, Graphics&, int, int, bool)
  183. {
  184. }
  185. Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
  186. {
  187. jassert (existingComponentToUpdate == nullptr || dynamic_cast<ItemComponent*> (existingComponentToUpdate) != nullptr);
  188. auto comp = static_cast<ItemComponent*> (existingComponentToUpdate);
  189. if (comp == nullptr)
  190. comp = new ItemComponent (*this, directoryContentsList.getTimeSliceThread());
  191. DirectoryContentsList::FileInfo fileInfo;
  192. comp->update (directoryContentsList.getDirectory(),
  193. directoryContentsList.getFileInfo (row, fileInfo) ? &fileInfo : nullptr,
  194. row, isSelected);
  195. return comp;
  196. }
  197. void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
  198. {
  199. sendSelectionChangeMessage();
  200. }
  201. void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
  202. {
  203. }
  204. void FileListComponent::returnKeyPressed (int currentSelectedRow)
  205. {
  206. sendDoubleClickMessage (directoryContentsList.getFile (currentSelectedRow));
  207. }