I am developing a plugin for eclipse in which i want to draw some images into a custom view. Creating the view and my layout works fine, but the scrolling in my canvas seems to be broken. If I try to scroll over an image it looks like that: http://prntscr.com/xswwvw It is just repeating small parts of the picture again instead of real scrolling. I tried the same outside a plugin, i didnt get the Problem there.
I tried to search for a solution alot, but couldnt find one. Hopefully someone has an idea or some hints on how to solve this. Here is my relevant Code:
public class FaultTreeView {
private Canvas canvas;
private ArrayList<FTShape> shapeList = new ArrayList<>();
private Point origin = new Point(0, 0);
private Image image;
@PostConstruct
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setBackground(getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
container.setLayout(new GridLayout(1, false));
Composite comp = new Composite(container, SWT.NONE);
addToolBar(comp);
createCanvas(container);
AndGate ag = new AndGate(canvas, 0, 0);
this.image = ag.getImage();
handleScrollBars();
paintElementsInCanvas();
}
And here the methods:
public void addToolBar(Composite composite) {
ToolBar toolbar = new ToolBar(composite, SWT.BORDER | SWT.VERTICAL);
addDropdownMenu(toolbar);
}
public void createCanvas(Composite container) {
this.canvas = new Canvas(container, SWT.V_SCROLL | SWT.H_SCROLL);
this.canvas.setLayout(new GridLayout(1, false));
this.canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
}
For the ScrollBars i used some Code i found, but i added the *hBar.getPageIncrement() and *vBar.getPageIncrement() because otherwise it didnt let me scroll at all.
private void handleScrollBars() {
ScrollBar hBar = canvas.getHorizontalBar();
hBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
int hSelection = hBar.getSelection()*hBar.getPageIncrement();
int destX = -hSelection - origin.x;
Rectangle rect = image.getBounds();
canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
origin.x = -hSelection;
}
});
ScrollBar vBar = canvas.getVerticalBar();
vBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
int vSelection = vBar.getSelection() * vBar.getPageIncrement();
int destY = -vSelection - origin.y;
Rectangle rect = image.getBounds();
canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
origin.y = -vSelection;
}
});
}
public void paintElementsInCanvas() {
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
}
The drawImage
call must use the origin
to place the image correctly:
e.gc.drawImage(image, origin.x, origin.y);
Also look at SWT Snippet 48 scrolled canvas for how to handle resize and scroll bar sizing correctly.