4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage sun.lwawt.macosx;
4632N/A
4632N/Aimport java.awt.*;
4929N/Aimport java.awt.image.BufferedImage;
4632N/A
4632N/Apublic class CCustomCursor extends Cursor {
4632N/A static Dimension sMaxCursorSize;
4632N/A static Dimension getMaxCursorSize() {
4632N/A if (sMaxCursorSize != null) return sMaxCursorSize;
4632N/A final Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
4632N/A return sMaxCursorSize = new Dimension(bounds.width / 2, bounds.height / 2);
4632N/A }
4632N/A
4632N/A Image fImage;
4632N/A Point fHotspot;
4632N/A
4632N/A public CCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
4632N/A super(name);
4632N/A fImage = cursor;
4632N/A fHotspot = hotSpot;
4632N/A
4632N/A // This chunk of code is copied from sun.awt.CustomCursor
4632N/A final Toolkit toolkit = Toolkit.getDefaultToolkit();
4632N/A
4632N/A // Make sure image is fully loaded.
4632N/A final Component c = new Canvas(); // for its imageUpdate method
4632N/A final MediaTracker tracker = new MediaTracker(c);
4632N/A tracker.addImage(fImage, 0);
4632N/A try {
4632N/A tracker.waitForAll();
4632N/A } catch (final InterruptedException e) {}
4632N/A
4632N/A int width = fImage.getWidth(c);
4632N/A int height = fImage.getHeight(c);
4632N/A
4632N/A // Fix for bug 4212593 The Toolkit.createCustomCursor does not
4632N/A // check absence of the image of cursor
4632N/A // If the image is invalid, the cursor will be hidden (made completely
4929N/A // transparent).
4632N/A if (tracker.isErrorAny() || width < 0 || height < 0) {
4632N/A fHotspot.x = fHotspot.y = 0;
4929N/A width = height = 1;
4929N/A fImage = createTransparentImage(width, height);
4769N/A } else {
4929N/A // Scale image to nearest supported size
4929N/A final Dimension nativeSize = toolkit.getBestCursorSize(width, height);
4929N/A if (nativeSize.width != width || nativeSize.height != height) {
4929N/A fImage = fImage.getScaledInstance(nativeSize.width, nativeSize.height, Image.SCALE_DEFAULT);
4929N/A width = nativeSize.width;
4929N/A height = nativeSize.height;
4929N/A }
4632N/A }
4632N/A
4632N/A // NOTE: this was removed for 3169146, but in 1.5 the JCK tests for an exception and fails if one isn't thrown.
4632N/A // See what JBuilder does.
4632N/A // Verify that the hotspot is within cursor bounds.
4632N/A if (fHotspot.x >= width || fHotspot.y >= height || fHotspot.x < 0 || fHotspot.y < 0) {
4632N/A throw new IndexOutOfBoundsException("invalid hotSpot");
4632N/A }
4632N/A
4632N/A // Must normalize the hotspot
4632N/A if (fHotspot.x >= width) {
4632N/A fHotspot.x = width - 1; // it is zero based.
4632N/A } else if (fHotspot.x < 0) {
4632N/A fHotspot.x = 0;
4632N/A }
4632N/A if (fHotspot.y >= height) {
4632N/A fHotspot.y = height - 1; // it is zero based.
4632N/A } else if (fHotspot.y < 0) {
4632N/A fHotspot.y = 0;
4632N/A }
4632N/A }
4632N/A
4929N/A private static BufferedImage createTransparentImage(int w, int h) {
4929N/A GraphicsEnvironment ge =
4929N/A GraphicsEnvironment.getLocalGraphicsEnvironment();
4929N/A GraphicsDevice gs = ge.getDefaultScreenDevice();
4929N/A GraphicsConfiguration gc = gs.getDefaultConfiguration();
4929N/A
4929N/A BufferedImage img = gc.createCompatibleImage(w, h, Transparency.BITMASK);
4929N/A Graphics2D g = (Graphics2D)img.getGraphics();
4929N/A g.setBackground(new Color(0, 0, 0, 0));
4929N/A g.clearRect(0, 0, w, h);
4929N/A g.dispose();
4929N/A
4929N/A return img;
4929N/A }
4929N/A
4632N/A public static Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) {
4632N/A // With Panther, cursors have no limit on their size. So give the client their
4632N/A // preferred size, but no larger than half the dimensions of the main screen
4632N/A // This will allow large cursors, but not cursors so large that they cover the
4632N/A // screen. Since solaris nor windows allow cursors this big, this shouldn't be
4632N/A // a limitation.
4632N/A // JCK triggers an overflow in the int -- if we get a bizarre value normalize it.
4632N/A final Dimension maxCursorSize = getMaxCursorSize();
4632N/A final Dimension d = new Dimension(Math.max(1, Math.abs(preferredWidth)), Math.max(1, Math.abs(preferredHeight)));
4632N/A return new Dimension(Math.min(d.width, maxCursorSize.width), Math.min(d.height, maxCursorSize.height));
4632N/A }
4632N/A
4632N/A // Called from native when the cursor is set
4632N/A CImage fCImage;
4632N/A long getImageData() {
4769N/A if (fCImage != null) {
4769N/A return fCImage.ptr;
4769N/A }
4769N/A
4929N/A try {
4929N/A fCImage = CImage.getCreator().createFromImage(fImage);
4929N/A if (fCImage == null) {
4929N/A // Something unexpected happened: CCustomCursor constructor
4929N/A // takes care of invalid cursor images, yet createFromImage()
4929N/A // failed to do its job. Return null to keep the cursor unchanged.
4632N/A return 0L;
4929N/A } else {
4929N/A return fCImage.ptr;
4632N/A }
4929N/A } catch (IllegalArgumentException iae) {
4929N/A // see comment above
4929N/A return 0L;
4632N/A }
4632N/A }
4632N/A
4632N/A Point getHotSpot() {
4632N/A return fHotspot;
4632N/A }
4632N/A}