0

I'm trying to make the bullet of active line have a highlighted background. I'm using

int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());

To get the like that is currently active. This seems to work except when I hit enter and get a new line. Wrong line highlighted

getCaretOffset returns 35 and getCharCount returns 36.

However, if I click on the last line (for now I call redraw() on clicks) the line highlights correctly and getCaretOffset returns 36.

Correct highlighting

Here is the relevant code

public void lineGetStyle(LineStyleEvent event) {
    // Set the line number
    int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());
    System.out.println("Offset " + styledText.getCaretOffset() + " max " + styledText.getCharCount());
    int currentLine = styledText.getLineAtOffset(event.lineOffset);
    event.bulletIndex = currentLine;

    // Set the style, 12 pixles wide for each digit
    StyleRange style = new StyleRange();
    style.metrics = new GlyphMetrics(0, 0, 36);

    if (activeLine == currentLine) {
        style.background = highlightedLine;
        if (curActiveLine != activeLine){
            System.out.println("ActiveLine " + activeLine + " old " + curActiveLine);
            int redrawLine = curActiveLine;
            curActiveLine = activeLine;
            styledText.redraw(0, styledText.getLinePixel(redrawLine), 36, styledText.getLineHeight(),true);
        }
    }

    style.foreground = mainBackground;


    // Create and set the bullet
    event.bullet = new Bullet(ST.BULLET_NUMBER, style);

    event.styles = matchKeywords(event);
}
Alchitry
  • 1,369
  • 1
  • 16
  • 29

1 Answers1

0

I just realized that you can set a CaretListener to get notifications for each move of the caret. By issuing the redraws from that it works great now.

Alchitry
  • 1,369
  • 1
  • 16
  • 29