jdk/src/solaris/classes/sun/awt/X11/XCustomCursor.java
author dcherepanov
Mon, 07 Dec 2009 13:43:04 +0300
changeset 4371 dc9dcb8b0ae7
parent 439 3488710b02f8
child 5506 202f599c92aa
permissions -rw-r--r--
6823138: Need to replace ComponentAccessor with AWTAccessor Reviewed-by: art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
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.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.awt.X11CustomCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
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
 * @see Component#setCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author      Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author      Bino George
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class XCustomCursor extends X11CustomCursor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public XCustomCursor(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(cursor, hotSpot, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Returns the supported cursor size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static Dimension getBestCursorSize(int preferredWidth, int preferredHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // Fix for bug 4212593 The Toolkit.createCustomCursor does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        //                     check absence of the image of cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // We use XQueryBestCursor which accepts unsigned ints to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // the largest cursor size that could be dislpayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        //Dimension d = new Dimension(Math.abs(preferredWidth), Math.abs(preferredHeight));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            long root_window = XlibWrapper.RootWindow(display,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    XlibWrapper.DefaultScreen(display));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            XlibWrapper.XQueryBestCursor(display,root_window, Math.abs(preferredWidth),Math.abs(preferredHeight),XlibWrapper.larg1,XlibWrapper.larg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            d = new Dimension(XlibWrapper.unsafe.getInt(XlibWrapper.larg1),XlibWrapper.unsafe.getInt(XlibWrapper.larg2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected void createCursor(byte[] xorMask, byte[] andMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                int fcolor, int bcolor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                int xHotSpot, int yHotSpot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            long root_window = XlibWrapper.RootWindow(display,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    XlibWrapper.DefaultScreen(display));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            long colormap = XToolkit.getDefaultXColormap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            XColor fore_color = new XColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
    86
            fore_color.set_flags((byte) (XConstants.DoRed | XConstants.DoGreen | XConstants.DoBlue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            fore_color.set_red((short)(((fcolor >> 16) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            fore_color.set_green((short) (((fcolor >> 8) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            fore_color.set_blue((short)(((fcolor >> 0) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            XlibWrapper.XAllocColor(display,colormap,fore_color.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            XColor back_color = new XColor();
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
    95
            back_color.set_flags((byte) (XConstants.DoRed | XConstants.DoGreen | XConstants.DoBlue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            back_color.set_red((short) (((bcolor >> 16) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            back_color.set_green((short) (((bcolor >> 8) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            back_color.set_blue((short) (((bcolor >> 0) & 0x000000ff) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            XlibWrapper.XAllocColor(display,colormap,back_color.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            long nativeXorMask = Native.toData(xorMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            long source = XlibWrapper.XCreateBitmapFromData(display,root_window,nativeXorMask,width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            long nativeAndMask = Native.toData(andMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            long mask =  XlibWrapper.XCreateBitmapFromData(display,root_window,nativeAndMask,width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            long cursor = XlibWrapper.XCreatePixmapCursor(display,source,mask,fore_color.pData,back_color.pData,xHotSpot,yHotSpot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            XlibWrapper.unsafe.freeMemory(nativeXorMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            XlibWrapper.unsafe.freeMemory(nativeAndMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            XlibWrapper.XFreePixmap(display,source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            XlibWrapper.XFreePixmap(display,mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            back_color.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            fore_color.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            XGlobalCursorManager.setPData(this,cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}