Windows 7 Seems to break SWT Control.print(GC)

Posted by GreenKiwi on Stack Overflow See other posts from Stack Overflow or by GreenKiwi
Published on 2010-03-10T08:50:07Z Indexed on 2010/03/15 22:19 UTC
Read the original article Hit count: 286

Filed under:
|
|

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();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swt