author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 439 | 3488710b02f8 |
child 2802 | d05a9dcc8296 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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.dnd.DropTarget; |
|
30 |
import java.awt.dnd.DropTargetListener; |
|
31 |
import java.awt.event.*; |
|
32 |
import java.awt.image.ColorModel; |
|
33 |
import java.awt.image.ImageObserver; |
|
34 |
import java.awt.image.ImageProducer; |
|
35 |
import java.awt.image.VolatileImage; |
|
36 |
import java.awt.peer.*; |
|
37 |
import sun.awt.*; |
|
38 |
import sun.awt.motif.X11FontMetrics; |
|
39 |
import java.lang.reflect.*; |
|
40 |
import java.util.logging.*; |
|
41 |
import java.util.*; |
|
42 |
import static sun.awt.X11.XEmbedHelper.*; |
|
43 |
||
44 |
import java.security.AccessController; |
|
45 |
import sun.security.action.GetBooleanAction; |
|
46 |
||
47 |
public class XEmbedCanvasPeer extends XCanvasPeer implements WindowFocusListener, KeyEventPostProcessor, ModalityListener, WindowIDProvider { |
|
48 |
private static final Logger xembedLog = Logger.getLogger("sun.awt.X11.xembed.XEmbedCanvasPeer"); |
|
49 |
||
50 |
boolean applicationActive; // Whether the application is active(has focus) |
|
51 |
XEmbedServer xembed = new XEmbedServer(); // Helper object, contains XEmbed intrinsics |
|
52 |
Map<Long, AWTKeyStroke> accelerators = new HashMap<Long, AWTKeyStroke>(); // Maps accelerator ID into AWTKeyStroke |
|
53 |
Map<AWTKeyStroke, Long> accel_lookup = new HashMap<AWTKeyStroke, Long>(); // Maps AWTKeyStroke into accelerator ID |
|
54 |
Set<GrabbedKey> grabbed_keys = new HashSet<GrabbedKey>(); // A set of keys grabbed by client |
|
55 |
Object ACCEL_LOCK = accelerators; // Lock object for working with accelerators; |
|
56 |
Object GRAB_LOCK = grabbed_keys; // Lock object for working with keys grabbed by client |
|
57 |
||
58 |
XEmbedCanvasPeer() {} |
|
59 |
||
60 |
XEmbedCanvasPeer(XCreateWindowParams params) { |
|
61 |
super(params); |
|
62 |
} |
|
63 |
||
64 |
XEmbedCanvasPeer(Component target) { |
|
65 |
super(target); |
|
66 |
} |
|
67 |
||
68 |
protected void postInit(XCreateWindowParams params) { |
|
69 |
super.postInit(params); |
|
70 |
||
71 |
installActivateListener(); |
|
72 |
installAcceleratorListener(); |
|
73 |
installModalityListener(); |
|
74 |
||
75 |
// XEmbed canvas should be non-traversable. |
|
76 |
// FIXME: Probably should be removed and enforced setting of it by the users |
|
77 |
target.setFocusTraversalKeysEnabled(false); |
|
78 |
} |
|
79 |
||
80 |
protected void preInit(XCreateWindowParams params) { |
|
81 |
super.preInit(params); |
|
82 |
||
83 |
params.put(EVENT_MASK, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
84 |
XConstants.KeyPressMask | XConstants.KeyReleaseMask |
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
85 |
| XConstants.FocusChangeMask | XConstants.ButtonPressMask | XConstants.ButtonReleaseMask |
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
86 |
| XConstants.EnterWindowMask | XConstants.LeaveWindowMask | XConstants.PointerMotionMask |
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
87 |
| XConstants.ButtonMotionMask | XConstants.ExposureMask | XConstants.StructureNotifyMask | XConstants.SubstructureNotifyMask); |
2 | 88 |
|
89 |
} |
|
90 |
||
91 |
void installModalityListener() { |
|
92 |
((SunToolkit)Toolkit.getDefaultToolkit()).addModalityListener(this); |
|
93 |
} |
|
94 |
||
95 |
void deinstallModalityListener() { |
|
96 |
((SunToolkit)Toolkit.getDefaultToolkit()).removeModalityListener(this); |
|
97 |
} |
|
98 |
||
99 |
void installAcceleratorListener() { |
|
100 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(this); |
|
101 |
} |
|
102 |
||
103 |
void deinstallAcceleratorListener() { |
|
104 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor(this); |
|
105 |
} |
|
106 |
||
107 |
void installActivateListener() { |
|
108 |
// FIXME: should watch for hierarchy changes |
|
109 |
Window toplevel = getTopLevel(target); |
|
110 |
if (toplevel != null) { |
|
111 |
toplevel.addWindowFocusListener(this); |
|
112 |
applicationActive = toplevel.isFocused(); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
void deinstallActivateListener() { |
|
117 |
Window toplevel = getTopLevel(target); |
|
118 |
if (toplevel != null) { |
|
119 |
toplevel.removeWindowFocusListener(this); |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
boolean isXEmbedActive() { |
|
124 |
return xembed.handle != 0; |
|
125 |
} |
|
126 |
||
127 |
boolean isApplicationActive() { |
|
128 |
return applicationActive; |
|
129 |
} |
|
130 |
||
131 |
void initDispatching() { |
|
132 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Init embedding for " + Long.toHexString(xembed.handle)); |
|
133 |
XToolkit.awtLock(); |
|
134 |
try { |
|
135 |
XToolkit.addEventDispatcher(xembed.handle, xembed); |
|
136 |
XlibWrapper.XSelectInput(XToolkit.getDisplay(), xembed.handle, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
137 |
XConstants.StructureNotifyMask | XConstants.PropertyChangeMask); |
2 | 138 |
|
139 |
XDropTargetRegistry.getRegistry().registerXEmbedClient(getWindow(), xembed.handle); |
|
140 |
} finally { |
|
141 |
XToolkit.awtUnlock(); |
|
142 |
} |
|
143 |
xembed.processXEmbedInfo(); |
|
144 |
||
145 |
notifyChildEmbedded(); |
|
146 |
} |
|
147 |
||
148 |
void endDispatching() { |
|
149 |
xembedLog.fine("End dispatching for " + Long.toHexString(xembed.handle)); |
|
150 |
XToolkit.awtLock(); |
|
151 |
try { |
|
152 |
XDropTargetRegistry.getRegistry().unregisterXEmbedClient(getWindow(), xembed.handle); |
|
153 |
// We can't deselect input since someone else might be interested in it |
|
154 |
XToolkit.removeEventDispatcher(xembed.handle, xembed); |
|
155 |
} finally { |
|
156 |
XToolkit.awtUnlock(); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
void embedChild(long child) { |
|
161 |
if (xembed.handle != 0) { |
|
162 |
detachChild(); |
|
163 |
} |
|
164 |
xembed.handle = child; |
|
165 |
initDispatching(); |
|
166 |
} |
|
167 |
||
168 |
void childDestroyed() { |
|
169 |
xembedLog.fine("Child " + Long.toHexString(xembed.handle) + " has self-destroyed."); |
|
170 |
endDispatching(); |
|
171 |
xembed.handle = 0; |
|
172 |
} |
|
173 |
||
174 |
public void handleEvent(AWTEvent e) { |
|
175 |
super.handleEvent(e); |
|
176 |
if (isXEmbedActive()) { |
|
177 |
switch (e.getID()) { |
|
178 |
case FocusEvent.FOCUS_GAINED: |
|
179 |
canvasFocusGained((FocusEvent)e); |
|
180 |
break; |
|
181 |
case FocusEvent.FOCUS_LOST: |
|
182 |
canvasFocusLost((FocusEvent)e); |
|
183 |
break; |
|
184 |
case KeyEvent.KEY_PRESSED: |
|
185 |
case KeyEvent.KEY_RELEASED: |
|
186 |
if (!((InputEvent)e).isConsumed()) { |
|
187 |
forwardKeyEvent((KeyEvent)e); |
|
188 |
} |
|
189 |
break; |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
public void dispatchEvent(XEvent ev) { |
|
195 |
super.dispatchEvent(ev); |
|
196 |
switch (ev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
197 |
case XConstants.CreateNotify: |
2 | 198 |
XCreateWindowEvent cr = ev.get_xcreatewindow(); |
199 |
if (xembedLog.isLoggable(Level.FINEST)) { |
|
200 |
xembedLog.finest("Message on embedder: " + cr); |
|
201 |
} |
|
202 |
if (xembedLog.isLoggable(Level.FINER)) { |
|
203 |
xembedLog.finer("Create notify for parent " + Long.toHexString(cr.get_parent()) + |
|
204 |
", window " + Long.toHexString(cr.get_window())); |
|
205 |
} |
|
206 |
embedChild(cr.get_window()); |
|
207 |
break; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
208 |
case XConstants.DestroyNotify: |
2 | 209 |
XDestroyWindowEvent dn = ev.get_xdestroywindow(); |
210 |
if (xembedLog.isLoggable(Level.FINEST)) { |
|
211 |
xembedLog.finest("Message on embedder: " + dn); |
|
212 |
} |
|
213 |
if (xembedLog.isLoggable(Level.FINER)) { |
|
214 |
xembedLog.finer("Destroy notify for parent: " + dn); |
|
215 |
} |
|
216 |
childDestroyed(); |
|
217 |
break; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
218 |
case XConstants.ReparentNotify: |
2 | 219 |
XReparentEvent rep = ev.get_xreparent(); |
220 |
if (xembedLog.isLoggable(Level.FINEST)) { |
|
221 |
xembedLog.finest("Message on embedder: " + rep); |
|
222 |
} |
|
223 |
if (xembedLog.isLoggable(Level.FINER)) { |
|
224 |
xembedLog.finer("Reparent notify for parent " + Long.toHexString(rep.get_parent()) + |
|
225 |
", window " + Long.toHexString(rep.get_window()) + |
|
226 |
", event " + Long.toHexString(rep.get_event())); |
|
227 |
} |
|
228 |
if (rep.get_parent() == getWindow()) { |
|
229 |
// Reparented into us - embed it |
|
230 |
embedChild(rep.get_window()); |
|
231 |
} else { |
|
232 |
// Reparented out of us - detach it |
|
233 |
childDestroyed(); |
|
234 |
} |
|
235 |
break; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
public Dimension getPreferredSize() { |
|
240 |
if (isXEmbedActive()) { |
|
241 |
XToolkit.awtLock(); |
|
242 |
try { |
|
243 |
long p_hints = XlibWrapper.XAllocSizeHints(); |
|
244 |
XSizeHints hints = new XSizeHints(p_hints); |
|
245 |
XlibWrapper.XGetWMNormalHints(XToolkit.getDisplay(), xembed.handle, p_hints, XlibWrapper.larg1); |
|
246 |
Dimension res = new Dimension(hints.get_width(), hints.get_height()); |
|
247 |
XlibWrapper.XFree(p_hints); |
|
248 |
return res; |
|
249 |
} finally { |
|
250 |
XToolkit.awtUnlock(); |
|
251 |
} |
|
252 |
} else { |
|
253 |
return super.getPreferredSize(); |
|
254 |
} |
|
255 |
} |
|
256 |
public Dimension getMinimumSize() { |
|
257 |
if (isXEmbedActive()) { |
|
258 |
XToolkit.awtLock(); |
|
259 |
try { |
|
260 |
long p_hints = XlibWrapper.XAllocSizeHints(); |
|
261 |
XSizeHints hints = new XSizeHints(p_hints); |
|
262 |
XlibWrapper.XGetWMNormalHints(XToolkit.getDisplay(), xembed.handle, p_hints, XlibWrapper.larg1); |
|
263 |
Dimension res = new Dimension(hints.get_min_width(), hints.get_min_height()); |
|
264 |
XlibWrapper.XFree(p_hints); |
|
265 |
return res; |
|
266 |
} finally { |
|
267 |
XToolkit.awtUnlock(); |
|
268 |
} |
|
269 |
} else { |
|
270 |
return super.getMinimumSize(); |
|
271 |
} |
|
272 |
} |
|
273 |
public void dispose() { |
|
274 |
if (isXEmbedActive()) { |
|
275 |
detachChild(); |
|
276 |
} |
|
277 |
deinstallActivateListener(); |
|
278 |
deinstallModalityListener(); |
|
279 |
deinstallAcceleratorListener(); |
|
280 |
||
281 |
// BUG: Focus traversal doesn't become enabled after the one round of embedding |
|
282 |
//target.setFocusTraversalKeysEnabled(true); |
|
283 |
||
284 |
super.dispose(); |
|
285 |
} |
|
286 |
||
287 |
// Focusable is true in order to enable focus traversal through this Canvas |
|
288 |
public boolean isFocusable() { |
|
289 |
return true; |
|
290 |
} |
|
291 |
||
292 |
Window getTopLevel(Component comp) { |
|
293 |
while (comp != null && !(comp instanceof Window)) { |
|
294 |
comp = comp.getParent(); |
|
295 |
} |
|
296 |
return (Window)comp; |
|
297 |
} |
|
298 |
||
299 |
Rectangle getClientBounds() { |
|
300 |
XToolkit.awtLock(); |
|
301 |
try { |
|
302 |
XWindowAttributes wattr = new XWindowAttributes(); |
|
303 |
try { |
|
304 |
XToolkit.WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler); |
|
305 |
int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), |
|
306 |
xembed.handle, wattr.pData); |
|
307 |
||
308 |
XToolkit.RESTORE_XERROR_HANDLER(); |
|
309 |
||
310 |
if (status == 0 || |
|
311 |
(XToolkit.saved_error != null && |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
312 |
XToolkit.saved_error.get_error_code() != XConstants.Success)) { |
2 | 313 |
return null; |
314 |
} |
|
315 |
||
316 |
return new Rectangle(wattr.get_x(), wattr.get_y(), wattr.get_width(), wattr.get_height()); |
|
317 |
} finally { |
|
318 |
wattr.dispose(); |
|
319 |
} |
|
320 |
} finally { |
|
321 |
XToolkit.awtUnlock(); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
void childResized() { |
|
326 |
if (xembedLog.isLoggable(Level.FINER)) { |
|
327 |
Rectangle bounds = getClientBounds(); |
|
328 |
xembedLog.finer("Child resized: " + bounds); |
|
329 |
// It is not required to update embedder's size when client size changes |
|
330 |
// However, since there is no any means to get client size it seems to be the |
|
331 |
// only way to provide it. However, it contradicts with Java layout concept - |
|
332 |
// so it is disabled for now. |
|
333 |
// Rectangle my_bounds = getBounds(); |
|
334 |
// setBounds(my_bounds.x, my_bounds.y, bounds.width, bounds.height, SET_BOUNDS); |
|
335 |
} |
|
336 |
XToolkit.postEvent(XToolkit.targetToAppContext(target), new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED)); |
|
337 |
} |
|
338 |
||
339 |
void focusNext() { |
|
340 |
if (isXEmbedActive()) { |
|
341 |
xembedLog.fine("Requesting focus for the next component after embedder"); |
|
342 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
343 |
public void run() { |
|
344 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(target); |
|
345 |
} |
|
346 |
})); |
|
347 |
} else { |
|
348 |
xembedLog.fine("XEmbed is not active - denying focus next"); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
void focusPrev() { |
|
353 |
if (isXEmbedActive()) { |
|
354 |
xembedLog.fine("Requesting focus for the next component after embedder"); |
|
355 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
356 |
public void run() { |
|
357 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent(target); |
|
358 |
} |
|
359 |
})); |
|
360 |
} else { |
|
361 |
xembedLog.fine("XEmbed is not active - denying focus prev"); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
void requestXEmbedFocus() { |
|
366 |
if (isXEmbedActive()) { |
|
367 |
xembedLog.fine("Requesting focus for client"); |
|
368 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
369 |
public void run() { |
|
370 |
target.requestFocus(); |
|
371 |
} |
|
372 |
})); |
|
373 |
} else { |
|
374 |
xembedLog.fine("XEmbed is not active - denying request focus"); |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
void notifyChildEmbedded() { |
|
379 |
xembed.sendMessage(xembed.handle, XEMBED_EMBEDDED_NOTIFY, getWindow(), Math.min(xembed.version, XEMBED_VERSION), 0); |
|
380 |
if (isApplicationActive()) { |
|
381 |
xembedLog.fine("Sending WINDOW_ACTIVATE during initialization"); |
|
382 |
xembed.sendMessage(xembed.handle, XEMBED_WINDOW_ACTIVATE); |
|
383 |
if (hasFocus()) { |
|
384 |
xembedLog.fine("Sending FOCUS_GAINED during initialization"); |
|
385 |
xembed.sendMessage(xembed.handle, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0); |
|
386 |
} |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
void detachChild() { |
|
391 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Detaching child " + Long.toHexString(xembed.handle)); |
|
392 |
/** |
|
393 |
* XEmbed specification: |
|
394 |
* "The embedder can unmap the client and reparent the client window to the root window. If the |
|
395 |
* client receives an ReparentNotify event, it should check the parent field of the XReparentEvent |
|
396 |
* structure. If this is the root window of the window's screen, then the protocol is finished and |
|
397 |
* there is no further interaction. If it is a window other than the root window, then the protocol |
|
398 |
* continues with the new parent acting as the embedder window." |
|
399 |
*/ |
|
400 |
XToolkit.awtLock(); |
|
401 |
try { |
|
402 |
XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), xembed.handle); |
|
403 |
XlibWrapper.XReparentWindow(XToolkit.getDisplay(), xembed.handle, XToolkit.getDefaultRootWindow(), 0, 0); |
|
404 |
} finally { |
|
405 |
XToolkit.awtUnlock(); |
|
406 |
} |
|
407 |
endDispatching(); |
|
408 |
xembed.handle = 0; |
|
409 |
} |
|
410 |
||
411 |
public void windowGainedFocus(WindowEvent e) { |
|
412 |
applicationActive = true; |
|
413 |
if (isXEmbedActive()) { |
|
414 |
xembedLog.fine("Sending WINDOW_ACTIVATE"); |
|
415 |
xembed.sendMessage(xembed.handle, XEMBED_WINDOW_ACTIVATE); |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
public void windowLostFocus(WindowEvent e) { |
|
420 |
applicationActive = false; |
|
421 |
if (isXEmbedActive()) { |
|
422 |
xembedLog.fine("Sending WINDOW_DEACTIVATE"); |
|
423 |
xembed.sendMessage(xembed.handle, XEMBED_WINDOW_DEACTIVATE); |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
void canvasFocusGained(FocusEvent e) { |
|
428 |
if (isXEmbedActive()) { |
|
429 |
xembedLog.fine("Forwarding FOCUS_GAINED"); |
|
430 |
int flavor = XEMBED_FOCUS_CURRENT; |
|
431 |
if (e instanceof CausedFocusEvent) { |
|
432 |
CausedFocusEvent ce = (CausedFocusEvent)e; |
|
433 |
if (ce.getCause() == CausedFocusEvent.Cause.TRAVERSAL_FORWARD) { |
|
434 |
flavor = XEMBED_FOCUS_FIRST; |
|
435 |
} else if (ce.getCause() == CausedFocusEvent.Cause.TRAVERSAL_BACKWARD) { |
|
436 |
flavor = XEMBED_FOCUS_LAST; |
|
437 |
} |
|
438 |
} |
|
439 |
xembed.sendMessage(xembed.handle, XEMBED_FOCUS_IN, flavor, 0, 0); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
void canvasFocusLost(FocusEvent e) { |
|
444 |
if (isXEmbedActive() && !e.isTemporary()) { |
|
445 |
xembedLog.fine("Forwarding FOCUS_LOST"); |
|
446 |
int num = 0; |
|
447 |
if (AccessController.doPrivileged(new GetBooleanAction("sun.awt.xembed.testing"))) { |
|
448 |
Component opp = e.getOppositeComponent(); |
|
449 |
try { |
|
450 |
num = Integer.parseInt(opp.getName()); |
|
451 |
} catch (NumberFormatException nfe) { |
|
452 |
} |
|
453 |
} |
|
454 |
xembed.sendMessage(xembed.handle, XEMBED_FOCUS_OUT, num, 0, 0); |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
static Field bdataField; |
|
459 |
static byte[] getBData(KeyEvent e) { |
|
460 |
try { |
|
461 |
if (bdataField == null) { |
|
462 |
bdataField = SunToolkit.getField(java.awt.AWTEvent.class, "bdata"); |
|
463 |
} |
|
464 |
return (byte[])bdataField.get(e); |
|
465 |
} catch (IllegalAccessException ex) { |
|
466 |
return null; |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
void forwardKeyEvent(KeyEvent e) { |
|
471 |
xembedLog.fine("Try to forward key event"); |
|
472 |
byte[] bdata = getBData(e); |
|
473 |
long data = Native.toData(bdata); |
|
474 |
if (data == 0) { |
|
475 |
return; |
|
476 |
} |
|
477 |
try { |
|
478 |
XKeyEvent ke = new XKeyEvent(data); |
|
479 |
ke.set_window(xembed.handle); |
|
480 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Forwarding native key event: " + ke); |
|
481 |
XToolkit.awtLock(); |
|
482 |
try { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
483 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(), xembed.handle, false, XConstants.NoEventMask, data); |
2 | 484 |
} finally { |
485 |
XToolkit.awtUnlock(); |
|
486 |
} |
|
487 |
} finally { |
|
488 |
XlibWrapper.unsafe.freeMemory(data); |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
||
493 |
/** |
|
494 |
* Grab/ungrab key functionality is an unofficial API supported by |
|
495 |
* GTK. Unfortunately, it doesn't support accelerator API, so, |
|
496 |
* since this is the ONLY shortcut-processing API available, we |
|
497 |
* must support it. See XEmbed.NON_STANDARD_XEMBED_GTK_* |
|
498 |
* messages. The format of these messages is as follows: |
|
499 |
* - request from client: |
|
500 |
* data[1] = NON_STANDARD_XEMBED_GTK_GRAB_KEY or NON_STANDARD_XEMBED_GTK_UNGRAB_KEY |
|
501 |
* data[3] = X keysym |
|
502 |
* data[4] = X modifiers |
|
503 |
* |
|
504 |
* - response from server (in case the grabbed key has been pressed): |
|
505 |
* forwarded XKeyEvent that matches keysym/modifiers pair |
|
506 |
*/ |
|
507 |
void grabKey(final long keysym, final long modifiers) { |
|
508 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
509 |
public void run() { |
|
510 |
GrabbedKey grab = new GrabbedKey(keysym, modifiers); |
|
511 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Grabbing key: " + grab); |
|
512 |
synchronized(GRAB_LOCK) { |
|
513 |
grabbed_keys.add(grab); |
|
514 |
} |
|
515 |
} |
|
516 |
})); |
|
517 |
} |
|
518 |
||
519 |
void ungrabKey(final long keysym, final long modifiers) { |
|
520 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
521 |
public void run() { |
|
522 |
GrabbedKey grab = new GrabbedKey(keysym, modifiers); |
|
523 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("UnGrabbing key: " + grab); |
|
524 |
synchronized(GRAB_LOCK) { |
|
525 |
grabbed_keys.remove(grab); |
|
526 |
} |
|
527 |
} |
|
528 |
})); |
|
529 |
} |
|
530 |
||
531 |
void registerAccelerator(final long accel_id, final long keysym, final long modifiers) { |
|
532 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
533 |
public void run() { |
|
534 |
AWTKeyStroke stroke = xembed.getKeyStrokeForKeySym(keysym, modifiers); |
|
535 |
if (stroke != null) { |
|
536 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Registering accelerator " + accel_id + " for " + stroke); |
|
537 |
synchronized(ACCEL_LOCK) { |
|
538 |
accelerators.put(accel_id, stroke); |
|
539 |
accel_lookup.put(stroke, accel_id); |
|
540 |
} |
|
541 |
} |
|
542 |
propogateRegisterAccelerator(stroke); |
|
543 |
} |
|
544 |
})); |
|
545 |
} |
|
546 |
||
547 |
void unregisterAccelerator(final long accel_id) { |
|
548 |
postEvent(new InvocationEvent(target, new Runnable() { |
|
549 |
public void run() { |
|
550 |
AWTKeyStroke stroke = null; |
|
551 |
synchronized(ACCEL_LOCK) { |
|
552 |
stroke = accelerators.get(accel_id); |
|
553 |
if (stroke != null) { |
|
554 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Unregistering accelerator: " + accel_id); |
|
555 |
accelerators.remove(accel_id); |
|
556 |
accel_lookup.remove(stroke); // FIXME: How about several accelerators with the same stroke? |
|
557 |
} |
|
558 |
} |
|
559 |
propogateUnRegisterAccelerator(stroke); |
|
560 |
} |
|
561 |
})); |
|
562 |
} |
|
563 |
||
564 |
void propogateRegisterAccelerator(AWTKeyStroke stroke) { |
|
565 |
// Find the top-level and see if it is XEmbed client. If so, ask him to |
|
566 |
// register the accelerator |
|
567 |
XWindowPeer parent = getToplevelXWindow(); |
|
568 |
if (parent != null && parent instanceof XEmbeddedFramePeer) { |
|
569 |
XEmbeddedFramePeer embedded = (XEmbeddedFramePeer)parent; |
|
570 |
embedded.registerAccelerator(stroke); |
|
571 |
} |
|
572 |
} |
|
573 |
||
574 |
void propogateUnRegisterAccelerator(AWTKeyStroke stroke) { |
|
575 |
// Find the top-level and see if it is XEmbed client. If so, ask him to |
|
576 |
// register the accelerator |
|
577 |
XWindowPeer parent = getToplevelXWindow(); |
|
578 |
if (parent != null && parent instanceof XEmbeddedFramePeer) { |
|
579 |
XEmbeddedFramePeer embedded = (XEmbeddedFramePeer)parent; |
|
580 |
embedded.unregisterAccelerator(stroke); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
public boolean postProcessKeyEvent(KeyEvent e) { |
|
585 |
// Processing events only if we are in the focused window but |
|
586 |
// we are not focus owner since otherwise we will get |
|
587 |
// duplicate shortcut events in the client - one is from |
|
588 |
// activate_accelerator, another from forwarded event |
|
589 |
// FIXME: This is probably an incompatibility, protocol |
|
590 |
// doesn't say anything about disable accelerators when client |
|
591 |
// is focused. |
|
592 |
||
593 |
XWindowPeer parent = getToplevelXWindow(); |
|
594 |
if (parent == null || !((Window)parent.getTarget()).isFocused() || target.isFocusOwner()) { |
|
595 |
return false; |
|
596 |
} |
|
597 |
||
598 |
boolean result = false; |
|
599 |
||
600 |
if (xembedLog.isLoggable(Level.FINER)) xembedLog.finer("Post-processing event " + e); |
|
601 |
||
602 |
// Process ACCELERATORS |
|
603 |
AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e); |
|
604 |
long accel_id = 0; |
|
605 |
boolean exists = false; |
|
606 |
synchronized(ACCEL_LOCK) { |
|
607 |
exists = accel_lookup.containsKey(stroke); |
|
608 |
if (exists) { |
|
609 |
accel_id = accel_lookup.get(stroke).longValue(); |
|
610 |
} |
|
611 |
} |
|
612 |
if (exists) { |
|
613 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Activating accelerator " + accel_id); |
|
614 |
xembed.sendMessage(xembed.handle, XEMBED_ACTIVATE_ACCELERATOR, accel_id, 0, 0); // FIXME: How about overloaded? |
|
615 |
result = true; |
|
616 |
} |
|
617 |
||
618 |
// Process Grabs, unofficial GTK feature |
|
619 |
exists = false; |
|
620 |
GrabbedKey key = new GrabbedKey(e); |
|
621 |
synchronized(GRAB_LOCK) { |
|
622 |
exists = grabbed_keys.contains(key); |
|
623 |
} |
|
624 |
if (exists) { |
|
625 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Forwarding grabbed key " + e); |
|
626 |
forwardKeyEvent(e); |
|
627 |
result = true; |
|
628 |
} |
|
629 |
||
630 |
return result; |
|
631 |
} |
|
632 |
||
633 |
public void modalityPushed(ModalityEvent ev) { |
|
634 |
xembed.sendMessage(xembed.handle, XEMBED_MODALITY_ON); |
|
635 |
} |
|
636 |
||
637 |
public void modalityPopped(ModalityEvent ev) { |
|
638 |
xembed.sendMessage(xembed.handle, XEMBED_MODALITY_OFF); |
|
639 |
} |
|
640 |
||
641 |
public void handleClientMessage(XEvent xev) { |
|
642 |
super.handleClientMessage(xev); |
|
643 |
XClientMessageEvent msg = xev.get_xclient(); |
|
644 |
if (xembedLog.isLoggable(Level.FINER)) xembedLog.finer("Client message to embedder: " + msg); |
|
645 |
if (msg.get_message_type() == xembed.XEmbed.getAtom()) { |
|
646 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine(xembed.XEmbedMessageToString(msg)); |
|
647 |
} |
|
648 |
if (isXEmbedActive()) { |
|
649 |
switch ((int)msg.get_data(1)) { |
|
650 |
case XEMBED_REQUEST_FOCUS: |
|
651 |
requestXEmbedFocus(); |
|
652 |
break; |
|
653 |
case XEMBED_FOCUS_NEXT: |
|
654 |
focusNext(); |
|
655 |
break; |
|
656 |
case XEMBED_FOCUS_PREV: |
|
657 |
focusPrev(); |
|
658 |
break; |
|
659 |
case XEMBED_REGISTER_ACCELERATOR: |
|
660 |
registerAccelerator(msg.get_data(2), msg.get_data(3), msg.get_data(4)); |
|
661 |
break; |
|
662 |
case XEMBED_UNREGISTER_ACCELERATOR: |
|
663 |
unregisterAccelerator(msg.get_data(2)); |
|
664 |
break; |
|
665 |
case NON_STANDARD_XEMBED_GTK_GRAB_KEY: |
|
666 |
grabKey(msg.get_data(3), msg.get_data(4)); |
|
667 |
break; |
|
668 |
case NON_STANDARD_XEMBED_GTK_UNGRAB_KEY: |
|
669 |
ungrabKey(msg.get_data(3), msg.get_data(4)); |
|
670 |
break; |
|
671 |
} |
|
672 |
} else { |
|
673 |
xembedLog.finer("But XEmbed is not Active!"); |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
private static class XEmbedDropTarget extends DropTarget { |
|
678 |
public void addDropTargetListener(DropTargetListener dtl) |
|
679 |
throws TooManyListenersException { |
|
680 |
// Drop target listeners registered with this target will never be |
|
681 |
// notified, since all drag notifications are routed to the XEmbed |
|
682 |
// client. To avoid confusion we prohibit listeners registration |
|
683 |
// by throwing TooManyListenersException as if there is a listener |
|
684 |
// registered with this target already. |
|
685 |
throw new TooManyListenersException(); |
|
686 |
} |
|
687 |
} |
|
688 |
||
689 |
public void setXEmbedDropTarget() { |
|
690 |
// Register a drop site on the top level. |
|
691 |
Runnable r = new Runnable() { |
|
692 |
public void run() { |
|
693 |
target.setDropTarget(new XEmbedDropTarget()); |
|
694 |
} |
|
695 |
}; |
|
696 |
SunToolkit.executeOnEventHandlerThread(target, r); |
|
697 |
} |
|
698 |
||
699 |
public void removeXEmbedDropTarget() { |
|
700 |
// Unregister a drop site on the top level. |
|
701 |
Runnable r = new Runnable() { |
|
702 |
public void run() { |
|
703 |
if (target.getDropTarget() instanceof XEmbedDropTarget) { |
|
704 |
target.setDropTarget(null); |
|
705 |
} |
|
706 |
} |
|
707 |
}; |
|
708 |
SunToolkit.executeOnEventHandlerThread(target, r); |
|
709 |
} |
|
710 |
||
711 |
public boolean processXEmbedDnDEvent(long ctxt, int eventID) { |
|
712 |
if (xembedLog.isLoggable(Level.FINEST)) { |
|
713 |
xembedLog.finest(" Drop target=" + target.getDropTarget()); |
|
714 |
} |
|
715 |
if (target.getDropTarget() instanceof XEmbedDropTarget) { |
|
716 |
AppContext appContext = XToolkit.targetToAppContext(getTarget()); |
|
717 |
XDropTargetContextPeer peer = |
|
718 |
XDropTargetContextPeer.getPeer(appContext); |
|
719 |
peer.forwardEventToEmbedded(xembed.handle, ctxt, eventID); |
|
720 |
return true; |
|
721 |
} else { |
|
722 |
return false; |
|
723 |
} |
|
724 |
} |
|
725 |
||
726 |
class XEmbedServer extends XEmbedHelper implements XEventDispatcher { |
|
727 |
long handle; // Handle to XEmbed client |
|
728 |
long version; |
|
729 |
long flags; |
|
730 |
||
731 |
boolean processXEmbedInfo() { |
|
732 |
long xembed_info_data = Native.allocateLongArray(2); |
|
733 |
try { |
|
734 |
if (!XEmbedInfo.getAtomData(handle, xembed_info_data, 2)) { |
|
735 |
// No more XEMBED_INFO? This is not XEmbed client! |
|
736 |
// Unfortunately this is the initial state of the most clients |
|
737 |
// FIXME: add 5-state processing |
|
738 |
//childDestroyed(); |
|
739 |
xembedLog.finer("Unable to get XEMBED_INFO atom data"); |
|
740 |
return false; |
|
741 |
} |
|
742 |
version = Native.getCard32(xembed_info_data, 0); |
|
743 |
flags = Native.getCard32(xembed_info_data, 1); |
|
744 |
boolean new_mapped = (flags & XEMBED_MAPPED) != 0; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
745 |
boolean currently_mapped = XlibUtil.getWindowMapState(handle) != XConstants.IsUnmapped; |
2 | 746 |
if (new_mapped != currently_mapped) { |
747 |
if (xembedLog.isLoggable(Level.FINER)) |
|
748 |
xembedLog.fine("Mapping state of the client has changed, old state: " + currently_mapped + ", new state: " + new_mapped); |
|
749 |
if (new_mapped) { |
|
750 |
XToolkit.awtLock(); |
|
751 |
try { |
|
752 |
XlibWrapper.XMapWindow(XToolkit.getDisplay(), handle); |
|
753 |
} finally { |
|
754 |
XToolkit.awtUnlock(); |
|
755 |
} |
|
756 |
} else { |
|
757 |
XToolkit.awtLock(); |
|
758 |
try { |
|
759 |
XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), handle); |
|
760 |
} finally { |
|
761 |
XToolkit.awtUnlock(); |
|
762 |
} |
|
763 |
} |
|
764 |
} else { |
|
765 |
xembedLog.finer("Mapping state didn't change, mapped: " + currently_mapped); |
|
766 |
} |
|
767 |
return true; |
|
768 |
} finally { |
|
769 |
XlibWrapper.unsafe.freeMemory(xembed_info_data); |
|
770 |
} |
|
771 |
} |
|
772 |
||
773 |
public void handlePropertyNotify(XEvent xev) { |
|
774 |
if (isXEmbedActive()) { |
|
775 |
XPropertyEvent ev = xev.get_xproperty(); |
|
776 |
if (xembedLog.isLoggable(Level.FINER)) xembedLog.finer("Property change on client: " + ev); |
|
777 |
if (ev.get_atom() == XAtom.XA_WM_NORMAL_HINTS) { |
|
778 |
childResized(); |
|
779 |
} else if (ev.get_atom() == XEmbedInfo.getAtom()) { |
|
780 |
processXEmbedInfo(); |
|
781 |
} else if (ev.get_atom() == |
|
782 |
XDnDConstants.XA_XdndAware.getAtom()) { |
|
783 |
XDropTargetRegistry.getRegistry().unregisterXEmbedClient(getWindow(), |
|
784 |
xembed.handle); |
|
785 |
if (ev.get_state() == XConstants.PropertyNewValue) { |
|
786 |
XDropTargetRegistry.getRegistry().registerXEmbedClient(getWindow(), |
|
787 |
xembed.handle); |
|
788 |
} |
|
789 |
} |
|
790 |
} else { |
|
791 |
xembedLog.finer("XEmbed is not active"); |
|
792 |
} |
|
793 |
} |
|
794 |
void handleConfigureNotify(XEvent xev) { |
|
795 |
if (isXEmbedActive()) { |
|
796 |
XConfigureEvent ev = xev.get_xconfigure(); |
|
797 |
if (xembedLog.isLoggable(Level.FINER)) xembedLog.finer("Bounds change on client: " + ev); |
|
798 |
if (xev.get_xany().get_window() == handle) { |
|
799 |
childResized(); |
|
800 |
} |
|
801 |
} |
|
802 |
} |
|
803 |
public void dispatchEvent(XEvent xev) { |
|
804 |
int type = xev.get_type(); |
|
805 |
switch (type) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
806 |
case XConstants.PropertyNotify: |
2 | 807 |
handlePropertyNotify(xev); |
808 |
break; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
809 |
case XConstants.ConfigureNotify: |
2 | 810 |
handleConfigureNotify(xev); |
811 |
break; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
812 |
case XConstants.ClientMessage: |
2 | 813 |
handleClientMessage(xev); |
814 |
break; |
|
815 |
} |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
static class GrabbedKey { |
|
820 |
long keysym; |
|
821 |
long modifiers; |
|
822 |
GrabbedKey(long keysym, long modifiers) { |
|
823 |
this.keysym = keysym; |
|
824 |
this.modifiers = modifiers; |
|
825 |
} |
|
826 |
||
827 |
GrabbedKey(KeyEvent ev) { |
|
828 |
init(ev); |
|
829 |
} |
|
830 |
||
831 |
private void init(KeyEvent e) { |
|
832 |
byte[] bdata = getBData(e); |
|
833 |
long data = Native.toData(bdata); |
|
834 |
if (data == 0) { |
|
835 |
return; |
|
836 |
} |
|
837 |
try { |
|
838 |
XToolkit.awtLock(); |
|
839 |
try { |
|
840 |
keysym = XWindow.getKeySymForAWTKeyCode(e.getKeyCode()); |
|
841 |
} finally { |
|
842 |
XToolkit.awtUnlock(); |
|
843 |
} |
|
844 |
XKeyEvent ke = new XKeyEvent(data); |
|
845 |
||
846 |
// We recognize only these masks |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
847 |
modifiers = ke.get_state() & (XConstants.ShiftMask | XConstants.ControlMask | XConstants.LockMask); |
2 | 848 |
if (xembedLog.isLoggable(Level.FINEST)) xembedLog.finest("Mapped " + e + " to " + this); |
849 |
} finally { |
|
850 |
XlibWrapper.unsafe.freeMemory(data); |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
public int hashCode() { |
|
855 |
return (int)keysym & 0xFFFFFFFF; |
|
856 |
} |
|
857 |
||
858 |
public boolean equals(Object o) { |
|
859 |
if (!(o instanceof GrabbedKey)) { |
|
860 |
return false; |
|
861 |
} |
|
862 |
GrabbedKey key = (GrabbedKey)o; |
|
863 |
return (keysym == key.keysym && modifiers == key.modifiers); |
|
864 |
} |
|
865 |
||
866 |
public String toString() { |
|
867 |
return "Key combination[keysym=" + keysym + ", mods=" + modifiers + "]"; |
|
868 |
} |
|
869 |
} |
|
870 |
} |