1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
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"
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
57 # include <CoreServices/CoreServices.h>
58 # if defined(WXMAKINGDLL)
59 # include <mach-o/dyld.h>
64 # include <ToolUtils.h>
65 # include <DiskInit.h>
69 extern wxList wxPendingDelete
;
70 extern wxList
*wxWinMacWindowList
;
71 extern wxList
*wxWinMacControlList
;
73 // statics for implementation
75 static bool s_inYield
= FALSE
;
76 static bool s_inOnIdle
= FALSE
;
79 static bool s_inReceiveEvent
= FALSE
;
80 static EventTime sleepTime
= kEventDurationNoWait
;
82 static long sleepTime
= 0 ;
85 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
87 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
88 EVT_IDLE(wxApp::OnIdle
)
89 EVT_END_SESSION(wxApp::OnEndSession
)
90 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
95 const short kMacMinHeap
= (29 * 1024) ;
96 // platform specific static variables
98 const short kwxMacMenuBarResource
= 1 ;
99 const short kwxMacAppleMenuId
= 1 ;
101 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
102 wxWindow
* wxApp::s_captureWindow
= NULL
;
103 int wxApp::s_lastMouseDown
= 0 ;
104 long wxApp::sm_lastMessageTime
= 0;
105 long wxApp::s_lastModifiers
= 0 ;
108 bool wxApp::s_macDefaultEncodingIsPC
= true ;
109 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
110 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
111 long wxApp::s_macPreferencesMenuItemId
= 0 ;
112 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
113 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
119 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
126 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
129 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
131 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
134 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
136 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
139 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
141 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
144 // AEODoc Calls MacOpenFile on each of the files passed
146 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
150 DescType returnedType
;
156 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
160 err
= AECountItems(&docList
, &itemsInList
);
164 ProcessSerialNumber PSN
;
165 PSN
.highLongOfPSN
= 0 ;
166 PSN
.lowLongOfPSN
= kCurrentProcess
;
167 SetFrontProcess( &PSN
) ;
169 for (i
= 1; i
<= itemsInList
; i
++) {
170 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
171 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
172 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
178 // AEPDoc Calls MacPrintFile on each of the files passed
180 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
184 DescType returnedType
;
190 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
194 err
= AECountItems(&docList
, &itemsInList
);
198 ProcessSerialNumber PSN
;
199 PSN
.highLongOfPSN
= 0 ;
200 PSN
.lowLongOfPSN
= kCurrentProcess
;
201 SetFrontProcess( &PSN
) ;
203 for (i
= 1; i
<= itemsInList
; i
++) {
204 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
205 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
206 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
212 // AEOApp calls MacNewFile
214 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
220 // AEQuit attempts to quit the application
222 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
224 /* wxWindow* win = GetTopWindow() ;
237 //----------------------------------------------------------------------
238 // Support Routines linking the Mac...File Calls to the Document Manager
239 //----------------------------------------------------------------------
241 void wxApp::MacOpenFile(const wxString
& fileName
)
243 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
245 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
248 void wxApp::MacPrintFile(const wxString
& fileName
)
250 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
253 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
256 wxView
* view
= doc
->GetFirstView() ;
259 wxPrintout
*printout
= view
->OnCreatePrintout();
263 printer
.Print(view
->GetFrame(), printout
, TRUE
);
269 doc
->DeleteAllViews();
270 dm
->RemoveDocument(doc
) ;
276 void wxApp::MacNewFile()
280 //----------------------------------------------------------------------
281 // Carbon Event Handler
282 //----------------------------------------------------------------------
286 static const EventTypeSpec eventList
[] =
288 { kEventClassCommand
, kEventProcessCommand
} ,
289 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
291 { kEventClassApplication
, kEventAppActivated
} ,
292 { kEventClassApplication
, kEventAppDeactivated
} ,
293 // handling the quit event is not recommended by apple
294 // rather using the quit apple event - which we do
296 { kEventClassAppleEvent
, kEventAppleEvent
} ,
298 { kEventClassMouse
, kEventMouseDown
} ,
302 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
304 return eventNotHandledErr
;
307 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
308 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
309 // deal with clicks in the menu bar explicitely
311 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
313 OSStatus result
= eventNotHandledErr
;
315 switch( GetEventKind(event
) )
317 case kEventMouseDown
:
322 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
323 sizeof( Point
), NULL
, &point
);
324 short windowPart
= ::FindWindow(point
, &window
);
326 if ( windowPart
== inMenuBar
)
328 MenuSelect( point
) ;
338 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
340 OSStatus result
= eventNotHandledErr
;
344 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
345 sizeof( HICommand
), NULL
, &command
);
347 MenuCommand id
= command
.commandID
;
348 if ( id
== kHICommandPreferences
)
349 id
= wxApp::s_macPreferencesMenuItemId
;
351 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
352 wxMenu
* menu
= NULL
;
353 wxMenuItem
* item
= NULL
;
356 item
= mbar
->FindItem( id
, &menu
) ;
358 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
361 switch( GetEventKind( event
) )
363 case kEventProcessCommand
:
365 if (item
->IsCheckable())
367 item
->Check( !item
->IsChecked() ) ;
370 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
374 case kEventCommandUpdateStatus
:
375 // eventually trigger an updateui round
385 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
387 OSStatus result
= eventNotHandledErr
;
388 switch ( GetEventKind( event
) )
390 case kEventAppActivated
:
393 wxTheApp
->MacResume( true ) ;
397 case kEventAppDeactivated
:
400 wxTheApp
->MacSuspend( true ) ;
410 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
412 OSStatus result
= eventNotHandledErr
;
413 switch( GetEventClass( event
) )
415 case kEventClassCommand
:
416 result
= CommandEventHandler( handler
, event
, data
) ;
418 case kEventClassApplication
:
419 result
= ApplicationEventHandler( handler
, event
, data
) ;
421 case kEventClassMenu
:
422 result
= MenuEventHandler( handler
, event
, data
) ;
424 case kEventClassMouse
:
425 result
= MouseEventHandler( handler
, event
, data
) ;
427 case kEventClassAppleEvent
:
430 wxMacConvertEventToRecord( event
, &rec
) ;
431 result
= AEProcessAppleEvent( &rec
) ;
441 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
445 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
446 // we know it's there ;-)
447 WXIMPORT
char std::__throws_bad_alloc
;
450 bool wxApp::Initialize()
456 UMAInitToolbox( 4 ) ;
457 SetEventMask( everyEvent
) ;
458 UMAShowWatchCursor() ;
460 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
461 // open shared library resources from here since we don't have
462 // __wxinitialize in Mach-O shared libraries
463 wxStAppResource::OpenSharedLibraryResource(NULL
);
467 // test the minimal configuration necessary
473 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
475 error
= kMacSTRWrongMachine
;
477 else if (theMachine
< gestaltMacPlus
)
479 error
= kMacSTRWrongMachine
;
481 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
483 error
= kMacSTROldSystem
;
485 else if ( theSystem
< 0x0860 )
487 error
= kMacSTROldSystem
;
489 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
491 error
= kMacSTRSmallSize
;
497 if ( !UMAHasAppearance() )
499 error = kMacSTRNoPre8Yet ;
505 // if we encountered any problems so far, give the error code and exit immediately
509 wxStAppResource resload
;
513 GetIndString(message
, 128, error
);
514 UMAShowArrowCursor() ;
515 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
516 itemHit
= Alert(128, nil
);
521 # if __option(profile)
522 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
527 // now avoid exceptions thrown for new (bad_alloc)
528 // FIXME CS for some changes outside wxMac does not compile anymore
530 std::__throws_bad_alloc
= 0 ;
534 wxMacSetupConverters() ;
536 s_macCursorRgn
= ::NewRgn() ;
538 wxClassInfo::InitializeClasses();
541 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
545 wxPendingEventsLocker
= new wxCriticalSection
;
548 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
549 wxTheColourDatabase
->Initialize();
551 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
552 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
554 wxInitializeStockLists();
555 wxInitializeStockObjects();
557 wxBitmap::InitStandardHandlers();
559 wxModule::RegisterModules();
560 if (!wxModule::InitializeModules()) {
564 wxMacCreateNotifierTable() ;
566 UMAShowArrowCursor() ;
571 bool wxApp::OnInitGui()
573 if( !wxAppBase::OnInitGui() )
577 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
579 InstallApplicationEventHandler(
580 GetwxAppEventHandlerUPP(),
581 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
584 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
585 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
586 NewAEEventHandlerUPP(AEHandleODoc
) ,
588 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
589 NewAEEventHandlerUPP(AEHandleOApp
) ,
591 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
592 NewAEEventHandlerUPP(AEHandlePDoc
) ,
594 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
595 NewAEEventHandlerUPP(AEHandleQuit
) ,
598 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
599 NewAEEventHandlerProc(AEHandleODoc
) ,
601 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
602 NewAEEventHandlerProc(AEHandleOApp
) ,
604 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
605 NewAEEventHandlerProc(AEHandlePDoc
) ,
607 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
608 NewAEEventHandlerProc(AEHandleQuit
) ,
615 void wxApp::CleanUp()
617 wxToolTip::RemoveToolTips() ;
619 // flush the logged messages if any and install a 'safer' log target: the
620 // default one (wxLogGui) can't be used after the resources are freed just
621 // below and the user suppliedo ne might be even more unsafe (using any
622 // wxWindows GUI function is unsafe starting from now)
623 wxLog::DontCreateOnDemand();
625 // this will flush the old messages if any
626 delete wxLog::SetActiveTarget(new wxLogStderr
);
629 // One last chance for pending objects to be cleaned up
630 wxTheApp
->DeletePendingObjects();
632 wxModule::CleanUpModules();
634 wxDeleteStockObjects() ;
636 // Destroy all GDI lists, etc.
637 wxDeleteStockLists();
639 delete wxTheColourDatabase
;
640 wxTheColourDatabase
= NULL
;
642 wxBitmap::CleanUpHandlers();
644 wxMacDestroyNotifierTable() ;
645 if (wxWinMacWindowList
) {
646 delete wxWinMacWindowList
;
648 if (wxWinMacControlList
) {
649 delete wxWinMacControlList
;
651 delete wxPendingEvents
;
652 wxPendingEvents
= NULL
;
655 delete wxPendingEventsLocker
;
656 // There is still more cleanup code that will try to use this if not NULL.
657 wxPendingEventsLocker
= NULL
;
658 // If we don't do the following, we get an apparent memory leak.
659 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
662 wxClassInfo::CleanUpClasses();
665 # if __option(profile)
666 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
674 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
675 // At this point we want to check if there are any memory
676 // blocks that aren't part of the wxDebugContext itself,
677 // as a special case. Then when dumping we need to ignore
678 // wxDebugContext, too.
679 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
681 wxLogDebug(wxT("There were memory leaks."));
682 wxDebugContext::Dump();
683 wxDebugContext::PrintStatistics();
685 // wxDebugContext::SetStream(NULL, NULL);
689 // do it as the very last thing because everything else can log messages
690 delete wxLog::SetActiveTarget(NULL
);
693 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
694 // close shared library resources from here since we don't have
695 // __wxterminate in Mach-O shared libraries
696 wxStAppResource::CloseSharedLibraryResource();
698 wxMacCleanupConverters() ;
700 UMACleanupToolbox() ;
701 if (s_macCursorRgn
) {
702 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
710 //----------------------------------------------------------------------
712 //----------------------------------------------------------------------
714 // extern variable for shared library resource id
715 // need to be able to find it with NSLookupAndBindSymbol
716 short gSharedLibraryResource
= kResFileNotOpened
;
718 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
719 CFBundleRef gSharedLibraryBundle
= NULL
;
720 #endif /* WXMAKINGDLL && __DARWIN__ */
722 wxStAppResource::wxStAppResource()
724 m_currentRefNum
= CurResFile() ;
725 if ( gSharedLibraryResource
!= kResFileNotOpened
)
727 UseResFile( gSharedLibraryResource
) ;
731 wxStAppResource::~wxStAppResource()
733 if ( m_currentRefNum
!= kResFileNotOpened
)
735 UseResFile( m_currentRefNum
) ;
739 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
741 gSharedLibraryResource
= kResFileNotOpened
;
744 if ( initBlock
!= NULL
) {
745 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
746 FSSpec
*fileSpec
= NULL
;
748 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
749 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
751 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
752 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
755 if (fileSpec
!= NULL
) {
756 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
761 // Open the shared library resource file if it is not yet open
764 const char *theLibPath
;
766 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
767 if (gSharedLibraryBundle
!= NULL
) {
768 // wxWindows has been bundled into a framework
769 // load the framework resources
771 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
774 // wxWindows is a simple dynamic shared library
775 // load the resources from the data fork of a separate resource file
780 OSErr theErr
= noErr
;
782 // get the library path
783 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
784 theModule
= NSModuleForSymbol(theSymbol
);
785 theLibPath
= NSLibraryNameForModule(theModule
);
787 // if we call wxLogDebug from here then, as wxTheApp hasn't been
788 // created yet when we're called from wxApp::Initialize(), wxLog
789 // is going to create a default stderr-based log target instead of
790 // the expected normal GUI one -- don't do it, if we really want
791 // to see this message just use fprintf() here
793 wxLogDebug( wxT("wxMac library installation name is '%s'"),
797 // allocate copy to replace .dylib.* extension with .rsrc
798 theResPath
= strdup(theLibPath
);
799 if (theResPath
!= NULL
) {
800 theName
= strrchr(theResPath
, '/');
801 if (theName
== NULL
) {
802 // no directory elements in path
803 theName
= theResPath
;
805 // find ".dylib" shared library extension
806 theExt
= strstr(theName
, ".dylib");
807 // overwrite extension with ".rsrc"
808 strcpy(theExt
, ".rsrc");
811 wxLogDebug( wxT("wxMac resources file name is '%s'"),
815 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
816 if (theErr
!= noErr
) {
817 // try in current directory (using name only)
818 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
821 // open the resource file
822 if (theErr
== noErr
) {
823 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
824 &gSharedLibraryResource
);
826 if (theErr
!= noErr
) {
829 wxT("unable to open wxMac resource file '%s'\n"),
831 #endif // __WXDEBUG__
834 // free duplicated resource file path
838 #endif /* __DARWIN__ */
840 #endif /* WXMAKINGDLL */
843 void wxStAppResource::CloseSharedLibraryResource()
846 // Close the shared library resource file
847 if (gSharedLibraryResource
!= kResFileNotOpened
) {
849 if (gSharedLibraryBundle
!= NULL
) {
850 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
851 gSharedLibraryResource
);
852 gSharedLibraryBundle
= NULL
;
855 #endif /* __DARWIN__ */
857 CloseResFile(gSharedLibraryResource
);
859 gSharedLibraryResource
= kResFileNotOpened
;
861 #endif /* WXMAKINGDLL */
864 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
866 // for shared libraries we have to manually get the correct resource
867 // ref num upon initializing and releasing when terminating, therefore
868 // the __wxinitialize and __wxterminate must be used
871 void __sinit(void); /* (generated by linker) */
872 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
873 pascal void __terminate(void);
876 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
878 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
879 return __initialize( theInitBlock
) ;
882 pascal void __wxterminate(void)
884 wxStAppResource::CloseSharedLibraryResource() ;
888 #endif /* WXMAKINGDLL && !__DARWIN__ */
890 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
892 return wxApp::Initialize();
895 int WXDLLEXPORT
wxEntryInitGui()
897 return wxTheApp
->OnInitGui();
900 void WXDLLEXPORT
wxEntryCleanup()
905 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
908 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
909 // This seems to be necessary since there are 'rogue'
910 // objects present at this point (perhaps global objects?)
911 // Setting a checkpoint will ignore them as far as the
912 // memory checking facility is concerned.
913 // Of course you may argue that memory allocated in globals should be
914 // checked, but this is a reasonable compromise.
915 wxDebugContext::SetCheckpoint();
918 if (!wxEntryStart(argc
, argv
)) {
921 // create the application object or ensure that one already exists
924 // The app may have declared a global application object, but we recommend
925 // the IMPLEMENT_APP macro is used instead, which sets an initializer
926 // function for delayed, dynamic app object construction.
927 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
928 wxT("No initializer - use IMPLEMENT_APP macro.") );
930 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
933 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
936 // Mac OS X passes a process serial number command line argument when
937 // the application is launched from the Finder. This argument must be
938 // removed from the command line arguments before being handled by the
939 // application (otherwise applications would need to handle it)
942 if (strncmp(argv
[1], "-psn_", 5) == 0) {
943 // assume the argument is always the only one and remove it
948 argc
= 0 ; // currently we don't support files as parameters
950 // we could try to get the open apple events here to adjust argc and argv better
952 wxTheApp
->argc
= argc
;
954 wxTheApp
->argv
= new wxChar
*[argc
+1];
956 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
958 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
960 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
962 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 ) ;
1127 #if WXWIN_COMPATIBILITY_2_2
1128 m_wantDebugOutput
= TRUE
;
1134 m_printMode
= wxPRINT_WINDOWS
;
1138 bool wxApp::Initialized()
1146 int wxApp::MainLoop()
1158 // Returns TRUE if more time is needed.
1159 bool wxApp::ProcessIdle()
1162 event
.SetEventObject(this);
1163 ProcessEvent(event
);
1165 return event
.MoreRequested();
1168 void wxApp::ExitMainLoop()
1170 m_keepGoing
= FALSE
;
1173 // Is a message/event pending?
1174 bool wxApp::Pending()
1177 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1181 return EventAvail( everyEvent
, &event
) ;
1185 // Dispatch a message.
1186 void wxApp::Dispatch()
1191 void wxApp::OnIdle(wxIdleEvent
& event
)
1193 // Avoid recursion (via ProcessEvent default case)
1200 // 'Garbage' collection of windows deleted with Close().
1201 DeletePendingObjects();
1203 // flush the logged messages if any
1204 wxLog
*pLog
= wxLog::GetActiveTarget();
1205 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1208 // Send OnIdle events to all windows
1209 bool needMore
= SendIdleEvents();
1212 event
.RequestMore(TRUE
);
1214 // If they are pending events, we must process them: pending events are
1215 // either events to the threads other than main or events posted with
1216 // wxPostEvent() functions
1217 wxMacProcessNotifierAndPendingEvents();
1220 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1221 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1224 void wxApp::WakeUpIdle()
1229 // Send idle event to all top-level windows
1230 bool wxApp::SendIdleEvents()
1232 bool needMore
= FALSE
;
1233 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1236 wxWindow
* win
= node
->GetData();
1237 if (SendIdleEvents(win
))
1240 node
= node
->GetNext();
1245 // Send idle event to window and all subwindows
1246 bool wxApp::SendIdleEvents(wxWindow
* win
)
1248 bool needMore
= FALSE
;
1251 event
.SetEventObject(win
);
1252 win
->ProcessEvent(event
);
1254 if (event
.MoreRequested())
1257 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1260 wxWindow
* win
= node
->GetData();
1261 if (SendIdleEvents(win
))
1264 node
= node
->GetNext();
1269 void wxApp::DeletePendingObjects()
1271 wxNode
*node
= wxPendingDelete
.GetFirst();
1274 wxObject
*obj
= (wxObject
*)node
->GetData();
1278 if (wxPendingDelete
.Member(obj
))
1281 // Deleting one object may have deleted other pending
1282 // objects, so start from beginning of list again.
1283 node
= wxPendingDelete
.GetFirst();
1293 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1296 GetTopWindow()->Close(TRUE
);
1299 // Default behaviour: close the application with prompts. The
1300 // user can veto the close, and therefore the end session.
1301 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1305 if (!GetTopWindow()->Close(!event
.CanVeto()))
1310 extern "C" void wxCYield() ;
1316 // Yield to other processes
1318 bool wxApp::Yield(bool onlyIfNeeded
)
1322 if ( !onlyIfNeeded
)
1324 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1333 YieldToAnyThread() ;
1335 // by definition yield should handle all non-processed events
1339 OSStatus status
= noErr
;
1342 s_inReceiveEvent
= true ;
1343 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1344 s_inReceiveEvent
= false ;
1346 if ( status
== eventLoopTimedOutErr
)
1348 // make sure next time the event loop will trigger idle events
1349 sleepTime
= kEventDurationNoWait
;
1351 else if ( status
== eventLoopQuitErr
)
1353 // according to QA1061 this may also occur when a WakeUp Process
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
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 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1396 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1398 #if 0 // having problems right now with that
1399 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1402 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1404 node
= node
->GetNext();
1407 ::HideFloatingWindows() ;
1409 s_lastMouseDown
= 0 ;
1411 if( convertClipboard
)
1413 MacConvertPrivateToPublicScrap() ;
1417 extern wxList wxModalDialogs
;
1419 void wxApp::MacResume( bool convertClipboard
)
1421 s_lastMouseDown
= 0 ;
1422 if( convertClipboard
)
1424 MacConvertPublicToPrivateScrap() ;
1428 ::ShowFloatingWindows() ;
1429 // raise modal dialogs in case a non modal window was selected to activate the app
1431 wxNode
* node
= wxModalDialogs
.GetFirst();
1434 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1437 node
= node
->GetNext();
1442 void wxApp::MacConvertPrivateToPublicScrap()
1446 void wxApp::MacConvertPublicToPrivateScrap()
1450 void wxApp::MacDoOneEvent()
1455 s_inReceiveEvent
= true ;
1456 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1457 s_inReceiveEvent
= false ;
1458 if ( status
== eventLoopTimedOutErr
)
1460 if ( wxTheApp
->ProcessIdle() )
1461 sleepTime
= kEventDurationNoWait
;
1463 sleepTime
= kEventDurationForever
;
1465 else if ( status
== eventLoopQuitErr
)
1467 // according to QA1061 this may also occur when a WakeUp Process
1472 MacHandleOneEvent( theEvent
) ;
1473 ReleaseEvent(theEvent
);
1474 sleepTime
= kEventDurationNoWait
;
1479 EventMask eventMask
= everyEvent
;
1481 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1483 MacHandleModifierEvents( &event
) ;
1484 MacHandleOneEvent( &event
);
1488 MacHandleModifierEvents( &event
) ;
1490 WindowPtr window
= ::FrontWindow() ;
1492 ::IdleControls( window
) ;
1494 if ( wxTheApp
->ProcessIdle() )
1497 sleepTime
= GetCaretTime() / 2 ;
1499 if ( event
.what
!= kHighLevelEvent
)
1500 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1504 DeletePendingObjects() ;
1505 wxMacProcessNotifierAndPendingEvents() ;
1508 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1511 EventTargetRef theTarget
;
1512 theTarget
= GetEventDispatcherTarget();
1513 m_macCurrentEvent
= evr
;
1514 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1516 EventRecord
* ev
= (EventRecord
*) evr
;
1517 m_macCurrentEvent
= ev
;
1519 wxApp::sm_lastMessageTime
= ev
->when
;
1524 MacHandleMouseDownEvent( ev
) ;
1525 if ( ev
->modifiers
& controlKey
)
1526 s_lastMouseDown
= 2;
1528 s_lastMouseDown
= 1;
1531 if ( s_lastMouseDown
== 2 )
1533 ev
->modifiers
|= controlKey
;
1537 ev
->modifiers
&= ~controlKey
;
1539 MacHandleMouseUpEvent( ev
) ;
1540 s_lastMouseDown
= 0;
1543 MacHandleActivateEvent( ev
) ;
1546 MacHandleUpdateEvent( ev
) ;
1550 MacHandleKeyDownEvent( ev
) ;
1553 MacHandleKeyUpEvent( ev
) ;
1556 MacHandleDiskEvent( ev
) ;
1559 MacHandleOSEvent( ev
) ;
1561 case kHighLevelEvent
:
1562 MacHandleHighLevelEvent( ev
) ;
1568 wxMacProcessNotifierAndPendingEvents() ;
1572 bool s_macIsInModalLoop
= false ;
1574 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1576 EventRecord
* ev
= (EventRecord
*) evr
;
1577 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1579 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1581 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1582 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1583 event
.m_altDown
= ev
->modifiers
& optionKey
;
1584 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1586 event
.m_x
= ev
->where
.h
;
1587 event
.m_y
= ev
->where
.v
;
1588 event
.m_timeStamp
= ev
->when
;
1589 wxWindow
* focus
= wxWindow::FindFocus() ;
1590 event
.SetEventObject(focus
);
1592 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1594 event
.m_keyCode
= WXK_CONTROL
;
1595 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1596 focus
->GetEventHandler()->ProcessEvent( event
) ;
1598 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1600 event
.m_keyCode
= WXK_SHIFT
;
1601 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1602 focus
->GetEventHandler()->ProcessEvent( event
) ;
1604 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1606 event
.m_keyCode
= WXK_ALT
;
1607 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1608 focus
->GetEventHandler()->ProcessEvent( event
) ;
1610 s_lastModifiers
= ev
->modifiers
;
1614 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1616 // we must avoid reentrancy problems when processing high level events eg printing
1617 bool former
= s_inYield
;
1619 EventRecord
* ev
= (EventRecord
*) evr
;
1620 ::AEProcessAppleEvent( ev
) ;
1621 s_inYield
= former
;
1624 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1626 EventRecord
* ev
= (EventRecord
*) evr
;
1627 wxToolTip::RemoveToolTips() ;
1630 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1631 WindowAttributes frontWindowAttributes
= NULL
;
1633 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1635 short windowPart
= ::FindWindow(ev
->where
, &window
);
1636 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1637 if ( wxPendingDelete
.Member(win
) )
1641 GetQDGlobalsScreenBits( &screenBits
);
1646 if ( s_macIsInModalLoop
)
1652 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1653 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1654 s_lastMouseDown
= 0;
1658 SystemClick( ev
, window
) ;
1659 s_lastMouseDown
= 0;
1662 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1668 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1673 Point pt
= { 0, 0 } ;
1674 SetPortWindowPort(window
) ;
1675 LocalToGlobal( &pt
) ;
1677 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1678 -1 , wxSIZE_USE_EXISTING
);
1680 s_lastMouseDown
= 0;
1684 if (TrackGoAway(window
, ev
->where
))
1689 s_lastMouseDown
= 0;
1693 Rect newContentRect
;
1694 Rect constraintRect
;
1695 constraintRect
.top
= win
->GetMinHeight() ;
1696 if ( constraintRect
.top
== -1 )
1697 constraintRect
.top
= 0 ;
1698 constraintRect
.left
= win
->GetMinWidth() ;
1699 if ( constraintRect
.left
== -1 )
1700 constraintRect
.left
= 0 ;
1701 constraintRect
.right
= win
->GetMaxWidth() ;
1702 if ( constraintRect
.right
== -1 )
1703 constraintRect
.right
= 32000 ;
1704 constraintRect
.bottom
= win
->GetMaxHeight() ;
1705 if ( constraintRect
.bottom
== -1 )
1706 constraintRect
.bottom
= 32000 ;
1708 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1709 &constraintRect
, &newContentRect
) ;
1712 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1713 newContentRect
.right
- newContentRect
.left
,
1714 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1716 s_lastMouseDown
= 0;
1721 if (TrackBox(window
, ev
->where
, windowPart
))
1723 // TODO setup size event
1724 ZoomWindow( window
, windowPart
, false ) ;
1730 Point pt
= { 0, 0 } ;
1731 SetPortWindowPort(window
) ;
1732 LocalToGlobal( &pt
) ;
1735 GetWindowPortBounds(window
, &tempRect
) ;
1736 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1737 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1740 s_lastMouseDown
= 0;
1742 case inCollapseBox
:
1743 // TODO setup size event
1744 s_lastMouseDown
= 0;
1751 SetPortWindowPort(window
) ;
1754 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1756 if ( s_macIsInModalLoop
)
1760 else if ( UMAIsWindowFloating( window
) )
1763 win
->MacMouseDown( ev
, windowPart
) ;
1768 win
->MacMouseDown( ev
, windowPart
) ;
1769 ::SelectWindow( window
) ;
1775 win
->MacMouseDown( ev
, windowPart
) ;
1783 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1785 EventRecord
* ev
= (EventRecord
*) evr
;
1788 short windowPart
= inNoWindow
;
1789 if ( wxTheApp
->s_captureWindow
)
1791 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1792 windowPart
= inContent
;
1796 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1807 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1809 win
->MacMouseUp( ev
, windowPart
) ;
1817 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1818 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1823 case kHomeCharCode
:
1826 case kEnterCharCode
:
1827 retval
= WXK_RETURN
;
1832 case kHelpCharCode
:
1835 case kBackspaceCharCode
:
1841 case kPageUpCharCode
:
1842 retval
= WXK_PAGEUP
;
1844 case kPageDownCharCode
:
1845 retval
= WXK_PAGEDOWN
;
1847 case kReturnCharCode
:
1848 retval
= WXK_RETURN
;
1850 case kFunctionKeyCharCode
:
1902 case kEscapeCharCode
:
1903 retval
= WXK_ESCAPE
;
1905 case kLeftArrowCharCode
:
1908 case kRightArrowCharCode
:
1909 retval
= WXK_RIGHT
;
1911 case kUpArrowCharCode
:
1914 case kDownArrowCharCode
:
1917 case kDeleteCharCode
:
1918 retval
= WXK_DELETE
;
1927 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1929 EventRecord
* ev
= (EventRecord
*) evr
;
1930 wxToolTip::RemoveToolTips() ;
1932 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1933 if ( HiWord( menuresult
) )
1935 if ( !s_macIsInModalLoop
)
1936 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1940 wxWindow
* focus
= wxWindow::FindFocus() ;
1942 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1944 // has not been handled -> perform default
1945 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1946 if ( control
&& control
->GetMacControl() != NULL
)
1950 keychar
= short(ev
->message
& charCodeMask
);
1951 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1952 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1958 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1960 EventRecord
* ev
= (EventRecord
*) evr
;
1961 wxToolTip::RemoveToolTips() ;
1963 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1964 if ( HiWord( menuresult
) )
1969 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1975 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1982 keychar
= short(keymessage
& charCodeMask
);
1983 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1985 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1987 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1988 // and look at the character after
1990 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1991 keychar
= short(keyInfo
& charCodeMask
);
1992 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1994 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1995 long realkeyval
= keyval
;
1996 if ( keyval
== keychar
)
1998 // 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)
1999 realkeyval
= short(keymessage
& charCodeMask
) ;
2000 keyval
= wxToupper( keyval
) ;
2003 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2004 bool handled
= false ;
2005 event
.m_shiftDown
= modifiers
& shiftKey
;
2006 event
.m_controlDown
= modifiers
& controlKey
;
2007 event
.m_altDown
= modifiers
& optionKey
;
2008 event
.m_metaDown
= modifiers
& cmdKey
;
2009 event
.m_keyCode
= keyval
;
2013 event
.m_timeStamp
= when
;
2014 event
.SetEventObject(focus
);
2015 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2016 if ( handled
&& event
.GetSkipped() )
2023 wxWindow
*ancestor
= focus
;
2026 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2029 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2030 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2033 if (ancestor
->IsTopLevel())
2035 ancestor
= ancestor
->GetParent();
2038 #endif // wxUSE_ACCEL
2042 event
.Skip( FALSE
) ;
2043 event
.SetEventType( wxEVT_CHAR
) ;
2045 event
.m_keyCode
= realkeyval
;
2047 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2048 if ( handled
&& event
.GetSkipped() )
2052 (keyval
== WXK_TAB
) &&
2053 // CS: copied the change below from wxGTK
2054 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2055 // have this style, yet choose not to process this particular TAB in which
2056 // case TAB must still work as a navigational character
2058 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2060 (focus
->GetParent()) &&
2061 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2063 wxNavigationKeyEvent new_event
;
2064 new_event
.SetEventObject( focus
);
2065 new_event
.SetDirection( !event
.ShiftDown() );
2066 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2067 new_event
.SetWindowChange( event
.ControlDown() );
2068 new_event
.SetCurrentFocus( focus
);
2069 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2070 if ( handled
&& new_event
.GetSkipped() )
2073 // backdoor handler for default return and command escape
2074 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2076 // if window is not having a focus still testing for default enter or cancel
2077 // TODO add the UMA version for ActiveNonFloatingWindow
2078 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2081 if ( keyval
== WXK_RETURN
)
2083 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2085 if ( def
&& def
->IsEnabled() )
2087 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2088 event
.SetEventObject(def
);
2089 def
->Command(event
);
2093 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2094 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2096 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2097 new_event
.SetEventObject( focus
);
2098 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2105 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2112 keychar
= short(keymessage
& charCodeMask
);
2113 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2114 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2116 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2117 // and look at the character after
2119 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2120 keychar
= short(keyInfo
& charCodeMask
);
2121 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2123 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2125 if ( keyval
== keychar
)
2127 keyval
= wxToupper( keyval
) ;
2129 bool handled
= false ;
2131 wxKeyEvent
event(wxEVT_KEY_UP
);
2132 event
.m_shiftDown
= modifiers
& shiftKey
;
2133 event
.m_controlDown
= modifiers
& controlKey
;
2134 event
.m_altDown
= modifiers
& optionKey
;
2135 event
.m_metaDown
= modifiers
& cmdKey
;
2136 event
.m_keyCode
= keyval
;
2140 event
.m_timeStamp
= when
;
2141 event
.SetEventObject(focus
);
2142 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2148 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2150 EventRecord
* ev
= (EventRecord
*) evr
;
2151 WindowRef window
= (WindowRef
) ev
->message
;
2154 bool activate
= (ev
->modifiers
& activeFlag
) ;
2155 WindowClass wclass
;
2156 ::GetWindowClass ( window
, &wclass
) ;
2157 if ( wclass
== kFloatingWindowClass
)
2159 // if it is a floater we activate/deactivate the front non-floating window instead
2160 window
= ::FrontNonFloatingWindow() ;
2162 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2164 win
->MacActivate( ev
->when
, activate
) ;
2168 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2170 EventRecord
* ev
= (EventRecord
*) evr
;
2171 WindowRef window
= (WindowRef
) ev
->message
;
2172 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2175 if ( !wxPendingDelete
.Member(win
) )
2176 win
->MacUpdate( ev
->when
) ;
2180 // since there is no way of telling this foreign window to update itself
2181 // we have to invalidate the update region otherwise we keep getting the same
2182 // event over and over again
2183 BeginUpdate( window
) ;
2184 EndUpdate( window
) ;
2188 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2190 EventRecord
* ev
= (EventRecord
*) evr
;
2191 if ( HiWord( ev
->message
) != noErr
)
2195 SetPt( &point
, 100 , 100 ) ;
2197 err
= DIBadMount( point
, ev
->message
) ;
2198 wxASSERT( err
== noErr
) ;
2202 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2204 EventRecord
* ev
= (EventRecord
*) evr
;
2205 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2207 case suspendResumeMessage
:
2209 bool isResuming
= ev
->message
& resumeFlag
;
2210 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2212 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2215 WindowRef oldFrontWindow
= NULL
;
2216 WindowRef newFrontWindow
= NULL
;
2218 // in case we don't take care of activating ourselves, we have to synchronize
2219 // our idea of the active window with the process manager's - which it already activated
2221 if ( !doesActivate
)
2222 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2224 MacResume( convertClipboard
) ;
2226 newFrontWindow
= ::FrontNonFloatingWindow() ;
2228 if ( oldFrontWindow
)
2230 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2232 win
->MacActivate( ev
->when
, false ) ;
2234 if ( newFrontWindow
)
2236 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2238 win
->MacActivate( ev
->when
, true ) ;
2243 MacSuspend( convertClipboard
) ;
2247 case mouseMovedMessage
:
2251 wxWindow
* currentMouseWindow
= NULL
;
2253 if (s_captureWindow
)
2255 currentMouseWindow
= s_captureWindow
;
2259 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2260 ¤tMouseWindow
) ;
2263 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2265 wxMouseEvent event
;
2267 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2268 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2270 event
.m_leftDown
= isDown
&& !controlDown
;
2271 event
.m_middleDown
= FALSE
;
2272 event
.m_rightDown
= isDown
&& controlDown
;
2273 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2274 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2275 event
.m_altDown
= ev
->modifiers
& optionKey
;
2276 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2277 event
.m_x
= ev
->where
.h
;
2278 event
.m_y
= ev
->where
.v
;
2279 event
.m_timeStamp
= ev
->when
;
2280 event
.SetEventObject(this);
2282 if ( wxWindow::s_lastMouseWindow
)
2284 wxMouseEvent
eventleave(event
);
2285 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2286 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2287 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2289 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2291 if ( currentMouseWindow
)
2293 wxMouseEvent
evententer(event
);
2294 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2295 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2296 evententer
.SetEventObject( currentMouseWindow
) ;
2297 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2299 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2302 short windowPart
= inNoWindow
;
2304 if ( s_captureWindow
)
2306 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2307 windowPart
= inContent
;
2311 windowPart
= ::FindWindow(ev
->where
, &window
);
2318 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2320 win
->MacMouseMoved( ev
, windowPart
) ;
2327 UMAShowArrowCursor();
2337 UMAShowArrowCursor();
2348 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2350 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2351 wxFrame
* frame
= mbar
->GetFrame();
2352 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2355 frame
->ProcessCommand(id
);
2360 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2363 return; // no menu item selected
2365 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2368 Str255 deskAccessoryName
;
2371 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2372 GetPort(&savedPort
);
2373 OpenDeskAcc(deskAccessoryName
);
2380 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2381 MacHandleMenuCommand( id
) ;