X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a02a5cfcf33bb2c0edae246c1b19e286bf86d422..60372b0d54379701d62edf5595a7dc25c48330e1:/src/osx/uiaction_osx.cpp?ds=sidebyside diff --git a/src/osx/uiaction_osx.cpp b/src/osx/uiaction_osx.cpp index bd4afa12c1..60cd4da9d8 100644 --- a/src/osx/uiaction_osx.cpp +++ b/src/osx/uiaction_osx.cpp @@ -1,72 +1,79 @@ ///////////////////////////////////////////////////////////////////////////// // Name: src/osx/uiaction_osx.cpp // Purpose: wxUIActionSimulator implementation -// Author: Kevin Ollivier +// Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin // Modified by: // Created: 2010-03-06 -// RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $ +// RCS-ID: $Id$ // Copyright: (c) Kevin Ollivier +// (c) 2010 Steven Lamerton +// (c) 2010 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#include -#include +#include "wx/defs.h" -#include +#if wxUSE_UIACTIONSIMULATOR +#include "wx/uiaction.h" + +#include "wx/log.h" + +#include "wx/osx/private.h" +#include "wx/osx/core/cfref.h" + +namespace +{ + CGEventTapLocation tap = kCGSessionEventTap; CGEventType CGEventTypeForMouseButton(int button, bool isDown) { - switch (button) + switch ( button ) { case wxMOUSE_BTN_LEFT: - if (isDown) - return kCGEventLeftMouseDown; - else - return kCGEventLeftMouseUp; + return isDown ? kCGEventLeftMouseDown : kCGEventLeftMouseUp; + case wxMOUSE_BTN_RIGHT: - if (isDown) - return kCGEventRightMouseDown; - else - return kCGEventRightMouseUp; - - // Apparently all other buttons use the constant OtherMouseDown - + return isDown ? kCGEventRightMouseDown : kCGEventRightMouseUp; + + // All the other buttons use the constant OtherMouseDown but we still + // want to check for invalid parameters so assert first default: - if (isDown) - return kCGEventOtherMouseDown; - else - return kCGEventOtherMouseUp; - } -} + wxFAIL_MSG("Unsupported button passed in."); + // fall back to the only known remaining case -void SendCharCode(CGCharCode keycode, bool isDown) -{ - CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, isDown); - if (event) - { - CGEventPost(kCGHIDEventTap, event); + case wxMOUSE_BTN_MIDDLE: + return isDown ? kCGEventOtherMouseDown : kCGEventOtherMouseUp; } - CFRelease(event); } -bool wxUIActionSimulator::MouseDown(int button) +CGPoint GetMousePosition() { - CGPoint pos; int x, y; - wxGetMousePosition(&x, &y); + wxGetMousePosition(&x, &y); + + CGPoint pos; pos.x = x; pos.y = y; + + return pos; +} + +} // anonymous namespace + +bool wxUIActionSimulator::MouseDown(int button) +{ CGEventType type = CGEventTypeForMouseButton(button, true); - CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button); + wxCFRef event( + CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button)); + + if ( !event ) + return false; + CGEventSetType(event, type); - - if (event) - { - CGEventPost(tap, event); - } - CFRelease(event); + CGEventPost(tap, event); + return true; } @@ -75,58 +82,48 @@ bool wxUIActionSimulator::MouseMove(long x, long y) CGPoint pos; pos.x = x; pos.y = y; + CGEventType type = kCGEventMouseMoved; - CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft); + wxCFRef event( + CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft)); + + if ( !event ) + return false; + CGEventSetType(event, type); - - if (event) - { - CGEventPost(tap, event); - } - CFRelease(event); - + CGEventPost(tap, event); + return true; } bool wxUIActionSimulator::MouseUp(int button) { - CGPoint pos; - int x, y; - wxGetMousePosition(&x, &y); - pos.x = x; - pos.y = y; CGEventType type = CGEventTypeForMouseButton(button, false); - CGEventRef event = CGEventCreateMouseEvent(NULL, type, pos, button); + wxCFRef event( + CGEventCreateMouseEvent(NULL, type, GetMousePosition(), button)); + + if ( !event ) + return false; + CGEventSetType(event, type); - - if (event) - { - CGEventPost(tap, event); - } - CFRelease(event); + CGEventPost(tap, event); return true; } -bool wxUIActionSimulator::Key(int keycode, bool isDown, bool shiftDown, bool cmdDown, bool altDown) +bool +wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown) { - if (shiftDown) - SendCharCode((CGCharCode)56, true); - if (altDown) - SendCharCode((CGCharCode)58, true); - if (cmdDown) - SendCharCode((CGCharCode)55, true); - - SendCharCode((CGCharCode)keycode, isDown); - - if (shiftDown) - SendCharCode((CGCharCode)56, false); - if (altDown) - SendCharCode((CGCharCode)58, false); - if (cmdDown) - SendCharCode((CGCharCode)55, false); - + CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode); + + wxCFRef + event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown)); + if ( !event ) + return false; + + CGEventPost(kCGHIDEventTap, event); return true; } +#endif // wxUSE_UIACTIONSIMULATOR