jdk/src/share/classes/sun/awt/CustomCursor.java
author ant
Fri, 31 Aug 2012 16:31:29 +0400
changeset 13652 42544e68dc39
parent 5506 202f599c92aa
child 22636 5bb85dc6771b
permissions -rw-r--r--
6981400: Tabbing between textfield do not work properly when ALT+TAB 7157015: [macosx] Situation when KeyEventDispatcher doesn't work on AWT but does on Swing. 7121442: Regression : Reopen CR 6458497 still reproducible using JDK 7. Reviewed-by: art, leonidr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A class to encapsulate a custom image-based cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author      ThomasBall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public abstract class CustomCursor extends Cursor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public CustomCursor(Image cursor, Point hotSpot, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            throws IndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        image = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // Make sure image is fully loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Component c = new Canvas(); // for its imageUpdate method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        MediaTracker tracker = new MediaTracker(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        tracker.addImage(cursor, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            tracker.waitForAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int width = cursor.getWidth(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int height = cursor.getHeight(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Fix for bug 4212593 The Toolkit.createCustomCursor does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        //                     check absence of the image of cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // If the image is invalid, the cursor will be hidden (made completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // transparent). In this case, getBestCursorSize() will adjust negative w and h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // but we need to set the hotspot inside the image here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (tracker.isErrorAny() || width < 0 || height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
              hotSpot.x = hotSpot.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Scale image to nearest supported size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Dimension nativeSize = toolkit.getBestCursorSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (nativeSize.width != width || nativeSize.height != height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            cursor = cursor.getScaledInstance(nativeSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                              nativeSize.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                              Image.SCALE_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            width = nativeSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            height = nativeSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Verify that the hotspot is within cursor bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (hotSpot.x >= width || hotSpot.y >= height || hotSpot.x < 0 || hotSpot.y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new IndexOutOfBoundsException("invalid hotSpot");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* Extract ARGB array from image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * A transparency mask can be created in native code by checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * each pixel's top byte -- a 0 value means the pixel's transparent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * Since each platform's format of the bitmap and mask are likely to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * be different, their creation shouldn't be here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int[] pixels = new int[width * height];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ImageProducer ip = cursor.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        PixelGrabber pg = new PixelGrabber(ip, 0, 0, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                           pixels, 0, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            pg.grabPixels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        createNativeCursor(image, pixels, width, height, hotSpot.x, hotSpot.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected abstract void createNativeCursor(Image im,  int[] pixels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                               int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                               int xHotSpot, int yHotSpot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}