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 #if wxUSE_WX_RESOURCES
40 # include "wx/resource.h"
55 #include "wx/mac/uma.h"
56 #include "wx/mac/macnotfy.h"
59 # include <CoreServices/CoreServices.h>
60 # if defined(WXMAKINGDLL)
61 # include <mach-o/dyld.h>
66 # include <ToolUtils.h>
67 # include <DiskInit.h>
71 extern char *wxBuffer
;
72 extern wxList wxPendingDelete
;
73 extern wxList
*wxWinMacWindowList
;
74 extern wxList
*wxWinMacControlList
;
76 wxApp
*wxTheApp
= NULL
;
78 #if !USE_SHARED_LIBRARY
79 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
80 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
81 EVT_IDLE(wxApp::OnIdle
)
82 EVT_END_SESSION(wxApp::OnEndSession
)
83 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
88 const short kMacMinHeap
= (29 * 1024) ;
89 // platform specific static variables
91 const short kwxMacMenuBarResource
= 1 ;
92 const short kwxMacAppleMenuId
= 1 ;
94 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
95 wxWindow
* wxApp::s_captureWindow
= NULL
;
96 int wxApp::s_lastMouseDown
= 0 ;
97 long wxApp::sm_lastMessageTime
= 0;
98 long wxApp::s_lastModifiers
= 0 ;
101 bool wxApp::s_macDefaultEncodingIsPC
= true ;
102 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
103 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
104 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
106 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
107 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
108 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
109 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
112 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
114 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
115 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
118 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
120 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
121 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
124 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
126 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
127 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
130 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
133 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
136 // new virtual public method in wxApp
137 void wxApp::MacOpenFile(const wxString
& WXUNUSED(fileName
) )
141 void wxApp::MacPrintFile(const wxString
& WXUNUSED(fileName
) )
145 void wxApp::MacNewFile()
149 // new implementation, which parses the event and calls
150 // MacOpenFile on each of the files it's passed
151 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
155 DescType returnedType
;
161 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
165 err
= AECountItems(&docList
, &itemsInList
);
169 ProcessSerialNumber PSN
;
170 PSN
.highLongOfPSN
= 0 ;
171 PSN
.lowLongOfPSN
= kCurrentProcess
;
172 SetFrontProcess( &PSN
) ;
174 for (i
= 1; i
<= itemsInList
; i
++) {
175 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
176 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
177 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
183 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
187 DescType returnedType
;
193 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
197 err
= AECountItems(&docList
, &itemsInList
);
201 ProcessSerialNumber PSN
;
202 PSN
.highLongOfPSN
= 0 ;
203 PSN
.lowLongOfPSN
= kCurrentProcess
;
204 SetFrontProcess( &PSN
) ;
206 for (i
= 1; i
<= itemsInList
; i
++) {
207 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
208 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
209 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
215 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
221 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
223 wxWindow
* win
= GetTopWindow() ;
235 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
236 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
237 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
238 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
239 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
240 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
242 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
243 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
244 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
245 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
246 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
247 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
249 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
254 for( int i
= 0 ; i
< len
; ++ i
)
256 c
= strchr( StringANSI
, *from
) ;
259 *to
= StringMac
[ c
- StringANSI
] ;
267 for( int i
= 0 ; i
< len
; ++ i
)
269 c
= strchr( StringANSI
, *from
) ;
272 *to
= StringMac
[ c
- StringANSI
] ;
284 void wxMacConvertToPC( const char *from
, char *to
, int len
)
289 for( int i
= 0 ; i
< len
; ++ i
)
291 c
= strchr( StringMac
, *from
) ;
294 *to
= StringANSI
[ c
- StringMac
] ;
302 for( int i
= 0 ; i
< len
; ++ i
)
304 c
= strchr( StringMac
, *from
) ;
307 *to
= StringANSI
[ c
- StringMac
] ;
319 void wxMacConvertFromPC( char * p
)
322 int len
= strlen ( p
) ;
324 wxMacConvertFromPC( ptr
, ptr
, len
) ;
327 void wxMacConvertFromPCForControls( char * p
)
330 int len
= strlen ( p
) ;
332 wxMacConvertFromPC( ptr
, ptr
, len
) ;
333 for ( unsigned int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
335 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
337 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
342 void wxMacConvertFromPC( unsigned char *p
)
344 char *ptr
= (char*) p
+ 1 ;
347 wxMacConvertFromPC( ptr
, ptr
, len
) ;
350 extern char *wxBuffer
;
352 wxString
wxMacMakeMacStringFromPC( const char * p
)
354 const char *ptr
= p
;
355 int len
= strlen ( p
) ;
356 char *buf
= wxBuffer
;
358 if ( len
>= BUFSIZ
+ 512 )
360 buf
= new char [len
+1] ;
363 wxMacConvertFromPC( ptr
, buf
, len
) ;
365 wxString
result( buf
) ;
366 if ( buf
!= wxBuffer
)
372 void wxMacConvertToPC( char * p
)
375 int len
= strlen ( p
) ;
377 wxMacConvertToPC( ptr
, ptr
, len
) ;
380 void wxMacConvertToPC( unsigned char *p
)
382 char *ptr
= (char*) p
+ 1 ;
385 wxMacConvertToPC( ptr
, ptr
, len
) ;
388 wxString
wxMacMakePCStringFromMac( const char * p
)
390 const char *ptr
= p
;
391 int len
= strlen ( p
) ;
392 char *buf
= wxBuffer
;
394 if ( len
>= BUFSIZ
+ 512 )
396 buf
= new char [len
+1] ;
399 wxMacConvertToPC( ptr
, buf
, len
) ;
402 wxString
result( buf
) ;
403 if ( buf
!= wxBuffer
)
408 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
412 return wxMacMakePCStringFromMac( from
) ;
416 return wxString( from
) ;
420 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
422 // this is safe since a pascal string can never be larger than 256 bytes
424 CopyPascalStringToC( from
, s
) ;
427 return wxMacMakePCStringFromMac( s
) ;
431 return wxString( s
) ;
435 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
439 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
443 CopyCStringToPascal( from
, to
) ;
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
);
468 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
469 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
470 NewAEEventHandlerUPP(AEHandleODoc
) ,
471 (long) wxTheApp
, FALSE
) ;
472 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
473 NewAEEventHandlerUPP(AEHandleOApp
) ,
474 (long) wxTheApp
, FALSE
) ;
475 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
476 NewAEEventHandlerUPP(AEHandlePDoc
) ,
477 (long) wxTheApp
, FALSE
) ;
478 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
479 NewAEEventHandlerUPP(AEHandleQuit
) ,
480 (long) wxTheApp
, FALSE
) ;
482 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
483 NewAEEventHandlerProc(AEHandleODoc
) ,
484 (long) wxTheApp
, FALSE
) ;
485 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
486 NewAEEventHandlerProc(AEHandleOApp
) ,
487 (long) wxTheApp
, FALSE
) ;
488 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
489 NewAEEventHandlerProc(AEHandlePDoc
) ,
490 (long) wxTheApp
, FALSE
) ;
491 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
492 NewAEEventHandlerProc(AEHandleQuit
) ,
493 (long) wxTheApp
, FALSE
) ;
497 // test the minimal configuration necessary
503 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
505 error
= kMacSTRWrongMachine
;
507 else if (theMachine
< gestaltMacPlus
)
509 error
= kMacSTRWrongMachine
;
511 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
513 error
= kMacSTROldSystem
;
515 else if ( theSystem
< 0x0860 )
517 error
= kMacSTROldSystem
;
519 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
521 error
= kMacSTRSmallSize
;
527 if ( !UMAHasAppearance() )
529 error = kMacSTRNoPre8Yet ;
535 // if we encountered any problems so far, give the error code and exit immediately
539 wxStAppResource resload
;
543 GetIndString(message
, 128, error
);
544 UMAShowArrowCursor() ;
545 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
546 itemHit
= Alert(128, nil
);
551 # if __option(profile)
552 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
557 // now avoid exceptions thrown for new (bad_alloc)
558 std::__throws_bad_alloc
= FALSE
;
561 s_macCursorRgn
= ::NewRgn() ;
563 wxBuffer
= new char[BUFSIZ
+ 512];
565 wxClassInfo::InitializeClasses();
568 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
572 wxPendingEventsLocker
= new wxCriticalSection
;
575 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
576 wxTheColourDatabase
->Initialize();
580 // flush the logged messages if any and install a 'safer' log target: the
581 // default one (wxLogGui) can't be used after the resources are freed just
582 // below and the user suppliedo ne might be even more unsafe (using any
583 // wxWindows GUI function is unsafe starting from now)
584 wxLog::DontCreateOnDemand();
586 // this will flush the old messages if any
587 delete wxLog::SetActiveTarget(new wxLogStderr
);
591 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
592 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
594 wxInitializeStockLists();
595 wxInitializeStockObjects();
597 #if wxUSE_WX_RESOURCES
598 wxInitializeResourceSystem();
601 wxBitmap::InitStandardHandlers();
603 wxModule::RegisterModules();
604 if (!wxModule::InitializeModules()) {
608 wxMacCreateNotifierTable() ;
611 UMAShowArrowCursor() ;
616 void wxApp::CleanUp()
618 wxToolTip::RemoveToolTips() ;
620 // flush the logged messages if any and install a 'safer' log target: the
621 // default one (wxLogGui) can't be used after the resources are freed just
622 // below and the user suppliedo ne might be even more unsafe (using any
623 // wxWindows GUI function is unsafe starting from now)
624 wxLog::DontCreateOnDemand();
626 // this will flush the old messages if any
627 delete wxLog::SetActiveTarget(new wxLogStderr
);
630 // One last chance for pending objects to be cleaned up
631 wxTheApp
->DeletePendingObjects();
633 wxModule::CleanUpModules();
635 #if wxUSE_WX_RESOURCES
636 wxCleanUpResourceSystem();
639 wxDeleteStockObjects() ;
641 // Destroy all GDI lists, etc.
642 wxDeleteStockLists();
644 delete wxTheColourDatabase
;
645 wxTheColourDatabase
= NULL
;
647 wxBitmap::CleanUpHandlers();
652 wxMacDestroyNotifierTable() ;
653 if (wxWinMacWindowList
) {
654 delete wxWinMacWindowList
;
656 if (wxWinMacControlList
) {
657 delete wxWinMacControlList
;
659 delete wxPendingEvents
;
662 delete wxPendingEventsLocker
;
663 // If we don't do the following, we get an apparent memory leak.
664 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
667 wxClassInfo::CleanUpClasses();
670 # if __option(profile)
671 ProfilerDump( "\papp.prof" ) ;
679 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
680 // At this point we want to check if there are any memory
681 // blocks that aren't part of the wxDebugContext itself,
682 // as a special case. Then when dumping we need to ignore
683 // wxDebugContext, too.
684 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
686 wxLogDebug(wxT("There were memory leaks."));
687 wxDebugContext::Dump();
688 wxDebugContext::PrintStatistics();
690 // wxDebugContext::SetStream(NULL, NULL);
694 // do it as the very last thing because everything else can log messages
695 delete wxLog::SetActiveTarget(NULL
);
698 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
699 // close shared library resources from here since we don't have
700 // __wxterminate in Mach-O shared libraries
701 wxStAppResource::CloseSharedLibraryResource();
704 UMACleanupToolbox() ;
705 if (s_macCursorRgn
) {
706 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
714 //----------------------------------------------------------------------
716 //----------------------------------------------------------------------
718 // extern variable for shared library resource id
719 // need to be able to find it with NSLookupAndBindSymbol
720 short gSharedLibraryResource
= kResFileNotOpened
;
722 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
723 CFBundleRef gSharedLibraryBundle
= NULL
;
724 #endif /* WXMAKINGDLL && __DARWIN__ */
726 wxStAppResource::wxStAppResource()
728 m_currentRefNum
= CurResFile() ;
729 if ( gSharedLibraryResource
!= kResFileNotOpened
)
731 UseResFile( gSharedLibraryResource
) ;
735 wxStAppResource::~wxStAppResource()
737 if ( m_currentRefNum
!= kResFileNotOpened
)
739 UseResFile( m_currentRefNum
) ;
743 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
745 gSharedLibraryResource
= kResFileNotOpened
;
748 if ( initBlock
!= NULL
) {
749 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
750 FSSpec
*fileSpec
= NULL
;
752 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
753 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
755 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
756 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
759 if (fileSpec
!= NULL
) {
760 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
765 // Open the shared library resource file if it is not yet open
768 const char *theLibPath
;
770 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
771 if (gSharedLibraryBundle
!= NULL
) {
772 // wxWindows has been bundled into a framework
773 // load the framework resources
775 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
778 // wxWindows is a simple dynamic shared library
779 // load the resources from the data fork of a separate resource file
784 OSErr theErr
= noErr
;
786 // get the library path
787 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
788 theModule
= NSModuleForSymbol(theSymbol
);
789 theLibPath
= NSLibraryNameForModule(theModule
);
791 // allocate copy to replace .dylib.* extension with .rsrc
792 theResPath
= strdup(theLibPath
);
793 if (theResPath
!= NULL
) {
794 theName
= strrchr(theResPath
, '/');
795 if (theName
== NULL
) {
796 // no directory elements in path
797 theName
= theResPath
;
799 // find ".dylib" shared library extension
800 theExt
= strstr(theName
, ".dylib");
801 // overwrite extension with ".rsrc"
802 strcpy(theExt
, ".rsrc");
804 wxLogDebug( theResPath
);
806 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
807 if (theErr
!= noErr
) {
808 // try in current directory (using name only)
809 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
812 // free duplicated resource file path
815 // open the resource file
816 if (theErr
== noErr
) {
817 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
818 &gSharedLibraryResource
);
822 #endif /* __DARWIN__ */
824 #endif /* WXMAKINGDLL */
827 void wxStAppResource::CloseSharedLibraryResource()
830 // Close the shared library resource file
831 if (gSharedLibraryResource
!= kResFileNotOpened
) {
833 if (gSharedLibraryBundle
!= NULL
) {
834 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
835 gSharedLibraryResource
);
836 gSharedLibraryBundle
= NULL
;
839 #endif /* __DARWIN__ */
841 CloseResFile(gSharedLibraryResource
);
843 gSharedLibraryResource
= kResFileNotOpened
;
845 #endif /* WXMAKINGDLL */
848 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
850 // for shared libraries we have to manually get the correct resource
851 // ref num upon initializing and releasing when terminating, therefore
852 // the __wxinitialize and __wxterminate must be used
855 void __sinit(void); /* (generated by linker) */
856 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
857 pascal void __terminate(void);
860 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
862 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
863 return __initialize( theInitBlock
) ;
866 pascal void __wxterminate(void)
868 wxStAppResource::CloseSharedLibraryResource() ;
872 #endif /* WXMAKINGDLL && !__DARWIN__ */
874 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
876 return wxApp::Initialize();
879 int WXDLLEXPORT
wxEntryInitGui()
881 return wxTheApp
->OnInitGui();
884 void WXDLLEXPORT
wxEntryCleanup()
889 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
892 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
893 // This seems to be necessary since there are 'rogue'
894 // objects present at this point (perhaps global objects?)
895 // Setting a checkpoint will ignore them as far as the
896 // memory checking facility is concerned.
897 // Of course you may argue that memory allocated in globals should be
898 // checked, but this is a reasonable compromise.
899 wxDebugContext::SetCheckpoint();
902 if (!wxEntryStart(argc
, argv
)) {
905 // create the application object or ensure that one already exists
908 // The app may have declared a global application object, but we recommend
909 // the IMPLEMENT_APP macro is used instead, which sets an initializer
910 // function for delayed, dynamic app object construction.
911 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
912 wxT("No initializer - use IMPLEMENT_APP macro.") );
914 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
917 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
920 // Mac OS X passes a process serial number command line argument when
921 // the application is launched from the Finder. This argument must be
922 // removed from the command line arguments before being handled by the
923 // application (otherwise applications would need to handle it)
927 strncpy(theArg
, argv
[1], 5);
929 if (strcmp(theArg
, "-psn_") == 0) {
930 // assume the argument is always the only one and remove it
935 argc
= 0 ; // currently we don't support files as parameters
937 // we could try to get the open apple events here to adjust argc and argv better
939 wxTheApp
->argc
= argc
;
940 wxTheApp
->argv
= argv
;
942 // GUI-specific initialization, such as creating an app context.
945 // Here frames insert themselves automatically
946 // into wxTopLevelWindows by getting created
951 if ( wxTheApp
->OnInit() )
955 retValue
= wxTheApp
->OnRun();
958 // We want to initialize, but not run or exit immediately.
961 //else: app initialization failed, so we skipped OnRun()
963 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
966 // Forcibly delete the window.
967 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
968 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
970 topWindow
->Close(TRUE
);
971 wxTheApp
->DeletePendingObjects();
976 wxTheApp
->SetTopWindow(NULL
);
989 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
991 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
992 OSStatus err
= noErr
;
995 switch( GetEventClass( event
) )
997 case kEventClassKeyboard
:
1000 switch( GetEventKind(event
) )
1002 case kEventRawKeyDown
:
1003 rec
->what
= keyDown
;
1005 case kEventRawKeyRepeat
:
1006 rec
->what
= autoKey
;
1008 case kEventRawKeyUp
:
1011 case kEventRawKeyModifiersChanged
:
1012 rec
->what
= nullEvent
;
1021 unsigned char charCode
;
1023 GetMouse( &rec
->where
) ;
1025 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1026 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1027 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1028 rec
->modifiers
= modifiers
;
1029 rec
->message
= (keyCode
<< 8 ) + charCode
;
1033 case kEventClassTextInput
:
1035 switch( GetEventKind( event
) )
1037 case kEventTextInputUnicodeForKeyEvent
:
1040 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1044 unsigned char charCode
;
1046 GetMouse( &rec
->where
) ;
1047 rec
->what
= keyDown
;
1048 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1049 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1050 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1051 rec
->modifiers
= modifiers
;
1052 rec
->message
= (keyCode
<< 8 ) + charCode
;
1067 pascal OSStatus
wxMacApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1069 OSStatus result
= eventNotHandledErr
;
1072 switch ( GetEventClass( event
) )
1074 case kEventClassKeyboard
:
1075 if ( wxMacConvertEventToRecord( event
, &rec
) )
1077 wxTheApp
->MacHandleModifierEvents( &rec
) ;
1078 wxTheApp
->MacHandleOneEvent( &rec
) ;
1082 case kEventClassTextInput
:
1083 if ( wxMacConvertEventToRecord( event
, &rec
) )
1085 wxTheApp
->MacHandleModifierEvents( &rec
) ;
1086 wxTheApp
->MacHandleOneEvent( &rec
) ;
1098 bool wxApp::OnInit()
1100 if ( ! wxAppBase::OnInit() )
1103 #if 0 // TARGET_CARBON
1104 static const EventTypeSpec eventList
[] =
1106 { kEventClassKeyboard
, kEventRawKeyDown
} ,
1107 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
1108 { kEventClassKeyboard
, kEventRawKeyUp
} ,
1109 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
1111 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
1114 InstallApplicationEventHandler(NewEventHandlerUPP(wxMacApplicationEventHandler
)
1115 , WXSIZEOF(eventList
), eventList
, this, NULL
);
1119 // Static member initialization
1120 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1127 #if WXWIN_COMPATIBILITY_2_2
1128 m_wantDebugOutput
= TRUE
;
1134 m_printMode
= wxPRINT_WINDOWS
;
1138 bool wxApp::Initialized()
1146 int wxApp::MainLoop()
1158 // Returns TRUE if more time is needed.
1159 bool wxApp::ProcessIdle()
1162 event
.SetEventObject(this);
1163 ProcessEvent(event
);
1165 return event
.MoreRequested();
1168 void wxApp::ExitMainLoop()
1170 m_keepGoing
= FALSE
;
1173 // Is a message/event pending?
1174 bool wxApp::Pending()
1178 return EventAvail( everyEvent
, &event
) ;
1181 // Dispatch a message.
1182 void wxApp::Dispatch()
1187 void wxApp::OnIdle(wxIdleEvent
& event
)
1189 static bool s_inOnIdle
= FALSE
;
1191 // Avoid recursion (via ProcessEvent default case)
1198 // 'Garbage' collection of windows deleted with Close().
1199 DeletePendingObjects();
1201 // flush the logged messages if any
1202 wxLog
*pLog
= wxLog::GetActiveTarget();
1203 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1206 // Send OnIdle events to all windows
1207 bool needMore
= SendIdleEvents();
1210 event
.RequestMore(TRUE
);
1212 // If they are pending events, we must process them: pending events are
1213 // either events to the threads other than main or events posted with
1214 // wxPostEvent() functions
1215 wxMacProcessNotifierAndPendingEvents();
1225 // Send idle event to all top-level windows
1226 bool wxApp::SendIdleEvents()
1228 bool needMore
= FALSE
;
1229 wxNode
* node
= wxTopLevelWindows
.First();
1232 wxWindow
* win
= (wxWindow
*) node
->Data();
1233 if (SendIdleEvents(win
))
1236 node
= node
->Next();
1241 // Send idle event to window and all subwindows
1242 bool wxApp::SendIdleEvents(wxWindow
* win
)
1244 bool needMore
= FALSE
;
1247 event
.SetEventObject(win
);
1248 win
->ProcessEvent(event
);
1250 if (event
.MoreRequested())
1253 wxNode
* node
= win
->GetChildren().First();
1256 wxWindow
* win
= (wxWindow
*) node
->Data();
1257 if (SendIdleEvents(win
))
1260 node
= node
->Next();
1265 void wxApp::DeletePendingObjects()
1267 wxNode
*node
= wxPendingDelete
.First();
1270 wxObject
*obj
= (wxObject
*)node
->Data();
1274 if (wxPendingDelete
.Member(obj
))
1277 // Deleting one object may have deleted other pending
1278 // objects, so start from beginning of list again.
1279 node
= wxPendingDelete
.First();
1285 wxLogError(_("Fatal error: exiting"));
1291 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1294 GetTopWindow()->Close(TRUE
);
1297 // Default behaviour: close the application with prompts. The
1298 // user can veto the close, and therefore the end session.
1299 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1303 if (!GetTopWindow()->Close(!event
.CanVeto()))
1308 extern "C" void wxCYield() ;
1314 // Yield to other processes
1316 bool wxApp::Yield(bool onlyIfNeeded
)
1318 static bool s_inYield
= FALSE
;
1322 if ( !onlyIfNeeded
)
1324 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1333 YieldToAnyThread() ;
1337 long sleepTime
= 1 ; //::GetCaretTime();
1339 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1341 wxTheApp
->MacHandleModifierEvents( &event
) ;
1342 wxTheApp
->MacHandleOneEvent( &event
);
1343 if ( event
.what
!= kHighLevelEvent
)
1344 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1346 wxTheApp
->MacHandleModifierEvents( &event
) ;
1348 wxMacProcessNotifierAndPendingEvents() ;
1355 // platform specifics
1357 void wxApp::MacSuspend( bool convertClipboard
)
1359 // we have to deactive the top level windows manually
1361 wxNode
* node
= wxTopLevelWindows
.First();
1364 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1365 win
->MacActivate( MacGetCurrentEvent() , false ) ;
1367 node
= node
->Next();
1370 s_lastMouseDown
= 0 ;
1371 if( convertClipboard
)
1373 MacConvertPrivateToPublicScrap() ;
1376 ::HideFloatingWindows() ;
1379 extern wxList wxModalDialogs
;
1381 void wxApp::MacResume( bool convertClipboard
)
1383 s_lastMouseDown
= 0 ;
1384 if( convertClipboard
)
1386 MacConvertPublicToPrivateScrap() ;
1389 ::ShowFloatingWindows() ;
1391 // raise modal dialogs in case a non modal window was selected to activate the app
1393 wxNode
* node
= wxModalDialogs
.First();
1396 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1399 node
= node
->Next();
1403 void wxApp::MacConvertPrivateToPublicScrap()
1407 void wxApp::MacConvertPublicToPrivateScrap()
1411 void wxApp::MacDoOneEvent()
1415 long sleepTime
= 1; // GetCaretTime() / 4 ;
1417 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1419 MacHandleModifierEvents( &event
) ;
1420 MacHandleOneEvent( &event
);
1424 MacHandleModifierEvents( &event
) ;
1426 WindowPtr window
= ::FrontWindow() ;
1428 ::IdleControls( window
) ;
1430 wxTheApp
->ProcessIdle() ;
1432 if ( event
.what
!= kHighLevelEvent
)
1433 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1437 DeletePendingObjects() ;
1438 wxMacProcessNotifierAndPendingEvents() ;
1441 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1443 EventRecord
* ev
= (EventRecord
*) evr
;
1445 if ( ev
->what
== mouseDown
|| ev
->what
== mouseUp
|| ev
->what
== activateEvt
||
1446 ev
->what
== keyDown
|| ev
->what
== autoKey
|| ev
->what
== keyUp
|| ev
->what
== nullEvent
)
1448 // in these cases the modifiers are already correctly setup by carbon
1453 WaitNextEvent( 0 , &nev
, 0 , NULL
) ;
1454 ev
->modifiers
= nev
.modifiers
;
1455 // KeyModifiers unfortunately don't include btnState...
1456 // ev->modifiers = GetCurrentKeyModifiers() ;
1459 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1461 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1463 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1464 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1465 event
.m_altDown
= ev
->modifiers
& optionKey
;
1466 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1468 event
.m_x
= ev
->where
.h
;
1469 event
.m_y
= ev
->where
.v
;
1470 event
.m_timeStamp
= ev
->when
;
1471 wxWindow
* focus
= wxWindow::FindFocus() ;
1472 event
.SetEventObject(focus
);
1474 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1476 event
.m_keyCode
= WXK_CONTROL
;
1477 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1478 focus
->GetEventHandler()->ProcessEvent( event
) ;
1480 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1482 event
.m_keyCode
= WXK_SHIFT
;
1483 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1484 focus
->GetEventHandler()->ProcessEvent( event
) ;
1486 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1488 event
.m_keyCode
= WXK_ALT
;
1489 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1490 focus
->GetEventHandler()->ProcessEvent( event
) ;
1492 s_lastModifiers
= ev
->modifiers
;
1496 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1498 EventRecord
* ev
= (EventRecord
*) evr
;
1499 m_macCurrentEvent
= ev
;
1501 wxApp::sm_lastMessageTime
= ev
->when
;
1506 MacHandleMouseDownEvent( ev
) ;
1507 if ( ev
->modifiers
& controlKey
)
1508 s_lastMouseDown
= 2;
1510 s_lastMouseDown
= 1;
1513 if ( s_lastMouseDown
== 2 )
1515 ev
->modifiers
|= controlKey
;
1519 ev
->modifiers
&= ~controlKey
;
1521 MacHandleMouseUpEvent( ev
) ;
1522 s_lastMouseDown
= 0;
1525 MacHandleActivateEvent( ev
) ;
1528 MacHandleUpdateEvent( ev
) ;
1532 MacHandleKeyDownEvent( ev
) ;
1535 MacHandleKeyUpEvent( ev
) ;
1538 MacHandleDiskEvent( ev
) ;
1541 MacHandleOSEvent( ev
) ;
1543 case kHighLevelEvent
:
1544 MacHandleHighLevelEvent( ev
) ;
1549 wxMacProcessNotifierAndPendingEvents() ;
1552 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1554 EventRecord
* ev
= (EventRecord
*) evr
;
1555 ::AEProcessAppleEvent( ev
) ;
1558 bool s_macIsInModalLoop
= false ;
1560 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1562 EventRecord
* ev
= (EventRecord
*) evr
;
1563 wxToolTip::RemoveToolTips() ;
1566 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1567 WindowAttributes frontWindowAttributes
= NULL
;
1569 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1571 short windowPart
= ::FindWindow(ev
->where
, &window
);
1572 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1573 if ( wxPendingDelete
.Member(win
) )
1577 GetQDGlobalsScreenBits( &screenBits
);
1582 if ( s_macIsInModalLoop
)
1588 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1589 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1590 s_lastMouseDown
= 0;
1595 SystemClick( ev
, window
) ;
1596 s_lastMouseDown
= 0;
1600 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1606 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1611 Point pt
= { 0, 0 } ;
1612 SetPortWindowPort(window
) ;
1613 LocalToGlobal( &pt
) ;
1615 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1616 -1 , wxSIZE_USE_EXISTING
);
1618 s_lastMouseDown
= 0;
1622 if (TrackGoAway(window
, ev
->where
))
1627 s_lastMouseDown
= 0;
1631 int growResult
= GrowWindow(window
, ev
->where
, &screenBits
.bounds
);
1632 if (growResult
!= 0)
1634 int newWidth
= LoWord(growResult
);
1635 int newHeight
= HiWord(growResult
);
1636 int oldWidth
, oldHeight
;
1641 win
->GetSize(&oldWidth
, &oldHeight
);
1643 newWidth
= oldWidth
;
1645 newHeight
= oldHeight
;
1646 win
->SetSize( -1, -1 , newWidth
, newHeight
, wxSIZE_USE_EXISTING
);
1649 s_lastMouseDown
= 0;
1654 if (TrackBox(window
, ev
->where
, windowPart
))
1656 // TODO setup size event
1657 ZoomWindow( window
, windowPart
, false ) ;
1663 Point pt
= { 0, 0 } ;
1664 SetPortWindowPort(window
) ;
1665 LocalToGlobal( &pt
) ;
1668 GetWindowPortBounds(window
, &tempRect
) ;
1669 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1670 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1673 s_lastMouseDown
= 0;
1675 case inCollapseBox
:
1676 // TODO setup size event
1677 s_lastMouseDown
= 0;
1684 SetPortWindowPort(window
) ;
1687 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1689 if ( s_macIsInModalLoop
)
1693 else if ( UMAIsWindowFloating( window
) )
1696 win
->MacMouseDown( ev
, windowPart
) ;
1701 win
->MacMouseDown( ev
, windowPart
) ;
1702 ::SelectWindow( window
) ;
1708 win
->MacMouseDown( ev
, windowPart
) ;
1717 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1719 EventRecord
* ev
= (EventRecord
*) evr
;
1722 short windowPart
= inNoWindow
;
1723 if ( wxTheApp
->s_captureWindow
)
1725 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1726 windowPart
= inContent
;
1730 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1741 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1743 win
->MacMouseUp( ev
, windowPart
) ;
1749 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1750 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1755 case kHomeCharCode
:
1758 case kEnterCharCode
:
1759 retval
= WXK_RETURN
;
1764 case kHelpCharCode
:
1767 case kBackspaceCharCode
:
1773 case kPageUpCharCode
:
1774 retval
= WXK_PAGEUP
;
1776 case kPageDownCharCode
:
1777 retval
= WXK_PAGEDOWN
;
1779 case kReturnCharCode
:
1780 retval
= WXK_RETURN
;
1782 case kFunctionKeyCharCode
:
1834 case kEscapeCharCode
:
1835 retval
= WXK_ESCAPE
;
1837 case kLeftArrowCharCode
:
1840 case kRightArrowCharCode
:
1841 retval
= WXK_RIGHT
;
1843 case kUpArrowCharCode
:
1846 case kDownArrowCharCode
:
1849 case kDeleteCharCode
:
1850 retval
= WXK_DELETE
;
1858 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1860 EventRecord
* ev
= (EventRecord
*) evr
;
1861 wxToolTip::RemoveToolTips() ;
1863 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1864 if ( HiWord( menuresult
) )
1866 if ( !s_macIsInModalLoop
)
1867 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1871 wxWindow
* focus
= wxWindow::FindFocus() ;
1873 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1875 // has not been handled -> perform default
1876 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1877 if ( control
&& control
->GetMacControl() != NULL
)
1881 keychar
= short(ev
->message
& charCodeMask
);
1882 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1883 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1889 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1896 keychar
= short(keymessage
& charCodeMask
);
1897 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1899 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1901 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1902 // and look at the character after
1904 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1905 keychar
= short(keyInfo
& charCodeMask
);
1906 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1908 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1909 long realkeyval
= keyval
;
1910 if ( keyval
== keychar
)
1912 // 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)
1913 realkeyval
= short(keymessage
& charCodeMask
) ;
1914 keyval
= wxToupper( keyval
) ;
1917 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1918 bool handled
= false ;
1919 event
.m_shiftDown
= modifiers
& shiftKey
;
1920 event
.m_controlDown
= modifiers
& controlKey
;
1921 event
.m_altDown
= modifiers
& optionKey
;
1922 event
.m_metaDown
= modifiers
& cmdKey
;
1923 event
.m_keyCode
= keyval
;
1927 event
.m_timeStamp
= when
;
1928 event
.SetEventObject(focus
);
1929 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1930 if ( handled
&& event
.GetSkipped() )
1937 wxWindow
*ancestor
= focus
;
1940 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1943 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1944 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1947 if (ancestor
->IsTopLevel())
1949 ancestor
= ancestor
->GetParent();
1952 #endif // wxUSE_ACCEL
1956 event
.Skip( FALSE
) ;
1957 event
.SetEventType( wxEVT_CHAR
) ;
1959 event
.m_keyCode
= realkeyval
;
1961 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1962 if ( handled
&& event
.GetSkipped() )
1966 (keyval
== WXK_TAB
) &&
1967 // CS: copied the change below from wxGTK
1968 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1969 // have this style, yet choose not to process this particular TAB in which
1970 // case TAB must still work as a navigational character
1972 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1974 (focus
->GetParent()) &&
1975 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1977 wxNavigationKeyEvent new_event
;
1978 new_event
.SetEventObject( focus
);
1979 new_event
.SetDirection( !event
.ShiftDown() );
1980 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1981 new_event
.SetWindowChange( event
.ControlDown() );
1982 new_event
.SetCurrentFocus( focus
);
1983 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1984 if ( handled
&& new_event
.GetSkipped() )
1987 // backdoor handler for default return and command escape
1988 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1990 // if window is not having a focus still testing for default enter or cancel
1991 // TODO add the UMA version for ActiveNonFloatingWindow
1992 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1995 if ( keyval
== WXK_RETURN
)
1997 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1999 if ( def
&& def
->IsEnabled() )
2001 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2002 event
.SetEventObject(def
);
2003 def
->Command(event
);
2007 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2008 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2010 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2011 new_event
.SetEventObject( focus
);
2012 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2020 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
2022 EventRecord
* ev
= (EventRecord
*) evr
;
2023 wxToolTip::RemoveToolTips() ;
2025 UInt32 menuresult
= UMAMenuEvent(ev
) ;
2026 if ( HiWord( menuresult
) )
2031 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
2035 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2042 keychar
= short(keymessage
& charCodeMask
);
2043 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2044 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2046 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2047 // and look at the character after
2049 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2050 keychar
= short(keyInfo
& charCodeMask
);
2051 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2053 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2055 if ( keyval
== keychar
)
2057 keyval
= wxToupper( keyval
) ;
2059 bool handled
= false ;
2061 wxKeyEvent
event(wxEVT_KEY_UP
);
2062 event
.m_shiftDown
= modifiers
& shiftKey
;
2063 event
.m_controlDown
= modifiers
& controlKey
;
2064 event
.m_altDown
= modifiers
& optionKey
;
2065 event
.m_metaDown
= modifiers
& cmdKey
;
2066 event
.m_keyCode
= keyval
;
2070 event
.m_timeStamp
= when
;
2071 event
.SetEventObject(focus
);
2072 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2076 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2078 EventRecord
* ev
= (EventRecord
*) evr
;
2079 WindowRef window
= (WindowRef
) ev
->message
;
2082 bool activate
= (ev
->modifiers
& activeFlag
) ;
2083 WindowClass wclass
;
2084 ::GetWindowClass ( window
, &wclass
) ;
2085 if ( wclass
== kFloatingWindowClass
)
2087 // if it is a floater we activate/deactivate the front non-floating window instead
2088 window
= ::FrontNonFloatingWindow() ;
2090 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2092 win
->MacActivate( ev
, activate
) ;
2096 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2098 EventRecord
* ev
= (EventRecord
*) evr
;
2099 WindowRef window
= (WindowRef
) ev
->message
;
2100 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2103 if ( !wxPendingDelete
.Member(win
) )
2104 win
->MacUpdate( ev
->when
) ;
2108 // since there is no way of telling this foreign window to update itself
2109 // we have to invalidate the update region otherwise we keep getting the same
2110 // event over and over again
2111 BeginUpdate( window
) ;
2112 EndUpdate( window
) ;
2116 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2118 EventRecord
* ev
= (EventRecord
*) evr
;
2119 if ( HiWord( ev
->message
) != noErr
)
2124 SetPt( &point
, 100 , 100 ) ;
2126 err
= DIBadMount( point
, ev
->message
) ;
2127 wxASSERT( err
== noErr
) ;
2132 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2134 EventRecord
* ev
= (EventRecord
*) evr
;
2135 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2137 case suspendResumeMessage
:
2139 bool isResuming
= ev
->message
& resumeFlag
;
2141 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2143 bool convertClipboard
= false;
2145 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2148 WindowRef oldFrontWindow
= NULL
;
2149 WindowRef newFrontWindow
= NULL
;
2151 // in case we don't take care of activating ourselves, we have to synchronize
2152 // our idea of the active window with the process manager's - which it already activated
2154 if ( !doesActivate
)
2155 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2157 MacResume( convertClipboard
) ;
2159 newFrontWindow
= ::FrontNonFloatingWindow() ;
2161 if ( oldFrontWindow
)
2163 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2165 win
->MacActivate( ev
, false ) ;
2167 if ( newFrontWindow
)
2169 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2171 win
->MacActivate( ev
, true ) ;
2176 MacSuspend( convertClipboard
) ;
2178 // in case this suspending did close an active window, another one might
2179 // have surfaced -> lets deactivate that one
2181 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
2182 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
2183 if ( newActiveWindow )
2185 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
2187 win->MacActivate( ev , false ) ;
2193 case mouseMovedMessage
:
2197 wxWindow
* currentMouseWindow
= NULL
;
2199 if (s_captureWindow
)
2201 currentMouseWindow
= s_captureWindow
;
2205 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2206 ¤tMouseWindow
) ;
2209 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2211 wxMouseEvent event
;
2213 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2214 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2216 event
.m_leftDown
= isDown
&& !controlDown
;
2217 event
.m_middleDown
= FALSE
;
2218 event
.m_rightDown
= isDown
&& controlDown
;
2219 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2220 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2221 event
.m_altDown
= ev
->modifiers
& optionKey
;
2222 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2223 event
.m_x
= ev
->where
.h
;
2224 event
.m_y
= ev
->where
.v
;
2225 event
.m_timeStamp
= ev
->when
;
2226 event
.SetEventObject(this);
2228 if ( wxWindow::s_lastMouseWindow
)
2230 wxMouseEvent
eventleave(event
);
2231 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2232 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2233 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2235 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2237 if ( currentMouseWindow
)
2239 wxMouseEvent
evententer(event
);
2240 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2241 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2242 evententer
.SetEventObject( currentMouseWindow
) ;
2243 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2245 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2248 short windowPart
= inNoWindow
;
2250 if ( s_captureWindow
)
2252 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2253 windowPart
= inContent
;
2257 windowPart
= ::FindWindow(ev
->where
, &window
);
2264 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2266 win
->MacMouseMoved( ev
, windowPart
) ;
2273 UMAShowArrowCursor();
2283 UMAShowArrowCursor();
2293 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2296 return; // no menu item selected
2298 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2301 Str255 deskAccessoryName
;
2304 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2305 GetPort(&savedPort
);
2306 OpenDeskAcc(deskAccessoryName
);
2312 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
2313 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
2314 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
2320 long wxApp::MacTranslateKey(char key, int mods)
2324 void wxApp::MacAdjustCursor()
2331 wxApp::macAdjustCursor()
2333 if (ev->what != kHighLevelEvent)
2335 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
2338 if (!theMacWxFrame->MacAdjustCursor(ev->where))
2339 ::SetCursor(&(qd.arrow));