Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / filebrowser / juce_FileListComponent.cpp
blob28ecc3b6221208620fcdc9abff79d4b5eefb34e0
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_FileListComponent.h"
31 #include "../lookandfeel/juce_LookAndFeel.h"
32 #include "../../graphics/imaging/juce_ImageCache.h"
33 #include "../../../events/juce_AsyncUpdater.h"
35 Image juce_createIconForFile (const File& file);
38 //==============================================================================
39 FileListComponent::FileListComponent (DirectoryContentsList& listToShow)
40 : ListBox (String::empty, nullptr),
41 DirectoryContentsDisplayComponent (listToShow)
43 setModel (this);
44 fileList.addChangeListener (this);
47 FileListComponent::~FileListComponent()
49 fileList.removeChangeListener (this);
52 int FileListComponent::getNumSelectedFiles() const
54 return getNumSelectedRows();
57 const File FileListComponent::getSelectedFile (int index) const
59 return fileList.getFile (getSelectedRow (index));
62 void FileListComponent::deselectAllFiles()
64 deselectAllRows();
67 void FileListComponent::scrollToTop()
69 getVerticalScrollBar()->setCurrentRangeStart (0);
72 //==============================================================================
73 void FileListComponent::changeListenerCallback (ChangeBroadcaster*)
75 updateContent();
77 if (lastDirectory != fileList.getDirectory())
79 lastDirectory = fileList.getDirectory();
80 deselectAllRows();
84 //==============================================================================
85 class FileListItemComponent : public Component,
86 public TimeSliceClient,
87 public AsyncUpdater
89 public:
90 //==============================================================================
91 FileListItemComponent (FileListComponent& owner_, TimeSliceThread& thread_)
92 : owner (owner_), thread (thread_), index (0), highlighted (false)
96 ~FileListItemComponent()
98 thread.removeTimeSliceClient (this);
101 //==============================================================================
102 void paint (Graphics& g)
104 getLookAndFeel().drawFileBrowserRow (g, getWidth(), getHeight(),
105 file.getFileName(),
106 &icon, fileSize, modTime,
107 isDirectory, highlighted,
108 index, owner);
111 void mouseDown (const MouseEvent& e)
113 owner.selectRowsBasedOnModifierKeys (index, e.mods, false);
114 owner.sendMouseClickMessage (file, e);
117 void mouseDoubleClick (const MouseEvent&)
119 owner.sendDoubleClickMessage (file);
122 void update (const File& root,
123 const DirectoryContentsList::FileInfo* const fileInfo,
124 const int index_,
125 const bool highlighted_)
127 thread.removeTimeSliceClient (this);
129 if (highlighted_ != highlighted || index_ != index)
131 index = index_;
132 highlighted = highlighted_;
133 repaint();
136 File newFile;
137 String newFileSize, newModTime;
139 if (fileInfo != nullptr)
141 newFile = root.getChildFile (fileInfo->filename);
142 newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
143 newModTime = fileInfo->modificationTime.formatted ("%d %b '%y %H:%M");
146 if (newFile != file
147 || fileSize != newFileSize
148 || modTime != newModTime)
150 file = newFile;
151 fileSize = newFileSize;
152 modTime = newModTime;
153 icon = Image::null;
154 isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
156 repaint();
159 if (file != File::nonexistent && icon.isNull() && ! isDirectory)
161 updateIcon (true);
163 if (! icon.isValid())
164 thread.addTimeSliceClient (this);
168 int useTimeSlice()
170 updateIcon (false);
171 return -1;
174 void handleAsyncUpdate()
176 repaint();
179 private:
180 //==============================================================================
181 FileListComponent& owner;
182 TimeSliceThread& thread;
183 File file;
184 String fileSize, modTime;
185 Image icon;
186 int index;
187 bool highlighted, isDirectory;
189 void updateIcon (const bool onlyUpdateIfCached)
191 if (icon.isNull())
193 const int hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
194 Image im (ImageCache::getFromHashCode (hashCode));
196 if (im.isNull() && ! onlyUpdateIfCached)
198 im = juce_createIconForFile (file);
200 if (im.isValid())
201 ImageCache::addImageToCache (im, hashCode);
204 if (im.isValid())
206 icon = im;
207 triggerAsyncUpdate();
212 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileListItemComponent);
215 //==============================================================================
216 int FileListComponent::getNumRows()
218 return fileList.getNumFiles();
221 void FileListComponent::paintListBoxItem (int, Graphics&, int, int, bool)
225 Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
227 FileListItemComponent* comp = dynamic_cast <FileListItemComponent*> (existingComponentToUpdate);
229 if (comp == nullptr)
231 delete existingComponentToUpdate;
232 comp = new FileListItemComponent (*this, fileList.getTimeSliceThread());
235 DirectoryContentsList::FileInfo fileInfo;
237 if (fileList.getFileInfo (row, fileInfo))
238 comp->update (fileList.getDirectory(), &fileInfo, row, isSelected);
239 else
240 comp->update (fileList.getDirectory(), nullptr, row, isSelected);
242 return comp;
245 void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
247 sendSelectionChangeMessage();
250 void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
254 void FileListComponent::returnKeyPressed (int currentSelectedRow)
256 sendDoubleClickMessage (fileList.getFile (currentSelectedRow));
260 END_JUCE_NAMESPACE