]> git.saurik.com Git - wxWidgets.git/blob - src/osx/uiaction_osx.cpp
better support for ui action simulation
[wxWidgets.git] / src / osx / uiaction_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/uiaction_osx.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 #ifndef WX_PRECOMP
17 #include "wx/object.h"
18 #endif
19
20 #if wxUSE_UIACTIONSIMULATOR
21
22 #include "wx/uiaction.h"
23
24 #include "wx/log.h"
25
26 #include "wx/osx/private.h"
27 #include "wx/osx/core/cfref.h"
28
29 namespace
30 {
31
32 CGEventTapLocation tap = kCGSessionEventTap;
33
34 CGEventType CGEventTypeForMouseButton(int button, bool isDown)
35 {
36 switch ( button )
37 {
38 case wxMOUSE_BTN_LEFT:
39 return isDown ? kCGEventLeftMouseDown : kCGEventLeftMouseUp;
40
41 case wxMOUSE_BTN_RIGHT:
42 return isDown ? kCGEventRightMouseDown : kCGEventRightMouseUp;
43
44 // All the other buttons use the constant OtherMouseDown but we still
45 // want to check for invalid parameters so assert first
46 default:
47 wxFAIL_MSG("Unsupported button passed in.");
48 // fall back to the only known remaining case
49
50 case wxMOUSE_BTN_MIDDLE:
51 return isDown ? kCGEventOtherMouseDown : kCGEventOtherMouseUp;
52 }
53 }
54
55 CGPoint GetMousePosition()
56 {
57 int x, y;
58 wxGetMousePosition(&x, &y);
59
60 CGPoint pos;
61 pos.x = x;
62 pos.y = y;
63
64 return pos;
65 }
66
67 } // anonymous namespace
68
69 bool wxUIActionSimulator::MouseDown(int button)
70 {
71 CGEventType type = CGEventTypeForMouseButton(button, true);
72 wxCFRef<CGEventRef> event(
73 CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button));
74
75 if ( !event )
76 return false;
77
78 CGEventSetType(event, type);
79 CGEventPost(tap, event);
80
81 return true;
82 }
83
84 bool wxUIActionSimulator::MouseMove(long x, long y)
85 {
86 CGPoint pos;
87 pos.x = x;
88 pos.y = y;
89
90 CGEventType type = kCGEventMouseMoved;
91 wxCFRef<CGEventRef> event(
92 CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft));
93
94 if ( !event )
95 return false;
96
97 CGEventSetType(event, type);
98 CGEventPost(tap, event);
99
100 return true;
101 }
102
103 bool wxUIActionSimulator::MouseUp(int button)
104 {
105 CGEventType type = CGEventTypeForMouseButton(button, false);
106 wxCFRef<CGEventRef> event(
107 CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button));
108
109 if ( !event )
110 return false;
111
112 CGEventSetType(event, type);
113 CGEventPost(tap, event);
114
115 return true;
116 }
117
118 bool
119 wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown)
120 {
121 CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode);
122
123 wxCFRef<CGEventRef>
124 event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown));
125 if ( !event )
126 return false;
127
128 CGEventPost(kCGHIDEventTap, event);
129 return true;
130 }
131
132 #endif // wxUSE_UIACTIONSIMULATOR
133