1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) Stefan Csomor 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation "app.h" 
  18 #include "wx/window.h" 
  20 #include "wx/button.h" 
  23 #include "wx/gdicmn.h" 
  26 #include "wx/cursor.h" 
  29 #include "wx/palette.h" 
  31 #include "wx/dialog.h" 
  32 #include "wx/msgdlg.h" 
  34 #include "wx/module.h" 
  35 #include "wx/memory.h" 
  36 #include "wx/tooltip.h" 
  37 #include "wx/textctrl.h" 
  39 #include "wx/docview.h" 
  53 #include "wx/mac/uma.h" 
  54 #include "wx/mac/macnotfy.h" 
  57 #  include <CoreServices/CoreServices.h> 
  58 #  if defined(WXMAKINGDLL) 
  59 #    include <mach-o/dyld.h> 
  64 #  include <ToolUtils.h> 
  65 #  include <DiskInit.h> 
  69 extern wxList wxPendingDelete
; 
  70 extern wxList 
*wxWinMacWindowList
; 
  71 extern wxList 
*wxWinMacControlList
; 
  73 // statics for implementation 
  75 static bool s_inYield 
= FALSE
; 
  76 static bool s_inOnIdle 
= FALSE
; 
  79 static bool s_inReceiveEvent 
= FALSE 
; 
  80 static EventTime sleepTime 
= kEventDurationNoWait 
; 
  82 static long sleepTime 
= 0 ; 
  85 wxApp 
*wxTheApp 
= NULL
; 
  87 #if !USE_SHARED_LIBRARY 
  88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
) 
  89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
) 
  90     EVT_IDLE(wxApp::OnIdle
) 
  91     EVT_END_SESSION(wxApp::OnEndSession
) 
  92     EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
) 
  97 const short    kMacMinHeap 
= (29 * 1024) ; 
  98 // platform specific static variables 
 100 const short kwxMacMenuBarResource 
= 1 ; 
 101 const short kwxMacAppleMenuId 
= 1 ; 
 103 WXHRGN    
wxApp::s_macCursorRgn 
= NULL
; 
 104 wxWindow
* wxApp::s_captureWindow 
= NULL 
; 
 105 int       wxApp::s_lastMouseDown 
= 0 ; 
 106 long      wxApp::sm_lastMessageTime 
= 0; 
 107 long      wxApp::s_lastModifiers 
= 0 ; 
 110 bool      wxApp::s_macDefaultEncodingIsPC 
= true ; 
 111 bool      wxApp::s_macSupportPCMenuShortcuts 
= true ; 
 112 long      wxApp::s_macAboutMenuItemId 
= wxID_ABOUT 
; 
 113 long      wxApp::s_macPreferencesMenuItemId 
= 0 ; 
 114 long      wxApp::s_macExitMenuItemId 
= wxID_EXIT 
; 
 115 wxString  
wxApp::s_macHelpMenuTitleName 
= wxT("&Help") ; 
 117 //---------------------------------------------------------------------- 
 118 // Core Apple Event Support 
 119 //---------------------------------------------------------------------- 
 121 pascal OSErr 
AEHandleODoc( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long refcon 
) ; 
 122 pascal OSErr 
AEHandleOApp( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long refcon 
) ; 
 123 pascal OSErr 
AEHandlePDoc( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long refcon 
) ; 
 124 pascal OSErr 
AEHandleQuit( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long refcon 
) ; 
 126 pascal OSErr 
AEHandleODoc( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long WXUNUSED(refcon
) ) 
 128     return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event 
, reply
) ; 
 131 pascal OSErr 
AEHandleOApp( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long WXUNUSED(refcon
) ) 
 133     return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event 
, reply 
) ; 
 136 pascal OSErr 
AEHandlePDoc( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long WXUNUSED(refcon
) ) 
 138     return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event 
, reply 
) ; 
 141 pascal OSErr 
AEHandleQuit( const AppleEvent 
*event 
, AppleEvent 
*reply 
, long WXUNUSED(refcon
) ) 
 143     return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event 
, reply
) ; 
 146 // AEODoc Calls MacOpenFile on each of the files passed 
 148 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF 
WXUNUSED(reply
)) 
 152     DescType returnedType
; 
 158     err 
= AEGetParamDesc((AppleEvent 
*)event
, keyDirectObject
, typeAEList
,&docList
); 
 162     err 
= AECountItems(&docList
, &itemsInList
); 
 166     ProcessSerialNumber PSN 
; 
 167     PSN
.highLongOfPSN 
= 0 ; 
 168     PSN
