LMMS
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 {
40 setEditable (editable, editable);
41
42 updateColours();
43 }
44
45 bool isInterestedInFileDrag (const StringArray&) override
46 {
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
82
83 void setInterestedInFileDrag (bool isInterested)
84 {
85 interestedInFileDrag = isInterested;
86 }
87
88 void setTextToDisplayWhenEmpty (const String& text, float alpha)
89 {
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
117
120};
121
122//==============================================================================
124{
125public:
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)
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
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));
212
213 if (isMultiLine)
214 {
215 textEditor->setJustificationType (Justification::topLeft);
216 preferredHeight = 100;
217 }
218}
219
224
226{
227 auto newText = textEditor->getText();
228
229 if (getText() != newText)
230 setText (newText);
231
233}
234
237
239{
240 Component::BailOutChecker checker (this);
241 listenerList.callChecked (checker, [this] (Listener& l) { l.textPropertyComponentChanged (this); });
242}
243
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
Definition juce_Component.h:2331
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
void repaint()
Definition juce_Component.cpp:1917
void setColour(int colourID, Colour newColour)
Definition juce_Component.cpp:2242
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
virtual void colourChanged()
Definition juce_Component.cpp:2192
Definition juce_FileDragAndDropTarget.h:37
Definition juce_GraphicsContext.h:45
@ topLeft
Definition juce_Justification.h:163
virtual TextEditor * createEditorComponent()
Definition juce_Label.cpp:319
@ outlineColourId
Definition juce_Label.h:108
@ backgroundColourId
Definition juce_Label.h:106
@ textColourId
Definition juce_Label.h:107
Justification getJustificationType() const noexcept
Definition juce_Label.h:122
bool isBeingEdited() const noexcept
Definition juce_Label.cpp:308
void showEditor()
Definition juce_Label.cpp:221
Label(const String &componentName=String(), const String &labelText=String())
Definition juce_Label.cpp:29
float getMinimumHorizontalScale() const noexcept
Definition juce_Label.h:166
String getText(bool returnActiveEditorContents=false) const
Definition juce_Label.cpp:72
void setText(const String &newText, NotificationType notification)
Definition juce_Label.cpp:52
PropertyComponent(const String &propertyName, int preferredHeight=25)
Definition juce_PropertyComponent.cpp:29
int preferredHeight
Definition juce_PropertyComponent.h:139
Definition juce_StringArray.h:35
Definition juce_String.h:53
bool isEmpty() const noexcept
Definition juce_String.h:310
Definition juce_TextEditor.h:43
Definition juce_TextPropertyComponent.cpp:32
void filesDropped(const StringArray &files, int, int) override
Definition juce_TextPropertyComponent.cpp:50
void paintOverChildren(Graphics &g) override
Definition juce_TextPropertyComponent.cpp:94
bool isInterestedInFileDrag(const StringArray &) override
Definition juce_TextPropertyComponent.cpp:45
TextEditor * createEditorComponent() override
Definition juce_TextPropertyComponent.cpp:56
void setTextToDisplayWhenEmpty(const String &text, float alpha)
Definition juce_TextPropertyComponent.cpp:88
void updateColours()
Definition juce_TextPropertyComponent.cpp:75
LabelComp(TextPropertyComponent &tpc, int charLimit, bool multiline, bool editable)
Definition juce_TextPropertyComponent.cpp:34
TextPropertyComponent & owner
Definition juce_TextPropertyComponent.cpp:112
int maxChars
Definition juce_TextPropertyComponent.cpp:114
void textWasEdited() override
Definition juce_TextPropertyComponent.cpp:70
bool interestedInFileDrag
Definition juce_TextPropertyComponent.cpp:116
float alphaToUseForEmptyText
Definition juce_TextPropertyComponent.cpp:119
void setInterestedInFileDrag(bool isInterested)
Definition juce_TextPropertyComponent.cpp:83
String textToDisplayWhenEmpty
Definition juce_TextPropertyComponent.cpp:118
bool isMultiline
Definition juce_TextPropertyComponent.cpp:115
Definition juce_TextPropertyComponent.h:131
void createEditor(int maxNumChars, bool isEditable)
Definition juce_TextPropertyComponent.cpp:208
~TextPropertyComponent() override
Definition juce_TextPropertyComponent.cpp:191
void setInterestedInFileDrag(bool isInterested)
Definition juce_TextPropertyComponent.cpp:250
virtual void textWasEdited()
Definition juce_TextPropertyComponent.cpp:225
void refresh() override
Definition juce_TextPropertyComponent.cpp:220
ValueTreePropertyWithDefault value
Definition juce_TextPropertyComponent.h:178
void setEditable(bool isEditable)
Definition juce_TextPropertyComponent.cpp:256
void colourChanged() override
Definition juce_TextPropertyComponent.cpp:244
virtual void setText(const String &newText)
Definition juce_TextPropertyComponent.cpp:193
Value & getValue() const
Definition juce_TextPropertyComponent.cpp:203
virtual String getText() const
Definition juce_TextPropertyComponent.cpp:198
ListenerList< Listener > listenerList
Definition juce_TextPropertyComponent.h:180
void callListeners()
Definition juce_TextPropertyComponent.cpp:238
const bool isMultiLine
Definition juce_TextPropertyComponent.h:176
std::unique_ptr< LabelComp > textEditor
Definition juce_TextPropertyComponent.h:179
void addListener(Listener *newListener)
Definition juce_TextPropertyComponent.cpp:235
TextPropertyComponent(const String &propertyName, int maxNumChars, bool isMultiLine, bool isEditable=true)
Definition juce_TextPropertyComponent.cpp:158
@ backgroundColourId
Definition juce_TextPropertyComponent.h:121
@ outlineColourId
Definition juce_TextPropertyComponent.h:123
@ textColourId
Definition juce_TextPropertyComponent.h:122
void removeListener(Listener *listener)
Definition juce_TextPropertyComponent.cpp:236
Definition juce_TextPropertyComponent.cpp:124
var getValue() const override
Definition juce_TextPropertyComponent.cpp:131
TextRemapperValueSourceWithDefault(const ValueTreePropertyWithDefault &v)
Definition juce_TextPropertyComponent.cpp:126
void setValue(const var &newValue) override
Definition juce_TextPropertyComponent.cpp:139
ValueTreePropertyWithDefault value
Definition juce_TextPropertyComponent.cpp:151
Definition juce_Value.h:180
Definition juce_Value.h:51
Definition juce_ValueTreePropertyWithDefault.h:39
Definition juce_Variant.h:42
String toString() const
Definition juce_Variant.cpp:570
int * l
Definition inflate.c:1579
unsigned v[N_MAX]
Definition inflate.c:1584
int g
Definition inflate.c:1573
static const char * name
Definition pugl.h:1582
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
static char ** files
Definition misc.c:28
Definition carla_juce.cpp:31
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
@ sendNotificationSync
Definition juce_NotificationType.h:35
@ dontSendNotification
Definition juce_NotificationType.h:33
const char * text
Definition swell-functions.h:167