1 /*
   2  * Copyright (c) 2008, 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.events.WindowEvent;
  29 import com.sun.glass.ui.Application;
  30 import com.sun.glass.ui.Screen;
  31 import com.sun.glass.ui.Window;
  32 import com.sun.glass.ui.Window.Level;
  33 
  34 import com.sun.javafx.tk.FocusCause;
  35 
  36 import java.security.AccessControlContext;
  37 import java.security.AccessController;
  38 import java.security.PrivilegedAction;
  39 
  40 class GlassWindowEventHandler extends Window.EventHandler implements PrivilegedAction<Void> {
  41 
  42     private final WindowStage stage;
  43 
  44     private Window window;
  45     private int type;
  46 
  47     public GlassWindowEventHandler(WindowStage stage) {
  48         this.stage = stage;
  49     }
  50 
  51     @Override
  52     public Void run() {
  53         if (stage == null || stage.stageListener == null) {
  54             return null;
  55         }
  56         switch (type) {
  57             case WindowEvent.MINIMIZE:
  58                 stage.stageListener.changedIconified(true);
  59                 break;
  60             case WindowEvent.MAXIMIZE:
  61                 stage.stageListener.changedMaximized(true);
  62                 break;
  63             case WindowEvent.RESTORE:
  64                 stage.stageListener.changedIconified(false);
  65                 stage.stageListener.changedMaximized(false);
  66                 break;
  67             case WindowEvent.MOVE: {
  68                 float wx = window.getX();
  69                 float wy = window.getY();
  70                 Screen screen = window.getScreen();
  71                 float newx, newy;
  72                 if (screen != null) {
  73                     float pScaleX = screen.getPlatformScaleX();
  74                     float pScaleY = screen.getPlatformScaleY();
  75                     float sx = screen.getX();
  76                     float sy = screen.getY();
  77                     float px = screen.getPlatformX();
  78                     float py = screen.getPlatformY();
  79                     newx = sx + (wx - px) / pScaleX;
  80                     newy = sy + (wy - py) / pScaleY;
  81                 } else {
  82                     newx = wx;
  83                     newy = wy;
  84                 }
  85                 stage.stageListener.changedLocation(newx, newy);
  86                 //We need to sync the new x,y for painting
  87                 if (!Application.GetApplication().hasWindowManager()) {
  88                     QuantumToolkit.runWithRenderLock(() -> {
  89                         GlassScene scene = stage.getScene();
  90                         if (scene != null) {
  91                             scene.updateSceneState();
  92                         }
  93                         return null;
  94                     });
  95                 }
  96                 break;
  97             }
  98             case WindowEvent.RESIZE: {
  99                 float pScaleX = window.getPlatformScaleX();
 100                 float pScaleY = window.getPlatformScaleY();
 101                 stage.stageListener.changedSize(window.getWidth()  / pScaleX,
 102                                                 window.getHeight() / pScaleY);
 103                  break;
 104             }
 105             case WindowEvent.RESCALE: {
 106                 float outScaleX = window.getOutputScaleX();
 107                 float outScaleY = window.getOutputScaleY();
 108                 stage.stageListener.changedScale(outScaleX, outScaleY);
 109                 // We need to sync the new scales for painting
 110                 QuantumToolkit.runWithRenderLock(() -> {
 111                     GlassScene scene = stage.getScene();
 112                     if (scene != null) {
 113                         scene.entireSceneNeedsRepaint();
 114                         scene.updateSceneState();
 115                     }
 116                     return null;
 117                 });
 118                 break;
 119             }
 120             case WindowEvent.FOCUS_GAINED:
 121                 WindowStage.addActiveWindow(stage);
 122                 stage.stageListener.changedFocused(true, FocusCause.ACTIVATED);
 123                 break;
 124             case WindowEvent.FOCUS_LOST:
 125                 stage.stageListener.changedFocused(false, FocusCause.DEACTIVATED);
 126                 break;
 127             case WindowEvent.FOCUS_UNGRAB:
 128                 stage.stageListener.focusUngrab();
 129                 break;
 130             case WindowEvent.FOCUS_GAINED_FORWARD:
 131                 WindowStage.addActiveWindow(stage);
 132                 stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_FORWARD);
 133                 break;
 134             case WindowEvent.FOCUS_GAINED_BACKWARD:
 135                 WindowStage.addActiveWindow(stage);
 136                 stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_BACKWARD);
 137                 break;
 138             case WindowEvent.FOCUS_DISABLED:
 139                 stage.handleFocusDisabled();
 140                 break;
 141             case WindowEvent.DESTROY:
 142                 stage.setPlatformWindowClosed();
 143                 stage.stageListener.closed();
 144                 break;
 145             case WindowEvent.CLOSE:
 146                 stage.stageListener.closing();
 147                 break;
 148             default:
 149                 if (QuantumToolkit.verbose) {
 150                     System.err.println("GlassWindowEventHandler: unknown type: " + type);
 151                 }
 152                 break;
 153         }
 154         return null;
 155     }
 156 
 157     @Override
 158     public void handleLevelEvent(int level) {
 159         QuantumToolkit.runWithoutRenderLock(() -> {
 160             AccessControlContext acc = stage.getAccessControlContext();
 161             return AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
 162                 stage.stageListener.changedAlwaysOnTop(level != Level.NORMAL);
 163                 return (Void)null;
 164             } , acc);
 165         });
 166     }
 167 
 168     @Override
 169     public void handleWindowEvent(final Window window, final long time, final int type) {
 170         this.window = window;
 171         this.type = type;
 172 
 173         QuantumToolkit.runWithoutRenderLock(() -> {
 174             AccessControlContext acc = stage.getAccessControlContext();
 175             return AccessController.doPrivileged(this, acc);
 176         });
 177     }
 178 
 179     @Override
 180     public void handleScreenChangedEvent(Window window, long time, Screen oldScreen, Screen newScreen) {
 181         GlassScene scene = stage.getScene();
 182         if (scene != null) {
 183             QuantumToolkit.runWithRenderLock(() -> {
 184                 scene.entireSceneNeedsRepaint();
 185                 scene.updateSceneState();
 186                 return null;
 187             });
 188         }
 189 
 190         QuantumToolkit.runWithoutRenderLock(() -> {
 191             AccessControlContext acc = stage.getAccessControlContext();
 192             return AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
 193                 stage.stageListener.changedScreen(oldScreen, newScreen);
 194                 return (Void)null;
 195             } , acc);
 196         });
 197     }
 198 }