+pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ wxWindowMac* focus = (wxWindowMac*) data ;
+
+ wchar_t* uniChars = NULL ;
+ UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
+
+ UniChar* charBuf;
+ UInt32 dataSize = 0 ;
+ int numChars = 0 ;
+ UniChar buf[2] ;
+ if ( GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
+ {
+ numChars = dataSize / sizeof( UniChar) ;
+ charBuf = buf ;
+
+ if ( dataSize > sizeof(buf) )
+ charBuf = new UniChar[ numChars ] ;
+ else
+ charBuf = buf ;
+
+ uniChars = new wchar_t[ numChars ] ;
+ GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
+#if SIZEOF_WCHAR_T == 2
+ uniChars = (wchar_t*) charBuf ;
+ memcpy( uniChars , charBuf , dataSize ) ;
+#else
+ // the resulting string will never have more chars than the utf16 version, so this is safe
+ wxMBConvUTF16 converter ;
+ numChars = converter.MB2WC( uniChars , (const char*)charBuf , numChars ) ;
+#endif
+ }
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventTextInputUpdateActiveInputArea :
+ {
+ // An IME input event may return several characters, but we need to send one char at a time to
+ // EVT_CHAR
+ for (int pos=0 ; pos < numChars ; pos++)
+ {
+ WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
+ WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
+ wxTheApp->MacSetCurrentEvent( event , handler ) ;
+
+ UInt32 message = (0 << 8) + ((char)uniChars[pos] );
+ if ( wxTheApp->MacSendCharEvent(
+ focus , message , 0 , when , 0 , 0 , uniChars[pos] ) )
+ {
+ result = noErr ;
+ }
+
+ wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
+ }
+ }
+ break ;
+ case kEventTextInputUnicodeForKeyEvent :
+ {
+ UInt32 keyCode, modifiers ;
+ Point point ;
+ EventRef rawEvent ;
+ unsigned char charCode ;
+
+ 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 );
+
+ UInt32 message = (keyCode << 8) + charCode;
+
+ // An IME input event may return several characters, but we need to send one char at a time to
+ // EVT_CHAR
+ for (int pos=0 ; pos < numChars ; pos++)
+ {
+ WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
+ WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
+ wxTheApp->MacSetCurrentEvent( event , handler ) ;
+
+ if ( wxTheApp->MacSendCharEvent(
+ focus , message , modifiers , when , point.h , point.v , uniChars[pos] ) )
+ {
+ result = noErr ;
+ }
+
+ wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
+ }
+ }
+ break;
+ default:
+ break ;
+ }
+
+ delete [] uniChars ;
+ if ( charBuf != buf )
+ delete [] charBuf ;
+
+ return result ;
+}
+
+static pascal OSStatus wxMacWindowCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ wxWindowMac* focus = (wxWindowMac*) data ;
+
+ HICommand command ;
+
+ wxMacCarbonEvent cEvent( event ) ;
+ cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
+
+ wxMenuItem* item = NULL ;
+ wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ;
+ int id = wxMacCommandToId( command.commandID ) ;
+
+ if ( item )
+ {
+ wxASSERT( itemMenu != NULL ) ;
+
+ switch ( cEvent.GetKind() )
+ {
+ case kEventProcessCommand :
+ {
+ if (item->IsCheckable())
+ item->Check( !item->IsChecked() ) ;
+
+ if ( itemMenu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) )
+ result = noErr ;
+ else
+ {
+ wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , id);
+ event.SetEventObject(focus);
+ event.SetInt(item->IsCheckable() ? item->IsChecked() : -1);
+
+ if ( focus->GetEventHandler()->ProcessEvent(event) )
+ result = noErr ;
+ }
+ }
+ break ;
+
+ case kEventCommandUpdateStatus:
+ {
+ wxUpdateUIEvent event(id);
+ event.SetEventObject( itemMenu );
+
+ bool processed = false;
+
+ // Try the menu's event handler
+ {
+ wxEvtHandler *handler = itemMenu->GetEventHandler();
+ if ( handler )
+ processed = handler->ProcessEvent(event);
+ }
+
+ // Try the window the menu was popped up from
+ // (and up through the hierarchy)
+ if ( !processed )
+ {
+ const wxMenuBase *menu = itemMenu;
+ while ( menu )
+ {
+ wxWindow *win = menu->GetInvokingWindow();
+ if ( win )
+ {
+ processed = win->GetEventHandler()->ProcessEvent(event);
+ break;
+ }
+
+ menu = menu->GetParent();
+ }
+ }
+
+ if ( !processed )
+ {
+ processed = focus->GetEventHandler()->ProcessEvent(event);
+ }
+
+ if ( processed )
+ {
+ // if anything changed, update the changed attribute
+ if (event.GetSetText())
+ itemMenu->SetLabel(id, event.GetText());
+ if (event.GetSetChecked())
+ itemMenu->Check(id, event.GetChecked());
+ if (event.GetSetEnabled())
+ itemMenu->Enable(id, event.GetEnabled());
+
+ result = noErr ;
+ }
+ }
+ break ;
+
+ default :
+ break ;
+ }
+ }
+ return result ;
+}
+