+#ifdef __WXDEBUG__
+ if (gs_inYield)
+ wxFAIL_MSG( wxT("wxYield called recursively" ) );
+#endif
+
+ gs_inYield = TRUE;
+
+#if wxUSE_THREADS
+ YieldToAnyThread() ;
+#endif
+ EventRecord event ;
+
+ long sleepTime = 1 ; //::GetCaretTime();
+
+ while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, wxApp::s_macCursorRgn))
+ {
+ wxTheApp->MacHandleOneEvent( &event );
+ if ( event.what != kHighLevelEvent )
+ SetRectRgn( wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
+ }
+
+ wxMacProcessNotifierAndPendingEvents() ;
+
+ gs_inYield = FALSE;
+
+ return TRUE;
+}
+
+// Yield to incoming messages; but fail silently if recursion is detected.
+bool wxYieldIfNeeded()
+{
+ if (gs_inYield)
+ return FALSE;
+
+ return wxYield();
+}
+
+// platform specifics
+
+void wxApp::MacSuspend( bool convertClipboard )
+{
+ // we have to deactive the window manually
+
+ wxWindow* window = GetTopWindow() ;
+ if ( window )
+ window->MacActivate( MacGetCurrentEvent() , false ) ;
+
+ s_lastMouseDown = 0 ;
+ if( convertClipboard )
+ {
+ MacConvertPrivateToPublicScrap() ;
+ }
+
+ UMAHideFloatingWindows() ;
+}
+
+void wxApp::MacResume( bool convertClipboard )
+{
+ s_lastMouseDown = 0 ;
+ if( convertClipboard )
+ {
+ MacConvertPublicToPrivateScrap() ;
+ }
+
+ UMAShowFloatingWindows() ;
+}
+
+void wxApp::MacConvertPrivateToPublicScrap()
+{
+}
+
+void wxApp::MacConvertPublicToPrivateScrap()
+{
+}
+
+void wxApp::MacDoOneEvent()
+{
+ EventRecord event ;
+
+ long sleepTime = 1 ; // GetCaretTime() / 4 ;
+
+ if (WaitNextEvent(everyEvent, &event,sleepTime, s_macCursorRgn))
+ {
+ MacHandleOneEvent( &event );
+ }
+ else
+ {
+ // idlers
+ WindowPtr window = UMAFrontWindow() ;
+ if ( window )
+ UMAIdleControls( window ) ;
+
+ wxTheApp->ProcessIdle() ;
+ }
+ if ( event.what != kHighLevelEvent )
+ SetRectRgn( s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
+
+ // repeaters
+
+ DeletePendingObjects() ;
+ wxMacProcessNotifierAndPendingEvents() ;
+}
+
+void wxApp::MacHandleOneEvent( EventRecord *ev )
+{
+ 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( EventRecord *ev )
+{
+ ::AEProcessAppleEvent( ev ) ;
+}
+
+bool s_macIsInModalLoop = false ;
+
+void wxApp::MacHandleMouseDownEvent( EventRecord *ev )
+{
+ wxToolTip::RemoveToolTips() ;
+
+ WindowRef window;
+ WindowRef frontWindow = UMAFrontNonFloatingWindow() ;
+ WindowAttributes frontWindowAttributes = NULL ;
+ if ( frontWindow )
+ UMAGetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
+
+ short windowPart = ::FindWindow(ev->where, &window);
+ wxWindow* 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 } ;
+ #if TARGET_CARBON
+ SetPort( GetWindowPort(window) ) ;
+ #else
+ SetPort( (window) ) ;
+ #endif
+ SetOrigin( 0 , 0 ) ;
+ 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 ) ;
+ #if TARGET_CARBON
+ SetPort( GetWindowPort(window) ) ;
+ #else
+ SetPort( (window) ) ;
+ #endif
+ SetOrigin( 0 , 0 ) ;
+ 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 ) ;
+ UMASelectWindow( window ) ;
+ }
+ }
+ else
+ {
+ if ( win )
+ win->MacMouseDown( ev , windowPart ) ;
+ }
+ break ;
+
+ default:
+ break;
+ }
+}
+
+void wxApp::MacHandleMouseUpEvent( EventRecord *ev )
+{
+ WindowRef window;
+
+ short windowPart = ::FindWindow(ev->where, &window);
+
+ switch (windowPart)
+ {
+ case inMenuBar :
+ break ;
+ case inSysWindow :
+ break ;
+ default:
+ {
+ wxWindow* win = wxFindWinFromMacWindow( window ) ;
+ if ( win )
+ win->MacMouseUp( ev , windowPart ) ;
+ }
+ break;
+ }
+}
+
+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( EventRecord *ev )
+{
+ 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 ;
+
+ wxWindow* focus = wxWindow::FindFocus() ;
+ if ( focus )
+ {
+ long keyval = wxMacTranslateKey(keychar, keycode) ;
+
+ 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);
+ bool 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 );
+ }
+ /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
+ if ( (!handled) &&
+ (keyval == '.' && event.ControlDown() ) )
+ {
+ wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
+ new_event.SetEventObject( focus );
+ handled = focus->GetEventHandler()->ProcessEvent( new_event );
+ }
+ }
+ }
+}
+
+void wxApp::MacHandleKeyUpEvent( EventRecord *ev )
+{
+ wxToolTip::RemoveToolTips() ;
+
+ UInt32 menuresult = UMAMenuEvent(ev) ;
+ if ( HiWord( menuresult ) )
+ {
+ }
+ else
+ {
+ short keycode ;
+ short keychar ;
+ keychar = short(ev->message & charCodeMask);
+ keycode = short(ev->message & keyCodeMask) >> 8 ;
+
+ wxWindow* focus = wxWindow::FindFocus() ;
+ if ( focus )
+ {
+ long keyval = wxMacTranslateKey(keychar, keycode) ;
+
+ wxKeyEvent event(wxEVT_KEY_UP);
+ 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);
+ bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
+ }
+ }
+}
+
+void wxApp::MacHandleActivateEvent( EventRecord *ev )
+{
+ WindowRef window = (WindowRef) ev->message ;
+ if ( window )
+ {
+ bool activate = (ev->modifiers & activeFlag ) ;
+ WindowClass wclass ;
+ UMAGetWindowClass ( window , &wclass ) ;
+ if ( wclass == kFloatingWindowClass )
+ {
+ // if it is a floater we activate/deactivate the front non-floating window instead
+ window = UMAFrontNonFloatingWindow() ;
+ }
+ wxWindow* win = wxFindWinFromMacWindow( window ) ;
+ if ( win )
+ win->MacActivate( ev , activate ) ;
+ }