2
|
1 |
/*
|
|
2 |
* Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.awt.X11;
|
|
27 |
|
|
28 |
import java.awt.*;
|
|
29 |
import java.awt.peer.ComponentPeer;
|
|
30 |
import java.awt.peer.LightweightPeer;
|
|
31 |
import java.lang.ref.WeakReference;
|
|
32 |
import java.lang.reflect.Field;
|
|
33 |
import java.lang.reflect.Method;
|
|
34 |
import sun.awt.ComponentAccessor;
|
|
35 |
|
|
36 |
import sun.awt.GlobalCursorManager;
|
|
37 |
import sun.awt.SunToolkit;
|
|
38 |
|
|
39 |
public final class XGlobalCursorManager extends GlobalCursorManager {
|
|
40 |
|
|
41 |
private static Field field_pData;
|
|
42 |
private static Field field_type;
|
|
43 |
private static Class cursorClass;
|
|
44 |
private static Method method_setPData;
|
|
45 |
static {
|
|
46 |
cursorClass = java.awt.Cursor.class;
|
|
47 |
field_pData = SunToolkit.getField(cursorClass, "pData");
|
|
48 |
field_type = SunToolkit.getField(cursorClass, "type");
|
|
49 |
method_setPData = SunToolkit.getMethod(cursorClass, "setPData", new Class[] {long.class});
|
|
50 |
if (field_pData == null || field_type == null || method_setPData == null) {
|
|
51 |
System.out.println("Unable to initialize XGlobalCursorManager: ");
|
|
52 |
Thread.dumpStack();
|
|
53 |
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
|
|
58 |
// cached nativeContainer
|
|
59 |
private WeakReference<Component> nativeContainer;
|
|
60 |
|
|
61 |
|
|
62 |
/**
|
|
63 |
* The XGlobalCursorManager is a singleton.
|
|
64 |
*/
|
|
65 |
private static XGlobalCursorManager manager;
|
|
66 |
|
|
67 |
|
|
68 |
static GlobalCursorManager getCursorManager() {
|
|
69 |
if (manager == null) {
|
|
70 |
manager = new XGlobalCursorManager();
|
|
71 |
}
|
|
72 |
return manager;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Should be called in response to a native mouse enter or native mouse
|
|
77 |
* button released message. Should not be called during a mouse drag.
|
|
78 |
*/
|
|
79 |
static void nativeUpdateCursor(Component heavy) {
|
|
80 |
XGlobalCursorManager.getCursorManager().updateCursorLater(heavy);
|
|
81 |
}
|
|
82 |
|
|
83 |
|
|
84 |
protected void setCursor(Component comp, Cursor cursor, boolean useCache) {
|
|
85 |
if (comp == null) {
|
|
86 |
return;
|
|
87 |
}
|
|
88 |
|
|
89 |
Cursor cur = useCache ? cursor : getCapableCursor(comp);
|
|
90 |
|
|
91 |
Component nc = null;
|
|
92 |
if (useCache) {
|
|
93 |
synchronized (this) {
|
|
94 |
nc = nativeContainer.get();
|
|
95 |
}
|
|
96 |
} else {
|
|
97 |
nc = getNativeContainer(comp);
|
|
98 |
}
|
|
99 |
|
|
100 |
if (nc != null) {
|
|
101 |
ComponentPeer nc_peer = ComponentAccessor.getPeer(nc);
|
|
102 |
if (nc_peer instanceof XComponentPeer) {
|
|
103 |
synchronized (this) {
|
|
104 |
nativeContainer = new WeakReference<Component>(nc);
|
|
105 |
}
|
|
106 |
|
|
107 |
((XComponentPeer)nc_peer).pSetCursor(cur);
|
|
108 |
// in case of grab we do for Swing we need to update keep cursor updated
|
|
109 |
// (we don't need this in case of AWT menus). Window Manager consider
|
|
110 |
// the grabber as a current window and use its cursor. So we need to
|
|
111 |
// change cursor on the grabber too.
|
|
112 |
updateGrabbedCursor(cur);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Updates cursor on the grabber if it is window peer (i.e. current grab is for
|
|
119 |
* Swing, not for AWT.
|
|
120 |
*/
|
|
121 |
private static void updateGrabbedCursor(Cursor cur) {
|
|
122 |
XBaseWindow target = XAwtState.getGrabWindow();
|
|
123 |
if (target instanceof XWindowPeer) {
|
|
124 |
XWindowPeer grabber = (XWindowPeer) target;
|
|
125 |
grabber.pSetCursor(cur);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
protected void updateCursorOutOfJava() {
|
|
130 |
// in case we have grabbed input for Swing we need to reset cursor
|
|
131 |
// when mouse pointer is out of any java toplevel.
|
|
132 |
// let's use default cursor for this.
|
|
133 |
updateGrabbedCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
|
|
134 |
}
|
|
135 |
|
|
136 |
private Component getNativeContainer(Component comp) {
|
|
137 |
while (comp != null && ComponentAccessor.getPeer(comp) instanceof LightweightPeer) {
|
|
138 |
comp = ComponentAccessor.getParent_NoClientCode(comp);
|
|
139 |
}
|
|
140 |
return comp;
|
|
141 |
}
|
|
142 |
|
|
143 |
protected void getCursorPos(Point p) {
|
|
144 |
|
|
145 |
if (!((XToolkit)Toolkit.getDefaultToolkit()).getLastCursorPos(p)) {
|
|
146 |
XToolkit.awtLock();
|
|
147 |
try {
|
|
148 |
long display = XToolkit.getDisplay();
|
|
149 |
long root_window = XlibWrapper.RootWindow(display,
|
|
150 |
XlibWrapper.DefaultScreen(display));
|
|
151 |
|
|
152 |
XlibWrapper.XQueryPointer(display, root_window,
|
|
153 |
XlibWrapper.larg1,
|
|
154 |
XlibWrapper.larg2,
|
|
155 |
XlibWrapper.larg3,
|
|
156 |
XlibWrapper.larg4,
|
|
157 |
XlibWrapper.larg5,
|
|
158 |
XlibWrapper.larg6,
|
|
159 |
XlibWrapper.larg7);
|
|
160 |
|
|
161 |
p.x = (int) XlibWrapper.unsafe.getInt(XlibWrapper.larg3);
|
|
162 |
p.y = (int) XlibWrapper.unsafe.getInt(XlibWrapper.larg4);
|
|
163 |
} finally {
|
|
164 |
XToolkit.awtUnlock();
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|
|
168 |
protected Component findHeavyweightUnderCursor() {
|
|
169 |
return XAwtState.getComponentMouseEntered();
|
|
170 |
}
|
|
171 |
|
|
172 |
/*
|
|
173 |
* two native methods to call corresponding methods in Container and
|
|
174 |
* Component
|
|
175 |
*/
|
|
176 |
protected Component findComponentAt(Container con, int x, int y) {
|
|
177 |
return con.findComponentAt(x,y);
|
|
178 |
}
|
|
179 |
|
|
180 |
protected Point getLocationOnScreen(Component c) {
|
|
181 |
return c.getLocationOnScreen();
|
|
182 |
}
|
|
183 |
|
|
184 |
protected Component findHeavyweightUnderCursor(boolean useCache) {
|
|
185 |
return findHeavyweightUnderCursor();
|
|
186 |
}
|
|
187 |
|
|
188 |
private Cursor getCapableCursor(Component comp) {
|
|
189 |
Component c = comp;
|
|
190 |
while ((c != null) && !(c instanceof Window)
|
|
191 |
&& ComponentAccessor.isEnabledImpl(c)
|
|
192 |
&& ComponentAccessor.getVisible(c)
|
|
193 |
&& ComponentAccessor.getPeer(c) != null)
|
|
194 |
{
|
|
195 |
c = ComponentAccessor.getParent_NoClientCode(c);
|
|
196 |
}
|
|
197 |
if (c instanceof Window) {
|
|
198 |
return (ComponentAccessor.isEnabledImpl(c)
|
|
199 |
&& ComponentAccessor.getVisible(c)
|
|
200 |
&& (ComponentAccessor.getPeer(c) != null)
|
|
201 |
&& ComponentAccessor.isEnabledImpl(comp))
|
|
202 |
?
|
|
203 |
ComponentAccessor.getCursor_NoClientCode(comp)
|
|
204 |
:
|
|
205 |
Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
|
|
206 |
} else if (c == null) {
|
|
207 |
return null;
|
|
208 |
}
|
|
209 |
return getCapableCursor(ComponentAccessor.getParent_NoClientCode(c));
|
|
210 |
}
|
|
211 |
|
|
212 |
/* This methods needs to be called from within XToolkit.awtLock / XToolkit.awtUnlock section. */
|
|
213 |
|
|
214 |
static long getCursor(Cursor c) {
|
|
215 |
|
|
216 |
long pData = 0;
|
|
217 |
int type = 0;
|
|
218 |
try {
|
|
219 |
pData = field_pData.getLong(c);
|
|
220 |
type = field_type.getInt(c);
|
|
221 |
}
|
|
222 |
catch (Exception e)
|
|
223 |
{
|
|
224 |
e.printStackTrace();
|
|
225 |
}
|
|
226 |
|
|
227 |
if (pData != 0) return pData;
|
|
228 |
|
|
229 |
int cursorType = 0;
|
|
230 |
switch (type) {
|
|
231 |
case Cursor.DEFAULT_CURSOR:
|
|
232 |
cursorType = XlibWrapper.XC_left_ptr;
|
|
233 |
break;
|
|
234 |
case Cursor.CROSSHAIR_CURSOR:
|
|
235 |
cursorType = XlibWrapper.XC_crosshair;
|
|
236 |
break;
|
|
237 |
case Cursor.TEXT_CURSOR:
|
|
238 |
cursorType = XlibWrapper.XC_xterm;
|
|
239 |
break;
|
|
240 |
case Cursor.WAIT_CURSOR:
|
|
241 |
cursorType = XlibWrapper.XC_watch;
|
|
242 |
break;
|
|
243 |
case Cursor.SW_RESIZE_CURSOR:
|
|
244 |
cursorType = XlibWrapper.XC_bottom_left_corner;
|
|
245 |
break;
|
|
246 |
case Cursor.NW_RESIZE_CURSOR:
|
|
247 |
cursorType = XlibWrapper.XC_top_left_corner;
|
|
248 |
break;
|
|
249 |
case Cursor.SE_RESIZE_CURSOR:
|
|
250 |
cursorType = XlibWrapper.XC_bottom_right_corner;
|
|
251 |
break;
|
|
252 |
case Cursor.NE_RESIZE_CURSOR:
|
|
253 |
cursorType = XlibWrapper.XC_top_right_corner;
|
|
254 |
break;
|
|
255 |
case Cursor.S_RESIZE_CURSOR:
|
|
256 |
cursorType = XlibWrapper.XC_bottom_side;
|
|
257 |
break;
|
|
258 |
case Cursor.N_RESIZE_CURSOR:
|
|
259 |
cursorType = XlibWrapper.XC_top_side;
|
|
260 |
break;
|
|
261 |
case Cursor.W_RESIZE_CURSOR:
|
|
262 |
cursorType = XlibWrapper.XC_left_side;
|
|
263 |
break;
|
|
264 |
case Cursor.E_RESIZE_CURSOR:
|
|
265 |
cursorType = XlibWrapper.XC_right_side;
|
|
266 |
break;
|
|
267 |
case Cursor.HAND_CURSOR:
|
|
268 |
cursorType = XlibWrapper.XC_hand2;
|
|
269 |
break;
|
|
270 |
case Cursor.MOVE_CURSOR:
|
|
271 |
cursorType = XlibWrapper.XC_fleur;
|
|
272 |
break;
|
|
273 |
}
|
|
274 |
|
|
275 |
XToolkit.awtLock();
|
|
276 |
try {
|
|
277 |
pData =(long) XlibWrapper.XCreateFontCursor(XToolkit.getDisplay(), cursorType);
|
|
278 |
}
|
|
279 |
finally {
|
|
280 |
XToolkit.awtUnlock();
|
|
281 |
}
|
|
282 |
|
|
283 |
setPData(c,pData);
|
|
284 |
return pData;
|
|
285 |
}
|
|
286 |
|
|
287 |
|
|
288 |
static void setPData(Cursor c, long pData) {
|
|
289 |
try {
|
|
290 |
method_setPData.invoke(c, pData);
|
|
291 |
}
|
|
292 |
catch (Exception e)
|
|
293 |
{
|
|
294 |
e.printStackTrace();
|
|
295 |
}
|
|
296 |
|
|
297 |
}
|
|
298 |
}
|