Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Display mouse events


As of R3.0, Displays only support SWT.Close and SWT.Dispose.  On Windows, you can't get mouse mouse outside of a shell unless the mouse is down and you are "grabbing". Here is working code (using a timer).  You might consider not bothering to cache the screenGC.  Just create and dispose it whenever you need it.

package steve;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;

/**
 * @author Brian Sam-Bodden
 */
public class Magnifier extends Canvas {

        private Display display;
        private int zoom = 2;
        private Image buffer;
        private GC screenGC;
        private Point previousCursorPosition;
        private boolean alwaysRepaint = true;

        /**
         * @param parent
         * @param style
         */
        public Magnifier(Composite parent, int style) {
                super(parent, style | SWT.NO_BACKGROUND);

                // create a GC for the display
                display = Display.getDefault();
                screenGC = new GC(display);

                zoom = 2;

                addPaintListener(new PaintListener() {
                        public void paintControl(PaintEvent e) {
                                Magnifier.this.onPaint(e);
                        }
                });

                addDisposeListener(new DisposeListener() {
                        public void widgetDisposed(DisposeEvent e) {
                                Magnifier.this.onDispose(e);
                        }
                });
               
                display.timerExec(100, new Runnable () {
                        public void run() {
                                if (isDisposed ()) return;
                                redraw();
                                display.timerExec (100, this);
                        }
                });

        }

        /**
         * @param e
         */
        protected void onDispose(DisposeEvent e) {
                if (screenGC != null) {
                        screenGC.dispose();
                }
        }

        /**
         * @param e
         */
        protected void onPaint(PaintEvent e) {
                // get the cursor position
                Point cp = display.getCursorLocation();

                // don't repaint if the mouse hasn't moved
                if (!alwaysRepaint && (cp.equals(previousCursorPosition))) {
                        return;
                }

                previousCursorPosition = cp;

                // get the size of the control
                Point size = getSize();

                // determine the area to capture
                Point capture = new Point(size.x / zoom, size.y / zoom);

                // determine the area to get
                Rectangle screen = display.getBounds();
                Point copyPos = new Point(0, 0);

                if ((cp.x - capture.x / 2) < 0) {
                        copyPos.x = 0;
                } else if ((cp.x + capture.x / 2) > screen.width) {
                        copyPos.x = screen.width - capture.x;
                } else {
                        copyPos.x = cp.x - (capture.x / 2);
                }

                if ((cp.y - capture.y / 2) < 0) {
                        copyPos.y = 0;
                } else if ((cp.y + capture.y / 2) > screen.height) {
                        copyPos.y = screen.height - capture.y;
                } else {
                        copyPos.y = cp.y - (capture.y / 2);
                }

                // Copy rectangle from screen to memory...
                buffer = new Image(display, capture.x, capture.y);
                screenGC.copyArea(buffer, copyPos.x, copyPos.y);

                // zoom the memory image
                final Image scaled = new Image(display, buffer.getImageData().scaledTo(
                                size.x, size.y));

                // paint scaled image on the canvas
                e.gc.drawImage(scaled, 0, 0);

                // clean up
                buffer.dispose();
                scaled.dispose();
        }

        public Point computeSize(int wHint, int hHint, boolean changed) {
                checkWidget();
                Point e = new Point(0, 0);

                if (wHint == SWT.DEFAULT) {
                        e.x = 100;
                } else {
                        e.x = wHint;
                }
                if (hHint == SWT.DEFAULT) {
                        e.y = 100;
                } else {
                        e.y = hHint;
                }

                return e;
        }

        /**
         * @return
         */
        public int getZoom() {
                return zoom;
        }

        /**
         * @param i
         */
        public void setZoom(int i) {
                if (i == zoom)
                        return;
                zoom = i;
        }

        /**
         * @return
         */
        public boolean getAlwaysRepaint() {
                return alwaysRepaint;
        }

        /**
         * @param b
         */
        public void setAlwaysRepaint(boolean b) {
                alwaysRepaint = b;
        }
       
        public static void main (String [] args) {
                Display display = new Display ();
                Shell shell = new Shell (display);
                shell.setLayout (new FillLayout ());
                new Magnifier (shell, SWT.NONE);
                shell.pack ();
                shell.open ();
                while (!shell.isDisposed ()) {
                        if (!display.readAndDispatch ()) display.sleep ();
                }
                display.dispose ();
        }

}



"Brian Sam-Bodden" <bsbodden@xxxxxxxxxxxxxxx>
Sent by: platform-swt-dev-admin@xxxxxxxxxxx

07/22/2004 04:09 PM

Please respond to
platform-swt-dev

To
platform-swt-dev@xxxxxxxxxxx
cc
Subject
[platform-swt-dev] Display mouse events





