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