Make wxUIActionSimulator mouse move events marginally more precise.
[wxWidgets.git] / src / msw / uiaction.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/uiaction.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin
5 // Modified by:
6 // Created: 2010-03-06
7 // RCS-ID: $Id$
8 // Copyright: (c) Kevin Ollivier
9 // (c) 2010 Steven Lamerton
10 // (c) 2010 Vadim Zeitlin
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "wx/wxprec.h"
15
16 #if wxUSE_UIACTIONSIMULATOR
17
18 #include "wx/uiaction.h"
19 #include "wx/msw/wrapwin.h"
20
21 #include "wx/msw/private/keyboard.h"
22
23 #include "wx/math.h"
24
25 namespace
26 {
27
28 DWORD EventTypeForMouseButton(int button, bool isDown)
29 {
30 switch (button)
31 {
32 case wxMOUSE_BTN_LEFT:
33 return isDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
34
35 case wxMOUSE_BTN_RIGHT:
36 return isDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
37
38 case wxMOUSE_BTN_MIDDLE:
39 return isDown ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
40
41 default:
42 wxFAIL_MSG("Unsupported button passed in.");
43 return (DWORD)-1;
44 }
45 }
46
47 } // anonymous namespace
48
49 bool wxUIActionSimulator::MouseDown(int button)
50 {
51 POINT p;
52 GetCursorPos(&p);
53 mouse_event(EventTypeForMouseButton(button, true), p.x, p.y, 0, 0);
54 return true;
55 }
56
57 bool wxUIActionSimulator::MouseMove(long x, long y)
58 {
59 // Because MOUSEEVENTF_ABSOLUTE takes measurements scaled between 0 & 65535
60 // we need to scale our input too
61 int displayx, displayy;
62 wxDisplaySize(&displayx, &displayy);
63
64 int scaledx = wxRound(((float)x / displayx) * 65535);
65 int scaledy = wxRound(((float)y / displayy) * 65535);
66 mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, scaledx, scaledy, 0, 0);
67
68 return true;
69 }
70
71 bool wxUIActionSimulator::MouseUp(int button)
72 {
73 POINT p;
74 GetCursorPos(&p);
75 mouse_event(EventTypeForMouseButton(button, false), p.x, p.y, 0, 0);
76 return true;
77 }
78
79 bool
80 wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown)
81 {
82 bool isExtended;
83 DWORD vkkeycode = wxMSWKeyboard::WXToVK(keycode, &isExtended);
84
85 DWORD flags = 0;
86 if ( isExtended )
87 flags |= KEYEVENTF_EXTENDEDKEY;
88 if ( !isDown )
89 flags |= KEYEVENTF_KEYUP;
90
91 keybd_event(vkkeycode, 0, flags, 0);
92
93 return true;
94 }
95
96 #endif // wxUSE_UIACTIONSIMULATOR