Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / events / cocoa / events_mac.mm
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/events/event_utils.h"
6
7 #include <Cocoa/Cocoa.h>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "ui/events/cocoa/cocoa_event_utils.h"
13 #include "ui/events/event_utils.h"
14 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
15 #include "ui/gfx/point.h"
16 #include "ui/gfx/vector2d.h"
17
18 namespace ui {
19
20 void UpdateDeviceList() {
21   NOTIMPLEMENTED();
22 }
23
24 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
25   NSEventType type = [native_event type];
26   switch (type) {
27     case NSKeyDown:
28       return ET_KEY_PRESSED;
29     case NSKeyUp:
30       return ET_KEY_RELEASED;
31     case NSLeftMouseDown:
32     case NSRightMouseDown:
33     case NSOtherMouseDown:
34       return ET_MOUSE_PRESSED;
35     case NSLeftMouseUp:
36     case NSRightMouseUp:
37     case NSOtherMouseUp:
38       return ET_MOUSE_RELEASED;
39     case NSLeftMouseDragged:
40     case NSRightMouseDragged:
41     case NSOtherMouseDragged:
42       return ET_MOUSE_DRAGGED;
43     case NSMouseMoved:
44     case NSScrollWheel:
45       return ET_MOUSEWHEEL;
46     case NSMouseEntered:
47       return ET_MOUSE_ENTERED;
48     case NSMouseExited:
49       return ET_MOUSE_EXITED;
50     case NSEventTypeSwipe:
51       return ET_SCROLL_FLING_START;
52     case NSFlagsChanged:
53     case NSAppKitDefined:
54     case NSSystemDefined:
55     case NSApplicationDefined:
56     case NSPeriodic:
57     case NSCursorUpdate:
58     case NSTabletPoint:
59     case NSTabletProximity:
60     case NSEventTypeGesture:
61     case NSEventTypeMagnify:
62     case NSEventTypeRotate:
63     case NSEventTypeBeginGesture:
64     case NSEventTypeEndGesture:
65       NOTIMPLEMENTED() << type;
66       break;
67     default:
68       NOTIMPLEMENTED() << type;
69       break;
70   }
71   return ET_UNKNOWN;
72 }
73
74 int EventFlagsFromNative(const base::NativeEvent& event) {
75   NSUInteger modifiers = [event modifierFlags];
76   return EventFlagsFromNSEventWithModifiers(event, modifiers);
77 }
78
79 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
80   NSTimeInterval since_system_startup = [native_event timestamp];
81   // Truncate to extract seconds before doing floating point arithmetic.
82   int64_t seconds = since_system_startup;
83   since_system_startup -= seconds;
84   int64_t microseconds = since_system_startup * 1000000;
85   return base::TimeDelta::FromSeconds(seconds) +
86       base::TimeDelta::FromMicroseconds(microseconds);
87 }
88
89 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
90   if (![native_event window]) {
91     NOTIMPLEMENTED();  // Point will be in screen coordinates.
92     return gfx::Point();
93   }
94   NSPoint location = [native_event locationInWindow];
95   return gfx::Point(location.x,
96                     NSHeight([[native_event window] frame]) - location.y);
97 }
98
99 gfx::Point EventSystemLocationFromNative(
100     const base::NativeEvent& native_event) {
101   NOTIMPLEMENTED();
102   return gfx::Point();
103 }
104
105 int EventButtonFromNative(const base::NativeEvent& native_event) {
106   NOTIMPLEMENTED();
107   return 0;
108 }
109
110 int GetChangedMouseButtonFlagsFromNative(
111     const base::NativeEvent& native_event) {
112   NSEventType type = [native_event type];
113   switch (type) {
114     case NSLeftMouseDown:
115     case NSLeftMouseUp:
116     case NSLeftMouseDragged:
117       return EF_LEFT_MOUSE_BUTTON;
118     case NSRightMouseDown:
119     case NSRightMouseUp:
120     case NSRightMouseDragged:
121       return EF_RIGHT_MOUSE_BUTTON;
122     case NSOtherMouseDown:
123     case NSOtherMouseUp:
124     case NSOtherMouseDragged:
125       return EF_MIDDLE_MOUSE_BUTTON;
126   }
127   return 0;
128 }
129
130 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
131   // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
132   // values when scrolling up or to the left. Scrolling quickly results in a
133   // higher delta per click, up to about 15.0. (Quartz documentation suggests
134   // +/-10).
135   // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
136   const CGFloat kWheelDeltaMultiplier = 1000;
137   return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
138                        kWheelDeltaMultiplier * [event deltaY]);
139 }
140
141 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
142   return [event copy];
143 }
144
145 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
146   [event release];
147 }
148
149 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
150   NOTIMPLEMENTED();
151 }
152
153 int GetTouchId(const base::NativeEvent& native_event) {
154   NOTIMPLEMENTED();
155   return 0;
156 }
157
158 float GetTouchRadiusX(const base::NativeEvent& native_event) {
159   NOTIMPLEMENTED();
160   return 0.f;
161 }
162
163 float GetTouchRadiusY(const base::NativeEvent& native_event) {
164   NOTIMPLEMENTED();
165   return 0.f;
166 }
167
168 float GetTouchAngle(const base::NativeEvent& native_event) {
169   NOTIMPLEMENTED();
170   return 0.f;
171 }
172
173 float GetTouchForce(const base::NativeEvent& native_event) {
174   NOTIMPLEMENTED();
175   return 0.f;
176 }
177
178 bool GetScrollOffsets(const base::NativeEvent& native_event,
179                       float* x_offset,
180                       float* y_offset,
181                       float* x_offset_ordinal,
182                       float* y_offset_ordinal,
183                       int* finger_count) {
184   NOTIMPLEMENTED();
185   return false;
186 }
187
188 bool GetFlingData(const base::NativeEvent& native_event,
189                   float* vx,
190                   float* vy,
191                   float* vx_ordinal,
192                   float* vy_ordinal,
193                   bool* is_cancel) {
194   NOTIMPLEMENTED();
195   return false;
196 }
197
198 bool GetGestureTimes(const base::NativeEvent& native_event,
199                      double* start_time,
200                      double* end_time) {
201   NOTIMPLEMENTED();
202   return false;
203 }
204
205 void SetNaturalScroll(bool enabled) {
206   NOTIMPLEMENTED();
207 }
208
209 bool IsNaturalScrollEnabled() {
210   NOTIMPLEMENTED();
211   return false;
212 }
213
214 bool IsTouchpadEvent(const base::NativeEvent& native_event) {
215   NOTIMPLEMENTED();
216   return false;
217 }
218
219 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
220   return KeyboardCodeFromNSEvent(native_event);
221 }
222
223 const char* CodeFromNative(const base::NativeEvent& native_event) {
224   return CodeFromNSEvent(native_event);
225 }
226
227 }  // namespace ui