author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 439 | 3488710b02f8 |
child 1974 | 1e1487c04864 |
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.AWTKeyStroke; |
|
29 |
import java.util.logging.*; |
|
30 |
import sun.awt.SunToolkit; |
|
31 |
import java.awt.Component; |
|
32 |
import java.awt.Container; |
|
33 |
||
34 |
/** |
|
35 |
* Helper class implementing XEmbed protocol handling routines(client side) |
|
36 |
* Window which wants to participate in a protocol should create an instance, |
|
37 |
* call install and forward all XClientMessageEvents to it. |
|
38 |
*/ |
|
39 |
public class XEmbedClientHelper extends XEmbedHelper implements XEventDispatcher { |
|
40 |
private static final Logger xembedLog = Logger.getLogger("sun.awt.X11.xembed.XEmbedClientHelper"); |
|
41 |
||
42 |
private XEmbeddedFramePeer embedded; |
|
43 |
private boolean active; |
|
44 |
private long server; |
|
45 |
private boolean applicationActive; |
|
46 |
||
47 |
XEmbedClientHelper() { |
|
48 |
super(); |
|
49 |
} |
|
50 |
||
51 |
void install(XEmbeddedFramePeer embedded) { |
|
52 |
this.embedded = embedded; |
|
53 |
||
54 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Installing xembedder on " + embedded); |
|
55 |
XToolkit.addEventDispatcher(embedded.getWindow(), this); |
|
56 |
long[] info = new long[] { XEMBED_VERSION, XEMBED_MAPPED }; |
|
57 |
long data = Native.card32ToData(info); |
|
58 |
try { |
|
59 |
XEmbedInfo.setAtomData(embedded.getWindow(), data, 2); |
|
60 |
} finally { |
|
61 |
unsafe.freeMemory(data); |
|
62 |
} |
|
63 |
// XEmbeddedFrame is initially created with a null parent.. |
|
64 |
// Here it is reparented to the proper parent window. |
|
65 |
long parentWindow = embedded.getParentWindowHandle(); |
|
66 |
if (parentWindow != 0) { |
|
67 |
XToolkit.awtLock(); |
|
68 |
try { |
|
69 |
XlibWrapper.XReparentWindow(XToolkit.getDisplay(), |
|
70 |
embedded.getWindow(), |
|
71 |
parentWindow, |
|
72 |
0, 0); |
|
73 |
} finally { |
|
74 |
XToolkit.awtUnlock(); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
void handleClientMessage(XEvent xev) { |
|
80 |
XClientMessageEvent msg = xev.get_xclient(); |
|
81 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine(msg.toString()); |
|
82 |
if (msg.get_message_type() == XEmbed.getAtom()) { |
|
83 |
if (xembedLog.isLoggable(Level.FINE)) xembedLog.fine("Embedded message: " + msgidToString((int)msg.get_data(1))); |
|
84 |
switch ((int)msg.get_data(1)) { |
|
85 |
case XEMBED_EMBEDDED_NOTIFY: // Notification about embedding protocol start |
|
86 |
active = true; |
|
87 |
server = getEmbedder(embedded, msg); |
|
88 |
// Check if window is reparented. If not - it was created with |
|
89 |
// parent and so we should update it here. |
|
90 |
if (!embedded.isReparented()) { |
|
91 |
embedded.setReparented(true); |
|
92 |
embedded.updateSizeHints(); |
|
93 |
} |
|
94 |
embedded.notifyStarted(); |
|
95 |
break; |
|
96 |
case XEMBED_WINDOW_ACTIVATE: |
|
97 |
applicationActive = true; |
|
98 |
break; |
|
99 |
case XEMBED_WINDOW_DEACTIVATE: |
|
100 |
if (applicationActive) { |
|
101 |
applicationActive = false; |
|
102 |
handleWindowFocusOut(); |
|
103 |
} |
|
104 |
break; |
|
105 |
case XEMBED_FOCUS_IN: // We got focus! |
|
106 |
// Check for direction |
|
107 |
handleFocusIn((int)msg.get_data(2)); |
|
108 |
break; |
|
109 |
case XEMBED_FOCUS_OUT: |
|
110 |
if (applicationActive) { |
|
111 |
handleWindowFocusOut(); |
|
112 |
} |
|
113 |
break; |
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
void handleFocusIn(int detail) { |
|
118 |
if (embedded.focusAllowedFor()) { |
|
119 |
embedded.handleWindowFocusInSync(0); |
|
120 |
} |
|
121 |
switch(detail) { |
|
122 |
case XEMBED_FOCUS_CURRENT: |
|
123 |
// Do nothing - just restore to the current value |
|
124 |
break; |
|
125 |
case XEMBED_FOCUS_FIRST: |
|
126 |
SunToolkit.executeOnEventHandlerThread(embedded.target, new Runnable() { |
|
127 |
public void run() { |
|
128 |
Component comp = ((Container)embedded.target).getFocusTraversalPolicy().getFirstComponent((Container)embedded.target); |
|
129 |
if (comp != null) { |
|
130 |
comp.requestFocusInWindow(); |
|
131 |
} |
|
132 |
}}); |
|
133 |
break; |
|
134 |
case XEMBED_FOCUS_LAST: |
|
135 |
SunToolkit.executeOnEventHandlerThread(embedded.target, new Runnable() { |
|
136 |
public void run() { |
|
137 |
Component comp = ((Container)embedded.target).getFocusTraversalPolicy().getLastComponent((Container)embedded.target); |
|
138 |
if (comp != null) { |
|
139 |
comp.requestFocusInWindow(); |
|
140 |
} |
|
141 |
}}); |
|
142 |
break; |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
public void dispatchEvent(XEvent xev) { |
|
147 |
switch(xev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
148 |
case XConstants.ClientMessage: |
2 | 149 |
handleClientMessage(xev); |
150 |
break; |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
120
diff
changeset
|
151 |
case XConstants.ReparentNotify: |
2 | 152 |
handleReparentNotify(xev); |
153 |
break; |
|
154 |
} |
|
155 |
} |
|
156 |
public void handleReparentNotify(XEvent xev) { |
|
157 |
XReparentEvent re = xev.get_xreparent(); |
|
158 |
server = re.get_parent(); |
|
159 |
} |
|
160 |
boolean requestFocus() { |
|
161 |
if (active && embedded.focusAllowedFor()) { |
|
162 |
sendMessage(server, XEMBED_REQUEST_FOCUS); |
|
163 |
return true; |
|
164 |
} |
|
165 |
return false; |
|
166 |
} |
|
167 |
void handleWindowFocusOut() { |
|
168 |
// fix for 6269309: it is possible that we call this method twice |
|
169 |
// (for example, when receiving XEMBED_WINDOW_DEACTIVATE and then |
|
170 |
// XEMBED_FOCUS_OUT client messages), so we first need to check if |
|
171 |
// embedded is an active window before sending WINDOW_LOST_FOCUS |
|
172 |
// to shared code |
|
173 |
if (XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() == embedded.target) { |
|
174 |
embedded.handleWindowFocusOutSync(null, 0); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
long getEmbedder(XWindowPeer embedded, XClientMessageEvent info) { |
|
179 |
// Embedder is the parent of embedded. |
|
180 |
return XlibUtil.getParentWindow(embedded.getWindow()); |
|
181 |
} |
|
182 |
||
183 |
boolean isApplicationActive() { |
|
184 |
return applicationActive; |
|
185 |
} |
|
186 |
||
187 |
boolean isActive() { |
|
188 |
return active; |
|
189 |
} |
|
190 |
||
191 |
void traverseOutForward() { |
|
192 |
if (active) { |
|
193 |
sendMessage(server, XEMBED_FOCUS_NEXT); |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
void traverseOutBackward() { |
|
198 |
if (active) { |
|
199 |
sendMessage(server, XEMBED_FOCUS_PREV); |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
void registerAccelerator(AWTKeyStroke stroke, int id) { |
|
204 |
long sym = getX11KeySym(stroke); |
|
205 |
long mods = getX11Mods(stroke); |
|
206 |
sendMessage(server, XEMBED_REGISTER_ACCELERATOR, id, sym, mods); |
|
207 |
} |
|
208 |
void unregisterAccelerator(int id) { |
|
209 |
sendMessage(server, XEMBED_UNREGISTER_ACCELERATOR, id, 0, 0); |
|
210 |
} |
|
211 |
||
212 |
long getX11KeySym(AWTKeyStroke stroke) { |
|
213 |
XToolkit.awtLock(); |
|
214 |
try { |
|
215 |
return XWindow.getKeySymForAWTKeyCode(stroke.getKeyCode()); |
|
216 |
} finally { |
|
217 |
XToolkit.awtUnlock(); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
long getX11Mods(AWTKeyStroke stroke) { |
|
222 |
return XWindow.getXModifiers(stroke); |
|
223 |
} |
|
224 |
} |