1 /*
2 * Copyright (c) 2011, 2014, 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
37
38 /**
39 * Translates NSEvents/NPCocoaEvents into AWT events.
40 */
41 final class CPlatformResponder {
42
43 private final PlatformEventNotifier eventNotifier;
44 private final boolean isNpapiCallback;
45 private int lastKeyPressCode = KeyEvent.VK_UNDEFINED;
46
47 CPlatformResponder(final PlatformEventNotifier eventNotifier,
48 final boolean isNpapiCallback) {
49 this.eventNotifier = eventNotifier;
50 this.isNpapiCallback = isNpapiCallback;
51 }
52
53 /**
54 * Handles mouse events.
55 */
56 void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
57 int clickCount, int x, int y, int absoluteX,
58 int absoluteY) {
59 final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
60 if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
61 || buttonNumber > tk.getNumberOfButtons() - 1) {
62 return;
63 }
64
65 int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
66 NSEvent.nsToJavaEventType(eventType);
67
68 int jbuttonNumber = MouseEvent.NOBUTTON;
69 int jclickCount = 0;
70
71 if (jeventType != MouseEvent.MOUSE_MOVED &&
72 jeventType != MouseEvent.MOUSE_ENTERED &&
73 jeventType != MouseEvent.MOUSE_EXITED)
74 {
75 jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
76 jclickCount = clickCount;
77 }
78
79 int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
80 modifierFlags);
81 boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
82
83 eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
84 x, y, absoluteX, absoluteY, jmodifiers, jclickCount,
85 jpopupTrigger, null);
86 }
87
88 /**
89 * Handles scroll events.
90 */
91 void handleScrollEvent(final int x, final int y, final int modifierFlags,
92 final double deltaX, final double deltaY) {
93 final int buttonNumber = CocoaConstants.kCGMouseButtonCenter;
94 int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
95 modifierFlags);
96 final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
97
98 // Vertical scroll.
99 if (!isShift && deltaY != 0.0) {
100 dispatchScrollEvent(x, y, jmodifiers, deltaY);
101 }
102 // Horizontal scroll or shirt+vertical scroll.
103 final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
104 if (delta != 0.0) {
105 jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
106 dispatchScrollEvent(x, y, jmodifiers, delta);
107 }
108 }
109
110 private void dispatchScrollEvent(final int x, final int y,
111 final int modifiers, final double delta) {
112 final long when = System.currentTimeMillis();
113 final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
114 final int scrollAmount = 1;
115 int wheelRotation = (int) delta;
116 int signum = (int) Math.signum(delta);
117 if (signum * delta < 1) {
118 wheelRotation = signum;
119 }
120 // invert the wheelRotation for the peer
121 eventNotifier.notifyMouseWheelEvent(when, x, y, modifiers, scrollType,
122 scrollAmount, -wheelRotation, -delta, null);
123 }
124
125 /**
126 * Handles key events.
127 */
128 void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
129 short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
130 boolean isFlagsChangedEvent =
131 isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
132 (eventType == CocoaConstants.NSFlagsChanged);
133
134 int jeventType = KeyEvent.KEY_PRESSED;
135 int jkeyCode = KeyEvent.VK_UNDEFINED;
136 int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
137 boolean postsTyped = false;
138
139 char testChar = KeyEvent.CHAR_UNDEFINED;
140 boolean isDeadChar = (chars!= null && chars.length() == 0);
141
142 if (isFlagsChangedEvent) {
|
1 /*
2 * Copyright (c) 2011, 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
37
38 /**
39 * Translates NSEvents/NPCocoaEvents into AWT events.
40 */
41 final class CPlatformResponder {
42
43 private final PlatformEventNotifier eventNotifier;
44 private final boolean isNpapiCallback;
45 private int lastKeyPressCode = KeyEvent.VK_UNDEFINED;
46
47 CPlatformResponder(final PlatformEventNotifier eventNotifier,
48 final boolean isNpapiCallback) {
49 this.eventNotifier = eventNotifier;
50 this.isNpapiCallback = isNpapiCallback;
51 }
52
53 /**
54 * Handles mouse events.
55 */
56 void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
57 int clickCount, int x, int y, int absX, int absY) {
58 final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
59 if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
60 || buttonNumber > tk.getNumberOfButtons() - 1) {
61 return;
62 }
63
64 int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
65 NSEvent.nsToJavaEventType(eventType);
66
67 int jbuttonNumber = MouseEvent.NOBUTTON;
68 int jclickCount = 0;
69
70 if (jeventType != MouseEvent.MOUSE_MOVED &&
71 jeventType != MouseEvent.MOUSE_ENTERED &&
72 jeventType != MouseEvent.MOUSE_EXITED)
73 {
74 jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
75 jclickCount = clickCount;
76 }
77
78 int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
79 modifierFlags);
80 boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
81
82 eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
83 x, y, absX, absY, jmodifiers, jclickCount,
84 jpopupTrigger, null);
85 }
86
87 /**
88 * Handles scroll events.
89 */
90 void handleScrollEvent(final int x, final int y, final int absX,
91 final int absY, final int modifierFlags,
92 final double deltaX, final double deltaY) {
93 final int buttonNumber = CocoaConstants.kCGMouseButtonCenter;
94 int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
95 modifierFlags);
96 final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
97
98 // Vertical scroll.
99 if (!isShift && deltaY != 0.0) {
100 dispatchScrollEvent(x, y, absX, absY, jmodifiers, deltaY);
101 }
102 // Horizontal scroll or shirt+vertical scroll.
103 final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
104 if (delta != 0.0) {
105 jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
106 dispatchScrollEvent(x, y, absX, absY, jmodifiers, delta);
107 }
108 }
109
110 private void dispatchScrollEvent(final int x, final int y, final int absX,
111 final int absY, final int modifiers,
112 final double delta) {
113 final long when = System.currentTimeMillis();
114 final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
115 final int scrollAmount = 1;
116 int wheelRotation = (int) delta;
117 int signum = (int) Math.signum(delta);
118 if (signum * delta < 1) {
119 wheelRotation = signum;
120 }
121 // invert the wheelRotation for the peer
122 eventNotifier.notifyMouseWheelEvent(when, x, y, absX, absY, modifiers,
123 scrollType, scrollAmount,
124 -wheelRotation, -delta, null);
125 }
126
127 /**
128 * Handles key events.
129 */
130 void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
131 short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
132 boolean isFlagsChangedEvent =
133 isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
134 (eventType == CocoaConstants.NSFlagsChanged);
135
136 int jeventType = KeyEvent.KEY_PRESSED;
137 int jkeyCode = KeyEvent.VK_UNDEFINED;
138 int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
139 boolean postsTyped = false;
140
141 char testChar = KeyEvent.CHAR_UNDEFINED;
142 boolean isDeadChar = (chars!= null && chars.length() == 0);
143
144 if (isFlagsChangedEvent) {
|