CaretComponent not picking up caret colour


#1

When the color of the caret is set on a TextEditor, i.e. textEditor->setColour (CaretComponent::caretColourId, colour);, the color is not applied when the CaretComponent is created. The Label component relies on this to work in order for the caret color to be set correctly (in Label::createEditorComponent). This is one possible fix:

juce_LookAndFeel.cpp:

CaretComponent* LookAndFeel::createCaretComponent (Component* keyFocusOwner)
{
  CaretComponent* caret = new CaretComponent (keyFocusOwner);
  Colour colour = keyFocusOwner->findColour (CaretComponent::caretColourId);
  if (colour != Colours::transparentBlack)
      caret->setColour (CaretComponent::caretColourId, colour);
  return caret;
}

juce_TextEditor.cpp

void TextEditor::colourChanged()
{
    setOpaque (findColour (backgroundColourId).isOpaque());
    if (isCaretVisible())
    {
        Colour colour = findColour (CaretComponent::caretColourId);
        if (colour != Colours::transparentBlack)
            caret->setColour (CaretComponent::caretColourId, colour);
    }
    repaint();
}

#2

Hmm… since the CaretComponent looks up the colour each time it repaints, wouldn’t this be a simpler solution:

void CaretComponent::paint (Graphics& g) { g.fillAll (findColour (caretColourId, true)); }

(I think that’s probably what I meant it to do in the first place, I just forgot to add the ‘true’ parameter)


#3

[attachment=0]mistakes.gif[/attachment]