Adding wxUIActionSimulator, a class for programmatically controlling the mouse and...
[wxWidgets.git] / src / msw / uiaction.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/uiaction.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier
5 // Modified by:
6 // Created: 2010-03-06
7 // RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/defs.h"
16 #endif
17
18 #include "wx/uiaction.h"
19
20 #include <windows.h>
21
22 DWORD EventTypeForMouseButton(int button, bool isDown)
23 {
24 switch (button)
25 {
26 case wxMOUSE_BTN_LEFT:
27 if (isDown)
28 return MOUSEEVENTF_LEFTDOWN;
29 else
30 return MOUSEEVENTF_LEFTUP;
31 case wxMOUSE_BTN_RIGHT:
32 if (isDown)
33 return MOUSEEVENTF_RIGHTDOWN;
34 else
35 return MOUSEEVENTF_RIGHTUP;
36 case wxMOUSE_BTN_MIDDLE:
37 if (isDown)
38 return MOUSEEVENTF_MIDDLEDOWN;
39 else
40 return MOUSEEVENTF_MIDDLEUP;
41
42 default:
43 wxFAIL_MSG("Unsupported button passed in.");
44 return -1;
45 }
46 }
47
48 bool wxUIActionSimulator::MouseDown(int button)
49 {
50 POINT p;
51 GetCursorPos(&p);
52 mouse_event(EventTypeForMouseButton(button, true), p.x, p.y, 0, 0);
53 return true;
54 }
55
56 bool wxUIActionSimulator::MouseMove(long x, long y)
57 {
58 mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
59 return true;
60 }
61
62 bool wxUIActionSimulator::MouseUp(int button)
63 {
64 POINT p;
65 GetCursorPos(&p);
66 mouse_event(EventTypeForMouseButton(button, false), p.x, p.y, 0, 0);
67 return true;
68 }
69
70 bool wxUIActionSimulator::Key(int keycode, bool isDown, bool shiftDown, bool cmdDown, bool altDown)
71 {
72 DWORD flags = 0;
73 if (!isDown)
74 flags = KEYEVENTF_KEYUP;
75 keybd_event(keycode, 0, flags, 0);
76 return true;
77 }
78