2

我试图突出显示当前活动行的项目符号背景,但如果我只是设置项目符号的背景颜色,它只会突出显示数字部分。我希望突出显示项目符号占用的所有空间。

仅突出显示 9

我希望 9 左侧的所有空间也被突出显示,并且可能也有点向右。

我用来获取到目前为止的代码是

@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);
}

这可能吗?

4

1 回答 1

3

您可以使用自定义绘画来做到这一点。首先,将项目符号类型更改为ST.BULLET_CUSTOM. 然后添加一个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);
    }
});

drawBulletStyledTextRenderer中有一个标准实现。我已将其更改为接受自定义项目符号作为编号项目符号,并在背景中绘制一个额外的矩形:

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();
}
于 2016-03-28T14:49:11.153 回答