1 /*
   2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.X11;
  26 
  27 import java.awt.*;
  28 
  29 import java.awt.event.ComponentEvent;
  30 
  31 import sun.util.logging.PlatformLogger;
  32 
  33 import sun.awt.AWTAccessor;
  34 
  35 /**
  36  * This class implements window which serves as content window for decorated frames.
  37  * Its purpose to provide correct events dispatching for the complex
  38  * constructs such as decorated frames.
  39  *
  40  * It should always be located at (- left inset, - top inset) in the associated
  41  * decorated window.  So coordinates in it would be the same as java coordinates.
  42  */
  43 public final class XContentWindow extends XWindow {
  44     private static PlatformLogger insLog = PlatformLogger.getLogger("sun.awt.X11.insets.XContentWindow");
  45 
  46     static XContentWindow createContent(XDecoratedPeer parentFrame) {
  47         final WindowDimensions dims = parentFrame.getDimensions();
  48         Rectangle rec = dims.getBounds();
  49         // Fix for  - set the location of the content window to the (-left inset, -top inset)
  50         Insets ins = dims.getInsets();
  51         if (ins != null) {
  52             rec.x = -ins.left;
  53             rec.y = -ins.top;
  54         } else {
  55             rec.x = 0;
  56             rec.y = 0;
  57         }
  58         final XContentWindow cw = new XContentWindow(parentFrame, rec);
  59         cw.xSetVisible(true);
  60         return cw;
  61     }
  62 
  63     private final XDecoratedPeer parentFrame;
  64 
  65     // A list of expose events that come when the parentFrame is iconified
  66     private final java.util.List<SavedExposeEvent> iconifiedExposeEvents =
  67             new java.util.ArrayList<SavedExposeEvent>();
  68 
  69     private XContentWindow(XDecoratedPeer parentFrame, Rectangle bounds) {
  70         super((Component)parentFrame.getTarget(), parentFrame.getShell(), bounds);
  71         this.parentFrame = parentFrame;
  72     }
  73 
  74     void preInit(XCreateWindowParams params) {
  75         super.preInit(params);
  76         params.putIfNull(BIT_GRAVITY, Integer.valueOf(XConstants.NorthWestGravity));
  77         Long eventMask = (Long)params.get(EVENT_MASK);
  78         if (eventMask != null) {
  79             eventMask = eventMask & ~(XConstants.StructureNotifyMask);
  80             params.put(EVENT_MASK, eventMask);
  81         }
  82     }
  83 
  84     protected String getWMName() {
  85         return "Content window";
  86     }
  87     protected boolean isEventDisabled(XEvent e) {
  88         switch (e.get_type()) {
  89           // Override parentFrame to receive MouseEnter/Exit
  90           case XConstants.EnterNotify:
  91           case XConstants.LeaveNotify:
  92               return false;
  93           // We handle ConfigureNotify specifically in XDecoratedPeer
  94           case XConstants.ConfigureNotify:
  95               return true;
  96           // We don't want SHOWN/HIDDEN on content window since it will duplicate XDecoratedPeer
  97           case XConstants.MapNotify:
  98           case XConstants.UnmapNotify:
  99               return true;
 100           default:
 101               return super.isEventDisabled(e) || parentFrame.isEventDisabled(e);
 102         }
 103     }
 104 
 105     // Coordinates are that of the shell
 106     void setContentBounds(WindowDimensions dims) {
 107         XToolkit.awtLock();
 108         try {
 109             // Bounds of content window are of the same size as bounds of Java window and with
 110             // location as -(insets)
 111             Rectangle newBounds = dims.getBounds();
 112             Insets in = dims.getInsets();
 113             if (in != null) {
 114                 newBounds.setLocation(-in.left, -in.top);
 115             }
 116             if (insLog.isLoggable(PlatformLogger.FINE)) insLog.fine("Setting content bounds {0}, old bounds {1}",
 117                                                                     newBounds, getBounds());
 118             // Fix for 5023533:
 119             // Change in the size of the content window means, well, change of the size
 120             // Change in the location of the content window means change in insets
 121             boolean needHandleResize = !(newBounds.equals(getBounds()));
 122             reshape(newBounds);
 123             if (needHandleResize) {
 124                 insLog.fine("Sending RESIZED");
 125                 handleResize(newBounds);
 126             }
 127         } finally {
 128             XToolkit.awtUnlock();
 129         }
 130         validateSurface();
 131     }
 132 
 133     // NOTE: This method may be called by privileged threads.
 134     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 135     public void handleResize(Rectangle bounds) {
 136         AWTAccessor.getComponentAccessor().setSize((Component)target, bounds.width, bounds.height);
 137         postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED));
 138     }
 139 
 140 
 141     public void handleExposeEvent(Component target, int x, int y, int w, int h) {
 142         // TODO: ?
 143         // get rid of 'istanceof' by subclassing:
 144         // XContentWindow -> XFrameContentWindow
 145 
 146         // Expose event(s) that result from deiconification
 147         // come before a deicinofication notification.
 148         // We reorder these events by saving all expose events
 149         // that come when the frame is iconified. Then we
 150         // actually handle saved expose events on deiconification.
 151 
 152         if (parentFrame instanceof XFramePeer &&
 153                 (((XFramePeer)parentFrame).getState() & java.awt.Frame.ICONIFIED) != 0) {
 154             // Save expose events if the frame is iconified
 155             // in order to handle them on deiconification.
 156             iconifiedExposeEvents.add(new SavedExposeEvent(target, x, y, w, h));
 157         } else {
 158             // Normal case: [it is not a frame or] the frame is not iconified.
 159             super.handleExposeEvent(target, x, y, w, h);
 160         }
 161     }
 162 
 163     public void handleButtonPressRelease(XEvent xev) {
 164         if (xev.get_type() == XConstants.ButtonPress) {
 165             Window parentWindow = (Window)parentFrame.getTarget();
 166             /*
 167              * In case the decorated frame is active but not focused
 168              * (that is an owned window is currently focused)
 169              * it should be made a focused window.
 170              * This is needed to focus the frame when it's clicked
 171              * in an empty spot of its content area. See 6886678.
 172              */
 173             if (parentWindow != null && parentWindow.isActive() && !parentWindow.isFocused()) {
 174                 parentFrame.requestWindowFocus();
 175             }
 176         }
 177         super.handleButtonPressRelease(xev);
 178     }
 179 
 180     void purgeIconifiedExposeEvents() {
 181         for (SavedExposeEvent evt : iconifiedExposeEvents) {
 182             super.handleExposeEvent(evt.target, evt.x, evt.y, evt.w, evt.h);
 183         }
 184         iconifiedExposeEvents.clear();
 185     }
 186 
 187     private static class SavedExposeEvent {
 188         Component target;
 189         int x, y, w, h;
 190         SavedExposeEvent(Component target, int x, int y, int w, int h) {
 191             this.target = target;
 192             this.x = x;
 193             this.y = y;
 194             this.w = w;
 195             this.h = h;
 196         }
 197     }
 198 
 199     public String toString() {
 200         return getClass().getName() + "[" + getBounds() + "]";
 201     }
 202 }