src/macosx/classes/sun/lwawt/macosx/CCustomCursor.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import java.awt.*;

  29 
  30 public class CCustomCursor extends Cursor {
  31     static Dimension sMaxCursorSize;
  32     static Dimension getMaxCursorSize() {
  33         if (sMaxCursorSize != null) return sMaxCursorSize;
  34         final Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
  35         return sMaxCursorSize = new Dimension(bounds.width / 2, bounds.height / 2);
  36     }
  37 
  38     Image fImage;
  39     private boolean isImageOk = false;
  40     Point fHotspot;
  41 
  42     public CCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
  43         super(name);
  44         fImage = cursor;
  45         fHotspot = hotSpot;
  46 
  47         // This chunk of code is copied from sun.awt.CustomCursor
  48         final Toolkit toolkit = Toolkit.getDefaultToolkit();
  49 
  50         // Make sure image is fully loaded.
  51         final Component c = new Canvas(); // for its imageUpdate method
  52         final MediaTracker tracker = new MediaTracker(c);
  53         tracker.addImage(fImage, 0);
  54         try {
  55             tracker.waitForAll();
  56         } catch (final InterruptedException e) {}
  57 
  58         int width = fImage.getWidth(c);
  59         int height = fImage.getHeight(c);
  60 
  61         // Fix for bug 4212593 The Toolkit.createCustomCursor does not
  62         // check absence of the image of cursor
  63         // If the image is invalid, the cursor will be hidden (made completely
  64         // transparent). In this case, getBestCursorSize() will adjust negative w and h,
  65         // but we need to set the hotspot inside the image here.
  66         if (tracker.isErrorAny() || width < 0 || height < 0) {
  67             fHotspot.x = fHotspot.y = 0;
  68             isImageOk = false;

  69         } else {
  70             isImageOk = true;
  71         }
  72 
  73         // Scale image to nearest supported size
  74         final Dimension nativeSize = toolkit.getBestCursorSize(width, height);
  75         if (nativeSize.width != width || nativeSize.height != height) {
  76             fImage = fImage.getScaledInstance(nativeSize.width, nativeSize.height, Image.SCALE_DEFAULT);
  77             width = nativeSize.width;
  78             height = nativeSize.height;
  79         }

  80 
  81         // NOTE: this was removed for 3169146, but in 1.5 the JCK tests for an exception and fails if one isn't thrown.
  82         // See what JBuilder does.
  83         // Verify that the hotspot is within cursor bounds.
  84         if (fHotspot.x >= width || fHotspot.y >= height || fHotspot.x < 0 || fHotspot.y < 0) {
  85             throw new IndexOutOfBoundsException("invalid hotSpot");
  86         }
  87 
  88         // Must normalize the hotspot
  89         if (fHotspot.x >= width) {
  90             fHotspot.x = width - 1; // it is zero based.
  91         } else if (fHotspot.x < 0) {
  92             fHotspot.x = 0;
  93         }
  94         if (fHotspot.y >= height) {
  95             fHotspot.y = height - 1; // it is zero based.
  96         } else if (fHotspot.y < 0) {
  97             fHotspot.y = 0;
  98         }
  99     }
 100 















 101     public static Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) {
 102         // With Panther, cursors have no limit on their size. So give the client their
 103         // preferred size, but no larger than half the dimensions of the main screen
 104         // This will allow large cursors, but not cursors so large that they cover the
 105         // screen. Since solaris nor windows allow cursors this big, this shouldn't be
 106         // a limitation.
 107         // JCK triggers an overflow in the int -- if we get a bizarre value normalize it.
 108         final Dimension maxCursorSize = getMaxCursorSize();
 109         final Dimension d = new Dimension(Math.max(1, Math.abs(preferredWidth)), Math.max(1, Math.abs(preferredHeight)));
 110         return new Dimension(Math.min(d.width, maxCursorSize.width), Math.min(d.height, maxCursorSize.height));
 111     }
 112 
 113     // Called from native when the cursor is set
 114     // Returns long array of [NSImage ptr, x hotspot, y hotspot]
 115     CImage fCImage;
 116     long getImageData() {
 117         if (fCImage != null) {
 118             return fCImage.ptr;
 119         }
 120 
 121         if (isImageOk) {
 122             try {
 123                 fCImage = CImage.getCreator().createFromImage(fImage);
 124 
 125                 if (fCImage == null) {
 126                     isImageOk = false;


 127                     return 0L;
 128                 } else {
 129                     return fCImage.ptr;
 130                 }
 131             } catch (IllegalArgumentException iae) {
 132                 // Silently return null - we want to hide cursor by providing an empty
 133                 // ByteArray or just null
 134                 return 0L;
 135             }
 136         }
 137 
 138         return 0L;
 139     }
 140 
 141     Point getHotSpot() {
 142         return fHotspot;
 143     }
 144 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import java.awt.*;
  29 import java.awt.image.BufferedImage;
  30 
  31 public class CCustomCursor extends Cursor {
  32     static Dimension sMaxCursorSize;
  33     static Dimension getMaxCursorSize() {
  34         if (sMaxCursorSize != null) return sMaxCursorSize;
  35         final Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
  36         return sMaxCursorSize = new Dimension(bounds.width / 2, bounds.height / 2);
  37     }
  38 
  39     Image fImage;

  40     Point fHotspot;
  41 
  42     public CCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
  43         super(name);
  44         fImage = cursor;
  45         fHotspot = hotSpot;
  46 
  47         // This chunk of code is copied from sun.awt.CustomCursor
  48         final Toolkit toolkit = Toolkit.getDefaultToolkit();
  49 
  50         // Make sure image is fully loaded.
  51         final Component c = new Canvas(); // for its imageUpdate method
  52         final MediaTracker tracker = new MediaTracker(c);
  53         tracker.addImage(fImage, 0);
  54         try {
  55             tracker.waitForAll();
  56         } catch (final InterruptedException e) {}
  57 
  58         int width = fImage.getWidth(c);
  59         int height = fImage.getHeight(c);
  60 
  61         // Fix for bug 4212593 The Toolkit.createCustomCursor does not
  62         // check absence of the image of cursor
  63         // If the image is invalid, the cursor will be hidden (made completely
  64         // transparent).

  65         if (tracker.isErrorAny() || width < 0 || height < 0) {
  66             fHotspot.x = fHotspot.y = 0;
  67             width = height = 1;
  68             fImage = createTransparentImage(width, height);
  69         } else {



  70             // Scale image to nearest supported size
  71             final Dimension nativeSize = toolkit.getBestCursorSize(width, height);
  72             if (nativeSize.width != width || nativeSize.height != height) {
  73                 fImage = fImage.getScaledInstance(nativeSize.width, nativeSize.height, Image.SCALE_DEFAULT);
  74                 width = nativeSize.width;
  75                 height = nativeSize.height;
  76             }
  77         }
  78 
  79         // NOTE: this was removed for 3169146, but in 1.5 the JCK tests for an exception and fails if one isn't thrown.
  80         // See what JBuilder does.
  81         // Verify that the hotspot is within cursor bounds.
  82         if (fHotspot.x >= width || fHotspot.y >= height || fHotspot.x < 0 || fHotspot.y < 0) {
  83             throw new IndexOutOfBoundsException("invalid hotSpot");
  84         }
  85 
  86         // Must normalize the hotspot
  87         if (fHotspot.x >= width) {
  88             fHotspot.x = width - 1; // it is zero based.
  89         } else if (fHotspot.x < 0) {
  90             fHotspot.x = 0;
  91         }
  92         if (fHotspot.y >= height) {
  93             fHotspot.y = height - 1; // it is zero based.
  94         } else if (fHotspot.y < 0) {
  95             fHotspot.y = 0;
  96         }
  97     }
  98 
  99     private static BufferedImage createTransparentImage(int w, int h) {
 100         GraphicsEnvironment ge =
 101                 GraphicsEnvironment.getLocalGraphicsEnvironment();
 102         GraphicsDevice gs = ge.getDefaultScreenDevice();
 103         GraphicsConfiguration gc = gs.getDefaultConfiguration();
 104 
 105         BufferedImage img = gc.createCompatibleImage(w, h, Transparency.BITMASK);
 106         Graphics2D g = (Graphics2D)img.getGraphics();
 107         g.setBackground(new Color(0, 0, 0, 0));
 108         g.clearRect(0, 0, w, h);
 109         g.dispose();
 110 
 111         return img;
 112     }
 113 
 114     public static Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) {
 115         // With Panther, cursors have no limit on their size. So give the client their
 116         // preferred size, but no larger than half the dimensions of the main screen
 117         // This will allow large cursors, but not cursors so large that they cover the
 118         // screen. Since solaris nor windows allow cursors this big, this shouldn't be
 119         // a limitation.
 120         // JCK triggers an overflow in the int -- if we get a bizarre value normalize it.
 121         final Dimension maxCursorSize = getMaxCursorSize();
 122         final Dimension d = new Dimension(Math.max(1, Math.abs(preferredWidth)), Math.max(1, Math.abs(preferredHeight)));
 123         return new Dimension(Math.min(d.width, maxCursorSize.width), Math.min(d.height, maxCursorSize.height));
 124     }
 125 
 126     // Called from native when the cursor is set

 127     CImage fCImage;
 128     long getImageData() {
 129         if (fCImage != null) {
 130             return fCImage.ptr;
 131         }
 132 

 133         try {
 134             fCImage = CImage.getCreator().createFromImage(fImage);

 135             if (fCImage == null) {
 136                 // Something unexpected happened: CCustomCursor constructor
 137                 // takes care of invalid cursor images, yet createFromImage()
 138                 // failed to do its job. Return null to keep the cursor unchanged.
 139                 return 0L;
 140             } else {
 141                 return fCImage.ptr;
 142             }
 143         } catch (IllegalArgumentException iae) {
 144             // see comment above

 145             return 0L;
 146         }
 147     }
 148 



 149     Point getHotSpot() {
 150         return fHotspot;
 151     }
 152 }