+ wxWindow* focus = wxWindow::FindFocus() ;
+ char charCode ;
+ UInt32 keyCode ;
+ UInt32 modifiers ;
+ Point point ;
+
+ EventRef rawEvent ;
+
+ GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
+
+ GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
+ GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
+ GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
+ GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL,
+ sizeof( Point ), NULL, &point );
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventTextInputUnicodeForKeyEvent :
+ // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
+ // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
+ wxControl* control = wxDynamicCast( focus , wxControl ) ;
+ if ( control )
+ {
+ ControlRef macControl = (ControlRef) control->GetHandle() ;
+ if ( macControl )
+ {
+ ::HandleControlKey( macControl , keyCode , charCode , modifiers ) ;
+ result = noErr ;
+ }
+ }
+ /*
+ // this may lead to double events sent to a window in case all handlers have skipped the key down event
+ UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
+ UInt32 message = (keyCode << 8) + charCode;
+
+ if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
+ focus , message , modifiers , when , point.h , point.v ) )
+ {
+ result = noErr ;
+ }
+ */
+ break ;
+ }
+
+ return result ;
+}
+
+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 )
+ return result ;
+
+ char charCode ;
+ wxChar uniChar = 0 ;
+ UInt32 keyCode ;
+ UInt32 modifiers ;
+ Point point ;
+ UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
+
+#if wxUSE_UNICODE
+ UInt32 dataSize = 0 ;
+ if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize , NULL ) == noErr )
+ {
+ UniChar buf[2] ;
+
+ UniChar* charBuf = buf ;
+
+ if ( dataSize > 4 )
+ charBuf = new UniChar[ dataSize / sizeof( UniChar) ] ;
+ GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
+#if SIZEOF_WCHAR_T == 2
+ uniChar = charBuf[0] ;
+#else
+ wxMBConvUTF16BE converter ;
+ converter.MB2WC( &uniChar , (const char*)charBuf , 1 ) ;
+#endif
+ if ( dataSize > 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 != NULL) && wxTheApp->MacSendKeyDownEvent(
+ focus , message , modifiers , when , point.h , point.v , uniChar ) )
+ {
+ result = noErr ;
+ }
+ wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
+ }
+ break ;
+ case kEventRawKeyUp :
+ if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
+ focus , message , modifiers , when , point.h , point.v , uniChar ) )
+ {
+ 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;
+#if wxUSE_UNICODE
+ event.m_uniChar = uniChar ;
+#endif
+ event.m_x = point.h;
+ event.m_y = point.v;
+ 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->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ if ( focus && (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
+ {
+ event.m_keyCode = WXK_SHIFT ;
+ event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ if ( focus && (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
+ {
+ event.m_keyCode = WXK_ALT ;
+ event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ if ( focus && (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
+ {
+ event.m_keyCode = WXK_COMMAND ;
+ event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ wxApp::s_lastModifiers = modifiers ;
+ }
+ 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 ;
+
+static EventMouseButton lastButton = 0 ;
+
+static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
+{
+ UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
+ Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
+
+ // this parameter are not given for all events
+ EventMouseButton button = 0 ;
+ UInt32 clickCount = 0 ;
+ cEvent.GetParameter<EventMouseButton>(kEventParamMouseButton, typeMouseButton , &button) ;
+ cEvent.GetParameter<UInt32>(kEventParamClickCount, typeUInt32 , &clickCount ) ;
+
+ 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.SetTimestamp( cEvent.GetTicks() ) ;
+ // a control click is interpreted as a right click
+ if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
+ {
+ button = kEventMouseButtonSecondary ;
+ }
+
+ // otherwise we report double clicks by connecting a left click with a ctrl-left click
+ if ( clickCount > 1 && button != 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 )
+ lastButton = button ;
+
+ if ( button == 0 )
+ lastButton = 0 ;
+ else if ( lastButton )
+ button = lastButton ;
+
+ // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
+ // this button
+ if ( button != 0 && cEvent.GetKind() != kEventMouseUp )