Fellow SWTers,
  I wrote a simple widget similar to the Windows
"magnifier" (see source at the end of the email). My
problem is that I want my application to listen to
MouseMove events globally to the display. Is that
possible in SWT? I've tried adding a MouseMove listener
to an instance of display obtained using
Display.getDefault() but that doesn't seem to work.

Thanks,
 Brian

Here's the code:

package com.integrallis.ui.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
* @author Brian Sam-Bodden
*/
public class Magnifier extends Canvas {

                private Display display;
                private int zoom = 2;
                private Image buffer;
                private GC screenGC;
   private Point previousCursorPosition;
   private boolean alwaysRepaint = false;

                /**
                 * @param parent
                 * @param style
                 */
                public Magnifier(Composite parent, int style) {
                                 super(parent, style);

                                 // create a GC for the display
                                 display = Display.getDefault();
                                 screenGC = new GC(display);

                                 zoom = 2;

                                 addPaintListener(new PaintListener() {
                                                  public void paintControl(PaintEvent e) {
                                                                   Magnifier.this.onPaint(e);
                                                  }
                                 });

                                 addDisposeListener(new DisposeListener() {
                                                  public void widgetDisposed(DisposeEvent e) {
                                                                   Magnifier.this.onDispose(e);
                                                  }
                                 });
       
       // this doesn't work :(
                                 display.addListener(SWT.MouseMove, new Listener() {
                                                  public void handleEvent(Event event) {
               System.out.println("display MouseMove
event");
                                                                   redraw();
                                                  }
                                 });
       
       // .. and this doesn't help
                                 addMouseMoveListener(new MouseMoveListener() {
                                                  public void mouseMove(MouseEvent e) {
                                                                   redraw();
                                                  }
                                 });
       
                }

                /**
                 * @param e
                 */
                protected void onDispose(DisposeEvent e) {
                                 if (screenGC != null) {
                                                  screenGC.dispose();
                                 }
                }

                /**
                 * @param e
                 */
                protected void onPaint(PaintEvent e) {
                                 // get the cursor position
                                 Point cp = display.getCursorLocation();
       
       // don't repaint if the mouse hasn't moved
       if (!alwaysRepaint &&
(cp.equals(previousCursorPosition))) {
           return;
       }
       
       previousCursorPosition = cp;

       // get the size of the control  
                                 Point size = getSize();
       
       // determine the area to capture
                                 Point capture = new Point(size.x / zoom, size.y /
zoom);

                                 // determine the area to get
                                 Rectangle screen = display.getBounds();  
                                 Point copyPos = new Point(0, 0);

                                 if ((cp.x - capture.x / 2) < 0) {
                                                  copyPos.x = 0;
                                 } else if ((cp.x + capture.x / 2) > screen.width) {
                                                  copyPos.x = screen.width - capture.x;
                                 } else {
                                                  copyPos.x = cp.x - (capture.x / 2);
                                 }

                                 if ((cp.y - capture.y / 2) < 0) {
                                                  copyPos.y = 0;
                                 } else if ((cp.y + capture.y / 2) > screen.height) {
                                                  copyPos.y = screen.height - capture.y;
                                 } else {
                                                  copyPos.y = cp.y - (capture.y / 2);
                                 }

                                 // Copy rectangle from screen to memory...  
                                 buffer = new Image(display, capture.x, capture.y);
                                 screenGC.copyArea(buffer, copyPos.x, copyPos.y);

                                 // zoom the memory image
                                 final Image scaled =
                                                  new Image(
                                                                   display,
                                                                   buffer.getImageData().scaledTo(size.x, size.y));

                                 // paint scaled image on the canvas
                                 e.gc.drawImage(scaled, 0, 0);
       
       // clean up
       buffer.dispose();
       scaled.dispose();
                }

                public Point computeSize(int wHint, int hHint, boolean
changed) {
                                 checkWidget();
                                 Point e = new Point(0, 0);

                                 if (wHint == SWT.DEFAULT) {
                                                  e.x = 100;
                                 } else {
                                                  e.x = wHint;
                                 }
                                 if (hHint == SWT.DEFAULT) {
                                                  e.y = 100;
                                 } else {
                                                  e.y = hHint;
                                 }

                                 return e;
                }

                /**
                 * @return
                 */
                public int getZoom() {
                                 return zoom;
                }

                /**
                 * @param i
                 */
                public void setZoom(int i) {
       if (i == zoom) return;
                                 zoom = i;
                }

                /**
                 * @return
                 */
                public boolean getAlwaysRepaint() {
                                 return alwaysRepaint;
                }

                /**
                 * @param b
                 */
                public void setAlwaysRepaint(boolean b) {
                                 alwaysRepaint = b;
                }

}
_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev


Back to the top