1/*
2 * Copyright (c) 2002, 2013, 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 */
25package sun.awt.X11;
26
27import java.awt.*;
28import java.awt.peer.*;
29
30import sun.awt.SunToolkit;
31
32import sun.awt.X11GraphicsConfig;
33import sun.awt.X11GraphicsDevice;
34
35class XCanvasPeer extends XComponentPeer implements CanvasPeer {
36
37    private boolean eraseBackgroundDisabled;
38
39    XCanvasPeer() {}
40
41    XCanvasPeer(XCreateWindowParams params) {
42        super(params);
43    }
44
45    XCanvasPeer(Component target) {
46        super(target);
47    }
48
49    void preInit(XCreateWindowParams params) {
50        super.preInit(params);
51        if (SunToolkit.getSunAwtNoerasebackground()) {
52            disableBackgroundErase();
53        }
54    }
55
56    /* Get a GraphicsConfig with the same visual on the new
57     * screen, which should be easy in Xinerama mode.
58     */
59    public GraphicsConfiguration getAppropriateGraphicsConfiguration(
60                                    GraphicsConfiguration gc)
61    {
62        if (graphicsConfig == null || gc == null) {
63            return gc;
64        }
65        // Opt: Only need to do if we're not using the default GC
66
67        int screenNum = ((X11GraphicsDevice)gc.getDevice()).getScreen();
68
69        X11GraphicsConfig parentgc;
70        // save vis id of current gc
71        int visual = graphicsConfig.getVisual();
72
73        X11GraphicsDevice newDev = (X11GraphicsDevice) GraphicsEnvironment.
74            getLocalGraphicsEnvironment().
75            getScreenDevices()[screenNum];
76
77        for (int i = 0; i < newDev.getNumConfigs(screenNum); i++) {
78            if (visual == newDev.getConfigVisualId(i, screenNum)) {
79                // use that
80                graphicsConfig = (X11GraphicsConfig)newDev.getConfigurations()[i];
81                break;
82            }
83        }
84        // just in case...
85        if (graphicsConfig == null) {
86            graphicsConfig = (X11GraphicsConfig) GraphicsEnvironment.
87                getLocalGraphicsEnvironment().
88                getScreenDevices()[screenNum].
89                getDefaultConfiguration();
90        }
91
92        return graphicsConfig;
93    }
94
95    protected boolean shouldFocusOnClick() {
96        // Canvas should always be able to be focused by mouse clicks.
97        return true;
98    }
99
100    public void disableBackgroundErase() {
101        eraseBackgroundDisabled = true;
102    }
103    protected boolean doEraseBackground() {
104        return !eraseBackgroundDisabled;
105    }
106}
107