1 11 package org.eclipse.compare.internal; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.widgets.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.events.*; 17 18 21 public abstract class BufferedCanvas extends Canvas { 22 23 private static final boolean USE_DOUBLE_BUFFER= true; 25 26 27 Image fBuffer; 28 29 public BufferedCanvas(Composite parent, int flags) { 30 super(parent, flags | SWT.NO_BACKGROUND); 31 32 addPaintListener( 33 new PaintListener() { 34 public void paintControl(PaintEvent event) { 35 doubleBufferPaint(event.gc); 36 } 37 } 38 ); 39 40 addDisposeListener( 41 new DisposeListener() { 42 public void widgetDisposed(DisposeEvent e) { 43 if (fBuffer != null) { 44 fBuffer.dispose(); 45 fBuffer= null; 46 } 47 } 48 } 49 ); 50 } 51 52 public void repaint() { 53 if (!isDisposed()) { 54 GC gc= new GC(this); 55 doubleBufferPaint(gc); 56 gc.dispose(); 57 } 58 } 59 60 63 void doubleBufferPaint(GC dest) { 64 65 if (!USE_DOUBLE_BUFFER) { 66 doPaint(dest); 67 return; 68 } 69 70 Point size= getSize(); 71 72 if (size.x <= 1 || size.y <= 1) return; 74 75 if (fBuffer != null) { 76 Rectangle r= fBuffer.getBounds(); 77 if (r.width != size.x || r.height != size.y) { 78 fBuffer.dispose(); 79 fBuffer= null; 80 } 81 } 82 if (fBuffer == null) 83 fBuffer= new Image(getDisplay(), size.x, size.y); 84 85 GC gc= new GC(fBuffer); 86 try { 87 gc.setBackground(getBackground()); 88 gc.fillRectangle(0, 0, size.x, size.y); 89 doPaint(gc); 90 } finally { 91 gc.dispose(); 92 } 93 94 dest.drawImage(fBuffer, 0, 0); 95 } 96 97 abstract public void doPaint(GC gc); 98 } 99 | Popular Tags |