+// ---------------------------------------------------------------------------
+// Carbon Events
+// ---------------------------------------------------------------------------
+
+#if TARGET_CARBON
+
+extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
+
+static const EventTypeSpec eventList[] =
+{
+ { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
+
+ { kEventClassKeyboard, kEventRawKeyDown } ,
+ { kEventClassKeyboard, kEventRawKeyRepeat } ,
+ { kEventClassKeyboard, kEventRawKeyUp } ,
+ { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
+
+ { kEventClassWindow , kEventWindowShown } ,
+ { kEventClassWindow , kEventWindowUpdate } ,
+ { kEventClassWindow , kEventWindowActivated } ,
+ { kEventClassWindow , kEventWindowDeactivated } ,
+ { kEventClassWindow , kEventWindowBoundsChanging } ,
+ { kEventClassWindow , kEventWindowBoundsChanged } ,
+ { kEventClassWindow , kEventWindowClose } ,
+
+ { kEventClassMouse , kEventMouseDown } ,
+ { kEventClassMouse , kEventMouseUp } ,
+ { kEventClassMouse , kEventMouseWheelMoved } ,
+ { kEventClassMouse , kEventMouseMoved } ,
+ { kEventClassMouse , kEventMouseDragged } ,
+
+} ;
+
+static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ 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 )
+ {
+ ControlHandle macControl = (ControlHandle) control->GetMacControl() ;
+ 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 ;
+
+ wxWindow* focus = wxWindow::FindFocus() ;
+ char charCode ;
+ UInt32 keyCode ;
+ UInt32 modifiers ;
+ Point point ;
+ UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
+
+ 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 ) )
+ {
+ result = noErr ;
+ }
+ wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
+ }
+ break ;
+ case kEventRawKeyUp :
+ if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
+ focus , message , modifiers , when , point.h , point.v ) )
+ {
+ 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;
+ event.m_timeStamp = when;
+ wxWindow* focus = wxWindow::FindFocus() ;
+ event.SetEventObject(focus);
+
+ if ( focus && (modifiers ^ wxTheApp->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 ^ wxTheApp->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 ^ wxTheApp->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 ^ wxTheApp->s_lastModifiers ) & cmdKey )
+ {
+ event.m_keyCode = WXK_COMMAND ;
+ event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
+ focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ wxTheApp->s_lastModifiers = modifiers ;
+ }
+ break ;
+ }
+
+ return result ;
+}
+
+pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
+ Point point ;
+ UInt32 modifiers = 0;
+ EventMouseButton button = 0 ;
+ UInt32 click = 0 ;
+
+ GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
+ sizeof( Point ), NULL, &point );
+ GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
+ sizeof( UInt32 ), NULL, &modifiers );
+ GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
+ sizeof( EventMouseButton ), NULL, &button );
+ GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
+ sizeof( UInt32 ), NULL, &click );
+
+ if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
+ modifiers += btnState ;
+
+ // temporary hack to support true two button mouse
+ if ( button == kEventMouseButtonSecondary )
+ {
+ modifiers |= controlKey ;
+ }
+ WindowRef window ;
+ short windowPart = ::FindWindow(point, &window);
+
+ // either we really are active or we are capturing mouse events
+
+ if ( (IsWindowActive(window) && windowPart == inContent) ||
+ (wxTheApp->s_captureWindow && wxTheApp->s_captureWindow->MacGetTopLevelWindow() == toplevelWindow) )
+ {
+ switch ( GetEventKind( event ) )
+ {
+ case kEventMouseDown :
+ toplevelWindow->MacFireMouseEvent( mouseDown , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
+ result = noErr ;
+ break ;
+ case kEventMouseUp :
+ toplevelWindow->MacFireMouseEvent( mouseUp , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
+ result = noErr ;
+ break ;
+ case kEventMouseMoved :
+ wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
+ result = noErr ;
+ break ;
+ case kEventMouseDragged :
+ toplevelWindow->MacFireMouseEvent( nullEvent , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
+ result = noErr ;
+ break ;
+ case kEventMouseWheelMoved :
+ {
+ //bClearTooltip = false;
+ EventMouseWheelAxis axis = kEventMouseWheelAxisY;
+ SInt32 delta = 0;
+ Point mouseLoc = {0, 0};
+ if (::GetEventParameter(event, kEventParamMouseWheelAxis, typeMouseWheelAxis,
+ NULL, sizeof(EventMouseWheelAxis), NULL, &axis) == noErr &&
+ ::GetEventParameter(event, kEventParamMouseWheelDelta, typeLongInteger,
+ NULL, sizeof(SInt32), NULL, &delta) == noErr &&
+ ::GetEventParameter(event, kEventParamMouseLocation, typeQDPoint,
+ NULL, sizeof(Point), NULL, &mouseLoc) == noErr)
+ {
+ wxMouseEvent wheelEvent(wxEVT_MOUSEWHEEL);
+
+ wheelEvent.m_x = mouseLoc.h;
+ wheelEvent.m_y = mouseLoc.v;
+
+ wheelEvent.m_wheelRotation = delta;
+ wheelEvent.m_wheelDelta = 1;
+ wheelEvent.m_linesPerAction = 1;
+
+ wxWindow* currentMouseWindow = NULL;
+ wxWindow::MacGetWindowFromPoint(wxPoint(mouseLoc.h, mouseLoc.v), ¤tMouseWindow);
+
+ if (currentMouseWindow)
+ {
+ currentMouseWindow->GetEventHandler()->ProcessEvent(wheelEvent);
+ result = noErr;
+ }
+ }
+ }
+ break ;
+ default :
+ break ;
+ }
+ }
+
+ return result ;
+
+
+}
+static pascal OSStatus WindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ OSStatus err = noErr ;
+
+ UInt32 attributes;
+ WindowRef windowRef ;
+ wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
+
+ GetEventParameter( event, kEventParamDirectObject, typeWindowRef, NULL,
+ sizeof( WindowRef ), NULL, &windowRef );
+
+ switch( GetEventKind( event ) )
+ {
+ case kEventWindowUpdate :
+ if ( !wxPendingDelete.Member(toplevelWindow) )
+ toplevelWindow->MacUpdate( EventTimeToTicks( GetEventTime( event ) ) ) ;
+ result = noErr ;
+ break ;
+ case kEventWindowActivated :
+ toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , true) ;
+ result = noErr ;
+ break ;
+ case kEventWindowDeactivated :
+ toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , false) ;
+ result = noErr ;
+ break ;
+ case kEventWindowShown :
+ toplevelWindow->Refresh() ;
+ result = noErr ;
+ break ;
+ case kEventWindowClose :
+ toplevelWindow->Close() ;
+ result = noErr ;
+ break ;
+ case kEventWindowBoundsChanged :
+ err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
+ NULL, sizeof( UInt32 ), NULL, &attributes );
+ if ( err == noErr )
+ {
+ Rect newContentRect ;
+
+ GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
+ sizeof( newContentRect ), NULL, &newContentRect );
+
+ toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
+ newContentRect.right - newContentRect.left ,
+ newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
+
+ result = noErr;
+ }
+ break ;
+ case kEventWindowBoundsChanging :
+ err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
+ NULL, sizeof( UInt32 ), NULL, &attributes );
+ if ( err == noErr )
+ {
+ Rect newContentRect ;
+
+ GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
+ sizeof( newContentRect ), NULL, &newContentRect );
+
+ wxSize formerSize = toplevelWindow->GetSize() ;
+
+ if ( (attributes & kWindowBoundsChangeSizeChanged ) ||
+ ( attributes & kWindowBoundsChangeOriginChanged ) )
+ toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
+ newContentRect.right - newContentRect.left ,
+ newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
+
+ int x , y , w , h ;
+ toplevelWindow->GetPosition( &x , &y ) ;
+ toplevelWindow->GetSize( &w , &h ) ;
+ Rect adjustedRect = { y , x , y + h , x + w } ;
+
+ if ( !EqualRect( &newContentRect , &adjustedRect ) )
+ {
+ SetEventParameter( event , kEventParamCurrentBounds , typeQDRectangle, sizeof( adjustedRect ) , &adjustedRect ) ;
+ }
+
+ if ( toplevelWindow->GetSize() != formerSize )
+ toplevelWindow->Update() ;
+
+ result = noErr ;
+ }
+ break ;
+ default :
+ break ;
+ }
+ return result ;
+}
+
+pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassKeyboard :
+ result = KeyboardEventHandler( handler, event , data ) ;
+ break ;
+ case kEventClassTextInput :
+ result = TextInputEventHandler( handler, event , data ) ;
+ break ;
+ case kEventClassWindow :
+ result = WindowEventHandler( handler, event , data ) ;
+ break ;
+ case kEventClassMouse :
+ result = MouseEventHandler( handler, event , data ) ;
+ break ;
+ default :
+ break ;
+ }
+ return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
+
+#endif
+