OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VIEWS_EVENTS_EVENT_H_ | 5 #ifndef VIEWS_EVENTS_EVENT_H_ |
6 #define VIEWS_EVENTS_EVENT_H_ | 6 #define VIEWS_EVENTS_EVENT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "ui/base/events.h" | 11 #include "ui/base/events.h" |
12 #include "ui/base/keycodes/keyboard_codes.h" | 12 #include "ui/base/keycodes/keyboard_codes.h" |
13 #include "ui/gfx/point.h" | 13 #include "ui/gfx/point.h" |
14 #include "views/native_types.h" | |
15 #include "views/views_export.h" | 14 #include "views/views_export.h" |
16 | 15 |
17 #if defined(USE_X11) | |
18 typedef union _XEvent XEvent; | |
19 #endif | |
20 | |
21 namespace ui { | 16 namespace ui { |
22 class OSExchangeData; | 17 class OSExchangeData; |
23 } | 18 } |
24 using ui::OSExchangeData; | 19 |
| 20 #if defined(USE_AURA) |
| 21 namespace aura { |
| 22 class Event; |
| 23 } |
| 24 #endif |
| 25 |
| 26 // TODO(msw): Remove GTK support from views event code when possible. |
| 27 #if defined(TOOLKIT_USES_GTK) |
| 28 typedef union _GdkEvent GdkEvent; |
| 29 #endif |
25 | 30 |
26 namespace views { | 31 namespace views { |
27 | 32 |
| 33 #if defined(USE_AURA) |
| 34 typedef aura::Event* NativeEvent; |
| 35 #else |
| 36 typedef ui::NativeEvent NativeEvent; |
| 37 #endif |
| 38 |
28 class View; | 39 class View; |
29 | 40 |
30 namespace internal { | 41 namespace internal { |
31 class NativeWidgetView; | 42 class NativeWidgetView; |
32 class RootView; | 43 class RootView; |
33 } | 44 } |
34 | 45 |
35 #if defined(OS_WIN) || defined(USE_AURA) | |
36 VIEWS_EXPORT bool IsClientMouseEvent(const views::NativeEvent& native_event); | |
37 VIEWS_EXPORT bool IsNonClientMouseEvent(const views::NativeEvent& native_event); | |
38 #endif | |
39 | |
40 //////////////////////////////////////////////////////////////////////////////// | 46 //////////////////////////////////////////////////////////////////////////////// |
41 // | 47 // |
42 // Event class | 48 // Event class |
43 // | 49 // |
44 // An event encapsulates an input event that can be propagated into view | 50 // An event encapsulates an input event that can be propagated into view |
45 // hierarchies. An event has a type, some flags and a time stamp. | 51 // hierarchies. An event has a type, some flags and a time stamp. |
46 // | 52 // |
47 // Each major event type has a corresponding Event subclass. | 53 // Each major event type has a corresponding Event subclass. |
48 // | 54 // |
49 // Events are immutable but support copy | 55 // Events are immutable but support copy |
50 // | 56 // |
51 //////////////////////////////////////////////////////////////////////////////// | 57 //////////////////////////////////////////////////////////////////////////////// |
52 class VIEWS_EXPORT Event { | 58 class VIEWS_EXPORT Event { |
53 public: | 59 public: |
54 // This type exists to distinguish between the NativeEvent and NativeEvent2 | |
55 // constructors. | |
56 // TODO(beng): remove once we rid views of Gtk/Gdk. | |
57 struct FromNativeEvent2 {}; | |
58 | |
59 const NativeEvent& native_event() const { return native_event_; } | 60 const NativeEvent& native_event() const { return native_event_; } |
60 const NativeEvent2& native_event_2() const { return native_event_2_; } | 61 #if defined(TOOLKIT_USES_GTK) |
| 62 GdkEvent* gdk_event() const { return gdk_event_; } |
| 63 #endif |
61 ui::EventType type() const { return type_; } | 64 ui::EventType type() const { return type_; } |
62 const base::Time& time_stamp() const { return time_stamp_; } | 65 const base::Time& time_stamp() const { return time_stamp_; } |
63 int flags() const { return flags_; } | 66 int flags() const { return flags_; } |
64 void set_flags(int flags) { flags_ = flags; } | 67 void set_flags(int flags) { flags_ = flags; } |
65 | 68 |
66 // The following methods return true if the respective keys were pressed at | 69 // The following methods return true if the respective keys were pressed at |
67 // the time the event was created. | 70 // the time the event was created. |
68 bool IsShiftDown() const { return (flags_ & ui::EF_SHIFT_DOWN) != 0; } | 71 bool IsShiftDown() const { return (flags_ & ui::EF_SHIFT_DOWN) != 0; } |
69 bool IsControlDown() const { return (flags_ & ui::EF_CONTROL_DOWN) != 0; } | 72 bool IsControlDown() const { return (flags_ & ui::EF_CONTROL_DOWN) != 0; } |
70 bool IsCapsLockDown() const { return (flags_ & ui::EF_CAPS_LOCK_DOWN) != 0; } | 73 bool IsCapsLockDown() const { return (flags_ & ui::EF_CAPS_LOCK_DOWN) != 0; } |
(...skipping 10 matching lines...) Expand all Loading... |
81 } | 84 } |
82 | 85 |
83 bool IsTouchEvent() const { | 86 bool IsTouchEvent() const { |
84 return type_ == ui::ET_TOUCH_RELEASED || | 87 return type_ == ui::ET_TOUCH_RELEASED || |
85 type_ == ui::ET_TOUCH_PRESSED || | 88 type_ == ui::ET_TOUCH_PRESSED || |
86 type_ == ui::ET_TOUCH_MOVED || | 89 type_ == ui::ET_TOUCH_MOVED || |
87 type_ == ui::ET_TOUCH_STATIONARY || | 90 type_ == ui::ET_TOUCH_STATIONARY || |
88 type_ == ui::ET_TOUCH_CANCELLED; | 91 type_ == ui::ET_TOUCH_CANCELLED; |
89 } | 92 } |
90 | 93 |
91 #if defined(OS_WIN) && !defined(USE_AURA) | |
92 // Returns the EventFlags in terms of windows flags. | |
93 int GetWindowsFlags() const; | |
94 #elif defined(OS_LINUX) | |
95 // Get the views::Event flags from a native GdkEvent. | |
96 static int GetFlagsFromGdkEvent(NativeEvent native_event); | |
97 #endif | |
98 | |
99 protected: | 94 protected: |
100 Event(ui::EventType type, int flags); | 95 Event(ui::EventType type, int flags); |
101 Event(NativeEvent native_event, ui::EventType type, int flags); | 96 Event(const NativeEvent& native_event, ui::EventType type, int flags); |
102 // Because the world is complicated, sometimes we have two different kinds of | 97 #if defined(TOOLKIT_USES_GTK) |
103 // NativeEvent in play in the same executable. See native_types.h for the tale | 98 Event(GdkEvent* gdk_event, ui::EventType type, int flags); |
104 // of woe. | 99 #endif |
105 Event(NativeEvent2 native_event, ui::EventType type, int flags, | |
106 FromNativeEvent2); | |
107 | 100 |
108 Event(const Event& model) | 101 Event(const Event& model) |
109 : native_event_(model.native_event()), | 102 : native_event_(model.native_event()), |
110 native_event_2_(model.native_event_2()), | |
111 type_(model.type()), | 103 type_(model.type()), |
112 time_stamp_(model.time_stamp()), | 104 time_stamp_(model.time_stamp()), |
113 flags_(model.flags()) { | 105 flags_(model.flags()) { |
114 } | 106 } |
115 | 107 |
116 void set_type(ui::EventType type) { type_ = type; } | 108 void set_type(ui::EventType type) { type_ = type; } |
117 | 109 |
118 private: | 110 private: |
119 void operator=(const Event&); | 111 void operator=(const Event&); |
120 | 112 |
121 // Safely initializes the native event members of this class. | |
122 void Init(); | |
123 void InitWithNativeEvent(NativeEvent native_event); | |
124 void InitWithNativeEvent2(NativeEvent2 native_event_2, FromNativeEvent2); | |
125 | |
126 NativeEvent native_event_; | 113 NativeEvent native_event_; |
127 NativeEvent2 native_event_2_; | 114 #if defined(TOOLKIT_USES_GTK) |
| 115 GdkEvent* gdk_event_; |
| 116 #endif |
128 ui::EventType type_; | 117 ui::EventType type_; |
129 base::Time time_stamp_; | 118 base::Time time_stamp_; |
130 int flags_; | 119 int flags_; |
131 }; | 120 }; |
132 | 121 |
133 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
134 // | 123 // |
135 // LocatedEvent class | 124 // LocatedEvent class |
136 // | 125 // |
137 // A generic event that is used for any events that is located at a specific | 126 // A generic event that is used for any events that is located at a specific |
138 // position in the screen. | 127 // position in the screen. |
139 // | 128 // |
140 //////////////////////////////////////////////////////////////////////////////// | 129 //////////////////////////////////////////////////////////////////////////////// |
141 class VIEWS_EXPORT LocatedEvent : public Event { | 130 class VIEWS_EXPORT LocatedEvent : public Event { |
142 public: | 131 public: |
143 int x() const { return location_.x(); } | 132 int x() const { return location_.x(); } |
144 int y() const { return location_.y(); } | 133 int y() const { return location_.y(); } |
145 const gfx::Point& location() const { return location_; } | 134 const gfx::Point& location() const { return location_; } |
146 | 135 |
147 protected: | 136 protected: |
148 explicit LocatedEvent(NativeEvent native_event); | 137 explicit LocatedEvent(const NativeEvent& native_event); |
149 LocatedEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native); | 138 #if defined(TOOLKIT_USES_GTK) |
| 139 explicit LocatedEvent(GdkEvent* gdk_event); |
| 140 #endif |
150 | 141 |
151 // TODO(msw): Kill this legacy constructor when we update uses. | 142 // TODO(msw): Kill this legacy constructor when we update uses. |
152 // Simple initialization from cracked metadata. | 143 // Simple initialization from cracked metadata. |
153 LocatedEvent(ui::EventType type, const gfx::Point& location, int flags); | 144 LocatedEvent(ui::EventType type, const gfx::Point& location, int flags); |
154 | 145 |
155 // Create a new LocatedEvent which is identical to the provided model. | 146 // Create a new LocatedEvent which is identical to the provided model. |
156 // If source / target views are provided, the model location will be converted | 147 // If source / target views are provided, the model location will be converted |
157 // from |source| coordinate system to |target| coordinate system. | 148 // from |source| coordinate system to |target| coordinate system. |
158 LocatedEvent(const LocatedEvent& model, View* source, View* target); | 149 LocatedEvent(const LocatedEvent& model, View* source, View* target); |
159 | 150 |
160 // This constructor is to allow converting the location of an event from the | 151 // This constructor is to allow converting the location of an event from the |
161 // widget's coordinate system to the RootView's coordinate system. | 152 // widget's coordinate system to the RootView's coordinate system. |
162 LocatedEvent(const LocatedEvent& model, View* root); | 153 LocatedEvent(const LocatedEvent& model, View* root); |
163 | 154 |
164 gfx::Point location_; | 155 gfx::Point location_; |
165 }; | 156 }; |
166 | 157 |
167 class TouchEvent; | 158 class TouchEvent; |
168 | 159 |
169 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
170 // | 161 // |
171 // MouseEvent class | 162 // MouseEvent class |
172 // | 163 // |
173 // A mouse event is used for any input event related to the mouse. | 164 // A mouse event is used for any input event related to the mouse. |
174 // | 165 // |
175 //////////////////////////////////////////////////////////////////////////////// | 166 //////////////////////////////////////////////////////////////////////////////// |
176 class VIEWS_EXPORT MouseEvent : public LocatedEvent { | 167 class VIEWS_EXPORT MouseEvent : public LocatedEvent { |
177 public: | 168 public: |
178 explicit MouseEvent(NativeEvent native_event); | 169 explicit MouseEvent(const NativeEvent& native_event); |
179 MouseEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native); | 170 #if defined(TOOLKIT_USES_GTK) |
| 171 explicit MouseEvent(GdkEvent* gdk_event); |
| 172 #endif |
180 | 173 |
181 // Create a new MouseEvent which is identical to the provided model. | 174 // Create a new MouseEvent which is identical to the provided model. |
182 // If source / target views are provided, the model location will be converted | 175 // If source / target views are provided, the model location will be converted |
183 // from |source| coordinate system to |target| coordinate system. | 176 // from |source| coordinate system to |target| coordinate system. |
184 MouseEvent(const MouseEvent& model, View* source, View* target); | 177 MouseEvent(const MouseEvent& model, View* source, View* target); |
185 | 178 |
186 // Creates a new MouseEvent from a TouchEvent. The location of the TouchEvent | 179 // Creates a new MouseEvent from a TouchEvent. The location of the TouchEvent |
187 // is the same as the MouseEvent. Other attributes (e.g. type, flags) are | 180 // is the same as the MouseEvent. Other attributes (e.g. type, flags) are |
188 // mapped from the TouchEvent to appropriate MouseEvent attributes. | 181 // mapped from the TouchEvent to appropriate MouseEvent attributes. |
189 // GestureManager uses this to convert TouchEvents that are not handled by any | 182 // GestureManager uses this to convert TouchEvents that are not handled by any |
190 // view. | 183 // view. |
191 MouseEvent(const TouchEvent& touch, FromNativeEvent2 from_native); | 184 explicit MouseEvent(const TouchEvent& touch); |
192 | 185 |
193 // TODO(msw): Kill this legacy constructor when we update uses. | 186 // TODO(msw): Kill this legacy constructor when we update uses. |
194 // Create a new mouse event | 187 // Create a new mouse event |
195 MouseEvent(ui::EventType type, int x, int y, int flags) | 188 MouseEvent(ui::EventType type, int x, int y, int flags) |
196 : LocatedEvent(type, gfx::Point(x, y), flags) { | 189 : LocatedEvent(type, gfx::Point(x, y), flags) { |
197 } | 190 } |
198 | 191 |
199 // Conveniences to quickly test what button is down | 192 // Conveniences to quickly test what button is down |
200 bool IsOnlyLeftMouseButton() const { | 193 bool IsOnlyLeftMouseButton() const { |
201 return (flags() & ui::EF_LEFT_BUTTON_DOWN) && | 194 return (flags() & ui::EF_LEFT_BUTTON_DOWN) && |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // | 233 // |
241 // TouchEvent class | 234 // TouchEvent class |
242 // | 235 // |
243 // A touch event is generated by touch screen and advanced track | 236 // A touch event is generated by touch screen and advanced track |
244 // pad devices. There is a deliberate direct correspondence between | 237 // pad devices. There is a deliberate direct correspondence between |
245 // TouchEvent and PlatformTouchPoint. | 238 // TouchEvent and PlatformTouchPoint. |
246 // | 239 // |
247 //////////////////////////////////////////////////////////////////////////////// | 240 //////////////////////////////////////////////////////////////////////////////// |
248 class VIEWS_EXPORT TouchEvent : public LocatedEvent { | 241 class VIEWS_EXPORT TouchEvent : public LocatedEvent { |
249 public: | 242 public: |
250 TouchEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native); | 243 explicit TouchEvent(const NativeEvent& native_event); |
251 | 244 |
252 // Create a new touch event. | 245 // Create a new touch event. |
253 TouchEvent(ui::EventType type, | 246 TouchEvent(ui::EventType type, |
254 int x, | 247 int x, |
255 int y, | 248 int y, |
256 int flags, | 249 int flags, |
257 int touch_id, | 250 int touch_id, |
258 float radius_x, | 251 float radius_x, |
259 float radius_y, | 252 float radius_y, |
260 float angle, | 253 float angle, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 }; | 291 }; |
299 | 292 |
300 //////////////////////////////////////////////////////////////////////////////// | 293 //////////////////////////////////////////////////////////////////////////////// |
301 // KeyEvent class | 294 // KeyEvent class |
302 // | 295 // |
303 // KeyEvent encapsulates keyboard input events - key press and release. | 296 // KeyEvent encapsulates keyboard input events - key press and release. |
304 // | 297 // |
305 //////////////////////////////////////////////////////////////////////////////// | 298 //////////////////////////////////////////////////////////////////////////////// |
306 class VIEWS_EXPORT KeyEvent : public Event { | 299 class VIEWS_EXPORT KeyEvent : public Event { |
307 public: | 300 public: |
308 explicit KeyEvent(NativeEvent native_event); | 301 explicit KeyEvent(const NativeEvent& native_event); |
309 KeyEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native); | 302 #if defined(TOOLKIT_USES_GTK) |
| 303 explicit KeyEvent(GdkEvent* gdk_event); |
| 304 #endif |
310 | 305 |
311 // Creates a new KeyEvent synthetically (i.e. not in response to an input | 306 // Creates a new KeyEvent synthetically (i.e. not in response to an input |
312 // event from the host environment). This is typically only used in testing as | 307 // event from the host environment). This is typically only used in testing as |
313 // some metadata obtainable from the underlying native event is not present. | 308 // some metadata obtainable from the underlying native event is not present. |
314 // It's also used by input methods to fabricate keyboard events. | 309 // It's also used by input methods to fabricate keyboard events. |
315 KeyEvent(ui::EventType type, | 310 KeyEvent(ui::EventType type, |
316 ui::KeyboardCode key_code, | 311 ui::KeyboardCode key_code, |
317 int event_flags); | 312 int event_flags); |
318 | 313 |
319 ui::KeyboardCode key_code() const { return key_code_; } | 314 ui::KeyboardCode key_code() const { return key_code_; } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 // | 369 // |
375 // A MouseWheelEvent is used to propagate mouse wheel user events. | 370 // A MouseWheelEvent is used to propagate mouse wheel user events. |
376 // Note: e.GetOffset() > 0 means scroll up / left. | 371 // Note: e.GetOffset() > 0 means scroll up / left. |
377 // | 372 // |
378 //////////////////////////////////////////////////////////////////////////////// | 373 //////////////////////////////////////////////////////////////////////////////// |
379 class VIEWS_EXPORT MouseWheelEvent : public MouseEvent { | 374 class VIEWS_EXPORT MouseWheelEvent : public MouseEvent { |
380 public: | 375 public: |
381 // See |offset| for details. | 376 // See |offset| for details. |
382 static const int kWheelDelta; | 377 static const int kWheelDelta; |
383 | 378 |
384 explicit MouseWheelEvent(NativeEvent native_event); | 379 explicit MouseWheelEvent(const NativeEvent& native_event); |
385 MouseWheelEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native); | 380 #if defined(TOOLKIT_USES_GTK) |
| 381 explicit MouseWheelEvent(GdkEvent* gdk_event); |
| 382 #endif |
386 | 383 |
387 // The amount to scroll. This is in multiples of kWheelDelta. | 384 // The amount to scroll. This is in multiples of kWheelDelta. |
388 int offset() const { return offset_; } | 385 int offset() const { return offset_; } |
389 | 386 |
390 private: | 387 private: |
391 friend class internal::RootView; | 388 friend class internal::RootView; |
392 friend class internal::NativeWidgetView; | 389 friend class internal::NativeWidgetView; |
393 | 390 |
394 MouseWheelEvent(const MouseWheelEvent& model, View* root) | 391 MouseWheelEvent(const MouseWheelEvent& model, View* root) |
395 : MouseEvent(model, root), | 392 : MouseEvent(model, root), |
396 offset_(model.offset_) { | 393 offset_(model.offset_) { |
397 } | 394 } |
398 | 395 |
399 int offset_; | 396 int offset_; |
400 | 397 |
401 DISALLOW_COPY_AND_ASSIGN(MouseWheelEvent); | 398 DISALLOW_COPY_AND_ASSIGN(MouseWheelEvent); |
402 }; | 399 }; |
403 | 400 |
404 //////////////////////////////////////////////////////////////////////////////// | 401 //////////////////////////////////////////////////////////////////////////////// |
405 // | 402 // |
406 // DropTargetEvent class | 403 // DropTargetEvent class |
407 // | 404 // |
408 // A DropTargetEvent is sent to the view the mouse is over during a drag and | 405 // A DropTargetEvent is sent to the view the mouse is over during a drag and |
409 // drop operation. | 406 // drop operation. |
410 // | 407 // |
411 //////////////////////////////////////////////////////////////////////////////// | 408 //////////////////////////////////////////////////////////////////////////////// |
412 class VIEWS_EXPORT DropTargetEvent : public LocatedEvent { | 409 class VIEWS_EXPORT DropTargetEvent : public LocatedEvent { |
413 public: | 410 public: |
414 DropTargetEvent(const OSExchangeData& data, | 411 DropTargetEvent(const ui::OSExchangeData& data, |
415 int x, | 412 int x, |
416 int y, | 413 int y, |
417 int source_operations) | 414 int source_operations) |
418 : LocatedEvent(ui::ET_DROP_TARGET_EVENT, gfx::Point(x, y), 0), | 415 : LocatedEvent(ui::ET_DROP_TARGET_EVENT, gfx::Point(x, y), 0), |
419 data_(data), | 416 data_(data), |
420 source_operations_(source_operations) { | 417 source_operations_(source_operations) { |
421 // TODO(msw): Hook up key state flags for CTRL + drag and drop, etc. | 418 // TODO(msw): Hook up key state flags for CTRL + drag and drop, etc. |
422 } | 419 } |
423 | 420 |
424 const OSExchangeData& data() const { return data_; } | 421 const ui::OSExchangeData& data() const { return data_; } |
425 int source_operations() const { return source_operations_; } | 422 int source_operations() const { return source_operations_; } |
426 | 423 |
427 private: | 424 private: |
428 // Data associated with the drag/drop session. | 425 // Data associated with the drag/drop session. |
429 const OSExchangeData& data_; | 426 const ui::OSExchangeData& data_; |
430 | 427 |
431 // Bitmask of supported ui::DragDropTypes::DragOperation by the source. | 428 // Bitmask of supported ui::DragDropTypes::DragOperation by the source. |
432 int source_operations_; | 429 int source_operations_; |
433 | 430 |
434 DISALLOW_COPY_AND_ASSIGN(DropTargetEvent); | 431 DISALLOW_COPY_AND_ASSIGN(DropTargetEvent); |
435 }; | 432 }; |
436 | 433 |
437 } // namespace views | 434 } // namespace views |
438 | 435 |
439 #endif // VIEWS_EVENTS_EVENT_H_ | 436 #endif // VIEWS_EVENTS_EVENT_H_ |
OLD | NEW |