.lowLongOfPSN 
= kCurrentProcess 
; 
 169     SetFrontProcess( &PSN 
) ; 
 171     for (i 
= 1; i 
<= itemsInList
; i
++) { 
 172         AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
, 
 173         (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
); 
 174         wxString fName 
= wxMacFSSpec2MacFilename(&theSpec
); 
 180 // AEPDoc Calls MacPrintFile on each of the files passed 
 182 short wxApp::MacHandleAEPDoc(const WXEVENTREF event 
, WXEVENTREF 
WXUNUSED(reply
)) 
 186     DescType returnedType
; 
 192     err 
= AEGetParamDesc((AppleEvent 
*)event
, keyDirectObject
, typeAEList
,&docList
); 
 196     err 
= AECountItems(&docList
, &itemsInList
); 
 200     ProcessSerialNumber PSN 
; 
 201     PSN
.highLongOfPSN 
= 0 ; 
 202     PSN
.lowLongOfPSN 
= kCurrentProcess 
; 
 203     SetFrontProcess( &PSN 
) ; 
 205     for (i 
= 1; i 
<= itemsInList
; i
++) { 
 206         AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
, 
 207         (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
); 
 208         wxString fName 
= wxMacFSSpec2MacFilename(&theSpec
); 
 214 // AEOApp calls MacNewFile 
 216 short wxApp::MacHandleAEOApp(const WXEVENTREF 
WXUNUSED(event
) , WXEVENTREF 
WXUNUSED(reply
)) 
 222 // AEQuit attempts to quite the application 
 224 short wxApp::MacHandleAEQuit(const WXEVENTREF 
WXUNUSED(event
) , WXEVENTREF 
WXUNUSED(reply
)) 
 226  /*   wxWindow* win = GetTopWindow() ; 
 239 //---------------------------------------------------------------------- 
 240 // Support Routines linking the Mac...File Calls to the Document Manager 
 241 //---------------------------------------------------------------------- 
 243 void wxApp::MacOpenFile(const wxString 
& fileName 
) 
 245     wxDocManager
* dm 
= wxDocManager::GetDocumentManager() ; 
 247         dm
->CreateDocument(fileName 
, wxDOC_SILENT 
) ; 
 250 void wxApp::MacPrintFile(const wxString 
& fileName 
) 
 252     wxDocManager
* dm 
= wxDocManager::GetDocumentManager() ; 
 255         wxDocument 
*doc 
= dm
->CreateDocument(fileName 
, wxDOC_SILENT 
) ; 
 258             wxView
* view 
= doc
->GetFirstView() ; 
 261                 wxPrintout 
*printout 
= view
->OnCreatePrintout(); 
 265                     printer
.Print(view
->GetFrame(), printout
, TRUE
); 
 271                 doc
->DeleteAllViews(); 
 272                 dm
->RemoveDocument(doc
) ; 
 278 void wxApp::MacNewFile() 
 282 //---------------------------------------------------------------------- 
 283 // Carbon Event Handler 
 284 //---------------------------------------------------------------------- 
 288     static const EventTypeSpec eventList
[] = 
 290         { kEventClassCommand
, kEventProcessCommand 
} , 
 291         { kEventClassCommand
, kEventCommandUpdateStatus 
} , 
 293         { kEventClassApplication 
, kEventAppActivated 
} , 
 294         { kEventClassApplication 
, kEventAppDeactivated 
} , 
 295         // handling the quit event is not recommended by apple 
 296         // rather using the quit apple event - which we do 
 298         { kEventClassAppleEvent 
, kEventAppleEvent 
} , 
 300         { kEventClassMouse 
, kEventMouseDown 
} , 
 304 static pascal OSStatus 
MenuEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 306     return eventNotHandledErr 
; 
 309 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop 
 310 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have 
 311 // deal with clicks in the menu bar explicitely 
 313 static pascal OSStatus 
MouseEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 315     OSStatus result 
= eventNotHandledErr 
; 
 317     switch( GetEventKind(event
) ) 
 319         case kEventMouseDown 
: 
 324             GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
, 
 325                 sizeof( Point 
), NULL
, &point 
); 
 326             short windowPart 
= ::FindWindow(point
, &window
); 
 328             if ( windowPart 
== inMenuBar 
) 
 330                 MenuSelect( point 
) ; 
 340 static pascal OSStatus 
CommandEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 342     OSStatus result 
= eventNotHandledErr 
; 
 346     GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
, 
 347         sizeof( HICommand 
), NULL
, &command 
); 
 349     MenuCommand id 
= command
.commandID 
; 
 350     if ( id 
== kHICommandPreferences 
) 
 351         id 
= wxApp::s_macPreferencesMenuItemId 
; 
 353     wxMenuBar
* mbar 
= wxMenuBar::MacGetInstalledMenuBar() ; 
 354     wxMenu
* menu 
= NULL 
; 
 355     wxMenuItem
* item 
= NULL 
; 
 358         item 
= mbar
->FindItem( id 
, &menu 
) ; 
 360     if ( item 
== NULL 
|| menu 
== NULL 
|| mbar 
== NULL 
) 
 363        switch( GetEventKind( event 
) ) 
 365            case kEventProcessCommand 
: 
 367                 if (item
->IsCheckable()) 
 369                     item
->Check( !item
->IsChecked() ) ; 
 372                 menu
->SendEvent( id 
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ; 
 376         case kEventCommandUpdateStatus
: 
 377             // eventually trigger an updateui round 
 387 static pascal OSStatus 
ApplicationEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 389     OSStatus result 
= eventNotHandledErr 
; 
 390     switch ( GetEventKind( event 
) ) 
 392         case kEventAppActivated 
: 
 395                     wxTheApp
->MacResume( true ) ; 
 399         case kEventAppDeactivated 
: 
 402                     wxTheApp
->MacSuspend( true ) ; 
 412 pascal OSStatus 
wxAppEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 414     OSStatus result 
= eventNotHandledErr 
; 
 415     switch( GetEventClass( event 
) ) 
 417         case kEventClassCommand 
: 
 418             result 
= CommandEventHandler( handler 
, event 
, data 
) ; 
 420         case kEventClassApplication 
: 
 421             result 
= ApplicationEventHandler( handler 
, event 
, data 
) ; 
 423         case kEventClassMenu 
: 
 424             result 
= MenuEventHandler( handler 
, event 
, data 
) ; 
 426         case kEventClassMouse 
: 
 427             result 
= MouseEventHandler( handler 
, event 
, data 
) ; 
 429         case kEventClassAppleEvent 
: 
 432                 wxMacConvertEventToRecord( event 
, &rec 
) ; 
 433                 result 
= AEProcessAppleEvent( &rec 
) ; 
 443 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler 
) 
 447 #if defined(WXMAKINGDLL) && !defined(__DARWIN__) 
 448 // we know it's there ;-) 
 449 WXIMPORT 
char std::__throws_bad_alloc 
; 
 452 bool wxApp::Initialize() 
 458     UMAInitToolbox( 4 ) ; 
 459     SetEventMask( everyEvent 
) ; 
 460     UMAShowWatchCursor() ; 
 462 #if defined(WXMAKINGDLL) && defined(__DARWIN__) 
 463     // open shared library resources from here since we don't have 
 464     //   __wxinitialize in Mach-O shared libraries 
 465     wxStAppResource::OpenSharedLibraryResource(NULL
); 
 469     // test the minimal configuration necessary 
 475     if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
) 
 477         error 
= kMacSTRWrongMachine
; 
 479     else if (theMachine 
< gestaltMacPlus
) 
 481         error 
= kMacSTRWrongMachine
; 
 483     else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr 
) 
 485         error 
= kMacSTROldSystem  
; 
 487     else if ( theSystem 
< 0x0860 ) 
 489         error 
= kMacSTROldSystem  
; 
 491     else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
) 
 493         error 
= kMacSTRSmallSize
; 
 499         if ( !UMAHasAppearance() ) 
 501             error = kMacSTRNoPre8Yet ; 
 507     // if we encountered any problems so far, give the error code and exit immediately 
 511         wxStAppResource resload 
; 
 515         GetIndString(message
, 128, error
); 
 516         UMAShowArrowCursor() ; 
 517         ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p"); 
 518         itemHit 
= Alert(128, nil
); 
 523 #  if __option(profile) 
 524     ProfilerInit( collectDetailed
, bestTimeBase 
, 20000 , 40 ) ; 
 529     // now avoid exceptions thrown for new (bad_alloc) 
 530     // FIXME CS for some changes outside wxMac does not compile anymore 
 532     std::__throws_bad_alloc 
= 0 ; 
 536     wxMacSetupConverters() ; 
 538     s_macCursorRgn 
= ::NewRgn() ; 
 540     wxClassInfo::InitializeClasses(); 
 543 //    wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion); 
 547     wxPendingEventsLocker 
= new wxCriticalSection
; 
 550     wxTheColourDatabase 
= new wxColourDatabase(wxKEY_STRING
); 
 551     wxTheColourDatabase
->Initialize(); 
 553     wxWinMacWindowList 
= new wxList(wxKEY_INTEGER
); 
 554     wxWinMacControlList 
= new wxList(wxKEY_INTEGER
); 
 556     wxInitializeStockLists(); 
 557     wxInitializeStockObjects(); 
 559     wxBitmap::InitStandardHandlers(); 
 561     wxModule::RegisterModules(); 
 562     if (!wxModule::InitializeModules()) { 
 566     wxMacCreateNotifierTable() ; 
 568     UMAShowArrowCursor() ; 
 573 bool wxApp::OnInitGui() 
 575     if( !wxAppBase::OnInitGui() ) 
 579     InstallStandardEventHandler( GetApplicationEventTarget() ) ; 
 581     InstallApplicationEventHandler( 
 582         GetwxAppEventHandlerUPP(), 
 583         GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
)); 
 586 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340) 
 587     AEInstallEventHandler( kCoreEventClass 
, kAEOpenDocuments 
, 
 588                            NewAEEventHandlerUPP(AEHandleODoc
) , 
 590     AEInstallEventHandler( kCoreEventClass 
, kAEOpenApplication 
, 
 591                            NewAEEventHandlerUPP(AEHandleOApp
) , 
 593     AEInstallEventHandler( kCoreEventClass 
, kAEPrintDocuments 
, 
 594                            NewAEEventHandlerUPP(AEHandlePDoc
) , 
 596     AEInstallEventHandler( kCoreEventClass 
, kAEQuitApplication 
, 
 597                            NewAEEventHandlerUPP(AEHandleQuit
) , 
 600     AEInstallEventHandler( kCoreEventClass 
, kAEOpenDocuments 
, 
 601                            NewAEEventHandlerProc(AEHandleODoc
) , 
 603     AEInstallEventHandler( kCoreEventClass 
, kAEOpenApplication 
, 
 604                            NewAEEventHandlerProc(AEHandleOApp
) , 
 606     AEInstallEventHandler( kCoreEventClass 
, kAEPrintDocuments 
, 
 607                            NewAEEventHandlerProc(AEHandlePDoc
) , 
 609     AEInstallEventHandler( kCoreEventClass 
, kAEQuitApplication 
, 
 610                            NewAEEventHandlerProc(AEHandleQuit
) , 
 617 void wxApp::CleanUp() 
 619     wxToolTip::RemoveToolTips() ; 
 621     // flush the logged messages if any and install a 'safer' log target: the 
 622     // default one (wxLogGui) can't be used after the resources are freed just 
 623     // below and the user suppliedo ne might be even more unsafe (using any 
 624     // wxWindows GUI function is unsafe starting from now) 
 625     wxLog::DontCreateOnDemand(); 
 627     // this will flush the old messages if any 
 628     delete wxLog::SetActiveTarget(new wxLogStderr
); 
 631     // One last chance for pending objects to be cleaned up 
 632     wxTheApp
->DeletePendingObjects(); 
 634     wxModule::CleanUpModules(); 
 636     wxDeleteStockObjects() ; 
 638     // Destroy all GDI lists, etc. 
 639     wxDeleteStockLists(); 
 641     delete wxTheColourDatabase
; 
 642     wxTheColourDatabase 
= NULL
; 
 644     wxBitmap::CleanUpHandlers(); 
 646     wxMacDestroyNotifierTable() ; 
 647     if (wxWinMacWindowList
) { 
 648         delete wxWinMacWindowList 
; 
 650     if (wxWinMacControlList
) { 
 651         delete wxWinMacControlList 
; 
 653     delete wxPendingEvents
; 
 654     wxPendingEvents 
= NULL
; 
 657     delete wxPendingEventsLocker
; 
 658     // There is still more cleanup code that will try to use this if not NULL. 
 659     wxPendingEventsLocker 
= NULL
; 
 660     // If we don't do the following, we get an apparent memory leak. 
 661     ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker(); 
 664     wxClassInfo::CleanUpClasses(); 
 667 #  if __option(profile) 
 668     ProfilerDump( "\papp.prof" ) ; 
 676 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT 
 677     // At this point we want to check if there are any memory 
 678     // blocks that aren't part of the wxDebugContext itself, 
 679     // as a special case. Then when dumping we need to ignore 
 680     // wxDebugContext, too. 
 681     if (wxDebugContext::CountObjectsLeft(TRUE
) > 0) 
 683         wxLogDebug(wxT("There were memory leaks.")); 
 684         wxDebugContext::Dump(); 
 685         wxDebugContext::PrintStatistics(); 
 687     //  wxDebugContext::SetStream(NULL, NULL); 
 691     // do it as the very last thing because everything else can log messages 
 692     delete wxLog::SetActiveTarget(NULL
); 
 695 #if defined(WXMAKINGDLL) && defined(__DARWIN__) 
 696     // close shared library resources from here since we don't have 
 697     //   __wxterminate in Mach-O shared libraries 
 698     wxStAppResource::CloseSharedLibraryResource(); 
 700     wxMacCleanupConverters() ; 
 702     UMACleanupToolbox() ; 
 703     if (s_macCursorRgn
) { 
 704         ::DisposeRgn((RgnHandle
)s_macCursorRgn
); 
 712 //---------------------------------------------------------------------- 
 714 //---------------------------------------------------------------------- 
 716 // extern variable for shared library resource id 
 717 // need to be able to find it with NSLookupAndBindSymbol 
 718 short gSharedLibraryResource 
= kResFileNotOpened 
; 
 720 #if defined(WXMAKINGDLL) && defined(__DARWIN__) 
 721 CFBundleRef gSharedLibraryBundle 
= NULL
; 
 722 #endif /* WXMAKINGDLL && __DARWIN__ */ 
 724 wxStAppResource::wxStAppResource() 
 726     m_currentRefNum 
= CurResFile() ; 
 727     if ( gSharedLibraryResource 
!= kResFileNotOpened 
) 
 729         UseResFile( gSharedLibraryResource 
) ; 
 733 wxStAppResource::~wxStAppResource() 
 735     if ( m_currentRefNum 
!= kResFileNotOpened 
) 
 737         UseResFile( m_currentRefNum 
) ; 
 741 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
) 
 743     gSharedLibraryResource 
= kResFileNotOpened
; 
 746     if ( initBlock 
!= NULL 
) { 
 747         const CFragInitBlock 
*theInitBlock 
= (const CFragInitBlock 
*)initBlock
; 
 748         FSSpec 
*fileSpec 
= NULL
; 
 750         if (theInitBlock
->fragLocator
.where 
== kDataForkCFragLocator
) { 
 751             fileSpec 
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
; 
 753         else if (theInitBlock
->fragLocator
.where 
== kResourceCFragLocator
) { 
 754             fileSpec 
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
; 
 757         if (fileSpec 
!= NULL
) { 
 758             gSharedLibraryResource 
=  FSpOpenResFile(fileSpec
, fsRdPerm
); 
 763         // Open the shared library resource file if it is not yet open 
 766         const char *theLibPath
; 
 768         gSharedLibraryBundle 
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows")); 
 769         if (gSharedLibraryBundle 
!= NULL
) { 
 770             // wxWindows has been bundled into a framework 
 771             //   load the framework resources 
 773             gSharedLibraryResource 
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
); 
 776             // wxWindows is a simple dynamic shared library 
 777             //   load the resources from the data fork of a separate resource file 
 782             OSErr  theErr 
= noErr
; 
 784             // get the library path 
 785             theSymbol 
= NSLookupAndBindSymbol("_gSharedLibraryResource"); 
 786             theModule 
= NSModuleForSymbol(theSymbol
); 
 787             theLibPath 
= NSLibraryNameForModule(theModule
); 
 789             // if we call wxLogDebug from here then, as wxTheApp hasn't been 
 790             // created yet when we're called from wxApp::Initialize(), wxLog 
 791             // is going to create a default stderr-based log target instead of 
 792             // the expected normal GUI one -- don't do it, if we really want 
 793             // to see this message just use fprintf() here 
 795             wxLogDebug( wxT("wxMac library installation name is '%s'"), 
 799             // allocate copy to replace .dylib.* extension with .rsrc 
 800             theResPath 
= strdup(theLibPath
); 
 801             if (theResPath 
!= NULL
) { 
 802                 theName 
= strrchr(theResPath
, '/'); 
 803                 if (theName 
== NULL
) { 
 804                     // no directory elements in path 
 805                     theName 
= theResPath
; 
 807                 // find ".dylib" shared library extension 
 808                 theExt 
= strstr(theName
, ".dylib"); 
 809                 // overwrite extension with ".rsrc" 
 810                 strcpy(theExt
, ".rsrc"); 
 813                 wxLogDebug( wxT("wxMac resources file name is '%s'"), 
 817                 theErr 
= FSPathMakeRef((UInt8 
*) theResPath
, &theResRef
, false); 
 818                 if (theErr 
!= noErr
) { 
 819                     // try in current directory (using name only) 
 820                     theErr 
= FSPathMakeRef((UInt8 
*) theName
, &theResRef
, false); 
 823                 // open the resource file 
 824                 if (theErr 
== noErr
) { 
 825                     theErr 
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
, 
 826                                                  &gSharedLibraryResource
); 
 828                 if (theErr 
!= noErr
) { 
 831                             wxT("unable to open wxMac resource file '%s'\n"), 
 833 #endif // __WXDEBUG__ 
 836                 // free duplicated resource file path 
 840 #endif /* __DARWIN__ */ 
 842 #endif /* WXMAKINGDLL */ 
 845 void wxStAppResource::CloseSharedLibraryResource() 
 848     // Close the shared library resource file 
 849     if (gSharedLibraryResource 
!= kResFileNotOpened
) { 
 851         if (gSharedLibraryBundle 
!= NULL
) { 
 852             CFBundleCloseBundleResourceMap(gSharedLibraryBundle
, 
 853                                            gSharedLibraryResource
); 
 854             gSharedLibraryBundle 
= NULL
; 
 857 #endif /* __DARWIN__ */ 
 859             CloseResFile(gSharedLibraryResource
); 
 861         gSharedLibraryResource 
= kResFileNotOpened
; 
 863 #endif /* WXMAKINGDLL */ 
 866 #if defined(WXMAKINGDLL) && !defined(__DARWIN__) 
 868 // for shared libraries we have to manually get the correct resource 
 869 // ref num upon initializing and releasing when terminating, therefore 
 870 // the __wxinitialize and __wxterminate must be used 
 873     void __sinit(void);    /*    (generated by linker)    */ 
 874     pascal OSErr 
__initialize(const CFragInitBlock 
*theInitBlock
); 
 875     pascal void __terminate(void); 
 878 pascal OSErr 
__wxinitialize(const CFragInitBlock 
*theInitBlock
) 
 880     wxStAppResource::OpenSharedLibraryResource( theInitBlock 
) ; 
 881     return __initialize( theInitBlock 
) ; 
 884 pascal void __wxterminate(void) 
 886     wxStAppResource::CloseSharedLibraryResource() ; 
 890 #endif /* WXMAKINGDLL && !__DARWIN__ */ 
 892 int WXDLLEXPORT 
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] ) 
 894     return wxApp::Initialize(); 
 897 int WXDLLEXPORT 
wxEntryInitGui() 
 899     return wxTheApp
->OnInitGui(); 
 902 void WXDLLEXPORT 
wxEntryCleanup() 
 907 int wxEntry( int argc
, char *argv
[] , bool enterLoop 
) 
 910 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT 
 911     // This seems to be necessary since there are 'rogue' 
 912     // objects present at this point (perhaps global objects?) 
 913     // Setting a checkpoint will ignore them as far as the 
 914     // memory checking facility is concerned. 
 915     // Of course you may argue that memory allocated in globals should be 
 916     // checked, but this is a reasonable compromise. 
 917     wxDebugContext::SetCheckpoint(); 
 920     if (!wxEntryStart(argc
, argv
)) { 
 923    // create the application object or ensure that one already exists 
 926         // The app may have declared a global application object, but we recommend 
 927         // the IMPLEMENT_APP macro is used instead, which sets an initializer 
 928         // function for delayed, dynamic app object construction. 
 929         wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, 
 930                      wxT("No initializer - use IMPLEMENT_APP macro.") ); 
 932         wxTheApp 
= (wxApp
*) (*wxApp::GetInitializerFunction()) (); 
 935     wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") ); 
 938     // Mac OS X passes a process serial number command line argument when 
 939     // the application is launched from the Finder. This argument must be 
 940     // removed from the command line arguments before being handled by the 
 941     // application (otherwise applications would need to handle it) 
 944         if (strncmp(argv
[1], "-psn_", 5) == 0) { 
 945             // assume the argument is always the only one and remove it 
 950     argc 
= 0 ; // currently we don't support files as parameters 
 952     // we could try to get the open apple events here to adjust argc and argv better 
 954     wxTheApp
->argc 
= argc
; 
 956     wxTheApp
->argv 
= new wxChar
*[argc
+1]; 
 958     for ( mb_argc 
= 0; mb_argc 
< argc
; mb_argc
++ ) 
 960         wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
])); 
 962     wxTheApp
->argv
[mb_argc
] = (wxChar 
*)NULL
; 
 964     wxTheApp
->argv 
= argv
; 
 967     // GUI-specific initialization, such as creating an app context. 
 970     // Here frames insert themselves automatically 
 971     // into wxTopLevelWindows by getting created 
 976     if ( wxTheApp
->OnInit() ) 
 980             retValue 
= wxTheApp
->OnRun(); 
 983             // We want to initialize, but not run or exit immediately. 
 986     //else: app initialization failed, so we skipped OnRun() 
 988     wxWindow 
*topWindow 
= wxTheApp
->GetTopWindow(); 
 991         // Forcibly delete the window. 
 992         if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) || 
 993                 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) ) 
 995             topWindow
->Close(TRUE
); 
 996             wxTheApp
->DeletePendingObjects(); 
1001             wxTheApp
->SetTopWindow(NULL
); 
1014 bool wxMacConvertEventToRecord( EventRef event 
, EventRecord 
*rec
) 
1016     bool converted 
= ConvertEventRefToEventRecord( event
,rec
) ; 
1017     OSStatus err 
= noErr 
; 
1020         switch( GetEventClass( event 
) ) 
1022             case kEventClassKeyboard 
: 
1025                 switch( GetEventKind(event
) ) 
1027                     case kEventRawKeyDown 
: 
1028                         rec
->what 
= keyDown 
; 
1030                     case kEventRawKeyRepeat 
: 
1031                         rec
->what 
= autoKey 
; 
1033                     case kEventRawKeyUp 
: 
1036                     case kEventRawKeyModifiersChanged 
: 
1037                         rec
->what 
= nullEvent 
; 
1046                     unsigned char charCode 
; 
1048                     GetMouse( &rec
->where
) ; 
1050                     err 
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
); 
1051                     err 
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
); 
1052                     err 
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
); 
1053                     rec
->modifiers 
= modifiers 
; 
1054                     rec
->message 
= (keyCode 
<< 8 ) + charCode 
; 
1058             case kEventClassTextInput 
: 
1060                 switch( GetEventKind( event 
) ) 
1062                     case kEventTextInputUnicodeForKeyEvent 
: 
1065                             err 
= GetEventParameter( event 
, kEventParamTextInputSendKeyboardEvent 
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent 
) ; 
1069                                 unsigned char charCode 
; 
1071                                 GetMouse( &rec
->where
) ; 
1072                                 rec
->what 
= keyDown 
; 
1073                                 err 
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
); 
1074                                 err 
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
); 
1075                                 err 
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
); 
1076                                 rec
->modifiers 
= modifiers 
; 
1077                                 rec
->message 
= (keyCode 
<< 8 ) + charCode 
; 
1093 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) 
1095     OSStatus result = eventNotHandledErr ; 
1098     switch ( GetEventClass( event ) ) 
1100         case kEventClassKeyboard : 
1101             if ( wxMacConvertEventToRecord( event , &rec ) ) 
1103                 wxTheApp->MacHandleModifierEvents( &rec ) ; 
1104                 wxTheApp->MacHandleOneEvent( &rec ) ; 
1108         case kEventClassTextInput : 
1109             if ( wxMacConvertEventToRecord( event , &rec ) ) 
1111                 wxTheApp->MacHandleModifierEvents( &rec ) ; 
1112                 wxTheApp->MacHandleOneEvent( &rec ) ; 
1124 // Static member initialization 
1125 wxAppInitializerFunction 
wxAppBase::m_appInitFn 
= (wxAppInitializerFunction
) NULL
; 
1132 #if WXWIN_COMPATIBILITY_2_2 
1133   m_wantDebugOutput 
= TRUE
; 
1139   m_printMode 
= wxPRINT_WINDOWS
; 
1143 bool wxApp::Initialized() 
1151 int wxApp::MainLoop() 
1163 // Returns TRUE if more time is needed. 
1164 bool wxApp::ProcessIdle() 
1167     event
.SetEventObject(this); 
1168     ProcessEvent(event
); 
1170     return event
.MoreRequested(); 
1173 void wxApp::ExitMainLoop() 
1175       m_keepGoing 
= FALSE
; 
1178 // Is a message/event pending? 
1179 bool wxApp::Pending() 
1182     return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ; 
1186       return EventAvail( everyEvent 
, &event 
) ; 
1190 // Dispatch a message. 
1191 void wxApp::Dispatch() 
1196 void wxApp::OnIdle(wxIdleEvent
& event
) 
1198     // Avoid recursion (via ProcessEvent default case) 
1205   // 'Garbage' collection of windows deleted with Close(). 
1206   DeletePendingObjects(); 
1208   // flush the logged messages if any 
1209   wxLog 
*pLog 
= wxLog::GetActiveTarget(); 
1210   if ( pLog 
!= NULL 
&& pLog
->HasPendingMessages() ) 
1213   // Send OnIdle events to all windows 
1214   bool needMore 
= SendIdleEvents(); 
1217     event
.RequestMore(TRUE
); 
1219     // If they are pending events, we must process them: pending events are 
1220     // either events to the threads other than main or events posted with 
1221     // wxPostEvent() functions 
1222     wxMacProcessNotifierAndPendingEvents(); 
1232 // Send idle event to all top-level windows 
1233 bool wxApp::SendIdleEvents() 
1235     bool needMore 
= FALSE
; 
1236     wxWindowListNode
* node 
= wxTopLevelWindows
.GetFirst(); 
1239         wxWindow
* win 
= node
->GetData(); 
1240         if (SendIdleEvents(win
)) 
1243         node 
= node
->GetNext(); 
1248 // Send idle event to window and all subwindows 
1249 bool wxApp::SendIdleEvents(wxWindow
* win
) 
1251     bool needMore 
= FALSE
; 
1254     event
.SetEventObject(win
); 
1255     win
->ProcessEvent(event
); 
1257     if (event
.MoreRequested()) 
1260     wxWindowListNode
* node 
= win
->GetChildren().GetFirst(); 
1263         wxWindow
* win 
= node
->GetData(); 
1264         if (SendIdleEvents(win
)) 
1267         node 
= node
->GetNext(); 
1272 void wxApp::DeletePendingObjects() 
1274   wxNode 
*node 
= wxPendingDelete
.GetFirst(); 
1277     wxObject 
*obj 
= (wxObject 
*)node
->GetData(); 
1281     if (wxPendingDelete
.Member(obj
)) 
1284     // Deleting one object may have deleted other pending 
1285     // objects, so start from beginning of list again. 
1286     node 
= wxPendingDelete
.GetFirst(); 
1292     wxLogError(_("Fatal error: exiting")); 
1298 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
)) 
1301         GetTopWindow()->Close(TRUE
); 
1304 // Default behaviour: close the application with prompts. The 
1305 // user can veto the close, and therefore the end session. 
1306 void wxApp::OnQueryEndSession(wxCloseEvent
& event
) 
1310         if (!GetTopWindow()->Close(!event
.CanVeto())) 
1315 extern "C" void wxCYield() ; 
1321 // Yield to other processes 
1323 bool wxApp::Yield(bool onlyIfNeeded
) 
1327         if ( !onlyIfNeeded 
) 
1329             wxFAIL_MSG( wxT("wxYield called recursively" ) ); 
1338     YieldToAnyThread() ; 
1340     // by definition yield should handle all non-processed events 
1344     OSStatus status 
= noErr 
; 
1347         s_inReceiveEvent 
= true ; 
1348         status 
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ; 
1349         s_inReceiveEvent 
= false ; 
1351         if ( status 
== eventLoopTimedOutErr 
) 
1353             // make sure next time the event loop will trigger idle events 
1354             sleepTime 
= kEventDurationNoWait 
; 
1356         else if ( status 
== eventLoopQuitErr 
) 
1358             // according to QA1061 this may also occur when a WakeUp Process 
1363             MacHandleOneEvent( theEvent 
) ; 
1364             ReleaseEvent(theEvent
); 
1366     } while( status 
== noErr 
) ; 
1370     // having a larger value here leads to large performance slowdowns 
1371     // so we cannot give background apps more processor time here 
1372     // we do so however having a large sleep value in the main event loop 
1375     while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
)) 
1377         MacHandleModifierEvents( &event 
) ; 
1378         MacHandleOneEvent( &event 
); 
1379         if ( event
.what 
!= kHighLevelEvent 
) 
1380             SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn 
, event
.where
.h 
, event
.where
.v 
,  event
.where
.h 
+ 1 , event
.where
.v 
+ 1 ) ; 
1382     MacHandleModifierEvents( &event 
) ; 
1385     wxMacProcessNotifierAndPendingEvents() ; 
1391 // platform specifics 
1393 void wxApp::MacSuspend( bool convertClipboard 
) 
1396     // we have to deactive the top level windows manually 
1398     wxWindowListNode
* node 
= wxTopLevelWindows
.GetFirst(); 
1401         wxTopLevelWindow
* win 
= (wxTopLevelWindow
*) node
->Data(); 
1402         win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when 
, false ) ; 
1404         node 
= node
->GetNext(); 
1407     ::HideFloatingWindows() ; 
1409     s_lastMouseDown 
= 0 ; 
1411     if( convertClipboard 
) 
1413         MacConvertPrivateToPublicScrap() ; 
1417 extern wxList wxModalDialogs
; 
1419 void wxApp::MacResume( bool convertClipboard 
) 
1421     s_lastMouseDown 
= 0 ; 
1422     if( convertClipboard 
) 
1424         MacConvertPublicToPrivateScrap() ; 
1428     ::ShowFloatingWindows() ; 
1429     // raise modal dialogs in case a non modal window was selected to activate the app 
1431     wxNode
* node 
= wxModalDialogs
.GetFirst(); 
1434         wxDialog
* dialog 
= (wxDialog 
*) node
->GetData(); 
1437         node 
= node
->GetNext(); 
1442 void wxApp::MacConvertPrivateToPublicScrap() 
1446 void wxApp::MacConvertPublicToPrivateScrap() 
1450 void wxApp::MacDoOneEvent() 
1455     s_inReceiveEvent 
= true ; 
1456     OSStatus status 
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ; 
1457     s_inReceiveEvent 
= false ; 
1458     if ( status 
== eventLoopTimedOutErr 
) 
1460         if ( wxTheApp
->ProcessIdle() ) 
1461             sleepTime 
= kEventDurationNoWait 
; 
1463             sleepTime 
= kEventDurationForever 
; 
1465     else if ( status 
== eventLoopQuitErr 
) 
1467         // according to QA1061 this may also occur when a WakeUp Process 
1472         MacHandleOneEvent( theEvent 
) ; 
1473         ReleaseEvent(theEvent
); 
1474         sleepTime 
= kEventDurationNoWait 
; 
1479     EventMask eventMask 
= everyEvent 
; 
1481     if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
)) 
1483         MacHandleModifierEvents( &event 
) ; 
1484         MacHandleOneEvent( &event 
); 
1488         MacHandleModifierEvents( &event 
) ; 
1490         WindowPtr window 
= ::FrontWindow() ; 
1492             ::IdleControls( window 
) ; 
1494         if ( wxTheApp
->ProcessIdle() ) 
1497             sleepTime 
= GetCaretTime() / 2 ; 
1499     if ( event
.what 
!= kHighLevelEvent 
) 
1500         SetRectRgn( (RgnHandle
) s_macCursorRgn 
, event
.where
.h 
, event
.where
.v 
,  event
.where
.h 
+ 1 , event
.where
.v 
+ 1 ) ; 
1504     DeletePendingObjects() ; 
1505     wxMacProcessNotifierAndPendingEvents() ; 
1508 void wxApp::MacHandleOneEvent( WXEVENTREF evr 
) 
1511     EventTargetRef theTarget
; 
1512     theTarget 
= GetEventDispatcherTarget(); 
1513     m_macCurrentEvent 
= evr 
; 
1514     SendEventToEventTarget ((EventRef
) evr 
, theTarget
); 
1516     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1517     m_macCurrentEvent 
= ev 
; 
1519     wxApp::sm_lastMessageTime 
= ev
->when 
; 
1524             MacHandleMouseDownEvent( ev 
) ; 
1525             if ( ev
->modifiers 
& controlKey 
) 
1526                 s_lastMouseDown 
= 2; 
1528                 s_lastMouseDown 
= 1; 
1531             if ( s_lastMouseDown 
== 2 ) 
1533                 ev
->modifiers 
|= controlKey 
; 
1537                 ev
->modifiers 
&= ~controlKey 
; 
1539             MacHandleMouseUpEvent( ev 
) ; 
1540             s_lastMouseDown 
= 0; 
1543             MacHandleActivateEvent( ev 
) ; 
1546             MacHandleUpdateEvent( ev 
) ; 
1550             MacHandleKeyDownEvent( ev 
) ; 
1553             MacHandleKeyUpEvent( ev 
) ; 
1556             MacHandleDiskEvent( ev 
) ; 
1559             MacHandleOSEvent( ev 
) ; 
1561         case kHighLevelEvent
: 
1562             MacHandleHighLevelEvent( ev 
) ; 
1568     wxMacProcessNotifierAndPendingEvents() ; 
1572 bool s_macIsInModalLoop 
= false ; 
1574 void wxApp::MacHandleModifierEvents( WXEVENTREF evr 
) 
1576     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1577     if ( ev
->modifiers 
!= s_lastModifiers 
&& wxWindow::FindFocus() != NULL 
) 
1579         wxKeyEvent 
event(wxEVT_KEY_DOWN
); 
1581         event
.m_shiftDown 
= ev
->modifiers 
& shiftKey
; 
1582         event
.m_controlDown 
= ev
->modifiers 
& controlKey
; 
1583         event
.m_altDown 
= ev
->modifiers 
& optionKey
; 
1584         event
.m_metaDown 
= ev
->modifiers 
& cmdKey
; 
1586         event
.m_x 
= ev
->where
.h
; 
1587         event
.m_y 
= ev
->where
.v
; 
1588         event
.m_timeStamp 
= ev
->when
; 
1589         wxWindow
* focus 
= wxWindow::FindFocus() ; 
1590         event
.SetEventObject(focus
); 
1592         if ( (ev
->modifiers 
^ s_lastModifiers 
) & controlKey 
) 
1594             event
.m_keyCode 
= WXK_CONTROL 
; 
1595             event
.SetEventType( ( ev
->modifiers 
& controlKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
1596             focus
->GetEventHandler()->ProcessEvent( event 
) ; 
1598         if ( (ev
->modifiers 
^ s_lastModifiers 
) & shiftKey 
) 
1600             event
.m_keyCode 
= WXK_SHIFT 
; 
1601             event
.SetEventType( ( ev
->modifiers 
& shiftKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
1602             focus
->GetEventHandler()->ProcessEvent( event 
) ; 
1604         if ( (ev
->modifiers 
^ s_lastModifiers 
) & optionKey 
) 
1606             event
.m_keyCode 
= WXK_ALT 
; 
1607             event
.SetEventType( ( ev
->modifiers 
& optionKey 
) ? wxEVT_KEY_DOWN 
: wxEVT_KEY_UP 
) ; 
1608             focus
->GetEventHandler()->ProcessEvent( event 
) ; 
1610         s_lastModifiers 
= ev
->modifiers 
; 
1614 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr 
) 
1616     // we must avoid reentrancy problems when processing high level events eg printing 
1617     bool former 
= s_inYield 
; 
1619     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1620     ::AEProcessAppleEvent( ev 
) ; 
1621     s_inYield 
= former 
; 
1624 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr 
) 
1626     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1627     wxToolTip::RemoveToolTips() ; 
1630     WindowRef frontWindow 
= ::FrontNonFloatingWindow() ; 
1631     WindowAttributes frontWindowAttributes 
= NULL 
; 
1633         ::GetWindowAttributes( frontWindow 
, &frontWindowAttributes 
) ; 
1635     short windowPart 
= ::FindWindow(ev
->where
, &window
); 
1636     wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( window 
) ; 
1637     if ( wxPendingDelete
.Member(win
) ) 
1641     GetQDGlobalsScreenBits( &screenBits 
); 
1646             if ( s_macIsInModalLoop 
) 
1652                 UInt32 menuresult 
= MenuSelect(ev
->where
) ; 
1653                MacHandleMenuSelect( HiWord( menuresult 
) , LoWord( menuresult 
) ); 
1654                 s_lastMouseDown 
= 0; 
1658             SystemClick( ev 
, window 
) ; 
1659             s_lastMouseDown 
= 0; 
1662             if ( window 
!= frontWindow 
&& s_macIsInModalLoop 
&& !(ev
->modifiers 
& cmdKey 
) ) 
1668                 DragWindow(window
, ev
->where
, &screenBits
.bounds
); 
1673                     Point pt 
= { 0, 0 } ; 
1674                     SetPortWindowPort(window
) ; 
1675                     LocalToGlobal( &pt 
) ; 
1677                         win
->SetSize( pt
.h 
, pt
.v 
, -1 , 
1678                             -1 , wxSIZE_USE_EXISTING
); 
1680                 s_lastMouseDown 
= 0; 
1684             if (TrackGoAway(window
, ev
->where
)) 
1689             s_lastMouseDown 
= 0; 
1693                 Rect newContentRect 
; 
1694                 Rect constraintRect 
; 
1695                 constraintRect
.top 
= win
->GetMinHeight() ; 
1696                 if ( constraintRect
.top 
== -1 ) 
1697                     constraintRect
.top  
= 0 ; 
1698                 constraintRect
.left 
= win
->GetMinWidth() ; 
1699                 if ( constraintRect
.left 
== -1 ) 
1700                     constraintRect
.left  
= 0 ; 
1701                 constraintRect
.right 
= win
->GetMaxWidth() ; 
1702                 if ( constraintRect
.right 
== -1 ) 
1703                     constraintRect
.right  
= 32000 ; 
1704                 constraintRect
.bottom 
= win
->GetMaxHeight() ; 
1705                 if ( constraintRect
.bottom 
== -1 ) 
1706                     constraintRect
.bottom 
= 32000 ; 
1708                 Boolean growResult 
= ResizeWindow( window 
, ev
->where 
, 
1709                     &constraintRect 
, &newContentRect 
) ; 
1712                     win
->SetSize( newContentRect
.left 
, newContentRect
.top 
, 
1713                         newContentRect
.right 
- newContentRect
.left 
, 
1714                         newContentRect
.bottom 
- newContentRect
.top
, wxSIZE_USE_EXISTING
); 
1716                 s_lastMouseDown 
= 0; 
1721                 if (TrackBox(window
, ev
->where
, windowPart
)) 
1723                     // TODO setup size event 
1724                     ZoomWindow( window 
, windowPart 
, false ) ; 
1730                         Point pt 
= { 0, 0 } ; 
1731                         SetPortWindowPort(window
) ; 
1732                         LocalToGlobal( &pt 
) ; 
1735                         GetWindowPortBounds(window
, &tempRect 
) ; 
1736                         win
->SetSize( pt
.h 
, pt
.v 
, tempRect
.right
-tempRect
.left 
, 
1737                             tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
); 
1740             s_lastMouseDown 
= 0; 
1742         case inCollapseBox 
: 
1743                 // TODO setup size event 
1744             s_lastMouseDown 
= 0; 
1751                     SetPortWindowPort(window
) ; 
1754                 if ( window 
!= frontWindow 
&& wxTheApp
->s_captureWindow 
== NULL 
) 
1756                     if ( s_macIsInModalLoop 
) 
1760                     else if ( UMAIsWindowFloating( window 
) ) 
1763                             win
->MacMouseDown( ev 
, windowPart 
) ; 
1768                             win
->MacMouseDown( ev 
, windowPart 
) ; 
1769                         ::SelectWindow( window 
) ; 
1775                         win
->MacMouseDown( ev 
, windowPart 
) ; 
1783 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr 
) 
1785     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1788     short windowPart 
= inNoWindow 
; 
1789     if ( wxTheApp
->s_captureWindow 
) 
1791         window 
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ; 
1792         windowPart 
= inContent 
; 
1796         windowPart 
= ::FindWindow(ev
->where
, &window
) ; 
1807                 wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( window 
) ; 
1809                     win
->MacMouseUp( ev 
, windowPart 
) ; 
1817 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ; 
1818 long wxMacTranslateKey(unsigned char key
, unsigned char code
) 
1823         case kHomeCharCode 
: 
1826         case kEnterCharCode 
: 
1827                  retval 
= WXK_RETURN
; 
1832         case kHelpCharCode 
: 
1835         case kBackspaceCharCode 
: 
1841         case kPageUpCharCode 
: 
1842                  retval 
= WXK_PAGEUP
; 
1844         case kPageDownCharCode 
: 
1845                  retval 
= WXK_PAGEDOWN
; 
1847         case kReturnCharCode 
: 
1848                  retval 
= WXK_RETURN
; 
1850             case kFunctionKeyCharCode 
: 
1902             case kEscapeCharCode 
: 
1903                 retval 
= WXK_ESCAPE 
; 
1905             case kLeftArrowCharCode 
: 
1908             case kRightArrowCharCode 
: 
1909                 retval 
= WXK_RIGHT 
; 
1911             case kUpArrowCharCode 
: 
1914             case kDownArrowCharCode 
: 
1917             case kDeleteCharCode 
: 
1918                 retval 
= WXK_DELETE 
; 
1927 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr 
) 
1929     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1930     wxToolTip::RemoveToolTips() ; 
1932     UInt32 menuresult 
= UMAMenuEvent(ev
) ; 
1933     if ( HiWord( menuresult 
) ) 
1935         if ( !s_macIsInModalLoop 
) 
1936             MacHandleMenuSelect( HiWord( menuresult 
) , LoWord( menuresult 
) ) ; 
1940          wxWindow
* focus 
= wxWindow::FindFocus() ; 
1942         if ( MacSendKeyDownEvent( focus 
, ev
->message 
, ev
->modifiers 
, ev
->when 
, ev
->where
.h 
, ev
->where
.v 
) == false ) 
1944             // has not been handled -> perform default 
1945             wxControl
* control 
= wxDynamicCast( focus 
, wxControl 
) ; 
1946             if ( control 
&&  control
->GetMacControl() != NULL 
) 
1950                 keychar 
= short(ev
->message 
& charCodeMask
); 
1951                 keycode 
= short(ev
->message 
& keyCodeMask
) >> 8 ; 
1952                 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode 
, keychar 
, ev
->modifiers 
) ; 
1958 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr 
) 
1960     EventRecord
* ev 
= (EventRecord
*) evr 
; 
1961     wxToolTip::RemoveToolTips() ; 
1963     UInt32 menuresult 
= UMAMenuEvent(ev
) ; 
1964     if ( HiWord( menuresult 
) ) 
1969         MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message 
, ev
->modifiers 
, ev
->when 
, ev
->where
.h 
, ev
->where
.v 
) ; 
1975 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus 
, long keymessage 
, long modifiers 
, long when 
, short wherex 
, short wherey 
) 
1982     keychar 
= short(keymessage 
& charCodeMask
); 
1983     keycode 
= short(keymessage 
& keyCodeMask
) >> 8 ; 
1985     if ( modifiers 
& ( controlKey
|shiftKey
|optionKey 
) ) 
1987         // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier 
1988         // and look at the character after 
1990         UInt32 keyInfo 
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers 
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
); 
1991         keychar 
= short(keyInfo 
& charCodeMask
); 
1992         keycode 
= short(keyInfo 
& keyCodeMask
) >> 8 ; 
1994     long keyval 
= wxMacTranslateKey(keychar
, keycode
) ; 
1995     long realkeyval 
= keyval 
; 
1996     if ( keyval 
== keychar 
) 
1998         // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first) 
1999         realkeyval 
= short(keymessage 
& charCodeMask
) ; 
2000         keyval 
= wxToupper( keyval 
) ; 
2003     wxKeyEvent 
event(wxEVT_KEY_DOWN
); 
2004     bool handled 
= false ; 
2005     event
.m_shiftDown 
= modifiers 
& shiftKey
; 
2006     event
.m_controlDown 
= modifiers 
& controlKey
; 
2007     event
.m_altDown 
= modifiers 
& optionKey
; 
2008     event
.m_metaDown 
= modifiers 
& cmdKey
; 
2009     event
.m_keyCode 
= keyval 
; 
2013     event
.m_timeStamp 
= when
; 
2014     event
.SetEventObject(focus
); 
2015     handled 
= focus
->GetEventHandler()->ProcessEvent( event 
) ; 
2016     if ( handled 
&& event
.GetSkipped() ) 
2023             wxWindow 
*ancestor 
= focus
; 
2026                 int command 
= ancestor
->GetAcceleratorTable()->GetCommand( event 
); 
2029                     wxCommandEvent 
command_event( wxEVT_COMMAND_MENU_SELECTED
, command 
); 
2030                     handled 
= ancestor
->GetEventHandler()->ProcessEvent( command_event 
); 
2033                 if (ancestor
->IsTopLevel()) 
2035                 ancestor 
= ancestor
->GetParent(); 
2038 #endif // wxUSE_ACCEL 
2042         event
.Skip( FALSE 
) ; 
2043         event
.SetEventType( wxEVT_CHAR 
) ; 
2045         event
.m_keyCode 
= realkeyval 
; 
2047         handled 
= focus
->GetEventHandler()->ProcessEvent( event 
) ; 
2048         if ( handled 
&& event
.GetSkipped() ) 
2052          (keyval 
== WXK_TAB
) && 
2053 // CS: copied the change below from wxGTK 
2054 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may 
2055 //     have this style, yet choose not to process this particular TAB in which 
2056 //     case TAB must still work as a navigational character 
2058          (!focus
->HasFlag(wxTE_PROCESS_TAB
)) && 
2060          (focus
->GetParent()) && 
2061          (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) ) 
2063         wxNavigationKeyEvent new_event
; 
2064         new_event
.SetEventObject( focus 
); 
2065         new_event
.SetDirection( !event
.ShiftDown() ); 
2066         /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ 
2067         new_event
.SetWindowChange( event
.ControlDown() ); 
2068         new_event
.SetCurrentFocus( focus 
); 
2069         handled 
= focus
->GetEventHandler()->ProcessEvent( new_event 
); 
2070         if ( handled 
&& new_event
.GetSkipped() ) 
2073     // backdoor handler for default return and command escape 
2074     if ( !handled 
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) ) 
2076           // if window is not having a focus still testing for default enter or cancel 
2077           // TODO add the UMA version for ActiveNonFloatingWindow 
2078           wxWindow
* focus 
= wxFindWinFromMacWindow( FrontWindow() ) ; 
2081             if ( keyval 
== WXK_RETURN 
) 
2083                  wxButton 
*def 
= wxDynamicCast(focus
->GetDefaultItem(), 
2085                  if ( def 
&& def
->IsEnabled() ) 
2087                      wxCommandEvent 
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() ); 
2088                      event
.SetEventObject(def
); 
2089                      def
->Command(event
); 
2093             /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ 
2094             else if (keyval 
== WXK_ESCAPE 
|| (keyval 
== '.' && modifiers 
& cmdKey 
) ) 
2096                   wxCommandEvent 
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
); 
2097                   new_event
.SetEventObject( focus 
); 
2098                   handled 
= focus
->GetEventHandler()->ProcessEvent( new_event 
); 
2105 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus 
, long keymessage 
, long modifiers 
, long when 
, short wherex 
, short wherey 
) 
2112     keychar 
= short(keymessage 
& charCodeMask
); 
2113     keycode 
= short(keymessage 
& keyCodeMask
) >> 8 ; 
2114     if ( modifiers 
& ( controlKey
|shiftKey
|optionKey 
) ) 
2116         // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier 
2117         // and look at the character after 
2119         UInt32 keyInfo 
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers 
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
); 
2120         keychar 
= short(keyInfo 
& charCodeMask
); 
2121         keycode 
= short(keyInfo 
& keyCodeMask
) >> 8 ; 
2123     long keyval 
= wxMacTranslateKey(keychar
, keycode
) ; 
2125     if ( keyval 
== keychar 
) 
2127         keyval 
= wxToupper( keyval 
) ; 
2129     bool handled 
= false ; 
2131     wxKeyEvent 
event(wxEVT_KEY_UP
); 
2132     event
.m_shiftDown 
= modifiers 
& shiftKey
; 
2133     event
.m_controlDown 
= modifiers 
& controlKey
; 
2134     event
.m_altDown 
= modifiers 
& optionKey
; 
2135     event
.m_metaDown 
= modifiers 
& cmdKey
; 
2136     event
.m_keyCode 
= keyval 
; 
2140     event
.m_timeStamp 
= when
; 
2141     event
.SetEventObject(focus
); 
2142     handled 
= focus
->GetEventHandler()->ProcessEvent( event 
) ; 
2148 void wxApp::MacHandleActivateEvent( WXEVENTREF evr 
) 
2150     EventRecord
* ev 
= (EventRecord
*) evr 
; 
2151     WindowRef window 
= (WindowRef
) ev
->message 
; 
2154         bool activate 
= (ev
->modifiers 
& activeFlag 
) ; 
2155         WindowClass wclass 
; 
2156         ::GetWindowClass ( window 
, &wclass 
) ; 
2157         if ( wclass 
== kFloatingWindowClass 
) 
2159             // if it is a floater we activate/deactivate the front non-floating window instead 
2160             window 
= ::FrontNonFloatingWindow() ; 
2162         wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( window 
) ; 
2164             win
->MacActivate( ev
->when 
, activate 
) ; 
2168 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr 
) 
2170     EventRecord
* ev 
= (EventRecord
*) evr 
; 
2171     WindowRef window 
= (WindowRef
) ev
->message 
; 
2172     wxTopLevelWindowMac 
* win 
= wxFindWinFromMacWindow( window 
) ; 
2175         if ( !wxPendingDelete
.Member(win
) ) 
2176             win
->MacUpdate( ev
->when 
) ; 
2180         // since there is no way of telling this foreign window to update itself 
2181         // we have to invalidate the update region otherwise we keep getting the same 
2182         // event over and over again 
2183         BeginUpdate( window 
) ; 
2184         EndUpdate( window 
) ; 
2188 void wxApp::MacHandleDiskEvent( WXEVENTREF evr 
) 
2190     EventRecord
* ev 
= (EventRecord
*) evr 
; 
2191     if ( HiWord( ev
->message 
) != noErr 
) 
2195          SetPt( &point 
, 100 , 100 ) ; 
2197           err 
= DIBadMount( point 
, ev
->message 
) ; 
2198         wxASSERT( err 
== noErr 
) ; 
2202 void wxApp::MacHandleOSEvent( WXEVENTREF evr 
) 
2204     EventRecord
* ev 
= (EventRecord
*) evr 
; 
2205     switch( ( ev
->message 
& osEvtMessageMask 
) >> 24 ) 
2207         case suspendResumeMessage 
: 
2209                 bool isResuming 
= ev
->message 
& resumeFlag 
; 
2210                 bool convertClipboard 
= ev
->message 
& convertClipboardFlag 
; 
2212                 bool doesActivate 
= UMAGetProcessModeDoesActivateOnFGSwitch() ; 
2215                     WindowRef oldFrontWindow 
= NULL 
; 
2216                     WindowRef newFrontWindow 
= NULL 
; 
2218                     // in case we don't take care of activating ourselves, we have to synchronize 
2219                     // our idea of the active window with the process manager's - which it already activated 
2221                     if ( !doesActivate 
) 
2222                         oldFrontWindow 
= ::FrontNonFloatingWindow() ; 
2224                     MacResume( convertClipboard 
) ; 
2226                     newFrontWindow 
= ::FrontNonFloatingWindow() ; 
2228                     if ( oldFrontWindow 
) 
2230                         wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( oldFrontWindow 
) ; 
2232                             win
->MacActivate( ev
->when 
, false ) ; 
2234                     if ( newFrontWindow 
) 
2236                         wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( newFrontWindow 
) ; 
2238                             win
->MacActivate( ev
->when 
, true ) ; 
2243                     MacSuspend( convertClipboard 
) ; 
2247         case mouseMovedMessage 
: 
2251                 wxWindow
* currentMouseWindow 
= NULL 
; 
2253                 if (s_captureWindow 
) 
2255                     currentMouseWindow 
= s_captureWindow 
; 
2259                     wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h 
, ev
->where
.v 
) , 
2260                                                              ¤tMouseWindow 
) ; 
2263                 if ( currentMouseWindow 
!= wxWindow::s_lastMouseWindow 
) 
2265                     wxMouseEvent event 
; 
2267                     bool isDown 
= !(ev
->modifiers 
& btnState
) ; // 1 is for up 
2268                     bool controlDown 
= ev
->modifiers 
& controlKey 
; // for simulating right mouse 
2270                     event
.m_leftDown 
= isDown 
&& !controlDown
; 
2271                     event
.m_middleDown 
= FALSE
; 
2272                     event
.m_rightDown 
= isDown 
&& controlDown
; 
2273                     event
.m_shiftDown 
= ev
->modifiers 
& shiftKey
; 
2274                     event
.m_controlDown 
= ev
->modifiers 
& controlKey
; 
2275                     event
.m_altDown 
= ev
->modifiers 
& optionKey
; 
2276                     event
.m_metaDown 
= ev
->modifiers 
& cmdKey
; 
2277                     event
.m_x 
= ev
->where
.h
; 
2278                     event
.m_y 
= ev
->where
.v
; 
2279                     event
.m_timeStamp 
= ev
->when
; 
2280                     event
.SetEventObject(this); 
2282                     if ( wxWindow::s_lastMouseWindow 
) 
2284                         wxMouseEvent 
eventleave(event
); 
2285                         eventleave
.SetEventType( wxEVT_LEAVE_WINDOW 
); 
2286                         wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y 
); 
2287                         eventleave
.SetEventObject( wxWindow::s_lastMouseWindow 
) ; 
2289                         wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
); 
2291                     if ( currentMouseWindow 
) 
2293                         wxMouseEvent 
evententer(event
); 
2294                         evententer
.SetEventType( wxEVT_ENTER_WINDOW 
); 
2295                         currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y 
); 
2296                         evententer
.SetEventObject( currentMouseWindow 
) ; 
2297                         currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
); 
2299                     wxWindow::s_lastMouseWindow 
= currentMouseWindow 
; 
2302                 short windowPart 
= inNoWindow 
; 
2304                 if ( s_captureWindow 
) 
2306                     window 
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ; 
2307                     windowPart 
= inContent 
; 
2311                     windowPart 
= ::FindWindow(ev
->where
, &window
); 
2318                             wxTopLevelWindowMac
* win 
= wxFindWinFromMacWindow( window 
) ; 
2320                                 win
->MacMouseMoved( ev 
, windowPart 
) ; 
2327                                     UMAShowArrowCursor(); 
2337                                 UMAShowArrowCursor(); 
2348 void wxApp::MacHandleMenuCommand( wxUint32 id 
) 
2350     wxMenuBar
* mbar 
= wxMenuBar::MacGetInstalledMenuBar() ; 
2351     wxFrame
* frame 
= mbar
->GetFrame(); 
2352     wxCHECK_RET( mbar 
!= NULL 
&& frame 
!= NULL
, wxT("error in menu item callback") ); 
2355         frame
->ProcessCommand(id
); 
2360 void wxApp::MacHandleMenuSelect( int macMenuId 
, int macMenuItemNum 
) 
2363          return; // no menu item selected 
2365     if (macMenuId 
== kwxMacAppleMenuId 
&& macMenuItemNum 
> 1) 
2368         Str255        deskAccessoryName 
; 
2371         GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
); 
2372         GetPort(&savedPort
); 
2373         OpenDeskAcc(deskAccessoryName
); 
2380         GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum 
, &id 
) ; 
2381         MacHandleMenuCommand( id 
) ;