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.

265 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. Image juce_createIconForFile (const File& file);
  20. //==============================================================================
  21. FileListComponent::FileListComponent (DirectoryContentsList& listToShow)
  22. : ListBox (String::empty, 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 FileListItemComponent : public Component,
  72. public TimeSliceClient,
  73. public AsyncUpdater
  74. {
  75. public:
  76. FileListItemComponent (FileListComponent& owner_, TimeSliceThread& thread_)
  77. : owner (owner_), thread (thread_), index (0), highlighted (false)
  78. {
  79. }
  80. ~FileListItemComponent()
  81. {
  82. thread.removeTimeSliceClient (this);
  83. }
  84. //==============================================================================
  85. void paint (Graphics& g)
  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)
  94. {
  95. owner.selectRowsBasedOnModifierKeys (index, e.mods, false);
  96. owner.sendMouseClickMessage (file, e);
  97. }
  98. void mouseDoubleClick (const MouseEvent&)
  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::null;
  130. isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
  131. repaint();
  132. }
  133. if (file != File::nonexistent && icon.isNull() && ! isDirectory)
  134. {
  135. updateIcon (true);
  136. if (! icon.isValid())
  137. thread.addTimeSliceClient (this);
  138. }
  139. }
  140. int useTimeSlice()
  141. {
  142. updateIcon (false);
  143. return -1;
  144. }
  145. void handleAsyncUpdate()
  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 (FileListItemComponent);
  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. FileListItemComponent* comp = dynamic_cast <FileListItemComponent*> (existingComponentToUpdate);
  190. if (comp == nullptr)
  191. {
  192. delete existingComponentToUpdate;
  193. comp = new FileListItemComponent (*this, fileList.getTimeSliceThread());
  194. }
  195. DirectoryContentsList::FileInfo fileInfo;
  196. comp->update (fileList.getDirectory(),
  197. fileList.getFileInfo (row, fileInfo) ? &fileInfo : nullptr,
  198. row, isSelected);
  199. return comp;
  200. }
  201. void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
  202. {
  203. sendSelectionChangeMessage();
  204. }
  205. void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
  206. {
  207. }
  208. void FileListComponent::returnKeyPressed (int currentSelectedRow)
  209. {
  210. sendDoubleClickMessage (fileList.getFile (currentSelectedRow));
  211. }
  212. END_JUCE_NAMESPACE