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 <stdint.h> 9 10 #include <memory> 11 #include <string> 12 #include <vector> 13 14 #include "base/compiler_specific.h" 15 #include "base/containers/flat_map.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/logging.h" 18 #include "base/macros.h" 19 #include "base/strings/string16.h" 20 #include "base/time/time.h" 21 #include "build/build_config.h" 22 #include "ui/events/event_constants.h" 23 #include "ui/events/gesture_event_details.h" 24 #include "ui/events/gestures/gesture_types.h" 25 #include "ui/events/keycodes/dom/dom_key.h" 26 #include "ui/events/keycodes/keyboard_codes.h" 27 #include "ui/events/platform_event.h" 28 #include "ui/events/pointer_details.h" 29 #include "ui/events/types/event_type.h" 30 #include "ui/gfx/geometry/point.h" 31 #include "ui/gfx/geometry/point_conversions.h" 32 #include "ui/latency/latency_info.h" 33 34 namespace gfx { 35 class Transform; 36 } 37 38 namespace ui { 39 class CancelModeEvent; 40 class Event; 41 class EventTarget; 42 class KeyEvent; 43 class LocatedEvent; 44 class MouseEvent; 45 class MouseWheelEvent; 46 class ScrollEvent; 47 class TouchEvent; 48 49 enum class DomCode; 50 51 class EVENTS_EXPORT Event { 52 public: 53 using Properties = base::flat_map<std::string, std::vector<uint8_t>>; 54 55 // Copies an arbitrary event. If you have a typed event (e.g. a MouseEvent) 56 // just use its copy constructor. 57 static std::unique_ptr<Event> Clone(const Event& event); 58 59 virtual ~Event(); 60 61 class DispatcherApi { 62 public: DispatcherApi(Event * event)63 explicit DispatcherApi(Event* event) : event_(event) {} 64 set_target(EventTarget * target)65 void set_target(EventTarget* target) { 66 event_->target_ = target; 67 } 68 set_phase(EventPhase phase)69 void set_phase(EventPhase phase) { event_->phase_ = phase; } set_result(int result)70 void set_result(int result) { 71 event_->result_ = static_cast<EventResult>(result); 72 } set_time_stamp(base::TimeTicks time)73 void set_time_stamp(base::TimeTicks time) { event_->time_stamp_ = time; } 74 75 private: 76 Event* event_; 77 78 DISALLOW_COPY_AND_ASSIGN(DispatcherApi); 79 }; 80 native_event()81 const PlatformEvent& native_event() const { return native_event_; } type()82 EventType type() const { return type_; } 83 // time_stamp represents time since machine was booted. time_stamp()84 const base::TimeTicks time_stamp() const { return time_stamp_; } flags()85 int flags() const { return flags_; } 86 87 // Returns a name for the event, typically used in logging/debugging. This is 88 // a convenience for EventTypeName(type()) (EventTypeName() is in 89 // event_utils). 90 const char* GetName() const; 91 92 // This is only intended to be used externally by classes that are modifying 93 // events in an EventRewriter. set_flags(int flags)94 void set_flags(int flags) { flags_ = flags; } 95 target()96 EventTarget* target() const { return target_; } phase()97 EventPhase phase() const { return phase_; } result()98 EventResult result() const { return result_; } 99 latency()100 LatencyInfo* latency() { return &latency_; } latency()101 const LatencyInfo* latency() const { return &latency_; } set_latency(const LatencyInfo & latency)102 void set_latency(const LatencyInfo& latency) { latency_ = latency; } 103 source_device_id()104 int source_device_id() const { return source_device_id_; } set_source_device_id(int id)105 void set_source_device_id(int id) { source_device_id_ = id; } 106 107 // Sets the properties associated with this Event. 108 void SetProperties(const Properties& properties); 109 110 // Returns the properties associated with this event, which may be null. 111 // The properties are meant to provide a way to associate arbitrary key/value 112 // pairs with Events and not used by Event. properties()113 const Properties* properties() const { return properties_.get(); } 114 115 // By default, events are "cancelable", this means any default processing that 116 // the containing abstraction layer may perform can be prevented by calling 117 // SetHandled(). SetHandled() or StopPropagation() must not be called for 118 // events that are not cancelable. cancelable()119 bool cancelable() const { return cancelable_; } 120 121 // The following methods return true if the respective keys were pressed at 122 // the time the event was created. IsShiftDown()123 bool IsShiftDown() const { return (flags_ & EF_SHIFT_DOWN) != 0; } IsControlDown()124 bool IsControlDown() const { return (flags_ & EF_CONTROL_DOWN) != 0; } IsAltDown()125 bool IsAltDown() const { return (flags_ & EF_ALT_DOWN) != 0; } IsCommandDown()126 bool IsCommandDown() const { return (flags_ & EF_COMMAND_DOWN) != 0; } IsAltGrDown()127 bool IsAltGrDown() const { return (flags_ & EF_ALTGR_DOWN) != 0; } IsCapsLockOn()128 bool IsCapsLockOn() const { return (flags_ & EF_CAPS_LOCK_ON) != 0; } 129 IsSynthesized()130 bool IsSynthesized() const { return (flags_ & EF_IS_SYNTHESIZED) != 0; } 131 IsCancelModeEvent()132 bool IsCancelModeEvent() const { 133 return type_ == ET_CANCEL_MODE; 134 } 135 IsKeyEvent()136 bool IsKeyEvent() const { 137 return type_ == ET_KEY_PRESSED || type_ == ET_KEY_RELEASED; 138 } 139 IsMouseEvent()140 bool IsMouseEvent() const { 141 return type_ == ET_MOUSE_PRESSED || 142 type_ == ET_MOUSE_DRAGGED || 143 type_ == ET_MOUSE_RELEASED || 144 type_ == ET_MOUSE_MOVED || 145 type_ == ET_MOUSE_ENTERED || 146 type_ == ET_MOUSE_EXITED || 147 type_ == ET_MOUSEWHEEL || 148 type_ == ET_MOUSE_CAPTURE_CHANGED; 149 } 150 IsTouchEvent()151 bool IsTouchEvent() const { 152 return type_ == ET_TOUCH_RELEASED || 153 type_ == ET_TOUCH_PRESSED || 154 type_ == ET_TOUCH_MOVED || 155 type_ == ET_TOUCH_CANCELLED; 156 } 157 IsGestureEvent()158 bool IsGestureEvent() const { 159 switch (type_) { 160 case ET_GESTURE_SCROLL_BEGIN: 161 case ET_GESTURE_SCROLL_END: 162 case ET_GESTURE_SCROLL_UPDATE: 163 case ET_GESTURE_TAP: 164 case ET_GESTURE_DOUBLE_TAP: 165 case ET_GESTURE_TAP_CANCEL: 166 case ET_GESTURE_TAP_DOWN: 167 case ET_GESTURE_TAP_UNCONFIRMED: 168 case ET_GESTURE_BEGIN: 169 case ET_GESTURE_END: 170 case ET_GESTURE_TWO_FINGER_TAP: 171 case ET_GESTURE_PINCH_BEGIN: 172 case ET_GESTURE_PINCH_END: 173 case ET_GESTURE_PINCH_UPDATE: 174 case ET_GESTURE_LONG_PRESS: 175 case ET_GESTURE_LONG_TAP: 176 case ET_GESTURE_SWIPE: 177 case ET_GESTURE_SHOW_PRESS: 178 // When adding a gesture event which is paired with an event which 179 // occurs earlier, add the event to |IsEndingEvent|. 180 return true; 181 182 case ET_SCROLL_FLING_CANCEL: 183 case ET_SCROLL_FLING_START: 184 // These can be ScrollEvents too. EF_FROM_TOUCH determines if they're 185 // Gesture or Scroll events. 186 return (flags_ & EF_FROM_TOUCH) == EF_FROM_TOUCH; 187 188 default: 189 break; 190 } 191 return false; 192 } 193 194 // An ending event is paired with the event which started it. Setting capture 195 // should not prevent ending events from getting to their initial target. IsEndingEvent()196 bool IsEndingEvent() const { 197 switch(type_) { 198 case ui::ET_TOUCH_CANCELLED: 199 case ui::ET_GESTURE_TAP_CANCEL: 200 case ui::ET_GESTURE_END: 201 case ui::ET_GESTURE_SCROLL_END: 202 case ui::ET_GESTURE_PINCH_END: 203 return true; 204 default: 205 return false; 206 } 207 } 208 IsScrollEvent()209 bool IsScrollEvent() const { 210 // Flings can be GestureEvents too. EF_FROM_TOUCH determines if they're 211 // Gesture or Scroll events. 212 return type_ == ET_SCROLL || 213 ((type_ == ET_SCROLL_FLING_START || 214 type_ == ET_SCROLL_FLING_CANCEL) && 215 !(flags() & EF_FROM_TOUCH)); 216 } 217 IsPinchEvent()218 bool IsPinchEvent() const { 219 return type_ == ET_GESTURE_PINCH_BEGIN || 220 type_ == ET_GESTURE_PINCH_UPDATE || type_ == ET_GESTURE_PINCH_END; 221 } 222 IsScrollGestureEvent()223 bool IsScrollGestureEvent() const { 224 return type_ == ET_GESTURE_SCROLL_BEGIN || 225 type_ == ET_GESTURE_SCROLL_UPDATE || 226 type_ == ET_GESTURE_SCROLL_END; 227 } 228 IsFlingScrollEvent()229 bool IsFlingScrollEvent() const { 230 return type_ == ET_SCROLL_FLING_CANCEL || 231 type_ == ET_SCROLL_FLING_START; 232 } 233 IsMouseWheelEvent()234 bool IsMouseWheelEvent() const { 235 return type_ == ET_MOUSEWHEEL; 236 } 237 IsLocatedEvent()238 bool IsLocatedEvent() const { 239 return IsMouseEvent() || IsScrollEvent() || IsTouchEvent() || 240 IsGestureEvent(); 241 } 242 243 // Convenience methods to cast |this| to a CancelModeEvent. 244 // IsCancelModeEvent() must be true as a precondition to calling these 245 // methods. 246 CancelModeEvent* AsCancelModeEvent(); 247 const CancelModeEvent* AsCancelModeEvent() const; 248 249 // Convenience methods to cast |this| to a GestureEvent. IsGestureEvent() 250 // must be true as a precondition to calling these methods. 251 GestureEvent* AsGestureEvent(); 252 const GestureEvent* AsGestureEvent() const; 253 254 // Convenience methods to cast |this| to a KeyEvent. IsKeyEvent() 255 // must be true as a precondition to calling these methods. 256 KeyEvent* AsKeyEvent(); 257 const KeyEvent* AsKeyEvent() const; 258 259 // Convenience methods to cast |this| to a LocatedEvent. IsLocatedEvent() 260 // must be true as a precondition to calling these methods. 261 LocatedEvent* AsLocatedEvent(); 262 const LocatedEvent* AsLocatedEvent() const; 263 264 // Convenience methods to cast |this| to a MouseEvent. IsMouseEvent() 265 // must be true as a precondition to calling these methods. 266 MouseEvent* AsMouseEvent(); 267 const MouseEvent* AsMouseEvent() const; 268 269 // Convenience methods to cast |this| to a MouseWheelEvent. 270 // IsMouseWheelEvent() must be true as a precondition to calling these 271 // methods. 272 MouseWheelEvent* AsMouseWheelEvent(); 273 const MouseWheelEvent* AsMouseWheelEvent() const; 274 275 // Convenience methods to cast |this| to a ScrollEvent. IsScrollEvent() 276 // must be true as a precondition to calling these methods. 277 ScrollEvent* AsScrollEvent(); 278 const ScrollEvent* AsScrollEvent() const; 279 280 // Convenience methods to cast |this| to a TouchEvent. IsTouchEvent() 281 // must be true as a precondition to calling these methods. 282 TouchEvent* AsTouchEvent(); 283 const TouchEvent* AsTouchEvent() const; 284 285 // Returns true if the event has a valid |native_event_|. 286 bool HasNativeEvent() const; 287 288 // Immediately stops the propagation of the event. This must be called only 289 // from an EventHandler during an event-dispatch. Any event handler that may 290 // be in the list will not receive the event after this is called. 291 // Note that StopPropagation() can be called only for cancelable events. 292 void StopPropagation(); stopped_propagation()293 bool stopped_propagation() const { return !!(result_ & ER_CONSUMED); } 294 295 // Marks the event as having been handled. A handled event does not reach the 296 // next event phase. For example, if an event is handled during the pre-target 297 // phase, then the event is dispatched to all pre-target handlers, but not to 298 // the target or post-target handlers. 299 // Note that SetHandled() can be called only for cancelable events. 300 void SetHandled(); handled()301 bool handled() const { return result_ != ER_UNHANDLED; } 302 303 // For debugging. Not a stable serialization format. 304 virtual std::string ToString() const; 305 306 protected: 307 Event(EventType type, base::TimeTicks time_stamp, int flags); 308 Event(const PlatformEvent& native_event, EventType type, int flags); 309 Event(const Event& copy); 310 Event& operator=(const Event& rhs); 311 312 void SetType(EventType type); set_cancelable(bool cancelable)313 void set_cancelable(bool cancelable) { cancelable_ = cancelable; } 314 set_time_stamp(base::TimeTicks time_stamp)315 void set_time_stamp(base::TimeTicks time_stamp) { 316 time_stamp_ = time_stamp; 317 } 318 319 private: 320 friend class EventTestApi; 321 322 EventType type_; 323 base::TimeTicks time_stamp_; 324 LatencyInfo latency_; 325 int flags_; 326 PlatformEvent native_event_; 327 bool delete_native_event_; 328 bool cancelable_; 329 EventTarget* target_; 330 EventPhase phase_; 331 EventResult result_; 332 333 // The device id the event came from, or ED_UNKNOWN_DEVICE if the information 334 // is not available. 335 int source_device_id_; 336 337 std::unique_ptr<Properties> properties_; 338 }; 339 340 class EVENTS_EXPORT CancelModeEvent : public Event { 341 public: 342 CancelModeEvent(); 343 ~CancelModeEvent() override; 344 }; 345 346 class EVENTS_EXPORT LocatedEvent : public Event { 347 public: 348 // Convenience function that casts |event| to a LocatedEvent if it is one, 349 // otherwise returns null. FromIfValid(const ui::Event * event)350 static const ui::LocatedEvent* FromIfValid(const ui::Event* event) { 351 return event && event->IsLocatedEvent() ? event->AsLocatedEvent() : nullptr; 352 } 353 354 ~LocatedEvent() override; 355 x()356 float x() const { return location_.x(); } y()357 float y() const { return location_.y(); } set_location(const gfx::Point & location)358 void set_location(const gfx::Point& location) { 359 location_ = gfx::PointF(location); 360 } set_location_f(const gfx::PointF & location)361 void set_location_f(const gfx::PointF& location) { location_ = location; } location()362 gfx::Point location() const { return gfx::ToFlooredPoint(location_); } location_f()363 const gfx::PointF& location_f() const { return location_; } set_root_location(const gfx::Point & root_location)364 void set_root_location(const gfx::Point& root_location) { 365 root_location_ = gfx::PointF(root_location); 366 } set_root_location_f(const gfx::PointF & root_location)367 void set_root_location_f(const gfx::PointF& root_location) { 368 root_location_ = root_location; 369 } root_location()370 gfx::Point root_location() const { 371 return gfx::ToFlooredPoint(root_location_); 372 } root_location_f()373 const gfx::PointF& root_location_f() const { 374 return root_location_; 375 } 376 377 // Transform the locations using |inverted_root_transform| and 378 // |inverted_local_transform|. |inverted_local_transform| is only used if 379 // the event has a target. 380 virtual void UpdateForRootTransform( 381 const gfx::Transform& inverted_root_transform, 382 const gfx::Transform& inverted_local_transform); 383 384 template <class T> ConvertLocationToTarget(const T * source,const T * target)385 void ConvertLocationToTarget(const T* source, const T* target) { 386 if (!target || target == source) 387 return; 388 gfx::Point offset = gfx::ToFlooredPoint(location_); 389 T::ConvertPointToTarget(source, target, &offset); 390 gfx::Vector2d diff = gfx::ToFlooredPoint(location_) - offset; 391 location_ = location_ - diff; 392 } 393 394 // Event: 395 std::string ToString() const override; 396 397 protected: 398 friend class LocatedEventTestApi; 399 400 LocatedEvent(const LocatedEvent& copy); 401 402 explicit LocatedEvent(const PlatformEvent& native_event); 403 404 // Create a new LocatedEvent which is identical to the provided model. 405 // If source / target windows are provided, the model location will be 406 // converted from |source| coordinate system to |target| coordinate system. 407 template <class T> LocatedEvent(const LocatedEvent & model,T * source,T * target)408 LocatedEvent(const LocatedEvent& model, T* source, T* target) 409 : Event(model), 410 location_(model.location_), 411 root_location_(model.root_location_) { 412 ConvertLocationToTarget(source, target); 413 } 414 415 // Used for synthetic events in testing. 416 LocatedEvent(EventType type, 417 const gfx::PointF& location, 418 const gfx::PointF& root_location, 419 base::TimeTicks time_stamp, 420 int flags); 421 422 // Location of the event relative to the target window and in the target 423 // window's coordinate space. If there is no target this is the same as 424 // |root_location_|. Native events may generate float values with sub-pixel 425 // precision. 426 gfx::PointF location_; 427 428 // Location of the event. What coordinate system this is in depends upon the 429 // phase of event dispatch. For client code (meaning EventHandlers) it is 430 // generally in screen coordinates, but early on it may be in pixels and 431 // relative to a display. Native events may generate float values with 432 // sub-pixel precision. 433 gfx::PointF root_location_; 434 }; 435 436 class EVENTS_EXPORT MouseEvent : public LocatedEvent { 437 public: 438 // NOTE: On some platforms this will allow an event to be constructed from a 439 // void*, see PlatformEvent. 440 explicit MouseEvent(const PlatformEvent& native_event); 441 442 // Create a new MouseEvent based on the provided model. 443 // Uses the provided |type| and |flags| for the new event. 444 // If source / target windows are provided, the model location will be 445 // converted from |source| coordinate system to |target| coordinate system. 446 template <class T> MouseEvent(const MouseEvent & model,T * source,T * target)447 MouseEvent(const MouseEvent& model, T* source, T* target) 448 : LocatedEvent(model, source, target), 449 changed_button_flags_(model.changed_button_flags_), 450 pointer_details_(model.pointer_details_) {} 451 452 template <class T> MouseEvent(const MouseEvent & model,T * source,T * target,EventType type,int flags)453 MouseEvent(const MouseEvent& model, 454 T* source, 455 T* target, 456 EventType type, 457 int flags) 458 : LocatedEvent(model, source, target), 459 changed_button_flags_(model.changed_button_flags_), 460 pointer_details_(model.pointer_details_) { 461 SetType(type); 462 set_flags(flags); 463 } 464 465 // Note: Use the ctor for MouseWheelEvent if type is ET_MOUSEWHEEL. 466 MouseEvent(EventType type, 467 const gfx::PointF& location, 468 const gfx::PointF& root_location, 469 base::TimeTicks time_stamp, 470 int flags, 471 int changed_button_flags, 472 const PointerDetails& pointer_details = 473 PointerDetails(EventPointerType::POINTER_TYPE_MOUSE, 474 kPointerIdMouse)); 475 476 // DEPRECATED: Prefer constructor that takes gfx::PointF. 477 MouseEvent(EventType type, 478 const gfx::Point& location, 479 const gfx::Point& root_location, 480 base::TimeTicks time_stamp, 481 int flags, 482 int changed_button_flags, 483 const PointerDetails& pointer_details = 484 PointerDetails(EventPointerType::POINTER_TYPE_MOUSE, 485 kPointerIdMouse)); 486 487 MouseEvent(const MouseEvent& copy); 488 ~MouseEvent() override; 489 490 void InitializeNative(); 491 492 class DispatcherApi { 493 public: DispatcherApi(MouseEvent * event)494 explicit DispatcherApi(MouseEvent* event) : event_(event) {} 495 496 // TODO(eirage): convert this to builder pattern. set_movement(const gfx::Vector2dF & movement)497 void set_movement(const gfx::Vector2dF& movement) { 498 event_->movement_ = movement; 499 event_->set_flags(event_->flags() | EF_UNADJUSTED_MOUSE); 500 } 501 502 private: 503 MouseEvent* event_; 504 505 DISALLOW_COPY_AND_ASSIGN(DispatcherApi); 506 }; 507 508 // Conveniences to quickly test what button is down IsOnlyLeftMouseButton()509 bool IsOnlyLeftMouseButton() const { 510 return button_flags() == EF_LEFT_MOUSE_BUTTON; 511 } 512 IsLeftMouseButton()513 bool IsLeftMouseButton() const { 514 return (flags() & EF_LEFT_MOUSE_BUTTON) != 0; 515 } 516 IsOnlyMiddleMouseButton()517 bool IsOnlyMiddleMouseButton() const { 518 return button_flags() == EF_MIDDLE_MOUSE_BUTTON; 519 } 520 IsMiddleMouseButton()521 bool IsMiddleMouseButton() const { 522 return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0; 523 } 524 IsOnlyRightMouseButton()525 bool IsOnlyRightMouseButton() const { 526 return button_flags() == EF_RIGHT_MOUSE_BUTTON; 527 } 528 IsRightMouseButton()529 bool IsRightMouseButton() const { 530 return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0; 531 } 532 IsAnyButton()533 bool IsAnyButton() const { 534 return button_flags() != 0; 535 } 536 537 // Returns the flags for the mouse buttons. button_flags()538 int button_flags() const { 539 return flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON | 540 EF_RIGHT_MOUSE_BUTTON | EF_BACK_MOUSE_BUTTON | 541 EF_FORWARD_MOUSE_BUTTON); 542 } 543 544 // Compares two mouse down events and returns true if the second one should 545 // be considered a repeat of the first. 546 static bool IsRepeatedClickEvent( 547 const MouseEvent& event1, 548 const MouseEvent& event2); 549 550 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise. 551 int GetClickCount() const; 552 553 // Set the click count for a mousedown message. Can be 1, 2 or 3. 554 void SetClickCount(int click_count); 555 556 // Identifies the button that changed. During a press this corresponds to the 557 // button that was pressed and during a release this corresponds to the button 558 // that was released. 559 // NOTE: during a press and release flags() contains the complete set of 560 // flags. Use this to determine the button that was pressed or released. changed_button_flags()561 int changed_button_flags() const { return changed_button_flags_; } 562 563 // Updates the button that changed. set_changed_button_flags(int flags)564 void set_changed_button_flags(int flags) { changed_button_flags_ = flags; } 565 movement()566 const gfx::Vector2dF& movement() const { return movement_; } 567 pointer_details()568 const PointerDetails& pointer_details() const { return pointer_details_; } 569 570 private: 571 FRIEND_TEST_ALL_PREFIXES(EventTest, DoubleClickRequiresUniqueTimestamp); 572 FRIEND_TEST_ALL_PREFIXES(EventTest, SingleClickRightLeft); 573 574 // Returns the repeat count based on the previous mouse click, if it is 575 // recent enough and within a small enough distance. 576 static int GetRepeatCount(const MouseEvent& click_event); 577 578 // Resets the last_click_event_ for unit tests. 579 static void ResetLastClickForTest(); 580 581 // See description above getter for details. 582 int changed_button_flags_; 583 584 // Raw mouse movement value reported from mouse hardware. The value of this is 585 // platform dependent and may change depending upon the hardware connected to 586 // the device. This field is only set if the flag EF_UNADJUSTED_MOUSE is 587 // present. 588 gfx::Vector2dF movement_; 589 590 // The most recent user-generated MouseEvent, used to detect double clicks. 591 static MouseEvent* last_click_event_; 592 593 // Structure for holding pointer details for implementing PointerEvents API. 594 PointerDetails pointer_details_; 595 }; 596 597 class ScrollEvent; 598 599 class EVENTS_EXPORT MouseWheelEvent : public MouseEvent { 600 public: 601 // See |offset| for details. 602 static const int kWheelDelta; 603 604 explicit MouseWheelEvent(const PlatformEvent& native_event); 605 explicit MouseWheelEvent(const ScrollEvent& scroll_event); 606 MouseWheelEvent(const MouseEvent& mouse_event, int x_offset, int y_offset); 607 MouseWheelEvent(const MouseWheelEvent& copy); 608 ~MouseWheelEvent() override; 609 610 template <class T> MouseWheelEvent(const MouseWheelEvent & model,T * source,T * target)611 MouseWheelEvent(const MouseWheelEvent& model, 612 T* source, 613 T* target) 614 : MouseEvent(model, source, target, model.type(), model.flags()), 615 offset_(model.x_offset(), model.y_offset()) { 616 } 617 618 // Used for synthetic events in testing and by the gesture recognizer. 619 MouseWheelEvent(const gfx::Vector2d& offset, 620 const gfx::PointF& location, 621 const gfx::PointF& root_location, 622 base::TimeTicks time_stamp, 623 int flags, 624 int changed_button_flags); 625 626 // DEPRECATED: Prefer the constructor that takes gfx::PointF. 627 MouseWheelEvent(const gfx::Vector2d& offset, 628 const gfx::Point& location, 629 const gfx::Point& root_location, 630 base::TimeTicks time_stamp, 631 int flags, 632 int changed_button_flags); 633 634 // The amount to scroll. This is in multiples of kWheelDelta. 635 // Note: x_offset() > 0/y_offset() > 0 means scroll left/up. x_offset()636 int x_offset() const { return offset_.x(); } y_offset()637 int y_offset() const { return offset_.y(); } offset()638 const gfx::Vector2d& offset() const { return offset_; } 639 640 private: 641 gfx::Vector2d offset_; 642 }; 643 644 // NOTE: Pen (stylus) events use TouchEvent with POINTER_TYPE_PEN. They were 645 // originally implemented as MouseEvent but switched to TouchEvent when UX 646 // decided not to show hover effects for pen. 647 class EVENTS_EXPORT TouchEvent : public LocatedEvent { 648 public: 649 explicit TouchEvent(const PlatformEvent& native_event); 650 651 // Create a new TouchEvent which is identical to the provided model. 652 // If source / target windows are provided, the model location will be 653 // converted from |source| coordinate system to |target| coordinate system. 654 template <class T> TouchEvent(const TouchEvent & model,T * source,T * target)655 TouchEvent(const TouchEvent& model, T* source, T* target) 656 : LocatedEvent(model, source, target), 657 unique_event_id_(model.unique_event_id_), 658 may_cause_scrolling_(model.may_cause_scrolling_), 659 hovering_(false), 660 pointer_details_(model.pointer_details_) {} 661 662 TouchEvent(EventType type, 663 const gfx::PointF& location, 664 const gfx::PointF& root_location, 665 base::TimeTicks time_stamp, 666 const PointerDetails& pointer_details, 667 int flags = 0); 668 669 // DEPRECATED: Prefer the constructor that takes gfx::PointF. 670 TouchEvent(EventType type, 671 const gfx::Point& location, 672 base::TimeTicks time_stamp, 673 const PointerDetails& pointer_details, 674 int flags = 0); 675 676 TouchEvent(const TouchEvent& copy); 677 678 ~TouchEvent() override; 679 680 // A unique identifier for this event. unique_event_id()681 uint32_t unique_event_id() const { return unique_event_id_; } 682 set_may_cause_scrolling(bool causes)683 void set_may_cause_scrolling(bool causes) { may_cause_scrolling_ = causes; } may_cause_scrolling()684 bool may_cause_scrolling() const { return may_cause_scrolling_; } 685 set_hovering(bool hovering)686 void set_hovering(bool hovering) { hovering_ = hovering; } hovering()687 bool hovering() const { return hovering_; } 688 689 // Overridden from LocatedEvent. 690 void UpdateForRootTransform( 691 const gfx::Transform& inverted_root_transform, 692 const gfx::Transform& inverted_local_transform) override; 693 694 // Marks the event as not participating in synchronous gesture recognition. 695 void DisableSynchronousHandling(); synchronous_handling_disabled()696 bool synchronous_handling_disabled() const { 697 return !!(result() & ER_DISABLE_SYNC_HANDLING); 698 } 699 pointer_details()700 const PointerDetails& pointer_details() const { return pointer_details_; } 701 void SetPointerDetailsForTest(const PointerDetails& pointer_details); 702 703 float ComputeRotationAngle() const; 704 705 private: 706 // A unique identifier for the touch event. 707 // NOTE: this is *not* serialized over mojom, as the number is unique to 708 // a particular process, and as mojom may go cross process, to serialize could 709 // lead to conflicts. 710 uint32_t unique_event_id_; 711 712 // Whether the (unhandled) touch event will produce a scroll event (e.g., a 713 // touchmove that exceeds the platform slop region, or a touchend that 714 // causes a fling). Defaults to false. 715 bool may_cause_scrolling_; 716 717 // True for devices like some pens when they support hovering over 718 // digitizer and they send events while hovering. 719 bool hovering_; 720 721 // Structure for holding pointer details for implementing PointerEvents API. 722 PointerDetails pointer_details_; 723 }; 724 725 // A KeyEvent is really two distinct classes, melded together due to the 726 // DOM legacy of Windows key events: a keystroke event (is_char_ == false), 727 // or a character event (is_char_ == true). 728 // 729 // For a keystroke event, 730 // -- |bool is_char_| is false. 731 // -- |EventType Event::type()| can be ET_KEY_PRESSED or ET_KEY_RELEASED. 732 // -- |DomCode code_| and |int Event::flags()| represent the physical key event. 733 // - code_ is a platform-independent representation of the physical key, 734 // based on DOM UI Events KeyboardEvent |code| values. It does not 735 // vary depending on key layout. 736 // http://www.w3.org/TR/DOM-Level-3-Events-code/ 737 // - Event::flags() provides the active modifiers for the physical key 738 // press. Its value reflects the state after the event; that is, for 739 // a modifier key, a press includes the corresponding flag and a release 740 // does not. 741 // -- |DomKey key_| provides the meaning (character or action) of the key 742 // event, in the context of the active layout and modifiers. It corresponds 743 // to DOM UI Events KeyboardEvent |key| values. 744 // http://www.w3.org/TR/DOM-Level-3-Events-key/ 745 // -- |KeyboardCode key_code_| supports the legacy web event |keyCode| field, 746 // and its VKEY_ values are chosen to match Windows/IE for compatibility. 747 // For printable characters, this may or may not be a layout-mapped value, 748 // imitating MS Windows: if the mapped key generates a character that has 749 // an associated VKEY_ code, then key_code_ is that code; if not, then 750 // key_code_ is the unmapped VKEY_ code. For example, US, Greek, Cyrillic, 751 // Japanese, etc. all use VKEY_Q for the key beside Tab, while French uses 752 // VKEY_A. The stored key_code_ is non-located (e.g. VKEY_SHIFT rather than 753 // VKEY_LSHIFT, VKEY_1 rather than VKEY_NUMPAD1). 754 // 755 // For a character event, 756 // -- |bool is_char_| is true. 757 // -- |EventType Event::type()| is ET_KEY_PRESSED. 758 // -- |DomCode code_| is DomCode::NONE. 759 // -- |DomKey key_| is a UTF-16 code point. 760 // -- |KeyboardCode key_code_| is conflated with the character-valued key_ 761 // by some code, because both arrive in the wParam field of a Windows event. 762 // 763 class EVENTS_EXPORT KeyEvent : public Event { 764 public: 765 // Create a KeyEvent from a NativeEvent. For Windows this native event can 766 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message 767 // (WM_CHAR). Other systems have only keystroke events. 768 explicit KeyEvent(const PlatformEvent& native_event); 769 770 // Create a KeyEvent from a NativeEvent but with mocked flags. 771 // This method is necessary for Windows since MSG does not contain all flags. 772 KeyEvent(const PlatformEvent& native_event, int event_flags); 773 774 // Create a keystroke event from a legacy KeyboardCode. 775 // This should not be used in new code. 776 KeyEvent(EventType type, 777 KeyboardCode key_code, 778 int flags, 779 base::TimeTicks time_stamp = base::TimeTicks()); 780 781 // Create a fully defined keystroke event. 782 KeyEvent(EventType type, 783 KeyboardCode key_code, 784 DomCode code, 785 int flags, 786 DomKey key, 787 base::TimeTicks time_stamp, 788 bool is_char = false); 789 790 // Create a character event. 791 KeyEvent(base::char16 character, 792 KeyboardCode key_code, 793 DomCode code, 794 int flags, 795 base::TimeTicks time_stamp = base::TimeTicks()); 796 797 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA') 798 // See also: ui/events/keycodes/dom/dom_values.txt 799 KeyEvent(EventType type, 800 KeyboardCode key_code, 801 DomCode code, 802 int flags); 803 804 KeyEvent(const KeyEvent& rhs); 805 806 KeyEvent& operator=(const KeyEvent& rhs); 807 808 ~KeyEvent() override; 809 810 void InitializeNative(); 811 812 // This bypasses the normal mapping from keystroke events to characters, 813 // which allows an I18N virtual keyboard to fabricate a keyboard event that 814 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small 815 // letter A with acute, U+0410 Cyrillic capital letter A). set_character(base::char16 character)816 void set_character(base::char16 character) { 817 key_ = DomKey::FromCharacter(character); 818 } 819 820 // Gets the character generated by this key event. It only supports Unicode 821 // BMP characters. 822 base::char16 GetCharacter() const; 823 824 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r'; 825 // otherwise returns the same as GetCharacter(). 826 base::char16 GetUnmodifiedText() const; 827 828 // If the Control key is down in the event, returns a layout-independent 829 // character (corresponding to US layout); otherwise returns the same 830 // as GetUnmodifiedText(). 831 base::char16 GetText() const; 832 833 // True if this is a character event, false if this is a keystroke event. is_char()834 bool is_char() const { return is_char_; } 835 is_repeat()836 bool is_repeat() const { return (flags() & EF_IS_REPEAT) != 0; } 837 838 // Gets the associated (Windows-based) KeyboardCode for this key event. 839 // Historically, this has also been used to obtain the character associated 840 // with a character event, because both use the Window message 'wParam' field. 841 // This should be avoided; if necessary for backwards compatibility, use 842 // GetConflatedWindowsKeyCode(). key_code()843 KeyboardCode key_code() const { return key_code_; } 844 845 // This is only intended to be used externally by classes that are modifying 846 // events in an EventRewriter. set_key_code(KeyboardCode key_code)847 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; } 848 849 // Returns the same value as key_code(), except that located codes are 850 // returned in place of non-located ones (e.g. VKEY_LSHIFT or VKEY_RSHIFT 851 // instead of VKEY_SHIFT). This is a hybrid of semantic and physical 852 // for legacy DOM reasons. 853 KeyboardCode GetLocatedWindowsKeyboardCode() const; 854 855 // For a keystroke event, returns the same value as key_code(). 856 // For a character event, returns the same value as GetCharacter(). 857 // This exists for backwards compatibility with Windows key events. 858 uint16_t GetConflatedWindowsKeyCode() const; 859 860 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win. 861 // TODO(msw): Additional work may be needed for analogues on other platforms. 862 bool IsUnicodeKeyCode() const; 863 864 // Returns the DOM .code (physical key identifier) for a keystroke event. code()865 DomCode code() const { return code_; } 866 std::string GetCodeString() const; 867 868 // Returns the DOM .key (layout meaning) for a keystroke event. 869 DomKey GetDomKey() const; 870 871 // Normalizes flags_ so that it describes the state after the event. 872 // (Native X11 event flags describe the state before the event.) 873 void NormalizeFlags(); 874 875 protected: 876 friend class KeyEventTestApi; 877 878 // This allows a subclass TranslatedKeyEvent to be a non character event. set_is_char(bool is_char)879 void set_is_char(bool is_char) { is_char_ = is_char; } 880 881 private: 882 // Determine key_ on a keystroke event from code_ and flags(). 883 void ApplyLayout() const; 884 885 // Tells if this is a repeated KeyEvent based on |last_key_event|, which is 886 // then updated with the new last KeyEvent address. 887 bool IsRepeated(KeyEvent** last_key_event); 888 889 KeyboardCode key_code_; 890 891 // DOM KeyboardEvent |code| (e.g. DomCode::US_A, DomCode::SPACE). 892 // http://www.w3.org/TR/DOM-Level-3-Events-code/ 893 // 894 // This value represents the physical position in the keyboard and can be 895 // converted from / to keyboard scan code like XKB. 896 DomCode code_; 897 898 // True if this is a character event, false if this is a keystroke event. 899 bool is_char_ = false; 900 901 // TODO(kpschoedel): refactor so that key_ is not mutable. 902 // This requires defining the KeyEvent completely at construction rather 903 // than lazily under GetCharacter(), which likely also means removing 904 // the two 'incomplete' constructors. crbug.com/444045 905 // 906 // DOM KeyboardEvent |key| 907 // http://www.w3.org/TR/DOM-Level-3-Events-key/ 908 // 909 // This value represents the meaning of a key, which is either a Unicode 910 // character, or a named DomKey:: value. 911 // This is not necessarily initialized when the event is constructed; 912 // it may be set only if and when GetCharacter() or GetDomKey() is called. 913 mutable DomKey key_ = DomKey::NONE; 914 915 static KeyEvent* last_key_event_; 916 #if defined(USE_X11) 917 static KeyEvent* last_ibus_key_event_; 918 #endif 919 }; 920 921 class EVENTS_EXPORT ScrollEvent : public MouseEvent { 922 public: 923 explicit ScrollEvent(const PlatformEvent& native_event); 924 925 template <class T> ScrollEvent(const ScrollEvent & model,T * source,T * target)926 ScrollEvent(const ScrollEvent& model, T* source, T* target) 927 : MouseEvent(model, source, target), 928 x_offset_(model.x_offset_), 929 y_offset_(model.y_offset_), 930 x_offset_ordinal_(model.x_offset_ordinal_), 931 y_offset_ordinal_(model.y_offset_ordinal_), 932 finger_count_(model.finger_count_), 933 momentum_phase_(model.momentum_phase_), 934 scroll_event_phase_(model.scroll_event_phase_) {} 935 936 ScrollEvent(EventType type, 937 const gfx::PointF& location, 938 const gfx::PointF& root_location, 939 base::TimeTicks time_stamp, 940 int flags, 941 float x_offset, 942 float y_offset, 943 float x_offset_ordinal, 944 float y_offset_ordinal, 945 int finger_count, 946 EventMomentumPhase momentum_phase = EventMomentumPhase::NONE, 947 ScrollEventPhase phase = ScrollEventPhase::kNone); 948 949 // DEPRECATED: Prefer the constructor that takes gfx::PointF. 950 ScrollEvent(EventType type, 951 const gfx::Point& location, 952 base::TimeTicks time_stamp, 953 int flags, 954 float x_offset, 955 float y_offset, 956 float x_offset_ordinal, 957 float y_offset_ordinal, 958 int finger_count, 959 EventMomentumPhase momentum_phase = EventMomentumPhase::NONE, 960 ScrollEventPhase phase = ScrollEventPhase::kNone); 961 962 ScrollEvent(const ScrollEvent& copy); 963 ~ScrollEvent() override; 964 965 // Scale the scroll event's offset value. 966 // This is useful in the multi-monitor setup where it needs to be scaled 967 // to provide a consistent user experience. 968 void Scale(const float factor); 969 x_offset()970 float x_offset() const { return x_offset_; } y_offset()971 float y_offset() const { return y_offset_; } x_offset_ordinal()972 float x_offset_ordinal() const { return x_offset_ordinal_; } y_offset_ordinal()973 float y_offset_ordinal() const { return y_offset_ordinal_; } finger_count()974 int finger_count() const { return finger_count_; } momentum_phase()975 EventMomentumPhase momentum_phase() const { return momentum_phase_; } scroll_event_phase()976 ScrollEventPhase scroll_event_phase() const { return scroll_event_phase_; } 977 978 // Event: 979 std::string ToString() const override; 980 981 private: 982 // Potential accelerated offsets. 983 float x_offset_; 984 float y_offset_; 985 // Unaccelerated offsets. 986 float x_offset_ordinal_; 987 float y_offset_ordinal_; 988 // Number of fingers on the pad. 989 int finger_count_; 990 991 // For non-fling events, provides momentum information (e.g. for the case 992 // where the device provides continuous event updates during a fling). 993 EventMomentumPhase momentum_phase_ = EventMomentumPhase::NONE; 994 995 // Provides phase information if device can provide. 996 ScrollEventPhase scroll_event_phase_ = ScrollEventPhase::kNone; 997 }; 998 999 class EVENTS_EXPORT GestureEvent : public LocatedEvent { 1000 public: 1001 // The constructor takes a default unique_touch_id of zero to support many 1002 // (80+) existing tests that doesn't care about this id. 1003 GestureEvent(float x, 1004 float y, 1005 int flags, 1006 base::TimeTicks time_stamp, 1007 const GestureEventDetails& details, 1008 uint32_t unique_touch_event_id = 0); 1009 1010 // Create a new GestureEvent which is identical to the provided model. 1011 // If source / target windows are provided, the model location will be 1012 // converted from |source| coordinate system to |target| coordinate system. 1013 template <typename T> GestureEvent(const GestureEvent & model,T * source,T * target)1014 GestureEvent(const GestureEvent& model, T* source, T* target) 1015 : LocatedEvent(model, source, target), 1016 details_(model.details_) { 1017 } 1018 GestureEvent(const GestureEvent& copy); 1019 ~GestureEvent() override; 1020 details()1021 const GestureEventDetails& details() const { return details_; } 1022 unique_touch_event_id()1023 uint32_t unique_touch_event_id() const { 1024 return unique_touch_event_id_; 1025 } 1026 1027 private: 1028 GestureEventDetails details_; 1029 1030 // The unique id of the touch event that caused the gesture event to be 1031 // dispatched. This field gets a non-zero value only for gestures that are 1032 // released through TouchDispositionGestureFilter::SendGesture. The gesture 1033 // events that aren't fired directly in response to processing a touch-event 1034 // (e.g. timer fired ones), this id is zero. See crbug.com/618738. 1035 uint32_t unique_touch_event_id_; 1036 }; 1037 1038 } // namespace ui 1039 1040 #endif // UI_EVENTS_EVENT_H_ 1041