+static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
+ // FindFocus does not return the actual focus window, but the enclosing window
+ wxWindow* focus = wxWindow::DoFindFocus();
+ if ( focus == NULL )
+ focus = (wxTopLevelWindowMac*) data ;
+
+ unsigned char charCode ;
+ wxChar uniChar[2] ;
+ uniChar[0] = 0;
+ uniChar[1] = 0;
+
+ UInt32 keyCode ;
+ UInt32 modifiers ;
+ Point point ;
+ UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
+
+#if wxUSE_UNICODE
+ ByteCount dataSize = 0 ;
+ if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
+ {
+ UniChar buf[2] ;
+ int numChars = dataSize / sizeof( UniChar) + 1;
+
+ UniChar* charBuf = buf ;
+
+ if ( numChars * 2 > 4 )
+ charBuf = new UniChar[ numChars ] ;
+ GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
+ charBuf[ numChars - 1 ] = 0;
+
+#if SIZEOF_WCHAR_T == 2
+ uniChar = charBuf[0] ;
+#else
+ wxMBConvUTF16 converter ;
+ converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
+#endif
+
+ if ( numChars * 2 > 4 )
+ delete[] charBuf ;
+ }
+#endif
+
+ GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
+ GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
+ GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
+ GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
+
+ UInt32 message = (keyCode << 8) + charCode;
+ switch ( GetEventKind( event ) )
+ {
+ case kEventRawKeyRepeat :
+ case kEventRawKeyDown :
+ {
+ WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
+ WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
+ wxTheApp->MacSetCurrentEvent( event , handler ) ;
+ if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
+ focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
+ {
+ result = noErr ;
+ }
+ wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
+ }
+ break ;
+
+ case kEventRawKeyUp :
+ if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
+ focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
+ {
+ result = noErr ;
+ }
+ break ;
+
+ case kEventRawKeyModifiersChanged :
+ {
+ wxKeyEvent event(wxEVT_KEY_DOWN);
+
+ event.m_shiftDown = modifiers & shiftKey;
+ event.m_controlDown = modifiers & controlKey;
+ event.m_altDown = modifiers & optionKey;
+ event.m_metaDown = modifiers & cmdKey;
+ event.m_x = point.h;
+ event.m_y = point.v;
+
+#if wxUSE_UNICODE
+ event.m_uniChar = uniChar[0] ;
+#endif
+
+ event.SetTimestamp(when);
+ event.SetEventObject(focus);
+
+ if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
+ {
+ event.m_keyCode = WXK_CONTROL ;
+ event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->HandleWindowEvent( event ) ;
+ }
+ if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
+ {
+ event.m_keyCode = WXK_SHIFT ;
+ event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->HandleWindowEvent( event ) ;
+ }
+ if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
+ {
+ event.m_keyCode = WXK_ALT ;
+ event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->HandleWindowEvent( event ) ;
+ }
+ if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
+ {
+ event.m_keyCode = WXK_COMMAND ;
+ event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->HandleWindowEvent( event ) ;
+ }
+
+ wxApp::s_lastModifiers = modifiers ;
+ }
+ break ;
+
+ default:
+ break;
+ }
+
+ return result ;
+}
+
+// we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
+// for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
+// mouse down at all
+//
+// This handler can also be called from app level where data (ie target window) may be null or a non wx window
+
+wxWindow* g_MacLastWindow = NULL ;
+
+EventMouseButton g_lastButton = 0 ;
+bool g_lastButtonWasFakeRight = false ;
+
+void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
+{
+ UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
+ Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
+
+ // these parameters are not given for all events
+ EventMouseButton button = 0 ;
+ UInt32 clickCount = 0 ;
+ UInt32 mouseChord = 0;
+
+ cEvent.GetParameter<EventMouseButton>( kEventParamMouseButton, typeMouseButton , &button ) ;
+ cEvent.GetParameter<UInt32>( kEventParamClickCount, typeUInt32 , &clickCount ) ;
+ // the chord is the state of the buttons pressed currently
+ cEvent.GetParameter<UInt32>( kEventParamMouseChord, typeUInt32 , &mouseChord ) ;
+
+ wxevent.m_x = screenMouseLocation.h;
+ wxevent.m_y = screenMouseLocation.v;
+ wxevent.m_shiftDown = modifiers & shiftKey;
+ wxevent.m_controlDown = modifiers & controlKey;
+ wxevent.m_altDown = modifiers & optionKey;
+ wxevent.m_metaDown = modifiers & cmdKey;
+ wxevent.m_clickCount = clickCount;
+ wxevent.SetTimestamp( cEvent.GetTicks() ) ;
+
+ // a control click is interpreted as a right click
+ bool thisButtonIsFakeRight = false ;
+ if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
+ {
+ button = kEventMouseButtonSecondary ;
+ thisButtonIsFakeRight = true ;
+ }
+
+ // otherwise we report double clicks by connecting a left click with a ctrl-left click
+ if ( clickCount > 1 && button != g_lastButton )
+ clickCount = 1 ;
+
+ // we must make sure that our synthetic 'right' button corresponds in
+ // mouse down, moved and mouse up, and does not deliver a right down and left up
+
+ if ( cEvent.GetKind() == kEventMouseDown )
+ {
+ g_lastButton = button ;
+ g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
+ }
+
+ if ( button == 0 )
+ {
+ g_lastButton = 0 ;
+ g_lastButtonWasFakeRight = false ;
+ }
+ else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
+ button = g_lastButton ;
+
+ // Adjust the chord mask to remove the primary button and add the
+ // secondary button. It is possible that the secondary button is
+ // already pressed, e.g. on a mouse connected to a laptop, but this
+ // possibility is ignored here:
+ if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
+ mouseChord = ((mouseChord & ~1U) | 2U);
+
+ if(mouseChord & 1U)
+ wxevent.m_leftDown = true ;
+ if(mouseChord & 2U)
+ wxevent.m_rightDown = true ;
+ if(mouseChord & 4U)
+ wxevent.m_middleDown = true ;
+
+ // translate into wx types
+ switch ( cEvent.GetKind() )
+ {
+ case kEventMouseDown :
+ switch ( button )
+ {
+ case kEventMouseButtonPrimary :
+ wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
+ break ;
+
+ case kEventMouseButtonSecondary :
+ wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
+ break ;
+
+ case kEventMouseButtonTertiary :
+ wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
+ break ;
+
+ default:
+ break ;
+ }
+ break ;
+
+ case kEventMouseUp :
+ switch ( button )
+ {
+ case kEventMouseButtonPrimary :
+ wxevent.SetEventType( wxEVT_LEFT_UP ) ;
+ break ;
+
+ case kEventMouseButtonSecondary :
+ wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
+ break ;
+
+ case kEventMouseButtonTertiary :
+ wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
+ break ;
+
+ default:
+ break ;
+ }
+ break ;
+
+ case kEventMouseWheelMoved :
+ {
+ wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
+
+ EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
+ SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
+
+ wxevent.m_wheelRotation = delta;
+ wxevent.m_wheelDelta = 1;
+ wxevent.m_linesPerAction = 1;
+ if ( axis == kEventMouseWheelAxisX )
+ wxevent.m_wheelAxis = 1;
+ }
+ break ;
+
+ case kEventMouseEntered :
+ case kEventMouseExited :
+ case kEventMouseDragged :
+ case kEventMouseMoved :
+ wxevent.SetEventType( wxEVT_MOTION ) ;
+ break;
+ default :
+ break ;
+ }
+}
+
+#define NEW_CAPTURE_HANDLING 1
+
+pascal OSStatus
+wxMacTopLevelMouseEventHandler(EventHandlerCallRef WXUNUSED(handler),
+ EventRef event,
+ void *data)