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 "ui/views/events/event.h" |
10 #include "base/time.h" | 10 // TODO(tfarina): remove this file once all includes have been updated. |
11 #include "ui/base/events.h" | |
12 #include "ui/base/keycodes/keyboard_codes.h" | |
13 #include "ui/gfx/point.h" | |
14 #include "views/views_export.h" | |
15 | |
16 namespace ui { | |
17 class OSExchangeData; | |
18 } | |
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 | |
30 | |
31 namespace views { | |
32 | |
33 #if defined(USE_AURA) | |
34 typedef aura::Event* NativeEvent; | |
35 #else | |
36 typedef base::NativeEvent NativeEvent; | |
37 #endif | |
38 | |
39 class View; | |
40 | |
41 namespace internal { | |
42 class NativeWidgetView; | |
43 class RootView; | |
44 } | |
45 | |
46 //////////////////////////////////////////////////////////////////////////////// | |
47 // | |
48 // Event class | |
49 // | |
50 // An event encapsulates an input event that can be propagated into view | |
51 // hierarchies. An event has a type, some flags and a time stamp. | |
52 // | |
53 // Each major event type has a corresponding Event subclass. | |
54 // | |
55 // Events are immutable but support copy | |
56 // | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 class VIEWS_EXPORT Event { | |
59 public: | |
60 const NativeEvent& native_event() const { return native_event_; } | |
61 #if defined(TOOLKIT_USES_GTK) | |
62 GdkEvent* gdk_event() const { return gdk_event_; } | |
63 #endif | |
64 ui::EventType type() const { return type_; } | |
65 const base::Time& time_stamp() const { return time_stamp_; } | |
66 int flags() const { return flags_; } | |
67 void set_flags(int flags) { flags_ = flags; } | |
68 | |
69 // The following methods return true if the respective keys were pressed at | |
70 // the time the event was created. | |
71 bool IsShiftDown() const { return (flags_ & ui::EF_SHIFT_DOWN) != 0; } | |
72 bool IsControlDown() const { return (flags_ & ui::EF_CONTROL_DOWN) != 0; } | |
73 bool IsCapsLockDown() const { return (flags_ & ui::EF_CAPS_LOCK_DOWN) != 0; } | |
74 bool IsAltDown() const { return (flags_ & ui::EF_ALT_DOWN) != 0; } | |
75 | |
76 bool IsMouseEvent() const { | |
77 return type_ == ui::ET_MOUSE_PRESSED || | |
78 type_ == ui::ET_MOUSE_DRAGGED || | |
79 type_ == ui::ET_MOUSE_RELEASED || | |
80 type_ == ui::ET_MOUSE_MOVED || | |
81 type_ == ui::ET_MOUSE_ENTERED || | |
82 type_ == ui::ET_MOUSE_EXITED || | |
83 type_ == ui::ET_MOUSEWHEEL; | |
84 } | |
85 | |
86 bool IsTouchEvent() const { | |
87 return type_ == ui::ET_TOUCH_RELEASED || | |
88 type_ == ui::ET_TOUCH_PRESSED || | |
89 type_ == ui::ET_TOUCH_MOVED || | |
90 type_ == ui::ET_TOUCH_STATIONARY || | |
91 type_ == ui::ET_TOUCH_CANCELLED; | |
92 } | |
93 | |
94 protected: | |
95 Event(ui::EventType type, int flags); | |
96 Event(const NativeEvent& native_event, ui::EventType type, int flags); | |
97 #if defined(TOOLKIT_USES_GTK) | |
98 Event(GdkEvent* gdk_event, ui::EventType type, int flags); | |
99 #endif | |
100 | |
101 Event(const Event& model) | |
102 : native_event_(model.native_event()), | |
103 #if defined(TOOLKIT_USES_GTK) | |
104 gdk_event_(model.gdk_event_), | |
105 #endif | |
106 type_(model.type()), | |
107 time_stamp_(model.time_stamp()), | |
108 flags_(model.flags()) { | |
109 } | |
110 | |
111 void set_type(ui::EventType type) { type_ = type; } | |
112 | |
113 private: | |
114 void operator=(const Event&); | |
115 | |
116 NativeEvent native_event_; | |
117 #if defined(TOOLKIT_USES_GTK) | |
118 GdkEvent* gdk_event_; | |
119 #endif | |
120 ui::EventType type_; | |
121 base::Time time_stamp_; | |
122 int flags_; | |
123 }; | |
124 | |
125 //////////////////////////////////////////////////////////////////////////////// | |
126 // | |
127 // LocatedEvent class | |
128 // | |
129 // A generic event that is used for any events that is located at a specific | |
130 // position in the screen. | |
131 // | |
132 //////////////////////////////////////////////////////////////////////////////// | |
133 class VIEWS_EXPORT LocatedEvent : public Event { | |
134 public: | |
135 int x() const { return location_.x(); } | |
136 int y() const { return location_.y(); } | |
137 const gfx::Point& location() const { return location_; } | |
138 | |
139 protected: | |
140 explicit LocatedEvent(const NativeEvent& native_event); | |
141 #if defined(TOOLKIT_USES_GTK) | |
142 explicit LocatedEvent(GdkEvent* gdk_event); | |
143 #endif | |
144 | |
145 // TODO(msw): Kill this legacy constructor when we update uses. | |
146 // Simple initialization from cracked metadata. | |
147 LocatedEvent(ui::EventType type, const gfx::Point& location, int flags); | |
148 | |
149 // Create a new LocatedEvent which is identical to the provided model. | |
150 // If source / target views are provided, the model location will be converted | |
151 // from |source| coordinate system to |target| coordinate system. | |
152 LocatedEvent(const LocatedEvent& model, View* source, View* target); | |
153 | |
154 // This constructor is to allow converting the location of an event from the | |
155 // widget's coordinate system to the RootView's coordinate system. | |
156 LocatedEvent(const LocatedEvent& model, View* root); | |
157 | |
158 gfx::Point location_; | |
159 }; | |
160 | |
161 class TouchEvent; | |
162 | |
163 //////////////////////////////////////////////////////////////////////////////// | |
164 // | |
165 // MouseEvent class | |
166 // | |
167 // A mouse event is used for any input event related to the mouse. | |
168 // | |
169 //////////////////////////////////////////////////////////////////////////////// | |
170 class VIEWS_EXPORT MouseEvent : public LocatedEvent { | |
171 public: | |
172 explicit MouseEvent(const NativeEvent& native_event); | |
173 #if defined(TOOLKIT_USES_GTK) | |
174 explicit MouseEvent(GdkEvent* gdk_event); | |
175 #endif | |
176 | |
177 // Create a new MouseEvent which is identical to the provided model. | |
178 // If source / target views are provided, the model location will be converted | |
179 // from |source| coordinate system to |target| coordinate system. | |
180 MouseEvent(const MouseEvent& model, View* source, View* target); | |
181 | |
182 // Creates a new MouseEvent from a TouchEvent. The location of the TouchEvent | |
183 // is the same as the MouseEvent. Other attributes (e.g. type, flags) are | |
184 // mapped from the TouchEvent to appropriate MouseEvent attributes. | |
185 // GestureManager uses this to convert TouchEvents that are not handled by any | |
186 // view. | |
187 explicit MouseEvent(const TouchEvent& touch); | |
188 | |
189 // TODO(msw): Kill this legacy constructor when we update uses. | |
190 // Create a new mouse event | |
191 MouseEvent(ui::EventType type, int x, int y, int flags) | |
192 : LocatedEvent(type, gfx::Point(x, y), flags) { | |
193 } | |
194 | |
195 // Conveniences to quickly test what button is down | |
196 bool IsOnlyLeftMouseButton() const { | |
197 return (flags() & ui::EF_LEFT_BUTTON_DOWN) && | |
198 !(flags() & (ui::EF_MIDDLE_BUTTON_DOWN | ui::EF_RIGHT_BUTTON_DOWN)); | |
199 } | |
200 | |
201 bool IsLeftMouseButton() const { | |
202 return (flags() & ui::EF_LEFT_BUTTON_DOWN) != 0; | |
203 } | |
204 | |
205 bool IsOnlyMiddleMouseButton() const { | |
206 return (flags() & ui::EF_MIDDLE_BUTTON_DOWN) && | |
207 !(flags() & (ui::EF_LEFT_BUTTON_DOWN | ui::EF_RIGHT_BUTTON_DOWN)); | |
208 } | |
209 | |
210 bool IsMiddleMouseButton() const { | |
211 return (flags() & ui::EF_MIDDLE_BUTTON_DOWN) != 0; | |
212 } | |
213 | |
214 bool IsOnlyRightMouseButton() const { | |
215 return (flags() & ui::EF_RIGHT_BUTTON_DOWN) && | |
216 !(flags() & (ui::EF_LEFT_BUTTON_DOWN | ui::EF_MIDDLE_BUTTON_DOWN)); | |
217 } | |
218 | |
219 bool IsRightMouseButton() const { | |
220 return (flags() & ui::EF_RIGHT_BUTTON_DOWN) != 0; | |
221 } | |
222 | |
223 protected: | |
224 MouseEvent(const MouseEvent& model, View* root) | |
225 : LocatedEvent(model, root) { | |
226 } | |
227 | |
228 private: | |
229 friend class internal::NativeWidgetView; | |
230 friend class internal::RootView; | |
231 | |
232 DISALLOW_COPY_AND_ASSIGN(MouseEvent); | |
233 }; | |
234 | |
235 //////////////////////////////////////////////////////////////////////////////// | |
236 // | |
237 // TouchEvent class | |
238 // | |
239 // A touch event is generated by touch screen and advanced track | |
240 // pad devices. There is a deliberate direct correspondence between | |
241 // TouchEvent and PlatformTouchPoint. | |
242 // | |
243 //////////////////////////////////////////////////////////////////////////////// | |
244 class VIEWS_EXPORT TouchEvent : public LocatedEvent { | |
245 public: | |
246 explicit TouchEvent(const NativeEvent& native_event); | |
247 | |
248 // Create a new touch event. | |
249 TouchEvent(ui::EventType type, | |
250 int x, | |
251 int y, | |
252 int flags, | |
253 int touch_id, | |
254 float radius_x, | |
255 float radius_y, | |
256 float angle, | |
257 float force); | |
258 | |
259 // Create a new TouchEvent which is identical to the provided model. | |
260 // If source / target views are provided, the model location will be converted | |
261 // from |source| coordinate system to |target| coordinate system. | |
262 TouchEvent(const TouchEvent& model, View* source, View* target); | |
263 | |
264 int identity() const { return touch_id_; } | |
265 | |
266 float radius_x() const { return radius_x_; } | |
267 float radius_y() const { return radius_y_; } | |
268 float rotation_angle() const { return rotation_angle_; } | |
269 float force() const { return force_; } | |
270 | |
271 private: | |
272 friend class internal::NativeWidgetView; | |
273 friend class internal::RootView; | |
274 | |
275 TouchEvent(const TouchEvent& model, View* root); | |
276 | |
277 // The identity (typically finger) of the touch starting at 0 and incrementing | |
278 // for each separable additional touch that the hardware can detect. | |
279 const int touch_id_; | |
280 | |
281 // Radius of the X (major) axis of the touch ellipse. 1.0 if unknown. | |
282 const float radius_x_; | |
283 | |
284 // Radius of the Y (minor) axis of the touch ellipse. 1.0 if unknown. | |
285 const float radius_y_; | |
286 | |
287 // Angle of the major axis away from the X axis. Default 0.0. | |
288 const float rotation_angle_; | |
289 | |
290 // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0. | |
291 const float force_; | |
292 | |
293 DISALLOW_COPY_AND_ASSIGN(TouchEvent); | |
294 }; | |
295 | |
296 //////////////////////////////////////////////////////////////////////////////// | |
297 // KeyEvent class | |
298 // | |
299 // KeyEvent encapsulates keyboard input events - key press and release. | |
300 // | |
301 //////////////////////////////////////////////////////////////////////////////// | |
302 class VIEWS_EXPORT KeyEvent : public Event { | |
303 public: | |
304 explicit KeyEvent(const NativeEvent& native_event); | |
305 #if defined(TOOLKIT_USES_GTK) | |
306 explicit KeyEvent(GdkEvent* gdk_event); | |
307 #endif | |
308 | |
309 // Creates a new KeyEvent synthetically (i.e. not in response to an input | |
310 // event from the host environment). This is typically only used in testing as | |
311 // some metadata obtainable from the underlying native event is not present. | |
312 // It's also used by input methods to fabricate keyboard events. | |
313 KeyEvent(ui::EventType type, | |
314 ui::KeyboardCode key_code, | |
315 int event_flags); | |
316 | |
317 ui::KeyboardCode key_code() const { return key_code_; } | |
318 | |
319 // These setters allow an I18N virtual keyboard to fabricate a keyboard event | |
320 // which does not have a corresponding ui::KeyboardCode (example: U+00E1 Latin | |
321 // small letter A with acute, U+0410 Cyrillic capital letter A.) | |
322 // GetCharacter() and GetUnmodifiedCharacter() return the character. | |
323 void set_character(uint16 character) { character_ = character; } | |
324 void set_unmodified_character(uint16 unmodified_character) { | |
325 unmodified_character_ = unmodified_character; | |
326 } | |
327 | |
328 // Gets the character generated by this key event. It only supports Unicode | |
329 // BMP characters. | |
330 uint16 GetCharacter() const; | |
331 | |
332 // Gets the character generated by this key event ignoring concurrently-held | |
333 // modifiers (except shift). | |
334 uint16 GetUnmodifiedCharacter() const; | |
335 | |
336 private: | |
337 ui::KeyboardCode key_code_; | |
338 | |
339 uint16 character_; | |
340 uint16 unmodified_character_; | |
341 | |
342 DISALLOW_COPY_AND_ASSIGN(KeyEvent); | |
343 }; | |
344 | |
345 //////////////////////////////////////////////////////////////////////////////// | |
346 // | |
347 // MouseWheelEvent class | |
348 // | |
349 // A MouseWheelEvent is used to propagate mouse wheel user events. | |
350 // Note: e.GetOffset() > 0 means scroll up / left. | |
351 // | |
352 //////////////////////////////////////////////////////////////////////////////// | |
353 class VIEWS_EXPORT MouseWheelEvent : public MouseEvent { | |
354 public: | |
355 // See |offset| for details. | |
356 static const int kWheelDelta; | |
357 | |
358 explicit MouseWheelEvent(const NativeEvent& native_event); | |
359 #if defined(TOOLKIT_USES_GTK) | |
360 explicit MouseWheelEvent(GdkEvent* gdk_event); | |
361 #endif | |
362 | |
363 // The amount to scroll. This is in multiples of kWheelDelta. | |
364 int offset() const { return offset_; } | |
365 | |
366 private: | |
367 friend class internal::RootView; | |
368 friend class internal::NativeWidgetView; | |
369 | |
370 MouseWheelEvent(const MouseWheelEvent& model, View* root) | |
371 : MouseEvent(model, root), | |
372 offset_(model.offset_) { | |
373 } | |
374 | |
375 int offset_; | |
376 | |
377 DISALLOW_COPY_AND_ASSIGN(MouseWheelEvent); | |
378 }; | |
379 | |
380 //////////////////////////////////////////////////////////////////////////////// | |
381 // | |
382 // DropTargetEvent class | |
383 // | |
384 // A DropTargetEvent is sent to the view the mouse is over during a drag and | |
385 // drop operation. | |
386 // | |
387 //////////////////////////////////////////////////////////////////////////////// | |
388 class VIEWS_EXPORT DropTargetEvent : public LocatedEvent { | |
389 public: | |
390 DropTargetEvent(const ui::OSExchangeData& data, | |
391 int x, | |
392 int y, | |
393 int source_operations) | |
394 : LocatedEvent(ui::ET_DROP_TARGET_EVENT, gfx::Point(x, y), 0), | |
395 data_(data), | |
396 source_operations_(source_operations) { | |
397 // TODO(msw): Hook up key state flags for CTRL + drag and drop, etc. | |
398 } | |
399 | |
400 const ui::OSExchangeData& data() const { return data_; } | |
401 int source_operations() const { return source_operations_; } | |
402 | |
403 private: | |
404 // Data associated with the drag/drop session. | |
405 const ui::OSExchangeData& data_; | |
406 | |
407 // Bitmask of supported ui::DragDropTypes::DragOperation by the source. | |
408 int source_operations_; | |
409 | |
410 DISALLOW_COPY_AND_ASSIGN(DropTargetEvent); | |
411 }; | |
412 | |
413 } // namespace views | |
414 | 11 |
415 #endif // VIEWS_EVENTS_EVENT_H_ | 12 #endif // VIEWS_EVENTS_EVENT_H_ |
OLD | NEW |