LMMS
Loading...
Searching...
No Matches
juce_FileTreeComponent.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
29//==============================================================================
31 private TimeSliceClient,
32 private AsyncUpdater,
33 private ChangeListener
34{
35public:
37 DirectoryContentsList* parentContents,
38 int indexInContents,
39 const File& f,
41 : file (f),
42 owner (treeComp),
43 parentContentsList (parentContents),
44 indexInContentsList (indexInContents),
46 thread (t)
47 {
49
50 if (parentContents != nullptr
51 && parentContents->getFileInfo (indexInContents, fileInfo))
52 {
53 fileSize = File::descriptionOfSizeInBytes (fileInfo.fileSize);
54 modTime = fileInfo.modificationTime.formatted ("%d %b '%y %H:%M");
55 isDirectory = fileInfo.isDirectory;
56 }
57 else
58 {
59 isDirectory = true;
60 }
61 }
62
64 {
65 thread.removeTimeSliceClient (this);
68 }
69
70 //==============================================================================
71 bool mightContainSubItems() override { return isDirectory; }
72 String getUniqueName() const override { return file.getFullPathName(); }
73 int getItemHeight() const override { return owner.getItemHeight(); }
74
75 var getDragSourceDescription() override { return owner.getDragAndDropDescription(); }
76
77 void itemOpennessChanged (bool isNowOpen) override
78 {
79 if (isNowOpen)
80 {
82
83 isDirectory = file.isDirectory();
84
85 if (isDirectory)
86 {
87 if (subContentsList == nullptr && parentContentsList != nullptr)
88 {
89 auto l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
90
91 l->setDirectory (file,
92 parentContentsList->isFindingDirectories(),
93 parentContentsList->isFindingFiles());
94
95 setSubContentsList (l, true);
96 }
97
98 changeListenerCallback (nullptr);
99 }
100 }
101 }
102
104 {
105 if (subContentsList != nullptr)
106 {
107 subContentsList->removeChangeListener (this);
108 subContentsList.reset();
109 }
110 }
111
112 void setSubContentsList (DirectoryContentsList* newList, const bool canDeleteList)
113 {
115
117 newList->addChangeListener (this);
118 }
119
120 bool selectFile (const File& target)
121 {
122 if (file == target)
123 {
124 setSelected (true, true);
125 return true;
126 }
127
128 if (target.isAChildOf (file))
129 {
130 setOpen (true);
131
132 for (int maxRetries = 500; --maxRetries > 0;)
133 {
134 for (int i = 0; i < getNumSubItems(); ++i)
135 if (auto* f = dynamic_cast<FileListTreeItem*> (getSubItem (i)))
136 if (f->selectFile (target))
137 return true;
138
139 // if we've just opened and the contents are still loading, wait for it..
140 if (subContentsList != nullptr && subContentsList->isStillLoading())
141 {
142 Thread::sleep (10);
144 }
145 else
146 {
147 break;
148 }
149 }
150 }
151
152 return false;
153 }
154
159
161 {
163
164 if (isOpen() && subContentsList != nullptr)
165 {
166 for (int i = 0; i < subContentsList->getNumFiles(); ++i)
168 subContentsList->getFile(i), thread));
169 }
170 }
171
172 void paintItem (Graphics& g, int width, int height) override
173 {
174 ScopedLock lock (iconUpdate);
175
176 if (file != File())
177 {
178 updateIcon (true);
179
180 if (icon.isNull())
181 thread.addTimeSliceClient (this);
182 }
183
184 owner.getLookAndFeel().drawFileBrowserRow (g, width, height,
185 file, file.getFileName(),
189 }
190
192 {
193 return file.getFileName();
194 }
195
196 void itemClicked (const MouseEvent& e) override
197 {
198 owner.sendMouseClickMessage (file, e);
199 }
200
201 void itemDoubleClicked (const MouseEvent& e) override
202 {
204
205 owner.sendDoubleClickMessage (file);
206 }
207
208 void itemSelectionChanged (bool) override
209 {
210 owner.sendSelectionChangeMessage();
211 }
212
213 int useTimeSlice() override
214 {
215 updateIcon (false);
216 return -1;
217 }
218
219 void handleAsyncUpdate() override
220 {
221 owner.repaint();
222 }
223
224 const File file;
225
226private:
236
237 void updateIcon (const bool onlyUpdateIfCached)
238 {
239 if (icon.isNull())
240 {
241 auto hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
242 auto im = ImageCache::getFromHashCode (hashCode);
243
244 if (im.isNull() && ! onlyUpdateIfCached)
245 {
247
248 if (im.isValid())
249 ImageCache::addImageToCache (im, hashCode);
250 }
251
252 if (im.isValid())
253 {
254 {
255 ScopedLock lock (iconUpdate);
256 icon = im;
257 }
258
260 }
261 }
262 }
263
265};
266
267//==============================================================================
275
280
282{
284
285 auto root = new FileListTreeItem (*this, nullptr, 0, directoryContentsList.getDirectory(),
286 directoryContentsList.getTimeSliceThread());
287
288 root->setSubContentsList (&directoryContentsList, false);
289 setRootItem (root);
290}
291
292//==============================================================================
294{
295 if (auto* item = dynamic_cast<const FileListTreeItem*> (getSelectedItem (index)))
296 return item->file;
297
298 return {};
299}
300
305
307{
308 getViewport()->getVerticalScrollBar().setCurrentRangeStart (0);
309}
310
312{
313 dragAndDropDescription = description;
314}
315
317{
318 if (auto* t = dynamic_cast<FileListTreeItem*> (getRootItem()))
319 if (! t->selectFile (target))
321}
322
324{
325 if (itemHeight != newHeight)
326 {
327 itemHeight = newHeight;
328
329 if (auto* root = getRootItem())
330 root->treeHasChanged();
331 }
332}
333
334} // namespace juce
#define nullptr
Definition DistrhoDefines.h:75
void triggerAsyncUpdate()
Definition juce_AsyncUpdater.cpp:62
AsyncUpdater()
Definition juce_AsyncUpdater.cpp:44
Definition juce_ChangeBroadcaster.h:35
void addChangeListener(ChangeListener *listener)
Definition juce_ChangeBroadcaster.cpp:35
Definition juce_ChangeListener.h:45
Definition juce_CriticalSection.h:43
DirectoryContentsDisplayComponent(DirectoryContentsList &listToShow)
Definition juce_DirectoryContentsDisplayComponent.cpp:29
DirectoryContentsList & directoryContentsList
Definition juce_DirectoryContentsDisplayComponent.h:49
Definition juce_DirectoryContentsList.h:43
bool getFileInfo(int index, FileInfo &resultInfo) const
Definition juce_DirectoryContentsList.cpp:133
Definition juce_File.h:45
bool isAChildOf(const File &potentialParentDirectory) const
Definition juce_File.cpp:380
Definition juce_FileTreeComponent.cpp:34
void changeListenerCallback(ChangeBroadcaster *) override
Definition juce_FileTreeComponent.cpp:155
String modTime
Definition juce_FileTreeComponent.cpp:235
void paintItem(Graphics &g, int width, int height) override
Definition juce_FileTreeComponent.cpp:172
void rebuildItemsFromContentList()
Definition juce_FileTreeComponent.cpp:160
String fileSize
Definition juce_FileTreeComponent.cpp:235
int getItemHeight() const override
Definition juce_FileTreeComponent.cpp:73
void itemSelectionChanged(bool) override
Definition juce_FileTreeComponent.cpp:208
bool isDirectory
Definition juce_FileTreeComponent.cpp:231
void handleAsyncUpdate() override
Definition juce_FileTreeComponent.cpp:219
var getDragSourceDescription() override
Definition juce_FileTreeComponent.cpp:75
CriticalSection iconUpdate
Definition juce_FileTreeComponent.cpp:233
int indexInContentsList
Definition juce_FileTreeComponent.cpp:229
void updateIcon(const bool onlyUpdateIfCached)
Definition juce_FileTreeComponent.cpp:237
String getAccessibilityName() override
Definition juce_FileTreeComponent.cpp:191
DirectoryContentsList * parentContentsList
Definition juce_FileTreeComponent.cpp:228
const File file
Definition juce_FileTreeComponent.cpp:224
void itemDoubleClicked(const MouseEvent &e) override
Definition juce_FileTreeComponent.cpp:201
~FileListTreeItem() override
Definition juce_FileTreeComponent.cpp:63
void removeSubContentsList()
Definition juce_FileTreeComponent.cpp:103
TimeSliceThread & thread
Definition juce_FileTreeComponent.cpp:232
int useTimeSlice() override
Definition juce_FileTreeComponent.cpp:213
void itemOpennessChanged(bool isNowOpen) override
Definition juce_FileTreeComponent.cpp:77
OptionalScopedPointer< DirectoryContentsList > subContentsList
Definition juce_FileTreeComponent.cpp:230
FileTreeComponent & owner
Definition juce_FileTreeComponent.cpp:227
void setSubContentsList(DirectoryContentsList *newList, const bool canDeleteList)
Definition juce_FileTreeComponent.cpp:112
bool mightContainSubItems() override
Definition juce_FileTreeComponent.cpp:71
FileListTreeItem(FileTreeComponent &treeComp, DirectoryContentsList *parentContents, int indexInContents, const File &f, TimeSliceThread &t)
Definition juce_FileTreeComponent.cpp:36
void itemClicked(const MouseEvent &e) override
Definition juce_FileTreeComponent.cpp:196
bool selectFile(const File &target)
Definition juce_FileTreeComponent.cpp:120
String getUniqueName() const override
Definition juce_FileTreeComponent.cpp:72
Image icon
Definition juce_FileTreeComponent.cpp:234
Definition juce_FileTreeComponent.h:45
File getSelectedFile(int index=0) const override
Definition juce_FileTreeComponent.cpp:293
void refresh()
Definition juce_FileTreeComponent.cpp:281
~FileTreeComponent() override
Definition juce_FileTreeComponent.cpp:276
void setDragAndDropDescription(const String &description)
Definition juce_FileTreeComponent.cpp:311
String dragAndDropDescription
Definition juce_FileTreeComponent.h:99
void setItemHeight(int newHeight)
Definition juce_FileTreeComponent.cpp:323
FileTreeComponent(DirectoryContentsList &listToShow)
Definition juce_FileTreeComponent.cpp:268
int itemHeight
Definition juce_FileTreeComponent.h:100
void deselectAllFiles() override
Definition juce_FileTreeComponent.cpp:301
void scrollToTop() override
Definition juce_FileTreeComponent.cpp:306
void setSelectedFile(const File &) override
Definition juce_FileTreeComponent.cpp:316
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
Definition juce_MouseEvent.h:39
Definition juce_OptionalScopedPointer.h:38
Definition juce_String.h:53
static void JUCE_CALLTYPE sleep(int milliseconds)
Definition juce_posix_SharedCode.h:44
Definition juce_TimeSliceThread.h:44
friend class TimeSliceThread
Definition juce_TimeSliceThread.h:68
TreeViewItem * getSelectedItem(int index) const noexcept
Definition juce_TreeView.cpp:903
TreeViewItem * getRootItem() const noexcept
Definition juce_TreeView.h:691
Viewport * getViewport() const noexcept
Definition juce_TreeView.cpp:886
void clearSelectedItems()
Definition juce_TreeView.cpp:892
void deleteRootItem()
Definition juce_TreeView.cpp:823
void setRootItem(TreeViewItem *newRootItem)
Definition juce_TreeView.cpp:792
void setRootItemVisible(bool shouldBeVisible)
Definition juce_TreeView.cpp:829
bool isSelected() const noexcept
Definition juce_TreeView.cpp:1632
void clearSubItems()
Definition juce_TreeView.cpp:1503
int getNumSubItems() const noexcept
Definition juce_TreeView.cpp:1493
virtual void itemDoubleClicked(const MouseEvent &)
Definition juce_TreeView.cpp:1706
bool isOpen() const noexcept
Definition juce_TreeView.cpp:1600
void setOpen(bool shouldBeOpen)
Definition juce_TreeView.cpp:1608
TreeViewItem()
Definition juce_TreeView.cpp:1472
void addSubItem(TreeViewItem *newItem, int insertPosition=-1)
Definition juce_TreeView.cpp:1525
TreeViewItem * getSubItem(int index) const noexcept
Definition juce_TreeView.cpp:1498
void setSelected(bool shouldBeSelected, bool deselectOtherItemsFirst, NotificationType shouldNotify=sendNotification)
Definition juce_TreeView.cpp:1646
Definition juce_Variant.h:42
* e
Definition inflate.c:1404
int * l
Definition inflate.c:1579
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
static int int height
Definition pugl.h:1594
static int width
Definition pugl.h:1593
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
Image juce_createIconForFile(const File &file)
Definition juce_linux_Windowing.cpp:941
#define false
Definition ordinals.h:83
Definition juce_DirectoryContentsList.h:126