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 #include "ui/views/events/event.h" | 5 #include "ui/views/events/event.h" |
6 | 6 |
7 #include <windowsx.h> | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "ui/base/keycodes/keyboard_code_conversion_win.h" | 8 #include "ui/base/keycodes/keyboard_code_conversion_win.h" |
| 9 #include "ui/base/win/event_util.h" |
11 | 10 |
12 namespace ui { | 11 namespace ui { |
13 | 12 |
14 namespace { | |
15 | |
16 // Returns a mask corresponding to the set of modifier keys that are currently | |
17 // pressed. Windows key messages don't come with control key state as parameters | |
18 // as with mouse messages, so we need to explicitly ask for these states. | |
19 int GetKeyStateFlags() { | |
20 int flags = 0; | |
21 flags |= (GetKeyState(VK_MENU) & 0x80)? ui::EF_ALT_DOWN : 0; | |
22 flags |= (GetKeyState(VK_SHIFT) & 0x80)? ui::EF_SHIFT_DOWN : 0; | |
23 flags |= (GetKeyState(VK_CONTROL) & 0x80)? ui::EF_CONTROL_DOWN : 0; | |
24 return flags; | |
25 } | |
26 | |
27 // Convert windows message identifiers to Event types. | |
28 ui::Event::EventType EventTypeFromNative(NativeEvent native_event) { | |
29 switch (native_event.message) { | |
30 case WM_KEYDOWN: | |
31 case WM_SYSKEYDOWN: | |
32 case WM_CHAR: | |
33 return ui::Event::ET_KEY_PRESSED; | |
34 case WM_KEYUP: | |
35 case WM_SYSKEYUP: | |
36 return ui::Event::ET_KEY_RELEASED; | |
37 case WM_LBUTTONDOWN: | |
38 case WM_MBUTTONDOWN: | |
39 case WM_NCLBUTTONDOWN: | |
40 case WM_NCMBUTTONDOWN: | |
41 case WM_NCRBUTTONDOWN: | |
42 case WM_RBUTTONDOWN: | |
43 return ui::Event::ET_MOUSE_PRESSED; | |
44 case WM_LBUTTONDBLCLK: | |
45 case WM_LBUTTONUP: | |
46 case WM_MBUTTONDBLCLK: | |
47 case WM_MBUTTONUP: | |
48 case WM_NCLBUTTONDBLCLK: | |
49 case WM_NCLBUTTONUP: | |
50 case WM_NCMBUTTONDBLCLK: | |
51 case WM_NCMBUTTONUP: | |
52 case WM_NCRBUTTONDBLCLK: | |
53 case WM_NCRBUTTONUP: | |
54 case WM_RBUTTONDBLCLK: | |
55 case WM_RBUTTONUP: | |
56 return ui::Event::ET_MOUSE_RELEASED; | |
57 case WM_MOUSEMOVE: | |
58 case WM_NCMOUSEMOVE: | |
59 return ui::Event::ET_MOUSE_MOVED; | |
60 case WM_MOUSEWHEEL: | |
61 return ui::Event::ET_MOUSEWHEEL; | |
62 case WM_MOUSELEAVE: | |
63 case WM_NCMOUSELEAVE: | |
64 return ui::Event::ET_MOUSE_EXITED; | |
65 default: | |
66 NOTREACHED(); | |
67 } | |
68 return ui::Event::ET_UNKNOWN; | |
69 } | |
70 | |
71 bool IsClientMouseEvent(NativeEvent native_event) { | |
72 return native_event.message == WM_MOUSELEAVE || | |
73 native_event.message == WM_MOUSEHOVER || | |
74 (native_event.message >= WM_MOUSEFIRST && | |
75 native_event.message <= WM_MOUSELAST); | |
76 } | |
77 | |
78 bool IsNonClientMouseEvent(NativeEvent native_event) { | |
79 return native_event.message == WM_NCMOUSELEAVE || | |
80 native_event.message == WM_NCMOUSEHOVER || | |
81 (native_event.message >= WM_NCMOUSEMOVE && | |
82 native_event.message <= WM_NCXBUTTONDBLCLK); | |
83 } | |
84 | |
85 gfx::Point MousePositionFromNative(NativeEvent native_event) { | |
86 // Client message. The position is contained in the LPARAM. | |
87 if (IsClientMouseEvent(native_event)) | |
88 return gfx::Point(native_event.lParam); | |
89 DCHECK(IsNonClientMouseEvent(native_event)); | |
90 // Non-client message. The position is contained in a POINTS structure in | |
91 // LPARAM, and is in screen coordinates so we have to convert to client. | |
92 POINT native_point = { GET_X_LPARAM(native_event.lParam), | |
93 GET_Y_LPARAM(native_event.lParam) }; | |
94 ScreenToClient(native_event.hwnd, &native_point); | |
95 return gfx::Point(native_point); | |
96 } | |
97 | |
98 int MouseEventFlagsFromNative(NativeEvent native_event) { | |
99 int flags = 0; | |
100 | |
101 // Check if the event occurred in the non-client area. | |
102 if (IsNonClientMouseEvent(native_event)) | |
103 flags |= ui::MouseEvent::EF_IS_NON_CLIENT; | |
104 | |
105 // Check for double click events. | |
106 switch (native_event.message) { | |
107 case WM_NCLBUTTONDBLCLK: | |
108 case WM_NCMBUTTONDBLCLK: | |
109 case WM_NCRBUTTONDBLCLK: | |
110 case WM_LBUTTONDBLCLK: | |
111 case WM_MBUTTONDBLCLK: | |
112 case WM_RBUTTONDBLCLK: | |
113 flags |= ui::MouseEvent::EF_IS_DOUBLE_CLICK; | |
114 break; | |
115 } | |
116 | |
117 // Check for pressed buttons. | |
118 if (IsClientMouseEvent(native_event)) { | |
119 if (native_event.wParam & MK_LBUTTON) | |
120 flags |= ui::MouseEvent::EF_LEFT_BUTTON_DOWN; | |
121 if (native_event.wParam & MK_MBUTTON) | |
122 flags |= ui::MouseEvent::EF_MIDDLE_BUTTON_DOWN; | |
123 if (native_event.wParam & MK_RBUTTON) | |
124 flags |= ui::MouseEvent::EF_RIGHT_BUTTON_DOWN; | |
125 } else if (IsNonClientMouseEvent(native_event)) { | |
126 switch (native_event.message) { | |
127 case WM_NCLBUTTONDOWN: | |
128 flags |= ui::MouseEvent::EF_LEFT_BUTTON_DOWN; | |
129 break; | |
130 case WM_NCMBUTTONDOWN: | |
131 flags |= ui::MouseEvent::EF_MIDDLE_BUTTON_DOWN; | |
132 break; | |
133 case WM_NCRBUTTONDOWN: | |
134 flags |= ui::MouseEvent::EF_RIGHT_BUTTON_DOWN; | |
135 break; | |
136 } | |
137 } | |
138 | |
139 // Finally make sure the key state flags are included. | |
140 return flags | GetKeyStateFlags(); | |
141 } | |
142 | |
143 int MouseWheelEventFlagsFromNative(NativeEvent native_event) { | |
144 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); | |
145 int flags = 0; | |
146 flags |= (win_flags & MK_CONTROL) ? ui::MouseEvent::EF_CONTROL_DOWN : 0; | |
147 flags |= (win_flags & MK_SHIFT) ? ui::MouseEvent::EF_SHIFT_DOWN : 0; | |
148 flags |= (GetKeyState(VK_MENU) < 0) ? ui::MouseEvent::EF_ALT_DOWN : 0; | |
149 flags |= (win_flags & MK_LBUTTON) ? ui::MouseEvent::EF_LEFT_BUTTON_DOWN : 0; | |
150 flags |= (win_flags & MK_MBUTTON) ? ui::MouseEvent::EF_MIDDLE_BUTTON_DOWN : 0; | |
151 flags |= (win_flags & MK_RBUTTON) ? ui::MouseEvent::EF_RIGHT_BUTTON_DOWN : 0; | |
152 return flags; | |
153 } | |
154 | |
155 } // namespace | |
156 | |
157 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
158 // KeyEvent, public: | 14 // KeyEvent, public: |
159 | 15 |
160 KeyEvent::KeyEvent(NativeEvent native_event) | 16 KeyEvent::KeyEvent(NativeEvent native_event) |
161 : Event(EventTypeFromNative(native_event), GetKeyStateFlags()), | 17 : Event(ui::EventTypeFromNative(native_event), |
| 18 ui::EventFlagsFromNative(native_event)), |
162 key_code_(KeyboardCodeForWindowsKeyCode(native_event.wParam)), | 19 key_code_(KeyboardCodeForWindowsKeyCode(native_event.wParam)), |
163 repeat_count_(native_event.lParam & 0xFFFF), | 20 repeat_count_(native_event.lParam & 0xFFFF), |
164 message_flags_((native_event.lParam & 0xFFFF0000) >> 16) { | 21 message_flags_((native_event.lParam & 0xFFFF0000) >> 16) { |
165 } | 22 } |
166 | 23 |
167 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
168 // MouseEvent, public: | 25 // MouseEvent, public: |
169 | 26 |
170 MouseEvent::MouseEvent(NativeEvent native_event) | 27 MouseEvent::MouseEvent(NativeEvent native_event) |
171 : LocatedEvent(EventTypeFromNative(native_event), | 28 : LocatedEvent(ui::EventTypeFromNative(native_event), |
172 MousePositionFromNative(native_event), | 29 ui::EventLocationFromNative(native_event), |
173 MouseEventFlagsFromNative(native_event)) { | 30 ui::EventFlagsFromNative(native_event)) { |
174 } | 31 } |
175 | 32 |
176 //////////////////////////////////////////////////////////////////////////////// | 33 //////////////////////////////////////////////////////////////////////////////// |
177 // MouseWheelEvent, public: | 34 // MouseWheelEvent, public: |
178 | 35 |
179 MouseWheelEvent::MouseWheelEvent(NativeEvent native_event) | 36 MouseWheelEvent::MouseWheelEvent(NativeEvent native_event) |
180 : LocatedEvent(ET_MOUSEWHEEL, | 37 : LocatedEvent(ui::EventTypeFromNative(native_event), |
181 MousePositionFromNative(native_event), | 38 ui::EventLocationFromNative(native_event), |
182 MouseWheelEventFlagsFromNative(native_event)), | 39 ui::EventFlagsFromNative(native_event)), |
183 offset_(GET_WHEEL_DELTA_WPARAM(native_event.wParam)) { | 40 offset_(ui::GetMouseWheelOffset(native_event)) { |
184 } | 41 } |
185 | 42 |
186 } // namespace ui | 43 } // namespace ui |
187 | 44 |
OLD | NEW |