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_FilenameComponent.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
30 const File& currentFile,
31 bool canEditFilename,
32 bool isDirectory,
33 bool isForSaving,
35 const String& suffix,
36 const String& textWhenNothingSelected)
37 : Component (name),
38 isDir (isDirectory),
39 isSaving (isForSaving),
40 wildcard (fileBrowserWildcard),
41 enforcedSuffix (suffix)
42{
43 addAndMakeVisible (filenameBox);
44 filenameBox.setEditableText (canEditFilename);
45 filenameBox.setTextWhenNothingSelected (textWhenNothingSelected);
46 filenameBox.setTextWhenNoChoicesAvailable (TRANS ("(no recently selected files)"));
47 filenameBox.onChange = [this] { setCurrentFile (getCurrentFile(), true); };
48
49 setBrowseButtonText ("...");
50
52}
53
57
58//==============================================================================
60{
61 if (isFileDragOver)
62 {
63 g.setColour (Colours::red.withAlpha (0.2f));
64 g.drawRect (getLocalBounds(), 3);
65 }
66}
67
69{
70 getLookAndFeel().layoutFilenameComponent (*this, &filenameBox, browseButton.get());
71}
72
74{
75 // This prevents the sub-components from grabbing focus if the
76 // FilenameComponent has been set to refuse focus.
78}
79
85
87{
88 browseButton.reset();
89 browseButton.reset (getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
90 addAndMakeVisible (browseButton.get());
91 browseButton->setConnectedEdges (Button::ConnectedOnLeft);
92 browseButton->onClick = [this] { showChooser(); };
93 resized();
94}
95
101
106
108{
109 if (lastFilename.isEmpty() && defaultBrowseFile != File())
110 return defaultBrowseFile;
111
112 return getCurrentFile();
113}
114
115void FilenameComponent::showChooser()
116{
117 chooser = std::make_unique<FileChooser> (isDir ? TRANS ("Choose a new directory")
118 : TRANS ("Choose a new file"),
120 wildcard);
121
125
126 chooser->launchAsync (chooserFlags, [this] (const FileChooser&)
127 {
128 if (chooser->getResult() == File{})
129 return;
130
131 setCurrentFile (chooser->getResult(), true);
132 });
133}
134
136{
137 return true;
138}
139
141{
142 isFileDragOver = false;
143 repaint();
144
145 const File f (filenames[0]);
146
147 if (f.exists() && (f.isDirectory() == isDir))
148 setCurrentFile (f, true);
149}
150
152{
153 isFileDragOver = true;
154 repaint();
155}
156
158{
159 isFileDragOver = false;
160 repaint();
161}
162
163//==============================================================================
165{
166 return filenameBox.getText();
167}
168
170{
172
173 if (enforcedSuffix.isNotEmpty())
174 f = f.withFileExtension (enforcedSuffix);
175
176 return f;
177}
178
180 const bool addToRecentlyUsedList,
182{
183 if (enforcedSuffix.isNotEmpty())
184 newFile = newFile.withFileExtension (enforcedSuffix);
185
186 if (newFile.getFullPathName() != lastFilename)
187 {
188 lastFilename = newFile.getFullPathName();
189
192
193 filenameBox.setText (lastFilename, dontSendNotification);
194
196 {
198
201 }
202 }
203}
204
209
211{
212 StringArray names;
213
214 for (int i = 0; i < filenameBox.getNumItems(); ++i)
215 names.add (filenameBox.getItemText (i));
216
217 return names;
218}
219
221{
223 {
224 filenameBox.clear();
225
226 for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
227 filenameBox.addItem (filenames[i], i + 1);
228 }
229}
230
237
239{
240 auto files = getRecentlyUsedFilenames();
241
242 if (file.getFullPathName().isNotEmpty())
243 {
244 files.removeString (file.getFullPathName(), true);
245 files.insert (0, file.getFullPathName());
246
248 }
249}
250
251//==============================================================================
253{
254 listeners.add (listener);
255}
256
258{
259 listeners.remove (listener);
260}
261
262void FilenameComponent::handleAsyncUpdate()
263{
265 listeners.callChecked (checker, [this] (FilenameComponentListener& l) { l.filenameComponentChanged (this); });
266}
267
268} // namespace juce
void handleUpdateNowIfNeeded()
If an update has been triggered and is pending, this will invoke it synchronously.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
void setText(const String &newText, NotificationType notification=sendNotificationAsync)
Sets the contents of the combo-box's text field.
void clear(NotificationType notification=sendNotificationAsync)
Removes all the items from the drop-down list.
std::function< void()> onChange
You can assign a lambda to this callback object to have it called when the selected ID is changed.
void setTextWhenNothingSelected(const String &newMessage)
Sets a message to display when there is no item currently selected.
String getItemText(int index) const
Returns the text for one of the items in the list.
void setTooltip(const String &newTooltip) override
Gives the ComboBox a tooltip.
void setEditableText(bool isEditable)
Sets whether the text in the combo-box is editable.
String getText() const
Returns the text that is currently shown in the combo-box's text field.
void addItem(const String &newItemText, int newItemId)
Adds an item to be shown in the drop-down list.
int getNumItems() const noexcept
Returns the number of items that have been added to the list.
void setTextWhenNoChoicesAvailable(const String &newMessage)
Sets the message to show when there are no items in the list, and the user clicks on the drop-down bo...
A class to keep an eye on a component and check for it being deleted.
The base class for all JUCE user-interface objects.
virtual std::unique_ptr< ComponentTraverser > createKeyboardFocusTraverser()
Creates a ComponentTraverser object to use to determine the logic by which keyboard focus should be p...
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
void repaint()
Marks the whole component as needing to be redrawn.
bool getWantsKeyboardFocus() const noexcept
Returns true if the component is interested in getting keyboard focus.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
@ saveMode
specifies that the component should allow the user to specify the name of a file that will be used to...
@ 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...
Creates a dialog box to choose a file or directory to load or save.
Represents a local file or directory.
Definition juce_File.h:45
bool isDirectory() const
Checks whether the file is a directory that exists.
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition juce_File.h:153
File getChildFile(StringRef relativeOrAbsolutePath) const
Returns a file that represents a relative (or absolute) sub-path of the current one.
static File getCurrentWorkingDirectory()
Returns the current working directory.
File withFileExtension(StringRef newExtension) const
Returns a version of this file with a different file extension.
bool exists() const
Checks whether the file actually exists.
Listens for events happening to a FilenameComponent.
void addRecentlyUsedFile(const File &file)
Adds an entry to the recently-used files dropdown list.
void lookAndFeelChanged() override
Called to let the component react to a change in the look-and-feel setting.
File getCurrentFile() const
Returns the currently displayed filename.
void setRecentlyUsedFilenames(const StringArray &filenames)
Sets all the entries on the recent files list.
bool isInterestedInFileDrag(const StringArray &) override
Callback to check whether this target is interested in the set of files being offered.
String getCurrentFileText() const
Returns the raw text that the user has entered.
void setDefaultBrowseTarget(const File &newDefaultDirectory)
Sets a file or directory to be the default starting point for the browser to show.
void removeListener(FilenameComponentListener *listener)
Removes a previously-registered listener.
void setFilenameIsEditable(bool shouldBeEditable)
Changes whether the use can type into the filename box.
void fileDragExit(const StringArray &) override
Callback to indicate that the mouse has moved away from this component.
void paintOverChildren(Graphics &) override
Components can override this method to draw over the top of their children.
~FilenameComponent() override
Destructor.
void fileDragEnter(const StringArray &, int, int) override
Callback to indicate that some files are being dragged over this component.
void setBrowseButtonText(const String &browseButtonText)
Changes the text shown on the 'browse' button.
std::unique_ptr< ComponentTraverser > createKeyboardFocusTraverser() override
Creates a ComponentTraverser object to use to determine the logic by which keyboard focus should be p...
StringArray getRecentlyUsedFilenames() const
Returns all the entries on the recent files list.
virtual File getLocationToBrowse()
This can be overridden to return a custom location that you want the dialog box to show when the brow...
FilenameComponent(const String &name, const File &currentFile, bool canEditFilename, bool isDirectory, bool isForSaving, const String &fileBrowserWildcard, const String &enforcedSuffix, const String &textWhenNothingSelected)
Creates a FilenameComponent.
void setMaxNumberOfRecentFiles(int newMaximum)
Changes the limit for the number of files that will be stored in the recent-file list.
void filesDropped(const StringArray &, int, int) override
Callback to indicate that the user has dropped the files onto this component.
void resized() override
Called when this component's size has been changed.
void addListener(FilenameComponentListener *listener)
Adds a listener that will be called when the selected file is changed.
void setCurrentFile(File newFile, bool addToRecentlyUsedList, NotificationType notification=sendNotificationAsync)
Changes the current filename.
void setTooltip(const String &newTooltip) override
Gives the component a tooltip.
A graphics context, used for drawing a component or image.
void drawRect(int x, int y, int width, int height, int lineThickness=1) const
Draws a rectangular outline, using the current colour or brush.
void setColour(Colour newColour)
Changes the current drawing colour.
virtual void setTooltip(const String &newTooltip)
Assigns a new tooltip to this object.
A special array for holding a list of strings.
void add(String stringToAdd)
Appends a string at the end of the array.
The JUCE String class!
Definition juce_String.h:53
bool isEmpty() const noexcept
Returns true if the string contains no characters.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
NotificationType
These enums are used in various classes to indicate whether a notification event should be sent out.
@ sendNotificationSync
Requests a synchronous notification.
@ dontSendNotification
No notification message should be sent.
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