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 wxList wxPendingDelete
;
74 extern wxList
*wxWinMacWindowList
;
75 extern wxList
*wxWinMacControlList
;
77 // statics for implementation
79 static bool s_inYield
= FALSE
;
80 static bool s_inOnIdle
= FALSE
;
83 static bool s_inReceiveEvent
= FALSE
;
84 static EventTime sleepTime
= kEventDurationNoWait
;
86 static long sleepTime
= 0 ;
89 wxApp
*wxTheApp
= NULL
;
91 #if !USE_SHARED_LIBRARY
92 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
93 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
94 EVT_IDLE(wxApp::OnIdle
)
95 EVT_END_SESSION(wxApp::OnEndSession
)
96 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
101 const short kMacMinHeap
= (29 * 1024) ;
102 // platform specific static variables
104 const short kwxMacMenuBarResource
= 1 ;
105 const short kwxMacAppleMenuId
= 1 ;
107 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
108 wxWindow
* wxApp::s_captureWindow
= NULL
;
109 int wxApp::s_lastMouseDown
= 0 ;
110 long wxApp::sm_lastMessageTime
= 0;
111 long wxApp::s_lastModifiers
= 0 ;
114 bool wxApp::s_macDefaultEncodingIsPC
= true ;
115 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
116 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
117 long wxApp::s_macPreferencesMenuItemId
= 0 ;
118 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
119 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
121 //----------------------------------------------------------------------
122 // Core Apple Event Support
123 //----------------------------------------------------------------------
125 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
150 // AEODoc Calls MacOpenFile on each of the files passed
152 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
156 DescType returnedType
;
162 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
166 err
= AECountItems(&docList
, &itemsInList
);
170 ProcessSerialNumber PSN
;
171 PSN
.highLongOfPSN
= 0 ;
172 PSN
.lowLongOfPSN
= kCurrentProcess
;
173 SetFrontProcess( &PSN
) ;
175 for (i
= 1; i
<= itemsInList
; i
++) {
176 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
177 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
178 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
184 // AEPDoc Calls MacPrintFile on each of the files passed
186 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
190 DescType returnedType
;
196 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
200 err
= AECountItems(&docList
, &itemsInList
);
204 ProcessSerialNumber PSN
;
205 PSN
.highLongOfPSN
= 0 ;
206 PSN
.lowLongOfPSN
= kCurrentProcess
;
207 SetFrontProcess( &PSN
) ;
209 for (i
= 1; i
<= itemsInList
; i
++) {
210 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
211 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
212 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
218 // AEOApp calls MacNewFile
220 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
226 // AEQuit attempts to quite the application
228 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
230 /* wxWindow* win = GetTopWindow() ;
243 //----------------------------------------------------------------------
244 // Support Routines linking the Mac...File Calls to the Document Manager
245 //----------------------------------------------------------------------
247 void wxApp::MacOpenFile(const wxString
& fileName
)
249 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
251 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
254 void wxApp::MacPrintFile(const wxString
& fileName
)
256 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
259 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
262 wxView
* view
= doc
->GetFirstView() ;
265 wxPrintout
*printout
= view
->OnCreatePrintout();
269 printer
.Print(view
->GetFrame(), printout
, TRUE
);
275 doc
->DeleteAllViews();
276 dm
->RemoveDocument(doc
) ;
282 void wxApp::MacNewFile()
286 //----------------------------------------------------------------------
287 // Carbon Event Handler
288 //----------------------------------------------------------------------
292 static const EventTypeSpec eventList
[] =
294 { kEventClassCommand
, kEventProcessCommand
} ,
295 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
297 { kEventClassApplication
, kEventAppActivated
} ,
298 { kEventClassApplication
, kEventAppDeactivated
} ,
299 // handling the quit event is not recommended by apple
300 // rather using the quit apple event - which we do
302 { kEventClassAppleEvent
, kEventAppleEvent
} ,
304 { kEventClassMouse
, kEventMouseDown
} ,
307 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
309 OSStatus result
= eventNotHandledErr
;
310 UInt32 kind
= GetEventKind( event
) ;
315 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
316 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
317 // deal with clicks in the menu bar explicitely
319 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
321 OSStatus result
= eventNotHandledErr
;
323 switch( GetEventKind(event
) )
325 case kEventMouseDown
:
330 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
331 sizeof( Point
), NULL
, &point
);
332 short windowPart
= ::FindWindow(point
, &window
);
334 if ( windowPart
== inMenuBar
)
336 MenuSelect( point
) ;
346 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
348 OSStatus result
= eventNotHandledErr
;
352 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
353 sizeof( HICommand
), NULL
, &command
);
355 MenuCommand id
= command
.commandID
;
356 if ( id
== kHICommandPreferences
)
357 id
= wxApp::s_macPreferencesMenuItemId
;
359 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
360 wxMenu
* menu
= NULL
;
361 wxMenuItem
* item
= NULL
;
364 item
= mbar
->FindItem( id
, &menu
) ;
366 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
369 switch( GetEventKind( event
) )
371 case kEventProcessCommand
:
373 if (item
->IsCheckable())
375 item
->Check( !item
->IsChecked() ) ;
378 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
382 case kEventCommandUpdateStatus
:
383 // eventually trigger an updateui round
393 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
395 OSStatus result
= eventNotHandledErr
;
396 switch ( GetEventKind( event
) )
398 case kEventAppActivated
:
400 wxTheApp
->MacResume( true ) ;
404 case kEventAppDeactivated
:
406 wxTheApp
->MacSuspend( true ) ;
416 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
418 OSStatus result
= eventNotHandledErr
;
419 switch( GetEventClass( event
) )
421 case kEventClassCommand
:
422 result
= CommandEventHandler( handler
, event
, data
) ;
424 case kEventClassApplication
:
425 result
= ApplicationEventHandler( handler
, event
, data
) ;
427 case kEventClassMenu
:
428 result
= MenuEventHandler( handler
, event
, data
) ;
430 case kEventClassMouse
:
431 result
= MouseEventHandler( handler
, event
, data
) ;
433 case kEventClassAppleEvent
:
436 wxMacConvertEventToRecord( event
, &rec
) ;
437 result
= AEProcessAppleEvent( &rec
) ;
447 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
451 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
452 // we know it's there ;-)
453 WXIMPORT
char std::__throws_bad_alloc
;
456 bool wxApp::Initialize()
462 UMAInitToolbox( 4 ) ;
463 SetEventMask( everyEvent
) ;
464 UMAShowWatchCursor() ;
466 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
467 // open shared library resources from here since we don't have
468 // __wxinitialize in Mach-O shared libraries
469 wxStAppResource::OpenSharedLibraryResource(NULL
);
473 // test the minimal configuration necessary
479 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
481 error
= kMacSTRWrongMachine
;
483 else if (theMachine
< gestaltMacPlus
)
485 error
= kMacSTRWrongMachine
;
487 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
489 error
= kMacSTROldSystem
;
491 else if ( theSystem
< 0x0860 )
493 error
= kMacSTROldSystem
;
495 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
497 error
= kMacSTRSmallSize
;
503 if ( !UMAHasAppearance() )
505 error = kMacSTRNoPre8Yet ;
511 // if we encountered any problems so far, give the error code and exit immediately
515 wxStAppResource resload
;
519 GetIndString(message
, 128, error
);
520 UMAShowArrowCursor() ;
521 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
522 itemHit
= Alert(128, nil
);
527 # if __option(profile)
528 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
533 // now avoid exceptions thrown for new (bad_alloc)
534 // FIXME CS for some changes outside wxMac does not compile anymore
536 std::__throws_bad_alloc
= 0 ;
541 s_macCursorRgn
= ::NewRgn() ;
543 wxClassInfo::InitializeClasses();
546 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
550 wxPendingEventsLocker
= new wxCriticalSection
;
553 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
554 wxTheColourDatabase
->Initialize();
558 // flush the logged messages if any and install a 'safer' log target: the
559 // default one (wxLogGui) can't be used after the resources are freed just
560 // below and the user suppliedo ne might be even more unsafe (using any
561 // wxWindows GUI function is unsafe starting from now)
562 wxLog::DontCreateOnDemand();
564 // this will flush the old messages if any
565 delete wxLog::SetActiveTarget(new wxLogStderr
);
569 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
570 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
572 wxInitializeStockLists();
573 wxInitializeStockObjects();
575 #if wxUSE_WX_RESOURCES
576 wxInitializeResourceSystem();
579 wxBitmap::InitStandardHandlers();
581 wxModule::RegisterModules();
582 if (!wxModule::InitializeModules()) {
586 wxMacCreateNotifierTable() ;
588 UMAShowArrowCursor() ;
593 bool wxApp::OnInitGui()
595 if( !wxAppBase::OnInitGui() )
599 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
601 InstallApplicationEventHandler(
602 GetwxAppEventHandlerUPP(),
603 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
606 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
607 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
608 NewAEEventHandlerUPP(AEHandleODoc
) ,
610 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
611 NewAEEventHandlerUPP(AEHandleOApp
) ,
613 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
614 NewAEEventHandlerUPP(AEHandlePDoc
) ,
616 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
617 NewAEEventHandlerUPP(AEHandleQuit
) ,
620 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
621 NewAEEventHandlerProc(AEHandleODoc
) ,
623 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
624 NewAEEventHandlerProc(AEHandleOApp
) ,
626 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
627 NewAEEventHandlerProc(AEHandlePDoc
) ,
629 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
630 NewAEEventHandlerProc(AEHandleQuit
) ,
637 void wxApp::CleanUp()
639 wxToolTip::RemoveToolTips() ;
641 // flush the logged messages if any and install a 'safer' log target: the
642 // default one (wxLogGui) can't be used after the resources are freed just
643 // below and the user suppliedo ne might be even more unsafe (using any
644 // wxWindows GUI function is unsafe starting from now)
645 wxLog::DontCreateOnDemand();
647 // this will flush the old messages if any
648 delete wxLog::SetActiveTarget(new wxLogStderr
);
651 // One last chance for pending objects to be cleaned up
652 wxTheApp
->DeletePendingObjects();
654 wxModule::CleanUpModules();
656 #if wxUSE_WX_RESOURCES
657 wxCleanUpResourceSystem();
660 wxDeleteStockObjects() ;
662 // Destroy all GDI lists, etc.
663 wxDeleteStockLists();
665 delete wxTheColourDatabase
;
666 wxTheColourDatabase
= NULL
;
668 wxBitmap::CleanUpHandlers();
670 wxMacDestroyNotifierTable() ;
671 if (wxWinMacWindowList
) {
672 delete wxWinMacWindowList
;
674 if (wxWinMacControlList
) {
675 delete wxWinMacControlList
;
677 delete wxPendingEvents
;
680 delete wxPendingEventsLocker
;
681 // If we don't do the following, we get an apparent memory leak.
682 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
685 wxClassInfo::CleanUpClasses();
688 # if __option(profile)
689 ProfilerDump( "\papp.prof" ) ;
697 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
698 // At this point we want to check if there are any memory
699 // blocks that aren't part of the wxDebugContext itself,
700 // as a special case. Then when dumping we need to ignore
701 // wxDebugContext, too.
702 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
704 wxLogDebug(wxT("There were memory leaks."));
705 wxDebugContext::Dump();
706 wxDebugContext::PrintStatistics();
708 // wxDebugContext::SetStream(NULL, NULL);
712 // do it as the very last thing because everything else can log messages
713 delete wxLog::SetActiveTarget(NULL
);
716 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
717 // close shared library resources from here since we don't have
718 // __wxterminate in Mach-O shared libraries
719 wxStAppResource::CloseSharedLibraryResource();
722 UMACleanupToolbox() ;
723 if (s_macCursorRgn
) {
724 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
732 //----------------------------------------------------------------------
734 //----------------------------------------------------------------------
736 // extern variable for shared library resource id
737 // need to be able to find it with NSLookupAndBindSymbol
738 short gSharedLibraryResource
= kResFileNotOpened
;
740 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
741 CFBundleRef gSharedLibraryBundle
= NULL
;
742 #endif /* WXMAKINGDLL && __DARWIN__ */
744 wxStAppResource::wxStAppResource()
746 m_currentRefNum
= CurResFile() ;
747 if ( gSharedLibraryResource
!= kResFileNotOpened
)
749 UseResFile( gSharedLibraryResource
) ;
753 wxStAppResource::~wxStAppResource()
755 if ( m_currentRefNum
!= kResFileNotOpened
)
757 UseResFile( m_currentRefNum
) ;
761 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
763 gSharedLibraryResource
= kResFileNotOpened
;
766 if ( initBlock
!= NULL
) {
767 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
768 FSSpec
*fileSpec
= NULL
;
770 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
771 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
773 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
774 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
777 if (fileSpec
!= NULL
) {
778 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
783 // Open the shared library resource file if it is not yet open
786 const char *theLibPath
;
788 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
789 if (gSharedLibraryBundle
!= NULL
) {
790 // wxWindows has been bundled into a framework
791 // load the framework resources
793 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
796 // wxWindows is a simple dynamic shared library
797 // load the resources from the data fork of a separate resource file
802 OSErr theErr
= noErr
;
804 // get the library path
805 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
806 theModule
= NSModuleForSymbol(theSymbol
);
807 theLibPath
= NSLibraryNameForModule(theModule
);
809 wxLogDebug( wxT("wxMac library installation name is '%s'"),
812 // allocate copy to replace .dylib.* extension with .rsrc
813 theResPath
= strdup(theLibPath
);
814 if (theResPath
!= NULL
) {
815 theName
= strrchr(theResPath
, '/');
816 if (theName
== NULL
) {
817 // no directory elements in path
818 theName
= theResPath
;
820 // find ".dylib" shared library extension
821 theExt
= strstr(theName
, ".dylib");
822 // overwrite extension with ".rsrc"
823 strcpy(theExt
, ".rsrc");
825 wxLogDebug( wxT("wxMac resources file name is '%s'"),
828 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
829 if (theErr
!= noErr
) {
830 // try in current directory (using name only)
831 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
834 // open the resource file
835 if (theErr
== noErr
) {
836 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
837 &gSharedLibraryResource
);
839 if (theErr
!= noErr
) {
840 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
844 // free duplicated resource file path
848 #endif /* __DARWIN__ */
850 #endif /* WXMAKINGDLL */
853 void wxStAppResource::CloseSharedLibraryResource()
856 // Close the shared library resource file
857 if (gSharedLibraryResource
!= kResFileNotOpened
) {
859 if (gSharedLibraryBundle
!= NULL
) {
860 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
861 gSharedLibraryResource
);
862 gSharedLibraryBundle
= NULL
;
865 #endif /* __DARWIN__ */
867 CloseResFile(gSharedLibraryResource
);
869 gSharedLibraryResource
= kResFileNotOpened
;
871 #endif /* WXMAKINGDLL */
874 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
876 // for shared libraries we have to manually get the correct resource
877 // ref num upon initializing and releasing when terminating, therefore
878 // the __wxinitialize and __wxterminate must be used
881 void __sinit(void); /* (generated by linker) */
882 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
883 pascal void __terminate(void);
886 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
888 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
889 return __initialize( theInitBlock
) ;
892 pascal void __wxterminate(void)
894 wxStAppResource::CloseSharedLibraryResource() ;
898 #endif /* WXMAKINGDLL && !__DARWIN__ */
900 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
902 return wxApp::Initialize();
905 int WXDLLEXPORT
wxEntryInitGui()
907 return wxTheApp
->OnInitGui();
910 void WXDLLEXPORT
wxEntryCleanup()
915 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
918 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
919 // This seems to be necessary since there are 'rogue'
920 // objects present at this point (perhaps global objects?)
921 // Setting a checkpoint will ignore them as far as the
922 // memory checking facility is concerned.
923 // Of course you may argue that memory allocated in globals should be
924 // checked, but this is a reasonable compromise.
925 wxDebugContext::SetCheckpoint();
928 if (!wxEntryStart(argc
, argv
)) {
931 // create the application object or ensure that one already exists
934 // The app may have declared a global application object, but we recommend
935 // the IMPLEMENT_APP macro is used instead, which sets an initializer
936 // function for delayed, dynamic app object construction.
937 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
938 wxT("No initializer - use IMPLEMENT_APP macro.") );
940 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
943 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
946 // Mac OS X passes a process serial number command line argument when
947 // the application is launched from the Finder. This argument must be
948 // removed from the command line arguments before being handled by the
949 // application (otherwise applications would need to handle it)
952 if (strncmp(argv
[1], "-psn_", 5) == 0) {
953 // assume the argument is always the only one and remove it
958 argc
= 0 ; // currently we don't support files as parameters
960 // we could try to get the open apple events here to adjust argc and argv better
962 wxTheApp
->argc
= argc
;
963 wxTheApp
->argv
= argv
;
965 // GUI-specific initialization, such as creating an app context.
968 // Here frames insert themselves automatically
969 // into wxTopLevelWindows by getting created
974 if ( wxTheApp
->OnInit() )
978 retValue
= wxTheApp
->OnRun();
981 // We want to initialize, but not run or exit immediately.
984 //else: app initialization failed, so we skipped OnRun()
986 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
989 // Forcibly delete the window.
990 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
991 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
993 topWindow
->Close(TRUE
);
994 wxTheApp
->DeletePendingObjects();
999 wxTheApp
->SetTopWindow(NULL
);
1012 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1014 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1015 OSStatus err
= noErr
;
1018 switch( GetEventClass( event
) )
1020 case kEventClassKeyboard
:
1023 switch( GetEventKind(event
) )
1025 case kEventRawKeyDown
:
1026 rec
->what
= keyDown
;
1028 case kEventRawKeyRepeat
:
1029 rec
->what
= autoKey
;
1031 case kEventRawKeyUp
:
1034 case kEventRawKeyModifiersChanged
:
1035 rec
->what
= nullEvent
;
1044 unsigned char charCode
;
1046 GetMouse( &rec
->where
) ;
1048 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1049 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1050 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1051 rec
->modifiers
= modifiers
;
1052 rec
->message
= (keyCode
<< 8 ) + charCode
;
1056 case kEventClassTextInput
:
1058 switch( GetEventKind( event
) )
1060 case kEventTextInputUnicodeForKeyEvent
:
1063 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1067 unsigned char charCode
;
1069 GetMouse( &rec
->where
) ;
1070 rec
->what
= keyDown
;
1071 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1072 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1073 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1074 rec
->modifiers
= modifiers
;
1075 rec
->message
= (keyCode
<< 8 ) + charCode
;
1091 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1093 OSStatus result = eventNotHandledErr ;
1096 switch ( GetEventClass( event ) )
1098 case kEventClassKeyboard :
1099 if ( wxMacConvertEventToRecord( event , &rec ) )
1101 wxTheApp->MacHandleModifierEvents( &rec ) ;
1102 wxTheApp->MacHandleOneEvent( &rec ) ;
1106 case kEventClassTextInput :
1107 if ( wxMacConvertEventToRecord( event , &rec ) )
1109 wxTheApp->MacHandleModifierEvents( &rec ) ;
1110 wxTheApp->MacHandleOneEvent( &rec ) ;
1122 // Static member initialization
1123 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1130 #if WXWIN_COMPATIBILITY_2_2
1131 m_wantDebugOutput
= TRUE
;
1137 m_printMode
= wxPRINT_WINDOWS
;
1141 bool wxApp::Initialized()
1149 int wxApp::MainLoop()
1161 // Returns TRUE if more time is needed.
1162 bool wxApp::ProcessIdle()
1165 event
.SetEventObject(this);
1166 ProcessEvent(event
);
1168 return event
.MoreRequested();
1171 void wxApp::ExitMainLoop()
1174 if (s_inReceiveEvent
)
1175 QuitApplicationEventLoop() ;
1177 m_keepGoing
= FALSE
;
1179 m_keepGoing
= FALSE
;
1183 // Is a message/event pending?
1184 bool wxApp::Pending()
1187 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1191 return EventAvail( everyEvent
, &event
) ;
1195 // Dispatch a message.
1196 void wxApp::Dispatch()
1201 void wxApp::OnIdle(wxIdleEvent
& event
)
1203 // Avoid recursion (via ProcessEvent default case)
1210 // 'Garbage' collection of windows deleted with Close().
1211 DeletePendingObjects();
1213 // flush the logged messages if any
1214 wxLog
*pLog
= wxLog::GetActiveTarget();
1215 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1218 // Send OnIdle events to all windows
1219 bool needMore
= SendIdleEvents();
1222 event
.RequestMore(TRUE
);
1224 // If they are pending events, we must process them: pending events are
1225 // either events to the threads other than main or events posted with
1226 // wxPostEvent() functions
1227 wxMacProcessNotifierAndPendingEvents();
1237 // Send idle event to all top-level windows
1238 bool wxApp::SendIdleEvents()
1240 bool needMore
= FALSE
;
1241 wxNode
* node
= wxTopLevelWindows
.First();
1244 wxWindow
* win
= (wxWindow
*) node
->Data();
1245 if (SendIdleEvents(win
))
1248 node
= node
->Next();
1253 // Send idle event to window and all subwindows
1254 bool wxApp::SendIdleEvents(wxWindow
* win
)
1256 bool needMore
= FALSE
;
1259 event
.SetEventObject(win
);
1260 win
->ProcessEvent(event
);
1262 if (event
.MoreRequested())
1265 wxNode
* node
= win
->GetChildren().First();
1268 wxWindow
* win
= (wxWindow
*) node
->Data();
1269 if (SendIdleEvents(win
))
1272 node
= node
->Next();
1277 void wxApp::DeletePendingObjects()
1279 wxNode
*node
= wxPendingDelete
.First();
1282 wxObject
*obj
= (wxObject
*)node
->Data();
1286 if (wxPendingDelete
.Member(obj
))
1289 // Deleting one object may have deleted other pending
1290 // objects, so start from beginning of list again.
1291 node
= wxPendingDelete
.First();
1297 wxLogError(_("Fatal error: exiting"));
1303 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1306 GetTopWindow()->Close(TRUE
);
1309 // Default behaviour: close the application with prompts. The
1310 // user can veto the close, and therefore the end session.
1311 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1315 if (!GetTopWindow()->Close(!event
.CanVeto()))
1320 extern "C" void wxCYield() ;
1326 // Yield to other processes
1328 bool wxApp::Yield(bool onlyIfNeeded
)
1332 if ( !onlyIfNeeded
)
1334 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1343 YieldToAnyThread() ;
1345 // by definition yield should handle all non-processed events
1349 OSStatus status
= noErr
;
1352 s_inReceiveEvent
= true ;
1353 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1354 s_inReceiveEvent
= false ;
1356 if ( status
== eventLoopTimedOutErr
)
1358 // make sure next time the event loop will trigger idle events
1359 sleepTime
= kEventDurationNoWait
;
1361 else if ( status
== eventLoopQuitErr
)
1363 m_keepGoing
= false ;
1367 MacHandleOneEvent( theEvent
) ;
1368 ReleaseEvent(theEvent
);
1370 } while( status
== noErr
) ;
1374 // having a larger value here leads to large performance slowdowns
1375 // so we cannot give background apps more processor time here
1376 // we do so however having a large sleep value in the main event loop
1377 long sleepTime
= 0 ;
1379 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1381 MacHandleModifierEvents( &event
) ;
1382 MacHandleOneEvent( &event
);
1383 if ( event
.what
!= kHighLevelEvent
)
1384 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1386 MacHandleModifierEvents( &event
) ;
1389 wxMacProcessNotifierAndPendingEvents() ;
1395 // platform specifics
1397 void wxApp::MacSuspend( bool convertClipboard
)
1400 // we have to deactive the top level windows manually
1402 wxNode
* node
= wxTopLevelWindows
.First();
1405 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1406 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1408 node
= node
->Next();
1411 ::HideFloatingWindows() ;
1413 s_lastMouseDown
= 0 ;
1415 if( convertClipboard
)
1417 MacConvertPrivateToPublicScrap() ;
1421 extern wxList wxModalDialogs
;
1423 void wxApp::MacResume( bool convertClipboard
)
1425 s_lastMouseDown
= 0 ;
1426 if( convertClipboard
)
1428 MacConvertPublicToPrivateScrap() ;
1432 ::ShowFloatingWindows() ;
1433 // raise modal dialogs in case a non modal window was selected to activate the app
1435 wxNode
* node
= wxModalDialogs
.First();
1438 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1441 node
= node
->Next();
1446 void wxApp::MacConvertPrivateToPublicScrap()
1450 void wxApp::MacConvertPublicToPrivateScrap()
1454 void wxApp::MacDoOneEvent()
1459 s_inReceiveEvent
= true ;
1460 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1461 s_inReceiveEvent
= false ;
1462 if ( status
== eventLoopTimedOutErr
)
1464 if ( wxTheApp
->ProcessIdle() )
1465 sleepTime
= kEventDurationNoWait
;
1467 sleepTime
= kEventDurationForever
;
1469 else if ( status
== eventLoopQuitErr
)
1471 m_keepGoing
= false ;
1475 MacHandleOneEvent( theEvent
) ;
1476 ReleaseEvent(theEvent
);
1481 EventMask eventMask
= everyEvent
;
1483 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1485 MacHandleModifierEvents( &event
) ;
1486 MacHandleOneEvent( &event
);
1490 MacHandleModifierEvents( &event
) ;
1492 WindowPtr window
= ::FrontWindow() ;
1494 ::IdleControls( window
) ;
1496 if ( wxTheApp
->ProcessIdle() )
1499 sleepTime
= GetCaretTime() / 2 ;
1501 if ( event
.what
!= kHighLevelEvent
)
1502 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1506 DeletePendingObjects() ;
1507 wxMacProcessNotifierAndPendingEvents() ;
1510 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1513 EventTargetRef theTarget
;
1514 theTarget
= GetEventDispatcherTarget();
1515 m_macCurrentEvent
= evr
;
1516 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1518 EventRecord
* ev
= (EventRecord
*) evr
;
1519 m_macCurrentEvent
= ev
;
1521 wxApp::sm_lastMessageTime
= ev
->when
;
1526 MacHandleMouseDownEvent( ev
) ;
1527 if ( ev
->modifiers
& controlKey
)
1528 s_lastMouseDown
= 2;
1530 s_lastMouseDown
= 1;
1533 if ( s_lastMouseDown
== 2 )
1535 ev
->modifiers
|= controlKey
;
1539 ev
->modifiers
&= ~controlKey
;
1541 MacHandleMouseUpEvent( ev
) ;
1542 s_lastMouseDown
= 0;
1545 MacHandleActivateEvent( ev
) ;
1548 MacHandleUpdateEvent( ev
) ;
1552 MacHandleKeyDownEvent( ev
) ;
1555 MacHandleKeyUpEvent( ev
) ;
1558 MacHandleDiskEvent( ev
) ;
1561 MacHandleOSEvent( ev
) ;
1563 case kHighLevelEvent
:
1564 MacHandleHighLevelEvent( ev
) ;
1570 wxMacProcessNotifierAndPendingEvents() ;
1574 bool s_macIsInModalLoop
= false ;
1576 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1578 EventRecord
* ev
= (EventRecord
*) evr
;
1579 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1581 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1583 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1584 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1585 event
.m_altDown
= ev
->modifiers
& optionKey
;
1586 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1588 event
.m_x
= ev
->where
.h
;
1589 event
.m_y
= ev
->where
.v
;
1590 event
.m_timeStamp
= ev
->when
;
1591 wxWindow
* focus
= wxWindow::FindFocus() ;
1592 event
.SetEventObject(focus
);
1594 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1596 event
.m_keyCode
= WXK_CONTROL
;
1597 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1598 focus
->GetEventHandler()->ProcessEvent( event
) ;
1600 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1602 event
.m_keyCode
= WXK_SHIFT
;
1603 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1604 focus
->GetEventHandler()->ProcessEvent( event
) ;
1606 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1608 event
.m_keyCode
= WXK_ALT
;
1609 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1610 focus
->GetEventHandler()->ProcessEvent( event
) ;
1612 s_lastModifiers
= ev
->modifiers
;
1616 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1618 // we must avoid reentrancy problems when processing high level events eg printing
1619 bool former
= s_inYield
;
1621 EventRecord
* ev
= (EventRecord
*) evr
;
1622 ::AEProcessAppleEvent( ev
) ;
1623 s_inYield
= former
;
1626 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1628 EventRecord
* ev
= (EventRecord
*) evr
;
1629 wxToolTip::RemoveToolTips() ;
1632 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1633 WindowAttributes frontWindowAttributes
= NULL
;
1635 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1637 short windowPart
= ::FindWindow(ev
->where
, &window
);
1638 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1639 if ( wxPendingDelete
.Member(win
) )
1643 GetQDGlobalsScreenBits( &screenBits
);
1648 if ( s_macIsInModalLoop
)
1654 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1655 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1656 s_lastMouseDown
= 0;
1660 SystemClick( ev
, window
) ;
1661 s_lastMouseDown
= 0;
1664 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1670 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1675 Point pt
= { 0, 0 } ;
1676 SetPortWindowPort(window
) ;
1677 LocalToGlobal( &pt
) ;
1679 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1680 -1 , wxSIZE_USE_EXISTING
);
1682 s_lastMouseDown
= 0;
1686 if (TrackGoAway(window
, ev
->where
))
1691 s_lastMouseDown
= 0;
1695 Rect newContentRect
;
1696 Rect constraintRect
;
1697 constraintRect
.top
= win
->GetMinHeight() ;
1698 if ( constraintRect
.top
== -1 )
1699 constraintRect
.top
= 0 ;
1700 constraintRect
.left
= win
->GetMinWidth() ;
1701 if ( constraintRect
.left
== -1 )
1702 constraintRect
.left
= 0 ;
1703 constraintRect
.right
= win
->GetMaxWidth() ;
1704 if ( constraintRect
.right
== -1 )
1705 constraintRect
.right
= 32000 ;
1706 constraintRect
.bottom
= win
->GetMaxHeight() ;
1707 if ( constraintRect
.bottom
== -1 )
1708 constraintRect
.bottom
= 32000 ;
1710 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1711 &constraintRect
, &newContentRect
) ;
1714 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1715 newContentRect
.right
- newContentRect
.left
,
1716 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1718 s_lastMouseDown
= 0;
1723 if (TrackBox(window
, ev
->where
, windowPart
))
1725 // TODO setup size event
1726 ZoomWindow( window
, windowPart
, false ) ;
1732 Point pt
= { 0, 0 } ;
1733 SetPortWindowPort(window
) ;
1734 LocalToGlobal( &pt
) ;
1737 GetWindowPortBounds(window
, &tempRect
) ;
1738 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1739 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1742 s_lastMouseDown
= 0;
1744 case inCollapseBox
:
1745 // TODO setup size event
1746 s_lastMouseDown
= 0;
1753 SetPortWindowPort(window
) ;
1756 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1758 if ( s_macIsInModalLoop
)
1762 else if ( UMAIsWindowFloating( window
) )
1765 win
->MacMouseDown( ev
, windowPart
) ;
1770 win
->MacMouseDown( ev
, windowPart
) ;
1771 ::SelectWindow( window
) ;
1777 win
->MacMouseDown( ev
, windowPart
) ;
1785 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1787 EventRecord
* ev
= (EventRecord
*) evr
;
1790 short windowPart
= inNoWindow
;
1791 if ( wxTheApp
->s_captureWindow
)
1793 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1794 windowPart
= inContent
;
1798 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1809 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1811 win
->MacMouseUp( ev
, windowPart
) ;
1819 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1820 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1825 case kHomeCharCode
:
1828 case kEnterCharCode
:
1829 retval
= WXK_RETURN
;
1834 case kHelpCharCode
:
1837 case kBackspaceCharCode
:
1843 case kPageUpCharCode
:
1844 retval
= WXK_PAGEUP
;
1846 case kPageDownCharCode
:
1847 retval
= WXK_PAGEDOWN
;
1849 case kReturnCharCode
:
1850 retval
= WXK_RETURN
;
1852 case kFunctionKeyCharCode
:
1904 case kEscapeCharCode
:
1905 retval
= WXK_ESCAPE
;
1907 case kLeftArrowCharCode
:
1910 case kRightArrowCharCode
:
1911 retval
= WXK_RIGHT
;
1913 case kUpArrowCharCode
:
1916 case kDownArrowCharCode
:
1919 case kDeleteCharCode
:
1920 retval
= WXK_DELETE
;
1929 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1931 EventRecord
* ev
= (EventRecord
*) evr
;
1932 wxToolTip::RemoveToolTips() ;
1934 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1935 if ( HiWord( menuresult
) )
1937 if ( !s_macIsInModalLoop
)
1938 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1942 wxWindow
* focus
= wxWindow::FindFocus() ;
1944 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1946 // has not been handled -> perform default
1947 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1948 if ( control
&& control
->GetMacControl() != NULL
)
1952 keychar
= short(ev
->message
& charCodeMask
);
1953 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1954 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1960 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1962 EventRecord
* ev
= (EventRecord
*) evr
;
1963 wxToolTip::RemoveToolTips() ;
1965 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1966 if ( HiWord( menuresult
) )
1971 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1977 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1984 keychar
= short(keymessage
& charCodeMask
);
1985 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1987 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1989 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1990 // and look at the character after
1992 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1993 keychar
= short(keyInfo
& charCodeMask
);
1994 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1996 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1997 long realkeyval
= keyval
;
1998 if ( keyval
== keychar
)
2000 // 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)
2001 realkeyval
= short(keymessage
& charCodeMask
) ;
2002 keyval
= wxToupper( keyval
) ;
2005 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2006 bool handled
= false ;
2007 event
.m_shiftDown
= modifiers
& shiftKey
;
2008 event
.m_controlDown
= modifiers
& controlKey
;
2009 event
.m_altDown
= modifiers
& optionKey
;
2010 event
.m_metaDown
= modifiers
& cmdKey
;
2011 event
.m_keyCode
= keyval
;
2015 event
.m_timeStamp
= when
;
2016 event
.SetEventObject(focus
);
2017 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2018 if ( handled
&& event
.GetSkipped() )
2025 wxWindow
*ancestor
= focus
;
2028 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2031 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2032 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2035 if (ancestor
->IsTopLevel())
2037 ancestor
= ancestor
->GetParent();
2040 #endif // wxUSE_ACCEL
2044 event
.Skip( FALSE
) ;
2045 event
.SetEventType( wxEVT_CHAR
) ;
2047 event
.m_keyCode
= realkeyval
;
2049 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2050 if ( handled
&& event
.GetSkipped() )
2054 (keyval
== WXK_TAB
) &&
2055 // CS: copied the change below from wxGTK
2056 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2057 // have this style, yet choose not to process this particular TAB in which
2058 // case TAB must still work as a navigational character
2060 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2062 (focus
->GetParent()) &&
2063 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2065 wxNavigationKeyEvent new_event
;
2066 new_event
.SetEventObject( focus
);
2067 new_event
.SetDirection( !event
.ShiftDown() );
2068 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2069 new_event
.SetWindowChange( event
.ControlDown() );
2070 new_event
.SetCurrentFocus( focus
);
2071 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2072 if ( handled
&& new_event
.GetSkipped() )
2075 // backdoor handler for default return and command escape
2076 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2078 // if window is not having a focus still testing for default enter or cancel
2079 // TODO add the UMA version for ActiveNonFloatingWindow
2080 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2083 if ( keyval
== WXK_RETURN
)
2085 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2087 if ( def
&& def
->IsEnabled() )
2089 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2090 event
.SetEventObject(def
);
2091 def
->Command(event
);
2095 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2096 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2098 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2099 new_event
.SetEventObject( focus
);
2100 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2107 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2114 keychar
= short(keymessage
& charCodeMask
);
2115 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2116 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2118 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2119 // and look at the character after
2121 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2122 keychar
= short(keyInfo
& charCodeMask
);
2123 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2125 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2127 if ( keyval
== keychar
)
2129 keyval
= wxToupper( keyval
) ;
2131 bool handled
= false ;
2133 wxKeyEvent
event(wxEVT_KEY_UP
);
2134 event
.m_shiftDown
= modifiers
& shiftKey
;
2135 event
.m_controlDown
= modifiers
& controlKey
;
2136 event
.m_altDown
= modifiers
& optionKey
;
2137 event
.m_metaDown
= modifiers
& cmdKey
;
2138 event
.m_keyCode
= keyval
;
2142 event
.m_timeStamp
= when
;
2143 event
.SetEventObject(focus
);
2144 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2150 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2152 EventRecord
* ev
= (EventRecord
*) evr
;
2153 WindowRef window
= (WindowRef
) ev
->message
;
2156 bool activate
= (ev
->modifiers
& activeFlag
) ;
2157 WindowClass wclass
;
2158 ::GetWindowClass ( window
, &wclass
) ;
2159 if ( wclass
== kFloatingWindowClass
)
2161 // if it is a floater we activate/deactivate the front non-floating window instead
2162 window
= ::FrontNonFloatingWindow() ;
2164 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2166 win
->MacActivate( ev
->when
, activate
) ;
2170 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2172 EventRecord
* ev
= (EventRecord
*) evr
;
2173 WindowRef window
= (WindowRef
) ev
->message
;
2174 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2177 if ( !wxPendingDelete
.Member(win
) )
2178 win
->MacUpdate( ev
->when
) ;
2182 // since there is no way of telling this foreign window to update itself
2183 // we have to invalidate the update region otherwise we keep getting the same
2184 // event over and over again
2185 BeginUpdate( window
) ;
2186 EndUpdate( window
) ;
2190 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2192 EventRecord
* ev
= (EventRecord
*) evr
;
2193 if ( HiWord( ev
->message
) != noErr
)
2197 SetPt( &point
, 100 , 100 ) ;
2199 err
= DIBadMount( point
, ev
->message
) ;
2200 wxASSERT( err
== noErr
) ;
2204 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2206 EventRecord
* ev
= (EventRecord
*) evr
;
2207 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2209 case suspendResumeMessage
:
2211 bool isResuming
= ev
->message
& resumeFlag
;
2212 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2214 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2217 WindowRef oldFrontWindow
= NULL
;
2218 WindowRef newFrontWindow
= NULL
;
2220 // in case we don't take care of activating ourselves, we have to synchronize
2221 // our idea of the active window with the process manager's - which it already activated
2223 if ( !doesActivate
)
2224 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2226 MacResume( convertClipboard
) ;
2228 newFrontWindow
= ::FrontNonFloatingWindow() ;
2230 if ( oldFrontWindow
)
2232 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2234 win
->MacActivate( ev
->when
, false ) ;
2236 if ( newFrontWindow
)
2238 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2240 win
->MacActivate( ev
->when
, true ) ;
2245 MacSuspend( convertClipboard
) ;
2249 case mouseMovedMessage
:
2253 wxWindow
* currentMouseWindow
= NULL
;
2255 if (s_captureWindow
)
2257 currentMouseWindow
= s_captureWindow
;
2261 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2262 ¤tMouseWindow
) ;
2265 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2267 wxMouseEvent event
;
2269 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2270 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2272 event
.m_leftDown
= isDown
&& !controlDown
;
2273 event
.m_middleDown
= FALSE
;
2274 event
.m_rightDown
= isDown
&& controlDown
;
2275 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2276 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2277 event
.m_altDown
= ev
->modifiers
& optionKey
;
2278 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2279 event
.m_x
= ev
->where
.h
;
2280 event
.m_y
= ev
->where
.v
;
2281 event
.m_timeStamp
= ev
->when
;
2282 event
.SetEventObject(this);
2284 if ( wxWindow::s_lastMouseWindow
)
2286 wxMouseEvent
eventleave(event
);
2287 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2288 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2289 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2291 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2293 if ( currentMouseWindow
)
2295 wxMouseEvent
evententer(event
);
2296 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2297 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2298 evententer
.SetEventObject( currentMouseWindow
) ;
2299 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2301 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2304 short windowPart
= inNoWindow
;
2306 if ( s_captureWindow
)
2308 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2309 windowPart
= inContent
;
2313 windowPart
= ::FindWindow(ev
->where
, &window
);
2320 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2322 win
->MacMouseMoved( ev
, windowPart
) ;
2329 UMAShowArrowCursor();
2339 UMAShowArrowCursor();
2350 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2352 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2353 wxMenu
* menu
= NULL
;
2354 wxMenuItem
* item
= NULL
;
2357 item
= mbar
->FindItem( id
, &menu
) ;
2359 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2361 if (item
->IsCheckable())
2363 item
->Check( !item
->IsChecked() ) ;
2366 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2370 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2373 return; // no menu item selected
2375 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2378 Str255 deskAccessoryName
;
2381 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2382 GetPort(&savedPort
);
2383 OpenDeskAcc(deskAccessoryName
);
2390 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2391 MacHandleMenuCommand( id
) ;