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