tags:

views:

272

answers:

1

A bug has been filed and fixed (super quickly) in SWT: https://bugs.eclipse.org/bugs/show_bug.cgi?id=305294

Just to preface this, my goal here is to print the two images into a canvas so that I can animate the canvas sliding across the screen (think iPhone), sliding the controls themselves was too CPU intensive, so this was a good alternative until I tested it on Win7. I'm open to anything that will help me solve my original problem, it doesn't have to be fixing the problem below.

Does anyone know how to get "Control.print(GC)" to work with Windows 7 Aero? I have code that works just fine in Windows XP and in Windows 7, when Aero is disabled, but the command:

control.print(GC) causes a non-top control to be effectively erased from the screen.

GC gc = new GC(image);
try {
    // As soon as this code is called, calling "layout" on the controls
    // causes them to disappear.
    control.print(gc);
} finally {
    gc.dispose();
}

I have stacked controls and would like to print the images from the current and next controls such that I can "slide" them off the screen. However, upon printing the non-top control, it is never redrawn again.

Here is some example code. (Interesting code bits are at the top and it will require pointing at SWT in order to work.)

Thanks for any and all help. As a work around, I'm thinking about swapping controls between prints to see if that helps, but I'd rather not.

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SWTImagePrintTest {
    private Composite stack;
    private StackLayout layout;
    private Label lblFlip;
    private Label lblFlop;
    private boolean flip = true;
    private Button buttonFlop;
    private Button buttonPrint;

    /**
     * Prints the control into an image
     * 
     * @param control
     */
    protected void print(Control control) {
        Image image = new Image(control.getDisplay(), control.getBounds());
        GC gc = new GC(image);
        try {
            // As soon as this code is called, calling "layout" on the controls
            // causes them to disappear.
            control.print(gc);
        } finally {
            gc.dispose();
        }
    }

    /**
     * Swaps the controls in the stack
     */
    private void flipFlop() {
        if (flip) {
            flip = false;
            layout.topControl = lblFlop;
            buttonFlop.setText("flop");
            stack.layout();
        } else {
            flip = true;
            layout.topControl = lblFlip;
            buttonFlop.setText("flip");
            stack.layout();
        }
    }

    private void createContents(Shell shell) {
        shell.setLayout(new GridLayout(2, true));
        stack = new Composite(shell, SWT.NONE);
        GridData gdStack = new GridData(GridData.FILL_BOTH);
        gdStack.horizontalSpan = 2;
        stack.setLayoutData(gdStack);
        layout = new StackLayout();
        stack.setLayout(layout);
        lblFlip = new Label(stack, SWT.BOLD);
        lblFlip.setBackground(Display.getCurrent().getSystemColor(
                SWT.COLOR_CYAN));
        lblFlip.setText("FlIp");
        lblFlop = new Label(stack, SWT.NONE);
        lblFlop.setBackground(Display.getCurrent().getSystemColor(
                SWT.COLOR_BLUE));
        lblFlop.setText("fLoP");
        layout.topControl = lblFlip;
        stack.layout();
        buttonFlop = new Button(shell, SWT.FLAT);
        buttonFlop.setText("Flip");
        GridData gdFlip = new GridData();
        gdFlip.horizontalAlignment = SWT.RIGHT;
        buttonFlop.setLayoutData(gdFlip);
        buttonFlop.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                flipFlop();
            }

        });
        buttonPrint = new Button(shell, SWT.FLAT);
        buttonPrint.setText("Print");
        GridData gdPrint = new GridData();
        gdPrint.horizontalAlignment = SWT.LEFT;
        buttonPrint.setLayoutData(gdPrint);
        buttonPrint.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                print(lblFlip);
                print(lblFlop);
            }
        });

    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        Shell shell = new Shell();
        shell.setText("Slider Test");
        shell.setSize(new Point(800, 600));
        shell.setLayout(new GridLayout());
        SWTImagePrintTest tt = new SWTImagePrintTest();
        tt.createContents(shell);
        shell.open();
        Display display = Display.getDefault();
        while (shell.isDisposed() == false) {
            if (display.readAndDispatch() == false) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
+1  A: 

This was the result of a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=305294

It's since been fixed. Hopefully a new packaged version of SWT will come out soon so we can officially use it.

GreenKiwi
Well documented. I'll review my own code to see if it is affected under Windows 7.
Paul Lammertsma