JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_FileChooser.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 public FileChooser::Pimpl
32{
33public:
35 : owner (fileChooser),
36 selectsDirectories ((flags & FileBrowserComponent::canSelectDirectories) != 0),
37 selectsFiles ((flags & FileBrowserComponent::canSelectFiles) != 0),
38 warnAboutOverwrite ((flags & FileBrowserComponent::warnAboutOverwriting) != 0),
39
40 filter (selectsFiles ? owner.filters : String(), selectsDirectories ? "*" : String(), {}),
41 browserComponent (flags, owner.startingFile, &filter, preview),
42 dialogBox (owner.title, {}, browserComponent, warnAboutOverwrite,
43 browserComponent.findColour (AlertWindow::backgroundColourId), owner.parent)
44 {}
45
46 ~NonNative() override
47 {
48 dialogBox.exitModalState (0);
49 }
50
51 void launch() override
52 {
53 dialogBox.centreWithDefaultSize (nullptr);
54
56 auto* callback = ModalCallbackFunction::create ([ref] (int r)
57 {
58 if (auto locked = ref.lock())
59 locked->modalStateFinished (r);
60 });
61
62 dialogBox.enterModalState (true, callback, true);
63 }
64
65 void runModally() override
66 {
67 #if JUCE_MODAL_LOOPS_PERMITTED
68 modalStateFinished (dialogBox.show() ? 1 : 0);
69 #else
71 #endif
72 }
73
74private:
75 void modalStateFinished (int returnValue)
76 {
77 Array<URL> result;
78
79 if (returnValue != 0)
80 {
81 for (int i = 0; i < browserComponent.getNumSelectedFiles(); ++i)
82 result.add (URL (browserComponent.getSelectedFile (i)));
83 }
84
85 owner.finished (result);
86 }
87
88 //==============================================================================
89 FileChooser& owner;
90 bool selectsDirectories, selectsFiles, warnAboutOverwrite;
91
92 WildcardFileFilter filter;
93 FileBrowserComponent browserComponent;
94 FileChooserDialogBox dialogBox;
95
97};
98
99//==============================================================================
102 const String& fileFilters,
103 const bool useNativeBox,
106 : title (chooserBoxTitle),
107 filters (fileFilters),
108 startingFile (currentFileOrDirectory),
109 parent (parentComponentToUse),
110 useNativeDialogBox (useNativeBox && isPlatformDialogAvailable()),
111 treatFilePackagesAsDirs (treatFilePackagesAsDirectories)
112{
113 #ifndef JUCE_MAC
114 ignoreUnused (treatFilePackagesAsDirs);
115 #endif
116
117 if (! fileFilters.containsNonWhitespaceChars())
118 filters = "*";
119}
120
122{
123 asyncCallback = nullptr;
124}
125
126#if JUCE_MODAL_LOOPS_PERMITTED
127bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComp)
128{
129 return showDialog (FileBrowserComponent::openMode
131 previewComp);
132}
133
134bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComp)
135{
136 return showDialog (FileBrowserComponent::openMode
139 previewComp);
140}
141
142bool FileChooser::browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComp)
143{
144 return showDialog (FileBrowserComponent::openMode
148 previewComp);
149}
150
151bool FileChooser::browseForFileToSave (const bool warnAboutOverwrite)
152{
153 return showDialog (FileBrowserComponent::saveMode
155 | (warnAboutOverwrite ? FileBrowserComponent::warnAboutOverwriting : 0),
156 nullptr);
157}
158
159bool FileChooser::browseForDirectory()
160{
161 return showDialog (FileBrowserComponent::openMode
163 nullptr);
164}
165
166bool FileChooser::showDialog (const int flags, FilePreviewComponent* const previewComp)
167{
168 detail::FocusRestorer focusRestorer;
169
170 pimpl = createPimpl (flags, previewComp);
171 pimpl->runModally();
172
173 // ensure that the finished function was invoked
174 jassert (pimpl == nullptr);
175
176 return (results.size() > 0);
177}
178#endif
179
180void FileChooser::launchAsync (int flags, std::function<void (const FileChooser&)> callback,
181 FilePreviewComponent* previewComp)
182{
183 // You must specify a callback when using launchAsync
184 jassert (callback);
185
186 // you cannot run two file chooser dialog boxes at the same time
187 jassert (asyncCallback == nullptr);
188
189 asyncCallback = std::move (callback);
190
191 pimpl = createPimpl (flags, previewComp);
192 pimpl->launch();
193}
194
195std::shared_ptr<FileChooser::Pimpl> FileChooser::createPimpl (int flags, FilePreviewComponent* previewComp)
196{
197 results.clear();
198
199 // the preview component needs to be the right size before you pass it in here..
200 jassert (previewComp == nullptr || (previewComp->getWidth() > 10
201 && previewComp->getHeight() > 10));
202
203 if (pimpl != nullptr)
204 {
205 // you cannot run two file chooser dialog boxes at the same time
207 pimpl.reset();
208 }
209
210 // You've set the flags for both saveMode and openMode!
211 jassert (! (((flags & FileBrowserComponent::saveMode) != 0)
212 && ((flags & FileBrowserComponent::openMode) != 0)));
213
214 #if JUCE_WINDOWS
215 const bool selectsFiles = (flags & FileBrowserComponent::canSelectFiles) != 0;
216 const bool selectsDirectories = (flags & FileBrowserComponent::canSelectDirectories) != 0;
217
218 if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
219 #else
220 if (useNativeDialogBox)
221 #endif
222 {
223 return showPlatformDialog (*this, flags, previewComp);
224 }
225
226 return std::make_unique<NonNative> (*this, flags, previewComp);
227}
228
230{
231 Array<File> files;
232
233 for (auto url : getURLResults())
234 if (url.isLocalFile())
235 files.add (url.getLocalFile());
236
237 return files;
238}
239
241{
242 auto fileResults = getResults();
243
244 // if you've used a multiple-file select, you should use the getResults() method
245 // to retrieve all the files that were chosen.
246 jassert (fileResults.size() <= 1);
247
248 return fileResults.getFirst();
249}
250
252{
253 // if you've used a multiple-file select, you should use the getResults() method
254 // to retrieve all the files that were chosen.
255 jassert (results.size() <= 1);
256
257 return results.getFirst();
258}
259
260void FileChooser::finished (const Array<URL>& asyncResults)
261{
262 const auto callback = std::exchange (asyncCallback, nullptr);
263
264 results = asyncResults;
265
266 pimpl.reset();
267
268 if (callback)
269 callback (*this);
270}
271
272#if ! JUCE_ANDROID
274 [[maybe_unused]] const String& fileExtension)
275{
276}
277#endif
278
279//==============================================================================
282
283} // namespace juce
@ backgroundColourId
The background colour for the window.
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
The base class for all JUCE user-interface objects.
void exitModalState(int returnValue=0)
Ends a component's modal state.
int getHeight() const noexcept
Returns the component's height in pixels.
void enterModalState(bool takeKeyboardFocus=true, ModalComponentManager::Callback *callback=nullptr, bool deleteWhenDismissed=false)
Puts the component into a modal state.
int getWidth() const noexcept
Returns the component's width in pixels.
A component for browsing and selecting a file or directory to open or save.
File getSelectedFile(int index) const noexcept
Returns one of the files that the user has chosen.
@ saveMode
specifies that the component should allow the user to specify the name of a file that will be used to...
@ canSelectMultipleItems
specifies that the user can select multiple items.
@ warnAboutOverwriting
specifies that the dialog should warn about overwriting existing files (if possible).
@ canSelectFiles
specifies that the user can select files (can be used in conjunction with canSelectDirectories).
@ canSelectDirectories
specifies that the user can select directories (can be used in conjunction with canSelectFiles).
@ openMode
specifies that the component should allow the user to choose an existing file with the intention of o...
int getNumSelectedFiles() const noexcept
Returns the number of files that the user has got selected.
A file open/save dialog box.
void centreWithDefaultSize(Component *componentToCentreAround=nullptr)
Sets the size of this dialog box to its default and positions it either in the centre of the screen,...
Creates a dialog box to choose a file or directory to load or save.
const Array< URL > & getURLResults() const noexcept
Returns a list of all the files that were chosen during the last call to a browse method.
URL getURLResult() const
Returns the last document that was chosen by one of the browseFor methods.
Array< File > getResults() const noexcept
Returns a list of all the files that were chosen during the last call to a browse method.
void launchAsync(int flags, std::function< void(const FileChooser &)>, FilePreviewComponent *previewComponent=nullptr)
Use this method to launch the file browser window asynchronously.
static void registerCustomMimeTypeForFileExtension(const String &mimeType, const String &fileExtension)
Associate a particular file-extension to a mime-type.
File getResult() const
Returns the last file that was chosen by one of the browseFor methods.
FileChooser(const String &dialogBoxTitle, const File &initialFileOrDirectory=File(), const String &filePatternsAllowed=String(), bool useOSNativeDialogBox=true, bool treatFilePackagesAsDirectories=false, Component *parentComponent=nullptr)
Creates a FileChooser.
Base class for components that live inside a file chooser dialog box and show previews of the files t...
~FilePreviewComponent() override
Destructor.
FilePreviewComponent()
Creates a FilePreviewComponent.
Represents a local file or directory.
Definition juce_File.h:45
static ModalComponentManager::Callback * create(CallbackFn &&fn)
This is a utility function to create a ModalComponentManager::Callback that will call a callable obje...
The JUCE String class!
Definition juce_String.h:53
Represents a URL and has a bunch of useful functions to manipulate it.
Definition juce_URL.h:38
A type of FileFilter that works by wildcard pattern matching.
T exchange(T... args)
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
void ignoreUnused(Types &&...) noexcept
Handy function for avoiding unused variables warning.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T reset(T... args)