001: /*
002: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import sun.awt.X11CustomCursor;
029: import java.awt.*;
030:
031: /**
032: * A class to encapsulate a custom image-based cursor.
033: *
034: * @see Component#setCursor
035: * @version 1.17 01/23/03
036: * @author Thomas Ball
037: * @author Bino George
038: */
039: public class XCustomCursor extends X11CustomCursor {
040:
041: public XCustomCursor(Image cursor, Point hotSpot, String name)
042: throws IndexOutOfBoundsException {
043: super (cursor, hotSpot, name);
044: }
045:
046: /**
047: * Returns the supported cursor size
048: */
049: static Dimension getBestCursorSize(int preferredWidth,
050: int preferredHeight) {
051:
052: // Fix for bug 4212593 The Toolkit.createCustomCursor does not
053: // check absence of the image of cursor
054: // We use XQueryBestCursor which accepts unsigned ints to obtain
055: // the largest cursor size that could be dislpayed
056: //Dimension d = new Dimension(Math.abs(preferredWidth), Math.abs(preferredHeight));
057: Dimension d;
058:
059: XToolkit.awtLock();
060: try {
061: long display = XToolkit.getDisplay();
062: long root_window = XlibWrapper.RootWindow(display,
063: XlibWrapper.DefaultScreen(display));
064:
065: XlibWrapper.XQueryBestCursor(display, root_window, Math
066: .abs(preferredWidth), Math.abs(preferredHeight),
067: XlibWrapper.larg1, XlibWrapper.larg2);
068: d = new Dimension(XlibWrapper.unsafe
069: .getInt(XlibWrapper.larg1), XlibWrapper.unsafe
070: .getInt(XlibWrapper.larg2));
071: } finally {
072: XToolkit.awtUnlock();
073: }
074: return d;
075: }
076:
077: protected void createCursor(byte[] xorMask, byte[] andMask,
078: int width, int height, int fcolor, int bcolor,
079: int xHotSpot, int yHotSpot) {
080: XToolkit.awtLock();
081: try {
082: long display = XToolkit.getDisplay();
083: long root_window = XlibWrapper.RootWindow(display,
084: XlibWrapper.DefaultScreen(display));
085:
086: long colormap = XToolkit.getDefaultXColormap();
087: XColor fore_color = new XColor();
088:
089: fore_color.set_flags((byte) (XlibWrapper.DoRed
090: | XlibWrapper.DoGreen | XlibWrapper.DoBlue));
091: fore_color
092: .set_red((short) (((fcolor >> 16) & 0x000000ff) << 8));
093: fore_color
094: .set_green((short) (((fcolor >> 8) & 0x000000ff) << 8));
095: fore_color
096: .set_blue((short) (((fcolor >> 0) & 0x000000ff) << 8));
097:
098: XlibWrapper
099: .XAllocColor(display, colormap, fore_color.pData);
100:
101: XColor back_color = new XColor();
102: back_color.set_flags((byte) (XlibWrapper.DoRed
103: | XlibWrapper.DoGreen | XlibWrapper.DoBlue));
104:
105: back_color
106: .set_red((short) (((bcolor >> 16) & 0x000000ff) << 8));
107: back_color
108: .set_green((short) (((bcolor >> 8) & 0x000000ff) << 8));
109: back_color
110: .set_blue((short) (((bcolor >> 0) & 0x000000ff) << 8));
111:
112: XlibWrapper
113: .XAllocColor(display, colormap, back_color.pData);
114:
115: long nativeXorMask = Native.toData(xorMask);
116: long source = XlibWrapper.XCreateBitmapFromData(display,
117: root_window, nativeXorMask, width, height);
118:
119: long nativeAndMask = Native.toData(andMask);
120: long mask = XlibWrapper.XCreateBitmapFromData(display,
121: root_window, nativeAndMask, width, height);
122:
123: long cursor = XlibWrapper.XCreatePixmapCursor(display,
124: source, mask, fore_color.pData, back_color.pData,
125: xHotSpot, yHotSpot);
126:
127: XlibWrapper.unsafe.freeMemory(nativeXorMask);
128: XlibWrapper.unsafe.freeMemory(nativeAndMask);
129: XlibWrapper.XFreePixmap(display, source);
130: XlibWrapper.XFreePixmap(display, mask);
131: back_color.dispose();
132: fore_color.dispose();
133:
134: XGlobalCursorManager.setPData(this, cursor);
135: } finally {
136: XToolkit.awtUnlock();
137: }
138:
139: }
140: }
|