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 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 
WCanvasPeer(Component target)44     WCanvasPeer(Component target) {
45         super(target);
46     }
47 
48     @Override
create(WComponentPeer parent)49     native void create(WComponentPeer parent);
50 
51     @Override
initialize()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
paint(Graphics g)70     public void paint(Graphics g) {
71         Dimension d = ((Component)target).getSize();
72         if (g instanceof Graphics2D ||
73             g instanceof sun.awt.Graphics2Delegate) {
74             // background color is setup correctly, so just use clearRect
75             g.clearRect(0, 0, d.width, d.height);
76         } else {
77             // emulate clearRect
78             g.setColor(((Component)target).getBackground());
79             g.fillRect(0, 0, d.width, d.height);
80             g.setColor(((Component)target).getForeground());
81         }
82         super.paint(g);
83     }
84 
85     @Override
shouldClearRectBeforePaint()86     public boolean shouldClearRectBeforePaint() {
87         return eraseBackground;
88     }
89 
90     /*
91      * Disables background erasing for this canvas, both for resizing
92      * and not-resizing repaints.
93      */
disableBackgroundErase()94     void disableBackgroundErase() {
95         eraseBackground = false;
96         setNativeBackgroundErase(false, false);
97     }
98 
99     /*
100      * Sets background erasing flags at the native level. If {@code
101      * doErase} is set to {@code true}, canvas background is erased on
102      * every repaint. If {@code doErase} is {@code false} and {@code
103      * doEraseOnResize} is {@code true}, then background is only erased
104      * on resizing repaints. If both {@code doErase} and {@code
105      * doEraseOnResize} are false, then background is never erased.
106      */
setNativeBackgroundErase(boolean doErase, boolean doEraseOnResize)107     private native void setNativeBackgroundErase(boolean doErase,
108                                                  boolean doEraseOnResize);
109 
110     @Override
getAppropriateGraphicsConfiguration( GraphicsConfiguration gc)111     public GraphicsConfiguration getAppropriateGraphicsConfiguration(
112             GraphicsConfiguration gc)
113     {
114         return gc;
115     }
116 }
117