Add ICU message format support
[chromium-blink-merge.git] / content / common / input / synthetic_web_input_event_builders.cc
blobd2f4c393950b25753f6e6587841db253838df85e
1 // Copyright 2013 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.
5 #include "content/common/input/synthetic_web_input_event_builders.h"
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
9 #include "ui/events/base_event_utils.h"
10 #include "ui/events/keycodes/keyboard_codes.h"
12 namespace content {
14 using blink::WebInputEvent;
15 using blink::WebKeyboardEvent;
16 using blink::WebGestureEvent;
17 using blink::WebMouseEvent;
18 using blink::WebMouseWheelEvent;
19 using blink::WebTouchEvent;
20 using blink::WebTouchPoint;
22 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
23 blink::WebInputEvent::Type type) {
24 WebMouseEvent result;
25 result.type = type;
26 return result;
29 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
30 blink::WebInputEvent::Type type,
31 int window_x,
32 int window_y,
33 int modifiers) {
34 DCHECK(WebInputEvent::isMouseEventType(type));
35 WebMouseEvent result = Build(type);
36 result.x = window_x;
37 result.y = window_y;
38 result.windowX = window_x;
39 result.windowY = window_y;
40 result.modifiers = modifiers;
42 if (type == WebInputEvent::MouseDown || type == WebInputEvent::MouseUp)
43 result.button = WebMouseEvent::ButtonLeft;
44 else
45 result.button = WebMouseEvent::ButtonNone;
47 return result;
50 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(
51 WebMouseWheelEvent::Phase phase) {
52 WebMouseWheelEvent result;
53 result.type = WebInputEvent::MouseWheel;
54 result.phase = phase;
55 return result;
58 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(float dx,
59 float dy,
60 int modifiers,
61 bool precise) {
62 WebMouseWheelEvent result;
63 result.type = WebInputEvent::MouseWheel;
64 result.deltaX = dx;
65 result.deltaY = dy;
66 if (dx)
67 result.wheelTicksX = dx > 0.0f ? 1.0f : -1.0f;
68 if (dy)
69 result.wheelTicksY = dy > 0.0f ? 1.0f : -1.0f;
70 result.modifiers = modifiers;
71 result.hasPreciseScrollingDeltas = precise;
72 result.canScroll = true;
73 return result;
76 WebKeyboardEvent SyntheticWebKeyboardEventBuilder::Build(
77 WebInputEvent::Type type) {
78 DCHECK(WebInputEvent::isKeyboardEventType(type));
79 WebKeyboardEvent result;
80 result.type = type;
81 result.windowsKeyCode = ui::VKEY_L; // non-null made up value.
82 return result;
85 WebGestureEvent SyntheticWebGestureEventBuilder::Build(
86 WebInputEvent::Type type,
87 blink::WebGestureDevice source_device) {
88 DCHECK(WebInputEvent::isGestureEventType(type));
89 WebGestureEvent result;
90 result.type = type;
91 result.sourceDevice = source_device;
92 if (type == WebInputEvent::GestureTap ||
93 type == WebInputEvent::GestureTapUnconfirmed ||
94 type == WebInputEvent::GestureDoubleTap) {
95 result.data.tap.tapCount = 1;
96 result.data.tap.width = 10;
97 result.data.tap.height = 10;
99 return result;
102 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollBegin(
103 float dx_hint,
104 float dy_hint) {
105 WebGestureEvent result = Build(WebInputEvent::GestureScrollBegin,
106 blink::WebGestureDeviceTouchscreen);
107 result.data.scrollBegin.deltaXHint = dx_hint;
108 result.data.scrollBegin.deltaYHint = dy_hint;
109 return result;
112 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollUpdate(
113 float dx,
114 float dy,
115 int modifiers,
116 blink::WebGestureDevice source_device) {
117 WebGestureEvent result =
118 Build(WebInputEvent::GestureScrollUpdate, source_device);
119 result.data.scrollUpdate.deltaX = dx;
120 result.data.scrollUpdate.deltaY = dy;
121 result.modifiers = modifiers;
122 return result;
125 WebGestureEvent SyntheticWebGestureEventBuilder::BuildPinchUpdate(
126 float scale,
127 float anchor_x,
128 float anchor_y,
129 int modifiers,
130 blink::WebGestureDevice source_device) {
131 WebGestureEvent result =
132 Build(WebInputEvent::GesturePinchUpdate, source_device);
133 result.data.pinchUpdate.scale = scale;
134 result.x = anchor_x;
135 result.y = anchor_y;
136 result.globalX = anchor_x;
137 result.globalY = anchor_y;
138 result.modifiers = modifiers;
139 return result;
142 WebGestureEvent SyntheticWebGestureEventBuilder::BuildFling(
143 float velocity_x,
144 float velocity_y,
145 blink::WebGestureDevice source_device) {
146 WebGestureEvent result = Build(WebInputEvent::GestureFlingStart,
147 source_device);
148 result.data.flingStart.velocityX = velocity_x;
149 result.data.flingStart.velocityY = velocity_y;
150 return result;
153 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {
154 uniqueTouchEventId = ui::GetNextTouchEventId();
155 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks());
158 void SyntheticWebTouchEvent::ResetPoints() {
159 int point = 0;
160 for (unsigned int i = 0; i < touchesLength; ++i) {
161 if (touches[i].state == WebTouchPoint::StateReleased)
162 continue;
164 touches[point] = touches[i];
165 touches[point].state = WebTouchPoint::StateStationary;
166 ++point;
168 touchesLength = point;
169 type = WebInputEvent::Undefined;
170 causesScrollingIfUncanceled = false;
171 uniqueTouchEventId = ui::GetNextTouchEventId();
174 int SyntheticWebTouchEvent::PressPoint(float x, float y) {
175 if (touchesLength == touchesLengthCap)
176 return -1;
177 WebTouchPoint& point = touches[touchesLength];
178 point.id = touchesLength;
179 point.position.x = point.screenPosition.x = x;
180 point.position.y = point.screenPosition.y = y;
181 point.state = WebTouchPoint::StatePressed;
182 point.radiusX = point.radiusY = 1.f;
183 point.rotationAngle = 1.f;
184 point.force = 1.f;
185 ++touchesLength;
186 WebTouchEventTraits::ResetType(
187 WebInputEvent::TouchStart, timeStampSeconds, this);
188 return point.id;
191 void SyntheticWebTouchEvent::MovePoint(int index, float x, float y) {
192 CHECK_GE(index, 0);
193 CHECK_LT(index, touchesLengthCap);
194 // Always set this bit to avoid otherwise unexpected touchmove suppression.
195 // The caller can opt-out explicitly, if necessary.
196 causesScrollingIfUncanceled = true;
197 WebTouchPoint& point = touches[index];
198 point.position.x = point.screenPosition.x = x;
199 point.position.y = point.screenPosition.y = y;
200 touches[index].state = WebTouchPoint::StateMoved;
201 WebTouchEventTraits::ResetType(
202 WebInputEvent::TouchMove, timeStampSeconds, this);
205 void SyntheticWebTouchEvent::ReleasePoint(int index) {
206 CHECK_GE(index, 0);
207 CHECK_LT(index, touchesLengthCap);
208 touches[index].state = WebTouchPoint::StateReleased;
209 WebTouchEventTraits::ResetType(
210 WebInputEvent::TouchEnd, timeStampSeconds, this);
213 void SyntheticWebTouchEvent::CancelPoint(int index) {
214 CHECK_GE(index, 0);
215 CHECK_LT(index, touchesLengthCap);
216 touches[index].state = WebTouchPoint::StateCancelled;
217 WebTouchEventTraits::ResetType(
218 WebInputEvent::TouchCancel, timeStampSeconds, this);
221 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp) {
222 timeStampSeconds = timestamp.InSecondsF();
225 } // namespace content