Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT.MouseWheel event for Canvas object
SWT.MouseWheel event for Canvas object [message #466936] |
Thu, 19 January 2006 14:40 |
Eclipse User |
|
|
|
Originally posted by: murat.tetracom.com
Hi,
I'm trying to handle SWT.MouseWheel event for a simple Canvas object,
but it doesn't work. I need this to zoom in/out an image. I need also to
catch the drag event, is this SWT.DragDetect? Thanx!
|
|
|
Re: SWT.MouseWheel event for Canvas object [message #466978 is a reply to message #466936] |
Fri, 20 January 2006 15:19 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
Please give some example code to show what is failing.
The following works for me:
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Canvas canvas = new Canvas(shell, SWT.BORDER);
Listener listener = new Listener() {
int zoomFactor = 50;
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseWheel:
zoomFactor = Math.max(0, zoomFactor +
event.count);
Canvas canvas = (Canvas)event.widget;
canvas.redraw();
break;
case SWT.Paint:
event.gc.drawText("Zoom = "+zoomFactor, 10,
10);
break;
}
}
};
canvas.addListener(SWT.MouseWheel, listener);
canvas.addListener(SWT.Paint, listener);
shell.setSize(400, 300);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
"Murad Murad" <murat@tetracom.com> wrote in message
news:dqo88e$e8k$1@utils.eclipse.org...
> Hi,
> I'm trying to handle SWT.MouseWheel event for a simple Canvas object, but
> it doesn't work. I need this to zoom in/out an image. I need also to catch
> the drag event, is this SWT.DragDetect? Thanx!
|
|
|
Re: SWT.MouseWheel event for Canvas object [message #466992 is a reply to message #466978] |
Fri, 20 January 2006 16:48 |
Eclipse User |
|
|
|
Originally posted by: murat.tetracom.com
Hi, thanx for the reply!
I have a CTabFolder and I create CTabItem objects dynamically. Each
CTabItem object has only one contol, a Canvas. I have MouseManager for
Mouse events. Here is the sample code. All other events are caught but
MouseWheel is not.
public class myTab extends CTabItem {
public myTab(final CTabFolder tabFolder) {
super(tabFolder, SWT.FLAT);
this.setText("New");
final Composite myComposite = new Composite(tabFolder, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = layout.marginWidth = 0;
myComposite.setLayout(layout);
final Canvas myCanvas = new Canvas(myComposite, SWT.NO_BACKGROUND |
SWT.BORDER);
myCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
MouseManager mouseManagerSWT = new MouseManager(myCanvas);
}
....
}
public class MouseManager implements Listener {
public MouseManager(Composite composite) {
this.composite = composite;
composite.addListener(SWT.MouseDoubleClick, this);
composite.addListener(SWT.MouseDown, this);
composite.addListener(SWT.MouseEnter, this);
composite.addListener(SWT.MouseExit, this);
composite.addListener(SWT.MouseHover, this);
composite.addListener(SWT.MouseMove, this);
composite.addListener(SWT.MouseUp, this);
composite.addListener(SWT.MouseWheel, this);
composite.addListener(SWT.DragDetect, this);
}
public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseDoubleClick:
mouseClicked(e.time, e.x, e.y, e.stateMask | e.button, 2);
break;
case SWT.MouseDown:
mouseDown(e.time, e.x, e.y, e.stateMask | e.button);
break;
case SWT.MouseEnter:
mouseEnter(e.time, e.x, e.y);
break;
case SWT.MouseExit:
mouseExit(e.time, e.x, e.y);
break;
case SWT.MouseHover:
break;
case SWT.MouseMove:
mouseMove(e.time, e.x, e.y, e.stateMask | e.button);
break;
case SWT.MouseUp:
mouseUp(e.time, e.x, e.y, e.stateMask | e.button);
break;
case SWT.MouseWheel:
System.out.println(new MouseEvent(e).toString());
mouseWheel(e.time, e.detail, e.stateMask | e.button);
break;
case SWT.DragDetect:
int stateMasks = e.stateMask;
if ((stateMasks & LEFT_MIDDLE_RIGHT) == 0) stateMasks |= LEFT;
mouseDrag(e.time, e.x, e.y, stateMasks);
break;
default:
break;
}
}
}
Thanx again!
Regards,
Murad
Veronika Irvine wrote:
> Please give some example code to show what is failing.
>
|
|
|
Re: SWT.MouseWheel event for Canvas object [message #467000 is a reply to message #466992] |
Fri, 20 January 2006 21:56 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
The MouseWheel is going to the widget with focus. Canvas will not take
focus until you add a key listener. In the example below I have placed the
canvas in a CTabFolder and given it a key listener. When the shell first
opens, focus in the CTabFolder tab (see the focus rectangle around "hello")
If you click on the canvas (or tab into) the canvas will take focus. Then
the canvas will start receiving the mouse wheel events. (My previous
example worked because the canvas was the only widget in the shell and
therefore all events were going to it - in your case, CTabFolder can take
focus so you need to make your canvas take focus too).
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
CTabItem item = new CTabItem(folder, SWT.NONE);
item.setText("hello");
Composite c = new Composite(folder, SWT.NONE);
c.setLayout(new FillLayout());
item.setControl(c);
Canvas canvas = new Canvas(c, SWT.BORDER);
Listener listener = new Listener() {
int zoomFactor = 50;
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseWheel:
zoomFactor = Math.max(0, zoomFactor +
event.count);
Canvas canvas = (Canvas)event.widget;
canvas.redraw();
break;
case SWT.Paint:
event.gc.drawText("Zoom = "+zoomFactor, 10,
10);
break;
}
}
};
canvas.addListener(SWT.MouseWheel, listener);
canvas.addListener(SWT.Paint, listener);
// Add KeyDown listener so that canvas takes focus
canvas.addListener(SWT.KeyDown, listener);
shell.setSize(400, 300);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
|
|
|
Re: SWT.MouseWheel event for Canvas object [message #467006 is a reply to message #467000] |
Sat, 21 January 2006 00:11 |
Eclipse User |
|
|
|
Originally posted by: murat.abv.bg
Thank you very much! Setting the key listener worked like a charm.
Thanks again.
Veronika Irvine wrote:
> The MouseWheel is going to the widget with focus. Canvas will not take
> focus until you add a key listener. In the example below I have placed the
> canvas in a CTabFolder and given it a key listener. When the shell first
> opens, focus in the CTabFolder tab (see the focus rectangle around "hello")
> If you click on the canvas (or tab into) the canvas will take focus. Then
> the canvas will start receiving the mouse wheel events. (My previous
> example worked because the canvas was the only widget in the shell and
> therefore all events were going to it - in your case, CTabFolder can take
> focus so you need to make your canvas take focus too).
>
> public static void main (String [] args) {
> Display display = new Display ();
> Shell shell = new Shell (display);
> shell.setLayout(new FillLayout());
>
> CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
> CTabItem item = new CTabItem(folder, SWT.NONE);
> item.setText("hello");
> Composite c = new Composite(folder, SWT.NONE);
> c.setLayout(new FillLayout());
> item.setControl(c);
> Canvas canvas = new Canvas(c, SWT.BORDER);
> Listener listener = new Listener() {
> int zoomFactor = 50;
> public void handleEvent(Event event) {
> switch (event.type) {
> case SWT.MouseWheel:
> zoomFactor = Math.max(0, zoomFactor +
> event.count);
> Canvas canvas = (Canvas)event.widget;
> canvas.redraw();
> break;
> case SWT.Paint:
> event.gc.drawText("Zoom = "+zoomFactor, 10,
> 10);
> break;
> }
> }
> };
> canvas.addListener(SWT.MouseWheel, listener);
> canvas.addListener(SWT.Paint, listener);
> // Add KeyDown listener so that canvas takes focus
> canvas.addListener(SWT.KeyDown, listener);
> shell.setSize(400, 300);
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
>
|
|
|
Goto Forum:
Current Time: Sun Dec 08 02:19:32 GMT 2024
Powered by FUDForum. Page generated in 0.03662 seconds
|