1 /*
   2  * Copyright (c) 1996, 2018, 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.windows;
  26 
  27 import java.awt.Color;
  28 import java.awt.Component;
  29 import java.awt.Dimension;
  30 import java.awt.Graphics;
  31 import java.awt.Graphics2D;
  32 import java.awt.GraphicsConfiguration;
  33 import java.awt.peer.CanvasPeer;
  34 
  35 import sun.awt.PaintEventDispatcher;
  36 import sun.awt.SunToolkit;
  37 
  38 class WCanvasPeer extends WComponentPeer implements CanvasPeer {
  39 
  40     private boolean eraseBackground;
  41 
  42     // Toolkit & peer internals
  43 
  44     WCanvasPeer(Component target) {
  45         super(target);
  46     }
  47 
  48     @Override
  49     native void create(WComponentPeer parent);
  50 
  51     @Override
  52     void initialize() {
  53         eraseBackground = !SunToolkit.getSunAwtNoerasebackground();
  54         boolean eraseBackgroundOnResize = SunToolkit.getSunAwtErasebackgroundonresize();
  55         // Optimization: the default value in the native code is true, so we
  56         // call setNativeBackgroundErase only when the value changes to false
  57         if (!PaintEventDispatcher.getPaintEventDispatcher().
  58                 shouldDoNativeBackgroundErase((Component)target)) {
  59             eraseBackground = false;
  60         }
  61         setNativeBackgroundErase(eraseBackground, eraseBackgroundOnResize);
  62         super.initialize();
  63         Color bg = ((Component)target).getBackground();
  64         if (bg != null) {
  65             setBackground(bg);
  66         }
  67     }
  68 
  69     @Override
  70     public void paint(Graphics g) {
  71         Dimension d = ((Component)target).getSize();
  72         if (g instanceof Graphics2D) {

  73             // background color is setup correctly, so just use clearRect
  74             g.clearRect(0, 0, d.width, d.height);
  75         } else {
  76             // emulate clearRect
  77             g.setColor(((Component)target).getBackground());
  78             g.fillRect(0, 0, d.width, d.height);
  79             g.setColor(((Component)target).getForeground());
  80         }
  81         super.paint(g);
  82     }
  83 
  84     @Override
  85     public boolean shouldClearRectBeforePaint() {
  86         return eraseBackground;
  87     }
  88 
  89     /*
  90      * Disables background erasing for this canvas, both for resizing
  91      * and not-resizing repaints.
  92      */
  93     void disableBackgroundErase() {
  94         eraseBackground = false;
  95         setNativeBackgroundErase(false, false);
  96     }
  97 
  98     /*
  99      * Sets background erasing flags at the native level. If {@code
 100      * doErase} is set to {@code true}, canvas background is erased on
 101      * every repaint. If {@code doErase} is {@code false} and {@code
 102      * doEraseOnResize} is {@code true}, then background is only erased
 103      * on resizing repaints. If both {@code doErase} and {@code
 104      * doEraseOnResize} are false, then background is never erased.
 105      */
 106     private native void setNativeBackgroundErase(boolean doErase,
 107                                                  boolean doEraseOnResize);
 108 
 109     @Override
 110     public GraphicsConfiguration getAppropriateGraphicsConfiguration(
 111             GraphicsConfiguration gc)
 112     {
 113         return gc;
 114     }
 115 }
--- EOF ---