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