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