]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/uiaction_osx.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / osx / uiaction_osx.cpp
index 603b3039774b9678b0708c76dbc42d206f1c777a..c916923a3fccfc3cd660ddbeabfa240750abdf07 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
 // 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 $
 // Copyright:   (c) Kevin Ollivier
+//              (c) 2010 Steven Lamerton
+//              (c) 2010 Vadim Zeitlin
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#include "wx/defs.h"
+#include "wx/wxprec.h"
+
+#ifndef WX_PRECOMP
+#include "wx/object.h"
+#endif
 
 #if wxUSE_UIACTIONSIMULATOR
 
 #include "wx/uiaction.h"
 
-#include <ApplicationServices/ApplicationServices.h>
+#include "wx/log.h"
+
+#include "wx/osx/private.h"
+#include "wx/osx/core/cfref.h"
 
+#include "wx/evtloop.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;
+            return isDown ? kCGEventRightMouseDown : kCGEventRightMouseUp;
 
-        // Apparently all other buttons use the constant OtherMouseDown
+        // All the other buttons use the constant OtherMouseDown but we still
+        // want to check for invalid parameters so assert first
+        default:
+            wxFAIL_MSG("Unsupported button passed in.");
+            // fall back to the only known remaining case
 
+        case wxMOUSE_BTN_MIDDLE:
+            return isDown ? kCGEventOtherMouseDown : kCGEventOtherMouseUp;
+    }
+}
+    
+CGEventType CGEventTypeForMouseDrag(int button)
+{
+    switch ( button )
+    {
+        case wxMOUSE_BTN_LEFT:
+            return kCGEventLeftMouseDragged;
+            break;
+            
+        case wxMOUSE_BTN_RIGHT:
+            return kCGEventRightMouseDragged;
+            break;
+            
+            // 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
+            
+        case wxMOUSE_BTN_MIDDLE:
+            return kCGEventOtherMouseDragged;
+            break;
     }
+
 }
 
-void SendCharCode(CGCharCode keycode, bool isDown)
+CGMouseButton CGButtonForMouseButton(int button)
 {
-    CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, isDown);
-    if (event)
+    switch ( button )
     {
-        CGEventPost(kCGHIDEventTap, event);
+        case wxMOUSE_BTN_LEFT:
+            return kCGMouseButtonLeft;
+            
+        case wxMOUSE_BTN_RIGHT:
+            return kCGMouseButtonRight;
+            
+            // All the other buttons use the constant OtherMouseDown but we still
+            // want to check for invalid parameters so assert first
+        default:
+            wxFAIL_MSG("Unsupported button passed in.");
+            // fall back to the only known remaining case
+            
+        case wxMOUSE_BTN_MIDDLE:
+            return kCGMouseButtonCenter;
     }
-    CFRelease(event);
 }
-
-bool wxUIActionSimulator::MouseDown(int button)
+    
+CGPoint GetMousePosition()
 {
-    CGPoint pos;
     int 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);
-    CGEventSetType(event, type);
+    wxCFRef<CGEventRef> event(
+            CGEventCreateMouseEvent(NULL, type, GetMousePosition(), CGButtonForMouseButton(button)));
 
-    if (event)
-    {
-        CGEventPost(tap, event);
-    }
-    CFRelease(event);
+    if ( !event )
+        return false;
+
+    CGEventSetType(event, type);
+    CGEventPost(tap, event);
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
+    
     return true;
 }
 
@@ -78,56 +136,124 @@ 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);
-    CGEventSetType(event, type);
+    wxCFRef<CGEventRef> event(
+            CGEventCreateMouseEvent(NULL, type, pos, kCGMouseButtonLeft));
 
-    if (event)
-    {
-        CGEventPost(tap, event);
-    }
-    CFRelease(event);
+    if ( !event )
+        return false;
 
+    CGEventSetType(event, type);
+    CGEventPost(tap, event);
+
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
+    
     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<CGEventRef> event(
+            CGEventCreateMouseEvent(NULL, type, GetMousePosition(), CGButtonForMouseButton(button)));
+
+    if ( !event )
+        return false;
+
     CGEventSetType(event, type);
+    CGEventPost(tap, event);
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
+    
+    return true;
+}
 
-    if (event)
-    {
-        CGEventPost(tap, event);
-    }
-    CFRelease(event);
+bool wxUIActionSimulator::MouseDblClick(int button)
+{
+    CGEventType downtype = CGEventTypeForMouseButton(button, true);
+    CGEventType uptype = CGEventTypeForMouseButton(button, false);
+    wxCFRef<CGEventRef> event(
+                              CGEventCreateMouseEvent(NULL, downtype, GetMousePosition(), CGButtonForMouseButton(button)));
+    
+    if ( !event )
+        return false;
+    
+    CGEventSetType(event,downtype);
+    CGEventPost(tap, event);
+    
+    CGEventSetType(event, uptype);
+    CGEventPost(tap, event);
+    
+    CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);
+    CGEventSetType(event, downtype);
+    CGEventPost(tap, event);
+    
+    CGEventSetType(event, uptype);
+    CGEventPost(tap, event);
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
+    
+    return true;
+}
+
+bool wxUIActionSimulator::MouseDragDrop(long x1, long y1, long x2, long y2,
+                                        int button)
+{
+    CGPoint pos1,pos2;
+    pos1.x = x1;
+    pos1.y = y1;
+    pos2.x = x2;
+    pos2.y = y2;
 
+    CGEventType downtype = CGEventTypeForMouseButton(button, true);
+    CGEventType uptype = CGEventTypeForMouseButton(button, false);
+    CGEventType dragtype = CGEventTypeForMouseDrag(button) ;
+
+    wxCFRef<CGEventRef> event(
+                              CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, pos1, CGButtonForMouseButton(button)));
+    
+    if ( !event )
+        return false;
+    
+    CGEventSetType(event,kCGEventMouseMoved);
+    CGEventPost(tap, event);
+    
+    CGEventSetType(event,downtype);
+    CGEventPost(tap, event);
+    
+    
+    CGEventSetType(event, dragtype);
+    CGEventSetLocation(event,pos2);
+    CGEventPost(tap, event);
+    
+    CGEventSetType(event, uptype);
+    CGEventPost(tap, event);
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
+    
     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<CGEventRef>
+        event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown));
+    if ( !event )
+        return false;
+
+    CGEventPost(kCGHIDEventTap, event);
+    wxCFEventLoop* loop = dynamic_cast<wxCFEventLoop*>(wxEventLoop::GetActive());
+    if (loop)
+        loop->SetShouldWaitForEvent(true);
 
     return true;
 }