Fix compilation under MinGW, also add missing SVN properties.
[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/window.h" //for wxCharCodeWXToMSW
20 #include "wx/msw/wrapwin.h"
21
22 namespace
23 {
24
25 DWORD EventTypeForMouseButton(int button, bool isDown)
26 {
27 switch (button)
28 {
29 case wxMOUSE_BTN_LEFT:
30 return isDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
31
32 case wxMOUSE_BTN_RIGHT:
33 return isDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
34
35 case wxMOUSE_BTN_MIDDLE:
36 return isDown ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
37
38 default:
39 wxFAIL_MSG("Unsupported button passed in.");
40 return (DWORD)-1;
41 }
42 }
43
44 void DoSimulateKbdEvent(DWORD vk, bool isDown)
45 {
46 keybd_event(vk, 0, isDown ? 0 : KEYEVENTF_KEYUP, 0);
47 }
48
49 } // anonymous namespace
50
51 bool wxUIActionSimulator::MouseDown(int button)
52 {
53 POINT p;
54 GetCursorPos(&p);
55 mouse_event(EventTypeForMouseButton(button, true), p.x, p.y, 0, 0);
56 return true;
57 }
58
59 bool wxUIActionSimulator::MouseMove(long x, long y)
60 {
61 // Because MOUSEEVENTF_ABSOLUTE takes measurements scaled between 0 & 65535
62 // we need to scale our input too
63 int displayx, displayy, scaledx, scaledy;
64 wxDisplaySize(&displayx, &displayy);
65 scaledx = ((float)x / displayx) * 65535;
66 scaledy = ((float)y / displayy) * 65535;
67 mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, scaledx, scaledy, 0, 0);
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 wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown)
80 {
81 if (isDown)
82 {
83 if (modifiers & wxMOD_SHIFT)
84 DoSimulateKbdEvent(VK_SHIFT, true);
85 if (modifiers & wxMOD_ALT)
86 DoSimulateKbdEvent(VK_MENU, true);
87 if (modifiers & wxMOD_CMD)
88 DoSimulateKbdEvent(VK_CONTROL, true);
89 }
90
91 DWORD vkkeycode = wxCharCodeWXToMSW(keycode);
92 keybd_event(vkkeycode, 0, isDown ? 0 : KEYEVENTF_KEYUP, 0);
93
94 if (!isDown)
95 {
96 if (modifiers & wxMOD_SHIFT)
97 DoSimulateKbdEvent(VK_SHIFT, false);
98 if (modifiers & wxMOD_ALT)
99 DoSimulateKbdEvent(VK_MENU, false);
100 if (modifiers & wxMOD_CMD)
101 DoSimulateKbdEvent(VK_CONTROL, false);
102 }
103
104 return true;
105 }
106
107 #endif // wxUSE_UIACTIONSIMULATOR