1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
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"
41 #if wxUSE_WX_RESOURCES
42 # include "wx/resource.h"
57 #include "wx/mac/uma.h"
58 #include "wx/mac/macnotfy.h"
61 # include <CoreServices/CoreServices.h>
62 # if defined(WXMAKINGDLL)
63 # include <mach-o/dyld.h>
68 # include <ToolUtils.h>
69 # include <DiskInit.h>
73 extern char *wxBuffer
;
74 extern wxList wxPendingDelete
;
75 extern wxList
*wxWinMacWindowList
;
76 extern wxList
*wxWinMacControlList
;
78 static bool s_inYield
= FALSE
;
80 wxApp
*wxTheApp
= NULL
;
82 #if !USE_SHARED_LIBRARY
83 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
84 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
85 EVT_IDLE(wxApp::OnIdle
)
86 EVT_END_SESSION(wxApp::OnEndSession
)
87 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
92 const short kMacMinHeap
= (29 * 1024) ;
93 // platform specific static variables
95 const short kwxMacMenuBarResource
= 1 ;
96 const short kwxMacAppleMenuId
= 1 ;
98 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
99 wxWindow
* wxApp::s_captureWindow
= NULL
;
100 int wxApp::s_lastMouseDown
= 0 ;
101 long wxApp::sm_lastMessageTime
= 0;
102 long wxApp::s_lastModifiers
= 0 ;
105 bool wxApp::s_macDefaultEncodingIsPC
= true ;
106 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
107 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
108 long wxApp::s_macPreferencesMenuItemId
= 0 ;
109 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
110 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
112 //----------------------------------------------------------------------
113 // Core Apple Event Support
114 //----------------------------------------------------------------------
116 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
117 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
118 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
119 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
123 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
126 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
128 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
131 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
133 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
136 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
138 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
141 // AEODoc Calls MacOpenFile on each of the files passed
143 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
147 DescType returnedType
;
153 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
157 err
= AECountItems(&docList
, &itemsInList
);
161 ProcessSerialNumber PSN
;
162 PSN
.highLongOfPSN
= 0 ;
163 PSN
.lowLongOfPSN
= kCurrentProcess
;
164 SetFrontProcess( &PSN
) ;
166 for (i
= 1; i
<= itemsInList
; i
++) {
167 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
168 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
169 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
175 // AEPDoc Calls MacPrintFile on each of the files passed
177 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
181 DescType returnedType
;
187 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
191 err
= AECountItems(&docList
, &itemsInList
);
195 ProcessSerialNumber PSN
;
196 PSN
.highLongOfPSN
= 0 ;
197 PSN
.lowLongOfPSN
= kCurrentProcess
;
198 SetFrontProcess( &PSN
) ;
200 for (i
= 1; i
<= itemsInList
; i
++) {
201 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
202 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
203 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
209 // AEOApp calls MacNewFile
211 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
217 // AEQuit attempts to quite the application
219 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
221 wxWindow
* win
= GetTopWindow() ;
233 //----------------------------------------------------------------------
234 // Support Routines linking the Mac...File Calls to the Document Manager
235 //----------------------------------------------------------------------
237 void wxApp::MacOpenFile(const wxString
& fileName
)
239 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
241 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
244 void wxApp::MacPrintFile(const wxString
& fileName
)
246 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
249 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
252 wxView
* view
= doc
->GetFirstView() ;
255 wxPrintout
*printout
= view
->OnCreatePrintout();
259 printer
.Print(view
->GetFrame(), printout
, TRUE
);
265 doc
->DeleteAllViews();
266 dm
->RemoveDocument(doc
) ;
272 void wxApp::MacNewFile()
276 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
277 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
278 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
279 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
280 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
281 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
283 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
284 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
285 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
286 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
287 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
288 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
290 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
295 for( int i
= 0 ; i
< len
; ++ i
)
297 c
= strchr( StringANSI
, *from
) ;
300 *to
= StringMac
[ c
- StringANSI
] ;
308 for( int i
= 0 ; i
< len
; ++ i
)
310 c
= strchr( StringANSI
, *from
) ;
313 *to
= StringMac
[ c
- StringANSI
] ;
325 void wxMacConvertToPC( const char *from
, char *to
, int len
)
330 for( int i
= 0 ; i
< len
; ++ i
)
332 c
= strchr( StringMac
, *from
) ;
335 *to
= StringANSI
[ c
- StringMac
] ;
343 for( int i
= 0 ; i
< len
; ++ i
)
345 c
= strchr( StringMac
, *from
) ;
348 *to
= StringANSI
[ c
- StringMac
] ;
360 void wxMacConvertFromPC( char * p
)
363 int len
= strlen ( p
) ;
365 wxMacConvertFromPC( ptr
, ptr
, len
) ;
368 void wxMacConvertFromPCForControls( char * p
)
371 int len
= strlen ( p
) ;
373 wxMacConvertFromPC( ptr
, ptr
, len
) ;
374 for ( unsigned int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
376 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
378 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
383 void wxMacConvertFromPC( unsigned char *p
)
385 char *ptr
= (char*) p
+ 1 ;
388 wxMacConvertFromPC( ptr
, ptr
, len
) ;
391 extern char *wxBuffer
;
393 wxString
wxMacMakeMacStringFromPC( const char * p
)
395 const char *ptr
= p
;
396 int len
= strlen ( p
) ;
397 char *buf
= wxBuffer
;
399 if ( len
>= BUFSIZ
+ 512 )
401 buf
= new char [len
+1] ;
404 wxMacConvertFromPC( ptr
, buf
, len
) ;
406 wxString
result( buf
) ;
407 if ( buf
!= wxBuffer
)
413 void wxMacConvertToPC( char * p
)
416 int len
= strlen ( p
) ;
418 wxMacConvertToPC( ptr
, ptr
, len
) ;
421 void wxMacConvertToPC( unsigned char *p
)
423 char *ptr
= (char*) p
+ 1 ;
426 wxMacConvertToPC( ptr
, ptr
, len
) ;
429 wxString
wxMacMakePCStringFromMac( const char * p
)
431 const char *ptr
= p
;
432 int len
= strlen ( p
) ;
433 char *buf
= wxBuffer
;
435 if ( len
>= BUFSIZ
+ 512 )
437 buf
= new char [len
+1] ;
440 wxMacConvertToPC( ptr
, buf
, len
) ;
443 wxString
result( buf
) ;
444 if ( buf
!= wxBuffer
)
449 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
453 return wxMacMakePCStringFromMac( from
) ;
457 return wxString( from
) ;
461 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
463 // this is safe since a pascal string can never be larger than 256 bytes
465 CopyPascalStringToC( from
, s
) ;
468 return wxMacMakePCStringFromMac( s
) ;
472 return wxString( s
) ;
476 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
480 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
484 CopyCStringToPascal( from
, to
) ;
488 //----------------------------------------------------------------------
489 // Carbon Event Handler
490 //----------------------------------------------------------------------
494 static const EventTypeSpec eventList
[] =
496 { kEventClassCommand
, kEventProcessCommand
} ,
497 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
498 { kEventClassApplication
, kEventAppActivated
} ,
499 { kEventClassApplication
, kEventAppDeactivated
} ,
502 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
504 OSStatus result
= eventNotHandledErr
;
508 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
509 sizeof( HICommand
), NULL
, &command
);
511 MenuCommand id
= command
.commandID
;
512 if ( id
== kHICommandPreferences
)
513 id
= wxApp::s_macPreferencesMenuItemId
;
515 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
516 wxMenu
* menu
= NULL
;
517 wxMenuItem
* item
= NULL
;
520 item
= mbar
->FindItem( id
, &menu
) ;
522 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
525 switch( GetEventKind( event
) )
527 case kEventProcessCommand
:
529 if (item
->IsCheckable())
531 item
->Check( !item
->IsChecked() ) ;
534 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
538 case kEventCommandUpdateStatus
:
539 // eventually trigger an updateui round
549 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
551 OSStatus result
= eventNotHandledErr
;
552 switch ( GetEventKind( event
) )
554 case kEventAppActivated
:
556 wxTheApp
->MacResume( true ) ;
560 case kEventAppDeactivated
:
562 wxTheApp
->MacSuspend( true ) ;
572 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
574 OSStatus result
= eventNotHandledErr
;
575 switch( GetEventClass( event
) )
577 case kEventClassCommand
:
578 result
= CommandEventHandler( handler
, event
, data
) ;
580 case kEventClassApplication
:
581 result
= ApplicationEventHandler( handler
, event
, data
) ;
591 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
595 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
596 // we know it's there ;-)
597 WXIMPORT
char std::__throws_bad_alloc
;
600 bool wxApp::Initialize()
606 UMAInitToolbox( 4 ) ;
607 SetEventMask( everyEvent
) ;
608 UMAShowWatchCursor() ;
610 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
611 // open shared library resources from here since we don't have
612 // __wxinitialize in Mach-O shared libraries
613 wxStAppResource::OpenSharedLibraryResource(NULL
);
617 // test the minimal configuration necessary
623 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
625 error
= kMacSTRWrongMachine
;
627 else if (theMachine
< gestaltMacPlus
)
629 error
= kMacSTRWrongMachine
;
631 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
633 error
= kMacSTROldSystem
;
635 else if ( theSystem
< 0x0860 )
637 error
= kMacSTROldSystem
;
639 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
641 error
= kMacSTRSmallSize
;
647 if ( !UMAHasAppearance() )
649 error = kMacSTRNoPre8Yet ;
655 // if we encountered any problems so far, give the error code and exit immediately
659 wxStAppResource resload
;
663 GetIndString(message
, 128, error
);
664 UMAShowArrowCursor() ;
665 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
666 itemHit
= Alert(128, nil
);
671 # if __option(profile)
672 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
677 // now avoid exceptions thrown for new (bad_alloc)
678 // FIXME CS for some changes outside wxMac does not compile anymore
680 std::__throws_bad_alloc
= 0 ;
685 s_macCursorRgn
= ::NewRgn() ;
687 wxBuffer
= new char[BUFSIZ
+ 512];
689 wxClassInfo::InitializeClasses();
692 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
696 wxPendingEventsLocker
= new wxCriticalSection
;
699 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
700 wxTheColourDatabase
->Initialize();
704 // flush the logged messages if any and install a 'safer' log target: the
705 // default one (wxLogGui) can't be used after the resources are freed just
706 // below and the user suppliedo ne might be even more unsafe (using any
707 // wxWindows GUI function is unsafe starting from now)
708 wxLog::DontCreateOnDemand();
710 // this will flush the old messages if any
711 delete wxLog::SetActiveTarget(new wxLogStderr
);
715 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
716 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
718 wxInitializeStockLists();
719 wxInitializeStockObjects();
721 #if wxUSE_WX_RESOURCES
722 wxInitializeResourceSystem();
725 wxBitmap::InitStandardHandlers();
727 wxModule::RegisterModules();
728 if (!wxModule::InitializeModules()) {
732 wxMacCreateNotifierTable() ;
734 UMAShowArrowCursor() ;
739 bool wxApp::OnInitGui()
741 if( !wxAppBase::OnInitGui() )
745 InstallApplicationEventHandler(
746 GetwxAppEventHandlerUPP(),
747 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
750 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
751 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
752 NewAEEventHandlerUPP(AEHandleODoc
) ,
754 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
755 NewAEEventHandlerUPP(AEHandleOApp
) ,
757 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
758 NewAEEventHandlerUPP(AEHandlePDoc
) ,
760 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
761 NewAEEventHandlerUPP(AEHandleQuit
) ,
764 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
765 NewAEEventHandlerProc(AEHandleODoc
) ,
767 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
768 NewAEEventHandlerProc(AEHandleOApp
) ,
770 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
771 NewAEEventHandlerProc(AEHandlePDoc
) ,
773 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
774 NewAEEventHandlerProc(AEHandleQuit
) ,
781 void wxApp::CleanUp()
783 wxToolTip::RemoveToolTips() ;
785 // flush the logged messages if any and install a 'safer' log target: the
786 // default one (wxLogGui) can't be used after the resources are freed just
787 // below and the user suppliedo ne might be even more unsafe (using any
788 // wxWindows GUI function is unsafe starting from now)
789 wxLog::DontCreateOnDemand();
791 // this will flush the old messages if any
792 delete wxLog::SetActiveTarget(new wxLogStderr
);
795 // One last chance for pending objects to be cleaned up
796 wxTheApp
->DeletePendingObjects();
798 wxModule::CleanUpModules();
800 #if wxUSE_WX_RESOURCES
801 wxCleanUpResourceSystem();
804 wxDeleteStockObjects() ;
806 // Destroy all GDI lists, etc.
807 wxDeleteStockLists();
809 delete wxTheColourDatabase
;
810 wxTheColourDatabase
= NULL
;
812 wxBitmap::CleanUpHandlers();
817 wxMacDestroyNotifierTable() ;
818 if (wxWinMacWindowList
) {
819 delete wxWinMacWindowList
;
821 if (wxWinMacControlList
) {
822 delete wxWinMacControlList
;
824 delete wxPendingEvents
;
827 delete wxPendingEventsLocker
;
828 // If we don't do the following, we get an apparent memory leak.
829 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
832 wxClassInfo::CleanUpClasses();
835 # if __option(profile)
836 ProfilerDump( "\papp.prof" ) ;
844 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
845 // At this point we want to check if there are any memory
846 // blocks that aren't part of the wxDebugContext itself,
847 // as a special case. Then when dumping we need to ignore
848 // wxDebugContext, too.
849 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
851 wxLogDebug(wxT("There were memory leaks."));
852 wxDebugContext::Dump();
853 wxDebugContext::PrintStatistics();
855 // wxDebugContext::SetStream(NULL, NULL);
859 // do it as the very last thing because everything else can log messages
860 delete wxLog::SetActiveTarget(NULL
);
863 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
864 // close shared library resources from here since we don't have
865 // __wxterminate in Mach-O shared libraries
866 wxStAppResource::CloseSharedLibraryResource();
869 UMACleanupToolbox() ;
870 if (s_macCursorRgn
) {
871 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
879 //----------------------------------------------------------------------
881 //----------------------------------------------------------------------
883 // extern variable for shared library resource id
884 // need to be able to find it with NSLookupAndBindSymbol
885 short gSharedLibraryResource
= kResFileNotOpened
;
887 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
888 CFBundleRef gSharedLibraryBundle
= NULL
;
889 #endif /* WXMAKINGDLL && __DARWIN__ */
891 wxStAppResource::wxStAppResource()
893 m_currentRefNum
= CurResFile() ;
894 if ( gSharedLibraryResource
!= kResFileNotOpened
)
896 UseResFile( gSharedLibraryResource
) ;
900 wxStAppResource::~wxStAppResource()
902 if ( m_currentRefNum
!= kResFileNotOpened
)
904 UseResFile( m_currentRefNum
) ;
908 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
910 gSharedLibraryResource
= kResFileNotOpened
;
913 if ( initBlock
!= NULL
) {
914 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
915 FSSpec
*fileSpec
= NULL
;
917 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
918 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
920 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
921 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
924 if (fileSpec
!= NULL
) {
925 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
930 // Open the shared library resource file if it is not yet open
933 const char *theLibPath
;
935 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
936 if (gSharedLibraryBundle
!= NULL
) {
937 // wxWindows has been bundled into a framework
938 // load the framework resources
940 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
943 // wxWindows is a simple dynamic shared library
944 // load the resources from the data fork of a separate resource file
949 OSErr theErr
= noErr
;
951 // get the library path
952 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
953 theModule
= NSModuleForSymbol(theSymbol
);
954 theLibPath
= NSLibraryNameForModule(theModule
);
956 wxLogDebug( wxT("wxMac library installation name is '%s'"),
959 // allocate copy to replace .dylib.* extension with .rsrc
960 theResPath
= strdup(theLibPath
);
961 if (theResPath
!= NULL
) {
962 theName
= strrchr(theResPath
, '/');
963 if (theName
== NULL
) {
964 // no directory elements in path
965 theName
= theResPath
;
967 // find ".dylib" shared library extension
968 theExt
= strstr(theName
, ".dylib");
969 // overwrite extension with ".rsrc"
970 strcpy(theExt
, ".rsrc");
972 wxLogDebug( wxT("wxMac resources file name is '%s'"),
975 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
976 if (theErr
!= noErr
) {
977 // try in current directory (using name only)
978 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
981 // open the resource file
982 if (theErr
== noErr
) {
983 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
984 &gSharedLibraryResource
);
986 if (theErr
!= noErr
) {
987 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
991 // free duplicated resource file path
995 #endif /* __DARWIN__ */
997 #endif /* WXMAKINGDLL */
1000 void wxStAppResource::CloseSharedLibraryResource()
1003 // Close the shared library resource file
1004 if (gSharedLibraryResource
!= kResFileNotOpened
) {
1006 if (gSharedLibraryBundle
!= NULL
) {
1007 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
1008 gSharedLibraryResource
);
1009 gSharedLibraryBundle
= NULL
;
1012 #endif /* __DARWIN__ */
1014 CloseResFile(gSharedLibraryResource
);
1016 gSharedLibraryResource
= kResFileNotOpened
;
1018 #endif /* WXMAKINGDLL */
1021 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
1023 // for shared libraries we have to manually get the correct resource
1024 // ref num upon initializing and releasing when terminating, therefore
1025 // the __wxinitialize and __wxterminate must be used
1028 void __sinit(void); /* (generated by linker) */
1029 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
1030 pascal void __terminate(void);
1033 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
1035 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
1036 return __initialize( theInitBlock
) ;
1039 pascal void __wxterminate(void)
1041 wxStAppResource::CloseSharedLibraryResource() ;
1045 #endif /* WXMAKINGDLL && !__DARWIN__ */
1047 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
1049 return wxApp::Initialize();
1052 int WXDLLEXPORT
wxEntryInitGui()
1054 return wxTheApp
->OnInitGui();
1057 void WXDLLEXPORT
wxEntryCleanup()
1062 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
1065 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
1066 // This seems to be necessary since there are 'rogue'
1067 // objects present at this point (perhaps global objects?)
1068 // Setting a checkpoint will ignore them as far as the
1069 // memory checking facility is concerned.
1070 // Of course you may argue that memory allocated in globals should be
1071 // checked, but this is a reasonable compromise.
1072 wxDebugContext::SetCheckpoint();
1075 if (!wxEntryStart(argc
, argv
)) {
1078 // create the application object or ensure that one already exists
1081 // The app may have declared a global application object, but we recommend
1082 // the IMPLEMENT_APP macro is used instead, which sets an initializer
1083 // function for delayed, dynamic app object construction.
1084 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
1085 wxT("No initializer - use IMPLEMENT_APP macro.") );
1087 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
1090 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
1093 // Mac OS X passes a process serial number command line argument when
1094 // the application is launched from the Finder. This argument must be
1095 // removed from the command line arguments before being handled by the
1096 // application (otherwise applications would need to handle it)
1099 if (strncmp(argv
[1], "-psn_", 5) == 0) {
1100 // assume the argument is always the only one and remove it
1105 argc
= 0 ; // currently we don't support files as parameters
1107 // we could try to get the open apple events here to adjust argc and argv better
1109 wxTheApp
->argc
= argc
;
1110 wxTheApp
->argv
= argv
;
1112 // GUI-specific initialization, such as creating an app context.
1115 // Here frames insert themselves automatically
1116 // into wxTopLevelWindows by getting created
1121 if ( wxTheApp
->OnInit() )
1125 retValue
= wxTheApp
->OnRun();
1128 // We want to initialize, but not run or exit immediately.
1131 //else: app initialization failed, so we skipped OnRun()
1133 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1136 // Forcibly delete the window.
1137 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
1138 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
1140 topWindow
->Close(TRUE
);
1141 wxTheApp
->DeletePendingObjects();
1146 wxTheApp
->SetTopWindow(NULL
);
1159 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1161 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1162 OSStatus err
= noErr
;
1165 switch( GetEventClass( event
) )
1167 case kEventClassKeyboard
:
1170 switch( GetEventKind(event
) )
1172 case kEventRawKeyDown
:
1173 rec
->what
= keyDown
;
1175 case kEventRawKeyRepeat
:
1176 rec
->what
= autoKey
;
1178 case kEventRawKeyUp
:
1181 case kEventRawKeyModifiersChanged
:
1182 rec
->what
= nullEvent
;
1191 unsigned char charCode
;
1193 GetMouse( &rec
->where
) ;
1195 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1196 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1197 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1198 rec
->modifiers
= modifiers
;
1199 rec
->message
= (keyCode
<< 8 ) + charCode
;
1203 case kEventClassTextInput
:
1205 switch( GetEventKind( event
) )
1207 case kEventTextInputUnicodeForKeyEvent
:
1210 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1214 unsigned char charCode
;
1216 GetMouse( &rec
->where
) ;
1217 rec
->what
= keyDown
;
1218 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1219 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1220 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1221 rec
->modifiers
= modifiers
;
1222 rec
->message
= (keyCode
<< 8 ) + charCode
;
1238 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1240 OSStatus result = eventNotHandledErr ;
1243 switch ( GetEventClass( event ) )
1245 case kEventClassKeyboard :
1246 if ( wxMacConvertEventToRecord( event , &rec ) )
1248 wxTheApp->MacHandleModifierEvents( &rec ) ;
1249 wxTheApp->MacHandleOneEvent( &rec ) ;
1253 case kEventClassTextInput :
1254 if ( wxMacConvertEventToRecord( event , &rec ) )
1256 wxTheApp->MacHandleModifierEvents( &rec ) ;
1257 wxTheApp->MacHandleOneEvent( &rec ) ;
1269 // Static member initialization
1270 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1277 #if WXWIN_COMPATIBILITY_2_2
1278 m_wantDebugOutput
= TRUE
;
1284 m_printMode
= wxPRINT_WINDOWS
;
1288 bool wxApp::Initialized()
1296 int wxApp::MainLoop()
1308 // Returns TRUE if more time is needed.
1309 bool wxApp::ProcessIdle()
1312 event
.SetEventObject(this);
1313 ProcessEvent(event
);
1315 return event
.MoreRequested();
1318 void wxApp::ExitMainLoop()
1320 m_keepGoing
= FALSE
;
1323 // Is a message/event pending?
1324 bool wxApp::Pending()
1328 return EventAvail( everyEvent
, &event
) ;
1331 // Dispatch a message.
1332 void wxApp::Dispatch()
1337 void wxApp::OnIdle(wxIdleEvent
& event
)
1339 static bool s_inOnIdle
= FALSE
;
1341 // Avoid recursion (via ProcessEvent default case)
1348 // 'Garbage' collection of windows deleted with Close().
1349 DeletePendingObjects();
1351 // flush the logged messages if any
1352 wxLog
*pLog
= wxLog::GetActiveTarget();
1353 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1356 // Send OnIdle events to all windows
1357 bool needMore
= SendIdleEvents();
1360 event
.RequestMore(TRUE
);
1362 // If they are pending events, we must process them: pending events are
1363 // either events to the threads other than main or events posted with
1364 // wxPostEvent() functions
1365 wxMacProcessNotifierAndPendingEvents();
1375 // Send idle event to all top-level windows
1376 bool wxApp::SendIdleEvents()
1378 bool needMore
= FALSE
;
1379 wxNode
* node
= wxTopLevelWindows
.First();
1382 wxWindow
* win
= (wxWindow
*) node
->Data();
1383 if (SendIdleEvents(win
))
1386 node
= node
->Next();
1391 // Send idle event to window and all subwindows
1392 bool wxApp::SendIdleEvents(wxWindow
* win
)
1394 bool needMore
= FALSE
;
1397 event
.SetEventObject(win
);
1398 win
->ProcessEvent(event
);
1400 if (event
.MoreRequested())
1403 wxNode
* node
= win
->GetChildren().First();
1406 wxWindow
* win
= (wxWindow
*) node
->Data();
1407 if (SendIdleEvents(win
))
1410 node
= node
->Next();
1415 void wxApp::DeletePendingObjects()
1417 wxNode
*node
= wxPendingDelete
.First();
1420 wxObject
*obj
= (wxObject
*)node
->Data();
1424 if (wxPendingDelete
.Member(obj
))
1427 // Deleting one object may have deleted other pending
1428 // objects, so start from beginning of list again.
1429 node
= wxPendingDelete
.First();
1435 wxLogError(_("Fatal error: exiting"));
1441 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1444 GetTopWindow()->Close(TRUE
);
1447 // Default behaviour: close the application with prompts. The
1448 // user can veto the close, and therefore the end session.
1449 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1453 if (!GetTopWindow()->Close(!event
.CanVeto()))
1458 extern "C" void wxCYield() ;
1464 // Yield to other processes
1466 bool wxApp::Yield(bool onlyIfNeeded
)
1470 if ( !onlyIfNeeded
)
1472 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1481 YieldToAnyThread() ;
1485 long sleepTime
= 1 ; //::GetCaretTime();
1487 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1489 wxTheApp
->MacHandleModifierEvents( &event
) ;
1490 wxTheApp
->MacHandleOneEvent( &event
);
1491 if ( event
.what
!= kHighLevelEvent
)
1492 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1494 wxTheApp
->MacHandleModifierEvents( &event
) ;
1496 wxMacProcessNotifierAndPendingEvents() ;
1503 // platform specifics
1505 void wxApp::MacSuspend( bool convertClipboard
)
1508 // we have to deactive the top level windows manually
1510 wxNode
* node
= wxTopLevelWindows
.First();
1513 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1514 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1516 node
= node
->Next();
1519 ::HideFloatingWindows() ;
1521 s_lastMouseDown
= 0 ;
1523 if( convertClipboard
)
1525 MacConvertPrivateToPublicScrap() ;
1529 extern wxList wxModalDialogs
;
1531 void wxApp::MacResume( bool convertClipboard
)
1533 s_lastMouseDown
= 0 ;
1534 if( convertClipboard
)
1536 MacConvertPublicToPrivateScrap() ;
1540 ::ShowFloatingWindows() ;
1541 // raise modal dialogs in case a non modal window was selected to activate the app
1543 wxNode
* node
= wxModalDialogs
.First();
1546 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1549 node
= node
->Next();
1554 void wxApp::MacConvertPrivateToPublicScrap()
1558 void wxApp::MacConvertPublicToPrivateScrap()
1562 void wxApp::MacDoOneEvent()
1566 long sleepTime
= 1; // GetCaretTime() / 4 ;
1568 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1570 MacHandleModifierEvents( &event
) ;
1571 MacHandleOneEvent( &event
);
1575 MacHandleModifierEvents( &event
) ;
1577 WindowPtr window
= ::FrontWindow() ;
1579 ::IdleControls( window
) ;
1581 wxTheApp
->ProcessIdle() ;
1583 if ( event
.what
!= kHighLevelEvent
)
1584 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1588 DeletePendingObjects() ;
1589 wxMacProcessNotifierAndPendingEvents() ;
1592 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1594 EventRecord
* ev
= (EventRecord
*) evr
;
1596 if ( ev
->what
== mouseDown
|| ev
->what
== mouseUp
|| ev
->what
== activateEvt
||
1597 ev
->what
== keyDown
|| ev
->what
== autoKey
|| ev
->what
== keyUp
|| ev
->what
== kHighLevelEvent
||
1598 ev
->what
== nullEvent
1601 // in these cases the modifiers are already correctly setup by carbon
1606 WaitNextEvent( 0 , &nev
, 0 , NULL
) ;
1607 ev
->modifiers
= nev
.modifiers
;
1608 // KeyModifiers unfortunately don't include btnState...
1609 // ev->modifiers = GetCurrentKeyModifiers() ;
1612 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1614 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1616 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1617 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1618 event
.m_altDown
= ev
->modifiers
& optionKey
;
1619 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1621 event
.m_x
= ev
->where
.h
;
1622 event
.m_y
= ev
->where
.v
;
1623 event
.m_timeStamp
= ev
->when
;
1624 wxWindow
* focus
= wxWindow::FindFocus() ;
1625 event
.SetEventObject(focus
);
1627 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1629 event
.m_keyCode
= WXK_CONTROL
;
1630 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1631 focus
->GetEventHandler()->ProcessEvent( event
) ;
1633 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1635 event
.m_keyCode
= WXK_SHIFT
;
1636 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1637 focus
->GetEventHandler()->ProcessEvent( event
) ;
1639 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1641 event
.m_keyCode
= WXK_ALT
;
1642 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1643 focus
->GetEventHandler()->ProcessEvent( event
) ;
1645 s_lastModifiers
= ev
->modifiers
;
1649 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1651 EventRecord
* ev
= (EventRecord
*) evr
;
1652 m_macCurrentEvent
= ev
;
1654 wxApp::sm_lastMessageTime
= ev
->when
;
1659 MacHandleMouseDownEvent( ev
) ;
1660 if ( ev
->modifiers
& controlKey
)
1661 s_lastMouseDown
= 2;
1663 s_lastMouseDown
= 1;
1667 if ( s_lastMouseDown
== 2 )
1669 ev
->modifiers
|= controlKey
;
1673 ev
->modifiers
&= ~controlKey
;
1675 MacHandleMouseUpEvent( ev
) ;
1676 s_lastMouseDown
= 0;
1679 MacHandleActivateEvent( ev
) ;
1682 MacHandleUpdateEvent( ev
) ;
1687 MacHandleKeyDownEvent( ev
) ;
1690 MacHandleKeyUpEvent( ev
) ;
1694 MacHandleDiskEvent( ev
) ;
1697 MacHandleOSEvent( ev
) ;
1700 case kHighLevelEvent
:
1701 MacHandleHighLevelEvent( ev
) ;
1706 wxMacProcessNotifierAndPendingEvents() ;
1709 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1711 // we must avoid reentrancy problems when processing high level events eg printing
1712 bool former
= s_inYield
;
1714 EventRecord
* ev
= (EventRecord
*) evr
;
1715 ::AEProcessAppleEvent( ev
) ;
1716 s_inYield
= former
;
1719 bool s_macIsInModalLoop
= false ;
1721 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1723 EventRecord
* ev
= (EventRecord
*) evr
;
1724 wxToolTip::RemoveToolTips() ;
1727 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1728 WindowAttributes frontWindowAttributes
= NULL
;
1730 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1732 short windowPart
= ::FindWindow(ev
->where
, &window
);
1733 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1734 if ( wxPendingDelete
.Member(win
) )
1738 GetQDGlobalsScreenBits( &screenBits
);
1743 if ( s_macIsInModalLoop
)
1749 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1751 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1753 s_lastMouseDown
= 0;
1758 SystemClick( ev
, window
) ;
1759 s_lastMouseDown
= 0;
1762 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1768 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1773 Point pt
= { 0, 0 } ;
1774 SetPortWindowPort(window
) ;
1775 LocalToGlobal( &pt
) ;
1777 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1778 -1 , wxSIZE_USE_EXISTING
);
1780 s_lastMouseDown
= 0;
1784 if (TrackGoAway(window
, ev
->where
))
1789 s_lastMouseDown
= 0;
1793 Rect newContentRect
;
1794 Rect constraintRect
;
1795 constraintRect
.top
= win
->GetMinHeight() ;
1796 if ( constraintRect
.top
== -1 )
1797 constraintRect
.top
= 0 ;
1798 constraintRect
.left
= win
->GetMinWidth() ;
1799 if ( constraintRect
.left
== -1 )
1800 constraintRect
.left
= 0 ;
1801 constraintRect
.right
= win
->GetMaxWidth() ;
1802 if ( constraintRect
.right
== -1 )
1803 constraintRect
.right
= 32000 ;
1804 constraintRect
.bottom
= win
->GetMaxHeight() ;
1805 if ( constraintRect
.bottom
== -1 )
1806 constraintRect
.bottom
= 32000 ;
1808 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1809 &constraintRect
, &newContentRect
) ;
1812 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1813 newContentRect
.right
- newContentRect
.left
,
1814 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1816 s_lastMouseDown
= 0;
1821 if (TrackBox(window
, ev
->where
, windowPart
))
1823 // TODO setup size event
1824 ZoomWindow( window
, windowPart
, false ) ;
1830 Point pt
= { 0, 0 } ;
1831 SetPortWindowPort(window
) ;
1832 LocalToGlobal( &pt
) ;
1835 GetWindowPortBounds(window
, &tempRect
) ;
1836 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1837 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1840 s_lastMouseDown
= 0;
1842 case inCollapseBox
:
1843 // TODO setup size event
1844 s_lastMouseDown
= 0;
1851 SetPortWindowPort(window
) ;
1854 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1856 if ( s_macIsInModalLoop
)
1860 else if ( UMAIsWindowFloating( window
) )
1863 win
->MacMouseDown( ev
, windowPart
) ;
1868 win
->MacMouseDown( ev
, windowPart
) ;
1869 ::SelectWindow( window
) ;
1875 win
->MacMouseDown( ev
, windowPart
) ;
1886 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1888 EventRecord
* ev
= (EventRecord
*) evr
;
1891 short windowPart
= inNoWindow
;
1892 if ( wxTheApp
->s_captureWindow
)
1894 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1895 windowPart
= inContent
;
1899 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1910 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1912 win
->MacMouseUp( ev
, windowPart
) ;
1920 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1921 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1926 case kHomeCharCode
:
1929 case kEnterCharCode
:
1930 retval
= WXK_RETURN
;
1935 case kHelpCharCode
:
1938 case kBackspaceCharCode
:
1944 case kPageUpCharCode
:
1945 retval
= WXK_PAGEUP
;
1947 case kPageDownCharCode
:
1948 retval
= WXK_PAGEDOWN
;
1950 case kReturnCharCode
:
1951 retval
= WXK_RETURN
;
1953 case kFunctionKeyCharCode
:
2005 case kEscapeCharCode
:
2006 retval
= WXK_ESCAPE
;
2008 case kLeftArrowCharCode
:
2011 case kRightArrowCharCode
:
2012 retval
= WXK_RIGHT
;
2014 case kUpArrowCharCode
:
2017 case kDownArrowCharCode
:
2020 case kDeleteCharCode
:
2021 retval
= WXK_DELETE
;
2029 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
2031 EventRecord
* ev
= (EventRecord
*) evr
;
2032 wxToolTip::RemoveToolTips() ;
2035 UInt32 menuresult
= UMAMenuEvent(ev
) ;
2036 if ( HiWord( menuresult
) )
2038 if ( !s_macIsInModalLoop
)
2039 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
2044 wxWindow
* focus
= wxWindow::FindFocus() ;
2046 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
2048 // has not been handled -> perform default
2049 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
2050 if ( control
&& control
->GetMacControl() != NULL
)
2054 keychar
= short(ev
->message
& charCodeMask
);
2055 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
2056 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
2062 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2069 keychar
= short(keymessage
& charCodeMask
);
2070 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2072 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2074 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2075 // and look at the character after
2077 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2078 keychar
= short(keyInfo
& charCodeMask
);
2079 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2081 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2082 long realkeyval
= keyval
;
2083 if ( keyval
== keychar
)
2085 // 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)
2086 realkeyval
= short(keymessage
& charCodeMask
) ;
2087 keyval
= wxToupper( keyval
) ;
2090 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2091 bool handled
= false ;
2092 event
.m_shiftDown
= modifiers
& shiftKey
;
2093 event
.m_controlDown
= modifiers
& controlKey
;
2094 event
.m_altDown
= modifiers
& optionKey
;
2095 event
.m_metaDown
= modifiers
& cmdKey
;
2096 event
.m_keyCode
= keyval
;
2100 event
.m_timeStamp
= when
;
2101 event
.SetEventObject(focus
);
2102 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2103 if ( handled
&& event
.GetSkipped() )
2110 wxWindow
*ancestor
= focus
;
2113 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2116 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2117 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2120 if (ancestor
->IsTopLevel())
2122 ancestor
= ancestor
->GetParent();
2125 #endif // wxUSE_ACCEL
2129 event
.Skip( FALSE
) ;
2130 event
.SetEventType( wxEVT_CHAR
) ;
2132 event
.m_keyCode
= realkeyval
;
2134 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2135 if ( handled
&& event
.GetSkipped() )
2139 (keyval
== WXK_TAB
) &&
2140 // CS: copied the change below from wxGTK
2141 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2142 // have this style, yet choose not to process this particular TAB in which
2143 // case TAB must still work as a navigational character
2145 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2147 (focus
->GetParent()) &&
2148 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2150 wxNavigationKeyEvent new_event
;
2151 new_event
.SetEventObject( focus
);
2152 new_event
.SetDirection( !event
.ShiftDown() );
2153 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2154 new_event
.SetWindowChange( event
.ControlDown() );
2155 new_event
.SetCurrentFocus( focus
);
2156 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2157 if ( handled
&& new_event
.GetSkipped() )
2160 // backdoor handler for default return and command escape
2161 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2163 // if window is not having a focus still testing for default enter or cancel
2164 // TODO add the UMA version for ActiveNonFloatingWindow
2165 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2168 if ( keyval
== WXK_RETURN
)
2170 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2172 if ( def
&& def
->IsEnabled() )
2174 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2175 event
.SetEventObject(def
);
2176 def
->Command(event
);
2180 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2181 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2183 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2184 new_event
.SetEventObject( focus
);
2185 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2193 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
2195 EventRecord
* ev
= (EventRecord
*) evr
;
2196 wxToolTip::RemoveToolTips() ;
2198 UInt32 menuresult
= UMAMenuEvent(ev
) ;
2199 if ( HiWord( menuresult
) )
2204 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
2208 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2215 keychar
= short(keymessage
& charCodeMask
);
2216 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2217 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2219 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2220 // and look at the character after
2222 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2223 keychar
= short(keyInfo
& charCodeMask
);
2224 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2226 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2228 if ( keyval
== keychar
)
2230 keyval
= wxToupper( keyval
) ;
2232 bool handled
= false ;
2234 wxKeyEvent
event(wxEVT_KEY_UP
);
2235 event
.m_shiftDown
= modifiers
& shiftKey
;
2236 event
.m_controlDown
= modifiers
& controlKey
;
2237 event
.m_altDown
= modifiers
& optionKey
;
2238 event
.m_metaDown
= modifiers
& cmdKey
;
2239 event
.m_keyCode
= keyval
;
2243 event
.m_timeStamp
= when
;
2244 event
.SetEventObject(focus
);
2245 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2250 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2252 EventRecord
* ev
= (EventRecord
*) evr
;
2253 WindowRef window
= (WindowRef
) ev
->message
;
2256 bool activate
= (ev
->modifiers
& activeFlag
) ;
2257 WindowClass wclass
;
2258 ::GetWindowClass ( window
, &wclass
) ;
2259 if ( wclass
== kFloatingWindowClass
)
2261 // if it is a floater we activate/deactivate the front non-floating window instead
2262 window
= ::FrontNonFloatingWindow() ;
2264 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2266 win
->MacActivate( ev
->when
, activate
) ;
2270 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2272 EventRecord
* ev
= (EventRecord
*) evr
;
2273 WindowRef window
= (WindowRef
) ev
->message
;
2274 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2277 if ( !wxPendingDelete
.Member(win
) )
2278 win
->MacUpdate( ev
->when
) ;
2282 // since there is no way of telling this foreign window to update itself
2283 // we have to invalidate the update region otherwise we keep getting the same
2284 // event over and over again
2285 BeginUpdate( window
) ;
2286 EndUpdate( window
) ;
2290 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2292 EventRecord
* ev
= (EventRecord
*) evr
;
2293 if ( HiWord( ev
->message
) != noErr
)
2297 SetPt( &point
, 100 , 100 ) ;
2299 err
= DIBadMount( point
, ev
->message
) ;
2300 wxASSERT( err
== noErr
) ;
2304 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2306 EventRecord
* ev
= (EventRecord
*) evr
;
2307 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2309 case suspendResumeMessage
:
2311 bool isResuming
= ev
->message
& resumeFlag
;
2312 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2314 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2317 WindowRef oldFrontWindow
= NULL
;
2318 WindowRef newFrontWindow
= NULL
;
2320 // in case we don't take care of activating ourselves, we have to synchronize
2321 // our idea of the active window with the process manager's - which it already activated
2323 if ( !doesActivate
)
2324 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2326 MacResume( convertClipboard
) ;
2328 newFrontWindow
= ::FrontNonFloatingWindow() ;
2330 if ( oldFrontWindow
)
2332 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2334 win
->MacActivate( ev
->when
, false ) ;
2336 if ( newFrontWindow
)
2338 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2340 win
->MacActivate( ev
->when
, true ) ;
2345 MacSuspend( convertClipboard
) ;
2349 case mouseMovedMessage
:
2353 wxWindow
* currentMouseWindow
= NULL
;
2355 if (s_captureWindow
)
2357 currentMouseWindow
= s_captureWindow
;
2361 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2362 ¤tMouseWindow
) ;
2365 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2367 wxMouseEvent event
;
2369 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2370 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2372 event
.m_leftDown
= isDown
&& !controlDown
;
2373 event
.m_middleDown
= FALSE
;
2374 event
.m_rightDown
= isDown
&& controlDown
;
2375 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2376 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2377 event
.m_altDown
= ev
->modifiers
& optionKey
;
2378 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2379 event
.m_x
= ev
->where
.h
;
2380 event
.m_y
= ev
->where
.v
;
2381 event
.m_timeStamp
= ev
->when
;
2382 event
.SetEventObject(this);
2384 if ( wxWindow::s_lastMouseWindow
)
2386 wxMouseEvent
eventleave(event
);
2387 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2388 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2389 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2391 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2393 if ( currentMouseWindow
)
2395 wxMouseEvent
evententer(event
);
2396 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2397 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2398 evententer
.SetEventObject( currentMouseWindow
) ;
2399 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2401 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2404 short windowPart
= inNoWindow
;
2406 if ( s_captureWindow
)
2408 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2409 windowPart
= inContent
;
2413 windowPart
= ::FindWindow(ev
->where
, &window
);
2420 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2422 win
->MacMouseMoved( ev
, windowPart
) ;
2429 UMAShowArrowCursor();
2439 UMAShowArrowCursor();
2450 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2452 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2453 wxMenu
* menu
= NULL
;
2454 wxMenuItem
* item
= NULL
;
2457 item
= mbar
->FindItem( id
, &menu
) ;
2459 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2461 if (item
->IsCheckable())
2463 item
->Check( !item
->IsChecked() ) ;
2466 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2470 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2473 return; // no menu item selected
2475 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2478 Str255 deskAccessoryName
;
2481 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2482 GetPort(&savedPort
);
2483 OpenDeskAcc(deskAccessoryName
);
2490 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2491 MacHandleMenuCommand( id
) ;