1 /* 2 * Copyright (c) 2011, 2015, 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 com.sun.javafx.tk.quantum; 27 28 import com.sun.glass.ui.Application; 29 import com.sun.glass.ui.Window; 30 import com.sun.glass.ui.View; 31 32 import com.sun.javafx.tk.AppletWindow; 33 import com.sun.javafx.tk.TKStage; 34 35 import java.lang.ref.WeakReference; 36 import java.util.Map; 37 import java.util.concurrent.atomic.AtomicInteger; 38 import java.util.concurrent.atomic.AtomicReference; 39 40 import javafx.stage.Stage; 41 42 /** 43 * Implementation class for AppletWindow using a Glass Window 44 */ 45 class GlassAppletWindow implements AppletWindow { 46 private final Window glassWindow; 47 private WeakReference<Stage> topStage; 48 private String serverName; 49 50 GlassAppletWindow(long nativeParent, String serverName) { 51 if (0 == nativeParent) { 52 if (serverName != null) { 53 throw new RuntimeException("GlassAppletWindow constructor used incorrectly."); 54 } 55 glassWindow = Application.GetApplication().createWindow(null, Window.NORMAL); 56 } else { 57 this.serverName = serverName; 58 glassWindow = Application.GetApplication().createWindow(nativeParent); 59 } 60 } 61 62 Window getGlassWindow() { 63 return glassWindow; 64 } 65 66 @Override 67 public void setBackgroundColor(final int color) { 68 Application.invokeLater(() -> { 69 float RR = (float) ((color >> 16) & 0xff) / 255f; 70 float GG = (float) ((color >> 8) & 0xff) / 255f; 71 float BB = (float) (color & 0xff) / 255f; 72 glassWindow.setBackground(RR, GG, BB); 73 }); 74 } 75 76 @Override 77 public void setForegroundColor(int color) { 78 // Do nothing, foreground color is not supported 79 } 80 81 @Override 82 public void setVisible(final boolean state) { 83 Application.invokeLater(() -> glassWindow.setVisible(state)); 84 } 85 86 @Override 87 public void setSize(final int width, final int height) { 88 Application.invokeLater(() -> glassWindow.setSize(width, height)); 89 } 90 91 @Override 92 public int getWidth() { 93 final AtomicInteger width = new AtomicInteger(0); 94 Application.invokeAndWait(() -> width.set(glassWindow.getWidth())); 95 return width.get(); 96 } 97 98 @Override 99 public int getHeight() { 100 final AtomicInteger height = new AtomicInteger(0); 101 Application.invokeAndWait(() -> height.set(glassWindow.getHeight())); 102 return height.get(); 103 } 104 105 @Override 106 public void setPosition(final int x, final int y) { 107 Application.invokeLater(() -> glassWindow.setPosition(x, y)); 108 } 109 110 @Override 111 public int getPositionX() { 112 final AtomicInteger x = new AtomicInteger(0); 113 Application.invokeAndWait(() -> x.set(glassWindow.getX())); 114 return x.get(); 115 } 116 117 @Override 118 public int getPositionY() { 119 final AtomicInteger y = new AtomicInteger(0); 120 Application.invokeAndWait(() -> y.set(glassWindow.getY())); 121 return y.get(); 122 } 123 124 @Override 125 public float getPlatformScaleX() { 126 final AtomicReference<Float> pScale = new AtomicReference<Float>(0.0f); 127 Application.invokeAndWait(() -> pScale.set(glassWindow.getPlatformScaleX())); 128 return pScale.get(); 129 } 130 131 @Override 132 public float getPlatformScaleY() { 133 final AtomicReference<Float> pScale = new AtomicReference<Float>(0.0f); 134 Application.invokeAndWait(() -> pScale.set(glassWindow.getPlatformScaleY())); 135 return pScale.get(); 136 } 137 138 void dispose() { 139 QuantumToolkit.runWithRenderLock(() -> { 140 glassWindow.close(); 141 //TODO - should update glass scene view state 142 //TODO - doesn't matter because we are disposing 143 return null; 144 }); 145 } 146 147 @Override 148 public void setStageOnTop(Stage topStage) { 149 if (null != topStage) { 150 this.topStage = new WeakReference<Stage>(topStage); 151 } else { 152 this.topStage = null; 153 } 154 } 155 156 @Override 157 public int getRemoteLayerId() { 158 final AtomicInteger id = new AtomicInteger(-1); 159 Application.invokeAndWait(() -> { 160 View view = glassWindow.getView(); 161 if (view != null) { 162 id.set(view.getNativeRemoteLayerId(serverName)); 163 } 164 }); 165 return id.get(); 166 } 167 168 @Override 169 public void dispatchEvent(final Map eventInfo) { 170 Application.invokeAndWait(() -> glassWindow.dispatchNpapiEvent(eventInfo)); 171 } 172 173 /** 174 * Call when a child stage becomes visible so we can make sure topStage 175 * is pushed to the front where it should be. 176 */ 177 void assertStageOrder() { 178 if (null != topStage) { 179 Stage ts = topStage.get(); 180 if (null != ts) { 181 TKStage tsp = ts.impl_getPeer(); 182 if (tsp instanceof WindowStage && ((WindowStage)tsp).isVisible()) { 183 // call the underlying Glass window toFront to bypass 184 // the check in WindowStage.toFront or we'll create an 185 // infinite loop 186 Window pw = ((WindowStage)tsp).getPlatformWindow(); 187 if (null != pw) { 188 pw.toFront(); 189 } 190 } 191 } 192 } 193 } 194 }