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 { kEventClassMouse
, kEventMouseDown
} ,
305 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
307 OSStatus result
= eventNotHandledErr
;
308 UInt32 kind
= GetEventKind( event
) ;
313 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
314 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
315 // deal with clicks in the menu bar explicitely
317 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
319 OSStatus result
= eventNotHandledErr
;
321 switch( GetEventKind(event
) )
323 case kEventMouseDown
:
328 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
329 sizeof( Point
), NULL
, &point
);
330 short windowPart
= ::FindWindow(point
, &window
);
332 if ( windowPart
== inMenuBar
)
334 MenuSelect( point
) ;
344 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
346 OSStatus result
= eventNotHandledErr
;
350 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
351 sizeof( HICommand
), NULL
, &command
);
353 MenuCommand id
= command
.commandID
;
354 if ( id
== kHICommandPreferences
)
355 id
= wxApp::s_macPreferencesMenuItemId
;
357 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
358 wxMenu
* menu
= NULL
;
359 wxMenuItem
* item
= NULL
;
362 item
= mbar
->FindItem( id
, &menu
) ;
364 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
367 switch( GetEventKind( event
) )
369 case kEventProcessCommand
:
371 if (item
->IsCheckable())
373 item
->Check( !item
->IsChecked() ) ;
376 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
380 case kEventCommandUpdateStatus
:
381 // eventually trigger an updateui round
391 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
393 OSStatus result
= eventNotHandledErr
;
394 switch ( GetEventKind( event
) )
396 case kEventAppActivated
:
398 wxTheApp
->MacResume( true ) ;
402 case kEventAppDeactivated
:
404 wxTheApp
->MacSuspend( true ) ;
414 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
416 OSStatus result
= eventNotHandledErr
;
417 switch( GetEventClass( event
) )
419 case kEventClassCommand
:
420 result
= CommandEventHandler( handler
, event
, data
) ;
422 case kEventClassApplication
:
423 result
= ApplicationEventHandler( handler
, event
, data
) ;
425 case kEventClassMenu
:
426 result
= MenuEventHandler( handler
, event
, data
) ;
428 case kEventClassMouse
:
429 result
= MouseEventHandler( handler
, event
, data
) ;
438 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
442 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
443 // we know it's there ;-)
444 WXIMPORT
char std::__throws_bad_alloc
;
447 bool wxApp::Initialize()
453 UMAInitToolbox( 4 ) ;
454 SetEventMask( everyEvent
) ;
455 UMAShowWatchCursor() ;
457 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
458 // open shared library resources from here since we don't have
459 // __wxinitialize in Mach-O shared libraries
460 wxStAppResource::OpenSharedLibraryResource(NULL
);
464 // test the minimal configuration necessary
470 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
472 error
= kMacSTRWrongMachine
;
474 else if (theMachine
< gestaltMacPlus
)
476 error
= kMacSTRWrongMachine
;
478 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
480 error
= kMacSTROldSystem
;
482 else if ( theSystem
< 0x0860 )
484 error
= kMacSTROldSystem
;
486 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
488 error
= kMacSTRSmallSize
;
494 if ( !UMAHasAppearance() )
496 error = kMacSTRNoPre8Yet ;
502 // if we encountered any problems so far, give the error code and exit immediately
506 wxStAppResource resload
;
510 GetIndString(message
, 128, error
);
511 UMAShowArrowCursor() ;
512 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
513 itemHit
= Alert(128, nil
);
518 # if __option(profile)
519 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
524 // now avoid exceptions thrown for new (bad_alloc)
525 // FIXME CS for some changes outside wxMac does not compile anymore
527 std::__throws_bad_alloc
= 0 ;
532 s_macCursorRgn
= ::NewRgn() ;
534 wxClassInfo::InitializeClasses();
537 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
541 wxPendingEventsLocker
= new wxCriticalSection
;
544 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
545 wxTheColourDatabase
->Initialize();
549 // flush the logged messages if any and install a 'safer' log target: the
550 // default one (wxLogGui) can't be used after the resources are freed just
551 // below and the user suppliedo ne might be even more unsafe (using any
552 // wxWindows GUI function is unsafe starting from now)
553 wxLog::DontCreateOnDemand();
555 // this will flush the old messages if any
556 delete wxLog::SetActiveTarget(new wxLogStderr
);
560 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
561 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
563 wxInitializeStockLists();
564 wxInitializeStockObjects();
566 #if wxUSE_WX_RESOURCES
567 wxInitializeResourceSystem();
570 wxBitmap::InitStandardHandlers();
572 wxModule::RegisterModules();
573 if (!wxModule::InitializeModules()) {
577 wxMacCreateNotifierTable() ;
579 UMAShowArrowCursor() ;
584 bool wxApp::OnInitGui()
586 if( !wxAppBase::OnInitGui() )
590 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
592 InstallApplicationEventHandler(
593 GetwxAppEventHandlerUPP(),
594 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
597 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
598 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
599 NewAEEventHandlerUPP(AEHandleODoc
) ,
601 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
602 NewAEEventHandlerUPP(AEHandleOApp
) ,
604 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
605 NewAEEventHandlerUPP(AEHandlePDoc
) ,
607 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
608 NewAEEventHandlerUPP(AEHandleQuit
) ,
611 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
612 NewAEEventHandlerProc(AEHandleODoc
) ,
614 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
615 NewAEEventHandlerProc(AEHandleOApp
) ,
617 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
618 NewAEEventHandlerProc(AEHandlePDoc
) ,
620 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
621 NewAEEventHandlerProc(AEHandleQuit
) ,
628 void wxApp::CleanUp()
630 wxToolTip::RemoveToolTips() ;
632 // flush the logged messages if any and install a 'safer' log target: the
633 // default one (wxLogGui) can't be used after the resources are freed just
634 // below and the user suppliedo ne might be even more unsafe (using any
635 // wxWindows GUI function is unsafe starting from now)
636 wxLog::DontCreateOnDemand();
638 // this will flush the old messages if any
639 delete wxLog::SetActiveTarget(new wxLogStderr
);
642 // One last chance for pending objects to be cleaned up
643 wxTheApp
->DeletePendingObjects();
645 wxModule::CleanUpModules();
647 #if wxUSE_WX_RESOURCES
648 wxCleanUpResourceSystem();
651 wxDeleteStockObjects() ;
653 // Destroy all GDI lists, etc.
654 wxDeleteStockLists();
656 delete wxTheColourDatabase
;
657 wxTheColourDatabase
= NULL
;
659 wxBitmap::CleanUpHandlers();
661 wxMacDestroyNotifierTable() ;
662 if (wxWinMacWindowList
) {
663 delete wxWinMacWindowList
;
665 if (wxWinMacControlList
) {
666 delete wxWinMacControlList
;
668 delete wxPendingEvents
;
671 delete wxPendingEventsLocker
;
672 // If we don't do the following, we get an apparent memory leak.
673 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
676 wxClassInfo::CleanUpClasses();
679 # if __option(profile)
680 ProfilerDump( "\papp.prof" ) ;
688 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
689 // At this point we want to check if there are any memory
690 // blocks that aren't part of the wxDebugContext itself,
691 // as a special case. Then when dumping we need to ignore
692 // wxDebugContext, too.
693 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
695 wxLogDebug(wxT("There were memory leaks."));
696 wxDebugContext::Dump();
697 wxDebugContext::PrintStatistics();
699 // wxDebugContext::SetStream(NULL, NULL);
703 // do it as the very last thing because everything else can log messages
704 delete wxLog::SetActiveTarget(NULL
);
707 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
708 // close shared library resources from here since we don't have
709 // __wxterminate in Mach-O shared libraries
710 wxStAppResource::CloseSharedLibraryResource();
713 UMACleanupToolbox() ;
714 if (s_macCursorRgn
) {
715 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
723 //----------------------------------------------------------------------
725 //----------------------------------------------------------------------
727 // extern variable for shared library resource id
728 // need to be able to find it with NSLookupAndBindSymbol
729 short gSharedLibraryResource
= kResFileNotOpened
;
731 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
732 CFBundleRef gSharedLibraryBundle
= NULL
;
733 #endif /* WXMAKINGDLL && __DARWIN__ */
735 wxStAppResource::wxStAppResource()
737 m_currentRefNum
= CurResFile() ;
738 if ( gSharedLibraryResource
!= kResFileNotOpened
)
740 UseResFile( gSharedLibraryResource
) ;
744 wxStAppResource::~wxStAppResource()
746 if ( m_currentRefNum
!= kResFileNotOpened
)
748 UseResFile( m_currentRefNum
) ;
752 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
754 gSharedLibraryResource
= kResFileNotOpened
;
757 if ( initBlock
!= NULL
) {
758 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
759 FSSpec
*fileSpec
= NULL
;
761 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
762 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
764 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
765 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
768 if (fileSpec
!= NULL
) {
769 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
774 // Open the shared library resource file if it is not yet open
777 const char *theLibPath
;
779 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
780 if (gSharedLibraryBundle
!= NULL
) {
781 // wxWindows has been bundled into a framework
782 // load the framework resources
784 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
787 // wxWindows is a simple dynamic shared library
788 // load the resources from the data fork of a separate resource file
793 OSErr theErr
= noErr
;
795 // get the library path
796 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
797 theModule
= NSModuleForSymbol(theSymbol
);
798 theLibPath
= NSLibraryNameForModule(theModule
);
800 wxLogDebug( wxT("wxMac library installation name is '%s'"),
803 // allocate copy to replace .dylib.* extension with .rsrc
804 theResPath
= strdup(theLibPath
);
805 if (theResPath
!= NULL
) {
806 theName
= strrchr(theResPath
, '/');
807 if (theName
== NULL
) {
808 // no directory elements in path
809 theName
= theResPath
;
811 // find ".dylib" shared library extension
812 theExt
= strstr(theName
, ".dylib");
813 // overwrite extension with ".rsrc"
814 strcpy(theExt
, ".rsrc");
816 wxLogDebug( wxT("wxMac resources file name is '%s'"),
819 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
820 if (theErr
!= noErr
) {
821 // try in current directory (using name only)
822 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
825 // open the resource file
826 if (theErr
== noErr
) {
827 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
828 &gSharedLibraryResource
);
830 if (theErr
!= noErr
) {
831 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
835 // free duplicated resource file path
839 #endif /* __DARWIN__ */
841 #endif /* WXMAKINGDLL */
844 void wxStAppResource::CloseSharedLibraryResource()
847 // Close the shared library resource file
848 if (gSharedLibraryResource
!= kResFileNotOpened
) {
850 if (gSharedLibraryBundle
!= NULL
) {
851 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
852 gSharedLibraryResource
);
853 gSharedLibraryBundle
= NULL
;
856 #endif /* __DARWIN__ */
858 CloseResFile(gSharedLibraryResource
);
860 gSharedLibraryResource
= kResFileNotOpened
;
862 #endif /* WXMAKINGDLL */
865 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
867 // for shared libraries we have to manually get the correct resource
868 // ref num upon initializing and releasing when terminating, therefore
869 // the __wxinitialize and __wxterminate must be used
872 void __sinit(void); /* (generated by linker) */
873 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
874 pascal void __terminate(void);
877 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
879 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
880 return __initialize( theInitBlock
) ;
883 pascal void __wxterminate(void)
885 wxStAppResource::CloseSharedLibraryResource() ;
889 #endif /* WXMAKINGDLL && !__DARWIN__ */
891 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
893 return wxApp::Initialize();
896 int WXDLLEXPORT
wxEntryInitGui()
898 return wxTheApp
->OnInitGui();
901 void WXDLLEXPORT
wxEntryCleanup()
906 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
909 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
910 // This seems to be necessary since there are 'rogue'
911 // objects present at this point (perhaps global objects?)
912 // Setting a checkpoint will ignore them as far as the
913 // memory checking facility is concerned.
914 // Of course you may argue that memory allocated in globals should be
915 // checked, but this is a reasonable compromise.
916 wxDebugContext::SetCheckpoint();
919 if (!wxEntryStart(argc
, argv
)) {
922 // create the application object or ensure that one already exists
925 // The app may have declared a global application object, but we recommend
926 // the IMPLEMENT_APP macro is used instead, which sets an initializer
927 // function for delayed, dynamic app object construction.
928 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
929 wxT("No initializer - use IMPLEMENT_APP macro.") );
931 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
934 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
937 // Mac OS X passes a process serial number command line argument when
938 // the application is launched from the Finder. This argument must be
939 // removed from the command line arguments before being handled by the
940 // application (otherwise applications would need to handle it)
943 if (strncmp(argv
[1], "-psn_", 5) == 0) {
944 // assume the argument is always the only one and remove it
949 argc
= 0 ; // currently we don't support files as parameters
951 // we could try to get the open apple events here to adjust argc and argv better
953 wxTheApp
->argc
= argc
;
954 wxTheApp
->argv
= argv
;
956 // GUI-specific initialization, such as creating an app context.
959 // Here frames insert themselves automatically
960 // into wxTopLevelWindows by getting created
965 if ( wxTheApp
->OnInit() )
969 retValue
= wxTheApp
->OnRun();
972 // We want to initialize, but not run or exit immediately.
975 //else: app initialization failed, so we skipped OnRun()
977 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
980 // Forcibly delete the window.
981 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
982 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
984 topWindow
->Close(TRUE
);
985 wxTheApp
->DeletePendingObjects();
990 wxTheApp
->SetTopWindow(NULL
);
1003 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1005 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1006 OSStatus err
= noErr
;
1009 switch( GetEventClass( event
) )
1011 case kEventClassKeyboard
:
1014 switch( GetEventKind(event
) )
1016 case kEventRawKeyDown
:
1017 rec
->what
= keyDown
;
1019 case kEventRawKeyRepeat
:
1020 rec
->what
= autoKey
;
1022 case kEventRawKeyUp
:
1025 case kEventRawKeyModifiersChanged
:
1026 rec
->what
= nullEvent
;
1035 unsigned char charCode
;
1037 GetMouse( &rec
->where
) ;
1039 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1040 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1041 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1042 rec
->modifiers
= modifiers
;
1043 rec
->message
= (keyCode
<< 8 ) + charCode
;
1047 case kEventClassTextInput
:
1049 switch( GetEventKind( event
) )
1051 case kEventTextInputUnicodeForKeyEvent
:
1054 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1058 unsigned char charCode
;
1060 GetMouse( &rec
->where
) ;
1061 rec
->what
= keyDown
;
1062 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1063 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1064 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1065 rec
->modifiers
= modifiers
;
1066 rec
->message
= (keyCode
<< 8 ) + charCode
;
1082 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1084 OSStatus result = eventNotHandledErr ;
1087 switch ( GetEventClass( event ) )
1089 case kEventClassKeyboard :
1090 if ( wxMacConvertEventToRecord( event , &rec ) )
1092 wxTheApp->MacHandleModifierEvents( &rec ) ;
1093 wxTheApp->MacHandleOneEvent( &rec ) ;
1097 case kEventClassTextInput :
1098 if ( wxMacConvertEventToRecord( event , &rec ) )
1100 wxTheApp->MacHandleModifierEvents( &rec ) ;
1101 wxTheApp->MacHandleOneEvent( &rec ) ;
1113 // Static member initialization
1114 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1121 #if WXWIN_COMPATIBILITY_2_2
1122 m_wantDebugOutput
= TRUE
;
1128 m_printMode
= wxPRINT_WINDOWS
;
1132 bool wxApp::Initialized()
1140 int wxApp::MainLoop()
1152 // Returns TRUE if more time is needed.
1153 bool wxApp::ProcessIdle()
1156 event
.SetEventObject(this);
1157 ProcessEvent(event
);
1159 return event
.MoreRequested();
1162 void wxApp::ExitMainLoop()
1165 if (s_inReceiveEvent
)
1166 QuitApplicationEventLoop() ;
1168 m_keepGoing
= FALSE
;
1170 m_keepGoing
= FALSE
;
1174 // Is a message/event pending?
1175 bool wxApp::Pending()
1178 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1182 return EventAvail( everyEvent
, &event
) ;
1186 // Dispatch a message.
1187 void wxApp::Dispatch()
1192 void wxApp::OnIdle(wxIdleEvent
& event
)
1194 // Avoid recursion (via ProcessEvent default case)
1201 // 'Garbage' collection of windows deleted with Close().
1202 DeletePendingObjects();
1204 // flush the logged messages if any
1205 wxLog
*pLog
= wxLog::GetActiveTarget();
1206 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1209 // Send OnIdle events to all windows
1210 bool needMore
= SendIdleEvents();
1213 event
.RequestMore(TRUE
);
1215 // If they are pending events, we must process them: pending events are
1216 // either events to the threads other than main or events posted with
1217 // wxPostEvent() functions
1218 wxMacProcessNotifierAndPendingEvents();
1228 // Send idle event to all top-level windows
1229 bool wxApp::SendIdleEvents()
1231 bool needMore
= FALSE
;
1232 wxNode
* node
= wxTopLevelWindows
.First();
1235 wxWindow
* win
= (wxWindow
*) node
->Data();
1236 if (SendIdleEvents(win
))
1239 node
= node
->Next();
1244 // Send idle event to window and all subwindows
1245 bool wxApp::SendIdleEvents(wxWindow
* win
)
1247 bool needMore
= FALSE
;
1250 event
.SetEventObject(win
);
1251 win
->ProcessEvent(event
);
1253 if (event
.MoreRequested())
1256 wxNode
* node
= win
->GetChildren().First();
1259 wxWindow
* win
= (wxWindow
*) node
->Data();
1260 if (SendIdleEvents(win
))
1263 node
= node
->Next();
1268 void wxApp::DeletePendingObjects()
1270 wxNode
*node
= wxPendingDelete
.First();
1273 wxObject
*obj
= (wxObject
*)node
->Data();
1277 if (wxPendingDelete
.Member(obj
))
1280 // Deleting one object may have deleted other pending
1281 // objects, so start from beginning of list again.
1282 node
= wxPendingDelete
.First();
1288 wxLogError(_("Fatal error: exiting"));
1294 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1297 GetTopWindow()->Close(TRUE
);
1300 // Default behaviour: close the application with prompts. The
1301 // user can veto the close, and therefore the end session.
1302 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1306 if (!GetTopWindow()->Close(!event
.CanVeto()))
1311 extern "C" void wxCYield() ;
1317 // Yield to other processes
1319 bool wxApp::Yield(bool onlyIfNeeded
)
1323 if ( !onlyIfNeeded
)
1325 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1334 YieldToAnyThread() ;
1336 // by definition yield should handle all non-processed events
1340 OSStatus status
= noErr
;
1343 s_inReceiveEvent
= true ;
1344 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1345 s_inReceiveEvent
= false ;
1347 if ( status
== eventLoopTimedOutErr
)
1349 // make sure next time the event loop will trigger idle events
1350 sleepTime
= kEventDurationNoWait
;
1352 else if ( status
== eventLoopQuitErr
)
1354 m_keepGoing
= false ;
1358 MacHandleOneEvent( theEvent
) ;
1359 ReleaseEvent(theEvent
);
1361 } while( status
== noErr
) ;
1365 // having a larger value here leads to large performance slowdowns
1366 // so we cannot give background apps more processor time here
1367 // we do so however having a large sleep value in the main event loop
1368 long sleepTime
= 0 ;
1370 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1372 MacHandleModifierEvents( &event
) ;
1373 MacHandleOneEvent( &event
);
1374 if ( event
.what
!= kHighLevelEvent
)
1375 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1377 MacHandleModifierEvents( &event
) ;
1380 wxMacProcessNotifierAndPendingEvents() ;
1386 // platform specifics
1388 void wxApp::MacSuspend( bool convertClipboard
)
1391 // we have to deactive the top level windows manually
1393 wxNode
* node
= wxTopLevelWindows
.First();
1396 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1397 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1399 node
= node
->Next();
1402 ::HideFloatingWindows() ;
1404 s_lastMouseDown
= 0 ;
1406 if( convertClipboard
)
1408 MacConvertPrivateToPublicScrap() ;
1412 extern wxList wxModalDialogs
;
1414 void wxApp::MacResume( bool convertClipboard
)
1416 s_lastMouseDown
= 0 ;
1417 if( convertClipboard
)
1419 MacConvertPublicToPrivateScrap() ;
1423 ::ShowFloatingWindows() ;
1424 // raise modal dialogs in case a non modal window was selected to activate the app
1426 wxNode
* node
= wxModalDialogs
.First();
1429 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1432 node
= node
->Next();
1437 void wxApp::MacConvertPrivateToPublicScrap()
1441 void wxApp::MacConvertPublicToPrivateScrap()
1445 void wxApp::MacDoOneEvent()
1450 s_inReceiveEvent
= true ;
1451 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1452 s_inReceiveEvent
= false ;
1453 if ( status
== eventLoopTimedOutErr
)
1455 if ( wxTheApp
->ProcessIdle() )
1456 sleepTime
= kEventDurationNoWait
;
1458 sleepTime
= kEventDurationForever
;
1460 else if ( status
== eventLoopQuitErr
)
1462 m_keepGoing
= false ;
1466 MacHandleOneEvent( theEvent
) ;
1467 ReleaseEvent(theEvent
);
1472 EventMask eventMask
= everyEvent
;
1474 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1476 MacHandleModifierEvents( &event
) ;
1477 MacHandleOneEvent( &event
);
1481 MacHandleModifierEvents( &event
) ;
1483 WindowPtr window
= ::FrontWindow() ;
1485 ::IdleControls( window
) ;
1487 if ( wxTheApp
->ProcessIdle() )
1490 sleepTime
= GetCaretTime() / 2 ;
1492 if ( event
.what
!= kHighLevelEvent
)
1493 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1497 DeletePendingObjects() ;
1498 wxMacProcessNotifierAndPendingEvents() ;
1501 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1504 EventTargetRef theTarget
;
1505 theTarget
= GetEventDispatcherTarget();
1506 m_macCurrentEvent
= evr
;
1507 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1509 EventRecord
* ev
= (EventRecord
*) evr
;
1510 m_macCurrentEvent
= ev
;
1512 wxApp::sm_lastMessageTime
= ev
->when
;
1517 MacHandleMouseDownEvent( ev
) ;
1518 if ( ev
->modifiers
& controlKey
)
1519 s_lastMouseDown
= 2;
1521 s_lastMouseDown
= 1;
1524 if ( s_lastMouseDown
== 2 )
1526 ev
->modifiers
|= controlKey
;
1530 ev
->modifiers
&= ~controlKey
;
1532 MacHandleMouseUpEvent( ev
) ;
1533 s_lastMouseDown
= 0;
1536 MacHandleActivateEvent( ev
) ;
1539 MacHandleUpdateEvent( ev
) ;
1543 MacHandleKeyDownEvent( ev
) ;
1546 MacHandleKeyUpEvent( ev
) ;
1549 MacHandleDiskEvent( ev
) ;
1552 MacHandleOSEvent( ev
) ;
1554 case kHighLevelEvent
:
1555 MacHandleHighLevelEvent( ev
) ;
1561 wxMacProcessNotifierAndPendingEvents() ;
1565 bool s_macIsInModalLoop
= false ;
1567 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1569 EventRecord
* ev
= (EventRecord
*) evr
;
1570 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1572 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1574 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1575 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1576 event
.m_altDown
= ev
->modifiers
& optionKey
;
1577 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1579 event
.m_x
= ev
->where
.h
;
1580 event
.m_y
= ev
->where
.v
;
1581 event
.m_timeStamp
= ev
->when
;
1582 wxWindow
* focus
= wxWindow::FindFocus() ;
1583 event
.SetEventObject(focus
);
1585 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1587 event
.m_keyCode
= WXK_CONTROL
;
1588 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1589 focus
->GetEventHandler()->ProcessEvent( event
) ;
1591 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1593 event
.m_keyCode
= WXK_SHIFT
;
1594 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1595 focus
->GetEventHandler()->ProcessEvent( event
) ;
1597 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1599 event
.m_keyCode
= WXK_ALT
;
1600 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1601 focus
->GetEventHandler()->ProcessEvent( event
) ;
1603 s_lastModifiers
= ev
->modifiers
;
1607 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1609 // we must avoid reentrancy problems when processing high level events eg printing
1610 bool former
= s_inYield
;
1612 EventRecord
* ev
= (EventRecord
*) evr
;
1613 ::AEProcessAppleEvent( ev
) ;
1614 s_inYield
= former
;
1617 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1619 EventRecord
* ev
= (EventRecord
*) evr
;
1620 wxToolTip::RemoveToolTips() ;
1623 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1624 WindowAttributes frontWindowAttributes
= NULL
;
1626 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1628 short windowPart
= ::FindWindow(ev
->where
, &window
);
1629 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1630 if ( wxPendingDelete
.Member(win
) )
1634 GetQDGlobalsScreenBits( &screenBits
);
1639 if ( s_macIsInModalLoop
)
1645 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1646 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1647 s_lastMouseDown
= 0;
1651 SystemClick( ev
, window
) ;
1652 s_lastMouseDown
= 0;
1655 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1661 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1666 Point pt
= { 0, 0 } ;
1667 SetPortWindowPort(window
) ;
1668 LocalToGlobal( &pt
) ;
1670 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1671 -1 , wxSIZE_USE_EXISTING
);
1673 s_lastMouseDown
= 0;
1677 if (TrackGoAway(window
, ev
->where
))
1682 s_lastMouseDown
= 0;
1686 Rect newContentRect
;
1687 Rect constraintRect
;
1688 constraintRect
.top
= win
->GetMinHeight() ;
1689 if ( constraintRect
.top
== -1 )
1690 constraintRect
.top
= 0 ;
1691 constraintRect
.left
= win
->GetMinWidth() ;
1692 if ( constraintRect
.left
== -1 )
1693 constraintRect
.left
= 0 ;
1694 constraintRect
.right
= win
->GetMaxWidth() ;
1695 if ( constraintRect
.right
== -1 )
1696 constraintRect
.right
= 32000 ;
1697 constraintRect
.bottom
= win
->GetMaxHeight() ;
1698 if ( constraintRect
.bottom
== -1 )
1699 constraintRect
.bottom
= 32000 ;
1701 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1702 &constraintRect
, &newContentRect
) ;
1705 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1706 newContentRect
.right
- newContentRect
.left
,
1707 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1709 s_lastMouseDown
= 0;
1714 if (TrackBox(window
, ev
->where
, windowPart
))
1716 // TODO setup size event
1717 ZoomWindow( window
, windowPart
, false ) ;
1723 Point pt
= { 0, 0 } ;
1724 SetPortWindowPort(window
) ;
1725 LocalToGlobal( &pt
) ;
1728 GetWindowPortBounds(window
, &tempRect
) ;
1729 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1730 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1733 s_lastMouseDown
= 0;
1735 case inCollapseBox
:
1736 // TODO setup size event
1737 s_lastMouseDown
= 0;
1744 SetPortWindowPort(window
) ;
1747 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1749 if ( s_macIsInModalLoop
)
1753 else if ( UMAIsWindowFloating( window
) )
1756 win
->MacMouseDown( ev
, windowPart
) ;
1761 win
->MacMouseDown( ev
, windowPart
) ;
1762 ::SelectWindow( window
) ;
1768 win
->MacMouseDown( ev
, windowPart
) ;
1776 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1778 EventRecord
* ev
= (EventRecord
*) evr
;
1781 short windowPart
= inNoWindow
;
1782 if ( wxTheApp
->s_captureWindow
)
1784 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1785 windowPart
= inContent
;
1789 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1800 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1802 win
->MacMouseUp( ev
, windowPart
) ;
1810 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1811 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1816 case kHomeCharCode
:
1819 case kEnterCharCode
:
1820 retval
= WXK_RETURN
;
1825 case kHelpCharCode
:
1828 case kBackspaceCharCode
:
1834 case kPageUpCharCode
:
1835 retval
= WXK_PAGEUP
;
1837 case kPageDownCharCode
:
1838 retval
= WXK_PAGEDOWN
;
1840 case kReturnCharCode
:
1841 retval
= WXK_RETURN
;
1843 case kFunctionKeyCharCode
:
1895 case kEscapeCharCode
:
1896 retval
= WXK_ESCAPE
;
1898 case kLeftArrowCharCode
:
1901 case kRightArrowCharCode
:
1902 retval
= WXK_RIGHT
;
1904 case kUpArrowCharCode
:
1907 case kDownArrowCharCode
:
1910 case kDeleteCharCode
:
1911 retval
= WXK_DELETE
;
1920 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1922 EventRecord
* ev
= (EventRecord
*) evr
;
1923 wxToolTip::RemoveToolTips() ;
1925 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1926 if ( HiWord( menuresult
) )
1928 if ( !s_macIsInModalLoop
)
1929 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1933 wxWindow
* focus
= wxWindow::FindFocus() ;
1935 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1937 // has not been handled -> perform default
1938 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1939 if ( control
&& control
->GetMacControl() != NULL
)
1943 keychar
= short(ev
->message
& charCodeMask
);
1944 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1945 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1951 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1953 EventRecord
* ev
= (EventRecord
*) evr
;
1954 wxToolTip::RemoveToolTips() ;
1956 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1957 if ( HiWord( menuresult
) )
1962 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1968 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1975 keychar
= short(keymessage
& charCodeMask
);
1976 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1978 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1980 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1981 // and look at the character after
1983 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1984 keychar
= short(keyInfo
& charCodeMask
);
1985 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1987 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1988 long realkeyval
= keyval
;
1989 if ( keyval
== keychar
)
1991 // 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)
1992 realkeyval
= short(keymessage
& charCodeMask
) ;
1993 keyval
= wxToupper( keyval
) ;
1996 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1997 bool handled
= false ;
1998 event
.m_shiftDown
= modifiers
& shiftKey
;
1999 event
.m_controlDown
= modifiers
& controlKey
;
2000 event
.m_altDown
= modifiers
& optionKey
;
2001 event
.m_metaDown
= modifiers
& cmdKey
;
2002 event
.m_keyCode
= keyval
;
2006 event
.m_timeStamp
= when
;
2007 event
.SetEventObject(focus
);
2008 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2009 if ( handled
&& event
.GetSkipped() )
2016 wxWindow
*ancestor
= focus
;
2019 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2022 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2023 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2026 if (ancestor
->IsTopLevel())
2028 ancestor
= ancestor
->GetParent();
2031 #endif // wxUSE_ACCEL
2035 event
.Skip( FALSE
) ;
2036 event
.SetEventType( wxEVT_CHAR
) ;
2038 event
.m_keyCode
= realkeyval
;
2040 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2041 if ( handled
&& event
.GetSkipped() )
2045 (keyval
== WXK_TAB
) &&
2046 // CS: copied the change below from wxGTK
2047 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2048 // have this style, yet choose not to process this particular TAB in which
2049 // case TAB must still work as a navigational character
2051 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2053 (focus
->GetParent()) &&
2054 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2056 wxNavigationKeyEvent new_event
;
2057 new_event
.SetEventObject( focus
);
2058 new_event
.SetDirection( !event
.ShiftDown() );
2059 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2060 new_event
.SetWindowChange( event
.ControlDown() );
2061 new_event
.SetCurrentFocus( focus
);
2062 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2063 if ( handled
&& new_event
.GetSkipped() )
2066 // backdoor handler for default return and command escape
2067 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2069 // if window is not having a focus still testing for default enter or cancel
2070 // TODO add the UMA version for ActiveNonFloatingWindow
2071 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2074 if ( keyval
== WXK_RETURN
)
2076 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2078 if ( def
&& def
->IsEnabled() )
2080 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2081 event
.SetEventObject(def
);
2082 def
->Command(event
);
2086 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2087 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2089 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2090 new_event
.SetEventObject( focus
);
2091 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2098 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2105 keychar
= short(keymessage
& charCodeMask
);
2106 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2107 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2109 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2110 // and look at the character after
2112 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2113 keychar
= short(keyInfo
& charCodeMask
);
2114 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2116 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2118 if ( keyval
== keychar
)
2120 keyval
= wxToupper( keyval
) ;
2122 bool handled
= false ;
2124 wxKeyEvent
event(wxEVT_KEY_UP
);
2125 event
.m_shiftDown
= modifiers
& shiftKey
;
2126 event
.m_controlDown
= modifiers
& controlKey
;
2127 event
.m_altDown
= modifiers
& optionKey
;
2128 event
.m_metaDown
= modifiers
& cmdKey
;
2129 event
.m_keyCode
= keyval
;
2133 event
.m_timeStamp
= when
;
2134 event
.SetEventObject(focus
);
2135 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2141 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2143 EventRecord
* ev
= (EventRecord
*) evr
;
2144 WindowRef window
= (WindowRef
) ev
->message
;
2147 bool activate
= (ev
->modifiers
& activeFlag
) ;
2148 WindowClass wclass
;
2149 ::GetWindowClass ( window
, &wclass
) ;
2150 if ( wclass
== kFloatingWindowClass
)
2152 // if it is a floater we activate/deactivate the front non-floating window instead
2153 window
= ::FrontNonFloatingWindow() ;
2155 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2157 win
->MacActivate( ev
->when
, activate
) ;
2161 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2163 EventRecord
* ev
= (EventRecord
*) evr
;
2164 WindowRef window
= (WindowRef
) ev
->message
;
2165 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2168 if ( !wxPendingDelete
.Member(win
) )
2169 win
->MacUpdate( ev
->when
) ;
2173 // since there is no way of telling this foreign window to update itself
2174 // we have to invalidate the update region otherwise we keep getting the same
2175 // event over and over again
2176 BeginUpdate( window
) ;
2177 EndUpdate( window
) ;
2181 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2183 EventRecord
* ev
= (EventRecord
*) evr
;
2184 if ( HiWord( ev
->message
) != noErr
)
2188 SetPt( &point
, 100 , 100 ) ;
2190 err
= DIBadMount( point
, ev
->message
) ;
2191 wxASSERT( err
== noErr
) ;
2195 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2197 EventRecord
* ev
= (EventRecord
*) evr
;
2198 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2200 case suspendResumeMessage
:
2202 bool isResuming
= ev
->message
& resumeFlag
;
2203 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2205 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2208 WindowRef oldFrontWindow
= NULL
;
2209 WindowRef newFrontWindow
= NULL
;
2211 // in case we don't take care of activating ourselves, we have to synchronize
2212 // our idea of the active window with the process manager's - which it already activated
2214 if ( !doesActivate
)
2215 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2217 MacResume( convertClipboard
) ;
2219 newFrontWindow
= ::FrontNonFloatingWindow() ;
2221 if ( oldFrontWindow
)
2223 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2225 win
->MacActivate( ev
->when
, false ) ;
2227 if ( newFrontWindow
)
2229 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2231 win
->MacActivate( ev
->when
, true ) ;
2236 MacSuspend( convertClipboard
) ;
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();
2341 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2343 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2344 wxMenu
* menu
= NULL
;
2345 wxMenuItem
* item
= NULL
;
2348 item
= mbar
->FindItem( id
, &menu
) ;
2350 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2352 if (item
->IsCheckable())
2354 item
->Check( !item
->IsChecked() ) ;
2357 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2361 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2364 return; // no menu item selected
2366 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2369 Str255 deskAccessoryName
;
2372 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2373 GetPort(&savedPort
);
2374 OpenDeskAcc(deskAccessoryName
);
2381 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2382 MacHandleMenuCommand( id
) ;