Even if it might not be the source of your current problem, when reading your code I get the feeling that you are missing some basic C++ know how.
So you are inheriting from TextEditor and adding a TextEditor member to your class. What’s your intention to do so, or what’s your understanding of the meaning of public TextEditor
?
When inheriting from TextEditor your class actually IS A TextEditor. Adding another TextEditor member also means that it has another additional TextEditor. So in fact, there are two independent editors, however, you never use the member editor in your class. So you could delete the member variable.
Furthermore, you inherit from Value::Listener
and override one of its virtual member functions. Usual syntax to do so would be
void valueChanged (Value& val) override
{
String s = val.getValue();
setText(s, false);
};
e.g. you don’t need the Value::Listener
bit before the valueChanged function because by inheriting from Value::Listener, valueChanged becomes a member function of your class which you override from the base class (and therefore mark it with override).
Now you didn’t really tell what’s not working with your code, however, here is a PIP that demonstrates a version that works:
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: TextListener
dependencies: juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics
exporters: xcode_mac
moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
type: Component
mainClass: MyComponent
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
class TextListener : public TextEditor, public Value::Listener
{
public:
TextListener (Value& value, const String& initialText) : v (value)
{
v.addListener(this);
setText (initialText, false);
onTextChange = [this] () { v.setValue (getText()); };
}
~TextListener() {v.removeListener (this); }
void valueChanged (Value& val) override
{
String s = val.getValue();
setText (s, false);
};
private:
Value& v;
};
struct PrintListener : public Value::Listener
{
void valueChanged(Value& val) override {std::cout << val.toString() << std::endl; }
};
//==============================================================================
class MyComponent : public Component
{
public:
//==============================================================================
MyComponent() : value ("Values initial text"), textListener (value, "Editors initial Text")
{
value.addListener (&printListener);
addAndMakeVisible (textListener);
setSize (200, 50);
}
~MyComponent() {value.removeListener (&printListener); }
//==============================================================================
void paint (Graphics& g) override
{
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
}
void resized() override
{
auto bounds = getLocalBounds();
textListener.setBounds (bounds);
}
private:
Value value;
TextListener textListener;
PrintListener printListener;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyComponent)
};