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 wxApp
*wxTheApp
= NULL
;
87 #if !USE_SHARED_LIBRARY
88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
90 EVT_IDLE(wxApp::OnIdle
)
91 EVT_END_SESSION(wxApp::OnEndSession
)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
97 const short kMacMinHeap
= (29 * 1024) ;
98 // platform specific static variables
100 const short kwxMacMenuBarResource
= 1 ;
101 const short kwxMacAppleMenuId
= 1 ;
103 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
104 wxWindow
* wxApp::s_captureWindow
= NULL
;
105 int wxApp::s_lastMouseDown
= 0 ;
106 long wxApp::sm_lastMessageTime
= 0;
107 long wxApp::s_lastModifiers
= 0 ;
110 bool wxApp::s_macDefaultEncodingIsPC
= true ;
111 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
112 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
113 long wxApp::s_macPreferencesMenuItemId
= 0 ;
114 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
115 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
117 //----------------------------------------------------------------------
118 // Core Apple Event Support
119 //----------------------------------------------------------------------
121 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
128 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
131 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
133 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
136 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
138 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
141 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
143 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
146 // AEODoc Calls MacOpenFile on each of the files passed
148 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
152 DescType returnedType
;
158 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
162 err
= AECountItems(&docList
, &itemsInList
);
166 ProcessSerialNumber PSN
;
167 PSN
.highLongOfPSN
= 0 ;
168 PSN
.lowLongOfPSN
= kCurrentProcess
;
169 SetFrontProcess( &PSN
) ;
171 for (i
= 1; i
<= itemsInList
; i
++) {
172 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
173 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
174 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
180 // AEPDoc Calls MacPrintFile on each of the files passed
182 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
186 DescType returnedType
;
192 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
196 err
= AECountItems(&docList
, &itemsInList
);
200 ProcessSerialNumber PSN
;
201 PSN
.highLongOfPSN
= 0 ;
202 PSN
.lowLongOfPSN
= kCurrentProcess
;
203 SetFrontProcess( &PSN
) ;
205 for (i
= 1; i
<= itemsInList
; i
++) {
206 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
207 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
208 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
214 // AEOApp calls MacNewFile
216 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
222 // AEQuit attempts to quite the application
224 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
226 /* wxWindow* win = GetTopWindow() ;
239 //----------------------------------------------------------------------
240 // Support Routines linking the Mac...File Calls to the Document Manager
241 //----------------------------------------------------------------------
243 void wxApp::MacOpenFile(const wxString
& fileName
)
245 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
247 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
250 void wxApp::MacPrintFile(const wxString
& fileName
)
252 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
255 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
258 wxView
* view
= doc
->GetFirstView() ;
261 wxPrintout
*printout
= view
->OnCreatePrintout();
265 printer
.Print(view
->GetFrame(), printout
, TRUE
);
271 doc
->DeleteAllViews();
272 dm
->RemoveDocument(doc
) ;
278 void wxApp::MacNewFile()
282 //----------------------------------------------------------------------
283 // Carbon Event Handler
284 //----------------------------------------------------------------------
288 static const EventTypeSpec eventList
[] =
290 { kEventClassCommand
, kEventProcessCommand
} ,
291 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
293 { kEventClassApplication
, kEventAppActivated
} ,
294 { kEventClassApplication
, kEventAppDeactivated
} ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
298 { kEventClassAppleEvent
, kEventAppleEvent
} ,
300 { kEventClassMouse
, kEventMouseDown
} ,
304 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
306 return eventNotHandledErr
;
309 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
310 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
311 // deal with clicks in the menu bar explicitely
313 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
315 OSStatus result
= eventNotHandledErr
;
317 switch( GetEventKind(event
) )
319 case kEventMouseDown
:
324 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
325 sizeof( Point
), NULL
, &point
);
326 short windowPart
= ::FindWindow(point
, &window
);
328 if ( windowPart
== inMenuBar
)
330 MenuSelect( point
) ;
340 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
342 OSStatus result
= eventNotHandledErr
;
346 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
347 sizeof( HICommand
), NULL
, &command
);
349 MenuCommand id
= command
.commandID
;
350 if ( id
== kHICommandPreferences
)
351 id
= wxApp::s_macPreferencesMenuItemId
;
353 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
354 wxMenu
* menu
= NULL
;
355 wxMenuItem
* item
= NULL
;
358 item
= mbar
->FindItem( id
, &menu
) ;
360 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
363 switch( GetEventKind( event
) )
365 case kEventProcessCommand
:
367 if (item
->IsCheckable())
369 item
->Check( !item
->IsChecked() ) ;
372 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
376 case kEventCommandUpdateStatus
:
377 // eventually trigger an updateui round
387 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
389 OSStatus result
= eventNotHandledErr
;
390 switch ( GetEventKind( event
) )
392 case kEventAppActivated
:
395 wxTheApp
->MacResume( true ) ;
399 case kEventAppDeactivated
:
402 wxTheApp
->MacSuspend( true ) ;
412 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
414 OSStatus result
= eventNotHandledErr
;
415 switch( GetEventClass( event
) )
417 case kEventClassCommand
:
418 result
= CommandEventHandler( handler
, event
, data
) ;
420 case kEventClassApplication
:
421 result
= ApplicationEventHandler( handler
, event
, data
) ;
423 case kEventClassMenu
:
424 result
= MenuEventHandler( handler
, event
, data
) ;
426 case kEventClassMouse
:
427 result
= MouseEventHandler( handler
, event
, data
) ;
429 case kEventClassAppleEvent
:
432 wxMacConvertEventToRecord( event
, &rec
) ;
433 result
= AEProcessAppleEvent( &rec
) ;
443 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
447 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
448 // we know it's there ;-)
449 WXIMPORT
char std::__throws_bad_alloc
;
452 bool wxApp::Initialize()
458 UMAInitToolbox( 4 ) ;
459 SetEventMask( everyEvent
) ;
460 UMAShowWatchCursor() ;
462 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
463 // open shared library resources from here since we don't have
464 // __wxinitialize in Mach-O shared libraries
465 wxStAppResource::OpenSharedLibraryResource(NULL
);
469 // test the minimal configuration necessary
475 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
477 error
= kMacSTRWrongMachine
;
479 else if (theMachine
< gestaltMacPlus
)
481 error
= kMacSTRWrongMachine
;
483 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
485 error
= kMacSTROldSystem
;
487 else if ( theSystem
< 0x0860 )
489 error
= kMacSTROldSystem
;
491 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
493 error
= kMacSTRSmallSize
;
499 if ( !UMAHasAppearance() )
501 error = kMacSTRNoPre8Yet ;
507 // if we encountered any problems so far, give the error code and exit immediately
511 wxStAppResource resload
;
515 GetIndString(message
, 128, error
);
516 UMAShowArrowCursor() ;
517 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
518 itemHit
= Alert(128, nil
);
523 # if __option(profile)
524 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
529 // now avoid exceptions thrown for new (bad_alloc)
530 // FIXME CS for some changes outside wxMac does not compile anymore
532 std::__throws_bad_alloc
= 0 ;
536 wxMacSetupConverters() ;
538 s_macCursorRgn
= ::NewRgn() ;
540 wxClassInfo::InitializeClasses();
543 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
547 wxPendingEventsLocker
= new wxCriticalSection
;
550 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
551 wxTheColourDatabase
->Initialize();
553 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
554 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
556 wxInitializeStockLists();
557 wxInitializeStockObjects();
559 wxBitmap::InitStandardHandlers();
561 wxModule::RegisterModules();
562 if (!wxModule::InitializeModules()) {
566 wxMacCreateNotifierTable() ;
568 UMAShowArrowCursor() ;
573 bool wxApp::OnInitGui()
575 if( !wxAppBase::OnInitGui() )
579 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
581 InstallApplicationEventHandler(
582 GetwxAppEventHandlerUPP(),
583 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
586 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
587 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
588 NewAEEventHandlerUPP(AEHandleODoc
) ,
590 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
591 NewAEEventHandlerUPP(AEHandleOApp
) ,
593 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
594 NewAEEventHandlerUPP(AEHandlePDoc
) ,
596 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
597 NewAEEventHandlerUPP(AEHandleQuit
) ,
600 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
601 NewAEEventHandlerProc(AEHandleODoc
) ,
603 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
604 NewAEEventHandlerProc(AEHandleOApp
) ,
606 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
607 NewAEEventHandlerProc(AEHandlePDoc
) ,
609 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
610 NewAEEventHandlerProc(AEHandleQuit
) ,
617 void wxApp::CleanUp()
619 wxToolTip::RemoveToolTips() ;
621 // flush the logged messages if any and install a 'safer' log target: the
622 // default one (wxLogGui) can't be used after the resources are freed just
623 // below and the user suppliedo ne might be even more unsafe (using any
624 // wxWindows GUI function is unsafe starting from now)
625 wxLog::DontCreateOnDemand();
627 // this will flush the old messages if any
628 delete wxLog::SetActiveTarget(new wxLogStderr
);
631 // One last chance for pending objects to be cleaned up
632 wxTheApp
->DeletePendingObjects();
634 wxModule::CleanUpModules();
636 wxDeleteStockObjects() ;
638 // Destroy all GDI lists, etc.
639 wxDeleteStockLists();
641 delete wxTheColourDatabase
;
642 wxTheColourDatabase
= NULL
;
644 wxBitmap::CleanUpHandlers();
646 wxMacDestroyNotifierTable() ;
647 if (wxWinMacWindowList
) {
648 delete wxWinMacWindowList
;
650 if (wxWinMacControlList
) {
651 delete wxWinMacControlList
;
653 delete wxPendingEvents
;
656 delete wxPendingEventsLocker
;
657 // There is still more cleanup code that will try to use this if not NULL.
658 wxPendingEventsLocker
= NULL
;
659 // If we don't do the following, we get an apparent memory leak.
660 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
663 wxClassInfo::CleanUpClasses();
666 # if __option(profile)
667 ProfilerDump( "\papp.prof" ) ;
675 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
676 // At this point we want to check if there are any memory
677 // blocks that aren't part of the wxDebugContext itself,
678 // as a special case. Then when dumping we need to ignore
679 // wxDebugContext, too.
680 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
682 wxLogDebug(wxT("There were memory leaks."));
683 wxDebugContext::Dump();
684 wxDebugContext::PrintStatistics();
686 // wxDebugContext::SetStream(NULL, NULL);
690 // do it as the very last thing because everything else can log messages
691 delete wxLog::SetActiveTarget(NULL
);
694 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
695 // close shared library resources from here since we don't have
696 // __wxterminate in Mach-O shared libraries
697 wxStAppResource::CloseSharedLibraryResource();
699 wxMacCleanupConverters() ;
701 UMACleanupToolbox() ;
702 if (s_macCursorRgn
) {
703 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
711 //----------------------------------------------------------------------
713 //----------------------------------------------------------------------
715 // extern variable for shared library resource id
716 // need to be able to find it with NSLookupAndBindSymbol
717 short gSharedLibraryResource
= kResFileNotOpened
;
719 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
720 CFBundleRef gSharedLibraryBundle
= NULL
;
721 #endif /* WXMAKINGDLL && __DARWIN__ */
723 wxStAppResource::wxStAppResource()
725 m_currentRefNum
= CurResFile() ;
726 if ( gSharedLibraryResource
!= kResFileNotOpened
)
728 UseResFile( gSharedLibraryResource
) ;
732 wxStAppResource::~wxStAppResource()
734 if ( m_currentRefNum
!= kResFileNotOpened
)
736 UseResFile( m_currentRefNum
) ;
740 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
742 gSharedLibraryResource
= kResFileNotOpened
;
745 if ( initBlock
!= NULL
) {
746 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
747 FSSpec
*fileSpec
= NULL
;
749 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
750 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
752 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
753 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
756 if (fileSpec
!= NULL
) {
757 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
762 // Open the shared library resource file if it is not yet open
765 const char *theLibPath
;
767 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
768 if (gSharedLibraryBundle
!= NULL
) {
769 // wxWindows has been bundled into a framework
770 // load the framework resources
772 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
775 // wxWindows is a simple dynamic shared library
776 // load the resources from the data fork of a separate resource file
781 OSErr theErr
= noErr
;
783 // get the library path
784 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
785 theModule
= NSModuleForSymbol(theSymbol
);
786 theLibPath
= NSLibraryNameForModule(theModule
);
788 // if we call wxLogDebug from here then, as wxTheApp hasn't been
789 // created yet when we're called from wxApp::Initialize(), wxLog
790 // is going to create a default stderr-based log target instead of
791 // the expected normal GUI one -- don't do it, if we really want
792 // to see this message just use fprintf() here
794 wxLogDebug( wxT("wxMac library installation name is '%s'"),
798 // allocate copy to replace .dylib.* extension with .rsrc
799 theResPath
= strdup(theLibPath
);
800 if (theResPath
!= NULL
) {
801 theName
= strrchr(theResPath
, '/');
802 if (theName
== NULL
) {
803 // no directory elements in path
804 theName
= theResPath
;
806 // find ".dylib" shared library extension
807 theExt
= strstr(theName
, ".dylib");
808 // overwrite extension with ".rsrc"
809 strcpy(theExt
, ".rsrc");
812 wxLogDebug( wxT("wxMac resources file name is '%s'"),
816 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
817 if (theErr
!= noErr
) {
818 // try in current directory (using name only)
819 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
822 // open the resource file
823 if (theErr
== noErr
) {
824 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
825 &gSharedLibraryResource
);
827 if (theErr
!= noErr
) {
830 wxT("unable to open wxMac resource file '%s'\n"),
832 #endif // __WXDEBUG__
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
;
955 wxTheApp
->argv
= new wxChar
*[argc
+1];
957 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
959 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
961 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
963 wxTheApp
->argv
= argv
;
966 // GUI-specific initialization, such as creating an app context.
969 // Here frames insert themselves automatically
970 // into wxTopLevelWindows by getting created
975 if ( wxTheApp
->OnInit() )
979 retValue
= wxTheApp
->OnRun();
982 // We want to initialize, but not run or exit immediately.
985 //else: app initialization failed, so we skipped OnRun()
987 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
990 // Forcibly delete the window.
991 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
992 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
994 topWindow
->Close(TRUE
);
995 wxTheApp
->DeletePendingObjects();
1000 wxTheApp
->SetTopWindow(NULL
);
1013 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1015 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1016 OSStatus err
= noErr
;
1019 switch( GetEventClass( event
) )
1021 case kEventClassKeyboard
:
1024 switch( GetEventKind(event
) )
1026 case kEventRawKeyDown
:
1027 rec
->what
= keyDown
;
1029 case kEventRawKeyRepeat
:
1030 rec
->what
= autoKey
;
1032 case kEventRawKeyUp
:
1035 case kEventRawKeyModifiersChanged
:
1036 rec
->what
= nullEvent
;
1045 unsigned char charCode
;
1047 GetMouse( &rec
->where
) ;
1049 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1050 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1051 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1052 rec
->modifiers
= modifiers
;
1053 rec
->message
= (keyCode
<< 8 ) + charCode
;
1057 case kEventClassTextInput
:
1059 switch( GetEventKind( event
) )
1061 case kEventTextInputUnicodeForKeyEvent
:
1064 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1068 unsigned char charCode
;
1070 GetMouse( &rec
->where
) ;
1071 rec
->what
= keyDown
;
1072 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1073 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1074 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1075 rec
->modifiers
= modifiers
;
1076 rec
->message
= (keyCode
<< 8 ) + charCode
;
1092 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1094 OSStatus result = eventNotHandledErr ;
1097 switch ( GetEventClass( event ) )
1099 case kEventClassKeyboard :
1100 if ( wxMacConvertEventToRecord( event , &rec ) )
1102 wxTheApp->MacHandleModifierEvents( &rec ) ;
1103 wxTheApp->MacHandleOneEvent( &rec ) ;
1107 case kEventClassTextInput :
1108 if ( wxMacConvertEventToRecord( event , &rec ) )
1110 wxTheApp->MacHandleModifierEvents( &rec ) ;
1111 wxTheApp->MacHandleOneEvent( &rec ) ;
1123 // Static member initialization
1124 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1131 #if WXWIN_COMPATIBILITY_2_2
1132 m_wantDebugOutput
= TRUE
;
1138 m_printMode
= wxPRINT_WINDOWS
;
1142 bool wxApp::Initialized()
1150 int wxApp::MainLoop()
1162 // Returns TRUE if more time is needed.
1163 bool wxApp::ProcessIdle()
1166 event
.SetEventObject(this);
1167 ProcessEvent(event
);
1169 return event
.MoreRequested();
1172 void wxApp::ExitMainLoop()
1174 m_keepGoing
= FALSE
;
1177 // Is a message/event pending?
1178 bool wxApp::Pending()
1181 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1185 return EventAvail( everyEvent
, &event
) ;
1189 // Dispatch a message.
1190 void wxApp::Dispatch()
1195 void wxApp::OnIdle(wxIdleEvent
& event
)
1197 // Avoid recursion (via ProcessEvent default case)
1204 // 'Garbage' collection of windows deleted with Close().
1205 DeletePendingObjects();
1207 // flush the logged messages if any
1208 wxLog
*pLog
= wxLog::GetActiveTarget();
1209 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1212 // Send OnIdle events to all windows
1213 bool needMore
= SendIdleEvents();
1216 event
.RequestMore(TRUE
);
1218 // If they are pending events, we must process them: pending events are
1219 // either events to the threads other than main or events posted with
1220 // wxPostEvent() functions
1221 wxMacProcessNotifierAndPendingEvents();
1231 // Send idle event to all top-level windows
1232 bool wxApp::SendIdleEvents()
1234 bool needMore
= FALSE
;
1235 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1238 wxWindow
* win
= node
->GetData();
1239 if (SendIdleEvents(win
))
1242 node
= node
->GetNext();
1247 // Send idle event to window and all subwindows
1248 bool wxApp::SendIdleEvents(wxWindow
* win
)
1250 bool needMore
= FALSE
;
1253 event
.SetEventObject(win
);
1254 win
->ProcessEvent(event
);
1256 if (event
.MoreRequested())
1259 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1262 wxWindow
* win
= node
->GetData();
1263 if (SendIdleEvents(win
))
1266 node
= node
->GetNext();
1271 void wxApp::DeletePendingObjects()
1273 wxNode
*node
= wxPendingDelete
.GetFirst();
1276 wxObject
*obj
= (wxObject
*)node
->GetData();
1280 if (wxPendingDelete
.Member(obj
))
1283 // Deleting one object may have deleted other pending
1284 // objects, so start from beginning of list again.
1285 node
= wxPendingDelete
.GetFirst();
1291 wxLogError(_("Fatal error: exiting"));
1297 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1300 GetTopWindow()->Close(TRUE
);
1303 // Default behaviour: close the application with prompts. The
1304 // user can veto the close, and therefore the end session.
1305 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1309 if (!GetTopWindow()->Close(!event
.CanVeto()))
1314 extern "C" void wxCYield() ;
1320 // Yield to other processes
1322 bool wxApp::Yield(bool onlyIfNeeded
)
1326 if ( !onlyIfNeeded
)
1328 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1337 YieldToAnyThread() ;
1339 // by definition yield should handle all non-processed events
1343 OSStatus status
= noErr
;
1346 s_inReceiveEvent
= true ;
1347 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1348 s_inReceiveEvent
= false ;
1350 if ( status
== eventLoopTimedOutErr
)
1352 // make sure next time the event loop will trigger idle events
1353 sleepTime
= kEventDurationNoWait
;
1355 else if ( status
== eventLoopQuitErr
)
1357 // according to QA1061 this may also occur when a WakeUp Process
1362 MacHandleOneEvent( theEvent
) ;
1363 ReleaseEvent(theEvent
);
1365 } while( status
== noErr
) ;
1369 // having a larger value here leads to large performance slowdowns
1370 // so we cannot give background apps more processor time here
1371 // we do so however having a large sleep value in the main event loop
1374 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1376 MacHandleModifierEvents( &event
) ;
1377 MacHandleOneEvent( &event
);
1378 if ( event
.what
!= kHighLevelEvent
)
1379 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1381 MacHandleModifierEvents( &event
) ;
1384 wxMacProcessNotifierAndPendingEvents() ;
1390 // platform specifics
1392 void wxApp::MacSuspend( bool convertClipboard
)
1395 // we have to deactive the top level windows manually
1397 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1400 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1401 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1403 node
= node
->GetNext();
1406 ::HideFloatingWindows() ;
1408 s_lastMouseDown
= 0 ;
1410 if( convertClipboard
)
1412 MacConvertPrivateToPublicScrap() ;
1416 extern wxList wxModalDialogs
;
1418 void wxApp::MacResume( bool convertClipboard
)
1420 s_lastMouseDown
= 0 ;
1421 if( convertClipboard
)
1423 MacConvertPublicToPrivateScrap() ;
1427 ::ShowFloatingWindows() ;
1428 // raise modal dialogs in case a non modal window was selected to activate the app
1430 wxNode
* node
= wxModalDialogs
.GetFirst();
1433 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1436 node
= node
->GetNext();
1441 void wxApp::MacConvertPrivateToPublicScrap()
1445 void wxApp::MacConvertPublicToPrivateScrap()
1449 void wxApp::MacDoOneEvent()
1454 s_inReceiveEvent
= true ;
1455 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1456 s_inReceiveEvent
= false ;
1457 if ( status
== eventLoopTimedOutErr
)
1459 if ( wxTheApp
->ProcessIdle() )
1460 sleepTime
= kEventDurationNoWait
;
1462 sleepTime
= kEventDurationForever
;
1464 else if ( status
== eventLoopQuitErr
)
1466 // according to QA1061 this may also occur when a WakeUp Process
1471 MacHandleOneEvent( theEvent
) ;
1472 ReleaseEvent(theEvent
);
1473 sleepTime
= kEventDurationNoWait
;
1478 EventMask eventMask
= everyEvent
;
1480 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1482 MacHandleModifierEvents( &event
) ;
1483 MacHandleOneEvent( &event
);
1487 MacHandleModifierEvents( &event
) ;
1489 WindowPtr window
= ::FrontWindow() ;
1491 ::IdleControls( window
) ;
1493 if ( wxTheApp
->ProcessIdle() )
1496 sleepTime
= GetCaretTime() / 2 ;
1498 if ( event
.what
!= kHighLevelEvent
)
1499 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1503 DeletePendingObjects() ;
1504 wxMacProcessNotifierAndPendingEvents() ;
1507 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1510 EventTargetRef theTarget
;
1511 theTarget
= GetEventDispatcherTarget();
1512 m_macCurrentEvent
= evr
;
1513 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1515 EventRecord
* ev
= (EventRecord
*) evr
;
1516 m_macCurrentEvent
= ev
;
1518 wxApp::sm_lastMessageTime
= ev
->when
;
1523 MacHandleMouseDownEvent( ev
) ;
1524 if ( ev
->modifiers
& controlKey
)
1525 s_lastMouseDown
= 2;
1527 s_lastMouseDown
= 1;
1530 if ( s_lastMouseDown
== 2 )
1532 ev
->modifiers
|= controlKey
;
1536 ev
->modifiers
&= ~controlKey
;
1538 MacHandleMouseUpEvent( ev
) ;
1539 s_lastMouseDown
= 0;
1542 MacHandleActivateEvent( ev
) ;
1545 MacHandleUpdateEvent( ev
) ;
1549 MacHandleKeyDownEvent( ev
) ;
1552 MacHandleKeyUpEvent( ev
) ;
1555 MacHandleDiskEvent( ev
) ;
1558 MacHandleOSEvent( ev
) ;
1560 case kHighLevelEvent
:
1561 MacHandleHighLevelEvent( ev
) ;
1567 wxMacProcessNotifierAndPendingEvents() ;
1571 bool s_macIsInModalLoop
= false ;
1573 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1575 EventRecord
* ev
= (EventRecord
*) evr
;
1576 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1578 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1580 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1581 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1582 event
.m_altDown
= ev
->modifiers
& optionKey
;
1583 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1585 event
.m_x
= ev
->where
.h
;
1586 event
.m_y
= ev
->where
.v
;
1587 event
.m_timeStamp
= ev
->when
;
1588 wxWindow
* focus
= wxWindow::FindFocus() ;
1589 event
.SetEventObject(focus
);
1591 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1593 event
.m_keyCode
= WXK_CONTROL
;
1594 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1595 focus
->GetEventHandler()->ProcessEvent( event
) ;
1597 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1599 event
.m_keyCode
= WXK_SHIFT
;
1600 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1601 focus
->GetEventHandler()->ProcessEvent( event
) ;
1603 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1605 event
.m_keyCode
= WXK_ALT
;
1606 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1607 focus
->GetEventHandler()->ProcessEvent( event
) ;
1609 s_lastModifiers
= ev
->modifiers
;
1613 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1615 // we must avoid reentrancy problems when processing high level events eg printing
1616 bool former
= s_inYield
;
1618 EventRecord
* ev
= (EventRecord
*) evr
;
1619 ::AEProcessAppleEvent( ev
) ;
1620 s_inYield
= former
;
1623 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1625 EventRecord
* ev
= (EventRecord
*) evr
;
1626 wxToolTip::RemoveToolTips() ;
1629 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1630 WindowAttributes frontWindowAttributes
= NULL
;
1632 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1634 short windowPart
= ::FindWindow(ev
->where
, &window
);
1635 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1636 if ( wxPendingDelete
.Member(win
) )
1640 GetQDGlobalsScreenBits( &screenBits
);
1645 if ( s_macIsInModalLoop
)
1651 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1652 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1653 s_lastMouseDown
= 0;
1657 SystemClick( ev
, window
) ;
1658 s_lastMouseDown
= 0;
1661 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1667 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1672 Point pt
= { 0, 0 } ;
1673 SetPortWindowPort(window
) ;
1674 LocalToGlobal( &pt
) ;
1676 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1677 -1 , wxSIZE_USE_EXISTING
);
1679 s_lastMouseDown
= 0;
1683 if (TrackGoAway(window
, ev
->where
))
1688 s_lastMouseDown
= 0;
1692 Rect newContentRect
;
1693 Rect constraintRect
;
1694 constraintRect
.top
= win
->GetMinHeight() ;
1695 if ( constraintRect
.top
== -1 )
1696 constraintRect
.top
= 0 ;
1697 constraintRect
.left
= win
->GetMinWidth() ;
1698 if ( constraintRect
.left
== -1 )
1699 constraintRect
.left
= 0 ;
1700 constraintRect
.right
= win
->GetMaxWidth() ;
1701 if ( constraintRect
.right
== -1 )
1702 constraintRect
.right
= 32000 ;
1703 constraintRect
.bottom
= win
->GetMaxHeight() ;
1704 if ( constraintRect
.bottom
== -1 )
1705 constraintRect
.bottom
= 32000 ;
1707 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1708 &constraintRect
, &newContentRect
) ;
1711 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1712 newContentRect
.right
- newContentRect
.left
,
1713 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1715 s_lastMouseDown
= 0;
1720 if (TrackBox(window
, ev
->where
, windowPart
))
1722 // TODO setup size event
1723 ZoomWindow( window
, windowPart
, false ) ;
1729 Point pt
= { 0, 0 } ;
1730 SetPortWindowPort(window
) ;
1731 LocalToGlobal( &pt
) ;
1734 GetWindowPortBounds(window
, &tempRect
) ;
1735 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1736 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1739 s_lastMouseDown
= 0;
1741 case inCollapseBox
:
1742 // TODO setup size event
1743 s_lastMouseDown
= 0;
1750 SetPortWindowPort(window
) ;
1753 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1755 if ( s_macIsInModalLoop
)
1759 else if ( UMAIsWindowFloating( window
) )
1762 win
->MacMouseDown( ev
, windowPart
) ;
1767 win
->MacMouseDown( ev
, windowPart
) ;
1768 ::SelectWindow( window
) ;
1774 win
->MacMouseDown( ev
, windowPart
) ;
1782 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1784 EventRecord
* ev
= (EventRecord
*) evr
;
1787 short windowPart
= inNoWindow
;
1788 if ( wxTheApp
->s_captureWindow
)
1790 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1791 windowPart
= inContent
;
1795 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1806 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1808 win
->MacMouseUp( ev
, windowPart
) ;
1816 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1817 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1822 case kHomeCharCode
:
1825 case kEnterCharCode
:
1826 retval
= WXK_RETURN
;
1831 case kHelpCharCode
:
1834 case kBackspaceCharCode
:
1840 case kPageUpCharCode
:
1841 retval
= WXK_PAGEUP
;
1843 case kPageDownCharCode
:
1844 retval
= WXK_PAGEDOWN
;
1846 case kReturnCharCode
:
1847 retval
= WXK_RETURN
;
1849 case kFunctionKeyCharCode
:
1901 case kEscapeCharCode
:
1902 retval
= WXK_ESCAPE
;
1904 case kLeftArrowCharCode
:
1907 case kRightArrowCharCode
:
1908 retval
= WXK_RIGHT
;
1910 case kUpArrowCharCode
:
1913 case kDownArrowCharCode
:
1916 case kDeleteCharCode
:
1917 retval
= WXK_DELETE
;
1926 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1928 EventRecord
* ev
= (EventRecord
*) evr
;
1929 wxToolTip::RemoveToolTips() ;
1931 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1932 if ( HiWord( menuresult
) )
1934 if ( !s_macIsInModalLoop
)
1935 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1939 wxWindow
* focus
= wxWindow::FindFocus() ;
1941 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1943 // has not been handled -> perform default
1944 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1945 if ( control
&& control
->GetMacControl() != NULL
)
1949 keychar
= short(ev
->message
& charCodeMask
);
1950 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1951 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1957 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1959 EventRecord
* ev
= (EventRecord
*) evr
;
1960 wxToolTip::RemoveToolTips() ;
1962 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1963 if ( HiWord( menuresult
) )
1968 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1974 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1981 keychar
= short(keymessage
& charCodeMask
);
1982 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1984 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1986 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1987 // and look at the character after
1989 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1990 keychar
= short(keyInfo
& charCodeMask
);
1991 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1993 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1994 long realkeyval
= keyval
;
1995 if ( keyval
== keychar
)
1997 // 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)
1998 realkeyval
= short(keymessage
& charCodeMask
) ;
1999 keyval
= wxToupper( keyval
) ;
2002 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2003 bool handled
= false ;
2004 event
.m_shiftDown
= modifiers
& shiftKey
;
2005 event
.m_controlDown
= modifiers
& controlKey
;
2006 event
.m_altDown
= modifiers
& optionKey
;
2007 event
.m_metaDown
= modifiers
& cmdKey
;
2008 event
.m_keyCode
= keyval
;
2012 event
.m_timeStamp
= when
;
2013 event
.SetEventObject(focus
);
2014 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2015 if ( handled
&& event
.GetSkipped() )
2022 wxWindow
*ancestor
= focus
;
2025 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2028 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2029 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2032 if (ancestor
->IsTopLevel())
2034 ancestor
= ancestor
->GetParent();
2037 #endif // wxUSE_ACCEL
2041 event
.Skip( FALSE
) ;
2042 event
.SetEventType( wxEVT_CHAR
) ;
2044 event
.m_keyCode
= realkeyval
;
2046 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2047 if ( handled
&& event
.GetSkipped() )
2051 (keyval
== WXK_TAB
) &&
2052 // CS: copied the change below from wxGTK
2053 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2054 // have this style, yet choose not to process this particular TAB in which
2055 // case TAB must still work as a navigational character
2057 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2059 (focus
->GetParent()) &&
2060 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2062 wxNavigationKeyEvent new_event
;
2063 new_event
.SetEventObject( focus
);
2064 new_event
.SetDirection( !event
.ShiftDown() );
2065 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2066 new_event
.SetWindowChange( event
.ControlDown() );
2067 new_event
.SetCurrentFocus( focus
);
2068 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2069 if ( handled
&& new_event
.GetSkipped() )
2072 // backdoor handler for default return and command escape
2073 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2075 // if window is not having a focus still testing for default enter or cancel
2076 // TODO add the UMA version for ActiveNonFloatingWindow
2077 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2080 if ( keyval
== WXK_RETURN
)
2082 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2084 if ( def
&& def
->IsEnabled() )
2086 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2087 event
.SetEventObject(def
);
2088 def
->Command(event
);
2092 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2093 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2095 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2096 new_event
.SetEventObject( focus
);
2097 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2104 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2111 keychar
= short(keymessage
& charCodeMask
);
2112 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2113 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2115 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2116 // and look at the character after
2118 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2119 keychar
= short(keyInfo
& charCodeMask
);
2120 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2122 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2124 if ( keyval
== keychar
)
2126 keyval
= wxToupper( keyval
) ;
2128 bool handled
= false ;
2130 wxKeyEvent
event(wxEVT_KEY_UP
);
2131 event
.m_shiftDown
= modifiers
& shiftKey
;
2132 event
.m_controlDown
= modifiers
& controlKey
;
2133 event
.m_altDown
= modifiers
& optionKey
;
2134 event
.m_metaDown
= modifiers
& cmdKey
;
2135 event
.m_keyCode
= keyval
;
2139 event
.m_timeStamp
= when
;
2140 event
.SetEventObject(focus
);
2141 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2147 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2149 EventRecord
* ev
= (EventRecord
*) evr
;
2150 WindowRef window
= (WindowRef
) ev
->message
;
2153 bool activate
= (ev
->modifiers
& activeFlag
) ;
2154 WindowClass wclass
;
2155 ::GetWindowClass ( window
, &wclass
) ;
2156 if ( wclass
== kFloatingWindowClass
)
2158 // if it is a floater we activate/deactivate the front non-floating window instead
2159 window
= ::FrontNonFloatingWindow() ;
2161 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2163 win
->MacActivate( ev
->when
, activate
) ;
2167 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2169 EventRecord
* ev
= (EventRecord
*) evr
;
2170 WindowRef window
= (WindowRef
) ev
->message
;
2171 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2174 if ( !wxPendingDelete
.Member(win
) )
2175 win
->MacUpdate( ev
->when
) ;
2179 // since there is no way of telling this foreign window to update itself
2180 // we have to invalidate the update region otherwise we keep getting the same
2181 // event over and over again
2182 BeginUpdate( window
) ;
2183 EndUpdate( window
) ;
2187 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2189 EventRecord
* ev
= (EventRecord
*) evr
;
2190 if ( HiWord( ev
->message
) != noErr
)
2194 SetPt( &point
, 100 , 100 ) ;
2196 err
= DIBadMount( point
, ev
->message
) ;
2197 wxASSERT( err
== noErr
) ;
2201 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2203 EventRecord
* ev
= (EventRecord
*) evr
;
2204 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2206 case suspendResumeMessage
:
2208 bool isResuming
= ev
->message
& resumeFlag
;
2209 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2211 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2214 WindowRef oldFrontWindow
= NULL
;
2215 WindowRef newFrontWindow
= NULL
;
2217 // in case we don't take care of activating ourselves, we have to synchronize
2218 // our idea of the active window with the process manager's - which it already activated
2220 if ( !doesActivate
)
2221 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2223 MacResume( convertClipboard
) ;
2225 newFrontWindow
= ::FrontNonFloatingWindow() ;
2227 if ( oldFrontWindow
)
2229 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2231 win
->MacActivate( ev
->when
, false ) ;
2233 if ( newFrontWindow
)
2235 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2237 win
->MacActivate( ev
->when
, true ) ;
2242 MacSuspend( convertClipboard
) ;
2246 case mouseMovedMessage
:
2250 wxWindow
* currentMouseWindow
= NULL
;
2252 if (s_captureWindow
)
2254 currentMouseWindow
= s_captureWindow
;
2258 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2259 ¤tMouseWindow
) ;
2262 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2264 wxMouseEvent event
;
2266 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2267 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2269 event
.m_leftDown
= isDown
&& !controlDown
;
2270 event
.m_middleDown
= FALSE
;
2271 event
.m_rightDown
= isDown
&& controlDown
;
2272 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2273 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2274 event
.m_altDown
= ev
->modifiers
& optionKey
;
2275 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2276 event
.m_x
= ev
->where
.h
;
2277 event
.m_y
= ev
->where
.v
;
2278 event
.m_timeStamp
= ev
->when
;
2279 event
.SetEventObject(this);
2281 if ( wxWindow::s_lastMouseWindow
)
2283 wxMouseEvent
eventleave(event
);
2284 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2285 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2286 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2288 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2290 if ( currentMouseWindow
)
2292 wxMouseEvent
evententer(event
);
2293 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2294 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2295 evententer
.SetEventObject( currentMouseWindow
) ;
2296 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2298 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2301 short windowPart
= inNoWindow
;
2303 if ( s_captureWindow
)
2305 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2306 windowPart
= inContent
;
2310 windowPart
= ::FindWindow(ev
->where
, &window
);
2317 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2319 win
->MacMouseMoved( ev
, windowPart
) ;
2326 UMAShowArrowCursor();
2336 UMAShowArrowCursor();
2347 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2349 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2350 wxFrame
* frame
= mbar
->GetFrame();
2351 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2354 frame
->ProcessCommand(id
);
2359 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2362 return; // no menu item selected
2364 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2367 Str255 deskAccessoryName
;
2370 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2371 GetPort(&savedPort
);
2372 OpenDeskAcc(deskAccessoryName
);
2379 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2380 MacHandleMenuCommand( id
) ;