001: /*
002: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.*;
029: import java.util.HashMap;
030: import java.awt.event.KeyEvent;
031: import java.lang.reflect.*;
032: import sun.awt.SunToolkit;
033:
034: public class XEmbeddingContainer extends XEmbedHelper implements
035: XEventDispatcher {
036: HashMap children = new HashMap();
037:
038: XEmbeddingContainer() {
039: }
040:
041: XWindow embedder;
042:
043: void install(XWindow embedder) {
044: this .embedder = embedder;
045: XToolkit.addEventDispatcher(embedder.getWindow(), this );
046: }
047:
048: void deinstall() {
049: XToolkit.removeEventDispatcher(embedder.getWindow(), this );
050: }
051:
052: void add(long child) {
053: if (checkXEmbed(child)) {
054: Component proxy = createChildProxy(child);
055: ((Container) embedder.getTarget()).add("Center", proxy);
056: if (proxy.getPeer() != null) {
057: children.put(Long.valueOf(child), proxy.getPeer());
058: }
059: }
060: }
061:
062: Component createChildProxy(long child) {
063: return new XEmbedChildProxy(this , child);
064: }
065:
066: void notifyChildEmbedded(long child) {
067: sendMessage(child, XEMBED_EMBEDDED_NOTIFY,
068: embedder.getWindow(), XEMBED_VERSION, 0);
069: }
070:
071: void childResized(Component child) {
072: }
073:
074: boolean checkXEmbed(long child) {
075: long data = unsafe.allocateMemory(8);
076: try {
077: if (XEmbedInfo.getAtomData(child, data, 2)) {
078: int protocol = unsafe.getInt(data);
079: int flags = unsafe.getInt(data);
080: return true;
081: }
082: } finally {
083: unsafe.freeMemory(data);
084: }
085: return false;
086: }
087:
088: void detachChild(long child) {
089: // The embedder can unmap the client and reparent the client window
090: // to the root window. If the client receives an ReparentNotify
091: // event, it should check the parent field of the XReparentEvent
092: // structure. If this is the root window of the window's screen, then
093: // the protocol is finished and there is no further interaction. If
094: // it is a window other than the root window, then the protocol
095: // continues with the new parent acting as the embedder window.
096: XToolkit.awtLock();
097: try {
098: XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), child);
099: XlibWrapper.XReparentWindow(XToolkit.getDisplay(), child,
100: XToolkit.getDefaultRootWindow(), 0, 0);
101: } finally {
102: XToolkit.awtUnlock();
103: }
104: }
105:
106: void focusGained(long child) {
107: sendMessage(child, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
108: }
109:
110: void focusLost(long child) {
111: sendMessage(child, XEMBED_FOCUS_OUT);
112: }
113:
114: XEmbedChildProxyPeer getChild(long child) {
115: return (XEmbedChildProxyPeer) children.get(Long.valueOf(child));
116: }
117:
118: public void handleClientMessage(XEvent xev) {
119: XClientMessageEvent msg = xev.get_xclient();
120: if (msg.get_message_type() == XEmbed.getAtom()) {
121: switch ((int) msg.get_data(1)) {
122: case XEMBED_REQUEST_FOCUS:
123: long child = msg.get_data(2); // Unspecified
124: getChild(child).requestXEmbedFocus();
125: break;
126: }
127: }
128: }
129:
130: public void dispatchEvent(XEvent xev) {
131: switch (xev.get_type()) {
132: case XlibWrapper.ClientMessage:
133: handleClientMessage(xev);
134: break;
135: }
136: }
137:
138: static Field bdata;
139:
140: byte[] getBData(KeyEvent e) {
141: try {
142: if (bdata == null) {
143: bdata = SunToolkit.getField(java.awt.AWTEvent.class,
144: "bdata");
145: }
146: return (byte[]) bdata.get(e);
147: } catch (IllegalAccessException ex) {
148: return null;
149: }
150: }
151:
152: void forwardKeyEvent(long child, KeyEvent e) {
153: byte[] bdata = getBData(e);
154: long data = Native.toData(bdata);
155: if (data == 0) {
156: return;
157: }
158: XKeyEvent ke = new XKeyEvent(data);
159: ke.set_window(child);
160: XToolkit.awtLock();
161: try {
162: XlibWrapper.XSendEvent(XToolkit.getDisplay(), child, false,
163: XlibWrapper.NoEventMask, data);
164: } finally {
165: XToolkit.awtUnlock();
166: }
167: XlibWrapper.unsafe.freeMemory(data);
168: }
169: }
|