Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to trigger a redraw of all widgets of my application ?
How to trigger a redraw of all widgets of my application ? [message #1623788] Thu, 19 February 2015 10:24
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 96
Registered: July 2009
Member
Hi,

I have to implement a kind of zoom utility for my application.
The mouse wheel is used to zoom / unzoom.

My problem is to trigger a redraw of all widgets from a global listener on my Display instance.

In the example below, a listener on the canvas works well but the high-level listener (display.addFilter()) is not able to trigger a redraw of all the widgets...

How can I do ?
Thanks

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;


public class ZoomImages {

	private static float scale = 1;
	private static Display display;
	private static Shell shell;


	private void initImage(boolean addMouseWheelListener) {
		final Image image = new Image(display, "button.png");

		final Canvas canvas = new Canvas(shell, SWT.NONE);
		canvas.setLayout(new FormLayout());

		canvas.addListener(SWT.Paint, new Listener() {
			private int initialFontSize = -1;

			public void handleEvent(Event event) {
				event.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, (int)(image.getBounds().width * scale), (int)(image.getBounds().height * scale));
			}
		});
		if (addMouseWheelListener) {
			canvas.addListener(SWT.MouseWheel, new Listener() {
				public void handleEvent(Event event) {
					if (event.count > 0) { 
						scale += .2f;
					} else {
						scale -= .2f;
					}
					scale = Math.max(scale, 0);
					canvas.redraw();
				}
			});
		}
	}


	public static void main(String[] args) {

		display = new Display();
		shell = new Shell(display);
		shell.setLayout(new FillLayout(SWT.VERTICAL));

		ZoomImages instance = new ZoomImages();
		instance.initImage(true);
		instance.initImage(false);

		display.addFilter( SWT.MouseWheel, new Listener() { 
			public void handleEvent(Event event) {
				if (event.count > 0) { 
					scale += .2f;
				} else {
					scale -= .2f;
				}
				scale = Math.max(scale, 0);
				
				// Desperately try to trigger a global redraw...
				shell.redraw();
				shell.layout(true, true);
				display.update();
			}
		});

		shell.pack();
		shell.setSize(800, 200);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
}
Previous Topic:DND, multiple Transfer classes
Next Topic:Want to run the widget without any open eclipse or cmd session
Goto Forum:
  


Current Time: Sun Jan 17 19:44:35 GMT 2021

Powered by FUDForum. Page generated in 0.03215 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top