[RESOLVED] TextEditor::setText and TextEditor::textWasChangedByValue()


#1

So I have class members:

TextEditor                  m_Editor;
bool                        m_bIsDirty;

and in the ctor:

m_bIsDirty = false;

m_Editor.setMultiLine (true);
m_Editor.setReturnKeyStartsNewLine (true);
m_Editor.setTabKeyUsedAsCharacter (true);
m_Editor.setPopupMenuEnabled (true);

addAndMakeVisible (m_Editor);

m_Editor.addListener (this);

then I have a method which contains:

MemoryBlock mb;

// yada yada yada

m_Editor.clear();                         // This doesn't trigger a textChanged

m_Editor.setText (mb.toString(), false);  // Note the second param.

m_bIsDirty = false;

and a callback:

void CMyComponent::textEditorTextChanged (TextEditor&)
{
    m_bIsDirty = true;
}

The problem is that I’m getting a TextEditor::textChanged() message from:

 void TextEditor::textWasChangedByValue()
{
    if (textValue.getValueSource().getReferenceCount() > 1)
        setText (textValue.getValue());
}

when m_Editor.setText() is called - but not sure why??

Any ideas?

Rail


#2

The only way I could think to fix it is:

void TextEditor::setText (const String& newText,
                          const bool sendTextChangeMessage)
{
    const int newLength = newText.length();

    if (newLength != getTotalNumChars() || getText() != newText || styleChanged)
    {
        if (! sendTextChangeMessage)
            textValue.removeListener (textHolder);   // Added this!

        textValue = newText;

        int oldCursorPos = caretPosition;
        const bool cursorWasAtEnd = oldCursorPos >= getTotalNumChars();

        clearInternal (nullptr);
        insert (newText, 0, currentFont, findColour (textColourId), 0, caretPosition);

        // if you're adding text with line-feeds to a single-line text editor, it
        // ain't gonna look right!
        jassert (multiline || ! newText.containsAnyOf ("\r\n"));

        if (cursorWasAtEnd && ! isMultiLine())
            oldCursorPos = getTotalNumChars();

        moveCaretTo (oldCursorPos, false);

        if (sendTextChangeMessage)
            textChanged();                       //  May not even need this since the Value::Listener will kick in
        else
            textValue.addListener (textHolder);  // Added this!

        updateTextHolderSize();
        scrollToMakeSureCursorIsVisible();
        undoManager.clearUndoHistory();

        styleChanged = false;

        repaint();
    }
}

Rail


#3

Bumping in case this got missed.

Rail


#4

Thanks for the commit.

Rail