1 /*
   2  * Copyright (c) 2012, 2013, 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 javafx.embed.swt;
  27 
  28 import org.eclipse.swt.SWT;
  29 import org.eclipse.swt.events.MouseEvent;
  30 
  31 import java.lang.reflect.Method;
  32 
  33 //import com.sun.glass.events.KeyEvent;
  34 import com.sun.javafx.embed.AbstractEvents;
  35 import org.eclipse.swt.widgets.Event;
  36 
  37 import java.lang.reflect.InvocationTargetException;
  38 
  39 /**
  40  * An utility class to translate event types between embedded
  41  * application and SWT.
  42  *
  43  */
  44 class SWTEvents {
  45 
  46 /*
  47     static int mouseIDToEmbedMouseType(int id) {
  48         switch (id) {
  49             case MouseEvent.MOUSE_PRESSED:
  50                 return AbstractEvents.MOUSEEVENT_PRESSED;
  51             case MouseEvent.MOUSE_RELEASED:
  52                 return AbstractEvents.MOUSEEVENT_RELEASED;
  53             case MouseEvent.MOUSE_CLICKED:
  54                 return AbstractEvents.MOUSEEVENT_CLICKED;
  55             case MouseEvent.MOUSE_MOVED:
  56                 return AbstractEvents.MOUSEEVENT_MOVED;
  57             case MouseEvent.MOUSE_DRAGGED:
  58                 return AbstractEvents.MOUSEEVENT_DRAGGED;
  59             case MouseEvent.MOUSE_ENTERED:
  60                 return AbstractEvents.MOUSEEVENT_ENTERED;
  61             case MouseEvent.MOUSE_EXITED:
  62                 return AbstractEvents.MOUSEEVENT_EXITED;
  63         }
  64         return 0;
  65     }
  66 */
  67     static int mouseButtonToEmbedMouseButton(int button, int extModifiers) {
  68         switch (button) {
  69             case 1: return AbstractEvents.MOUSEEVENT_PRIMARY_BUTTON;
  70             case 2: return AbstractEvents.MOUSEEVENT_MIDDLE_BUTTON;
  71             case 3: return AbstractEvents.MOUSEEVENT_SECONDARY_BUTTON;
  72         }
  73         return AbstractEvents.MOUSEEVENT_NONE_BUTTON;
  74     }
  75 
  76     static int getWheelRotation(Event e) {
  77         int divisor = 1;
  78         if ("win32".equals(SWT.getPlatform())) {
  79             int [] linesToScroll = new int [1];
  80             //OS.SystemParametersInfo (OS.SPI_GETWHEELSCROLLLINES, 0, linesToScroll, 0);
  81             try {
  82                 Class clazz = Class.forName("org.eclipse.swt.internal.win32.OS");
  83                 Method method = clazz.getDeclaredMethod("SystemParametersInfo", new Class []{int.class, int.class, int [].class, int.class});
  84                 method.invoke(clazz, 104 /*SPI_GETWHEELSCROLLLINES*/, 0, linesToScroll, 0);
  85             } catch (IllegalAccessException iae) {
  86             } catch (InvocationTargetException ite) {
  87             } catch (NoSuchMethodException nme) {
  88             } catch (ClassNotFoundException cfe) {
  89                 //Fail silently
  90             }
  91             if (linesToScroll [0] != -1 /*OS.WHEEL_PAGESCROLL*/) {
  92                 divisor = linesToScroll [0];
  93             }
  94         } else {
  95             if ("gtk".equals(SWT.getPlatform())) {
  96                 divisor = 3;
  97             }
  98         }
  99         return -e.count / Math.max(1, divisor);
 100     }
 101 
 102     static int keyIDToEmbedKeyType(int id) {
 103         switch (id) {
 104             case SWT.KeyDown:
 105                 return AbstractEvents.KEYEVENT_PRESSED;
 106             case SWT.KeyUp:
 107                 return AbstractEvents.KEYEVENT_RELEASED;
 108 //            case KeyEvent.KEY_TYPED:
 109 //                return AbstractEvents.KEYEVENT_TYPED;
 110         }
 111         return 0;
 112     }
 113 
 114     static final int [] [] KeyTable = {
 115 
 116         {0x0 /*KeyEvent.VK_UNDEFINED*/,     SWT.NULL},
 117 
 118         // SWT only
 119         {'\n' /*KeyEvent.VK_?????*/,         SWT.CR},
 120 
 121         // Misc
 122         {'\n' /*KeyEvent.VK_ENTER*/,         SWT.LF},
 123         {'\b' /*KeyEvent.VK_BACKSPACE*/,     SWT.BS},
 124         {'\t' /*KeyEvent.VK_TAB*/,           SWT.TAB},
 125 //      {KeyEvent.VK_CANCEL         SWT.???},
 126 //      {KeyEvent.VK_CLEAR          SWT.???},
 127 //      {KeyEvent.VK_PAUSE          SWT.???},
 128         {0x1B /*KeyEvent.VK_ESCAPE*/,        SWT.ESC},
 129         {0x20 /*KeyEvent.VK_SPACE*/,         0x20},
 130         {0x7F /*KeyEvent.VK_DELETE*/,        SWT.DEL},
 131 //      {KeyEvent.VK_PRINTSCREEN    SWT.???;
 132         {0x9B /*KeyEvent.VK_INSERT*/,        SWT.INSERT},
 133         {0x9C /*KeyEvent.VK_HELP*/,          SWT.HELP},
 134 
 135         // Modifiers
 136         {0x10 /*KeyEvent.VK_SHIFT*/,         SWT.SHIFT},
 137         {0x11 /*KeyEvent.VK_CONTROL*/,       SWT.CONTROL},
 138         {0x12 /*KeyEvent.VK_ALT*/,           SWT.ALT},
 139         {0x020C /*(KeyEvent.VK_WINDOWS*/,       SWT.COMMAND},
 140  //     {KeyEvent.VK_CONTEXT_MENU,  SWT.???},
 141         {0x14 /*KeyEvent.VK_CAPS_LOCK*/,     SWT.CAPS_LOCK},
 142         {0x90 /*KeyEvent.VK_NUM_LOCK*/,      SWT.NUM_LOCK},
 143         {0x91 /*KeyEvent.VK_SCROLL_LOCK*/,   SWT.SCROLL_LOCK},
 144 
 145         // Navigation keys
 146         {0x21 /*KeyEvent.VK_PAGE_UP*/,       SWT.PAGE_UP},
 147         {0x22 /*KeyEvent.VK_PAGE_DOWN*/,     SWT.PAGE_DOWN},
 148         {0x23 /*KeyEvent.VK_END*/,           SWT.END},
 149         {0x24 /*KeyEvent.VK_HOME*/,          SWT.HOME},
 150         {0x25 /*KeyEvent.VK_LEFT*/,          SWT.ARROW_LEFT},
 151         {0x26 /*KeyEvent.VK_UP*/,            SWT.ARROW_UP},
 152         {0x27 /*KeyEvent.VK_RIGHT*/,         SWT.ARROW_RIGHT},
 153         {0x28 /*KeyEvent.VK_DOWN*/,          SWT.ARROW_DOWN},
 154 
 155         // Misc 2
 156         // NOTE: suspect this only works for English keyboard
 157         {0x2C /*KeyEvent.VK_COMMA*/,                 ','}, // ','
 158         {0x2D /*KeyEvent.VK_MINUS*/,                 '-'}, // '-'
 159         {0x2E /*KeyEvent.VK_PERIOD*/,                '.'}, // '.'
 160         {0x2F /*KeyEvent.VK_SLASH*/,                 '/'}, // '/'
 161         {0x3B /*KeyEvent.VK_SEMICOLON*/,             ';'}, // ';'
 162         {0x3D /*KeyEvent.VK_EQUALS*/,                '='}, // '='
 163         {0x5B /*KeyEvent.VK_OPEN_BRACKET*/,          '['}, // '['
 164         {0x5C /*KeyEvent.VK_BACK_SLASH*/,            '\\'}, // '\'
 165         {0x5D /*KeyEvent.VK_CLOSE_BRACKET*/,         ']'}, // ']'
 166 
 167         // Numeric key pad keys
 168         {0x6A /*KeyEvent.VK_MULTIPLY*/,     SWT.KEYPAD_MULTIPLY}, // '*'
 169         {0x6B /*KeyEvent.VK_ADD*/,          SWT.KEYPAD_ADD}, // '+'
 170 //        {0x6C /*KeyEvent.VK_SEPARATOR*/,    SWT.???},
 171         {0x6D /*KeyEvent.VK_SUBTRACT*/,     SWT.KEYPAD_SUBTRACT},
 172         {0x6E /*KeyEvent.VK_DECIMAL*/,      SWT.KEYPAD_DECIMAL},
 173         {0x6F /*KeyEvent.VK_DIVIDE*/,       SWT.KEYPAD_DIVIDE},
 174 //        {0x?? /*KeyEvent.VK_????*/,         SWT.KEYPAD_EQUAL},
 175 //        {0x?? /*KeyEvent.VK_????*/,         SWT.KEYPAD_CR},
 176 
 177         {0x96 /*KeyEvent.VK_AMPERSAND*/,             '@'},
 178         {0x97 /*KeyEvent.VK_ASTERISK*/,              '*'},
 179 
 180         {0x98 /*KeyEvent.VK_DOUBLE_QUOTE*/,          '"'}, // '"'
 181         {0x99 /*KeyEvent.VK_LESS*/,                  '<'}, // '<'
 182         {0xa0 /*KeyEvent.VK_GREATER*/,               '>'}, // '>'
 183         {0xa1 /*KeyEvent.VK_BRACELEFT*/,             '{'}, // '{'
 184         {0xa2 /*KeyEvent.VK_BRACERIGHT*/,            '}'}, // '}'
 185         {0xC0 /*KeyEvent.VK_BACK_QUOTE*/,            '`'}, // '`'
 186         {0xDE /*KeyEvent.VK_QUOTE*/,                 '\''}, // '''
 187         {0x0200 /*KeyEvent.VK_AT*/,                    '@'}, // '@'
 188         {0x0201 /*KeyEvent.VK_COLON*/,                 ':'}, // ':'
 189         {0x0202 /*KeyEvent.VK_CIRCUMFLEX*/,            '^'}, // '^'
 190         {0x0203 /*KeyEvent.VK_DOLLAR*/,                '$'}, // '$'
 191 //        {KeyEvent.VK_EURO_SIGN,             0x0204},
 192         {0x0205 /*KeyEvent.VK_EXCLAMATION*/,           '!'}, // '!'
 193 //        {KeyEvent.VK_INV_EXCLAMATION,       0x0206},
 194         {0x0207 /*KeyEvent.VK_LEFT_PARENTHESIS*/,      '('}, // '('
 195         {0x0208 /*KeyEvent.VK_NUMBER_SIGN*/,           '#'}, // '#'
 196         {0x0209 /*KeyEvent.VK_PLUS*/,                  '+'}, // '+'
 197         {0x020A /*KeyEvent.VK_RIGHT_PARENTHESIS*/,      ')'}, // ')'
 198         {0x020B /*KeyEvent.VK_UNDERSCORE*/,             '_'}, // '_'
 199 
 200         // Numeric keys
 201         // NOTE: suspect this only works for English keyboard
 202         {0x30 /*KeyEvent.VK_0*/, '0'}, // '0'
 203         {0x31 /*KeyEvent.VK_1*/, '1'}, // '1'
 204         {0x32 /*KeyEvent.VK_2*/, '2'}, // '2'
 205         {0x33 /*KeyEvent.VK_3*/, '3'}, // '3'
 206         {0x34 /*KeyEvent.VK_4*/, '4'}, // '4'
 207         {0x35 /*KeyEvent.VK_5*/, '5'}, // '5'
 208         {0x36 /*KeyEvent.VK_6*/, '6'}, // '6'
 209         {0x37 /*KeyEvent.VK_7*/, '7'}, // '7'
 210         {0x38 /*KeyEvent.VK_8*/, '8'}, // '8'
 211         {0x39 /*KeyEvent.VK_9*/, '9'}, // '9'
 212 
 213         // Alpha keys
 214         // NOTE: suspect this only works for English keyboard
 215         {0x41 /*KeyEvent.VK_A*/, 'a'}, // 'A'
 216         {0x42 /*KeyEvent.VK_B*/, 'b'}, // 'B'
 217         {0x43 /*KeyEvent.VK_C*/, 'c'}, // 'C'
 218         {0x44 /*KeyEvent.VK_D*/, 'd'}, // 'D'
 219         {0x45 /*KeyEvent.VK_E*/, 'e'}, // 'E'
 220         {0x46 /*KeyEvent.VK_F*/, 'f'}, // 'F'
 221         {0x47 /*KeyEvent.VK_G*/, 'g'}, // 'G'
 222         {0x48 /*KeyEvent.VK_H*/, 'h'}, // 'H'
 223         {0x49 /*KeyEvent.VK_I*/, 'i'}, // 'I'
 224         {0x4A /*KeyEvent.VK_J*/, 'j'}, // 'J'
 225         {0x4B /*KeyEvent.VK_K*/, 'k'}, // 'K'
 226         {0x4C /*KeyEvent.VK_L*/, 'l'}, // 'L'
 227         {0x4D /*KeyEvent.VK_M*/, 'm'}, // 'M'
 228         {0x4E /*KeyEvent.VK_N*/, 'n'}, // 'N'
 229         {0x4F /*KeyEvent.VK_O*/, 'o'}, // 'O'
 230         {0x50 /*KeyEvent.VK_P*/, 'p'}, // 'P'
 231         {0x51 /*KeyEvent.VK_Q*/, 'q'}, // 'Q'
 232         {0x52 /*KeyEvent.VK_R*/, 'r'}, // 'R'
 233         {0x53 /*KeyEvent.VK_S*/, 's'}, // 'S'
 234         {0x54 /*KeyEvent.VK_T*/, 't'}, // 'T'
 235         {0x55 /*KeyEvent.VK_U*/, 'u'}, // 'U'
 236         {0x56 /*KeyEvent.VK_V*/, 'v'}, // 'V'
 237         {0x57 /*KeyEvent.VK_W*/, 'w'}, // 'W'
 238         {0x58 /*KeyEvent.VK_X*/, 'x'}, // 'X'
 239         {0x59 /*KeyEvent.VK_Y*/, 'y'}, // 'Y'
 240         {0x5A /*KeyEvent.VK_Z*/, 'z'}, // 'Z'
 241 
 242         // Numpad keys
 243         {0x60 /*KeyEvent.VK_NUMPAD0*/,   SWT.KEYPAD_0},
 244         {0x61 /*KeyEvent.VK_NUMPAD1*/,   SWT.KEYPAD_1},
 245         {0x62 /*KeyEvent.VK_NUMPAD2*/,   SWT.KEYPAD_2},
 246         {0x63 /*KeyEvent.VK_NUMPAD3*/,   SWT.KEYPAD_3},
 247         {0x64 /*KeyEvent.VK_NUMPAD4*/,   SWT.KEYPAD_4},
 248         {0x65 /*KeyEvent.VK_NUMPAD5*/,   SWT.KEYPAD_5},
 249         {0x66 /*KeyEvent.VK_NUMPAD6*/,   SWT.KEYPAD_6},
 250         {0x67 /*KeyEvent.VK_NUMPAD7*/,   SWT.KEYPAD_7},
 251         {0x68 /*KeyEvent.VK_NUMPAD8*/,   SWT.KEYPAD_8},
 252         {0x69 /*KeyEvent.VK_NUMPAD9*/,   SWT.KEYPAD_9},
 253 
 254         // Function keys
 255         {0x70 /*KeyEvent.VK_F1*/,    SWT.F1},
 256         {0x71 /*KeyEvent.VK_F2*/,    SWT.F2},
 257         {0x72 /*KeyEvent.VK_F3*/,    SWT.F3},
 258         {0x73 /*KeyEvent.VK_F4*/,    SWT.F4},
 259         {0x74 /*KeyEvent.VK_F5*/,    SWT.F5},
 260         {0x75 /*KeyEvent.VK_F6*/,    SWT.F6},
 261         {0x76 /*KeyEvent.VK_F7*/,    SWT.F7},
 262         {0x77 /*KeyEvent.VK_F8*/,    SWT.F8},
 263         {0x78 /*KeyEvent.VK_F9*/,    SWT.F9},
 264         {0x79 /*KeyEvent.VK_F10*/,   SWT.F10},
 265         {0x7A /*KeyEvent.VK_F11*/,   SWT.F11},
 266         {0x7B /*KeyEvent.VK_F12*/,   SWT.F12},
 267     };
 268 
 269     // RT-27940: map these to Fx keys
 270 //    /* Numeric Keypad Keys */
 271 //    {KeyEvent.VK_MULTIPLY,    SWT.KEYPAD_MULTIPLY},
 272 //    {KeyEvent.VK_ADD,         SWT.KEYPAD_ADD},
 273 //    {KeyEvent.VK_RETURN,      SWT.KEYPAD_CR},
 274 //    {KeyEvent.VK_SUBTRACT,    SWT.KEYPAD_SUBTRACT},
 275 //    {KeyEvent.VK_DECIMAL,     SWT.KEYPAD_DECIMAL},
 276 //    {KeyEvent.VK_DIVIDE,      SWT.KEYPAD_DIVIDE},
 277 ////  {KeyEvent.VK_????,        SWT.KEYPAD_EQUAL},
 278 
 279     static int keyCodeToEmbedKeyCode(int keyCode) {
 280         for (int i=0; i<KeyTable.length; i++) {
 281             if (KeyTable [i] [1] == keyCode) return KeyTable [i] [0];
 282         }
 283         return 0;
 284     }
 285 
 286     static int keyModifiersToEmbedKeyModifiers(int extModifiers) {
 287         int embedModifiers = 0;
 288         if ((extModifiers & SWT.SHIFT) != 0) {
 289             embedModifiers |= AbstractEvents.MODIFIER_SHIFT;
 290         }
 291         if ((extModifiers & SWT.CTRL) != 0) {
 292             embedModifiers |= AbstractEvents.MODIFIER_CONTROL;
 293         }
 294         if ((extModifiers & SWT.ALT) != 0) {
 295             embedModifiers |= AbstractEvents.MODIFIER_ALT;
 296         }
 297         // NOTE: can't get Windows key from SWT
 298         if ((extModifiers & SWT.COMMAND) != 0) {
 299             embedModifiers |= AbstractEvents.MODIFIER_META;
 300         }
 301         return embedModifiers;
 302     }
 303 }