Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: ui/base/event.h

Issue 10917075: events: Move some files into ui/base/events/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/cocoa/events_mac_unittest.mm ('k') | ui/base/event.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 bool IsKeyEvent() const {
59 return type_ == ET_KEY_PRESSED ||
60 type_ == ET_KEY_RELEASED ||
61 type_ == ET_TRANSLATED_KEY_PRESS ||
62 type_ == ET_TRANSLATED_KEY_RELEASE;
63 }
64
65 bool IsMouseEvent() const {
66 return type_ == ET_MOUSE_PRESSED ||
67 type_ == ET_MOUSE_DRAGGED ||
68 type_ == ET_MOUSE_RELEASED ||
69 type_ == ET_MOUSE_MOVED ||
70 type_ == ET_MOUSE_ENTERED ||
71 type_ == ET_MOUSE_EXITED ||
72 type_ == ET_MOUSEWHEEL;
73 }
74
75 bool IsTouchEvent() const {
76 return type_ == ET_TOUCH_RELEASED ||
77 type_ == ET_TOUCH_PRESSED ||
78 type_ == ET_TOUCH_MOVED ||
79 type_ == ET_TOUCH_STATIONARY ||
80 type_ == ET_TOUCH_CANCELLED;
81 }
82
83 bool IsGestureEvent() const {
84 switch (type_) {
85 case ET_GESTURE_SCROLL_BEGIN:
86 case ET_GESTURE_SCROLL_END:
87 case ET_GESTURE_SCROLL_UPDATE:
88 case ET_GESTURE_TAP:
89 case ET_GESTURE_TAP_DOWN:
90 case ET_GESTURE_BEGIN:
91 case ET_GESTURE_END:
92 case ET_GESTURE_DOUBLE_TAP:
93 case ET_GESTURE_TWO_FINGER_TAP:
94 case ET_GESTURE_PINCH_BEGIN:
95 case ET_GESTURE_PINCH_END:
96 case ET_GESTURE_PINCH_UPDATE:
97 case ET_GESTURE_LONG_PRESS:
98 case ET_GESTURE_MULTIFINGER_SWIPE:
99 return true;
100
101 case ET_SCROLL_FLING_CANCEL:
102 case ET_SCROLL_FLING_START:
103 // These can be ScrollEvents too. But for ScrollEvents have valid native
104 // events. No gesture events have native events.
105 return !HasNativeEvent();
106
107 default:
108 break;
109 }
110 return false;
111 }
112
113 bool IsScrollEvent() const {
114 return type_ == ET_SCROLL ||
115 ((type_ == ET_SCROLL_FLING_START ||
116 type_ == ET_SCROLL_FLING_CANCEL) && HasNativeEvent());
117 }
118
119 bool IsScrollGestureEvent() const {
120 return type_ == ET_GESTURE_SCROLL_BEGIN ||
121 type_ == ET_GESTURE_SCROLL_UPDATE ||
122 type_ == ET_GESTURE_SCROLL_END;
123 }
124
125 bool IsFlingScrollEvent() const {
126 return type_ == ET_SCROLL_FLING_CANCEL ||
127 type_ == ET_SCROLL_FLING_START;
128 }
129
130 // Returns true if the event has a valid |native_event_|.
131 bool HasNativeEvent() const;
132
133 protected:
134 Event(EventType type, int flags);
135 Event(const base::NativeEvent& native_event, EventType type, int flags);
136 Event(const Event& copy);
137 void set_type(EventType type) { type_ = type; }
138 void set_delete_native_event(bool delete_native_event) {
139 delete_native_event_ = delete_native_event;
140 }
141 void set_time_stamp(base::TimeDelta time_stamp) { time_stamp_ = time_stamp; }
142
143 private:
144 void operator=(const Event&);
145
146 // Safely initializes the native event members of this class.
147 void Init();
148 void InitWithNativeEvent(const base::NativeEvent& native_event);
149
150 base::NativeEvent native_event_;
151 EventType type_;
152 base::TimeDelta time_stamp_;
153 int flags_;
154 bool delete_native_event_;
155 };
156
157 class UI_EXPORT LocatedEvent : public Event {
158 public:
159 // For testing.
160 class TestApi : public Event::TestApi {
161 public:
162 explicit TestApi(LocatedEvent* located_event)
163 : Event::TestApi(located_event),
164 located_event_(located_event) {}
165
166 void set_location(const gfx::Point& location) {
167 located_event_->location_ = location;
168 }
169
170 private:
171 TestApi();
172 LocatedEvent* located_event_;
173 };
174
175 virtual ~LocatedEvent();
176
177 int x() const { return location_.x(); }
178 int y() const { return location_.y(); }
179 gfx::Point location() const { return location_; }
180 gfx::Point root_location() const { return root_location_; }
181
182 bool valid_system_location() const { return valid_system_location_; }
183 void set_system_location(const gfx::Point& loc) {
184 valid_system_location_ = true;
185 system_location_ = loc;
186 }
187 const gfx::Point& system_location() const { return system_location_; }
188
189 // Applies |root_transform| to the event.
190 // This is applied to both |location_| and |root_location_|.
191 virtual void UpdateForRootTransform(const Transform& root_transform);
192
193 template <class T> void ConvertLocationToTarget(T* source, T* target) {
194 if (target && target != source)
195 T::ConvertPointToTarget(source, target, &location_);
196 }
197
198 protected:
199 explicit LocatedEvent(const base::NativeEvent& native_event);
200
201 // Create a new LocatedEvent which is identical to the provided model.
202 // If source / target windows are provided, the model location will be
203 // converted from |source| coordinate system to |target| coordinate system.
204 template <class T>
205 LocatedEvent(const LocatedEvent& model, T* source, T* target)
206 : Event(model),
207 location_(model.location_),
208 root_location_(model.root_location_),
209 valid_system_location_(model.valid_system_location_),
210 system_location_(model.system_location_) {
211 // TODO(erg): May need to create system_location_ by converting location to
212 // system coordinates here.
213 ConvertLocationToTarget(source, target);
214 }
215
216 // Used for synthetic events in testing.
217 LocatedEvent(EventType type,
218 const gfx::Point& location,
219 const gfx::Point& root_location,
220 int flags);
221
222 gfx::Point location_;
223
224 // |location_| multiplied by an optional transformation matrix for
225 // rotations, animations and skews.
226 gfx::Point root_location_;
227
228 // |location_| in underlying system screen coordinates. This can be invalid
229 // |during synthesized events if a location isn't explicitly set.
230 bool valid_system_location_;
231 gfx::Point system_location_;
232 };
233
234 class UI_EXPORT MouseEvent : public LocatedEvent {
235 public:
236 explicit MouseEvent(const base::NativeEvent& native_event);
237
238 // Create a new MouseEvent based on the provided model.
239 // Uses the provided |type| and |flags| for the new event.
240 // If source / target windows are provided, the model location will be
241 // converted from |source| coordinate system to |target| coordinate system.
242 template <class T>
243 MouseEvent(const MouseEvent& model, T* source, T* target)
244 : LocatedEvent(model, source, target),
245 changed_button_flags_(model.changed_button_flags_) {
246 }
247
248 template <class T>
249 MouseEvent(const MouseEvent& model,
250 T* source,
251 T* target,
252 EventType type,
253 int flags)
254 : LocatedEvent(model, source, target),
255 changed_button_flags_(model.changed_button_flags_) {
256 set_type(type);
257 set_flags(flags);
258 }
259
260 // Used for synthetic events in testing and by the gesture recognizer.
261 MouseEvent(EventType type,
262 const gfx::Point& location,
263 const gfx::Point& root_location,
264 int flags);
265
266 // Conveniences to quickly test what button is down
267 bool IsOnlyLeftMouseButton() const {
268 return (flags() & EF_LEFT_MOUSE_BUTTON) &&
269 !(flags() & (EF_MIDDLE_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
270 }
271
272 bool IsLeftMouseButton() const {
273 return (flags() & EF_LEFT_MOUSE_BUTTON) != 0;
274 }
275
276 bool IsOnlyMiddleMouseButton() const {
277 return (flags() & EF_MIDDLE_MOUSE_BUTTON) &&
278 !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
279 }
280
281 bool IsMiddleMouseButton() const {
282 return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0;
283 }
284
285 bool IsOnlyRightMouseButton() const {
286 return (flags() & EF_RIGHT_MOUSE_BUTTON) &&
287 !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON));
288 }
289
290 bool IsRightMouseButton() const {
291 return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0;
292 }
293
294 // Compares two mouse down events and returns true if the second one should
295 // be considered a repeat of the first.
296 static bool IsRepeatedClickEvent(
297 const MouseEvent& event1,
298 const MouseEvent& event2);
299
300 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise.
301 int GetClickCount() const;
302
303 // Set the click count for a mousedown message. Can be 1, 2 or 3.
304 void SetClickCount(int click_count);
305
306 // Identifies the button that changed. During a press this corresponds to the
307 // button that was pressed and during a release this corresponds to the button
308 // that was released.
309 // NOTE: during a press and release flags() contains the complete set of
310 // flags. Use this to determine the button that was pressed or released.
311 int changed_button_flags() const { return changed_button_flags_; }
312
313 private:
314 // Returns the repeat count based on the previous mouse click, if it is
315 // recent enough and within a small enough distance.
316 static int GetRepeatCount(const MouseEvent& click_event);
317
318 gfx::Point root_location_;
319
320 // See description above getter for details.
321 int changed_button_flags_;
322
323 static MouseEvent* last_click_event_;
324 };
325
326 class ScrollEvent;
327
328 class UI_EXPORT MouseWheelEvent : public MouseEvent {
329 public:
330 // See |offset| for details.
331 static const int kWheelDelta;
332
333 explicit MouseWheelEvent(const base::NativeEvent& native_event);
334 explicit MouseWheelEvent(const ScrollEvent& scroll_event);
335
336 template <class T>
337 MouseWheelEvent(const MouseWheelEvent& model,
338 T* source,
339 T* target,
340 EventType type,
341 int flags)
342 : MouseEvent(model, source, target, type, flags),
343 offset_(model.offset_) {
344 }
345
346 // The amount to scroll. This is in multiples of kWheelDelta.
347 // Note: offset() > 0 means scroll up / left.
348 int offset() const { return offset_; }
349
350 private:
351 int offset_;
352
353 DISALLOW_COPY_AND_ASSIGN(MouseWheelEvent);
354 };
355
356 class UI_EXPORT TouchEvent : public LocatedEvent {
357 public:
358 explicit TouchEvent(const base::NativeEvent& native_event);
359
360 // Create a new TouchEvent which is identical to the provided model.
361 // If source / target windows are provided, the model location will be
362 // converted from |source| coordinate system to |target| coordinate system.
363 template <class T>
364 TouchEvent(const TouchEvent& model, T* source, T* target)
365 : LocatedEvent(model, source, target),
366 touch_id_(model.touch_id_),
367 radius_x_(model.radius_x_),
368 radius_y_(model.radius_y_),
369 rotation_angle_(model.rotation_angle_),
370 force_(model.force_) {
371 }
372
373 TouchEvent(EventType type,
374 const gfx::Point& root_location,
375 int touch_id,
376 base::TimeDelta time_stamp);
377
378 virtual ~TouchEvent();
379
380 int touch_id() const { return touch_id_; }
381 float radius_x() const { return radius_x_; }
382 float radius_y() const { return radius_y_; }
383 float rotation_angle() const { return rotation_angle_; }
384 float force() const { return force_; }
385
386 // Used for unit tests.
387 void set_radius_x(const float r) { radius_x_ = r; }
388 void set_radius_y(const float r) { radius_y_ = r; }
389
390 // Overridden from LocatedEvent.
391 virtual void UpdateForRootTransform(const Transform& root_transform) OVERRIDE;
392
393 protected:
394 void set_radius(float radius_x, float radius_y) {
395 radius_x_ = radius_x;
396 radius_y_ = radius_y;
397 }
398
399 void set_rotation_angle(float rotation_angle) {
400 rotation_angle_ = rotation_angle;
401 }
402
403 void set_force(float force) { force_ = force; }
404
405 private:
406 // The identity (typically finger) of the touch starting at 0 and incrementing
407 // for each separable additional touch that the hardware can detect.
408 const int touch_id_;
409
410 // Radius of the X (major) axis of the touch ellipse. 0.0 if unknown.
411 float radius_x_;
412
413 // Radius of the Y (minor) axis of the touch ellipse. 0.0 if unknown.
414 float radius_y_;
415
416 // Angle of the major axis away from the X axis. Default 0.0.
417 float rotation_angle_;
418
419 // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0.
420 float force_;
421
422 DISALLOW_COPY_AND_ASSIGN(TouchEvent);
423 };
424
425 class UI_EXPORT TestTouchEvent : public TouchEvent {
426 public:
427 // Create a new touch event.
428 TestTouchEvent(EventType type,
429 int x,
430 int y,
431 int flags,
432 int touch_id,
433 float radius_x,
434 float radius_y,
435 float angle,
436 float force);
437 private:
438 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent);
439 };
440
441 class UI_EXPORT KeyEvent : public Event {
442 public:
443 KeyEvent(const base::NativeEvent& native_event, bool is_char);
444
445 // Used for synthetic events in testing.
446 KeyEvent(EventType type, KeyboardCode key_code, int flags);
447
448 // These setters allow an I18N virtual keyboard to fabricate a keyboard event
449 // which does not have a corresponding KeyboardCode (example: U+00E1 Latin
450 // small letter A with acute, U+0410 Cyrillic capital letter A.)
451 // GetCharacter() and GetUnmodifiedCharacter() return the character.
452 void set_character(uint16 character) { character_ = character; }
453 void set_unmodified_character(uint16 unmodified_character) {
454 unmodified_character_ = unmodified_character;
455 }
456
457 // Gets the character generated by this key event. It only supports Unicode
458 // BMP characters.
459 uint16 GetCharacter() const;
460
461 // Gets the character generated by this key event ignoring concurrently-held
462 // modifiers (except shift).
463 uint16 GetUnmodifiedCharacter() const;
464
465 // Returns the copy of this key event. Used in NativeWebKeyboardEvent.
466 KeyEvent* Copy();
467
468 KeyboardCode key_code() const { return key_code_; }
469 bool is_char() const { return is_char_; }
470
471 // This is only intended to be used externally by classes that are modifying
472 // events in EventFilter::PreHandleKeyEvent(). set_character() should also be
473 // called.
474 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; }
475
476 // Normalizes flags_ to make it Windows/Mac compatible. Since the way
477 // of setting modifier mask on X is very different than Windows/Mac as shown
478 // in http://crbug.com/127142#c8, the normalization is necessary.
479 void NormalizeFlags();
480
481 private:
482 KeyboardCode key_code_;
483 // True if this is a translated character event (vs. a raw key down). Both
484 // share the same type: ET_KEY_PRESSED.
485 bool is_char_;
486
487 uint16 character_;
488 uint16 unmodified_character_;
489
490 DISALLOW_COPY_AND_ASSIGN(KeyEvent);
491 };
492
493 // A key event which is translated by an input method (IME).
494 // For example, if an IME receives a KeyEvent(VKEY_SPACE), and it does not
495 // consume the key, the IME usually generates and dispatches a
496 // TranslatedKeyEvent(VKEY_SPACE) event. If the IME receives a KeyEvent and
497 // it does consume the event, it might dispatch a
498 // TranslatedKeyEvent(VKEY_PROCESSKEY) event as defined in the DOM spec.
499 class UI_EXPORT TranslatedKeyEvent : public KeyEvent {
500 public:
501 TranslatedKeyEvent(const base::NativeEvent& native_event, bool is_char);
502
503 // Used for synthetic events such as a VKEY_PROCESSKEY key event.
504 TranslatedKeyEvent(bool is_press, KeyboardCode key_code, int flags);
505
506 // Changes the type() of the object from ET_TRANSLATED_KEY_* to ET_KEY_* so
507 // that RenderWidgetHostViewAura and NativeWidgetAura could handle the event.
508 void ConvertToKeyEvent();
509
510 private:
511 DISALLOW_COPY_AND_ASSIGN(TranslatedKeyEvent);
512 };
513
514 class UI_EXPORT DropTargetEvent : public LocatedEvent {
515 public:
516 DropTargetEvent(const OSExchangeData& data,
517 const gfx::Point& location,
518 const gfx::Point& root_location,
519 int source_operations)
520 : LocatedEvent(ET_DROP_TARGET_EVENT, location, root_location, 0),
521 data_(data),
522 source_operations_(source_operations) {
523 }
524
525 const OSExchangeData& data() const { return data_; }
526 int source_operations() const { return source_operations_; }
527
528 private:
529 // Data associated with the drag/drop session.
530 const OSExchangeData& data_;
531
532 // Bitmask of supported DragDropTypes::DragOperation by the source.
533 int source_operations_;
534
535 DISALLOW_COPY_AND_ASSIGN(DropTargetEvent);
536 };
537
538 class UI_EXPORT ScrollEvent : public MouseEvent {
539 public:
540 explicit ScrollEvent(const base::NativeEvent& native_event);
541 template <class T>
542 ScrollEvent(const ScrollEvent& model,
543 T* source,
544 T* target,
545 EventType type,
546 int flags)
547 : MouseEvent(model, source, target, type, flags),
548 x_offset_(model.x_offset_),
549 y_offset_(model.y_offset_) {
550 }
551
552 float x_offset() const { return x_offset_; }
553 float y_offset() const { return y_offset_; }
554
555 private:
556 float x_offset_;
557 float y_offset_;
558
559 DISALLOW_COPY_AND_ASSIGN(ScrollEvent);
560 };
561
562 class UI_EXPORT GestureEvent : public LocatedEvent {
563 public:
564 GestureEvent(EventType type,
565 int x,
566 int y,
567 int flags,
568 base::TimeDelta time_stamp,
569 const GestureEventDetails& details,
570 unsigned int touch_ids_bitfield);
571
572 // Create a new GestureEvent which is identical to the provided model.
573 // If source / target windows are provided, the model location will be
574 // converted from |source| coordinate system to |target| coordinate system.
575 template <typename T>
576 GestureEvent(const GestureEvent& model, T* source, T* target)
577 : LocatedEvent(model, source, target),
578 details_(model.details_),
579 touch_ids_bitfield_(model.touch_ids_bitfield_) {
580 }
581
582 virtual ~GestureEvent();
583
584 const GestureEventDetails& details() const { return details_; }
585
586 // Returns the lowest touch-id of any of the touches which make up this
587 // gesture. If there are no touches associated with this gesture, returns -1.
588 int GetLowestTouchId() const;
589
590 private:
591 GestureEventDetails details_;
592
593 // The set of indices of ones in the binary representation of
594 // touch_ids_bitfield_ is the set of touch_ids associate with this gesture.
595 // This value is stored as a bitfield because the number of touch ids varies,
596 // but we currently don't need more than 32 touches at a time.
597 const unsigned int touch_ids_bitfield_;
598
599 DISALLOW_COPY_AND_ASSIGN(GestureEvent);
600 };
601
602 } // namespace ui
603
604 #endif // UI_BASE_EVENT_H_
OLDNEW
« no previous file with comments | « ui/base/cocoa/events_mac_unittest.mm ('k') | ui/base/event.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698