1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/uiaction_osx.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin
8 // Copyright: (c) Kevin Ollivier
9 // (c) 2010 Steven Lamerton
10 // (c) 2010 Vadim Zeitlin
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
17 #include "wx/object.h"
20 #if wxUSE_UIACTIONSIMULATOR
22 #include "wx/uiaction.h"
26 #include "wx/osx/private.h"
27 #include "wx/osx/core/cfref.h"
29 #include "wx/evtloop.h"
34 CGEventTapLocation tap
= kCGSessionEventTap
;
36 CGEventType
CGEventTypeForMouseButton(int button
, bool isDown
)
40 case wxMOUSE_BTN_LEFT
:
41 return isDown ? kCGEventLeftMouseDown
: kCGEventLeftMouseUp
;
43 case wxMOUSE_BTN_RIGHT
:
44 return isDown ? kCGEventRightMouseDown
: kCGEventRightMouseUp
;
46 // All the other buttons use the constant OtherMouseDown but we still
47 // want to check for invalid parameters so assert first
49 wxFAIL_MSG("Unsupported button passed in.");
50 // fall back to the only known remaining case
52 case wxMOUSE_BTN_MIDDLE
:
53 return isDown ? kCGEventOtherMouseDown
: kCGEventOtherMouseUp
;
57 CGPoint
GetMousePosition()
60 wxGetMousePosition(&x
, &y
);
69 } // anonymous namespace
71 bool wxUIActionSimulator
::MouseDown(int button
)
73 CGEventType type
= CGEventTypeForMouseButton(button
, true);
74 wxCFRef
<CGEventRef
> event(
75 CGEventCreateMouseEvent(NULL
, type
, GetMousePosition(), button
));
80 CGEventSetType(event
, type
);
81 CGEventPost(tap
, event
);
82 wxCFEventLoop
* loop
= dynamic_cast<wxCFEventLoop
*>(wxEventLoop
::GetActive());
84 loop
->SetShouldWaitForEvent(true);
89 bool wxUIActionSimulator
::MouseDblClick(int button
)
91 CGEventType downtype
= CGEventTypeForMouseButton(button
, true);
92 CGEventType uptype
= CGEventTypeForMouseButton(button
, false);
93 wxCFRef
<CGEventRef
> event(
94 CGEventCreateMouseEvent(NULL
, downtype
, GetMousePosition(), button
));
99 CGEventSetType(event
,downtype
);
100 CGEventPost(tap
, event
);
102 CGEventSetType(event
, uptype
);
103 CGEventPost(tap
, event
);
105 CGEventSetIntegerValueField(event
, kCGMouseEventClickState
, 2);
106 CGEventSetType(event
, downtype
);
107 CGEventPost(tap
, event
);
109 CGEventSetType(event
, uptype
);
110 CGEventPost(tap
, event
);
111 wxCFEventLoop
* loop
= dynamic_cast<wxCFEventLoop
*>(wxEventLoop
::GetActive());
113 loop
->SetShouldWaitForEvent(true);
118 bool wxUIActionSimulator
::MouseMove(long x
, long y
)
124 CGEventType type
= kCGEventMouseMoved
;
125 wxCFRef
<CGEventRef
> event(
126 CGEventCreateMouseEvent(NULL
, type
, pos
, kCGMouseButtonLeft
));
131 CGEventSetType(event
, type
);
132 CGEventPost(tap
, event
);
133 wxCFEventLoop
* loop
= dynamic_cast<wxCFEventLoop
*>(wxEventLoop
::GetActive());
135 loop
->SetShouldWaitForEvent(true);
140 bool wxUIActionSimulator
::MouseUp(int button
)
142 CGEventType type
= CGEventTypeForMouseButton(button
, false);
143 wxCFRef
<CGEventRef
> event(
144 CGEventCreateMouseEvent(NULL
, type
, GetMousePosition(), button
));
149 CGEventSetType(event
, type
);
150 CGEventPost(tap
, event
);
151 wxCFEventLoop
* loop
= dynamic_cast<wxCFEventLoop
*>(wxEventLoop
::GetActive());
153 loop
->SetShouldWaitForEvent(true);
159 wxUIActionSimulator
::DoKey(int keycode
, int WXUNUSED(modifiers
), bool isDown
)
161 CGKeyCode cgcode
= wxCharCodeWXToOSX((wxKeyCode
)keycode
);
164 event(CGEventCreateKeyboardEvent(NULL
, cgcode
, isDown
));
168 CGEventPost(kCGHIDEventTap
, event
);
169 wxCFEventLoop
* loop
= dynamic_cast<wxCFEventLoop
*>(wxEventLoop
::GetActive());
171 loop
->SetShouldWaitForEvent(true);
176 #endif // wxUSE_UIACTIONSIMULATOR