]> git.saurik.com Git - wxWidgets.git/blame - src/osx/uiaction_osx.cpp
Refactor wxFileName existence checking code.
[wxWidgets.git] / src / osx / uiaction_osx.cpp
CommitLineData
a02a5cfc
KO
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/uiaction_osx.cpp
3// Purpose: wxUIActionSimulator implementation
571d991b 4// Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin
a02a5cfc
KO
5// Modified by:
6// Created: 2010-03-06
b5b208a1 7// RCS-ID: $Id$
a02a5cfc 8// Copyright: (c) Kevin Ollivier
571d991b
VZ
9// (c) 2010 Steven Lamerton
10// (c) 2010 Vadim Zeitlin
a02a5cfc
KO
11// Licence: wxWindows licence
12/////////////////////////////////////////////////////////////////////////////
13
d6eb3ff8
SC
14#include "wx/wxprec.h"
15
16#ifndef WX_PRECOMP
17#include "wx/object.h"
18#endif
9b7e0226
VZ
19
20#if wxUSE_UIACTIONSIMULATOR
21
22#include "wx/uiaction.h"
a02a5cfc 23
571d991b
VZ
24#include "wx/log.h"
25
26#include "wx/osx/private.h"
27#include "wx/osx/core/cfref.h"
28
29namespace
30{
eeb7bdd0 31
a02a5cfc
KO
32CGEventTapLocation tap = kCGSessionEventTap;
33
34CGEventType CGEventTypeForMouseButton(int button, bool isDown)
35{
571d991b 36 switch ( button )
a02a5cfc
KO
37 {
38 case wxMOUSE_BTN_LEFT:
571d991b 39 return isDown ? kCGEventLeftMouseDown : kCGEventLeftMouseUp;
9b7e0226 40
571d991b
VZ
41 case wxMOUSE_BTN_RIGHT:
42 return isDown ? kCGEventRightMouseDown : kCGEventRightMouseUp;
9b7e0226 43
571d991b
VZ
44 // All the other buttons use the constant OtherMouseDown but we still
45 // want to check for invalid parameters so assert first
a02a5cfc 46 default:
571d991b
VZ
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;
a02a5cfc
KO
52 }
53}
54
571d991b 55CGPoint GetMousePosition()
a02a5cfc 56{
571d991b
VZ
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
571d991b
VZ
67} // anonymous namespace
68
a02a5cfc
KO
69bool wxUIActionSimulator::MouseDown(int button)
70{
a02a5cfc 71 CGEventType type = CGEventTypeForMouseButton(button, true);
571d991b
VZ
72 wxCFRef<CGEventRef> event(
73 CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button));
74
75 if ( !event )
76 return false;
77
a02a5cfc 78 CGEventSetType(event, type);
571d991b 79 CGEventPost(tap, event);
9b7e0226 80
a02a5cfc
KO
81 return true;
82}
83
84bool wxUIActionSimulator::MouseMove(long x, long y)
85{
86 CGPoint pos;
87 pos.x = x;
88 pos.y = y;
571d991b 89
a02a5cfc 90 CGEventType type = kCGEventMouseMoved;
571d991b
VZ
91 wxCFRef<CGEventRef> event(
92 CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft));
9b7e0226 93
571d991b
VZ
94 if ( !event )
95 return false;
96
97 CGEventSetType(event, type);
98 CGEventPost(tap, event);
9b7e0226 99
a02a5cfc
KO
100 return true;
101}
102
103bool wxUIActionSimulator::MouseUp(int button)
104{
a02a5cfc 105 CGEventType type = CGEventTypeForMouseButton(button, false);
571d991b
VZ
106 wxCFRef<CGEventRef> event(
107 CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button));
9b7e0226 108
571d991b
VZ
109 if ( !event )
110 return false;
111
112 CGEventSetType(event, type);
113 CGEventPost(tap, event);
a02a5cfc
KO
114
115 return true;
116}
117
03bec791
VZ
118bool
119wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown)
a02a5cfc 120{
571d991b 121 CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode);
571d991b 122
37141214
VZ
123 wxCFRef<CGEventRef>
124 event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown));
125 if ( !event )
126 return false;
9b7e0226 127
37141214 128 CGEventPost(kCGHIDEventTap, event);
a02a5cfc
KO
129 return true;
130}
131
9b7e0226 132#endif // wxUSE_UIACTIONSIMULATOR
a02a5cfc 133