OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #ifndef UI_BASE_EVENT_H_ | |
6 #define UI_BASE_EVENT_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/event_types.h" | |
11 #include "base/logging.h" | |
12 #include "base/time.h" | |
13 #include "ui/base/dragdrop/os_exchange_data.h" | |
14 #include "ui/base/events.h" | |
15 #include "ui/base/gestures/gesture_types.h" | |
16 #include "ui/base/keycodes/keyboard_codes.h" | |
17 #include "ui/base/ui_export.h" | |
18 #include "ui/gfx/point.h" | |
19 | |
20 namespace ui { | |
21 class Transform; | |
22 | |
23 class UI_EXPORT Event { | |
24 public: | |
25 virtual ~Event(); | |
26 | |
27 // For testing. | |
28 class TestApi { | |
29 public: | |
30 explicit TestApi(Event* event) : event_(event) {} | |
31 | |
32 void set_time_stamp(const base::TimeDelta& time_stamp) { | |
33 event_->time_stamp_ = time_stamp; | |
34 } | |
35 | |
36 private: | |
37 TestApi(); | |
38 Event* event_; | |
39 }; | |
40 | |
41 const base::NativeEvent& native_event() const { return native_event_; } | |
42 EventType type() const { return type_; } | |
43 // time_stamp represents time since machine was booted. | |
44 const base::TimeDelta& time_stamp() const { return time_stamp_; } | |
45 int flags() const { return flags_; } | |
46 | |
47 // This is only intended to be used externally by classes that are modifying | |
48 // events in EventFilter::PreHandleKeyEvent(). | |
49 void set_flags(int flags) { flags_ = flags; } | |
50 | |
51 // The following methods return true if the respective keys were pressed at | |
52 // the time the event was created. | |
53 bool IsShiftDown() const { return (flags_ & EF_SHIFT_DOWN) != 0; } | |
54 bool IsControlDown() const { return (flags_ & EF_CONTROL_DOWN) != 0; } | |
55 bool IsCapsLockDown() const { return (flags_ & EF_CAPS_LOCK_DOWN) != 0; } | |
56 bool IsAltDown() const { return (flags_ & EF_ALT_DOWN) != 0; } | |
57 | |
58 // Returns true if the event has a valid |native_event_|. | |
59 bool HasNativeEvent() const; | |
60 | |
61 protected: | |
62 Event(EventType type, int flags); | |
63 Event(const base::NativeEvent& native_event, EventType type, int flags); | |
64 Event(const Event& copy); | |
65 void set_type(EventType type) { type_ = type; } | |
66 void set_delete_native_event(bool delete_native_event) { | |
67 delete_native_event_ = delete_native_event; | |
68 } | |
69 void set_time_stamp(base::TimeDelta time_stamp) { time_stamp_ = time_stamp; } | |
70 | |
71 private: | |
72 void operator=(const Event&); | |
73 | |
74 // Safely initializes the native event members of this class. | |
75 void Init(); | |
76 void InitWithNativeEvent(const base::NativeEvent& native_event); | |
77 | |
78 base::NativeEvent native_event_; | |
79 EventType type_; | |
80 base::TimeDelta time_stamp_; | |
81 int flags_; | |
82 bool delete_native_event_; | |
83 }; | |
84 | |
85 class UI_EXPORT LocatedEvent : public Event { | |
86 public: | |
87 // For testing. | |
88 class TestApi : public Event::TestApi { | |
89 public: | |
90 explicit TestApi(LocatedEvent* located_event) | |
91 : Event::TestApi(located_event), | |
92 located_event_(located_event) {} | |
93 | |
94 void set_location(const gfx::Point& location) { | |
95 located_event_->location_ = location; | |
96 } | |
97 | |
98 private: | |
99 TestApi(); | |
100 LocatedEvent* located_event_; | |
101 }; | |
102 | |
103 virtual ~LocatedEvent(); | |
104 | |
105 int x() const { return location_.x(); } | |
106 int y() const { return location_.y(); } | |
107 gfx::Point location() const { return location_; } | |
108 gfx::Point root_location() const { return root_location_; } | |
109 | |
110 // Applies |root_transform| to the event. | |
111 // This is applied to both |location_| and |root_location_|. | |
112 virtual void UpdateForRootTransform(const Transform& root_transform); | |
113 | |
114 protected: | |
115 explicit LocatedEvent(const base::NativeEvent& native_event); | |
116 | |
117 // Create a new LocatedEvent which is identical to the provided model. | |
118 // If source / target windows are provided, the model location will be | |
119 // converted from |source| coordinate system to |target| coordinate system. | |
120 template <class T> | |
121 LocatedEvent(const LocatedEvent& model, T* source, T* target) | |
122 : Event(model), | |
123 location_(model.location_), | |
124 root_location_(model.root_location_) { | |
125 if (target && target != source) | |
126 T::ConvertPointToTarget(source, target, &location_); | |
127 } | |
128 | |
129 // Used for synthetic events in testing. | |
130 LocatedEvent(EventType type, | |
131 const gfx::Point& location, | |
132 const gfx::Point& root_location, | |
133 int flags); | |
134 | |
135 gfx::Point location_; | |
136 | |
137 gfx::Point root_location_; | |
138 | |
139 private: | |
140 DISALLOW_COPY_AND_ASSIGN(LocatedEvent); | |
141 }; | |
142 | |
143 class UI_EXPORT MouseEvent : public LocatedEvent { | |
144 public: | |
145 explicit MouseEvent(const base::NativeEvent& native_event); | |
146 | |
147 // Create a new MouseEvent based on the provided model. | |
148 // Uses the provided |type| and |flags| for the new event. | |
149 // If source / target windows are provided, the model location will be | |
150 // converted from |source| coordinate system to |target| coordinate system. | |
151 template <class T> | |
152 MouseEvent(const MouseEvent& model, T* source, T* target) | |
153 : LocatedEvent(model, source, target) { | |
154 } | |
155 | |
156 template <class T> | |
157 MouseEvent(const MouseEvent& model, | |
158 T* source, | |
159 T* target, | |
160 EventType type, | |
161 int flags) | |
162 : LocatedEvent(model, source, target) { | |
163 set_type(type); | |
164 set_flags(flags); | |
165 } | |
166 | |
167 // Used for synthetic events in testing and by the gesture recognizer. | |
168 MouseEvent(EventType type, | |
169 const gfx::Point& location, | |
170 const gfx::Point& root_location, | |
171 int flags); | |
172 | |
173 // Compares two mouse down events and returns true if the second one should | |
174 // be considered a repeat of the first. | |
175 static bool IsRepeatedClickEvent( | |
176 const MouseEvent& event1, | |
177 const MouseEvent& event2); | |
178 | |
179 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise. | |
180 int GetClickCount() const; | |
181 | |
182 // Set the click count for a mousedown message. Can be 1, 2 or 3. | |
183 void SetClickCount(int click_count); | |
184 | |
185 private: | |
186 gfx::Point root_location_; | |
187 | |
188 static MouseEvent* last_click_event_; | |
189 // Returns the repeat count based on the previous mouse click, if it is | |
190 // recent enough and within a small enough distance. | |
191 static int GetRepeatCount(const MouseEvent& click_event); | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(MouseEvent); | |
194 }; | |
195 | |
196 // TODO(beng): rename to TouchEvent after conversion is complete. | |
197 class UI_EXPORT TouchEventImpl : public LocatedEvent, | |
198 public TouchEvent { | |
199 public: | |
200 explicit TouchEventImpl(const base::NativeEvent& native_event); | |
201 | |
202 // Create a new TouchEventImpl which is identical to the provided model. | |
203 // If source / target windows are provided, the model location will be | |
204 // converted from |source| coordinate system to |target| coordinate system. | |
205 template <class T> | |
206 TouchEventImpl(const TouchEventImpl& model, T* source, T* target) | |
207 : LocatedEvent(model, source, target), | |
208 touch_id_(model.touch_id_), | |
209 radius_x_(model.radius_x_), | |
210 radius_y_(model.radius_y_), | |
211 rotation_angle_(model.rotation_angle_), | |
212 force_(model.force_) { | |
213 } | |
214 | |
215 TouchEventImpl(EventType type, | |
216 const gfx::Point& root_location, | |
217 int touch_id, | |
218 base::TimeDelta time_stamp); | |
219 | |
220 virtual ~TouchEventImpl(); | |
221 | |
222 int touch_id() const { return touch_id_; } | |
223 float radius_x() const { return radius_x_; } | |
224 float radius_y() const { return radius_y_; } | |
225 float rotation_angle() const { return rotation_angle_; } | |
226 float force() const { return force_; } | |
227 | |
228 // Used for unit tests. | |
229 void set_radius_x(const float r) { radius_x_ = r; } | |
230 void set_radius_y(const float r) { radius_y_ = r; } | |
231 | |
232 // Overridden from LocatedEvent. | |
233 virtual void UpdateForRootTransform(const Transform& root_transform) OVERRIDE; | |
234 | |
235 // Overridden from TouchEvent. | |
236 virtual EventType GetEventType() const OVERRIDE; | |
237 virtual gfx::Point GetLocation() const OVERRIDE; | |
238 virtual int GetTouchId() const OVERRIDE; | |
239 virtual int GetEventFlags() const OVERRIDE; | |
240 virtual base::TimeDelta GetTimestamp() const OVERRIDE; | |
241 virtual float RadiusX() const OVERRIDE; | |
242 virtual float RadiusY() const OVERRIDE; | |
243 virtual float RotationAngle() const OVERRIDE; | |
244 virtual float Force() const OVERRIDE; | |
245 | |
246 private: | |
247 // The identity (typically finger) of the touch starting at 0 and incrementing | |
248 // for each separable additional touch that the hardware can detect. | |
249 const int touch_id_; | |
250 | |
251 // Radius of the X (major) axis of the touch ellipse. 0.0 if unknown. | |
252 float radius_x_; | |
253 | |
254 // Radius of the Y (minor) axis of the touch ellipse. 0.0 if unknown. | |
255 float radius_y_; | |
256 | |
257 // Angle of the major axis away from the X axis. Default 0.0. | |
258 const float rotation_angle_; | |
259 | |
260 // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0. | |
261 const float force_; | |
262 | |
263 DISALLOW_COPY_AND_ASSIGN(TouchEventImpl); | |
264 }; | |
265 | |
266 class UI_EXPORT KeyEvent : public Event { | |
267 public: | |
268 KeyEvent(const base::NativeEvent& native_event, bool is_char); | |
269 | |
270 // Used for synthetic events in testing. | |
271 KeyEvent(EventType type, KeyboardCode key_code, int flags); | |
272 | |
273 // These setters allow an I18N virtual keyboard to fabricate a keyboard event | |
274 // which does not have a corresponding KeyboardCode (example: U+00E1 Latin | |
275 // small letter A with acute, U+0410 Cyrillic capital letter A.) | |
276 // GetCharacter() and GetUnmodifiedCharacter() return the character. | |
277 void set_character(uint16 character) { character_ = character; } | |
278 void set_unmodified_character(uint16 unmodified_character) { | |
279 unmodified_character_ = unmodified_character; | |
280 } | |
281 | |
282 // Gets the character generated by this key event. It only supports Unicode | |
283 // BMP characters. | |
284 uint16 GetCharacter() const; | |
285 | |
286 // Gets the character generated by this key event ignoring concurrently-held | |
287 // modifiers (except shift). | |
288 uint16 GetUnmodifiedCharacter() const; | |
289 | |
290 // Returns the copy of this key event. Used in NativeWebKeyboardEvent. | |
291 KeyEvent* Copy(); | |
292 | |
293 KeyboardCode key_code() const { return key_code_; } | |
294 bool is_char() const { return is_char_; } | |
295 | |
296 // This is only intended to be used externally by classes that are modifying | |
297 // events in EventFilter::PreHandleKeyEvent(). set_character() should also be | |
298 // called. | |
299 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; } | |
300 | |
301 private: | |
302 KeyboardCode key_code_; | |
303 // True if this is a translated character event (vs. a raw key down). Both | |
304 // share the same type: ET_KEY_PRESSED. | |
305 bool is_char_; | |
306 | |
307 uint16 character_; | |
308 uint16 unmodified_character_; | |
309 }; | |
310 | |
311 // A key event which is translated by an input method (IME). | |
312 // For example, if an IME receives a KeyEvent(VKEY_SPACE), and it does not | |
313 // consume the key, the IME usually generates and dispatches a | |
314 // TranslatedKeyEvent(VKEY_SPACE) event. If the IME receives a KeyEvent and | |
315 // it does consume the event, it might dispatch a | |
316 // TranslatedKeyEvent(VKEY_PROCESSKEY) event as defined in the DOM spec. | |
317 class UI_EXPORT TranslatedKeyEvent : public KeyEvent { | |
318 public: | |
319 TranslatedKeyEvent(const base::NativeEvent& native_event, bool is_char); | |
320 | |
321 // Used for synthetic events such as a VKEY_PROCESSKEY key event. | |
322 TranslatedKeyEvent(bool is_press, KeyboardCode key_code, int flags); | |
323 | |
324 // Changes the type() of the object from ET_TRANSLATED_KEY_* to ET_KEY_* so | |
325 // that RenderWidgetHostViewAura and NativeWidgetAura could handle the event. | |
326 void ConvertToKeyEvent(); | |
327 }; | |
328 | |
329 class UI_EXPORT DropTargetEvent : public LocatedEvent { | |
330 public: | |
331 DropTargetEvent(const OSExchangeData& data, | |
332 const gfx::Point& location, | |
333 const gfx::Point& root_location, | |
334 int source_operations) | |
335 : LocatedEvent(ET_DROP_TARGET_EVENT, location, root_location, 0), | |
336 data_(data), | |
337 source_operations_(source_operations) { | |
338 } | |
339 | |
340 const OSExchangeData& data() const { return data_; } | |
341 int source_operations() const { return source_operations_; } | |
342 | |
343 private: | |
344 // Data associated with the drag/drop session. | |
345 const OSExchangeData& data_; | |
346 | |
347 // Bitmask of supported DragDropTypes::DragOperation by the source. | |
348 int source_operations_; | |
349 | |
350 DISALLOW_COPY_AND_ASSIGN(DropTargetEvent); | |
351 }; | |
352 | |
353 class UI_EXPORT ScrollEvent : public MouseEvent { | |
354 public: | |
355 explicit ScrollEvent(const base::NativeEvent& native_event); | |
356 template <class T> | |
357 ScrollEvent(const ScrollEvent& model, | |
358 T* source, | |
359 T* target, | |
360 EventType type, | |
361 int flags) | |
362 : MouseEvent(model, source, target, type, flags), | |
363 x_offset_(model.x_offset_), | |
364 y_offset_(model.y_offset_) { | |
365 } | |
366 | |
367 float x_offset() const { return x_offset_; } | |
368 float y_offset() const { return y_offset_; } | |
369 | |
370 private: | |
371 float x_offset_; | |
372 float y_offset_; | |
373 | |
374 DISALLOW_COPY_AND_ASSIGN(ScrollEvent); | |
375 }; | |
376 | |
377 // TODO(beng): rename to GestureEvent after conversion is complete. | |
378 class UI_EXPORT GestureEventImpl : public LocatedEvent, | |
379 public GestureEvent { | |
380 public: | |
381 GestureEventImpl(EventType type, | |
382 int x, | |
383 int y, | |
384 int flags, | |
385 base::Time time_stamp, | |
386 const GestureEventDetails& details, | |
387 unsigned int touch_ids_bitfield); | |
388 | |
389 // Create a new GestureEventImpl which is identical to the provided model. | |
390 // If source / target windows are provided, the model location will be | |
391 // converted from |source| coordinate system to |target| coordinate system. | |
392 template <typename T> | |
393 GestureEventImpl(const GestureEventImpl& model, T* source, T* target) | |
394 : LocatedEvent(model, source, target), | |
395 details_(model.details_), | |
396 touch_ids_bitfield_(model.touch_ids_bitfield_) { | |
397 } | |
398 | |
399 virtual ~GestureEventImpl(); | |
400 | |
401 const GestureEventDetails& details() const { return details_; } | |
402 | |
403 // Returns the lowest touch-id of any of the touches which make up this | |
404 // gesture. | |
405 // If there are no touches associated with this gesture, returns -1. | |
406 virtual int GetLowestTouchId() const OVERRIDE; | |
407 | |
408 private: | |
409 GestureEventDetails details_; | |
410 | |
411 // The set of indices of ones in the binary representation of | |
412 // touch_ids_bitfield_ is the set of touch_ids associate with this gesture. | |
413 // This value is stored as a bitfield because the number of touch ids varies, | |
414 // but we currently don't need more than 32 touches at a time. | |
415 const unsigned int touch_ids_bitfield_; | |
416 | |
417 DISALLOW_COPY_AND_ASSIGN(GestureEventImpl); | |
418 }; | |
419 | |
420 } // namespace ui | |
421 | |
422 #endif // UI_BASE_EVENT_H_ | |
OLD | NEW |