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_TextPropertyComponent.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//==============================================================================
32{
33public:
34 LabelComp (TextPropertyComponent& tpc, int charLimit, bool multiline, bool editable)
35 : Label ({}, {}),
36 owner (tpc),
37 maxChars (charLimit),
38 isMultiline (multiline)
39 {
41
42 updateColours();
43 }
44
45 bool isInterestedInFileDrag (const StringArray&) override
46 {
47 return interestedInFileDrag;
48 }
49
50 void filesDropped (const StringArray& files, int, int) override
51 {
52 setText (getText() + files.joinIntoString (isMultiline ? "\n" : ", "), sendNotificationSync);
53 showEditor();
54 }
55
57 {
59 ed->setInputRestrictions (maxChars);
60
61 if (isMultiline)
62 {
63 ed->setMultiLine (true, true);
64 ed->setReturnKeyStartsNewLine (true);
65 }
66
67 return ed;
68 }
69
70 void textWasEdited() override
71 {
72 owner.textWasEdited();
73 }
74
75 void updateColours()
76 {
80 repaint();
81 }
82
83 void setInterestedInFileDrag (bool isInterested)
84 {
85 interestedInFileDrag = isInterested;
86 }
87
88 void setTextToDisplayWhenEmpty (const String& text, float alpha)
89 {
90 textToDisplayWhenEmpty = text;
91 alphaToUseForEmptyText = alpha;
92 }
93
94 void paintOverChildren (Graphics& g) override
95 {
96 if (getText().isEmpty() && ! isBeingEdited())
97 {
98 auto& lf = owner.getLookAndFeel();
99 auto textArea = lf.getLabelBorderSize (*this).subtractedFrom (getLocalBounds());
100 auto labelFont = lf.getLabelFont (*this);
101
102 g.setColour (owner.findColour (TextPropertyComponent::textColourId).withAlpha (alphaToUseForEmptyText));
103 g.setFont (labelFont);
104
105 g.drawFittedText (textToDisplayWhenEmpty, textArea, getJustificationType(),
106 jmax (1, (int) ((float) textArea.getHeight() / labelFont.getHeight())),
108 }
109 }
110
111private:
113
114 int maxChars;
115 bool isMultiline;
116 bool interestedInFileDrag = true;
117
118 String textToDisplayWhenEmpty;
119 float alphaToUseForEmptyText = 0.0f;
120};
121
122//==============================================================================
124{
125public:
127 : value (v)
128 {
129 }
130
131 var getValue() const override
132 {
133 if (value.isUsingDefault())
134 return {};
135
136 return value.get();
137 }
138
139 void setValue (const var& newValue) override
140 {
141 if (newValue.toString().isEmpty())
142 {
143 value.resetToDefault();
144 return;
145 }
146
147 value = newValue;
148 }
149
150private:
152
153 //==============================================================================
155};
156
157//==============================================================================
159 int maxNumChars,
160 bool multiLine,
161 bool isEditable)
162 : PropertyComponent (name),
163 isMultiLine (multiLine)
164{
165 createEditor (maxNumChars, isEditable);
166}
167
169 int maxNumChars, bool multiLine, bool isEditable)
170 : TextPropertyComponent (name, maxNumChars, multiLine, isEditable)
171{
172 textEditor->getTextValue().referTo (valueToControl);
173}
174
176 int maxNumChars, bool multiLine, bool isEditable)
177 : TextPropertyComponent (name, maxNumChars, multiLine, isEditable)
178{
179 value = valueToControl;
180
181 textEditor->getTextValue().referTo (Value (new TextRemapperValueSourceWithDefault (value)));
182 textEditor->setTextToDisplayWhenEmpty (value.getDefault(), 0.5f);
183
184 value.onDefaultChange = [this]
185 {
186 textEditor->setTextToDisplayWhenEmpty (value.getDefault(), 0.5f);
187 repaint();
188 };
189}
190
191TextPropertyComponent::~TextPropertyComponent() {}
192
194{
195 textEditor->setText (newText, sendNotificationSync);
196}
197
199{
200 return textEditor->getText();
201}
202
204{
205 return textEditor->getTextValue();
206}
207
208void TextPropertyComponent::createEditor (int maxNumChars, bool isEditable)
209{
210 textEditor.reset (new LabelComp (*this, maxNumChars, isMultiLine, isEditable));
211 addAndMakeVisible (textEditor.get());
212
213 if (isMultiLine)
214 {
215 textEditor->setJustificationType (Justification::topLeft);
216 preferredHeight = 100;
217 }
218}
219
221{
222 textEditor->setText (getText(), dontSendNotification);
223}
224
225void TextPropertyComponent::textWasEdited()
226{
227 auto newText = textEditor->getText();
228
229 if (getText() != newText)
231
232 callListeners();
233}
234
237
238void TextPropertyComponent::callListeners()
239{
241 listenerList.callChecked (checker, [this] (Listener& l) { l.textPropertyComponentChanged (this); });
242}
243
245{
247 textEditor->updateColours();
248}
249
251{
252 if (textEditor != nullptr)
253 textEditor->setInterestedInFileDrag (isInterested);
254}
255
257{
258 if (textEditor != nullptr)
259 textEditor->setEditable (isEditable, isEditable);
260}
261
262} // namespace juce
Rectangle< ValueType > subtractedFrom(const Rectangle< ValueType > &original) const noexcept
Returns a rectangle with these borders removed from it.
Colour withAlpha(uint8 newAlpha) const noexcept
Returns a colour that's the same colour as this one, but with a new alpha value.
A class to keep an eye on a component and check for it being deleted.
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.
void setColour(int colourID, Colour newColour)
Registers a colour to be used for a particular purpose.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
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.
virtual void colourChanged()
This method is called when a colour is changed by the setColour() method, or when the look-and-feel i...
Components derived from this class can have files dropped onto them by an external application.
A graphics context, used for drawing a component or image.
void drawFittedText(const String &text, int x, int y, int width, int height, Justification justificationFlags, int maximumNumberOfLines, float minimumHorizontalScale=0.0f) const
Tries to draw a text string inside a given space.
void setFont(const Font &newFont)
Changes the font to use for subsequent text-drawing functions.
void setColour(Colour newColour)
Changes the current drawing colour.
@ topLeft
Indicates that the item should be placed in the top-left corner.
A component that displays a text string, and can optionally become a text editor when clicked.
Definition juce_Label.h:41
virtual TextEditor * createEditorComponent()
Creates the TextEditor component that will be used when the user has clicked on the label.
void setEditable(bool editOnSingleClick, bool editOnDoubleClick=false, bool lossOfFocusDiscardsChanges=false)
Makes the label turn into a TextEditor when clicked.
@ outlineColourId
An optional colour to use to draw a border around the label.
Definition juce_Label.h:108
@ backgroundColourId
The background colour to fill the label with.
Definition juce_Label.h:106
@ textColourId
The colour for the text.
Definition juce_Label.h:107
Justification getJustificationType() const noexcept
Returns the type of justification, as set in setJustificationType().
Definition juce_Label.h:122
bool isBeingEdited() const noexcept
Returns true if the editor is currently focused and active.
void showEditor()
Makes the editor appear as if the label had been clicked by the user.
float getMinimumHorizontalScale() const noexcept
Specifies the amount that the font can be squashed horizontally.
Definition juce_Label.h:166
void setText(const String &newText, NotificationType notification)
Changes the label text.
A base class for a component that goes in a PropertyPanel and displays one of an item's properties.
int preferredHeight
Used by the PropertyPanel to determine how high this component needs to be.
A special array for holding a list of strings.
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Joins the strings in the array together into one string.
The JUCE String class!
Definition juce_String.h:53
bool isEmpty() const noexcept
Returns true if the string contains no characters.
An editable text box.
void filesDropped(const StringArray &files, int, int) override
Callback to indicate that the user has dropped the files onto this component.
void paintOverChildren(Graphics &g) override
Components can override this method to draw over the top of their children.
bool isInterestedInFileDrag(const StringArray &) override
Callback to check whether this target is interested in the set of files being offered.
TextEditor * createEditorComponent() override
Creates the TextEditor component that will be used when the user has clicked on the label.
void textWasEdited() override
Called after the user changes the text.
Used to receive callbacks for text changes.
A PropertyComponent that shows its value as editable text.
void setInterestedInFileDrag(bool isInterested)
Sets whether the text property component can have files dropped onto it by an external application.
void refresh() override
Updates the property component if the item it refers to has changed.
void setEditable(bool isEditable)
Sets whether the text editor is editable.
void colourChanged() override
This method is called when a colour is changed by the setColour() method, or when the look-and-feel i...
virtual void setText(const String &newText)
Called when the user edits the text.
Value & getValue() const
Returns the text that should be shown in the text editor as a Value object.
virtual String getText() const
Returns the text that should be shown in the text editor.
void addListener(Listener *newListener)
Registers a listener to receive events when this button's state changes.
TextPropertyComponent(const String &propertyName, int maxNumChars, bool isMultiLine, bool isEditable=true)
Creates a text property component.
@ backgroundColourId
The colour to fill the background of the text area.
@ outlineColourId
The colour to use to draw an outline around the text area.
@ textColourId
The colour to use for the editable text.
void removeListener(Listener *listener)
Removes a previously-registered button listener.
var getValue() const override
Returns the current value of this object.
void setValue(const var &newValue) override
Changes the current value.
This class acts as a wrapper around a property inside a ValueTree.
bool isUsingDefault() const
Returns true if the property does not exist in the referenced ValueTree.
var getDefault() const
Returns the current default value.
std::function< void()> onDefaultChange
You can assign a lambda to this callback and it will called when the default value is changed.
void resetToDefault() noexcept
Removes the property from the referenced ValueTree.
var get() const noexcept
Returns the current value of the property.
Used internally by the Value class as the base class for its shared value objects.
Definition juce_Value.h:180
Represents a shared variant value.
Definition juce_Value.h:51
A variant class, that can be used to hold a range of primitive values.
#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 ...
JUCE Namespace.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
@ 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