Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/XCustomCursor.java
41159 views
/*1* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import sun.awt.X11CustomCursor;28import java.awt.*;2930/**31* A class to encapsulate a custom image-based cursor.32*33* @see Component#setCursor34* @author Thomas Ball35* @author Bino George36*/37@SuppressWarnings("serial") // JDK-implementation class38public class XCustomCursor extends X11CustomCursor {3940public XCustomCursor(Image cursor, Point hotSpot, String name)41throws IndexOutOfBoundsException {42super(cursor, hotSpot, name);43}4445/**46* Returns the supported cursor size47*/48static Dimension getBestCursorSize(int preferredWidth, int preferredHeight) {4950// Fix for bug 4212593 The Toolkit.createCustomCursor does not51// check absence of the image of cursor52// We use XQueryBestCursor which accepts unsigned ints to obtain53// the largest cursor size that could be dislpayed54//Dimension d = new Dimension(Math.abs(preferredWidth), Math.abs(preferredHeight));55Dimension d;5657XToolkit.awtLock();58try {59long display = XToolkit.getDisplay();60long root_window = XlibWrapper.RootWindow(display,61XlibWrapper.DefaultScreen(display));6263XlibWrapper.XQueryBestCursor(display,root_window, Math.abs(preferredWidth),Math.abs(preferredHeight),XlibWrapper.larg1,XlibWrapper.larg2);64d = new Dimension(XlibWrapper.unsafe.getInt(XlibWrapper.larg1),XlibWrapper.unsafe.getInt(XlibWrapper.larg2));65if (preferredWidth > 0 && preferredHeight > 0) {66d.width = Math.min(d.width, preferredWidth);67d.height = Math.min(d.height, preferredHeight);68}69}70finally {71XToolkit.awtUnlock();72}73return d;74}7576protected void createCursor(byte[] xorMask, byte[] andMask,77int width, int height,78int fcolor, int bcolor,79int xHotSpot, int yHotSpot)80{81XToolkit.awtLock();82try {83long display = XToolkit.getDisplay();84long root_window = XlibWrapper.RootWindow(display,85XlibWrapper.DefaultScreen(display));8687long colormap = XToolkit.getDefaultXColormap();88XColor fore_color = new XColor();8990fore_color.set_flags((byte) (XConstants.DoRed | XConstants.DoGreen | XConstants.DoBlue));91fore_color.set_red((short)(((fcolor >> 16) & 0x000000ff) << 8));92fore_color.set_green((short) (((fcolor >> 8) & 0x000000ff) << 8));93fore_color.set_blue((short)(((fcolor >> 0) & 0x000000ff) << 8));9495XlibWrapper.XAllocColor(display,colormap,fore_color.pData);969798XColor back_color = new XColor();99back_color.set_flags((byte) (XConstants.DoRed | XConstants.DoGreen | XConstants.DoBlue));100101back_color.set_red((short) (((bcolor >> 16) & 0x000000ff) << 8));102back_color.set_green((short) (((bcolor >> 8) & 0x000000ff) << 8));103back_color.set_blue((short) (((bcolor >> 0) & 0x000000ff) << 8));104105XlibWrapper.XAllocColor(display,colormap,back_color.pData);106107108long nativeXorMask = Native.toData(xorMask);109long source = XlibWrapper.XCreateBitmapFromData(display,root_window,nativeXorMask,width,height);110111long nativeAndMask = Native.toData(andMask);112long mask = XlibWrapper.XCreateBitmapFromData(display,root_window,nativeAndMask,width,height);113114long cursor = XlibWrapper.XCreatePixmapCursor(display,source,mask,fore_color.pData,back_color.pData,xHotSpot,yHotSpot);115116XlibWrapper.unsafe.freeMemory(nativeXorMask);117XlibWrapper.unsafe.freeMemory(nativeAndMask);118XlibWrapper.XFreePixmap(display,source);119XlibWrapper.XFreePixmap(display,mask);120back_color.dispose();121fore_color.dispose();122123XGlobalCursorManager.setPData(this,cursor);124}125finally {126XToolkit.awtUnlock();127}128129}130}131132133