1 /* 2 * Copyright (c) 1996, 2009, 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.*; 28 import java.awt.peer.*; 29 import java.lang.ref.WeakReference; 30 import java.lang.reflect.Method; 31 import sun.awt.AWTAccessor; 32 import sun.awt.ComponentAccessor; 33 import sun.awt.SunToolkit; 34 import sun.awt.Win32GraphicsDevice; 35 import sun.awt.PaintEventDispatcher; 36 37 class WCanvasPeer extends WComponentPeer implements CanvasPeer { 38 39 private boolean eraseBackground; 40 41 Method resetGCMethod; 42 43 // Toolkit & peer internals 44 45 WCanvasPeer(Component target) { 46 super(target); 47 } 48 49 /* 50 * From the DisplayChangedListener interface. 51 * 52 * Overrides WComponentPeer version because Canvases can be created with 53 * a non-defulat GraphicsConfiguration, which is no longer valid. 54 * Up-called for other windows peer instances (WPanelPeer, WWindowPeer). 55 */ 56 public void displayChanged() { 57 clearLocalGC(); 58 resetTargetGC(); 59 super.displayChanged(); 60 } 61 62 /* 63 * Reset the graphicsConfiguration member of our target Component. 64 * Component.resetGC() is a package-private method, so we have to call it 65 * through reflection. 66 */ 67 public void resetTargetGC() { 68 ComponentAccessor.resetGC((Component)target); 69 } 70 71 /* 72 * Clears the peer's winGraphicsConfig member. 73 * Overridden by WWindowPeer, which shouldn't have a null winGraphicsConfig. 74 */ 75 void clearLocalGC() { 76 winGraphicsConfig = null; 77 } 78 79 native void create(WComponentPeer parent); 80 81 void initialize() { 82 eraseBackground = !SunToolkit.getSunAwtNoerasebackground(); 83 boolean eraseBackgroundOnResize = SunToolkit.getSunAwtErasebackgroundonresize(); 84 // Optimization: the default value in the native code is true, so we 85 // call setNativeBackgroundErase only when the value changes to false 86 if (!PaintEventDispatcher.getPaintEventDispatcher(). 87 shouldDoNativeBackgroundErase((Component)target)) { 88 eraseBackground = false; 89 } 90 setNativeBackgroundErase(eraseBackground, eraseBackgroundOnResize); 91 super.initialize(); 92 Color bg = ((Component)target).getBackground(); 93 if (bg != null) { 94 setBackground(bg); 95 } 96 } 97 98 public void paint(Graphics g) { 99 Dimension d = ((Component)target).getSize(); 100 if (g instanceof Graphics2D || 101 g instanceof sun.awt.Graphics2Delegate) { 102 // background color is setup correctly, so just use clearRect 103 g.clearRect(0, 0, d.width, d.height); 104 } else { 105 // emulate clearRect 106 g.setColor(((Component)target).getBackground()); 107 g.fillRect(0, 0, d.width, d.height); 108 g.setColor(((Component)target).getForeground()); 109 } 110 super.paint(g); 111 } 112 113 public void print(Graphics g) { 114 if (!(target instanceof Window) || 115 AWTAccessor.getWindowAccessor().isOpaque((Window)target)) 116 { 117 Dimension d = ((Component)target).getSize(); 118 if (g instanceof Graphics2D || 119 g instanceof sun.awt.Graphics2Delegate) { 120 // background color is setup correctly, so just use clearRect 121 g.clearRect(0, 0, d.width, d.height); 122 } else { 123 // emulate clearRect 124 g.setColor(((Component)target).getBackground()); 125 g.fillRect(0, 0, d.width, d.height); 126 g.setColor(((Component)target).getForeground()); 127 } 128 } 129 super.print(g); 130 } 131 132 public boolean shouldClearRectBeforePaint() { 133 return eraseBackground; 134 } 135 136 /* 137 * Disables background erasing for this canvas, both for resizing 138 * and not-resizing repaints. 139 */ 140 void disableBackgroundErase() { 141 eraseBackground = false; 142 setNativeBackgroundErase(false, false); 143 } 144 145 /* 146 * Sets background erasing flags at the native level. If {@code 147 * doErase} is set to {@code true}, canvas background is erased on 148 * every repaint. If {@code doErase} is {@code false} and {@code 149 * doEraseOnResize} is {@code true}, then background is only erased 150 * on resizing repaints. If both {@code doErase} and {@code 151 * doEraseOnResize} are false, then background is never erased. 152 */ 153 private native void setNativeBackgroundErase(boolean doErase, 154 boolean doEraseOnResize); 155 }