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

Side by Side Diff: ui/base/win/event_util.cc

Issue 7942004: Consolidate/cleanup event cracking code; single out GdkEvents. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows event code consolidation. Created 9 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/base/win/event_util.h"
6
7 #include <windowsx.h>
8
9 #include "base/logging.h"
10 #include "ui/gfx/point.h"
11
12 namespace {
13
14 // Get the native mouse key state from the native event message type.
15 int GetNativeMouseKey(const MSG& native_event) {
16 switch (native_event.message) {
17 case WM_LBUTTONDBLCLK:
18 case WM_LBUTTONDOWN:
19 case WM_LBUTTONUP:
20 case WM_NCLBUTTONDBLCLK:
21 case WM_NCLBUTTONDOWN:
22 case WM_NCLBUTTONUP:
23 return MK_LBUTTON;
24 case WM_MBUTTONDBLCLK:
25 case WM_MBUTTONDOWN:
26 case WM_MBUTTONUP:
27 case WM_NCMBUTTONDBLCLK:
28 case WM_NCMBUTTONDOWN:
29 case WM_NCMBUTTONUP:
30 return MK_MBUTTON;
31 case WM_RBUTTONDBLCLK:
32 case WM_RBUTTONDOWN:
33 case WM_RBUTTONUP:
34 case WM_NCRBUTTONDBLCLK:
35 case WM_NCRBUTTONDOWN:
36 case WM_NCRBUTTONUP:
37 return MK_RBUTTON;
38 case WM_NCXBUTTONDBLCLK:
39 case WM_NCXBUTTONDOWN:
40 case WM_NCXBUTTONUP:
41 case WM_XBUTTONDBLCLK:
42 case WM_XBUTTONDOWN:
43 case WM_XBUTTONUP:
44 return MK_XBUTTON1;
45 }
46 return 0;
47 }
48
49 bool IsButtonDown(const MSG& native_event) {
50 return ((MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | MK_XBUTTON1 | MK_XBUTTON2) &
51 native_event.wParam) != 0;
52 }
53
54 bool IsClientMouseEvent(const MSG& native_event) {
55 return native_event.message == WM_MOUSELEAVE ||
56 native_event.message == WM_MOUSEHOVER ||
57 (native_event.message >= WM_MOUSEFIRST &&
58 native_event.message <= WM_MOUSELAST);
59 }
60
61 bool IsNonClientMouseEvent(const MSG& native_event) {
62 return native_event.message == WM_NCMOUSELEAVE ||
63 native_event.message == WM_NCMOUSEHOVER ||
64 (native_event.message >= WM_NCMOUSEMOVE &&
65 native_event.message <= WM_NCXBUTTONDBLCLK);
66 }
67
68 bool IsDoubleClickMouseEvent(const MSG& native_event) {
69 return native_event.message == WM_NCLBUTTONDBLCLK ||
70 native_event.message == WM_NCMBUTTONDBLCLK ||
71 native_event.message == WM_NCRBUTTONDBLCLK ||
72 native_event.message == WM_NCXBUTTONDBLCLK ||
73 native_event.message == WM_LBUTTONDBLCLK ||
74 native_event.message == WM_MBUTTONDBLCLK ||
75 native_event.message == WM_RBUTTONDBLCLK ||
76 native_event.message == WM_XBUTTONDBLCLK;
77 }
78
79 // Returns a mask corresponding to the set of pressed modifier keys.
80 // Checks the current global state and the state sent by client mouse messages.
81 int KeyStateFlagsFromNative(const MSG& native_event) {
82 int flags = 0;
83 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0;
84 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_DOWN : 0;
85 flags |= (GetKeyState(VK_CONTROL) & 0x80) ? ui::EF_CONTROL_DOWN : 0;
86
87 // Most client mouse messages include key state information.
88 if (IsClientMouseEvent(native_event)) {
89 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam);
90 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0;
91 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0;
92 }
93
94 return flags;
95 }
96
97 // Returns a mask corresponding to the set of pressed mouse buttons.
98 // This includes the button of the given message, even if it is being released.
99 int MouseStateFlagsFromNative(const MSG& native_event) {
100 // TODO(msw): ORing the pressed/released button into the flags is _wrong_.
101 // It makes it impossible to tell which button was modified when multiple
102 // buttons are/were held down. Instead, we need to track the modified button
103 // independently and audit event consumers to do the right thing.
104 // TODO(msw): This event's MK_*BUTTON is no longer mixed into |native_event|.
105 int win_flags = GetNativeMouseKey(native_event);
106
107 // Client mouse messages provide key states in their WPARAMs.
108 if (IsClientMouseEvent(native_event))
109 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam);
110
111 int flags = 0;
112 flags |= (win_flags & MK_LBUTTON) ? ui::EF_LEFT_BUTTON_DOWN : 0;
113 flags |= (win_flags & MK_MBUTTON) ? ui::EF_MIDDLE_BUTTON_DOWN : 0;
114 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0;
115 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0;
116 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0;
117 return flags;
118 }
119
120 } // namespace
121
122 namespace ui {
123
124 EventType EventTypeFromNative(const MSG& native_event) {
125 switch (native_event.message) {
126 case WM_KEYDOWN:
127 case WM_SYSKEYDOWN:
128 case WM_CHAR:
129 return ET_KEY_PRESSED;
130 case WM_KEYUP:
131 case WM_SYSKEYUP:
132 return ET_KEY_RELEASED;
133 case WM_LBUTTONDBLCLK:
134 case WM_LBUTTONDOWN:
135 case WM_MBUTTONDBLCLK:
136 case WM_MBUTTONDOWN:
137 case WM_NCLBUTTONDBLCLK:
138 case WM_NCLBUTTONDOWN:
139 case WM_NCMBUTTONDBLCLK:
140 case WM_NCMBUTTONDOWN:
141 case WM_NCRBUTTONDBLCLK:
142 case WM_NCRBUTTONDOWN:
143 case WM_NCXBUTTONDBLCLK:
144 case WM_NCXBUTTONDOWN:
145 case WM_RBUTTONDBLCLK:
146 case WM_RBUTTONDOWN:
147 case WM_XBUTTONDBLCLK:
148 case WM_XBUTTONDOWN:
149 return ET_MOUSE_PRESSED;
150 case WM_LBUTTONUP:
151 case WM_MBUTTONUP:
152 case WM_NCLBUTTONUP:
153 case WM_NCMBUTTONUP:
154 case WM_NCRBUTTONUP:
155 case WM_NCXBUTTONUP:
156 case WM_RBUTTONUP:
157 case WM_XBUTTONUP:
158 return ET_MOUSE_RELEASED;
159 case WM_MOUSEMOVE:
160 return IsButtonDown(native_event) ? ET_MOUSE_DRAGGED : ET_MOUSE_MOVED;
161 case WM_NCMOUSEMOVE:
162 return ET_MOUSE_MOVED;
163 case WM_MOUSEWHEEL:
164 return ET_MOUSEWHEEL;
165 case WM_MOUSELEAVE:
166 case WM_NCMOUSELEAVE:
167 return ET_MOUSE_EXITED;
168 default:
169 NOTREACHED();
170 }
171 return ET_UNKNOWN;
172 }
173
174 int EventFlagsFromNative(const MSG& native_event) {
175 int flags = KeyStateFlagsFromNative(native_event);
176 if (IsMouseEvent(native_event))
177 flags |= MouseStateFlagsFromNative(native_event);
178
179 return flags;
180 }
181
182 gfx::Point EventLocationFromNative(const MSG& native_event) {
183 // Client message. The position is contained in the LPARAM.
184 if (IsClientMouseEvent(native_event))
185 return gfx::Point(native_event.lParam);
186 DCHECK(IsNonClientMouseEvent(native_event));
187 // Non-client message. The position is contained in a POINTS structure in
188 // LPARAM, and is in screen coordinates so we have to convert to client.
189 POINT native_point = { GET_X_LPARAM(native_event.lParam),
190 GET_Y_LPARAM(native_event.lParam) };
191 ScreenToClient(native_event.hwnd, &native_point);
192 return gfx::Point(native_point);
193 }
194
195 bool IsExtendedKey(const MSG& native_event) {
196 return (HIWORD(native_event.lParam) & KF_EXTENDED) == KF_EXTENDED;
197 }
198
199 bool IsMouseEvent(const MSG& native_event) {
200 return IsClientMouseEvent(native_event) ||
201 IsNonClientMouseEvent(native_event);
202 }
203
204 int GetMouseWheelOffset(const MSG& native_event) {
205 DCHECK(native_event.message == WM_MOUSEWHEEL);
206 return GET_WHEEL_DELTA_WPARAM(native_event.wParam);
207 }
208
209 } // namespace ui
OLDNEW
« ui/base/win/event_util.h ('K') | « ui/base/win/event_util.h ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698