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 pScale = window.getPlatformScale();
  69                 Screen screen = window.getScreen();
  70                 float sx = screen == null ? 0 : screen.getX();
  71                 float sy = screen == null ? 0 : screen.getY();
  72                 float wx = window.getX();
  73                 float wy = window.getY();
  74                 float newx = sx + (wx - sx) / pScale;
  75                 float newy = sy + (wy - sy) / pScale;
  76                 stage.stageListener.changedLocation(newx, newy);
  77                 //We need to sync the new x,y for painting
  78                 if (!Application.GetApplication().hasWindowManager()) {
  79                     QuantumToolkit.runWithRenderLock(() -> {
  80                         GlassScene scene = stage.getScene();
  81                         if (scene != null) {
  82                             scene.updateSceneState();
  83                         }
  84                         return null;
  85                     });
  86                 }
  87                 break;
  88             }
  89             case WindowEvent.RESIZE: {
  90                 float pScale = window.getPlatformScale();
  91                 stage.stageListener.changedSize(window.getWidth()  / pScale,
  92                                                 window.getHeight() / pScale);
  93                  break;
  94             }
  95             case WindowEvent.FOCUS_GAINED:
  96                 WindowStage.addActiveWindow(stage);
  97                 stage.stageListener.changedFocused(true, FocusCause.ACTIVATED);
  98                 break;
  99             case WindowEvent.FOCUS_LOST:
 100                 stage.stageListener.changedFocused(false, FocusCause.DEACTIVATED);
 101                 break;
 102             case WindowEvent.FOCUS_UNGRAB:
 103                 stage.stageListener.focusUngrab();
 104                 break;
 105             case WindowEvent.FOCUS_GAINED_FORWARD:
 106                 WindowStage.addActiveWindow(stage);
 107                 stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_FORWARD);
 108                 break;
 109             case WindowEvent.FOCUS_GAINED_BACKWARD:
 110                 WindowStage.addActiveWindow(stage);
 111                 stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_BACKWARD);
 112                 break;
 113             case WindowEvent.FOCUS_DISABLED:
 114                 stage.handleFocusDisabled();
 115                 break;
 116             case WindowEvent.DESTROY:
 117                 stage.setPlatformWindowClosed();
 118                 stage.stageListener.closed();
 119                 break;
 120             case WindowEvent.CLOSE:
 121                 stage.stageListener.closing();
 122                 break;
 123             default:
 124                 if (QuantumToolkit.verbose) {
 125                     System.err.println("GlassWindowEventHandler: unknown type: " + type);
 126                 }
 127                 break;
 128         }
 129         return null;
 130     }
 131 
 132     @Override
 133     public void handleLevelEvent(int level) {
 134         QuantumToolkit.runWithoutRenderLock(() -> {
 135             AccessControlContext acc = stage.getAccessControlContext();
 136             return AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
 137                 stage.stageListener.changedAlwaysOnTop(level != Level.NORMAL);
 138                 return (Void)null;
 139             } , acc);
 140         });
 141     }
 142 
 143     @Override
 144     public void handleWindowEvent(final Window window, final long time, final int type) {
 145         this.window = window;
 146         this.type = type;
 147 
 148         QuantumToolkit.runWithoutRenderLock(() -> {
 149             AccessControlContext acc = stage.getAccessControlContext();
 150             return AccessController.doPrivileged(this, acc);
 151         });
 152     }
 153 
 154     @Override
 155     public void handleScreenChangedEvent(Window window, long time, Screen oldScreen, Screen newScreen) {
 156         GlassScene scene = stage.getScene();
 157         if (scene != null) {
 158             QuantumToolkit.runWithRenderLock(() -> {
 159                 scene.entireSceneNeedsRepaint();
 160                 scene.updateSceneState();
 161                 return null;
 162             });
 163         }
 164 
 165         QuantumToolkit.runWithoutRenderLock(() -> {
 166             AccessControlContext acc = stage.getAccessControlContext();
 167             return AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
 168                 stage.stageListener.changedScreen(oldScreen, newScreen);
 169                 return (Void)null;
 170             } , acc);
 171         });
 172     }
 173 }