1 /*
   2  * Copyright 2003-2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 package sun.awt.X11;
  26 
  27 import java.awt.Component;
  28 import java.awt.Rectangle;
  29 import java.awt.Insets;
  30 
  31 import java.awt.event.ComponentEvent;
  32 
  33 import java.util.logging.Level;
  34 import java.util.logging.Logger;
  35 
  36 import sun.awt.ComponentAccessor;
  37 
  38 /**
  39  * This class implements window which serves as content window for decorated frames.
  40  * Its purpose to provide correct events dispatching for the complex
  41  * constructs such as decorated frames.
  42  *
  43  * It should always be located at (- left inset, - top inset) in the associated
  44  * decorated window.  So coordinates in it would be the same as java coordinates.
  45  */
  46 public final class XContentWindow extends XWindow {
  47     private static Logger insLog = Logger.getLogger("sun.awt.X11.insets.XContentWindow");
  48 
  49     static XContentWindow createContent(XDecoratedPeer parentFrame) {
  50         final WindowDimensions dims = parentFrame.getDimensions();
  51         Rectangle rec = dims.getBounds();
  52         // Fix for  - set the location of the content window to the (-left inset, -top inset)
  53         Insets ins = dims.getInsets();
  54         if (ins != null) {
  55             rec.x = -ins.left;
  56             rec.y = -ins.top;
  57         } else {
  58             rec.x = 0;
  59             rec.y = 0;
  60         }
  61         final XContentWindow cw = new XContentWindow(parentFrame, rec);
  62         cw.xSetVisible(true);
  63         return cw;
  64     }
  65 
  66     private final XDecoratedPeer parentFrame;
  67 
  68     // A list of expose events that come when the parentFrame is iconified
  69     private final java.util.List<SavedExposeEvent> iconifiedExposeEvents =
  70             new java.util.ArrayList<SavedExposeEvent>();
  71 
  72     private XContentWindow(XDecoratedPeer parentFrame, Rectangle bounds) {
  73         super((Component)parentFrame.getTarget(), parentFrame.getShell(), bounds);
  74         this.parentFrame = parentFrame;
  75     }
  76 
  77     void preInit(XCreateWindowParams params) {
  78         super.preInit(params);
  79         params.putIfNull(BIT_GRAVITY, Integer.valueOf(XConstants.NorthWestGravity));
  80         Long eventMask = (Long)params.get(EVENT_MASK);
  81         if (eventMask != null) {
  82             eventMask = eventMask & ~(XConstants.StructureNotifyMask);
  83             params.put(EVENT_MASK, eventMask);
  84         }
  85     }
  86 
  87     protected String getWMName() {
  88         return "Content window";
  89     }
  90     protected boolean isEventDisabled(XEvent e) {
  91         switch (e.get_type()) {
  92           // Override parentFrame to receive MouseEnter/Exit
  93           case XConstants.EnterNotify:
  94           case XConstants.LeaveNotify:
  95               return false;
  96           // We handle ConfigureNotify specifically in XDecoratedPeer
  97           case XConstants.ConfigureNotify:
  98               return true;
  99           // We don't want SHOWN/HIDDEN on content window since it will duplicate XDecoratedPeer
 100           case XConstants.MapNotify:
 101           case XConstants.UnmapNotify:
 102               return true;
 103           default:
 104               return super.isEventDisabled(e) || parentFrame.isEventDisabled(e);
 105         }
 106     }
 107 
 108     /**
 109      * Sets content window bounds.
 110      *
 111      * @return whether a COMPONENT_RESIZED event has been sent
 112      */
 113     boolean setContentBounds(WindowDimensions dims) {
 114         XToolkit.awtLock();
 115         try {
 116             // Bounds of content window are of the same size as bounds of Java window and with
 117             // location as -(insets)
 118             Rectangle newBounds = dims.getBounds();
 119             Insets in = dims.getInsets();
 120             if (in != null) {
 121                 newBounds.setLocation(-in.left, -in.top);
 122             }
 123             if (insLog.isLoggable(Level.FINE)) insLog.log(Level.FINE, "Setting content bounds {0}, old bounds {1}",
 124                                                           new Object[] {newBounds, getBounds()});
 125             // Fix for 5023533:
 126             // Change in the size of the content window means, well, change of the size
 127             // Change in the location of the content window means change in insets
 128             boolean needHandleResize = !(newBounds.equals(getBounds()));
 129             reshape(newBounds);
 130             if (needHandleResize) {
 131                 insLog.fine("Sending RESIZED");
 132                 handleResize(newBounds);
 133             }
 134             validateSurface();
 135             return needHandleResize;
 136         } finally {
 137             XToolkit.awtUnlock();
 138         }
 139     }
 140 
 141     // NOTE: This method may be called by privileged threads.
 142     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 143     public void handleResize(Rectangle bounds) {
 144         ComponentAccessor.setWidth((Component)target, bounds.width);
 145         ComponentAccessor.setHeight((Component)target, bounds.height);
 146         postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED));
 147     }
 148 
 149 
 150     public void handleExposeEvent(Component target, int x, int y, int w, int h) {
 151         // TODO: ?
 152         // get rid of 'istanceof' by subclassing:
 153         // XContentWindow -> XFrameContentWindow
 154 
 155         // Expose event(s) that result from deiconification
 156         // come before a deicinofication notification.
 157         // We reorder these events by saving all expose events
 158         // that come when the frame is iconified. Then we
 159         // actually handle saved expose events on deiconification.
 160 
 161         if (parentFrame instanceof XFramePeer &&
 162                 (((XFramePeer)parentFrame).getState() & java.awt.Frame.ICONIFIED) != 0) {
 163             // Save expose events if the frame is iconified
 164             // in order to handle them on deiconification.
 165             iconifiedExposeEvents.add(new SavedExposeEvent(target, x, y, w, h));
 166         } else {
 167             // Normal case: [it is not a frame or] the frame is not iconified.
 168             super.handleExposeEvent(target, x, y, w, h);
 169         }
 170     }
 171 
 172     void purgeIconifiedExposeEvents() {
 173         for (SavedExposeEvent evt : iconifiedExposeEvents) {
 174             super.handleExposeEvent(evt.target, evt.x, evt.y, evt.w, evt.h);
 175         }
 176         iconifiedExposeEvents.clear();
 177     }
 178 
 179     private static class SavedExposeEvent {
 180         Component target;
 181         int x, y, w, h;
 182         SavedExposeEvent(Component target, int x, int y, int w, int h) {
 183             this.target = target;
 184             this.x = x;
 185             this.y = y;
 186             this.w = w;
 187             this.h = h;
 188         }
 189     }
 190 
 191     public String toString() {
 192         return getClass().getName() + "[" + getBounds() + "]";
 193     }
 194 }