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 getUIScale() {
 126         final AtomicReference<Float> uiScale = new AtomicReference<Float>(0.0f);
 127         Application.invokeAndWait(() -> uiScale.set(glassWindow.getPlatformScale()));
 128         return uiScale.get();
 129     }
 130 
 131     void dispose() {
 132         QuantumToolkit.runWithRenderLock(() -> {
 133             glassWindow.close();
 134             //TODO - should update glass scene view state
 135             //TODO - doesn't matter because we are disposing
 136             return null;
 137         });
 138     }
 139 
 140     @Override
 141     public void setStageOnTop(Stage topStage) {
 142         if (null != topStage) {
 143             this.topStage = new WeakReference<Stage>(topStage);
 144         } else {
 145             this.topStage = null;
 146         }
 147     }
 148 
 149     @Override
 150     public int getRemoteLayerId() {
 151         final AtomicInteger id = new AtomicInteger(-1);
 152         Application.invokeAndWait(() -> {
 153             View view = glassWindow.getView();
 154             if (view != null) {
 155                 id.set(view.getNativeRemoteLayerId(serverName));
 156             }
 157         });
 158         return id.get();
 159     }
 160 
 161     @Override
 162     public void dispatchEvent(final Map eventInfo) {
 163         Application.invokeAndWait(() -> glassWindow.dispatchNpapiEvent(eventInfo));
 164     }
 165 
 166     /**
 167      * Call when a child stage becomes visible so we can make sure topStage
 168      * is pushed to the front where it should be.
 169      */
 170     void assertStageOrder() {
 171         if (null != topStage) {
 172             Stage ts = topStage.get();
 173             if (null != ts) {
 174                 TKStage tsp = ts.impl_getPeer();
 175                 if (tsp instanceof WindowStage && ((WindowStage)tsp).isVisible()) {
 176                     // call the underlying Glass window toFront to bypass
 177                     // the check in WindowStage.toFront or we'll create an
 178                     // infinite loop
 179                     Window pw = ((WindowStage)tsp).getPlatformWindow();
 180                     if (null != pw) {
 181                         pw.toFront();
 182                     }
 183                 }
 184             }
 185         }
 186     }
 187 }