]> git.saurik.com Git - wxWidgets.git/blame - src/osx/uiaction_osx.cpp
Don't enter an infinite loop if a spacer with min size of -1 is used.
[wxWidgets.git] / src / osx / uiaction_osx.cpp
CommitLineData
a02a5cfc
KO
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/uiaction_osx.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/defs.h>
13#include <wx/uiaction.h>
14
15#include <ApplicationServices/ApplicationServices.h>
16
17CGEventTapLocation tap = kCGSessionEventTap;
18
19CGEventType CGEventTypeForMouseButton(int button, bool isDown)
20{
21 switch (button)
22 {
23 case wxMOUSE_BTN_LEFT:
24 if (isDown)
25 return kCGEventLeftMouseDown;
26 else
27 return kCGEventLeftMouseUp;
28 case wxMOUSE_BTN_RIGHT:
29 if (isDown)
30 return kCGEventRightMouseDown;
31 else
32 return kCGEventRightMouseUp;
33
34 // Apparently all other buttons use the constant OtherMouseDown
35
36 default:
37 if (isDown)
38 return kCGEventOtherMouseDown;
39 else
40 return kCGEventOtherMouseUp;
41 }
42}
43
44void SendCharCode(CGCharCode keycode, bool isDown)
45{
46 CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, isDown);
47 if (event)
48 {
49 CGEventPost(kCGHIDEventTap, event);
50 }
51 CFRelease(event);
52}
53
54bool wxUIActionSimulator::MouseDown(int button)
55{
56 CGPoint pos;
57 int x, y;
58 wxGetMousePosition(&x, &y);
59 pos.x = x;
60 pos.y = y;
61 CGEventType type = CGEventTypeForMouseButton(button, true);
62 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button);
63 CGEventSetType(event, type);
64
65 if (event)
66 {
67 CGEventPost(tap, event);
68 }
69 CFRelease(event);
70 return true;
71}
72
73bool wxUIActionSimulator::MouseMove(long x, long y)
74{
75 CGPoint pos;
76 pos.x = x;
77 pos.y = y;
78 CGEventType type = kCGEventMouseMoved;
79 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft);
80 CGEventSetType(event, type);
81
82 if (event)
83 {
84 CGEventPost(tap, event);
85 }
86 CFRelease(event);
87
88 return true;
89}
90
91bool wxUIActionSimulator::MouseUp(int button)
92{
93 CGPoint pos;
94 int x, y;
95 wxGetMousePosition(&x, &y);
96 pos.x = x;
97 pos.y = y;
98 CGEventType type = CGEventTypeForMouseButton(button, false);
99 CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button);
100 CGEventSetType(event, type);
101
102 if (event)
103 {
104 CGEventPost(tap, event);
105 }
106 CFRelease(event);
107
108 return true;
109}
110
111bool wxUIActionSimulator::Key(int keycode, bool isDown, bool shiftDown, bool cmdDown, bool altDown)
112{
113 if (shiftDown)
114 SendCharCode((CGCharCode)56, true);
115 if (altDown)
116 SendCharCode((CGCharCode)58, true);
117 if (cmdDown)
118 SendCharCode((CGCharCode)55, true);
119
120 SendCharCode((CGCharCode)keycode, isDown);
121
122 if (shiftDown)
123 SendCharCode((CGCharCode)56, false);
124 if (altDown)
125 SendCharCode((CGCharCode)58, false);
126 if (cmdDown)
127 SendCharCode((CGCharCode)55, false);
128
129 return true;
130}
131
132