LMMS
Loading...
Searching...
No Matches
juce_FileListComponent.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29Image juce_createIconForFile (const File& file);
30
31
32//==============================================================================
34 : ListBox ({}, nullptr),
36 lastDirectory (listToShow.getDirectory())
37{
38 setTitle ("Files");
39 setModel (this);
40 directoryContentsList.addChangeListener (this);
41}
42
44{
45 directoryContentsList.removeChangeListener (this);
46}
47
52
54{
55 return directoryContentsList.getFile (getSelectedRow (index));
56}
57
62
64{
65 getVerticalScrollBar().setCurrentRangeStart (0);
66}
67
69{
70 for (int i = directoryContentsList.getNumFiles(); --i >= 0;)
71 {
72 if (directoryContentsList.getFile (i) == f)
73 {
75
76 selectRow (i);
77 return;
78 }
79 }
80
83}
84
85//==============================================================================
100
101//==============================================================================
103 private TimeSliceClient,
104 private AsyncUpdater
105{
106public:
108 : owner (fc), thread (t)
109 {
110 }
111
112 ~ItemComponent() override
113 {
114 thread.removeTimeSliceClient (this);
115 }
116
117 //==============================================================================
118 void paint (Graphics& g) override
119 {
120 getLookAndFeel().drawFileBrowserRow (g, getWidth(), getHeight(),
121 file, file.getFileName(),
124 index, owner);
125 }
126
127 void mouseDown (const MouseEvent& e) override
128 {
129 owner.selectRowsBasedOnModifierKeys (index, e.mods, true);
130 owner.sendMouseClickMessage (file, e);
131 }
132
133 void mouseDoubleClick (const MouseEvent&) override
134 {
135 owner.sendDoubleClickMessage (file);
136 }
137
138 void update (const File& root, const DirectoryContentsList::FileInfo* fileInfo,
139 int newIndex, bool nowHighlighted)
140 {
141 thread.removeTimeSliceClient (this);
142
143 if (nowHighlighted != highlighted || newIndex != index)
144 {
145 index = newIndex;
146 highlighted = nowHighlighted;
147 repaint();
148 }
149
150 File newFile;
151 String newFileSize, newModTime;
152
153 if (fileInfo != nullptr)
154 {
155 newFile = root.getChildFile (fileInfo->filename);
156 newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
157 newModTime = fileInfo->modificationTime.formatted ("%d %b '%y %H:%M");
158 }
159
160 if (newFile != file
161 || fileSize != newFileSize
162 || modTime != newModTime)
163 {
164 file = newFile;
165 fileSize = newFileSize;
166 modTime = newModTime;
167 icon = Image();
168 isDirectory = fileInfo != nullptr && fileInfo->isDirectory;
169
170 repaint();
171 }
172
173 if (file != File() && icon.isNull() && ! isDirectory)
174 {
175 updateIcon (true);
176
177 if (! icon.isValid())
178 thread.addTimeSliceClient (this);
179 }
180 }
181
182 int useTimeSlice() override
183 {
184 updateIcon (false);
185 return -1;
186 }
187
188 void handleAsyncUpdate() override
189 {
190 repaint();
191 }
192
193private:
194 //==============================================================================
200 int index = 0;
201 bool highlighted = false, isDirectory = false;
202
203 std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
204 {
206 }
207
208 void updateIcon (const bool onlyUpdateIfCached)
209 {
210 if (icon.isNull())
211 {
212 auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
213 auto im = ImageCache::getFromHashCode (hashCode);
214
215 if (im.isNull() && ! onlyUpdateIfCached)
216 {
218
219 if (im.isValid())
220 ImageCache::addImageToCache (im, hashCode);
221 }
222
223 if (im.isValid())
224 {
225 icon = im;
227 }
228 }
229 }
230
232};
233
234//==============================================================================
236{
237 return directoryContentsList.getNumFiles();
238}
239
241{
242 return directoryContentsList.getFile (rowNumber).getFileName();
243}
244
246{
247}
248
249Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
250{
251 jassert (existingComponentToUpdate == nullptr || dynamic_cast<ItemComponent*> (existingComponentToUpdate) != nullptr);
252
253 auto comp = static_cast<ItemComponent*> (existingComponentToUpdate);
254
255 if (comp == nullptr)
256 comp = new ItemComponent (*this, directoryContentsList.getTimeSliceThread());
257
259 comp->update (directoryContentsList.getDirectory(),
260 directoryContentsList.getFileInfo (row, fileInfo) ? &fileInfo : nullptr,
261 row, isSelected);
262
263 return comp;
264}
265
266void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
267{
269}
270
271void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
272{
273}
274
275void FileListComponent::returnKeyPressed (int currentSelectedRow)
276{
277 sendDoubleClickMessage (directoryContentsList.getFile (currentSelectedRow));
278}
279
280} // namespace juce
static String descriptionOfSizeInBytes(int64 bytes)
Definition File.cpp:479
void triggerAsyncUpdate()
Definition juce_AsyncUpdater.cpp:62
AsyncUpdater()
Definition juce_AsyncUpdater.cpp:44
Definition juce_ChangeBroadcaster.h:35
Definition juce_Component.h:36
int getHeight() const noexcept
Definition juce_Component.h:274
void repaint()
Definition juce_Component.cpp:1917
Component() noexcept
Definition juce_Component.cpp:517
static std::unique_ptr< AccessibilityHandler > createIgnoredAccessibilityHandler(Component &)
Definition juce_Component.cpp:3292
int getWidth() const noexcept
Definition juce_Component.h:271
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Definition juce_DirectoryContentsDisplayComponent.h:38
DirectoryContentsList & directoryContentsList
Definition juce_DirectoryContentsDisplayComponent.h:49
void sendSelectionChangeMessage()
Definition juce_DirectoryContentsDisplayComponent.cpp:46
void sendDoubleClickMessage(const File &)
Definition juce_DirectoryContentsDisplayComponent.cpp:61
Definition juce_DirectoryContentsList.h:43
Definition juce_File.h:45
File getChildFile(StringRef relativeOrAbsolutePath) const
Definition juce_File.cpp:412
Definition juce_FileListComponent.cpp:105
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Definition juce_FileListComponent.cpp:203
bool highlighted
Definition juce_FileListComponent.cpp:201
File file
Definition juce_FileListComponent.cpp:197
void updateIcon(const bool onlyUpdateIfCached)
Definition juce_FileListComponent.cpp:208
FileListComponent & owner
Definition juce_FileListComponent.cpp:195
void update(const File &root, const DirectoryContentsList::FileInfo *fileInfo, int newIndex, bool nowHighlighted)
Definition juce_FileListComponent.cpp:138
int useTimeSlice() override
Definition juce_FileListComponent.cpp:182
void mouseDoubleClick(const MouseEvent &) override
Definition juce_FileListComponent.cpp:133
void handleAsyncUpdate() override
Definition juce_FileListComponent.cpp:188
void mouseDown(const MouseEvent &e) override
Definition juce_FileListComponent.cpp:127
~ItemComponent() override
Definition juce_FileListComponent.cpp:112
String modTime
Definition juce_FileListComponent.cpp:198
TimeSliceThread & thread
Definition juce_FileListComponent.cpp:196
bool isDirectory
Definition juce_FileListComponent.cpp:201
void paint(Graphics &g) override
Definition juce_FileListComponent.cpp:118
Image icon
Definition juce_FileListComponent.cpp:199
int index
Definition juce_FileListComponent.cpp:200
String fileSize
Definition juce_FileListComponent.cpp:198
ItemComponent(FileListComponent &fc, TimeSliceThread &t)
Definition juce_FileListComponent.cpp:107
void deleteKeyPressed(int currentSelectedRow) override
Definition juce_FileListComponent.cpp:271
FileListComponent(DirectoryContentsList &listToShow)
Definition juce_FileListComponent.cpp:33
void paintListBoxItem(int, Graphics &, int, int, bool) override
Definition juce_FileListComponent.cpp:245
Component * refreshComponentForRow(int rowNumber, bool isRowSelected, Component *) override
Definition juce_FileListComponent.cpp:249
void selectedRowsChanged(int row) override
Definition juce_FileListComponent.cpp:266
void changeListenerCallback(ChangeBroadcaster *) override
Definition juce_FileListComponent.cpp:86
File lastDirectory
Definition juce_FileListComponent.h:80
void returnKeyPressed(int currentSelectedRow) override
Definition juce_FileListComponent.cpp:275
String getNameForRow(int rowNumber) override
Definition juce_FileListComponent.cpp:240
void deselectAllFiles() override
Definition juce_FileListComponent.cpp:58
void setSelectedFile(const File &) override
Definition juce_FileListComponent.cpp:68
~FileListComponent() override
Definition juce_FileListComponent.cpp:43
File fileWaitingToBeSelected
Definition juce_FileListComponent.h:80
void scrollToTop() override
Definition juce_FileListComponent.cpp:63
int getNumRows() override
Definition juce_FileListComponent.cpp:235
File getSelectedFile(int index=0) const override
Definition juce_FileListComponent.cpp:53
int getNumSelectedFiles() const override
Definition juce_FileListComponent.cpp:48
Definition juce_GraphicsContext.h:45
static void addImageToCache(const Image &image, int64 hashCode)
Definition juce_ImageCache.cpp:125
static Image getFromHashCode(int64 hashCode)
Definition juce_ImageCache.cpp:117
Definition juce_Image.h:58
void deselectAllRows()
Definition juce_ListBox.cpp:765
int getSelectedRow(int index=0) const
Definition juce_ListBox.cpp:807
ScrollBar & getVerticalScrollBar() const noexcept
Definition juce_ListBox.cpp:1032
void updateContent()
Definition juce_ListBox.cpp:622
int getNumSelectedRows() const
Definition juce_ListBox.cpp:802
void selectRow(int rowNumber, bool dontScrollToShowThisRow=false, bool deselectOthersFirst=true)
Definition juce_ListBox.cpp:651
friend class ListBox
Definition juce_ListBox.h:169
Definition juce_MouseEvent.h:39
Definition juce_String.h:53
String formatted(const String &format) const
Definition juce_Time.cpp:341
Definition juce_TimeSliceThread.h:44
friend class TimeSliceThread
Definition juce_TimeSliceThread.h:68
* e
Definition inflate.c:1404
struct huft * t
Definition inflate.c:943
int g
Definition inflate.c:1573
register unsigned i
Definition inflate.c:1575
unsigned f
Definition inflate.c:1572
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
static int JUCE_CDECL comp(const void *a, const void *b)
Definition lsp.c:298
Definition carla_juce.cpp:31
Image juce_createIconForFile(const File &file)
Definition juce_linux_Windowing.cpp:941
@ row
Definition juce_AccessibilityRole.h:53
Definition juce_DirectoryContentsList.h:126
String filename
Definition juce_DirectoryContentsList.h:135
Time modificationTime
Definition juce_DirectoryContentsList.h:143
bool isDirectory
Definition juce_DirectoryContentsList.h:151
int64 fileSize
Definition juce_DirectoryContentsList.h:138
struct zdirent * file
Definition win32.c:1500