0N/A/*
3909N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.peer.ComponentPeer;
0N/Aimport java.lang.ref.WeakReference;
1976N/Aimport sun.awt.AWTAccessor;
0N/A
0N/Aimport sun.awt.GlobalCursorManager;
0N/Aimport sun.awt.SunToolkit;
0N/A
0N/Apublic final class XGlobalCursorManager extends GlobalCursorManager {
0N/A
0N/A // cached nativeContainer
0N/A private WeakReference<Component> nativeContainer;
0N/A
0N/A
0N/A /**
0N/A * The XGlobalCursorManager is a singleton.
0N/A */
0N/A private static XGlobalCursorManager manager;
0N/A
0N/A
0N/A static GlobalCursorManager getCursorManager() {
0N/A if (manager == null) {
0N/A manager = new XGlobalCursorManager();
0N/A }
0N/A return manager;
0N/A }
0N/A
0N/A /**
0N/A * Should be called in response to a native mouse enter or native mouse
0N/A * button released message. Should not be called during a mouse drag.
0N/A */
0N/A static void nativeUpdateCursor(Component heavy) {
0N/A XGlobalCursorManager.getCursorManager().updateCursorLater(heavy);
0N/A }
0N/A
0N/A
0N/A protected void setCursor(Component comp, Cursor cursor, boolean useCache) {
0N/A if (comp == null) {
0N/A return;
0N/A }
0N/A
0N/A Cursor cur = useCache ? cursor : getCapableCursor(comp);
0N/A
0N/A Component nc = null;
0N/A if (useCache) {
0N/A synchronized (this) {
0N/A nc = nativeContainer.get();
0N/A }
0N/A } else {
1976N/A nc = SunToolkit.getHeavyweightComponent(comp);
0N/A }
0N/A
0N/A if (nc != null) {
1976N/A ComponentPeer nc_peer = AWTAccessor.getComponentAccessor().getPeer(nc);
0N/A if (nc_peer instanceof XComponentPeer) {
0N/A synchronized (this) {
0N/A nativeContainer = new WeakReference<Component>(nc);
0N/A }
0N/A
3446N/A //6431076. A subcomponents (a XTextArea in particular)
3446N/A //may want to override the cursor over some of their parts.
3446N/A ((XComponentPeer)nc_peer).pSetCursor(cur, false);
0N/A // in case of grab we do for Swing we need to update keep cursor updated
0N/A // (we don't need this in case of AWT menus). Window Manager consider
0N/A // the grabber as a current window and use its cursor. So we need to
0N/A // change cursor on the grabber too.
0N/A updateGrabbedCursor(cur);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Updates cursor on the grabber if it is window peer (i.e. current grab is for
0N/A * Swing, not for AWT.
0N/A */
0N/A private static void updateGrabbedCursor(Cursor cur) {
0N/A XBaseWindow target = XAwtState.getGrabWindow();
0N/A if (target instanceof XWindowPeer) {
0N/A XWindowPeer grabber = (XWindowPeer) target;
0N/A grabber.pSetCursor(cur);
0N/A }
0N/A }
0N/A
0N/A protected void updateCursorOutOfJava() {
0N/A // in case we have grabbed input for Swing we need to reset cursor
0N/A // when mouse pointer is out of any java toplevel.
0N/A // let's use default cursor for this.
0N/A updateGrabbedCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
0N/A }
0N/A
0N/A protected void getCursorPos(Point p) {
0N/A
0N/A if (!((XToolkit)Toolkit.getDefaultToolkit()).getLastCursorPos(p)) {
0N/A XToolkit.awtLock();
0N/A try {
0N/A long display = XToolkit.getDisplay();
0N/A long root_window = XlibWrapper.RootWindow(display,
0N/A XlibWrapper.DefaultScreen(display));
0N/A
0N/A XlibWrapper.XQueryPointer(display, root_window,
0N/A XlibWrapper.larg1,
0N/A XlibWrapper.larg2,
0N/A XlibWrapper.larg3,
0N/A XlibWrapper.larg4,
0N/A XlibWrapper.larg5,
0N/A XlibWrapper.larg6,
0N/A XlibWrapper.larg7);
0N/A
0N/A p.x = (int) XlibWrapper.unsafe.getInt(XlibWrapper.larg3);
0N/A p.y = (int) XlibWrapper.unsafe.getInt(XlibWrapper.larg4);
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A }
0N/A protected Component findHeavyweightUnderCursor() {
0N/A return XAwtState.getComponentMouseEntered();
0N/A }
0N/A
0N/A /*
0N/A * two native methods to call corresponding methods in Container and
0N/A * Component
0N/A */
0N/A protected Component findComponentAt(Container con, int x, int y) {
0N/A return con.findComponentAt(x,y);
0N/A }
0N/A
0N/A protected Point getLocationOnScreen(Component c) {
0N/A return c.getLocationOnScreen();
0N/A }
0N/A
0N/A protected Component findHeavyweightUnderCursor(boolean useCache) {
0N/A return findHeavyweightUnderCursor();
0N/A }
0N/A
0N/A private Cursor getCapableCursor(Component comp) {
1976N/A AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
1976N/A
0N/A Component c = comp;
0N/A while ((c != null) && !(c instanceof Window)
1976N/A && compAccessor.isEnabled(c)
1976N/A && compAccessor.isVisible(c)
1976N/A && compAccessor.isDisplayable(c))
0N/A {
1976N/A c = compAccessor.getParent(c);
0N/A }
0N/A if (c instanceof Window) {
1976N/A return (compAccessor.isEnabled(c)
1976N/A && compAccessor.isVisible(c)
1976N/A && compAccessor.isDisplayable(c)
1976N/A && compAccessor.isEnabled(comp))
0N/A ?
1976N/A compAccessor.getCursor(comp)
0N/A :
0N/A Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
0N/A } else if (c == null) {
0N/A return null;
0N/A }
1976N/A return getCapableCursor(compAccessor.getParent(c));
0N/A }
0N/A
0N/A /* This methods needs to be called from within XToolkit.awtLock / XToolkit.awtUnlock section. */
0N/A
0N/A static long getCursor(Cursor c) {
0N/A
0N/A long pData = 0;
0N/A int type = 0;
0N/A try {
5255N/A pData = AWTAccessor.getCursorAccessor().getPData(c);
5255N/A type = AWTAccessor.getCursorAccessor().getType(c);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A e.printStackTrace();
0N/A }
0N/A
0N/A if (pData != 0) return pData;
0N/A
0N/A int cursorType = 0;
0N/A switch (type) {
0N/A case Cursor.DEFAULT_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_left_ptr;
0N/A break;
0N/A case Cursor.CROSSHAIR_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_crosshair;
0N/A break;
0N/A case Cursor.TEXT_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_xterm;
0N/A break;
0N/A case Cursor.WAIT_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_watch;
0N/A break;
0N/A case Cursor.SW_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_bottom_left_corner;
0N/A break;
0N/A case Cursor.NW_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_top_left_corner;
0N/A break;
0N/A case Cursor.SE_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_bottom_right_corner;
0N/A break;
0N/A case Cursor.NE_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_top_right_corner;
0N/A break;
0N/A case Cursor.S_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_bottom_side;
0N/A break;
0N/A case Cursor.N_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_top_side;
0N/A break;
0N/A case Cursor.W_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_left_side;
0N/A break;
0N/A case Cursor.E_RESIZE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_right_side;
0N/A break;
0N/A case Cursor.HAND_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_hand2;
0N/A break;
0N/A case Cursor.MOVE_CURSOR:
216N/A cursorType = XCursorFontConstants.XC_fleur;
0N/A break;
0N/A }
0N/A
0N/A XToolkit.awtLock();
0N/A try {
0N/A pData =(long) XlibWrapper.XCreateFontCursor(XToolkit.getDisplay(), cursorType);
0N/A }
0N/A finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A
0N/A setPData(c,pData);
0N/A return pData;
0N/A }
0N/A
0N/A
0N/A static void setPData(Cursor c, long pData) {
0N/A try {
5255N/A AWTAccessor.getCursorAccessor().setPData(c, pData);
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A e.printStackTrace();
0N/A }
0N/A
0N/A }
0N/A}