2

I'm trying to highlight the bullet background of the currently active line but if I just set the background color of the bullet it only highlights the number portion. I would like all the space occupied by the bullet to highlighted.

Only the 9 is highlighted

I would like all the space to the left of the 9 to be highlighted as well and probably a bit to the right too.

The code I'm using to get what I have so far is

@Override
public void lineGetStyle(LineStyleEvent event) {
    // Set the line number
    int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());
    int currentLine = styledText.getLineAtOffset(event.lineOffset);
    event.bulletIndex = currentLine;

    int width = 36;
    if (styledText.getLineCount() > 999)
        width = (int) ((Math.floor(Math.log10(styledText.getLineCount()))+1) * 12);

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

    if (activeLine == currentLine) {
        style.background = highlightedLine;
    }

    style.foreground = mainBackground;


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

    event.styles = matchKeywords(event);
}

Is this possible?

javac
  • 2,347
  • 4
  • 15
  • 26
EmbMicro
  • 1,103
  • 1
  • 12
  • 25

1 Answers1

3

You can do this with custom painting. First, change the bullet type to ST.BULLET_CUSTOM. Then add a PaintObjectListener:

styledText.addPaintObjectListener(new PaintObjectListener() {
    public void paintObject(PaintObjectEvent event) {
        drawBullet(event.bullet, event.gc, event.x, event.y, event.bulletIndex, event.ascent, event.descent);
    }
});

There is a standard implementation of drawBullet in StyledTextRenderer. I've changed this to accept custom bullets as numbered bullets, and to draw an extra rectangle in the background:

void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
    StyleRange style = bullet.style;
    GlyphMetrics metrics = style.metrics;
    Color color = style.foreground;
    if (color != null) gc.setForeground(color);
    Font font = style.font;
    if (font != null) gc.setFont(font);
    String string = "";
    int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_CUSTOM|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
    switch (type) {
        case ST.BULLET_DOT: string = "\u2022"; break;
        case ST.BULLET_CUSTOM: string = String.valueOf(index + 1); break;
        case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break;
        case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break;
        case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break;
    }
    if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text;

    gc.setBackground(style.background);
    gc.fillRectangle(paintX, paintY, metrics.width, styledText.getLineHeight());

    Display display = styledText.getDisplay();
    TextLayout layout = new TextLayout(display);
    layout.setText(string);
    layout.setAscent(lineAscent);
    layout.setDescent(lineDescent);
    style = (StyleRange)style.clone();
    style.metrics = null;
    if (style.font == null) style.font = styledText.getFont();
    layout.setStyle(style, 0, string.length());    
    int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - 8);
    layout.draw(gc, x, paintY);
    layout.dispose();
}
fgb
  • 17,739
  • 2
  • 33
  • 50