+void wxApp::MacHandleOneEvent( WXEVENTREF evr )
+{
+ EventRecord* ev = (EventRecord*) evr ;
+ m_macCurrentEvent = ev ;
+
+ wxApp::sm_lastMessageTime = ev->when ;
+
+ switch (ev->what)
+ {
+ case mouseDown:
+ MacHandleMouseDownEvent( ev ) ;
+ if ( ev->modifiers & controlKey )
+ s_lastMouseDown = 2;
+ else
+ s_lastMouseDown = 1;
+ break;
+ case mouseUp:
+ if ( s_lastMouseDown == 2 )
+ {
+ ev->modifiers |= controlKey ;
+ }
+ else
+ {
+ ev->modifiers &= ~controlKey ;
+ }
+ MacHandleMouseUpEvent( ev ) ;
+ s_lastMouseDown = 0;
+ break;
+ case activateEvt:
+ MacHandleActivateEvent( ev ) ;
+ break;
+ case updateEvt:
+ MacHandleUpdateEvent( ev ) ;
+ break;
+ case keyDown:
+ case autoKey:
+ MacHandleKeyDownEvent( ev ) ;
+ break;
+ case keyUp:
+ MacHandleKeyUpEvent( ev ) ;
+ break;
+ case diskEvt:
+ MacHandleDiskEvent( ev ) ;
+ break;
+ case osEvt:
+ MacHandleOSEvent( ev ) ;
+ break;
+ case kHighLevelEvent:
+ MacHandleHighLevelEvent( ev ) ;
+ break;
+ default:
+ break;
+ }
+ wxMacProcessNotifierAndPendingEvents() ;
+}
+
+void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
+{
+ EventRecord* ev = (EventRecord*) evr ;
+ ::AEProcessAppleEvent( ev ) ;
+}
+
+bool s_macIsInModalLoop = false ;
+
+void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
+{
+ EventRecord* ev = (EventRecord*) evr ;
+ wxToolTip::RemoveToolTips() ;
+
+ WindowRef window;
+ WindowRef frontWindow = ::FrontNonFloatingWindow() ;
+ WindowAttributes frontWindowAttributes = NULL ;
+ if ( frontWindow )
+ ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
+
+ short windowPart = ::FindWindow(ev->where, &window);
+ wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
+ if ( wxPendingDelete.Member(win) )
+ return ;
+
+ BitMap screenBits;
+ GetQDGlobalsScreenBits( &screenBits );
+
+ switch (windowPart)
+ {
+ case inMenuBar :
+ if ( s_macIsInModalLoop )
+ {
+ SysBeep ( 30 ) ;
+ }
+ else
+ {
+ UInt32 menuresult = MenuSelect(ev->where) ;
+ MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
+ s_lastMouseDown = 0;
+ }
+ break ;
+#if !TARGET_CARBON
+ case inSysWindow :
+ SystemClick( ev , window ) ;
+ s_lastMouseDown = 0;
+ break ;
+#endif
+ case inDrag :
+ if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
+ {
+ SysBeep ( 30 ) ;
+ }
+ else
+ {
+ DragWindow(window, ev->where, &screenBits.bounds);
+ if (win)
+ {
+ GrafPtr port ;
+ GetPort( &port ) ;
+ Point pt = { 0, 0 } ;
+ SetPortWindowPort(window) ;
+ LocalToGlobal( &pt ) ;
+ SetPort( port ) ;
+ win->SetSize( pt.h , pt.v , -1 ,
+ -1 , wxSIZE_USE_EXISTING);
+ }
+ s_lastMouseDown = 0;
+ }
+ break ;
+ case inGoAway:
+ if (TrackGoAway(window, ev->where))
+ {
+ if ( win )
+ win->Close() ;
+ }
+ s_lastMouseDown = 0;
+ break;
+ case inGrow:
+ {
+ int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
+ if (growResult != 0)
+ {
+ int newWidth = LoWord(growResult);
+ int newHeight = HiWord(growResult);
+ int oldWidth, oldHeight;
+
+
+ if (win)
+ {
+ win->GetSize(&oldWidth, &oldHeight);
+ if (newWidth == 0)
+ newWidth = oldWidth;
+ if (newHeight == 0)
+ newHeight = oldHeight;
+ win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
+ }
+ }
+ s_lastMouseDown = 0;
+ }
+ break;
+ case inZoomIn:
+ case inZoomOut:
+ if (TrackBox(window, ev->where, windowPart))
+ {
+ // TODO setup size event
+ ZoomWindow( window , windowPart , false ) ;
+ if (win)
+ {
+ Rect tempRect ;
+
+ GetWindowPortBounds(window, &tempRect ) ;
+ win->SetSize( -1, -1, tempRect.right-tempRect.left ,
+ tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
+ }
+ }
+ s_lastMouseDown = 0;
+ break;
+ case inCollapseBox :
+ // TODO setup size event
+ s_lastMouseDown = 0;
+ break ;
+
+ case inContent :
+ {
+ GrafPtr port ;
+ GetPort( &port ) ;
+ SetPortWindowPort(window) ;
+ SetPort( port ) ;
+ }
+ if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
+ {
+ if ( s_macIsInModalLoop )
+ {
+ SysBeep ( 30 ) ;
+ }
+ else if ( UMAIsWindowFloating( window ) )
+ {
+ if ( win )
+ win->MacMouseDown( ev , windowPart ) ;
+ }
+ else
+ {
+ if ( win )
+ win->MacMouseDown( ev , windowPart ) ;
+ ::SelectWindow( window ) ;
+ }
+ }
+ else
+ {
+ if ( win )
+ win->MacMouseDown( ev , windowPart ) ;
+ }
+ break ;
+
+ default:
+ break;
+ }
+}
+
+void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
+{
+ EventRecord* ev = (EventRecord*) evr ;
+ WindowRef window;
+
+ short windowPart = inNoWindow ;
+ if ( wxTheApp->s_captureWindow )
+ {
+ window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
+ windowPart = inContent ;
+ }
+ else
+ {
+ windowPart = ::FindWindow(ev->where, &window) ;
+ }
+
+ switch (windowPart)
+ {
+ case inMenuBar :
+ break ;
+ case inSysWindow :
+ break ;
+ default:
+ {
+ wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
+ if ( win )
+ win->MacMouseUp( ev , windowPart ) ;
+ }
+ break;
+ }
+}
+
+long wxMacTranslateKey(unsigned char key, unsigned char code) ;
+long wxMacTranslateKey(unsigned char key, unsigned char code)
+{
+ long retval = key ;
+ switch (key)
+ {
+ case 0x01 :
+ retval = WXK_HOME;
+ break;
+ case 0x03 :
+ retval = WXK_RETURN;
+ break;
+ case 0x04 :
+ retval = WXK_END;
+ break;
+ case 0x05 :
+ retval = WXK_HELP;
+ break;
+ case 0x08 :
+ retval = WXK_BACK;
+ break;
+ case 0x09 :
+ retval = WXK_TAB;
+ break;
+ case 0x0b :
+ retval = WXK_PAGEUP;
+ break;
+ case 0x0c :
+ retval = WXK_PAGEDOWN;
+ break;
+ case 0x0d :
+ retval = WXK_RETURN;
+ break;
+ case 0x10 :
+ {
+ switch( code )
+ {
+ case 0x7a :
+ retval = WXK_F1 ;
+ break;
+ case 0x78 :
+ retval = WXK_F2 ;
+ break;
+ case 0x63 :
+ retval = WXK_F3 ;
+ break;
+ case 0x76 :
+ retval = WXK_F4 ;
+ break;
+ case 0x60 :
+ retval = WXK_F5 ;
+ break;
+ case 0x61 :
+ retval = WXK_F6 ;
+ break;
+ case 0x62:
+ retval = WXK_F7 ;
+ break;
+ case 0x64 :
+ retval = WXK_F8 ;
+ break;
+ case 0x65 :
+ retval = WXK_F9 ;
+ break;
+ case 0x6D :
+ retval = WXK_F10 ;
+ break;
+ case 0x67 :
+ retval = WXK_F11 ;
+ break;
+ case 0x6F :
+ retval = WXK_F12 ;
+ break;
+ case 0x69 :
+ retval = WXK_F13 ;
+ break;
+ case 0x6B :
+ retval = WXK_F14 ;
+ break;
+ case 0x71 :
+ retval = WXK_F15 ;
+ break;
+ }
+ }
+ break ;
+ case 0x1b :
+ retval = WXK_ESCAPE ;
+ break ;
+ case 0x1c :
+ retval = WXK_LEFT ;
+ break ;
+ case 0x1d :
+ retval = WXK_RIGHT ;
+ break ;
+ case 0x1e :
+ retval = WXK_UP ;
+ break ;
+ case 0x1f :
+ retval = WXK_DOWN ;
+ break ;
+ case 0x7F :
+ retval = WXK_DELETE ;
+ default:
+ break ;
+ } // end switch
+
+ return retval;
+}
+
+void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
+{
+ EventRecord* ev = (EventRecord*) evr ;
+ wxToolTip::RemoveToolTips() ;
+
+ UInt32 menuresult = UMAMenuEvent(ev) ;
+ if ( HiWord( menuresult ) )
+ {
+ if ( !s_macIsInModalLoop )
+ MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
+ }
+ else
+ {
+ short keycode ;
+ short keychar ;
+ keychar = short(ev->message & charCodeMask);
+ keycode = short(ev->message & keyCodeMask) >> 8 ;
+ long keyval = wxMacTranslateKey(keychar, keycode) ;
+ bool handled = false ;
+ wxWindow* focus = wxWindow::FindFocus() ;
+ if ( focus )
+ {
+
+ wxKeyEvent event(wxEVT_KEY_DOWN);
+ event.m_shiftDown = ev->modifiers & shiftKey;
+ event.m_controlDown = ev->modifiers & controlKey;
+ event.m_altDown = ev->modifiers & optionKey;
+ event.m_metaDown = ev->modifiers & cmdKey;
+ event.m_keyCode = keyval;
+ event.m_x = ev->where.h;
+ event.m_y = ev->where.v;
+ event.m_timeStamp = ev->when;
+ event.SetEventObject(focus);
+ handled = focus->GetEventHandler()->ProcessEvent( event ) ;
+ if ( !handled )
+ {
+#if wxUSE_ACCEL
+ if (!handled)
+ {
+ /*
+ wxWindow *ancestor = focus;
+ while (ancestor)
+ {
+ int command = ancestor->GetAcceleratorTable()->GetCommand( event );
+ if (command != -1)
+ {
+ wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
+ handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
+ break;
+ }
+ if (ancestor->m_isFrame)
+ break;
+ ancestor = ancestor->GetParent();
+ }
+ */
+ }
+#endif // wxUSE_ACCEL
+ }
+ if (!handled)
+ {
+ wxKeyEvent event(wxEVT_CHAR);
+ event.m_shiftDown = ev->modifiers & shiftKey;
+ event.m_controlDown = ev->modifiers & controlKey;
+ event.m_altDown = ev->modifiers & optionKey;
+ event.m_metaDown = ev->modifiers & cmdKey;
+ event.m_keyCode = keyval;
+ event.m_x = ev->where.h;
+ event.m_y = ev->where.v;
+ event.m_timeStamp = ev->when;
+ event.SetEventObject(focus);
+ handled = focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ if ( !handled &&
+ (keyval == WXK_TAB) &&
+ (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
+ (focus->GetParent()) &&
+ (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
+ {
+ wxNavigationKeyEvent new_event;
+ new_event.SetEventObject( focus );
+ new_event.SetDirection( !event.ShiftDown() );
+ /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
+ new_event.SetWindowChange( event.ControlDown() );
+ new_event.SetCurrentFocus( focus );
+ handled = focus->GetEventHandler()->ProcessEvent( new_event );
+ }
+ }
+ if ( !handled )
+ {
+ // if window is not having a focus still testing for default enter or cancel
+ // TODO add the UMA version for ActiveNonFloatingWindow
+ focus = wxFindWinFromMacWindow( FrontWindow() ) ;
+ if ( focus )
+ {
+ if ( keyval == WXK_RETURN )
+ {
+ wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
+ wxButton);
+ if ( def && def->IsEnabled() )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
+ event.SetEventObject(def);
+ def->Command(event);
+ return ;
+ }
+ }
+ /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
+ else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
+ {
+ wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
+ new_event.SetEventObject( focus );
+ handled = focus->GetEventHandler()->ProcessEvent( new_event );
+ }
+ }
+ }
+ }
+}
+
+void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )