Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / events / event.h
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_EVENTS_EVENT_H_
6 #define UI_EVENTS_EVENT_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/event_types.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/gesture_event_details.h"
17 #include "ui/events/gestures/gesture_types.h"
18 #include "ui/events/keycodes/keyboard_codes.h"
19 #include "ui/events/latency_info.h"
20 #include "ui/gfx/point.h"
21 #include "ui/gfx/point_conversions.h"
22
23 namespace gfx {
24 class Transform;
25 }
26
27 namespace ui {
28 class EventTarget;
29
30 class EVENTS_EXPORT Event {
31  public:
32   static scoped_ptr<Event> Clone(const Event& event);
33
34   virtual ~Event();
35
36   class DispatcherApi {
37    public:
38     explicit DispatcherApi(Event* event) : event_(event) {}
39
40     void set_target(EventTarget* target) {
41       event_->target_ = target;
42     }
43
44     void set_phase(EventPhase phase) { event_->phase_ = phase; }
45     void set_result(int result) {
46       event_->result_ = static_cast<EventResult>(result);
47     }
48
49    private:
50     DispatcherApi();
51     Event* event_;
52
53     DISALLOW_COPY_AND_ASSIGN(DispatcherApi);
54   };
55
56   const base::NativeEvent& native_event() const { return native_event_; }
57   EventType type() const { return type_; }
58   const std::string& name() const { return name_; }
59   // time_stamp represents time since machine was booted.
60   const base::TimeDelta& time_stamp() const { return time_stamp_; }
61   int flags() const { return flags_; }
62
63   // This is only intended to be used externally by classes that are modifying
64   // events in an EventRewriter.
65   void set_flags(int flags) { flags_ = flags; }
66
67   EventTarget* target() const { return target_; }
68   EventPhase phase() const { return phase_; }
69   EventResult result() const { return result_; }
70
71   LatencyInfo* latency() { return &latency_; }
72   const LatencyInfo* latency() const { return &latency_; }
73   void set_latency(const LatencyInfo& latency) { latency_ = latency; }
74
75   int source_device_id() const { return source_device_id_; }
76   void set_source_device_id(int id) { source_device_id_ = id; }
77
78   // By default, events are "cancelable", this means any default processing that
79   // the containing abstraction layer may perform can be prevented by calling
80   // SetHandled(). SetHandled() or StopPropagation() must not be called for
81   // events that are not cancelable.
82   bool cancelable() const { return cancelable_; }
83
84   // The following methods return true if the respective keys were pressed at
85   // the time the event was created.
86   bool IsShiftDown() const { return (flags_ & EF_SHIFT_DOWN) != 0; }
87   bool IsControlDown() const { return (flags_ & EF_CONTROL_DOWN) != 0; }
88   bool IsCapsLockDown() const { return (flags_ & EF_CAPS_LOCK_DOWN) != 0; }
89   bool IsAltDown() const { return (flags_ & EF_ALT_DOWN) != 0; }
90   bool IsAltGrDown() const { return (flags_ & EF_ALTGR_DOWN) != 0; }
91   bool IsCommandDown() const { return (flags_ & EF_COMMAND_DOWN) != 0; }
92   bool IsRepeat() const { return (flags_ & EF_IS_REPEAT) != 0; }
93
94   bool IsKeyEvent() const {
95     return type_ == ET_KEY_PRESSED ||
96            type_ == ET_KEY_RELEASED ||
97            type_ == ET_TRANSLATED_KEY_PRESS ||
98            type_ == ET_TRANSLATED_KEY_RELEASE;
99   }
100
101   bool IsMouseEvent() const {
102     return type_ == ET_MOUSE_PRESSED ||
103            type_ == ET_MOUSE_DRAGGED ||
104            type_ == ET_MOUSE_RELEASED ||
105            type_ == ET_MOUSE_MOVED ||
106            type_ == ET_MOUSE_ENTERED ||
107            type_ == ET_MOUSE_EXITED ||
108            type_ == ET_MOUSEWHEEL ||
109            type_ == ET_MOUSE_CAPTURE_CHANGED;
110   }
111
112   bool IsTouchEvent() const {
113     return type_ == ET_TOUCH_RELEASED ||
114            type_ == ET_TOUCH_PRESSED ||
115            type_ == ET_TOUCH_MOVED ||
116            type_ == ET_TOUCH_CANCELLED;
117   }
118
119   bool IsGestureEvent() const {
120     switch (type_) {
121       case ET_GESTURE_SCROLL_BEGIN:
122       case ET_GESTURE_SCROLL_END:
123       case ET_GESTURE_SCROLL_UPDATE:
124       case ET_GESTURE_TAP:
125       case ET_GESTURE_TAP_CANCEL:
126       case ET_GESTURE_TAP_DOWN:
127       case ET_GESTURE_BEGIN:
128       case ET_GESTURE_END:
129       case ET_GESTURE_TWO_FINGER_TAP:
130       case ET_GESTURE_PINCH_BEGIN:
131       case ET_GESTURE_PINCH_END:
132       case ET_GESTURE_PINCH_UPDATE:
133       case ET_GESTURE_LONG_PRESS:
134       case ET_GESTURE_LONG_TAP:
135       case ET_GESTURE_SWIPE:
136       case ET_GESTURE_SHOW_PRESS:
137       case ET_GESTURE_WIN8_EDGE_SWIPE:
138         // When adding a gesture event which is paired with an event which
139         // occurs earlier, add the event to |IsEndingEvent|.
140         return true;
141
142       case ET_SCROLL_FLING_CANCEL:
143       case ET_SCROLL_FLING_START:
144         // These can be ScrollEvents too. EF_FROM_TOUCH determines if they're
145         // Gesture or Scroll events.
146         return (flags_ & EF_FROM_TOUCH) == EF_FROM_TOUCH;
147
148       default:
149         break;
150     }
151     return false;
152   }
153
154   // An ending event is paired with the event which started it. Setting capture
155   // should not prevent ending events from getting to their initial target.
156   bool IsEndingEvent() const {
157     switch(type_) {
158       case ui::ET_TOUCH_CANCELLED:
159       case ui::ET_GESTURE_TAP_CANCEL:
160       case ui::ET_GESTURE_END:
161       case ui::ET_GESTURE_SCROLL_END:
162       case ui::ET_GESTURE_PINCH_END:
163         return true;
164       default:
165         return false;
166     }
167   }
168
169   bool IsScrollEvent() const {
170     // Flings can be GestureEvents too. EF_FROM_TOUCH determins if they're
171     // Gesture or Scroll events.
172     return type_ == ET_SCROLL ||
173            ((type_ == ET_SCROLL_FLING_START ||
174            type_ == ET_SCROLL_FLING_CANCEL) &&
175            !(flags() & EF_FROM_TOUCH));
176   }
177
178   bool IsScrollGestureEvent() const {
179     return type_ == ET_GESTURE_SCROLL_BEGIN ||
180            type_ == ET_GESTURE_SCROLL_UPDATE ||
181            type_ == ET_GESTURE_SCROLL_END;
182   }
183
184   bool IsFlingScrollEvent() const {
185     return type_ == ET_SCROLL_FLING_CANCEL ||
186            type_ == ET_SCROLL_FLING_START;
187   }
188
189   bool IsMouseWheelEvent() const {
190     return type_ == ET_MOUSEWHEEL;
191   }
192
193   bool IsLocatedEvent() const {
194     return IsMouseEvent() || IsScrollEvent() || IsTouchEvent() ||
195            IsGestureEvent();
196   }
197
198   // Convenience methods to cast |this| to a GestureEvent. IsGestureEvent()
199   // must be true as a precondition to calling these methods.
200   GestureEvent* AsGestureEvent();
201   const GestureEvent* AsGestureEvent() const;
202
203   // Returns true if the event has a valid |native_event_|.
204   bool HasNativeEvent() const;
205
206   // Immediately stops the propagation of the event. This must be called only
207   // from an EventHandler during an event-dispatch. Any event handler that may
208   // be in the list will not receive the event after this is called.
209   // Note that StopPropagation() can be called only for cancelable events.
210   void StopPropagation();
211   bool stopped_propagation() const { return !!(result_ & ER_CONSUMED); }
212
213   // Marks the event as having been handled. A handled event does not reach the
214   // next event phase. For example, if an event is handled during the pre-target
215   // phase, then the event is dispatched to all pre-target handlers, but not to
216   // the target or post-target handlers.
217   // Note that SetHandled() can be called only for cancelable events.
218   void SetHandled();
219   bool handled() const { return result_ != ER_UNHANDLED; }
220
221  protected:
222   Event(EventType type, base::TimeDelta time_stamp, int flags);
223   Event(const base::NativeEvent& native_event, EventType type, int flags);
224   Event(const Event& copy);
225   void SetType(EventType type);
226   void set_delete_native_event(bool delete_native_event) {
227     delete_native_event_ = delete_native_event;
228   }
229   void set_cancelable(bool cancelable) { cancelable_ = cancelable; }
230
231   void set_time_stamp(const base::TimeDelta& time_stamp) {
232     time_stamp_ = time_stamp;
233   }
234
235   void set_name(const std::string& name) { name_ = name; }
236
237  private:
238   friend class EventTestApi;
239
240   EventType type_;
241   std::string name_;
242   base::TimeDelta time_stamp_;
243   LatencyInfo latency_;
244   int flags_;
245   base::NativeEvent native_event_;
246   bool delete_native_event_;
247   bool cancelable_;
248   EventTarget* target_;
249   EventPhase phase_;
250   EventResult result_;
251
252   // The device id the event came from, or ED_UNKNOWN_DEVICE if the information
253   // is not available.
254   int source_device_id_;
255 };
256
257 class EVENTS_EXPORT CancelModeEvent : public Event {
258  public:
259   CancelModeEvent();
260   ~CancelModeEvent() override;
261 };
262
263 class EVENTS_EXPORT LocatedEvent : public Event {
264  public:
265   ~LocatedEvent() override;
266
267   float x() const { return location_.x(); }
268   float y() const { return location_.y(); }
269   void set_location(const gfx::PointF& location) { location_ = location; }
270   // TODO(tdresser): Always return floating point location. See
271   // crbug.com/337824.
272   gfx::Point location() const { return gfx::ToFlooredPoint(location_); }
273   const gfx::PointF& location_f() const { return location_; }
274   void set_root_location(const gfx::PointF& root_location) {
275     root_location_ = root_location;
276   }
277   gfx::Point root_location() const {
278     return gfx::ToFlooredPoint(root_location_);
279   }
280   const gfx::PointF& root_location_f() const {
281     return root_location_;
282   }
283
284   // Transform the locations using |inverted_root_transform|.
285   // This is applied to both |location_| and |root_location_|.
286   virtual void UpdateForRootTransform(
287       const gfx::Transform& inverted_root_transform);
288
289   template <class T> void ConvertLocationToTarget(T* source, T* target) {
290     if (!target || target == source)
291       return;
292     // TODO(tdresser): Rewrite ConvertPointToTarget to use PointF. See
293     // crbug.com/337824.
294     gfx::Point offset = gfx::ToFlooredPoint(location_);
295     T::ConvertPointToTarget(source, target, &offset);
296     gfx::Vector2d diff = gfx::ToFlooredPoint(location_) - offset;
297     location_= location_ - diff;
298   }
299
300  protected:
301   friend class LocatedEventTestApi;
302   explicit LocatedEvent(const base::NativeEvent& native_event);
303
304   // Create a new LocatedEvent which is identical to the provided model.
305   // If source / target windows are provided, the model location will be
306   // converted from |source| coordinate system to |target| coordinate system.
307   template <class T>
308   LocatedEvent(const LocatedEvent& model, T* source, T* target)
309       : Event(model),
310         location_(model.location_),
311         root_location_(model.root_location_) {
312     ConvertLocationToTarget(source, target);
313   }
314
315   // Used for synthetic events in testing.
316   LocatedEvent(EventType type,
317                const gfx::PointF& location,
318                const gfx::PointF& root_location,
319                base::TimeDelta time_stamp,
320                int flags);
321
322   gfx::PointF location_;
323
324   // |location_| multiplied by an optional transformation matrix for
325   // rotations, animations and skews.
326   gfx::PointF root_location_;
327 };
328
329 class EVENTS_EXPORT MouseEvent : public LocatedEvent {
330  public:
331   explicit MouseEvent(const base::NativeEvent& native_event);
332
333   // Create a new MouseEvent based on the provided model.
334   // Uses the provided |type| and |flags| for the new event.
335   // If source / target windows are provided, the model location will be
336   // converted from |source| coordinate system to |target| coordinate system.
337   template <class T>
338   MouseEvent(const MouseEvent& model, T* source, T* target)
339       : LocatedEvent(model, source, target),
340         changed_button_flags_(model.changed_button_flags_) {
341   }
342
343   template <class T>
344   MouseEvent(const MouseEvent& model,
345              T* source,
346              T* target,
347              EventType type,
348              int flags)
349       : LocatedEvent(model, source, target),
350         changed_button_flags_(model.changed_button_flags_) {
351     SetType(type);
352     set_flags(flags);
353   }
354
355   // Used for synthetic events in testing and by the gesture recognizer.
356   MouseEvent(EventType type,
357              const gfx::PointF& location,
358              const gfx::PointF& root_location,
359              int flags,
360              int changed_button_flags);
361
362   // Conveniences to quickly test what button is down
363   bool IsOnlyLeftMouseButton() const {
364     return (flags() & EF_LEFT_MOUSE_BUTTON) &&
365       !(flags() & (EF_MIDDLE_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
366   }
367
368   bool IsLeftMouseButton() const {
369     return (flags() & EF_LEFT_MOUSE_BUTTON) != 0;
370   }
371
372   bool IsOnlyMiddleMouseButton() const {
373     return (flags() & EF_MIDDLE_MOUSE_BUTTON) &&
374       !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
375   }
376
377   bool IsMiddleMouseButton() const {
378     return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0;
379   }
380
381   bool IsOnlyRightMouseButton() const {
382     return (flags() & EF_RIGHT_MOUSE_BUTTON) &&
383       !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON));
384   }
385
386   bool IsRightMouseButton() const {
387     return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0;
388   }
389
390   bool IsAnyButton() const {
391     return (flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON |
392                        EF_RIGHT_MOUSE_BUTTON)) != 0;
393   }
394
395   // Compares two mouse down events and returns true if the second one should
396   // be considered a repeat of the first.
397   static bool IsRepeatedClickEvent(
398       const MouseEvent& event1,
399       const MouseEvent& event2);
400
401   // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise.
402   int GetClickCount() const;
403
404   // Set the click count for a mousedown message. Can be 1, 2 or 3.
405   void SetClickCount(int click_count);
406
407   // Identifies the button that changed. During a press this corresponds to the
408   // button that was pressed and during a release this corresponds to the button
409   // that was released.
410   // NOTE: during a press and release flags() contains the complete set of
411   // flags. Use this to determine the button that was pressed or released.
412   int changed_button_flags() const { return changed_button_flags_; }
413
414   // Updates the button that changed.
415   void set_changed_button_flags(int flags) { changed_button_flags_ = flags; }
416
417  private:
418   FRIEND_TEST_ALL_PREFIXES(EventTest, DoubleClickRequiresRelease);
419   FRIEND_TEST_ALL_PREFIXES(EventTest, SingleClickRightLeft);
420
421   // Returns the repeat count based on the previous mouse click, if it is
422   // recent enough and within a small enough distance.
423   static int GetRepeatCount(const MouseEvent& click_event);
424
425   // Resets the last_click_event_ for unit tests.
426   static void ResetLastClickForTest();
427
428   // See description above getter for details.
429   int changed_button_flags_;
430
431   static MouseEvent* last_click_event_;
432
433   // We can create a MouseEvent for a native event more than once. We set this
434   // to true when the next event either has a different timestamp or we see a
435   // release signalling that the press (click) event was completed.
436   static bool last_click_complete_;
437 };
438
439 class ScrollEvent;
440
441 class EVENTS_EXPORT MouseWheelEvent : public MouseEvent {
442  public:
443   // See |offset| for details.
444   static const int kWheelDelta;
445
446   explicit MouseWheelEvent(const base::NativeEvent& native_event);
447   explicit MouseWheelEvent(const ScrollEvent& scroll_event);
448   MouseWheelEvent(const MouseEvent& mouse_event, int x_offset, int y_offset);
449   MouseWheelEvent(const MouseWheelEvent& mouse_wheel_event);
450
451   template <class T>
452   MouseWheelEvent(const MouseWheelEvent& model,
453                   T* source,
454                   T* target)
455       : MouseEvent(model, source, target, model.type(), model.flags()),
456         offset_(model.x_offset(), model.y_offset()) {
457   }
458
459   // Used for synthetic events in testing and by the gesture recognizer.
460   MouseWheelEvent(const gfx::Vector2d& offset,
461                   const gfx::PointF& location,
462                   const gfx::PointF& root_location,
463                   int flags,
464                   int changed_button_flags);
465
466   // The amount to scroll. This is in multiples of kWheelDelta.
467   // Note: x_offset() > 0/y_offset() > 0 means scroll left/up.
468   int x_offset() const { return offset_.x(); }
469   int y_offset() const { return offset_.y(); }
470   const gfx::Vector2d& offset() const { return offset_; }
471
472   // Overridden from LocatedEvent.
473   void UpdateForRootTransform(
474       const gfx::Transform& inverted_root_transform) override;
475
476  private:
477   gfx::Vector2d offset_;
478 };
479
480 class EVENTS_EXPORT TouchEvent : public LocatedEvent {
481  public:
482   explicit TouchEvent(const base::NativeEvent& native_event);
483
484   // Create a new TouchEvent which is identical to the provided model.
485   // If source / target windows are provided, the model location will be
486   // converted from |source| coordinate system to |target| coordinate system.
487   template <class T>
488   TouchEvent(const TouchEvent& model, T* source, T* target)
489       : LocatedEvent(model, source, target),
490         touch_id_(model.touch_id_),
491         radius_x_(model.radius_x_),
492         radius_y_(model.radius_y_),
493         rotation_angle_(model.rotation_angle_),
494         force_(model.force_) {
495   }
496
497   TouchEvent(EventType type,
498              const gfx::PointF& location,
499              int touch_id,
500              base::TimeDelta time_stamp);
501
502   TouchEvent(EventType type,
503              const gfx::PointF& location,
504              int flags,
505              int touch_id,
506              base::TimeDelta timestamp,
507              float radius_x,
508              float radius_y,
509              float angle,
510              float force);
511
512   ~TouchEvent() override;
513
514   int touch_id() const { return touch_id_; }
515   float radius_x() const { return radius_x_; }
516   float radius_y() const { return radius_y_; }
517   float rotation_angle() const { return rotation_angle_; }
518   float force() const { return force_; }
519
520   // Used for unit tests.
521   void set_radius_x(const float r) { radius_x_ = r; }
522   void set_radius_y(const float r) { radius_y_ = r; }
523
524   // Overridden from LocatedEvent.
525   void UpdateForRootTransform(
526       const gfx::Transform& inverted_root_transform) override;
527
528  protected:
529   void set_radius(float radius_x, float radius_y) {
530     radius_x_ = radius_x;
531     radius_y_ = radius_y;
532   }
533
534   void set_rotation_angle(float rotation_angle) {
535     rotation_angle_ = rotation_angle;
536   }
537
538   void set_force(float force) { force_ = force; }
539
540  private:
541   // The identity (typically finger) of the touch starting at 0 and incrementing
542   // for each separable additional touch that the hardware can detect.
543   const int touch_id_;
544
545   // Radius of the X (major) axis of the touch ellipse. 0.0 if unknown.
546   float radius_x_;
547
548   // Radius of the Y (minor) axis of the touch ellipse. 0.0 if unknown.
549   float radius_y_;
550
551   // Angle of the major axis away from the X axis. Default 0.0.
552   float rotation_angle_;
553
554   // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0.
555   float force_;
556 };
557
558 // An interface that individual platforms can use to store additional data on
559 // KeyEvent.
560 //
561 // Currently only used in mojo.
562 class EVENTS_EXPORT ExtendedKeyEventData {
563  public:
564   virtual ~ExtendedKeyEventData() {}
565
566   virtual ExtendedKeyEventData* Clone() const = 0;
567 };
568
569 // A KeyEvent is really two distinct classes, melded together due to the
570 // DOM legacy of Windows key events: a keystroke event (is_char_ == false),
571 // or a character event (is_char_ == true).
572 //
573 // For a keystroke event,
574 // -- is_char_ is false.
575 // -- type() can be any one of ET_KEY_PRESSED, ET_KEY_RELEASED,
576 //    ET_TRANSLATED_KEY_PRESS, or ET_TRANSLATED_KEY_RELEASE.
577 // -- character_ functions as a bypass or cache for GetCharacter().
578 // -- key_code_ is a VKEY_ value associated with the key. For printable
579 //    characters, this may or may not be a mapped value, imitating MS Windows:
580 //    if the mapped key generates a character that has an associated VKEY_
581 //    code, then key_code_ is that code; if not, then key_code_ is the unmapped
582 //    VKEY_ code. For example, US, Greek, Cyrillic, Japanese, etc. all use
583 //    VKEY_Q for the key beside Tab, while French uses VKEY_A.
584 // -- code_ is in one-to-one correspondence with a physical keyboard
585 //    location, and does not vary depending on key layout.
586 //
587 // For a character event,
588 // -- is_char_ is true.
589 // -- type() is ET_KEY_PRESSED.
590 // -- character_ is a UTF-16 character value.
591 // -- key_code_ is conflated with character_ by some code, because both
592 //    arrive in the wParam field of a Windows event.
593 // -- code_ is the empty string.
594 //
595 class EVENTS_EXPORT KeyEvent : public Event {
596  public:
597   // Create a KeyEvent from a NativeEvent. For Windows this native event can
598   // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message
599   // (WM_CHAR). Other systems have only keystroke events.
600   explicit KeyEvent(const base::NativeEvent& native_event);
601
602   // Create a keystroke event.
603   KeyEvent(EventType type, KeyboardCode key_code, int flags);
604
605   // Create a character event.
606   KeyEvent(base::char16 character, KeyboardCode key_code, int flags);
607
608   // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA')
609   // See also: ui/events/keycodes/dom4/keycode_converter_data.h
610   KeyEvent(EventType type,
611            KeyboardCode key_code,
612            const std::string& code,
613            int flags);
614
615   KeyEvent(const KeyEvent& rhs);
616
617   KeyEvent& operator=(const KeyEvent& rhs);
618
619   ~KeyEvent() override;
620
621   // TODO(erg): While we transition to mojo, we have to hack around a mismatch
622   // in our event types. Our ui::Events don't really have all the data we need
623   // to process key events, and we instead do per-platform conversions with
624   // native HWNDs or XEvents. And we can't reliably send those native data
625   // types across mojo types in a cross-platform way. So instead, we set the
626   // resulting data when read across IPC boundaries.
627   void SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data);
628   const ExtendedKeyEventData* extended_key_event_data() const {
629     return extended_key_event_data_.get();
630   }
631
632   // This bypasses the normal mapping from keystroke events to characters,
633   // which allows an I18N virtual keyboard to fabricate a keyboard event that
634   // does not have a corresponding KeyboardCode (example: U+00E1 Latin small
635   // letter A with acute, U+0410 Cyrillic capital letter A).
636   void set_character(base::char16 character) { character_ = character; }
637
638   // Gets the character generated by this key event. It only supports Unicode
639   // BMP characters.
640   base::char16 GetCharacter() const;
641
642   // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r';
643   // otherwise returns the same as GetCharacter().
644   base::char16 GetUnmodifiedText() const;
645
646   // If the Control key is down in the event, returns a layout-independent
647   // character (corresponding to US layout); otherwise returns the same
648   // as GetUnmodifiedText().
649   base::char16 GetText() const;
650
651   // Gets the platform key code. For XKB, this is the xksym value.
652   void set_platform_keycode(uint32 keycode) { platform_keycode_ = keycode; }
653   uint32 platform_keycode() const { return platform_keycode_; }
654
655   // Gets the associated (Windows-based) KeyboardCode for this key event.
656   // Historically, this has also been used to obtain the character associated
657   // with a character event, because both use the Window message 'wParam' field.
658   // This should be avoided; if necessary for backwards compatibility, use
659   // GetConflatedWindowsKeyCode().
660   KeyboardCode key_code() const { return key_code_; }
661
662   // True if this is a character event, false if this is a keystroke event.
663   bool is_char() const { return is_char_; }
664
665   // This is only intended to be used externally by classes that are modifying
666   // events in an EventRewriter.
667   void set_key_code(KeyboardCode key_code) { key_code_ = key_code; }
668
669   // Returns the same value as key_code(), except that located codes are
670   // returned in place of non-located ones (e.g. VKEY_LSHIFT or VKEY_RSHIFT
671   // instead of VKEY_SHIFT). This is a hybrid of semantic and physical
672   // for legacy DOM reasons.
673   KeyboardCode GetLocatedWindowsKeyboardCode() const;
674
675   // For a keystroke event, returns the same value as key_code().
676   // For a character event, returns the same value as GetCharacter().
677   // This exists for backwards compatibility with Windows key events.
678   uint16 GetConflatedWindowsKeyCode() const;
679
680   // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win.
681   // TODO(msw): Additional work may be needed for analogues on other platforms.
682   bool IsUnicodeKeyCode() const;
683
684   std::string code() const { return code_; }
685
686   // Normalizes flags_ so that it describes the state after the event.
687   // (Native X11 event flags describe the state before the event.)
688   void NormalizeFlags();
689
690   // Returns true if the key event has already been processed by an input method
691   // and there is no need to pass the key event to the input method again.
692   bool IsTranslated() const;
693   // Marks this key event as translated or not translated.
694   void SetTranslated(bool translated);
695
696  protected:
697   friend class KeyEventTestApi;
698
699   // This allows a subclass TranslatedKeyEvent to be a non character event.
700   void set_is_char(bool is_char) { is_char_ = is_char; }
701
702  private:
703   // True if the key press originated from a 'right' key (VKEY_RSHIFT, etc.).
704   bool IsRightSideKey() const;
705
706   KeyboardCode key_code_;
707
708   // String of 'code' defined in DOM KeyboardEvent (e.g. 'KeyA', 'Space')
709   // http://www.w3.org/TR/uievents/#keyboard-key-codes.
710   //
711   // This value represents the physical position in the keyboard and can be
712   // converted from / to keyboard scan code like XKB.
713   std::string code_;
714
715   // True if this is a character event, false if this is a keystroke event.
716   bool is_char_;
717
718   // The platform related keycode value. For XKB, it's keysym value.
719   // For now, this is used for CharacterComposer in ChromeOS.
720   uint32 platform_keycode_;
721
722   // String of 'key' defined in DOM KeyboardEvent (e.g. 'a', 'รข')
723   // http://www.w3.org/TR/uievents/#keyboard-key-codes.
724   //
725   // This value represents the text that the key event will insert to input
726   // field. For key with modifier key, it may have specifial text.
727   // e.g. CTRL+A has '\x01'.
728   mutable base::char16 character_;
729
730   // Parts of our event handling require raw native events (see both the
731   // windows and linux implementations of web_input_event in content/). Because
732   // mojo instead serializes and deserializes events in potentially different
733   // processes, we need to have a mechanism to keep track of this data.
734   scoped_ptr<ExtendedKeyEventData> extended_key_event_data_;
735
736   static bool IsRepeated(const KeyEvent& event);
737
738   static KeyEvent* last_key_event_;
739 };
740
741 class EVENTS_EXPORT ScrollEvent : public MouseEvent {
742  public:
743   explicit ScrollEvent(const base::NativeEvent& native_event);
744   template <class T>
745   ScrollEvent(const ScrollEvent& model,
746               T* source,
747               T* target)
748       : MouseEvent(model, source, target),
749         x_offset_(model.x_offset_),
750         y_offset_(model.y_offset_),
751         x_offset_ordinal_(model.x_offset_ordinal_),
752         y_offset_ordinal_(model.y_offset_ordinal_),
753         finger_count_(model.finger_count_){
754   }
755
756   // Used for tests.
757   ScrollEvent(EventType type,
758               const gfx::PointF& location,
759               base::TimeDelta time_stamp,
760               int flags,
761               float x_offset,
762               float y_offset,
763               float x_offset_ordinal,
764               float y_offset_ordinal,
765               int finger_count);
766
767   // Scale the scroll event's offset value.
768   // This is useful in the multi-monitor setup where it needs to be scaled
769   // to provide a consistent user experience.
770   void Scale(const float factor);
771
772   float x_offset() const { return x_offset_; }
773   float y_offset() const { return y_offset_; }
774   float x_offset_ordinal() const { return x_offset_ordinal_; }
775   float y_offset_ordinal() const { return y_offset_ordinal_; }
776   int finger_count() const { return finger_count_; }
777
778  private:
779   // Potential accelerated offsets.
780   float x_offset_;
781   float y_offset_;
782   // Unaccelerated offsets.
783   float x_offset_ordinal_;
784   float y_offset_ordinal_;
785   // Number of fingers on the pad.
786   int finger_count_;
787 };
788
789 class EVENTS_EXPORT GestureEvent : public LocatedEvent {
790  public:
791   GestureEvent(float x,
792                float y,
793                int flags,
794                base::TimeDelta time_stamp,
795                const GestureEventDetails& details);
796
797   // Create a new GestureEvent which is identical to the provided model.
798   // If source / target windows are provided, the model location will be
799   // converted from |source| coordinate system to |target| coordinate system.
800   template <typename T>
801   GestureEvent(const GestureEvent& model, T* source, T* target)
802       : LocatedEvent(model, source, target),
803         details_(model.details_) {
804   }
805
806   ~GestureEvent() override;
807
808   const GestureEventDetails& details() const { return details_; }
809
810  private:
811   GestureEventDetails details_;
812 };
813
814 }  // namespace ui
815
816 #endif  // UI_EVENTS_EVENT_H_