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.

260 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 (String(), nullptr),
  23. DirectoryContentsDisplayComponent (listToShow)
  24. {
  25. setModel (this);
  26. fileList.addChangeListener (this);
  27. }
  28. FileListComponent::~FileListComponent()
  29. {
  30. fileList.removeChangeListener (this);
  31. }
  32. int FileListComponent::getNumSelectedFiles() const
  33. {
  34. return getNumSelectedRows();
  35. }
  36. File FileListComponent::getSelectedFile (int index) const
  37. {
  38. return fileList.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 = fileList.getNumFiles(); --i >= 0;)
  51. {
  52. if (fileList.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 != fileList.getDirectory())
  65. {
  66. lastDirectory = fileList.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), index (0), highlighted (false)
  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.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,
  103. const DirectoryContentsList::FileInfo* const fileInfo,
  104. const int index_,
  105. const bool highlighted_)
  106. {
  107. thread.removeTimeSliceClient (this);
  108. if (highlighted_ != highlighted || index_ != index)
  109. {
  110. index = index_;
  111. highlighted = highlighted_;
  112. repaint();
  113. }
  114. File newFile;
  115. String newFileSize, newModTime;
  116. if (fileInfo != nullptr)
  117. {
  118. newFile = root.getChildFile (fileInfo->filename);
  119. newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
  120. newModTime = fileInfo->modificationTime.formatted ("%d %b '%y %H:%M");
  121. }
  122. if (newFile != file
  123. || fileSize != newFileSize
  124. || modTime != newModTime)
  125. {
  126. file = newFile;
  127. fileSize = newFileSize;
  128. modTime = newModTime;
  129. icon = Image();
  130. isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
  131. repaint();
  132. }
  133. if (file != File() && icon.isNull() && ! isDirectory)
  134. {
  135. updateIcon (true);
  136. if (! icon.isValid())
  137. thread.addTimeSliceClient (this);
  138. }
  139. }
  140. int useTimeSlice() override
  141. {
  142. updateIcon (false);
  143. return -1;
  144. }
  145. void handleAsyncUpdate() override
  146. {
  147. repaint();
  148. }
  149. private:
  150. //==============================================================================
  151. FileListComponent& owner;
  152. TimeSliceThread& thread;
  153. File file;
  154. String fileSize, modTime;
  155. Image icon;
  156. int index;
  157. bool highlighted, isDirectory;
  158. void updateIcon (const bool onlyUpdateIfCached)
  159. {
  160. if (icon.isNull())
  161. {
  162. const int hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
  163. Image im (ImageCache::getFromHashCode (hashCode));
  164. if (im.isNull() && ! onlyUpdateIfCached)
  165. {
  166. im = juce_createIconForFile (file);
  167. if (im.isValid())
  168. ImageCache::addImageToCache (im, hashCode);
  169. }
  170. if (im.isValid())
  171. {
  172. icon = im;
  173. triggerAsyncUpdate();
  174. }
  175. }
  176. }
  177. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  178. };
  179. //==============================================================================
  180. int FileListComponent::getNumRows()
  181. {
  182. return fileList.getNumFiles();
  183. }
  184. void FileListComponent::paintListBoxItem (int, Graphics&, int, int, bool)
  185. {
  186. }
  187. Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
  188. {
  189. jassert (existingComponentToUpdate == nullptr || dynamic_cast<ItemComponent*> (existingComponentToUpdate) != nullptr);
  190. ItemComponent* comp = static_cast<ItemComponent*> (existingComponentToUpdate);
  191. if (comp == nullptr)
  192. comp = new ItemComponent (*this, fileList.getTimeSliceThread());
  193. DirectoryContentsList::FileInfo fileInfo;
  194. comp->update (fileList.getDirectory(),
  195. fileList.getFileInfo (row, fileInfo) ? &fileInfo : nullptr,
  196. row, isSelected);
  197. return comp;
  198. }
  199. void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
  200. {
  201. sendSelectionChangeMessage();
  202. }
  203. void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
  204. {
  205. }
  206. void FileListComponent::returnKeyPressed (int currentSelectedRow)
  207. {
  208. sendDoubleClickMessage (fileList.getFile (currentSelectedRow));
  209. }