jdk/src/solaris/classes/sun/awt/X11/XEmbeddingContainer.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 439 3488710b02f8
child 5506 202f599c92aa
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class XEmbeddingContainer extends XEmbedHelper implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    HashMap children = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    XEmbeddingContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    XWindow embedder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    void install(XWindow embedder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.embedder = embedder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        XToolkit.addEventDispatcher(embedder.getWindow(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    void deinstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        XToolkit.removeEventDispatcher(embedder.getWindow(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void add(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (checkXEmbed(child)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Component proxy = createChildProxy(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            ((Container)embedder.getTarget()).add("Center", proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if (proxy.getPeer() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                children.put(Long.valueOf(child), proxy.getPeer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    Component createChildProxy(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return new XEmbedChildProxy(this, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    void notifyChildEmbedded(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        sendMessage(child, XEMBED_EMBEDDED_NOTIFY, embedder.getWindow(), XEMBED_VERSION, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    void childResized(Component child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    boolean checkXEmbed(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        long data = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (XEmbedInfo.getAtomData(child, data, 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                int protocol = unsafe.getInt(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                int flags = unsafe.getInt(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            unsafe.freeMemory(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    void detachChild(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // The embedder can unmap the client and reparent the client window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // to the root window. If the client receives an ReparentNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // event, it should check the parent field of the XReparentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // structure. If this is the root window of the window's screen, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // the protocol is finished and there is no further interaction. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // it is a window other than the root window, then the protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // continues with the new parent acting as the embedder window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            XlibWrapper.XReparentWindow(XToolkit.getDisplay(), child, XToolkit.getDefaultRootWindow(), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    void focusGained(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sendMessage(child, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    void focusLost(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        sendMessage(child, XEMBED_FOCUS_OUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    XEmbedChildProxyPeer getChild(long child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return (XEmbedChildProxyPeer)children.get(Long.valueOf(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void handleClientMessage(XEvent xev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        XClientMessageEvent msg = xev.get_xclient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (msg.get_message_type() == XEmbed.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            switch ((int)msg.get_data(1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
              case XEMBED_REQUEST_FOCUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                  long child = msg.get_data(2); // Unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                  getChild(child).requestXEmbedFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void dispatchEvent(XEvent xev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        switch(xev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   124
          case XConstants.ClientMessage:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
              handleClientMessage(xev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static Field bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    byte[] getBData(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (bdata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                bdata = SunToolkit.getField(java.awt.AWTEvent.class, "bdata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return (byte[])bdata.get(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void forwardKeyEvent(long child, KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        byte[] bdata = getBData(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        long data = Native.toData(bdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (data == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        XKeyEvent ke = new XKeyEvent(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ke.set_window(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   152
            XlibWrapper.XSendEvent(XToolkit.getDisplay(), child, false, XConstants.NoEventMask, data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        XlibWrapper.unsafe.freeMemory(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}