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_FileSearchPathListComponent.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 : addButton ("+"),
31 removeButton ("-"),
32 changeButton (TRANS ("change...")),
35{
36 listBox.setModel (this);
37 addAndMakeVisible (listBox);
38 listBox.setColour (ListBox::backgroundColourId, Colours::black.withAlpha (0.02f));
39 listBox.setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.1f));
40 listBox.setOutlineThickness (1);
41
42 addAndMakeVisible (addButton);
43 addButton.onClick = [this] { addPath(); };
44 addButton.setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);
45
46 addAndMakeVisible (removeButton);
47 removeButton.onClick = [this] { deleteSelected(); };
48 removeButton.setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);
49
50 addAndMakeVisible (changeButton);
51 changeButton.onClick = [this] { editSelected(); };
52
53 addAndMakeVisible (upButton);
54 upButton.onClick = [this] { moveSelection (-1); };
55
56 auto arrowColour = findColour (ListBox::textColourId);
57
58 {
59 Path arrowPath;
60 arrowPath.addArrow ({ 50.0f, 100.0f, 50.0f, 0.0f }, 40.0f, 100.0f, 50.0f);
61 DrawablePath arrowImage;
62 arrowImage.setFill (arrowColour);
63 arrowImage.setPath (arrowPath);
64
65 upButton.setImages (&arrowImage);
66 }
67
68 addAndMakeVisible (downButton);
69 downButton.onClick = [this] { moveSelection (1); };
70
71 {
72 Path arrowPath;
73 arrowPath.addArrow ({ 50.0f, 0.0f, 50.0f, 100.0f }, 40.0f, 100.0f, 50.0f);
74 DrawablePath arrowImage;
75 arrowImage.setFill (arrowColour);
76 arrowImage.setPath (arrowPath);
77
78 downButton.setImages (&arrowImage);
79 }
80
81 updateButtons();
82}
83
84void FileSearchPathListComponent::updateButtons()
85{
86 const bool anythingSelected = listBox.getNumSelectedRows() > 0;
87
88 removeButton.setEnabled (anythingSelected);
89 changeButton.setEnabled (anythingSelected);
91 downButton.setEnabled (anythingSelected);
92}
93
94void FileSearchPathListComponent::changed()
95{
96 listBox.updateContent();
97 listBox.repaint();
98 updateButtons();
99}
100
101//==============================================================================
103{
104 if (newPath.toString() != path.toString())
105 {
106 path = newPath;
107 changed();
108 }
109}
110
115
116//==============================================================================
118{
119 return path.getNumPaths();
120}
121
123{
124 if (rowIsSelected)
126
128 Font f ((float) height * 0.7f);
129 f.setHorizontalScale (0.9f);
130 g.setFont (f);
131
133 4, 0, width - 6, height,
135}
136
138{
139 if (isPositiveAndBelow (row, path.getNumPaths()))
140 {
141 path.remove (row);
142 changed();
143 }
144}
145
147{
148 chooser = std::make_unique<FileChooser> (TRANS ("Change folder..."), path.getRawString (row), "*");
150
151 chooser->launchAsync (chooserFlags, [this, row] (const FileChooser& fc)
152 {
153 if (fc.getResult() == File{})
154 return;
155
156 path.remove (row);
157 path.add (fc.getResult(), row);
158 changed();
159 });
160}
161
166
168{
169 updateButtons();
170}
171
176
178{
179 const int buttonH = 22;
180 const int buttonY = getHeight() - buttonH - 4;
181 listBox.setBounds (2, 2, getWidth() - 4, buttonY - 5);
182
183 addButton.setBounds (2, buttonY, buttonH, buttonH);
184 removeButton.setBounds (addButton.getRight(), buttonY, buttonH, buttonH);
185
186 changeButton.changeWidthToFitText (buttonH);
187 downButton.setSize (buttonH * 2, buttonH);
188 upButton.setSize (buttonH * 2, buttonH);
189
190 downButton.setTopRightPosition (getWidth() - 2, buttonY);
191 upButton.setTopRightPosition (downButton.getX() - 4, buttonY);
192 changeButton.setTopRightPosition (upButton.getX() - 8, buttonY);
193}
194
199
201{
202 for (int i = filenames.size(); --i >= 0;)
203 {
204 const File f (filenames[i]);
205
206 if (f.isDirectory())
207 {
208 auto row = listBox.getRowContainingPosition (0, mouseY - listBox.getY());
209 path.add (f, row);
210 changed();
211 }
212 }
213}
214
215void FileSearchPathListComponent::addPath()
216{
217 auto start = defaultBrowseTarget;
218
219 if (start == File())
220 start = path[0];
221
222 if (start == File())
224
225 chooser = std::make_unique<FileChooser> (TRANS ("Add a folder..."), start, "*");
227
228 chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
229 {
230 if (fc.getResult() == File{})
231 return;
232
233 path.add (fc.getResult(), listBox.getSelectedRow());
234 changed();
235 });
236}
237
238void FileSearchPathListComponent::deleteSelected()
239{
241 changed();
242}
243
244void FileSearchPathListComponent::editSelected()
245{
247 changed();
248}
249
250void FileSearchPathListComponent::moveSelection (int delta)
251{
252 jassert (delta == -1 || delta == 1);
253 auto currentRow = listBox.getSelectedRow();
254
256 {
257 auto newRow = jlimit (0, path.getNumPaths() - 1, currentRow + delta);
258
259 if (currentRow != newRow)
260 {
262 path.remove (currentRow);
263 path.add (f, newRow);
264 listBox.selectRow (newRow);
265 changed();
266 }
267 }
268}
269
270
271} // namespace juce
int getHeight() const noexcept
Returns the component's height in pixels.
int getX() const noexcept
Returns the x coordinate of the component's left edge.
void setTopRightPosition(int x, int y)
Moves the component to a new position.
void setEnabled(bool shouldBeEnabled)
Enables or disables this component.
void repaint()
Marks the whole component as needing to be redrawn.
int getY() const noexcept
Returns the y coordinate of the top of this component.
void setBounds(int x, int y, int width, int height)
Changes the component's position and size.
void setSize(int newWidth, int newHeight)
Changes the size of the component.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
int getWidth() const noexcept
Returns the component's width in pixels.
int getRight() const noexcept
Returns the x coordinate of the component's right-hand edge.
@ ImageOnButtonBackground
Draws the button as a standard rounded-rectangle button with the image on top.
@ 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.
void deleteKeyPressed(int lastRowSelected) override
Override this to be informed when the delete key is pressed.
bool isInterestedInFileDrag(const StringArray &) override
Callback to check whether this target is interested in the set of files being offered.
int getNumRows() override
This has to return the number of items in the list.
@ backgroundColourId
The background colour to fill the component with.
void setPath(const FileSearchPath &newPath)
Changes the current path.
void resized() override
Called when this component's size has been changed.
void paintListBoxItem(int rowNumber, Graphics &g, int width, int height, bool rowIsSelected) override
This method must be implemented to draw a row of the list.
FileSearchPathListComponent()
Creates an empty FileSearchPathListComponent.
void selectedRowsChanged(int lastRowSelected) override
Override this to be informed when rows are selected or deselected.
void setDefaultBrowseTarget(const File &newDefaultDirectory)
Sets a file or directory to be the default starting point for the browser to show.
void listBoxItemDoubleClicked(int row, const MouseEvent &) override
This can be overridden to react to the user double-clicking on a row.
void returnKeyPressed(int lastRowSelected) override
Override this to be informed when the return key is pressed.
void paint(Graphics &) override
Components can override this method to draw their content.
void filesDropped(const StringArray &files, int, int) override
Callback to indicate that the user has dropped the files onto this component.
Represents a set of folders that make up a search path.
void add(const File &directoryToAdd, int insertIndex=-1)
Adds a new directory to the search path.
void remove(int indexToRemove)
Removes a directory from the search path.
String toString() const
Returns the search path as a semicolon-separated list of directories.
int getNumPaths() const
Returns the number of folders in this search path.
String getRawString(int index) const
Returns the unaltered text of the folder at the specified index.
Represents a local file or directory.
Definition juce_File.h:45
bool isDirectory() const
Checks whether the file is a directory that exists.
static File getCurrentWorkingDirectory()
Returns the current working directory.
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Creates a file that simply contains this string, without doing the sanity-checking that the normal co...
Definition juce_File.cpp:31
Represents a particular font, including its size, style, etc.
Definition juce_Font.h:42
void setHorizontalScale(float scaleFactor)
Changes the font's horizontal scale factor.
A graphics context, used for drawing a component or image.
void drawText(const String &text, int x, int y, int width, int height, Justification justificationType, bool useEllipsesIfTooBig=true) const
Draws a line of text within a specified rectangle.
void setFont(const Font &newFont)
Changes the font to use for subsequent text-drawing functions.
void setColour(Colour newColour)
Changes the current drawing colour.
void fillAll() const
Fills the context's entire clip region with the current colour or brush.
@ centredLeft
Indicates that the item should be centred vertically but placed on the left hand side.
int getRowContainingPosition(int x, int y) const noexcept
Finds the row index that contains a given x,y position.
int getSelectedRow(int index=0) const
Returns the row number of a selected row.
@ textColourId
The preferred colour to use for drawing text in the listbox.
@ backgroundColourId
The background colour to fill the list with.
@ outlineColourId
An optional colour to use to draw a border around the list.
void updateContent()
Causes the list to refresh its content.
int getNumSelectedRows() const
Returns the number of rows that are currently selected.
void selectRow(int rowNumber, bool dontScrollToShowThisRow=false, bool deselectOthersFirst=true)
Selects a row.
Contains position and status information about a mouse event.
A special array for holding a list of strings.
void changeWidthToFitText()
Changes this button's width to fit neatly around its current text, without changing its height.
@ highlightColourId
The colour with which to fill the background of highlighted sections of the text - this can be transp...
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
#define jassert(expression)
Platform-independent assertion macro.
JUCE Namespace.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
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
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.