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 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
113 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
114 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
115 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
118 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
120 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
121 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
124 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
126 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
127 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
130 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
133 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
136 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
138 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
139 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
142 // new virtual public method in wxApp
143 void wxApp::MacOpenFile(const wxString
& fileName
)
145 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
147 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
150 void wxApp::MacPrintFile(const wxString
& fileName
)
152 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
155 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
158 wxView
* view
= doc
->GetFirstView() ;
161 wxPrintout
*printout
= view
->OnCreatePrintout();
165 printer
.Print(view
->GetFrame(), printout
, TRUE
);
171 doc
->DeleteAllViews();
172 dm
->RemoveDocument(doc
) ;
178 void wxApp::MacNewFile()
182 // new implementation, which parses the event and calls
183 // MacOpenFile on each of the files it's passed
184 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
188 DescType returnedType
;
194 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
198 err
= AECountItems(&docList
, &itemsInList
);
202 ProcessSerialNumber PSN
;
203 PSN
.highLongOfPSN
= 0 ;
204 PSN
.lowLongOfPSN
= kCurrentProcess
;
205 SetFrontProcess( &PSN
) ;
207 for (i
= 1; i
<= itemsInList
; i
++) {
208 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
209 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
210 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
216 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
220 DescType returnedType
;
226 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
230 err
= AECountItems(&docList
, &itemsInList
);
234 ProcessSerialNumber PSN
;
235 PSN
.highLongOfPSN
= 0 ;
236 PSN
.lowLongOfPSN
= kCurrentProcess
;
237 SetFrontProcess( &PSN
) ;
239 for (i
= 1; i
<= itemsInList
; i
++) {
240 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
241 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
242 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
248 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
254 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
256 wxWindow
* win
= GetTopWindow() ;
268 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
269 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
270 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
271 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
272 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
273 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
275 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
276 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
277 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
278 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
279 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
280 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
282 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
287 for( int i
= 0 ; i
< len
; ++ i
)
289 c
= strchr( StringANSI
, *from
) ;
292 *to
= StringMac
[ c
- StringANSI
] ;
300 for( int i
= 0 ; i
< len
; ++ i
)
302 c
= strchr( StringANSI
, *from
) ;
305 *to
= StringMac
[ c
- StringANSI
] ;
317 void wxMacConvertToPC( const char *from
, char *to
, int len
)
322 for( int i
= 0 ; i
< len
; ++ i
)
324 c
= strchr( StringMac
, *from
) ;
327 *to
= StringANSI
[ c
- StringMac
] ;
335 for( int i
= 0 ; i
< len
; ++ i
)
337 c
= strchr( StringMac
, *from
) ;
340 *to
= StringANSI
[ c
- StringMac
] ;
352 void wxMacConvertFromPC( char * p
)
355 int len
= strlen ( p
) ;
357 wxMacConvertFromPC( ptr
, ptr
, len
) ;
360 void wxMacConvertFromPCForControls( char * p
)
363 int len
= strlen ( p
) ;
365 wxMacConvertFromPC( ptr
, ptr
, len
) ;
366 for ( unsigned int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
368 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
370 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
375 void wxMacConvertFromPC( unsigned char *p
)
377 char *ptr
= (char*) p
+ 1 ;
380 wxMacConvertFromPC( ptr
, ptr
, len
) ;
383 extern char *wxBuffer
;
385 wxString
wxMacMakeMacStringFromPC( const char * p
)
387 const char *ptr
= p
;
388 int len
= strlen ( p
) ;
389 char *buf
= wxBuffer
;
391 if ( len
>= BUFSIZ
+ 512 )
393 buf
= new char [len
+1] ;
396 wxMacConvertFromPC( ptr
, buf
, len
) ;
398 wxString
result( buf
) ;
399 if ( buf
!= wxBuffer
)
405 void wxMacConvertToPC( char * p
)
408 int len
= strlen ( p
) ;
410 wxMacConvertToPC( ptr
, ptr
, len
) ;
413 void wxMacConvertToPC( unsigned char *p
)
415 char *ptr
= (char*) p
+ 1 ;
418 wxMacConvertToPC( ptr
, ptr
, len
) ;
421 wxString
wxMacMakePCStringFromMac( const char * p
)
423 const char *ptr
= p
;
424 int len
= strlen ( p
) ;
425 char *buf
= wxBuffer
;
427 if ( len
>= BUFSIZ
+ 512 )
429 buf
= new char [len
+1] ;
432 wxMacConvertToPC( ptr
, buf
, len
) ;
435 wxString
result( buf
) ;
436 if ( buf
!= wxBuffer
)
441 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
445 return wxMacMakePCStringFromMac( from
) ;
449 return wxString( from
) ;
453 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
455 // this is safe since a pascal string can never be larger than 256 bytes
457 CopyPascalStringToC( from
, s
) ;
460 return wxMacMakePCStringFromMac( s
) ;
464 return wxString( s
) ;
468 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
472 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
476 CopyCStringToPascal( from
, to
) ;
480 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
481 // we know it's there ;-)
482 WXIMPORT
char std::__throws_bad_alloc
;
485 bool wxApp::Initialize()
491 UMAInitToolbox( 4 ) ;
492 SetEventMask( everyEvent
) ;
493 UMAShowWatchCursor() ;
495 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
496 // open shared library resources from here since we don't have
497 // __wxinitialize in Mach-O shared libraries
498 wxStAppResource::OpenSharedLibraryResource(NULL
);
501 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
502 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
503 NewAEEventHandlerUPP(AEHandleODoc
) ,
504 (long) wxTheApp
, FALSE
) ;
505 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
506 NewAEEventHandlerUPP(AEHandleOApp
) ,
507 (long) wxTheApp
, FALSE
) ;
508 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
509 NewAEEventHandlerUPP(AEHandlePDoc
) ,
510 (long) wxTheApp
, FALSE
) ;
511 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
512 NewAEEventHandlerUPP(AEHandleQuit
) ,
513 (long) wxTheApp
, FALSE
) ;
515 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
516 NewAEEventHandlerProc(AEHandleODoc
) ,
517 (long) wxTheApp
, FALSE
) ;
518 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
519 NewAEEventHandlerProc(AEHandleOApp
) ,
520 (long) wxTheApp
, FALSE
) ;
521 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
522 NewAEEventHandlerProc(AEHandlePDoc
) ,
523 (long) wxTheApp
, FALSE
) ;
524 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
525 NewAEEventHandlerProc(AEHandleQuit
) ,
526 (long) wxTheApp
, FALSE
) ;
530 // test the minimal configuration necessary
536 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
538 error
= kMacSTRWrongMachine
;
540 else if (theMachine
< gestaltMacPlus
)
542 error
= kMacSTRWrongMachine
;
544 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
546 error
= kMacSTROldSystem
;
548 else if ( theSystem
< 0x0860 )
550 error
= kMacSTROldSystem
;
552 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
554 error
= kMacSTRSmallSize
;
560 if ( !UMAHasAppearance() )
562 error = kMacSTRNoPre8Yet ;
568 // if we encountered any problems so far, give the error code and exit immediately
572 wxStAppResource resload
;
576 GetIndString(message
, 128, error
);
577 UMAShowArrowCursor() ;
578 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
579 itemHit
= Alert(128, nil
);
584 # if __option(profile)
585 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
590 // now avoid exceptions thrown for new (bad_alloc)
591 std::__throws_bad_alloc
= FALSE
;
594 s_macCursorRgn
= ::NewRgn() ;
596 wxBuffer
= new char[BUFSIZ
+ 512];
598 wxClassInfo::InitializeClasses();
601 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
605 wxPendingEventsLocker
= new wxCriticalSection
;
608 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
609 wxTheColourDatabase
->Initialize();
613 // flush the logged messages if any and install a 'safer' log target: the
614 // default one (wxLogGui) can't be used after the resources are freed just
615 // below and the user suppliedo ne might be even more unsafe (using any
616 // wxWindows GUI function is unsafe starting from now)
617 wxLog::DontCreateOnDemand();
619 // this will flush the old messages if any
620 delete wxLog::SetActiveTarget(new wxLogStderr
);
624 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
625 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
627 wxInitializeStockLists();
628 wxInitializeStockObjects();
630 #if wxUSE_WX_RESOURCES
631 wxInitializeResourceSystem();
634 wxBitmap::InitStandardHandlers();
636 wxModule::RegisterModules();
637 if (!wxModule::InitializeModules()) {
641 wxMacCreateNotifierTable() ;
644 UMAShowArrowCursor() ;
649 void wxApp::CleanUp()
651 wxToolTip::RemoveToolTips() ;
653 // flush the logged messages if any and install a 'safer' log target: the
654 // default one (wxLogGui) can't be used after the resources are freed just
655 // below and the user suppliedo ne might be even more unsafe (using any
656 // wxWindows GUI function is unsafe starting from now)
657 wxLog::DontCreateOnDemand();
659 // this will flush the old messages if any
660 delete wxLog::SetActiveTarget(new wxLogStderr
);
663 // One last chance for pending objects to be cleaned up
664 wxTheApp
->DeletePendingObjects();
666 wxModule::CleanUpModules();
668 #if wxUSE_WX_RESOURCES
669 wxCleanUpResourceSystem();
672 wxDeleteStockObjects() ;
674 // Destroy all GDI lists, etc.
675 wxDeleteStockLists();
677 delete wxTheColourDatabase
;
678 wxTheColourDatabase
= NULL
;
680 wxBitmap::CleanUpHandlers();
685 wxMacDestroyNotifierTable() ;
686 if (wxWinMacWindowList
) {
687 delete wxWinMacWindowList
;
689 if (wxWinMacControlList
) {
690 delete wxWinMacControlList
;
692 delete wxPendingEvents
;
695 delete wxPendingEventsLocker
;
696 // If we don't do the following, we get an apparent memory leak.
697 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
700 wxClassInfo::CleanUpClasses();
703 # if __option(profile)
704 ProfilerDump( "\papp.prof" ) ;
712 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
713 // At this point we want to check if there are any memory
714 // blocks that aren't part of the wxDebugContext itself,
715 // as a special case. Then when dumping we need to ignore
716 // wxDebugContext, too.
717 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
719 wxLogDebug(wxT("There were memory leaks."));
720 wxDebugContext::Dump();
721 wxDebugContext::PrintStatistics();
723 // wxDebugContext::SetStream(NULL, NULL);
727 // do it as the very last thing because everything else can log messages
728 delete wxLog::SetActiveTarget(NULL
);
731 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
732 // close shared library resources from here since we don't have
733 // __wxterminate in Mach-O shared libraries
734 wxStAppResource::CloseSharedLibraryResource();
737 UMACleanupToolbox() ;
738 if (s_macCursorRgn
) {
739 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
747 //----------------------------------------------------------------------
749 //----------------------------------------------------------------------
751 // extern variable for shared library resource id
752 // need to be able to find it with NSLookupAndBindSymbol
753 short gSharedLibraryResource
= kResFileNotOpened
;
755 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
756 CFBundleRef gSharedLibraryBundle
= NULL
;
757 #endif /* WXMAKINGDLL && __DARWIN__ */
759 wxStAppResource::wxStAppResource()
761 m_currentRefNum
= CurResFile() ;
762 if ( gSharedLibraryResource
!= kResFileNotOpened
)
764 UseResFile( gSharedLibraryResource
) ;
768 wxStAppResource::~wxStAppResource()
770 if ( m_currentRefNum
!= kResFileNotOpened
)
772 UseResFile( m_currentRefNum
) ;
776 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
778 gSharedLibraryResource
= kResFileNotOpened
;
781 if ( initBlock
!= NULL
) {
782 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
783 FSSpec
*fileSpec
= NULL
;
785 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
786 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
788 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
789 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
792 if (fileSpec
!= NULL
) {
793 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
798 // Open the shared library resource file if it is not yet open
801 const char *theLibPath
;
803 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
804 if (gSharedLibraryBundle
!= NULL
) {
805 // wxWindows has been bundled into a framework
806 // load the framework resources
808 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
811 // wxWindows is a simple dynamic shared library
812 // load the resources from the data fork of a separate resource file
817 OSErr theErr
= noErr
;
819 // get the library path
820 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
821 theModule
= NSModuleForSymbol(theSymbol
);
822 theLibPath
= NSLibraryNameForModule(theModule
);
824 wxLogDebug( wxT("wxMac library installation name is '%s'"),
827 // allocate copy to replace .dylib.* extension with .rsrc
828 theResPath
= strdup(theLibPath
);
829 if (theResPath
!= NULL
) {
830 theName
= strrchr(theResPath
, '/');
831 if (theName
== NULL
) {
832 // no directory elements in path
833 theName
= theResPath
;
835 // find ".dylib" shared library extension
836 theExt
= strstr(theName
, ".dylib");
837 // overwrite extension with ".rsrc"
838 strcpy(theExt
, ".rsrc");
840 wxLogDebug( wxT("wxMac resources file name is '%s'"),
843 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
844 if (theErr
!= noErr
) {
845 // try in current directory (using name only)
846 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
849 // open the resource file
850 if (theErr
== noErr
) {
851 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
852 &gSharedLibraryResource
);
854 if (theErr
!= noErr
) {
855 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
859 // free duplicated resource file path
863 #endif /* __DARWIN__ */
865 #endif /* WXMAKINGDLL */
868 void wxStAppResource::CloseSharedLibraryResource()
871 // Close the shared library resource file
872 if (gSharedLibraryResource
!= kResFileNotOpened
) {
874 if (gSharedLibraryBundle
!= NULL
) {
875 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
876 gSharedLibraryResource
);
877 gSharedLibraryBundle
= NULL
;
880 #endif /* __DARWIN__ */
882 CloseResFile(gSharedLibraryResource
);
884 gSharedLibraryResource
= kResFileNotOpened
;
886 #endif /* WXMAKINGDLL */
889 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
891 // for shared libraries we have to manually get the correct resource
892 // ref num upon initializing and releasing when terminating, therefore
893 // the __wxinitialize and __wxterminate must be used
896 void __sinit(void); /* (generated by linker) */
897 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
898 pascal void __terminate(void);
901 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
903 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
904 return __initialize( theInitBlock
) ;
907 pascal void __wxterminate(void)
909 wxStAppResource::CloseSharedLibraryResource() ;
913 #endif /* WXMAKINGDLL && !__DARWIN__ */
915 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
917 return wxApp::Initialize();
920 int WXDLLEXPORT
wxEntryInitGui()
922 return wxTheApp
->OnInitGui();
925 void WXDLLEXPORT
wxEntryCleanup()
930 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
933 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
934 // This seems to be necessary since there are 'rogue'
935 // objects present at this point (perhaps global objects?)
936 // Setting a checkpoint will ignore them as far as the
937 // memory checking facility is concerned.
938 // Of course you may argue that memory allocated in globals should be
939 // checked, but this is a reasonable compromise.
940 wxDebugContext::SetCheckpoint();
943 if (!wxEntryStart(argc
, argv
)) {
946 // create the application object or ensure that one already exists
949 // The app may have declared a global application object, but we recommend
950 // the IMPLEMENT_APP macro is used instead, which sets an initializer
951 // function for delayed, dynamic app object construction.
952 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
953 wxT("No initializer - use IMPLEMENT_APP macro.") );
955 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
958 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
961 // Mac OS X passes a process serial number command line argument when
962 // the application is launched from the Finder. This argument must be
963 // removed from the command line arguments before being handled by the
964 // application (otherwise applications would need to handle it)
967 if (strncmp(argv
[1], "-psn_", 5) == 0) {
968 // assume the argument is always the only one and remove it
973 argc
= 0 ; // currently we don't support files as parameters
975 // we could try to get the open apple events here to adjust argc and argv better
977 wxTheApp
->argc
= argc
;
978 wxTheApp
->argv
= argv
;
980 // GUI-specific initialization, such as creating an app context.
983 // Here frames insert themselves automatically
984 // into wxTopLevelWindows by getting created
989 if ( wxTheApp
->OnInit() )
993 retValue
= wxTheApp
->OnRun();
996 // We want to initialize, but not run or exit immediately.
999 //else: app initialization failed, so we skipped OnRun()
1001 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1004 // Forcibly delete the window.
1005 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
1006 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
1008 topWindow
->Close(TRUE
);
1009 wxTheApp
->DeletePendingObjects();
1014 wxTheApp
->SetTopWindow(NULL
);
1027 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1029 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1030 OSStatus err
= noErr
;
1033 switch( GetEventClass( event
) )
1035 case kEventClassKeyboard
:
1038 switch( GetEventKind(event
) )
1040 case kEventRawKeyDown
:
1041 rec
->what
= keyDown
;
1043 case kEventRawKeyRepeat
:
1044 rec
->what
= autoKey
;
1046 case kEventRawKeyUp
:
1049 case kEventRawKeyModifiersChanged
:
1050 rec
->what
= nullEvent
;
1059 unsigned char charCode
;
1061 GetMouse( &rec
->where
) ;
1063 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1064 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1065 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1066 rec
->modifiers
= modifiers
;
1067 rec
->message
= (keyCode
<< 8 ) + charCode
;
1071 case kEventClassTextInput
:
1073 switch( GetEventKind( event
) )
1075 case kEventTextInputUnicodeForKeyEvent
:
1078 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1082 unsigned char charCode
;
1084 GetMouse( &rec
->where
) ;
1085 rec
->what
= keyDown
;
1086 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1087 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1088 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1089 rec
->modifiers
= modifiers
;
1090 rec
->message
= (keyCode
<< 8 ) + charCode
;
1105 pascal OSStatus
wxMacApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1107 OSStatus result
= eventNotHandledErr
;
1110 switch ( GetEventClass( event
) )
1112 case kEventClassKeyboard
:
1113 if ( wxMacConvertEventToRecord( event
, &rec
) )
1115 wxTheApp
->MacHandleModifierEvents( &rec
) ;
1116 wxTheApp
->MacHandleOneEvent( &rec
) ;
1120 case kEventClassTextInput
:
1121 if ( wxMacConvertEventToRecord( event
, &rec
) )
1123 wxTheApp
->MacHandleModifierEvents( &rec
) ;
1124 wxTheApp
->MacHandleOneEvent( &rec
) ;
1136 bool wxApp::OnInit()
1138 if ( ! wxAppBase::OnInit() )
1141 #if 0 // TARGET_CARBON
1142 static const EventTypeSpec eventList
[] =
1144 { kEventClassKeyboard
, kEventRawKeyDown
} ,
1145 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
1146 { kEventClassKeyboard
, kEventRawKeyUp
} ,
1147 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
1149 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
1152 InstallApplicationEventHandler(NewEventHandlerUPP(wxMacApplicationEventHandler
)
1153 , WXSIZEOF(eventList
), eventList
, this, NULL
);
1157 // Static member initialization
1158 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1165 #if WXWIN_COMPATIBILITY_2_2
1166 m_wantDebugOutput
= TRUE
;
1172 m_printMode
= wxPRINT_WINDOWS
;
1176 bool wxApp::Initialized()
1184 int wxApp::MainLoop()
1196 // Returns TRUE if more time is needed.
1197 bool wxApp::ProcessIdle()
1200 event
.SetEventObject(this);
1201 ProcessEvent(event
);
1203 return event
.MoreRequested();
1206 void wxApp::ExitMainLoop()
1208 m_keepGoing
= FALSE
;
1211 // Is a message/event pending?
1212 bool wxApp::Pending()
1216 return EventAvail( everyEvent
, &event
) ;
1219 // Dispatch a message.
1220 void wxApp::Dispatch()
1225 void wxApp::OnIdle(wxIdleEvent
& event
)
1227 static bool s_inOnIdle
= FALSE
;
1229 // Avoid recursion (via ProcessEvent default case)
1236 // 'Garbage' collection of windows deleted with Close().
1237 DeletePendingObjects();
1239 // flush the logged messages if any
1240 wxLog
*pLog
= wxLog::GetActiveTarget();
1241 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1244 // Send OnIdle events to all windows
1245 bool needMore
= SendIdleEvents();
1248 event
.RequestMore(TRUE
);
1250 // If they are pending events, we must process them: pending events are
1251 // either events to the threads other than main or events posted with
1252 // wxPostEvent() functions
1253 wxMacProcessNotifierAndPendingEvents();
1263 // Send idle event to all top-level windows
1264 bool wxApp::SendIdleEvents()
1266 bool needMore
= FALSE
;
1267 wxNode
* node
= wxTopLevelWindows
.First();
1270 wxWindow
* win
= (wxWindow
*) node
->Data();
1271 if (SendIdleEvents(win
))
1274 node
= node
->Next();
1279 // Send idle event to window and all subwindows
1280 bool wxApp::SendIdleEvents(wxWindow
* win
)
1282 bool needMore
= FALSE
;
1285 event
.SetEventObject(win
);
1286 win
->ProcessEvent(event
);
1288 if (event
.MoreRequested())
1291 wxNode
* node
= win
->GetChildren().First();
1294 wxWindow
* win
= (wxWindow
*) node
->Data();
1295 if (SendIdleEvents(win
))
1298 node
= node
->Next();
1303 void wxApp::DeletePendingObjects()
1305 wxNode
*node
= wxPendingDelete
.First();
1308 wxObject
*obj
= (wxObject
*)node
->Data();
1312 if (wxPendingDelete
.Member(obj
))
1315 // Deleting one object may have deleted other pending
1316 // objects, so start from beginning of list again.
1317 node
= wxPendingDelete
.First();
1323 wxLogError(_("Fatal error: exiting"));
1329 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1332 GetTopWindow()->Close(TRUE
);
1335 // Default behaviour: close the application with prompts. The
1336 // user can veto the close, and therefore the end session.
1337 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1341 if (!GetTopWindow()->Close(!event
.CanVeto()))
1346 extern "C" void wxCYield() ;
1352 // Yield to other processes
1354 bool wxApp::Yield(bool onlyIfNeeded
)
1358 if ( !onlyIfNeeded
)
1360 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1369 YieldToAnyThread() ;
1373 long sleepTime
= 1 ; //::GetCaretTime();
1375 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1377 wxTheApp
->MacHandleModifierEvents( &event
) ;
1378 wxTheApp
->MacHandleOneEvent( &event
);
1379 if ( event
.what
!= kHighLevelEvent
)
1380 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1382 wxTheApp
->MacHandleModifierEvents( &event
) ;
1384 wxMacProcessNotifierAndPendingEvents() ;
1391 // platform specifics
1393 void wxApp::MacSuspend( bool convertClipboard
)
1395 // we have to deactive the top level windows manually
1397 wxNode
* node
= wxTopLevelWindows
.First();
1400 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1401 win
->MacActivate( MacGetCurrentEvent() , false ) ;
1403 node
= node
->Next();
1406 s_lastMouseDown
= 0 ;
1407 if( convertClipboard
)
1409 MacConvertPrivateToPublicScrap() ;
1412 ::HideFloatingWindows() ;
1415 extern wxList wxModalDialogs
;
1417 void wxApp::MacResume( bool convertClipboard
)
1419 s_lastMouseDown
= 0 ;
1420 if( convertClipboard
)
1422 MacConvertPublicToPrivateScrap() ;
1425 ::ShowFloatingWindows() ;
1427 // raise modal dialogs in case a non modal window was selected to activate the app
1429 wxNode
* node
= wxModalDialogs
.First();
1432 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1435 node
= node
->Next();
1439 void wxApp::MacConvertPrivateToPublicScrap()
1443 void wxApp::MacConvertPublicToPrivateScrap()
1447 void wxApp::MacDoOneEvent()
1451 long sleepTime
= 1; // GetCaretTime() / 4 ;
1453 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1455 MacHandleModifierEvents( &event
) ;
1456 MacHandleOneEvent( &event
);
1460 MacHandleModifierEvents( &event
) ;
1462 WindowPtr window
= ::FrontWindow() ;
1464 ::IdleControls( window
) ;
1466 wxTheApp
->ProcessIdle() ;
1468 if ( event
.what
!= kHighLevelEvent
)
1469 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1473 DeletePendingObjects() ;
1474 wxMacProcessNotifierAndPendingEvents() ;
1477 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1479 EventRecord
* ev
= (EventRecord
*) evr
;
1481 if ( ev
->what
== mouseDown
|| ev
->what
== mouseUp
|| ev
->what
== activateEvt
||
1482 ev
->what
== keyDown
|| ev
->what
== autoKey
|| ev
->what
== keyUp
|| ev
->what
== kHighLevelEvent
||
1483 ev
->what
== nullEvent
1486 // in these cases the modifiers are already correctly setup by carbon
1491 WaitNextEvent( 0 , &nev
, 0 , NULL
) ;
1492 ev
->modifiers
= nev
.modifiers
;
1493 // KeyModifiers unfortunately don't include btnState...
1494 // ev->modifiers = GetCurrentKeyModifiers() ;
1497 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1499 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1501 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1502 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1503 event
.m_altDown
= ev
->modifiers
& optionKey
;
1504 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1506 event
.m_x
= ev
->where
.h
;
1507 event
.m_y
= ev
->where
.v
;
1508 event
.m_timeStamp
= ev
->when
;
1509 wxWindow
* focus
= wxWindow::FindFocus() ;
1510 event
.SetEventObject(focus
);
1512 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1514 event
.m_keyCode
= WXK_CONTROL
;
1515 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1516 focus
->GetEventHandler()->ProcessEvent( event
) ;
1518 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1520 event
.m_keyCode
= WXK_SHIFT
;
1521 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1522 focus
->GetEventHandler()->ProcessEvent( event
) ;
1524 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1526 event
.m_keyCode
= WXK_ALT
;
1527 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1528 focus
->GetEventHandler()->ProcessEvent( event
) ;
1530 s_lastModifiers
= ev
->modifiers
;
1534 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1536 EventRecord
* ev
= (EventRecord
*) evr
;
1537 m_macCurrentEvent
= ev
;
1539 wxApp::sm_lastMessageTime
= ev
->when
;
1544 MacHandleMouseDownEvent( ev
) ;
1545 if ( ev
->modifiers
& controlKey
)
1546 s_lastMouseDown
= 2;
1548 s_lastMouseDown
= 1;
1551 if ( s_lastMouseDown
== 2 )
1553 ev
->modifiers
|= controlKey
;
1557 ev
->modifiers
&= ~controlKey
;
1559 MacHandleMouseUpEvent( ev
) ;
1560 s_lastMouseDown
= 0;
1563 MacHandleActivateEvent( ev
) ;
1566 MacHandleUpdateEvent( ev
) ;
1570 MacHandleKeyDownEvent( ev
) ;
1573 MacHandleKeyUpEvent( ev
) ;
1576 MacHandleDiskEvent( ev
) ;
1579 MacHandleOSEvent( ev
) ;
1581 case kHighLevelEvent
:
1582 MacHandleHighLevelEvent( ev
) ;
1587 wxMacProcessNotifierAndPendingEvents() ;
1590 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1592 // we must avoid reentrancy problems when processing high level events eg printing
1593 bool former
= s_inYield
;
1595 EventRecord
* ev
= (EventRecord
*) evr
;
1596 ::AEProcessAppleEvent( ev
) ;
1597 s_inYield
= former
;
1600 bool s_macIsInModalLoop
= false ;
1602 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1604 EventRecord
* ev
= (EventRecord
*) evr
;
1605 wxToolTip::RemoveToolTips() ;
1608 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1609 WindowAttributes frontWindowAttributes
= NULL
;
1611 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1613 short windowPart
= ::FindWindow(ev
->where
, &window
);
1614 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1615 if ( wxPendingDelete
.Member(win
) )
1619 GetQDGlobalsScreenBits( &screenBits
);
1624 if ( s_macIsInModalLoop
)
1630 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1631 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1632 s_lastMouseDown
= 0;
1637 SystemClick( ev
, window
) ;
1638 s_lastMouseDown
= 0;
1642 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1648 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1653 Point pt
= { 0, 0 } ;
1654 SetPortWindowPort(window
) ;
1655 LocalToGlobal( &pt
) ;
1657 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1658 -1 , wxSIZE_USE_EXISTING
);
1660 s_lastMouseDown
= 0;
1664 if (TrackGoAway(window
, ev
->where
))
1669 s_lastMouseDown
= 0;
1673 Rect newContentRect
;
1674 Rect constraintRect
;
1675 constraintRect
.top
= win
->GetMinHeight() ;
1676 if ( constraintRect
.top
== -1 )
1677 constraintRect
.top
= 0 ;
1678 constraintRect
.left
= win
->GetMinWidth() ;
1679 if ( constraintRect
.left
== -1 )
1680 constraintRect
.left
= 0 ;
1681 constraintRect
.right
= win
->GetMaxWidth() ;
1682 if ( constraintRect
.right
== -1 )
1683 constraintRect
.right
= 32000 ;
1684 constraintRect
.bottom
= win
->GetMaxHeight() ;
1685 if ( constraintRect
.bottom
== -1 )
1686 constraintRect
.bottom
= 32000 ;
1688 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1689 &constraintRect
, &newContentRect
) ;
1692 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1693 newContentRect
.right
- newContentRect
.left
,
1694 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1696 s_lastMouseDown
= 0;
1701 if (TrackBox(window
, ev
->where
, windowPart
))
1703 // TODO setup size event
1704 ZoomWindow( window
, windowPart
, false ) ;
1710 Point pt
= { 0, 0 } ;
1711 SetPortWindowPort(window
) ;
1712 LocalToGlobal( &pt
) ;
1715 GetWindowPortBounds(window
, &tempRect
) ;
1716 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1717 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1720 s_lastMouseDown
= 0;
1722 case inCollapseBox
:
1723 // TODO setup size event
1724 s_lastMouseDown
= 0;
1731 SetPortWindowPort(window
) ;
1734 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1736 if ( s_macIsInModalLoop
)
1740 else if ( UMAIsWindowFloating( window
) )
1743 win
->MacMouseDown( ev
, windowPart
) ;
1748 win
->MacMouseDown( ev
, windowPart
) ;
1749 ::SelectWindow( window
) ;
1755 win
->MacMouseDown( ev
, windowPart
) ;
1764 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1766 EventRecord
* ev
= (EventRecord
*) evr
;
1769 short windowPart
= inNoWindow
;
1770 if ( wxTheApp
->s_captureWindow
)
1772 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1773 windowPart
= inContent
;
1777 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1788 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1790 win
->MacMouseUp( ev
, windowPart
) ;
1796 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1797 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1802 case kHomeCharCode
:
1805 case kEnterCharCode
:
1806 retval
= WXK_RETURN
;
1811 case kHelpCharCode
:
1814 case kBackspaceCharCode
:
1820 case kPageUpCharCode
:
1821 retval
= WXK_PAGEUP
;
1823 case kPageDownCharCode
:
1824 retval
= WXK_PAGEDOWN
;
1826 case kReturnCharCode
:
1827 retval
= WXK_RETURN
;
1829 case kFunctionKeyCharCode
:
1881 case kEscapeCharCode
:
1882 retval
= WXK_ESCAPE
;
1884 case kLeftArrowCharCode
:
1887 case kRightArrowCharCode
:
1888 retval
= WXK_RIGHT
;
1890 case kUpArrowCharCode
:
1893 case kDownArrowCharCode
:
1896 case kDeleteCharCode
:
1897 retval
= WXK_DELETE
;
1905 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1907 EventRecord
* ev
= (EventRecord
*) evr
;
1908 wxToolTip::RemoveToolTips() ;
1910 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1911 if ( HiWord( menuresult
) )
1913 if ( !s_macIsInModalLoop
)
1914 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1918 wxWindow
* focus
= wxWindow::FindFocus() ;
1920 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1922 // has not been handled -> perform default
1923 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1924 if ( control
&& control
->GetMacControl() != NULL
)
1928 keychar
= short(ev
->message
& charCodeMask
);
1929 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1930 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1936 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1943 keychar
= short(keymessage
& charCodeMask
);
1944 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1946 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1948 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1949 // and look at the character after
1951 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1952 keychar
= short(keyInfo
& charCodeMask
);
1953 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1955 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1956 long realkeyval
= keyval
;
1957 if ( keyval
== keychar
)
1959 // 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)
1960 realkeyval
= short(keymessage
& charCodeMask
) ;
1961 keyval
= wxToupper( keyval
) ;
1964 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1965 bool handled
= false ;
1966 event
.m_shiftDown
= modifiers
& shiftKey
;
1967 event
.m_controlDown
= modifiers
& controlKey
;
1968 event
.m_altDown
= modifiers
& optionKey
;
1969 event
.m_metaDown
= modifiers
& cmdKey
;
1970 event
.m_keyCode
= keyval
;
1974 event
.m_timeStamp
= when
;
1975 event
.SetEventObject(focus
);
1976 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1977 if ( handled
&& event
.GetSkipped() )
1984 wxWindow
*ancestor
= focus
;
1987 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1990 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1991 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1994 if (ancestor
->IsTopLevel())
1996 ancestor
= ancestor
->GetParent();
1999 #endif // wxUSE_ACCEL
2003 event
.Skip( FALSE
) ;
2004 event
.SetEventType( wxEVT_CHAR
) ;
2006 event
.m_keyCode
= realkeyval
;
2008 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2009 if ( handled
&& event
.GetSkipped() )
2013 (keyval
== WXK_TAB
) &&
2014 // CS: copied the change below from wxGTK
2015 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2016 // have this style, yet choose not to process this particular TAB in which
2017 // case TAB must still work as a navigational character
2019 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2021 (focus
->GetParent()) &&
2022 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2024 wxNavigationKeyEvent new_event
;
2025 new_event
.SetEventObject( focus
);
2026 new_event
.SetDirection( !event
.ShiftDown() );
2027 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2028 new_event
.SetWindowChange( event
.ControlDown() );
2029 new_event
.SetCurrentFocus( focus
);
2030 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2031 if ( handled
&& new_event
.GetSkipped() )
2034 // backdoor handler for default return and command escape
2035 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2037 // if window is not having a focus still testing for default enter or cancel
2038 // TODO add the UMA version for ActiveNonFloatingWindow
2039 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2042 if ( keyval
== WXK_RETURN
)
2044 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2046 if ( def
&& def
->IsEnabled() )
2048 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2049 event
.SetEventObject(def
);
2050 def
->Command(event
);
2054 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2055 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2057 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2058 new_event
.SetEventObject( focus
);
2059 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2067 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
2069 EventRecord
* ev
= (EventRecord
*) evr
;
2070 wxToolTip::RemoveToolTips() ;
2072 UInt32 menuresult
= UMAMenuEvent(ev
) ;
2073 if ( HiWord( menuresult
) )
2078 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
2082 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2089 keychar
= short(keymessage
& charCodeMask
);
2090 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2091 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2093 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2094 // and look at the character after
2096 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2097 keychar
= short(keyInfo
& charCodeMask
);
2098 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2100 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2102 if ( keyval
== keychar
)
2104 keyval
= wxToupper( keyval
) ;
2106 bool handled
= false ;
2108 wxKeyEvent
event(wxEVT_KEY_UP
);
2109 event
.m_shiftDown
= modifiers
& shiftKey
;
2110 event
.m_controlDown
= modifiers
& controlKey
;
2111 event
.m_altDown
= modifiers
& optionKey
;
2112 event
.m_metaDown
= modifiers
& cmdKey
;
2113 event
.m_keyCode
= keyval
;
2117 event
.m_timeStamp
= when
;
2118 event
.SetEventObject(focus
);
2119 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2123 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2125 EventRecord
* ev
= (EventRecord
*) evr
;
2126 WindowRef window
= (WindowRef
) ev
->message
;
2129 bool activate
= (ev
->modifiers
& activeFlag
) ;
2130 WindowClass wclass
;
2131 ::GetWindowClass ( window
, &wclass
) ;
2132 if ( wclass
== kFloatingWindowClass
)
2134 // if it is a floater we activate/deactivate the front non-floating window instead
2135 window
= ::FrontNonFloatingWindow() ;
2137 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2139 win
->MacActivate( ev
, activate
) ;
2143 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2145 EventRecord
* ev
= (EventRecord
*) evr
;
2146 WindowRef window
= (WindowRef
) ev
->message
;
2147 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2150 if ( !wxPendingDelete
.Member(win
) )
2151 win
->MacUpdate( ev
->when
) ;
2155 // since there is no way of telling this foreign window to update itself
2156 // we have to invalidate the update region otherwise we keep getting the same
2157 // event over and over again
2158 BeginUpdate( window
) ;
2159 EndUpdate( window
) ;
2163 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2165 EventRecord
* ev
= (EventRecord
*) evr
;
2166 if ( HiWord( ev
->message
) != noErr
)
2171 SetPt( &point
, 100 , 100 ) ;
2173 err
= DIBadMount( point
, ev
->message
) ;
2174 wxASSERT( err
== noErr
) ;
2179 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2181 EventRecord
* ev
= (EventRecord
*) evr
;
2182 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2184 case suspendResumeMessage
:
2186 bool isResuming
= ev
->message
& resumeFlag
;
2188 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2190 bool convertClipboard
= false;
2192 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2195 WindowRef oldFrontWindow
= NULL
;
2196 WindowRef newFrontWindow
= NULL
;
2198 // in case we don't take care of activating ourselves, we have to synchronize
2199 // our idea of the active window with the process manager's - which it already activated
2201 if ( !doesActivate
)
2202 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2204 MacResume( convertClipboard
) ;
2206 newFrontWindow
= ::FrontNonFloatingWindow() ;
2208 if ( oldFrontWindow
)
2210 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2212 win
->MacActivate( ev
, false ) ;
2214 if ( newFrontWindow
)
2216 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2218 win
->MacActivate( ev
, true ) ;
2223 MacSuspend( convertClipboard
) ;
2225 // in case this suspending did close an active window, another one might
2226 // have surfaced -> lets deactivate that one
2228 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
2229 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
2230 if ( newActiveWindow )
2232 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
2234 win->MacActivate( ev , false ) ;
2240 case mouseMovedMessage
:
2244 wxWindow
* currentMouseWindow
= NULL
;
2246 if (s_captureWindow
)
2248 currentMouseWindow
= s_captureWindow
;
2252 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2253 ¤tMouseWindow
) ;
2256 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2258 wxMouseEvent event
;
2260 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2261 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2263 event
.m_leftDown
= isDown
&& !controlDown
;
2264 event
.m_middleDown
= FALSE
;
2265 event
.m_rightDown
= isDown
&& controlDown
;
2266 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2267 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2268 event
.m_altDown
= ev
->modifiers
& optionKey
;
2269 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2270 event
.m_x
= ev
->where
.h
;
2271 event
.m_y
= ev
->where
.v
;
2272 event
.m_timeStamp
= ev
->when
;
2273 event
.SetEventObject(this);
2275 if ( wxWindow::s_lastMouseWindow
)
2277 wxMouseEvent
eventleave(event
);
2278 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2279 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2280 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2282 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2284 if ( currentMouseWindow
)
2286 wxMouseEvent
evententer(event
);
2287 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2288 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2289 evententer
.SetEventObject( currentMouseWindow
) ;
2290 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2292 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2295 short windowPart
= inNoWindow
;
2297 if ( s_captureWindow
)
2299 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2300 windowPart
= inContent
;
2304 windowPart
= ::FindWindow(ev
->where
, &window
);
2311 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2313 win
->MacMouseMoved( ev
, windowPart
) ;
2320 UMAShowArrowCursor();
2330 UMAShowArrowCursor();
2340 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2343 return; // no menu item selected
2345 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2348 Str255 deskAccessoryName
;
2351 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2352 GetPort(&savedPort
);
2353 OpenDeskAcc(deskAccessoryName
);
2360 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2361 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2362 wxMenu
* menu
= NULL
;
2363 wxMenuItem
* item
= NULL
;
2366 item
= mbar
->FindItem( id
, &menu
) ;
2368 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2370 if (item
->IsCheckable())
2372 item
->Check( !item
->IsChecked() ) ;
2375 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2377 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
2378 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id );
2379 event.m_timeStamp = ((EventRecord*) MacGetCurrentEvent())->when ;
2380 event.SetEventObject(menu);
2381 event.SetInt(item->IsCheckable() ? item->IsChecked() : -1);
2382 frontwindow->GetEventHandler()->ProcessEvent(event);