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 // Copyright:   (c) Kevin Ollivier
8 //              (c) 2010 Steven Lamerton
9 //              (c) 2010 Vadim Zeitlin
10 // Licence:     wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12 
13 #include "wx/wxprec.h"
14 
15 #if wxUSE_UIACTIONSIMULATOR
16 
17 #ifndef WX_PRECOMP
18     #include "wx/msw/private.h"             // For wxGetCursorPosMSW()
19 #endif
20 
21 #include "wx/uiaction.h"
22 #include "wx/msw/wrapwin.h"
23 
24 #include "wx/msw/private/keyboard.h"
25 
26 #include "wx/math.h"
27 
28 namespace
29 {
30 
EventTypeForMouseButton(int button,bool isDown)31 DWORD EventTypeForMouseButton(int button, bool isDown)
32 {
33     switch (button)
34     {
35         case wxMOUSE_BTN_LEFT:
36             return isDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
37 
38         case wxMOUSE_BTN_RIGHT:
39             return isDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
40 
41         case wxMOUSE_BTN_MIDDLE:
42             return isDown ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
43 
44         default:
45             wxFAIL_MSG("Unsupported button passed in.");
46             return (DWORD)-1;
47     }
48 }
49 
50 } // anonymous namespace
51 
MouseDown(int button)52 bool wxUIActionSimulator::MouseDown(int button)
53 {
54     POINT p;
55     wxGetCursorPosMSW(&p);
56     mouse_event(EventTypeForMouseButton(button, true), p.x, p.y, 0, 0);
57     return true;
58 }
59 
MouseMove(long x,long y)60 bool wxUIActionSimulator::MouseMove(long x, long y)
61 {
62     // Because MOUSEEVENTF_ABSOLUTE takes measurements scaled between 0 & 65535
63     // we need to scale our input too
64     int displayx, displayy;
65     wxDisplaySize(&displayx, &displayy);
66 
67     // Casts are safe because x and y are supposed to be less than the display
68     // size, so there is no danger of overflow.
69     DWORD scaledx = static_cast<DWORD>(ceil(x * 65535.0 / (displayx-1)));
70     DWORD scaledy = static_cast<DWORD>(ceil(y * 65535.0 / (displayy-1)));
71     mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, scaledx, scaledy, 0, 0);
72 
73     return true;
74 }
75 
MouseUp(int button)76 bool wxUIActionSimulator::MouseUp(int button)
77 {
78     POINT p;
79     wxGetCursorPosMSW(&p);
80     mouse_event(EventTypeForMouseButton(button, false), p.x, p.y, 0, 0);
81     return true;
82 }
83 
84 bool
DoKey(int keycode,int WXUNUSED (modifiers),bool isDown)85 wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown)
86 {
87     bool isExtended;
88     DWORD vkkeycode = wxMSWKeyboard::WXToVK(keycode, &isExtended);
89 
90     DWORD flags = 0;
91     if ( isExtended )
92         flags |= KEYEVENTF_EXTENDEDKEY;
93     if ( !isDown )
94         flags |= KEYEVENTF_KEYUP;
95 
96     keybd_event(vkkeycode, 0, flags, 0);
97 
98     return true;
99 }
100 
101 #endif // wxUSE_UIACTIONSIMULATOR
102