Wednesday, August 15, 2007

SWT Canvas default behaviour

This eclipse.platform.swt news group posting started me thinking again about platform differences in Eclipse RCP and Java. Without a bit of care you could end up having DOUBLE SECRET SWT.DOUBLE_BUFFERED! My apologies to Harold Ramis.

The default behaviour of org.eclipse.swt.widgets.Canvas does support SWT.DOUBLE_BUFFERED on Linux GTK but not on Windows.

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class CanvasTest {
 public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell(display);
  Canvas canvas = new Canvas(shell, SWT.NONE);
  System.out.println("SWT.NO_REDRAW_RESIZE: "
    + stateToString(canvas.getStyle(), SWT.NO_REDRAW_RESIZE));
  System.out.println("SWT.DOUBLE_BUFFERED: "
    + stateToString(canvas.getStyle(), SWT.DOUBLE_BUFFERED));
  System.out.println("SWT.NO_BACKGROUND: "
    + stateToString(canvas.getStyle(), SWT.NO_BACKGROUND));
  System.out.println("SWT.NO_MERGE_PAINTS: "
    + stateToString(canvas.getStyle(), SWT.NO_MERGE_PAINTS));
 }

 private static String stateToString(int style, int styleFlag) {
  return (style & styleFlag) != 0 ? "on" : "off";
 }
}

Run on a Windows platform has the org.eclipse.swt.widgets.Canvas set as:

           SWT.NO_REDRAW_RESIZE: off
           SWT.DOUBLE_BUFFERED: off
           SWT.NO_BACKGROUND: off
           SWT.NO_MERGE_PAINTS: off

Run on a Linux GTK platform has the org.eclipse.swt.widgets.Canvas set as:

           SWT.NO_REDRAW_RESIZE: off
           SWT.DOUBLE_BUFFERED: on
           SWT.NO_BACKGROUND: off
           SWT.NO_MERGE_PAINTS: off

1 comment:

JFC said...

Great remark...
Setting DOUBLE_BUFFERED in my Canvas constructor definitivily solved my slow graphical operations problem wich occured only in Windows...
Many thanks...
JFC