From fe3635bd83f5be14c5d7c1e9905381040cfe5004 Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 28 Dec 2017 16:15:41 +0000 Subject: [PATCH] Added a setEditable() method to TextPropertyComponent and an optional argument to its constructors to set whether its text editor should be editable --- .../properties/juce_TextPropertyComponent.cpp | 32 ++++++++++++------- .../properties/juce_TextPropertyComponent.h | 21 ++++++++---- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp index 9bff521da4..8f810cc3f1 100644 --- a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp @@ -31,13 +31,13 @@ class TextPropertyComponent::LabelComp : public Label, public FileDragAndDropTarget { public: - LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline) + LabelComp (TextPropertyComponent& tpc, int charLimit, bool multiline, bool editable) : Label (String(), String()), owner (tpc), maxChars (charLimit), isMultiline (multiline) { - setEditable (true, true, false); + setEditable (editable, editable); updateColours(); } @@ -55,7 +55,7 @@ public: TextEditor* createEditorComponent() override { - TextEditor* const ed = Label::createEditorComponent(); + auto* ed = Label::createEditorComponent(); ed->setInputRestrictions (maxChars); if (isMultiline) @@ -94,20 +94,22 @@ private: //============================================================================== TextPropertyComponent::TextPropertyComponent (const String& name, - const int maxNumChars, - const bool isMultiLine) + int maxNumChars, + bool isMultiLine, + bool isEditable) : PropertyComponent (name) { - createEditor (maxNumChars, isMultiLine); + createEditor (maxNumChars, isMultiLine, isEditable); } TextPropertyComponent::TextPropertyComponent (const Value& valueToControl, const String& name, - const int maxNumChars, - const bool isMultiLine) + int maxNumChars, + bool isMultiLine, + bool isEditable) : PropertyComponent (name) { - createEditor (maxNumChars, isMultiLine); + createEditor (maxNumChars, isMultiLine, isEditable); textEditor->getTextValue().referTo (valueToControl); } @@ -131,9 +133,9 @@ Value& TextPropertyComponent::getValue() const 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) { @@ -149,7 +151,7 @@ void TextPropertyComponent::refresh() void TextPropertyComponent::textWasEdited() { - const String newText (textEditor->getText()); + auto newText = textEditor->getText(); if (getText() != newText) setText (newText); @@ -185,4 +187,10 @@ void TextPropertyComponent::setInterestedInFileDrag (bool isInterested) textEditor->setInterestedInFileDrag (isInterested); } +void TextPropertyComponent::setEditable (bool isEditable) +{ + if (textEditor != nullptr) + textEditor->setEditable (isEditable, isEditable); +} + } // namespace juce diff --git a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h index 8769ac1b9e..abc7c10bd3 100644 --- a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h @@ -42,13 +42,15 @@ protected: @param propertyName The name of the property @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. - @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, int maxNumChars, - bool isMultiLine); + bool isMultiLine, + bool isEditable = true); public: /** Creates a text property component. @@ -57,14 +59,16 @@ public: @param propertyName The name of the property @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. - @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, const String& propertyName, int maxNumChars, - bool isMultiLine); + bool isMultiLine, + bool isEditable = true); /** Destructor. */ ~TextPropertyComponent(); @@ -130,6 +134,9 @@ public: */ void setInterestedInFileDrag (bool isInterested); + /** Sets whether the text editor is editable. The default setting for this is true. */ + void setEditable (bool isEditable); + //============================================================================== /** @internal */ void refresh() override; @@ -144,7 +151,7 @@ private: ListenerList listenerList; void callListeners(); - void createEditor (int maxNumChars, bool isMultiLine); + void createEditor (int maxNumChars, bool isMultiLine, bool isEditable); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent) };