0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/A
0N/Aimport java.util.LinkedList;
0N/Aimport java.util.Iterator;
0N/A
1696N/Aimport sun.util.logging.PlatformLogger;
0N/A
0N/Aimport sun.awt.EmbeddedFrame;
0N/Aimport sun.awt.SunToolkit;
0N/A
2765N/Aimport static sun.awt.X11.XConstants.*;
2765N/A
0N/Apublic class XEmbeddedFramePeer extends XFramePeer {
0N/A
1696N/A private static final PlatformLogger xembedLog = PlatformLogger.getLogger("sun.awt.X11.xembed.XEmbeddedFramePeer");
0N/A
0N/A LinkedList<AWTKeyStroke> strokes;
0N/A
0N/A XEmbedClientHelper embedder; // Caution - can be null if XEmbed is not supported
0N/A public XEmbeddedFramePeer(EmbeddedFrame target) {
0N/A // Don't specify PARENT_WINDOW param here. Instead we reparent
0N/A // this embedded frame peer to the proper parent window after
0N/A // an XEventDispatcher is registered to handle XEmbed events
0N/A super(new XCreateWindowParams(new Object[] {
0N/A TARGET, target,
0N/A VISIBLE, Boolean.TRUE,
0N/A EMBEDDED, Boolean.TRUE}));
0N/A }
0N/A
0N/A public void preInit(XCreateWindowParams params) {
0N/A super.preInit(params);
0N/A strokes = new LinkedList<AWTKeyStroke>();
0N/A if (supportsXEmbed()) {
0N/A embedder = new XEmbedClientHelper();
0N/A }
0N/A }
0N/A void postInit(XCreateWindowParams params) {
0N/A super.postInit(params);
0N/A if (embedder != null) {
882N/A // install X11 event dispatcher
882N/A embedder.setClient(this);
882N/A // reparent to XEmbed server
882N/A embedder.install();
0N/A } else if (getParentWindowHandle() != 0) {
0N/A XToolkit.awtLock();
0N/A try {
0N/A XlibWrapper.XReparentWindow(XToolkit.getDisplay(),
0N/A getWindow(),
0N/A getParentWindowHandle(),
0N/A 0, 0);
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A }
0N/A
882N/A @Override
882N/A public void dispose() {
882N/A if (embedder != null) {
882N/A // uninstall X11 event dispatcher
882N/A embedder.setClient(null);
882N/A }
882N/A super.dispose();
882N/A }
882N/A
0N/A public void updateMinimumSize() {
0N/A }
0N/A
0N/A protected String getWMName() {
0N/A return "JavaEmbeddedFrame";
0N/A }
0N/A
0N/A final long getParentWindowHandle() {
0N/A return ((XEmbeddedFrame)target).handle;
0N/A }
0N/A
0N/A boolean supportsXEmbed() {
0N/A return ((EmbeddedFrame)target).supportsXEmbed();
0N/A }
0N/A
0N/A public boolean requestWindowFocus(long time, boolean timeProvided) {
0N/A // Should check for active state of host application
0N/A if (embedder != null && embedder.isActive()) {
0N/A xembedLog.fine("Requesting focus from embedding host");
0N/A return embedder.requestFocus();
0N/A } else {
0N/A xembedLog.fine("Requesting focus from X");
0N/A return super.requestWindowFocus(time, timeProvided);
0N/A }
0N/A }
0N/A
0N/A protected void requestInitialFocus() {
0N/A if (embedder != null && supportsXEmbed()) {
0N/A embedder.requestFocus();
0N/A } else {
0N/A super.requestInitialFocus();
0N/A }
0N/A }
0N/A
0N/A protected boolean isEventDisabled(XEvent e) {
0N/A if (embedder != null && embedder.isActive()) {
0N/A switch (e.get_type()) {
216N/A case XConstants.FocusIn:
216N/A case XConstants.FocusOut:
0N/A return true;
0N/A }
0N/A }
0N/A return super.isEventDisabled(e);
0N/A }
0N/A
0N/A public void handleConfigureNotifyEvent(XEvent xev)
0N/A {
0N/A assert (SunToolkit.isAWTLockHeldByCurrentThread());
0N/A XConfigureEvent xe = xev.get_xconfigure();
1696N/A if (xembedLog.isLoggable(PlatformLogger.FINE)) {
0N/A xembedLog.fine(xe.toString());
0N/A }
0N/A
0N/A // fix for 5063031
0N/A // if we use super.handleConfigureNotifyEvent() we would get wrong
0N/A // size and position because embedded frame really is NOT a decorated one
0N/A checkIfOnNewScreen(toGlobal(new Rectangle(xe.get_x(),
0N/A xe.get_y(),
0N/A xe.get_width(),
0N/A xe.get_height())));
0N/A
0N/A Rectangle oldBounds = getBounds();
0N/A
0N/A synchronized (getStateLock()) {
0N/A x = xe.get_x();
0N/A y = xe.get_y();
0N/A width = xe.get_width();
0N/A height = xe.get_height();
0N/A
0N/A dimensions.setClientSize(width, height);
0N/A dimensions.setLocation(x, y);
0N/A }
0N/A
0N/A if (!getLocation().equals(oldBounds.getLocation())) {
0N/A handleMoved(dimensions);
0N/A }
0N/A reconfigureContentWindow(dimensions);
0N/A }
0N/A
0N/A protected void traverseOutForward() {
0N/A if (embedder != null && embedder.isActive()) {
0N/A if (embedder.isApplicationActive()) {
0N/A xembedLog.fine("Traversing out Forward");
0N/A embedder.traverseOutForward();
0N/A }
0N/A }
0N/A }
0N/A
0N/A protected void traverseOutBackward() {
0N/A if (embedder != null && embedder.isActive()) {
0N/A if (embedder.isApplicationActive()) {
0N/A xembedLog.fine("Traversing out Backward");
0N/A embedder.traverseOutBackward();
0N/A }
0N/A }
0N/A }
0N/A
0N/A // don't use getLocationOnScreen() inherited from XDecoratedPeer
0N/A public Point getLocationOnScreen() {
0N/A XToolkit.awtLock();
0N/A try {
0N/A return toGlobal(0, 0);
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A // don't use getBounds() inherited from XDecoratedPeer
0N/A public Rectangle getBounds() {
0N/A return new Rectangle(x, y, width, height);
0N/A }
0N/A
0N/A public void setBoundsPrivate(int x, int y, int width, int height) {
0N/A setBounds(x, y, width, height, SET_BOUNDS | NO_EMBEDDED_CHECK);
0N/A }
0N/A
0N/A public Rectangle getBoundsPrivate() {
0N/A int x = 0, y = 0;
0N/A int w = 0, h = 0;
0N/A XWindowAttributes attr = new XWindowAttributes();
0N/A
0N/A XToolkit.awtLock();
0N/A try {
0N/A XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
0N/A getWindow(), attr.pData);
0N/A x = attr.get_x();
0N/A y = attr.get_y();
0N/A w = attr.get_width();
0N/A h = attr.get_height();
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A attr.dispose();
0N/A
0N/A return new Rectangle(x, y, w, h);
0N/A }
0N/A void registerAccelerator(AWTKeyStroke stroke) {
0N/A if (stroke == null) return;
0N/A strokes.add(stroke);
0N/A if (embedder != null && embedder.isActive()) {
0N/A embedder.registerAccelerator(stroke, strokes.size()-1);
0N/A }
0N/A }
0N/A
0N/A void unregisterAccelerator(AWTKeyStroke stroke) {
0N/A if (stroke == null) return;
0N/A if (embedder != null && embedder.isActive()) {
0N/A int index = strokes.indexOf(stroke);
0N/A embedder.unregisterAccelerator(index);
0N/A }
0N/A }
0N/A
0N/A void notifyStarted() {
0N/A // Register accelerators
0N/A if (embedder != null && embedder.isActive()) {
0N/A int i = 0;
0N/A Iterator<AWTKeyStroke> iter = strokes.iterator();
0N/A while (iter.hasNext()) {
0N/A embedder.registerAccelerator(iter.next(), i++);
0N/A }
0N/A }
0N/A // Now we know that the the embedder is an XEmbed server, so we
0N/A // reregister the drop target to enable XDnD protocol support via
0N/A // XEmbed.
0N/A updateDropTarget();
0N/A }
882N/A void notifyStopped() {
882N/A if (embedder != null && embedder.isActive()) {
882N/A for (int i = strokes.size() - 1; i >= 0; i--) {
882N/A embedder.unregisterAccelerator(i);
882N/A }
882N/A }
882N/A }
882N/A
0N/A long getFocusTargetWindow() {
0N/A return getWindow();
0N/A }
0N/A
0N/A boolean isXEmbedActive() {
0N/A return embedder != null && embedder.isActive();
0N/A }
0N/A
0N/A public int getAbsoluteX()
0N/A {
0N/A Point absoluteLoc = XlibUtil.translateCoordinates(getWindow(),
0N/A XToolkit.getDefaultRootWindow(),
0N/A new Point(0, 0));
2447N/A return absoluteLoc != null ? absoluteLoc.x : 0;
0N/A }
0N/A
0N/A public int getAbsoluteY()
0N/A {
0N/A Point absoluteLoc = XlibUtil.translateCoordinates(getWindow(),
0N/A XToolkit.getDefaultRootWindow(),
0N/A new Point(0, 0));
2447N/A return absoluteLoc != null ? absoluteLoc.y : 0;
0N/A }
0N/A
0N/A public int getWidth() {
0N/A return width;
0N/A }
0N/A public int getHeight() {
0N/A return height;
0N/A }
0N/A
0N/A public Dimension getSize() {
0N/A return new Dimension(width, height);
0N/A }
0N/A
0N/A // override XWindowPeer's method to let the embedded frame to block
0N/A // the containing window
0N/A public void setModalBlocked(Dialog blocker, boolean blocked) {
0N/A super.setModalBlocked(blocker, blocked);
0N/A
0N/A EmbeddedFrame frame = (EmbeddedFrame)target;
0N/A frame.notifyModalBlocked(blocker, blocked);
0N/A }
2765N/A
2765N/A public void synthesizeFocusInOut(boolean doFocus) {
2765N/A XFocusChangeEvent xev = new XFocusChangeEvent();
2765N/A
2765N/A XToolkit.awtLock();
2765N/A try {
2765N/A xev.set_type(doFocus ? FocusIn : FocusOut);
2765N/A xev.set_window(getFocusProxy().getWindow());
2765N/A xev.set_mode(NotifyNormal);
2765N/A XlibWrapper.XSendEvent(XToolkit.getDisplay(), getFocusProxy().getWindow(), false,
2765N/A NoEventMask, xev.pData);
2765N/A } finally {
2765N/A XToolkit.awtUnlock();
2765N/A xev.dispose();
2765N/A }
2765N/A }
0N/A}