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