jdk/src/solaris/classes/sun/awt/X11CustomCursor.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 sun.awt.CustomCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.image.ImageRepresentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A class to encapsulate a custom image-based cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see Component#setCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public abstract class X11CustomCursor extends CustomCursor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public X11CustomCursor(Image cursor, Point hotSpot, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            throws IndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        super(cursor, hotSpot, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected void createNativeCursor(Image im, int[] pixels, int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                      int xHotSpot, int yHotSpot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        class CCount implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            int color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            public CCount(int cl, int ct) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                color = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                count = ct;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            public int compareTo(Object cc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                return ((CCount)cc).count - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int tmp[] = new int[pixels.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i=0; i<pixels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if ((pixels[i] & 0xff000000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                tmp[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                tmp[i] = pixels[i] & 0x00ffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        java.util.Arrays.sort(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int fc = 0x000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int bc = 0xffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        CCount cols[] = new CCount[pixels.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int is = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int numColors = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while ( is < pixels.length ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (tmp[is] != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                cols[numColors++] = new CCount(tmp[is], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            is ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i = is+1; i < pixels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (tmp[i] != cols[numColors-1].color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                cols[numColors++] = new CCount(tmp[i], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                cols[numColors-1].count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        java.util.Arrays.sort(cols, 0, numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (numColors > 0) fc = cols[0].color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int fcr = (fc >> 16) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int fcg = (fc >>  8) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int fcb = (fc >>  0) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int rdis = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int gdis = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int bdis = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int j = 1; j < numColors; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            int rr = (cols[j].color >> 16) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int gg = (cols[j].color >>  8) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int bb = (cols[j].color >>  0) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            rdis = rdis + cols[j].count * rr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            gdis = gdis + cols[j].count * gg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            bdis = bdis + cols[j].count * bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int rest = pixels.length - ((numColors > 0) ? cols[0].count : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // 4653170 Avoid divide / zero exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    if (rest > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        rdis = rdis / rest - fcr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        gdis = gdis / rest - fcg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        bdis = bdis / rest - fcb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        rdis = (rdis*rdis + gdis*gdis + bdis*bdis) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // System.out.println(" rdis is "+ rdis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int j = 1; j < numColors; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int rr = (cols[j].color >> 16) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int gg = (cols[j].color >>  8) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int bb = (cols[j].color >>  0) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if ( (rr-fcr)*(rr-fcr) + (gg-fcg)*(gg-fcg) + (bb-fcb)*(bb-fcb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                 >= rdis )  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                bc = cols[j].color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int bcr = (bc >> 16) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int bcg = (bc >>  8) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int bcb = (bc >>  0) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // On Solaris 2.5.x, the above code for cursor of any size runs fine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // but on Solaris 2.6, the width of a cursor has to be 8 divisible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        //   otherwise, the cursor could be displayed as garbaged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // To work around the 2.6 problem, the following code pads any cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        //   with a transparent area to make a new cursor of width 8 multiples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // --- Bug 4148455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int wNByte = (width + 7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int tNByte = wNByte * height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        byte[] xorMask = new byte[tNByte];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        byte[] andMask = new byte[tNByte];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int i = 0; i < width; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            int omask = 1 << (i % 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            for (int j = 0; j < height; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                int ip = j*width + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                int ibyte = j*wNByte + i/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if ((pixels[ip] & 0xff000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    andMask[ibyte] |= omask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                int pr = (pixels[ip] >> 16) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                int pg = (pixels[ip] >>  8) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                int pb = (pixels[ip] >>  0) & 0x000000ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if ( (pr-fcr)*(pr-fcr) + (pg-fcg)*(pg-fcg) + (pb-fcb)*(pb-fcb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                  <= (pr-bcr)*(pr-bcr) + (pg-bcg)*(pg-bcg) + (pb-bcb)*(pb-bcb) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    // show foreground color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    xorMask[ibyte] |= omask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        createCursor(xorMask, andMask, 8*wNByte, height, fc, bc, xHotSpot, yHotSpot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected abstract void createCursor(byte[] xorMask, byte[] andMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                     int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                     int fcolor, int bcolor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                     int xHotSpot, int yHotSpot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}