1 /*
   2  * Copyright (c) 1996, 2010, 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 
  26 package sun.awt.windows;
  27 
  28 import sun.awt.*;
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 import java.awt.peer.ComponentPeer;
  32 import java.awt.image.*;
  33 import sun.awt.image.ByteInterleavedRaster;
  34 import sun.security.action.GetPropertyAction;
  35 
  36 public class WEmbeddedFrame extends EmbeddedFrame {
  37 
  38     static {
  39         initIDs();
  40     }
  41 
  42     private long handle;
  43 
  44     private int bandWidth = 0;
  45     private int bandHeight = 0;
  46     private int imgWid = 0;
  47     private int imgHgt = 0;
  48 
  49     private static int pScale = 0;
  50     private static final int MAX_BAND_SIZE = (1024*30);
  51 
  52     private static String printScale = (String) java.security.AccessController
  53        .doPrivileged(new GetPropertyAction("sun.java2d.print.pluginscalefactor"));
  54 
  55     public WEmbeddedFrame() {
  56         this((long)0);
  57     }
  58 
  59     /**
  60      * @deprecated This constructor will be removed in 1.5
  61      */
  62     @Deprecated
  63     public WEmbeddedFrame(int handle) {
  64         this((long)handle);
  65     }
  66 
  67     public WEmbeddedFrame(long handle) {
  68         this.handle = handle;
  69         if (handle != 0) {
  70             addNotify();
  71             show();
  72         }
  73     }
  74 
  75     public void addNotify() {
  76         if (getPeer() == null) {
  77             WToolkit toolkit = (WToolkit)Toolkit.getDefaultToolkit();
  78             setPeer(toolkit.createEmbeddedFrame(this));
  79         }
  80         super.addNotify();
  81     }
  82 
  83     /*
  84      * Get the native handle
  85     */
  86     public long getEmbedderHandle() {
  87         return handle;
  88     }
  89 
  90     /*
  91      * Print the embedded frame and its children using the specified HDC.
  92      */
  93 
  94     void print(long hdc) {
  95         BufferedImage bandImage = null;
  96 
  97         int xscale = 1;
  98         int yscale = 1;
  99 
 100         /* Is this is either a printer DC or an enhanced meta file DC ?
 101          * Mozilla passes in a printer DC, IE passes plug-in a DC for an
 102          * enhanced meta file. Its possible we may be passed to a memory
 103          * DC. If we here create a larger image, draw in to it and have
 104          * that memory DC then lose the image resolution only to scale it
 105          * back up again when sending to a printer it will look really bad.
 106          * So, is this is either a printer DC or an enhanced meta file DC ?
 107          * Scale only if it is. Use a 4x scale factor, partly since for
 108          * an enhanced meta file we don't know anything about the
 109          * real resolution of the destination.
 110          *
 111          * For a printer DC we could probably derive the scale factor to use
 112          * by querying LOGPIXELSX/Y, and dividing that by the screen
 113          * resolution (typically 96 dpi or 120 dpi) but that would typically
 114          * make for even bigger output for marginal extra quality.
 115          * But for enhanced meta file we don't know anything about the
 116          * real resolution of the destination so
 117          */
 118         if (isPrinterDC(hdc)) {
 119             xscale = yscale = getPrintScaleFactor();
 120         }
 121 
 122         int frameHeight = getHeight();
 123         if (bandImage == null) {
 124             bandWidth = getWidth();
 125             if (bandWidth % 4 != 0) {
 126                 bandWidth += (4 - (bandWidth % 4));
 127             }
 128             if (bandWidth <= 0) {
 129                 return;
 130             }
 131 
 132             bandHeight = Math.min(MAX_BAND_SIZE/bandWidth, frameHeight);
 133 
 134             imgWid = (int)(bandWidth * xscale);
 135             imgHgt = (int)(bandHeight * yscale);
 136             bandImage = new BufferedImage(imgWid, imgHgt,
 137                                           BufferedImage.TYPE_3BYTE_BGR);
 138         }
 139 
 140         Graphics clearGraphics = bandImage.getGraphics();
 141         clearGraphics.setColor(Color.white);
 142         Graphics2D g2d = (Graphics2D)bandImage.getGraphics();
 143         g2d.translate(0, imgHgt);
 144         g2d.scale(xscale, -yscale);
 145 
 146         ByteInterleavedRaster ras = (ByteInterleavedRaster)bandImage.getRaster();
 147         byte[] data = ras.getDataStorage();
 148 
 149         for (int bandTop = 0; bandTop < frameHeight; bandTop += bandHeight) {
 150             clearGraphics.fillRect(0, 0, bandWidth, bandHeight);
 151 
 152             printComponents(g2d);
 153             int imageOffset =0;
 154             int currBandHeight = bandHeight;
 155             int currImgHeight = imgHgt;
 156             if ((bandTop+bandHeight) > frameHeight) {
 157                 // last band
 158                 currBandHeight = frameHeight - bandTop;
 159                 currImgHeight = (int)(currBandHeight*yscale);
 160 
 161                 // multiply by 3 because the image is a 3 byte BGR
 162                 imageOffset = imgWid*(imgHgt-currImgHeight)*3;
 163             }
 164 
 165             printBand(hdc, data, imageOffset,
 166                       0, 0, imgWid, currImgHeight,
 167                       0, bandTop, bandWidth, currBandHeight);
 168             g2d.translate(0, -bandHeight);
 169         }
 170     }
 171 
 172     protected static int getPrintScaleFactor() {
 173         // check if value is already cached
 174         if (pScale != 0)
 175             return pScale;
 176         if (printScale == null) {
 177             // if no system property is specified,
 178             // check for environment setting
 179             printScale = (String) java.security.AccessController.doPrivileged(
 180                 new java.security.PrivilegedAction() {
 181                     public Object run() {
 182                         return System.getenv("JAVA2D_PLUGIN_PRINT_SCALE");
 183                     }
 184                 }
 185             );
 186         }
 187         int default_printDC_scale = 4;
 188         int scale = default_printDC_scale;
 189         if (printScale != null) {
 190             try {
 191                 scale = Integer.parseInt(printScale);
 192                 if (scale > 8 || scale < 1) {
 193                     scale = default_printDC_scale;
 194                 }
 195             } catch (NumberFormatException nfe) {
 196             }
 197         }
 198         pScale = scale;
 199         return pScale;
 200     }
 201 
 202 
 203     protected native boolean isPrinterDC(long hdc);
 204 
 205     protected native void printBand(long hdc, byte[] data, int offset,
 206                                     int sx, int sy, int swidth, int sheight,
 207                                     int dx, int dy, int dwidth, int dheight);
 208 
 209     /**
 210      * Initialize JNI field IDs
 211      */
 212     private static native void initIDs();
 213 
 214     /**
 215      * This method is called from the native code when this embedded
 216      * frame should be activated. It is expected to be overridden in
 217      * subclasses, for example, in plugin to activate the browser
 218      * window that contains this embedded frame.
 219      *
 220      * NOTE: This method may be called by privileged threads.
 221      *     DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 222      */
 223     public void activateEmbeddingTopLevel() {
 224     }
 225 
 226     public void synthesizeWindowActivation(final boolean doActivate) {
 227         if (!doActivate || EventQueue.isDispatchThread()) {
 228             ((WEmbeddedFramePeer)getPeer()).synthesizeWmActivate(doActivate);
 229         } else {
 230             // To avoid focus concurrence b/w IE and EmbeddedFrame
 231             // activation is postponed by means of posting it to EDT.
 232             Runnable r = new Runnable() {
 233                 public void run() {
 234                     ((WEmbeddedFramePeer)getPeer()).synthesizeWmActivate(true);
 235                 }
 236             };
 237             WToolkit.postEvent(WToolkit.targetToAppContext(this),
 238                                new InvocationEvent(this, r));
 239         }
 240     }
 241 
 242     public void registerAccelerator(AWTKeyStroke stroke) {}
 243     public void unregisterAccelerator(AWTKeyStroke stroke) {}
 244 
 245     /**
 246      * Should be overridden in subclasses. Call to
 247      *     super.notifyModalBlocked(blocker, blocked) must be present
 248      *     when overriding.
 249      * It may occur that embedded frame is not put into its
 250      *     container at the moment when it is blocked, for example,
 251      *     when running an applet in IE. Then the call to this method
 252      *     should be delayed until embedded frame is reparented.
 253      *
 254      * NOTE: This method may be called by privileged threads.
 255      *     DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 256      */
 257     public void notifyModalBlocked(Dialog blocker, boolean blocked) {
 258         try {
 259             ComponentPeer thisPeer = (ComponentPeer)WToolkit.targetToPeer(this);
 260             ComponentPeer blockerPeer = (ComponentPeer)WToolkit.targetToPeer(blocker);
 261             notifyModalBlockedImpl((WEmbeddedFramePeer)thisPeer,
 262                                    (WWindowPeer)blockerPeer, blocked);
 263         } catch (Exception z) {
 264             z.printStackTrace(System.err);
 265         }
 266     }
 267     native void notifyModalBlockedImpl(WEmbeddedFramePeer peer, WWindowPeer blockerPeer, boolean blocked);
 268 }