1 /*
   2  * Copyright (c) 2011, 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 sun.lwawt.macosx.event;
  27 
  28 import sun.lwawt.macosx.CocoaConstants;
  29 import java.awt.event.*;
  30 
  31 /**
  32  * A class representing Cocoa NSEvent class with the fields only necessary for
  33  * JDK functionality.
  34  */
  35 public final class NSEvent {
  36     private int type;
  37     private int modifierFlags;
  38 
  39     // Mouse event information
  40     private int clickCount;
  41     private int buttonNumber;
  42     private int x;
  43     private int y;
  44     private double scrollDeltaY;
  45     private double scrollDeltaX;
  46     private int absX;
  47     private int absY;
  48 
  49     // Key event information
  50     private short keyCode;
  51     private String charactersIgnoringModifiers;
  52 
  53     public NSEvent(int type, int modifierFlags, short keyCode, String charactersIgnoringModifiers) {
  54         this.type = type;
  55         this.modifierFlags = modifierFlags;
  56         this.keyCode = keyCode;
  57         this.charactersIgnoringModifiers = charactersIgnoringModifiers;
  58     }
  59 
  60     public NSEvent(int type, int modifierFlags, int clickCount, int buttonNumber,
  61                    int x, int y, int absX, int absY,
  62                    double scrollDeltaY, double scrollDeltaX) {
  63         this.type = type;
  64         this.modifierFlags = modifierFlags;
  65         this.clickCount = clickCount;
  66         this.buttonNumber = buttonNumber;
  67         this.x = x;
  68         this.y = y;
  69         this.absX = absX;
  70         this.absY = absY;
  71         this.scrollDeltaY = scrollDeltaY;
  72         this.scrollDeltaX = scrollDeltaX;
  73     }
  74 
  75     public int getType() {
  76         return type;
  77     }
  78 
  79     public int getModifierFlags() {
  80         return modifierFlags;
  81     }
  82 
  83     public int getClickCount() {
  84         return clickCount;
  85     }
  86 
  87     public int getButtonNumber() {
  88         return buttonNumber;
  89     }
  90 
  91     public int getX() {
  92         return x;
  93     }
  94 
  95     public int getY() {
  96         return y;
  97     }
  98 
  99     public double getScrollDeltaY() {
 100         return scrollDeltaY;
 101     }
 102 
 103     public double getScrollDeltaX() {
 104         return scrollDeltaX;
 105     }
 106 
 107     public int getAbsX() {
 108         return absX;
 109     }
 110 
 111     public int getAbsY() {
 112         return absY;
 113     }
 114 
 115     public short getKeyCode() {
 116         return keyCode;
 117     }
 118 
 119     public String getCharactersIgnoringModifiers() {
 120         return charactersIgnoringModifiers;
 121     }
 122 
 123     @Override
 124     public String toString() {
 125         return "NSEvent[" + getType() + " ," + getModifierFlags() + " ,"
 126                 + getClickCount() + " ," + getButtonNumber() + " ," + getX() + " ,"
 127                 + getY() + " ," + getAbsX() + " ," + getAbsY()+ " ," + getKeyCode() + " ,"
 128                 + getCharactersIgnoringModifiers() + "]";
 129     }
 130 
 131     /*
 132      * Converts an NSEvent button number to a MouseEvent constant.
 133      */
 134     public static int nsToJavaButton(int buttonNumber) {
 135         int jbuttonNumber = buttonNumber + 1;
 136         switch (buttonNumber) {
 137             case CocoaConstants.kCGMouseButtonLeft:
 138                 jbuttonNumber = MouseEvent.BUTTON1;
 139                 break;
 140             case CocoaConstants.kCGMouseButtonRight:
 141                 jbuttonNumber = MouseEvent.BUTTON3;
 142                 break;
 143             case CocoaConstants.kCGMouseButtonCenter:
 144                 jbuttonNumber = MouseEvent.BUTTON2;
 145                 break;
 146         }
 147         return jbuttonNumber;
 148     }
 149 
 150     /*
 151      * Converts NPCocoaEvent types to AWT event types.
 152      */
 153     public static int npToJavaEventType(int npEventType) {
 154         int jeventType = 0;
 155         switch (npEventType) {
 156             case CocoaConstants.NPCocoaEventMouseDown:
 157                 jeventType = MouseEvent.MOUSE_PRESSED;
 158                 break;
 159             case CocoaConstants.NPCocoaEventMouseUp:
 160                 jeventType = MouseEvent.MOUSE_RELEASED;
 161                 break;
 162             case CocoaConstants.NPCocoaEventMouseMoved:
 163                 jeventType = MouseEvent.MOUSE_MOVED;
 164                 break;
 165             case CocoaConstants.NPCocoaEventMouseEntered:
 166                 jeventType = MouseEvent.MOUSE_ENTERED;
 167                 break;
 168             case CocoaConstants.NPCocoaEventMouseExited:
 169                 jeventType = MouseEvent.MOUSE_EXITED;
 170                 break;
 171             case CocoaConstants.NPCocoaEventMouseDragged:
 172                 jeventType = MouseEvent.MOUSE_DRAGGED;
 173                 break;
 174             case CocoaConstants.NPCocoaEventKeyDown:
 175                 jeventType = KeyEvent.KEY_PRESSED;
 176                 break;
 177             case CocoaConstants.NPCocoaEventKeyUp:
 178                 jeventType = KeyEvent.KEY_RELEASED;
 179                 break;
 180         }
 181         return jeventType;
 182     }
 183 
 184     /*
 185      * Converts NSEvent types to AWT event types.
 186      */
 187     public static int nsToJavaEventType(int nsEventType) {
 188         int jeventType = 0;
 189         switch (nsEventType) {
 190             case CocoaConstants.NSLeftMouseDown:
 191             case CocoaConstants.NSRightMouseDown:
 192             case CocoaConstants.NSOtherMouseDown:
 193                 jeventType = MouseEvent.MOUSE_PRESSED;
 194                 break;
 195             case CocoaConstants.NSLeftMouseUp:
 196             case CocoaConstants.NSRightMouseUp:
 197             case CocoaConstants.NSOtherMouseUp:
 198                 jeventType = MouseEvent.MOUSE_RELEASED;
 199                 break;
 200             case CocoaConstants.NSMouseMoved:
 201                 jeventType = MouseEvent.MOUSE_MOVED;
 202                 break;
 203             case CocoaConstants.NSLeftMouseDragged:
 204             case CocoaConstants.NSRightMouseDragged:
 205             case CocoaConstants.NSOtherMouseDragged:
 206                 jeventType = MouseEvent.MOUSE_DRAGGED;
 207                 break;
 208             case CocoaConstants.NSMouseEntered:
 209                 jeventType = MouseEvent.MOUSE_ENTERED;
 210                 break;
 211             case CocoaConstants.NSMouseExited:
 212                 jeventType = MouseEvent.MOUSE_EXITED;
 213                 break;
 214             case CocoaConstants.NSScrollWheel:
 215                 jeventType = MouseEvent.MOUSE_WHEEL;
 216                 break;
 217             case CocoaConstants.NSKeyDown:
 218                 jeventType = KeyEvent.KEY_PRESSED;
 219                 break;
 220             case CocoaConstants.NSKeyUp:
 221                 jeventType = KeyEvent.KEY_RELEASED;
 222                 break;
 223         }
 224         return jeventType;
 225     }
 226 
 227     /*
 228      * Converts NSEvent mouse modifiers to AWT mouse modifiers.
 229      */
 230     public static native int nsToJavaMouseModifiers(int buttonNumber,
 231                                                     int modifierFlags);
 232 
 233     /*
 234      * Converts NSEvent key modifiers to AWT key modifiers.
 235      */
 236     public static native int nsToJavaKeyModifiers(int modifierFlags);
 237 
 238     /*
 239      * Converts NSEvent key info to AWT key info.
 240      */
 241     public static native boolean nsToJavaKeyInfo(int[] in, int[] out);
 242 
 243     /*
 244      * Converts NSEvent key modifiers to AWT key info.
 245      */
 246     public static native void nsKeyModifiersToJavaKeyInfo(int[] in, int[] out);
 247 
 248     public static boolean isPopupTrigger(int jmodifiers) {
 249         final boolean isRightButtonDown = ((jmodifiers & InputEvent.BUTTON3_DOWN_MASK) != 0);
 250         final boolean isLeftButtonDown = ((jmodifiers & InputEvent.BUTTON1_DOWN_MASK) != 0);
 251         final boolean isControlDown = ((jmodifiers & InputEvent.CTRL_DOWN_MASK) != 0);
 252         return isRightButtonDown || (isControlDown && isLeftButtonDown);
 253     }
 254 }