I am trying to color the boxes against the background of my text editor in the Eclipse plug-in that I am developing. I succeed, but the problem is that when I scroll through the code in a text editor, the boxes also move in the direction I scroll. Does anyone know how to make boxes that keep their starting position there? My code for drawing:
<!-- language: lang-java -->
public class BoxHighLighting {
protected StyledText boxText;
private Color bc;
int startingChar;
int endingChar;
public BoxHighLighting(StyledText boxText, int startingChar, int endingChar, Color color){
this.boxText = boxText;
this.startingChar = startingChar;
this.endingChar = endingChar;
bc = color;
setStyledText();
}
public void setStyledText() {
if (boxText == null)
return;
Rectangle r0 = boxText.getClientArea();
if ((r0.width < 1) || (r0.height < 1)) {
return;
}
int xOffset = this.boxText.getHorizontalPixel();
int yOffset = this.boxText.getTopPixel();
Image newImage = new Image(null, r0.width, r0.height);
GC gc = new GC(newImage);
Rectangle rec = boxText.getTextBounds(startingChar, endingChar);
Rectangle editorWin = newImage.getBounds();
fillRectangle(bc, gc, rec.x, rec.y, editorWin.width, rec.height);
gc.drawRectangle(rec.x, rec.y, editorWin.width, rec.height);
Image oldImage = this.boxText.getBackgroundImage();
this.boxText.setBackgroundImage(newImage);
if (oldImage != null)
oldImage.dispose();
gc.dispose();
}
void fillRectangle(Color c, GC gc, int x, int y, int width, int height) {
if (c == null) {
return;
}
gc.setBackground(c);
gc.fillRoundRectangle(x, y, width, height, 5, 5);
}
}
source
share