Browse Source

Added a setEditable() method to TextPropertyComponent and an optional argument to its constructors to set whether its text editor should be editable

tags/2021-05-28
ed 7 years ago
parent
commit
fe3635bd83
2 changed files with 34 additions and 19 deletions
  1. +20
    -12
      modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp
  2. +14
    -7
      modules/juce_gui_basics/properties/juce_TextPropertyComponent.h

+ 20
- 12
modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp View File

@@ -31,13 +31,13 @@ class TextPropertyComponent::LabelComp : public Label,
public FileDragAndDropTarget public FileDragAndDropTarget
{ {
public: public:
LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline)
LabelComp (TextPropertyComponent& tpc, int charLimit, bool multiline, bool editable)
: Label (String(), String()), : Label (String(), String()),
owner (tpc), owner (tpc),
maxChars (charLimit), maxChars (charLimit),
isMultiline (multiline) isMultiline (multiline)
{ {
setEditable (true, true, false);
setEditable (editable, editable);
updateColours(); updateColours();
} }
@@ -55,7 +55,7 @@ public:
TextEditor* createEditorComponent() override TextEditor* createEditorComponent() override
{ {
TextEditor* const ed = Label::createEditorComponent();
auto* ed = Label::createEditorComponent();
ed->setInputRestrictions (maxChars); ed->setInputRestrictions (maxChars);
if (isMultiline) if (isMultiline)
@@ -94,20 +94,22 @@ private:
//============================================================================== //==============================================================================
TextPropertyComponent::TextPropertyComponent (const String& name, TextPropertyComponent::TextPropertyComponent (const String& name,
const int maxNumChars,
const bool isMultiLine)
int maxNumChars,
bool isMultiLine,
bool isEditable)
: PropertyComponent (name) : PropertyComponent (name)
{ {
createEditor (maxNumChars, isMultiLine);
createEditor (maxNumChars, isMultiLine, isEditable);
} }
TextPropertyComponent::TextPropertyComponent (const Value& valueToControl, TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
const String& name, const String& name,
const int maxNumChars,
const bool isMultiLine)
int maxNumChars,
bool isMultiLine,
bool isEditable)
: PropertyComponent (name) : PropertyComponent (name)
{ {
createEditor (maxNumChars, isMultiLine);
createEditor (maxNumChars, isMultiLine, isEditable);
textEditor->getTextValue().referTo (valueToControl); textEditor->getTextValue().referTo (valueToControl);
} }
@@ -131,9 +133,9 @@ Value& TextPropertyComponent::getValue() const
return textEditor->getTextValue(); return textEditor->getTextValue();
} }
void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
void TextPropertyComponent::createEditor (int maxNumChars, bool isMultiLine, bool isEditable)
{ {
addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine, isEditable));
if (isMultiLine) if (isMultiLine)
{ {
@@ -149,7 +151,7 @@ void TextPropertyComponent::refresh()
void TextPropertyComponent::textWasEdited() void TextPropertyComponent::textWasEdited()
{ {
const String newText (textEditor->getText());
auto newText = textEditor->getText();
if (getText() != newText) if (getText() != newText)
setText (newText); setText (newText);
@@ -185,4 +187,10 @@ void TextPropertyComponent::setInterestedInFileDrag (bool isInterested)
textEditor->setInterestedInFileDrag (isInterested); textEditor->setInterestedInFileDrag (isInterested);
} }
void TextPropertyComponent::setEditable (bool isEditable)
{
if (textEditor != nullptr)
textEditor->setEditable (isEditable, isEditable);
}
} // namespace juce } // namespace juce

+ 14
- 7
modules/juce_gui_basics/properties/juce_TextPropertyComponent.h View File

@@ -42,13 +42,15 @@ protected:
@param propertyName The name of the property @param propertyName The name of the property
@param maxNumChars If not zero, then this specifies the maximum allowable length of @param maxNumChars If not zero, then this specifies the maximum allowable length of
the string. If zero, then the string will have no length limit. the string. If zero, then the string will have no length limit.
@param isMultiLine isMultiLine sets whether the text editor allows carriage returns.
@param isMultiLine Sets whether the text editor allows carriage returns.
@param isEditable Sets whether the text editor is editable. The default is true.
@see TextEditor
@see TextEditor, setEditable
*/ */
TextPropertyComponent (const String& propertyName, TextPropertyComponent (const String& propertyName,
int maxNumChars, int maxNumChars,
bool isMultiLine);
bool isMultiLine,
bool isEditable = true);
public: public:
/** Creates a text property component. /** Creates a text property component.
@@ -57,14 +59,16 @@ public:
@param propertyName The name of the property @param propertyName The name of the property
@param maxNumChars If not zero, then this specifies the maximum allowable length of @param maxNumChars If not zero, then this specifies the maximum allowable length of
the string. If zero, then the string will have no length limit. the string. If zero, then the string will have no length limit.
@param isMultiLine isMultiLine sets whether the text editor allows carriage returns.
@param isMultiLine Sets whether the text editor allows carriage returns.
@param isEditable Sets whether the text editor is editable. The default is true.
@see TextEditor
@see TextEditor, setEditable
*/ */
TextPropertyComponent (const Value& valueToControl, TextPropertyComponent (const Value& valueToControl,
const String& propertyName, const String& propertyName,
int maxNumChars, int maxNumChars,
bool isMultiLine);
bool isMultiLine,
bool isEditable = true);
/** Destructor. */ /** Destructor. */
~TextPropertyComponent(); ~TextPropertyComponent();
@@ -130,6 +134,9 @@ public:
*/ */
void setInterestedInFileDrag (bool isInterested); void setInterestedInFileDrag (bool isInterested);
/** Sets whether the text editor is editable. The default setting for this is true. */
void setEditable (bool isEditable);
//============================================================================== //==============================================================================
/** @internal */ /** @internal */
void refresh() override; void refresh() override;
@@ -144,7 +151,7 @@ private:
ListenerList<Listener> listenerList; ListenerList<Listener> listenerList;
void callListeners(); void callListeners();
void createEditor (int maxNumChars, bool isMultiLine);
void createEditor (int maxNumChars, bool isMultiLine, bool isEditable);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent)
}; };


Loading…
Cancel
Save