1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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.awt.windows;
  27 
  28 import sun.awt.CustomCursor;
  29 import java.awt.*;
  30 import java.awt.image.*;
  31 import sun.awt.image.ImageRepresentation;
  32 import sun.awt.image.IntegerComponentRaster;
  33 import sun.awt.image.ToolkitImage;
  34 
  35 /**
  36  * A class to encapsulate a custom image-based cursor.
  37  *
  38  * @see Component#setCursor
  39  * @author      ThomasBall
  40  */
  41 final class WCustomCursor extends CustomCursor {
  42 
  43     WCustomCursor(Image cursor, Point hotSpot, String name)
  44             throws IndexOutOfBoundsException {
  45         super(cursor, hotSpot, name);
  46     }
  47 
  48     @Override
  49     protected void createNativeCursor(Image im, int[] pixels, int w, int h,
  50                                       int xHotSpot, int yHotSpot) {
  51         BufferedImage bimage = new BufferedImage(w, h,
  52                                BufferedImage.TYPE_INT_RGB);
  53         Graphics g = bimage.getGraphics();
  54         try {
  55             if (im instanceof ToolkitImage) {
  56                 ImageRepresentation ir = ((ToolkitImage)im).getImageRep();
  57                 ir.reconstruct(ImageObserver.ALLBITS);
  58             }
  59             g.drawImage(im, 0, 0, w, h, null);
  60         } finally {
  61             g.dispose();
  62         }
  63         Raster  raster = bimage.getRaster();
  64         DataBuffer buffer = raster.getDataBuffer();
  65         // REMIND: native code should use ScanStride _AND_ width
  66         int data[] = ((DataBufferInt)buffer).getData();
  67 
  68         byte[] andMask = new byte[w * h / 8];
  69         int npixels = pixels.length;
  70         for (int i = 0; i < npixels; i++) {
  71             int ibyte = i / 8;
  72             int omask = 1 << (7 - (i % 8));
  73             if ((pixels[i] & 0xff000000) == 0) {
  74                 // Transparent bit
  75                 andMask[ibyte] |= omask;
  76             }
  77         }
  78 
  79         {
  80             int     ficW = raster.getWidth();
  81             if( raster instanceof IntegerComponentRaster ) {
  82                 ficW = ((IntegerComponentRaster)raster).getScanlineStride();
  83             }
  84             createCursorIndirect(
  85                 ((DataBufferInt)bimage.getRaster().getDataBuffer()).getData(),
  86                 andMask, ficW, raster.getWidth(), raster.getHeight(),
  87                 xHotSpot, yHotSpot);
  88         }
  89     }
  90 
  91     private native void createCursorIndirect(int[] rData, byte[] andMask,
  92                                              int nScanStride, int width,
  93                                              int height, int xHotSpot,
  94                                              int yHotSpot);
  95     /**
  96      * Return the current value of SM_CXCURSOR.
  97      */
  98     static native int getCursorWidth();
  99 
 100     /**
 101      * Return the current value of SM_CYCURSOR.
 102      */
 103     static native int getCursorHeight();
 104 }