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