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 OSStatus result
= eventNotHandledErr
;
307 UInt32 kind
= GetEventKind( event
) ;
312 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
313 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
314 // deal with clicks in the menu bar explicitely
316 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
318 OSStatus result
= eventNotHandledErr
;
320 switch( GetEventKind(event
) )
322 case kEventMouseDown
:
327 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
328 sizeof( Point
), NULL
, &point
);
329 short windowPart
= ::FindWindow(point
, &window
);
331 if ( windowPart
== inMenuBar
)
333 MenuSelect( point
) ;
343 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
345 OSStatus result
= eventNotHandledErr
;
349 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
350 sizeof( HICommand
), NULL
, &command
);
352 MenuCommand id
= command
.commandID
;
353 if ( id
== kHICommandPreferences
)
354 id
= wxApp::s_macPreferencesMenuItemId
;
356 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
357 wxMenu
* menu
= NULL
;
358 wxMenuItem
* item
= NULL
;
361 item
= mbar
->FindItem( id
, &menu
) ;
363 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
366 switch( GetEventKind( event
) )
368 case kEventProcessCommand
:
370 if (item
->IsCheckable())
372 item
->Check( !item
->IsChecked() ) ;
375 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
379 case kEventCommandUpdateStatus
:
380 // eventually trigger an updateui round
390 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
392 OSStatus result
= eventNotHandledErr
;
393 switch ( GetEventKind( event
) )
395 case kEventAppActivated
:
398 wxTheApp
->MacResume( true ) ;
402 case kEventAppDeactivated
:
405 wxTheApp
->MacSuspend( true ) ;
415 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
417 OSStatus result
= eventNotHandledErr
;
418 switch( GetEventClass( event
) )
420 case kEventClassCommand
:
421 result
= CommandEventHandler( handler
, event
, data
) ;
423 case kEventClassApplication
:
424 result
= ApplicationEventHandler( handler
, event
, data
) ;
426 case kEventClassMenu
:
427 result
= MenuEventHandler( handler
, event
, data
) ;
429 case kEventClassMouse
:
430 result
= MouseEventHandler( handler
, event
, data
) ;
432 case kEventClassAppleEvent
:
435 wxMacConvertEventToRecord( event
, &rec
) ;
436 result
= AEProcessAppleEvent( &rec
) ;
446 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
450 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
451 // we know it's there ;-)
452 WXIMPORT
char std::__throws_bad_alloc
;
455 bool wxApp::Initialize()
461 UMAInitToolbox( 4 ) ;
462 SetEventMask( everyEvent
) ;
463 UMAShowWatchCursor() ;
465 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
466 // open shared library resources from here since we don't have
467 // __wxinitialize in Mach-O shared libraries
468 wxStAppResource::OpenSharedLibraryResource(NULL
);
472 // test the minimal configuration necessary
478 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
480 error
= kMacSTRWrongMachine
;
482 else if (theMachine
< gestaltMacPlus
)
484 error
= kMacSTRWrongMachine
;
486 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
488 error
= kMacSTROldSystem
;
490 else if ( theSystem
< 0x0860 )
492 error
= kMacSTROldSystem
;
494 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
496 error
= kMacSTRSmallSize
;
502 if ( !UMAHasAppearance() )
504 error = kMacSTRNoPre8Yet ;
510 // if we encountered any problems so far, give the error code and exit immediately
514 wxStAppResource resload
;
518 GetIndString(message
, 128, error
);
519 UMAShowArrowCursor() ;
520 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
521 itemHit
= Alert(128, nil
);
526 # if __option(profile)
527 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
532 // now avoid exceptions thrown for new (bad_alloc)
533 // FIXME CS for some changes outside wxMac does not compile anymore
535 std::__throws_bad_alloc
= 0 ;
539 wxMacSetupConverters() ;
541 s_macCursorRgn
= ::NewRgn() ;
543 wxClassInfo::InitializeClasses();
546 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
550 wxPendingEventsLocker
= new wxCriticalSection
;
553 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
554 wxTheColourDatabase
->Initialize();
556 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
557 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
559 wxInitializeStockLists();
560 wxInitializeStockObjects();
562 wxBitmap::InitStandardHandlers();
564 wxModule::RegisterModules();
565 if (!wxModule::InitializeModules()) {
569 wxMacCreateNotifierTable() ;
571 UMAShowArrowCursor() ;
576 bool wxApp::OnInitGui()
578 if( !wxAppBase::OnInitGui() )
582 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
584 InstallApplicationEventHandler(
585 GetwxAppEventHandlerUPP(),
586 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
589 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
590 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
591 NewAEEventHandlerUPP(AEHandleODoc
) ,
593 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
594 NewAEEventHandlerUPP(AEHandleOApp
) ,
596 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
597 NewAEEventHandlerUPP(AEHandlePDoc
) ,
599 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
600 NewAEEventHandlerUPP(AEHandleQuit
) ,
603 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
604 NewAEEventHandlerProc(AEHandleODoc
) ,
606 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
607 NewAEEventHandlerProc(AEHandleOApp
) ,
609 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
610 NewAEEventHandlerProc(AEHandlePDoc
) ,
612 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
613 NewAEEventHandlerProc(AEHandleQuit
) ,
620 void wxApp::CleanUp()
622 wxToolTip::RemoveToolTips() ;
624 // flush the logged messages if any and install a 'safer' log target: the
625 // default one (wxLogGui) can't be used after the resources are freed just
626 // below and the user suppliedo ne might be even more unsafe (using any
627 // wxWindows GUI function is unsafe starting from now)
628 wxLog::DontCreateOnDemand();
630 // this will flush the old messages if any
631 delete wxLog::SetActiveTarget(new wxLogStderr
);
634 // One last chance for pending objects to be cleaned up
635 wxTheApp
->DeletePendingObjects();
637 wxModule::CleanUpModules();
639 wxDeleteStockObjects() ;
641 // Destroy all GDI lists, etc.
642 wxDeleteStockLists();
644 delete wxTheColourDatabase
;
645 wxTheColourDatabase
= NULL
;
647 wxBitmap::CleanUpHandlers();
649 wxMacDestroyNotifierTable() ;
650 if (wxWinMacWindowList
) {
651 delete wxWinMacWindowList
;
653 if (wxWinMacControlList
) {
654 delete wxWinMacControlList
;
656 delete wxPendingEvents
;
659 delete wxPendingEventsLocker
;
660 // There is still more cleanup code that will try to use this if not NULL.
661 wxPendingEventsLocker
= NULL
;
662 // If we don't do the following, we get an apparent memory leak.
663 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
666 wxClassInfo::CleanUpClasses();
669 # if __option(profile)
670 ProfilerDump( "\papp.prof" ) ;
678 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
679 // At this point we want to check if there are any memory
680 // blocks that aren't part of the wxDebugContext itself,
681 // as a special case. Then when dumping we need to ignore
682 // wxDebugContext, too.
683 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
685 wxLogDebug(wxT("There were memory leaks."));
686 wxDebugContext::Dump();
687 wxDebugContext::PrintStatistics();
689 // wxDebugContext::SetStream(NULL, NULL);
693 // do it as the very last thing because everything else can log messages
694 delete wxLog::SetActiveTarget(NULL
);
697 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
698 // close shared library resources from here since we don't have
699 // __wxterminate in Mach-O shared libraries
700 wxStAppResource::CloseSharedLibraryResource();
702 wxMacCleanupConverters() ;
704 UMACleanupToolbox() ;
705 if (s_macCursorRgn
) {
706 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
714 //----------------------------------------------------------------------
716 //----------------------------------------------------------------------
718 // extern variable for shared library resource id
719 // need to be able to find it with NSLookupAndBindSymbol
720 short gSharedLibraryResource
= kResFileNotOpened
;
722 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
723 CFBundleRef gSharedLibraryBundle
= NULL
;
724 #endif /* WXMAKINGDLL && __DARWIN__ */
726 wxStAppResource::wxStAppResource()
728 m_currentRefNum
= CurResFile() ;
729 if ( gSharedLibraryResource
!= kResFileNotOpened
)
731 UseResFile( gSharedLibraryResource
) ;
735 wxStAppResource::~wxStAppResource()
737 if ( m_currentRefNum
!= kResFileNotOpened
)
739 UseResFile( m_currentRefNum
) ;
743 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
745 gSharedLibraryResource
= kResFileNotOpened
;
748 if ( initBlock
!= NULL
) {
749 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
750 FSSpec
*fileSpec
= NULL
;
752 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
753 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
755 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
756 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
759 if (fileSpec
!= NULL
) {
760 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
765 // Open the shared library resource file if it is not yet open
768 const char *theLibPath
;
770 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
771 if (gSharedLibraryBundle
!= NULL
) {
772 // wxWindows has been bundled into a framework
773 // load the framework resources
775 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
778 // wxWindows is a simple dynamic shared library
779 // load the resources from the data fork of a separate resource file
784 OSErr theErr
= noErr
;
786 // get the library path
787 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
788 theModule
= NSModuleForSymbol(theSymbol
);
789 theLibPath
= NSLibraryNameForModule(theModule
);
791 // if we call wxLogDebug from here then, as wxTheApp hasn't been
792 // created yet when we're called from wxApp::Initialize(), wxLog
793 // is going to create a default stderr-based log target instead of
794 // the expected normal GUI one -- don't do it, if we really want
795 // to see this message just use fprintf() here
797 wxLogDebug( wxT("wxMac library installation name is '%s'"),
801 // allocate copy to replace .dylib.* extension with .rsrc
802 theResPath
= strdup(theLibPath
);
803 if (theResPath
!= NULL
) {
804 theName
= strrchr(theResPath
, '/');
805 if (theName
== NULL
) {
806 // no directory elements in path
807 theName
= theResPath
;
809 // find ".dylib" shared library extension
810 theExt
= strstr(theName
, ".dylib");
811 // overwrite extension with ".rsrc"
812 strcpy(theExt
, ".rsrc");
815 wxLogDebug( wxT("wxMac resources file name is '%s'"),
819 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
820 if (theErr
!= noErr
) {
821 // try in current directory (using name only)
822 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
825 // open the resource file
826 if (theErr
== noErr
) {
827 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
828 &gSharedLibraryResource
);
830 if (theErr
!= noErr
) {
833 wxT("unable to open wxMac resource file '%s'\n"),
835 #endif // __WXDEBUG__
838 // free duplicated resource file path
842 #endif /* __DARWIN__ */
844 #endif /* WXMAKINGDLL */
847 void wxStAppResource::CloseSharedLibraryResource()
850 // Close the shared library resource file
851 if (gSharedLibraryResource
!= kResFileNotOpened
) {
853 if (gSharedLibraryBundle
!= NULL
) {
854 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
855 gSharedLibraryResource
);
856 gSharedLibraryBundle
= NULL
;
859 #endif /* __DARWIN__ */
861 CloseResFile(gSharedLibraryResource
);
863 gSharedLibraryResource
= kResFileNotOpened
;
865 #endif /* WXMAKINGDLL */
868 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
870 // for shared libraries we have to manually get the correct resource
871 // ref num upon initializing and releasing when terminating, therefore
872 // the __wxinitialize and __wxterminate must be used
875 void __sinit(void); /* (generated by linker) */
876 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
877 pascal void __terminate(void);
880 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
882 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
883 return __initialize( theInitBlock
) ;
886 pascal void __wxterminate(void)
888 wxStAppResource::CloseSharedLibraryResource() ;
892 #endif /* WXMAKINGDLL && !__DARWIN__ */
894 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
896 return wxApp::Initialize();
899 int WXDLLEXPORT
wxEntryInitGui()
901 return wxTheApp
->OnInitGui();
904 void WXDLLEXPORT
wxEntryCleanup()
909 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
912 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
913 // This seems to be necessary since there are 'rogue'
914 // objects present at this point (perhaps global objects?)
915 // Setting a checkpoint will ignore them as far as the
916 // memory checking facility is concerned.
917 // Of course you may argue that memory allocated in globals should be
918 // checked, but this is a reasonable compromise.
919 wxDebugContext::SetCheckpoint();
922 if (!wxEntryStart(argc
, argv
)) {
925 // create the application object or ensure that one already exists
928 // The app may have declared a global application object, but we recommend
929 // the IMPLEMENT_APP macro is used instead, which sets an initializer
930 // function for delayed, dynamic app object construction.
931 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
932 wxT("No initializer - use IMPLEMENT_APP macro.") );
934 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
937 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
940 // Mac OS X passes a process serial number command line argument when
941 // the application is launched from the Finder. This argument must be
942 // removed from the command line arguments before being handled by the
943 // application (otherwise applications would need to handle it)
946 if (strncmp(argv
[1], "-psn_", 5) == 0) {
947 // assume the argument is always the only one and remove it
952 argc
= 0 ; // currently we don't support files as parameters
954 // we could try to get the open apple events here to adjust argc and argv better
956 wxTheApp
->argc
= argc
;
958 wxTheApp
->argv
= new wxChar
*[argc
+1];
960 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
962 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
964 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
966 wxTheApp
->argv
= argv
;
969 // GUI-specific initialization, such as creating an app context.
972 // Here frames insert themselves automatically
973 // into wxTopLevelWindows by getting created
978 if ( wxTheApp
->OnInit() )
982 retValue
= wxTheApp
->OnRun();
985 // We want to initialize, but not run or exit immediately.
988 //else: app initialization failed, so we skipped OnRun()
990 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
993 // Forcibly delete the window.
994 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
995 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
997 topWindow
->Close(TRUE
);
998 wxTheApp
->DeletePendingObjects();
1003 wxTheApp
->SetTopWindow(NULL
);
1016 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1018 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1019 OSStatus err
= noErr
;
1022 switch( GetEventClass( event
) )
1024 case kEventClassKeyboard
:
1027 switch( GetEventKind(event
) )
1029 case kEventRawKeyDown
:
1030 rec
->what
= keyDown
;
1032 case kEventRawKeyRepeat
:
1033 rec
->what
= autoKey
;
1035 case kEventRawKeyUp
:
1038 case kEventRawKeyModifiersChanged
:
1039 rec
->what
= nullEvent
;
1048 unsigned char charCode
;
1050 GetMouse( &rec
->where
) ;
1052 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1053 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1054 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1055 rec
->modifiers
= modifiers
;
1056 rec
->message
= (keyCode
<< 8 ) + charCode
;
1060 case kEventClassTextInput
:
1062 switch( GetEventKind( event
) )
1064 case kEventTextInputUnicodeForKeyEvent
:
1067 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1071 unsigned char charCode
;
1073 GetMouse( &rec
->where
) ;
1074 rec
->what
= keyDown
;
1075 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1076 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1077 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1078 rec
->modifiers
= modifiers
;
1079 rec
->message
= (keyCode
<< 8 ) + charCode
;
1095 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1097 OSStatus result = eventNotHandledErr ;
1100 switch ( GetEventClass( event ) )
1102 case kEventClassKeyboard :
1103 if ( wxMacConvertEventToRecord( event , &rec ) )
1105 wxTheApp->MacHandleModifierEvents( &rec ) ;
1106 wxTheApp->MacHandleOneEvent( &rec ) ;
1110 case kEventClassTextInput :
1111 if ( wxMacConvertEventToRecord( event , &rec ) )
1113 wxTheApp->MacHandleModifierEvents( &rec ) ;
1114 wxTheApp->MacHandleOneEvent( &rec ) ;
1126 // Static member initialization
1127 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1134 #if WXWIN_COMPATIBILITY_2_2
1135 m_wantDebugOutput
= TRUE
;
1141 m_printMode
= wxPRINT_WINDOWS
;
1145 bool wxApp::Initialized()
1153 int wxApp::MainLoop()
1165 // Returns TRUE if more time is needed.
1166 bool wxApp::ProcessIdle()
1169 event
.SetEventObject(this);
1170 ProcessEvent(event
);
1172 return event
.MoreRequested();
1175 void wxApp::ExitMainLoop()
1177 m_keepGoing
= FALSE
;
1180 // Is a message/event pending?
1181 bool wxApp::Pending()
1184 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1188 return EventAvail( everyEvent
, &event
) ;
1192 // Dispatch a message.
1193 void wxApp::Dispatch()
1198 void wxApp::OnIdle(wxIdleEvent
& event
)
1200 // Avoid recursion (via ProcessEvent default case)
1207 // 'Garbage' collection of windows deleted with Close().
1208 DeletePendingObjects();
1210 // flush the logged messages if any
1211 wxLog
*pLog
= wxLog::GetActiveTarget();
1212 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1215 // Send OnIdle events to all windows
1216 bool needMore
= SendIdleEvents();
1219 event
.RequestMore(TRUE
);
1221 // If they are pending events, we must process them: pending events are
1222 // either events to the threads other than main or events posted with
1223 // wxPostEvent() functions
1224 wxMacProcessNotifierAndPendingEvents();
1234 // Send idle event to all top-level windows
1235 bool wxApp::SendIdleEvents()
1237 bool needMore
= FALSE
;
1238 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1241 wxWindow
* win
= node
->GetData();
1242 if (SendIdleEvents(win
))
1245 node
= node
->GetNext();
1250 // Send idle event to window and all subwindows
1251 bool wxApp::SendIdleEvents(wxWindow
* win
)
1253 bool needMore
= FALSE
;
1256 event
.SetEventObject(win
);
1257 win
->ProcessEvent(event
);
1259 if (event
.MoreRequested())
1262 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1265 wxWindow
* win
= node
->GetData();
1266 if (SendIdleEvents(win
))
1269 node
= node
->GetNext();
1274 void wxApp::DeletePendingObjects()
1276 wxNode
*node
= wxPendingDelete
.GetFirst();
1279 wxObject
*obj
= (wxObject
*)node
->GetData();
1283 if (wxPendingDelete
.Member(obj
))
1286 // Deleting one object may have deleted other pending
1287 // objects, so start from beginning of list again.
1288 node
= wxPendingDelete
.GetFirst();
1294 wxLogError(_("Fatal error: exiting"));
1300 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1303 GetTopWindow()->Close(TRUE
);
1306 // Default behaviour: close the application with prompts. The
1307 // user can veto the close, and therefore the end session.
1308 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1312 if (!GetTopWindow()->Close(!event
.CanVeto()))
1317 extern "C" void wxCYield() ;
1323 // Yield to other processes
1325 bool wxApp::Yield(bool onlyIfNeeded
)
1329 if ( !onlyIfNeeded
)
1331 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1340 YieldToAnyThread() ;
1342 // by definition yield should handle all non-processed events
1346 OSStatus status
= noErr
;
1349 s_inReceiveEvent
= true ;
1350 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1351 s_inReceiveEvent
= false ;
1353 if ( status
== eventLoopTimedOutErr
)
1355 // make sure next time the event loop will trigger idle events
1356 sleepTime
= kEventDurationNoWait
;
1358 else if ( status
== eventLoopQuitErr
)
1360 // according to QA1061 this may also occur when a WakeUp Process
1365 MacHandleOneEvent( theEvent
) ;
1366 ReleaseEvent(theEvent
);
1368 } while( status
== noErr
) ;
1372 // having a larger value here leads to large performance slowdowns
1373 // so we cannot give background apps more processor time here
1374 // we do so however having a large sleep value in the main event loop
1377 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1379 MacHandleModifierEvents( &event
) ;
1380 MacHandleOneEvent( &event
);
1381 if ( event
.what
!= kHighLevelEvent
)
1382 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1384 MacHandleModifierEvents( &event
) ;
1387 wxMacProcessNotifierAndPendingEvents() ;
1393 // platform specifics
1395 void wxApp::MacSuspend( bool convertClipboard
)
1398 // we have to deactive the top level windows manually
1400 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1403 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1404 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1406 node
= node
->GetNext();
1409 ::HideFloatingWindows() ;
1411 s_lastMouseDown
= 0 ;
1413 if( convertClipboard
)
1415 MacConvertPrivateToPublicScrap() ;
1419 extern wxList wxModalDialogs
;
1421 void wxApp::MacResume( bool convertClipboard
)
1423 s_lastMouseDown
= 0 ;
1424 if( convertClipboard
)
1426 MacConvertPublicToPrivateScrap() ;
1430 ::ShowFloatingWindows() ;
1431 // raise modal dialogs in case a non modal window was selected to activate the app
1433 wxNode
* node
= wxModalDialogs
.GetFirst();
1436 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1439 node
= node
->GetNext();
1444 void wxApp::MacConvertPrivateToPublicScrap()
1448 void wxApp::MacConvertPublicToPrivateScrap()
1452 void wxApp::MacDoOneEvent()
1457 s_inReceiveEvent
= true ;
1458 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1459 s_inReceiveEvent
= false ;
1460 if ( status
== eventLoopTimedOutErr
)
1462 if ( wxTheApp
->ProcessIdle() )
1463 sleepTime
= kEventDurationNoWait
;
1465 sleepTime
= kEventDurationForever
;
1467 else if ( status
== eventLoopQuitErr
)
1469 // according to QA1061 this may also occur when a WakeUp Process
1474 MacHandleOneEvent( theEvent
) ;
1475 ReleaseEvent(theEvent
);
1476 sleepTime
= kEventDurationNoWait
;
1481 EventMask eventMask
= everyEvent
;
1483 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1485 MacHandleModifierEvents( &event
) ;
1486 MacHandleOneEvent( &event
);
1490 MacHandleModifierEvents( &event
) ;
1492 WindowPtr window
= ::FrontWindow() ;
1494 ::IdleControls( window
) ;
1496 if ( wxTheApp
->ProcessIdle() )
1499 sleepTime
= GetCaretTime() / 2 ;
1501 if ( event
.what
!= kHighLevelEvent
)
1502 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1506 DeletePendingObjects() ;
1507 wxMacProcessNotifierAndPendingEvents() ;
1510 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1513 EventTargetRef theTarget
;
1514 theTarget
= GetEventDispatcherTarget();
1515 m_macCurrentEvent
= evr
;
1516 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1518 EventRecord
* ev
= (EventRecord
*) evr
;
1519 m_macCurrentEvent
= ev
;
1521 wxApp::sm_lastMessageTime
= ev
->when
;
1526 MacHandleMouseDownEvent( ev
) ;
1527 if ( ev
->modifiers
& controlKey
)
1528 s_lastMouseDown
= 2;
1530 s_lastMouseDown
= 1;
1533 if ( s_lastMouseDown
== 2 )
1535 ev
->modifiers
|= controlKey
;
1539 ev
->modifiers
&= ~controlKey
;
1541 MacHandleMouseUpEvent( ev
) ;
1542 s_lastMouseDown
= 0;
1545 MacHandleActivateEvent( ev
) ;
1548 MacHandleUpdateEvent( ev
) ;
1552 MacHandleKeyDownEvent( ev
) ;
1555 MacHandleKeyUpEvent( ev
) ;
1558 MacHandleDiskEvent( ev
) ;
1561 MacHandleOSEvent( ev
) ;
1563 case kHighLevelEvent
:
1564 MacHandleHighLevelEvent( ev
) ;
1570 wxMacProcessNotifierAndPendingEvents() ;
1574 bool s_macIsInModalLoop
= false ;
1576 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1578 EventRecord
* ev
= (EventRecord
*) evr
;
1579 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1581 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1583 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1584 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1585 event
.m_altDown
= ev
->modifiers
& optionKey
;
1586 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1588 event
.m_x
= ev
->where
.h
;
1589 event
.m_y
= ev
->where
.v
;
1590 event
.m_timeStamp
= ev
->when
;
1591 wxWindow
* focus
= wxWindow::FindFocus() ;
1592 event
.SetEventObject(focus
);
1594 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1596 event
.m_keyCode
= WXK_CONTROL
;
1597 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1598 focus
->GetEventHandler()->ProcessEvent( event
) ;
1600 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1602 event
.m_keyCode
= WXK_SHIFT
;
1603 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1604 focus
->GetEventHandler()->ProcessEvent( event
) ;
1606 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1608 event
.m_keyCode
= WXK_ALT
;
1609 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1610 focus
->GetEventHandler()->ProcessEvent( event
) ;
1612 s_lastModifiers
= ev
->modifiers
;
1616 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1618 // we must avoid reentrancy problems when processing high level events eg printing
1619 bool former
= s_inYield
;
1621 EventRecord
* ev
= (EventRecord
*) evr
;
1622 ::AEProcessAppleEvent( ev
) ;
1623 s_inYield
= former
;
1626 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1628 EventRecord
* ev
= (EventRecord
*) evr
;
1629 wxToolTip::RemoveToolTips() ;
1632 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1633 WindowAttributes frontWindowAttributes
= NULL
;
1635 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1637 short windowPart
= ::FindWindow(ev
->where
, &window
);
1638 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1639 if ( wxPendingDelete
.Member(win
) )
1643 GetQDGlobalsScreenBits( &screenBits
);
1648 if ( s_macIsInModalLoop
)
1654 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1655 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1656 s_lastMouseDown
= 0;
1660 SystemClick( ev
, window
) ;
1661 s_lastMouseDown
= 0;
1664 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1670 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1675 Point pt
= { 0, 0 } ;
1676 SetPortWindowPort(window
) ;
1677 LocalToGlobal( &pt
) ;
1679 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1680 -1 , wxSIZE_USE_EXISTING
);
1682 s_lastMouseDown
= 0;
1686 if (TrackGoAway(window
, ev
->where
))
1691 s_lastMouseDown
= 0;
1695 Rect newContentRect
;
1696 Rect constraintRect
;
1697 constraintRect
.top
= win
->GetMinHeight() ;
1698 if ( constraintRect
.top
== -1 )
1699 constraintRect
.top
= 0 ;
1700 constraintRect
.left
= win
->GetMinWidth() ;
1701 if ( constraintRect
.left
== -1 )
1702 constraintRect
.left
= 0 ;
1703 constraintRect
.right
= win
->GetMaxWidth() ;
1704 if ( constraintRect
.right
== -1 )
1705 constraintRect
.right
= 32000 ;
1706 constraintRect
.bottom
= win
->GetMaxHeight() ;
1707 if ( constraintRect
.bottom
== -1 )
1708 constraintRect
.bottom
= 32000 ;
1710 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1711 &constraintRect
, &newContentRect
) ;
1714 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1715 newContentRect
.right
- newContentRect
.left
,
1716 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1718 s_lastMouseDown
= 0;
1723 if (TrackBox(window
, ev
->where
, windowPart
))
1725 // TODO setup size event
1726 ZoomWindow( window
, windowPart
, false ) ;
1732 Point pt
= { 0, 0 } ;
1733 SetPortWindowPort(window
) ;
1734 LocalToGlobal( &pt
) ;
1737 GetWindowPortBounds(window
, &tempRect
) ;
1738 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1739 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1742 s_lastMouseDown
= 0;
1744 case inCollapseBox
:
1745 // TODO setup size event
1746 s_lastMouseDown
= 0;
1753 SetPortWindowPort(window
) ;
1756 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1758 if ( s_macIsInModalLoop
)
1762 else if ( UMAIsWindowFloating( window
) )
1765 win
->MacMouseDown( ev
, windowPart
) ;
1770 win
->MacMouseDown( ev
, windowPart
) ;
1771 ::SelectWindow( window
) ;
1777 win
->MacMouseDown( ev
, windowPart
) ;
1785 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1787 EventRecord
* ev
= (EventRecord
*) evr
;
1790 short windowPart
= inNoWindow
;
1791 if ( wxTheApp
->s_captureWindow
)
1793 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1794 windowPart
= inContent
;
1798 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1809 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1811 win
->MacMouseUp( ev
, windowPart
) ;
1819 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1820 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1825 case kHomeCharCode
:
1828 case kEnterCharCode
:
1829 retval
= WXK_RETURN
;
1834 case kHelpCharCode
:
1837 case kBackspaceCharCode
:
1843 case kPageUpCharCode
:
1844 retval
= WXK_PAGEUP
;
1846 case kPageDownCharCode
:
1847 retval
= WXK_PAGEDOWN
;
1849 case kReturnCharCode
:
1850 retval
= WXK_RETURN
;
1852 case kFunctionKeyCharCode
:
1904 case kEscapeCharCode
:
1905 retval
= WXK_ESCAPE
;
1907 case kLeftArrowCharCode
:
1910 case kRightArrowCharCode
:
1911 retval
= WXK_RIGHT
;
1913 case kUpArrowCharCode
:
1916 case kDownArrowCharCode
:
1919 case kDeleteCharCode
:
1920 retval
= WXK_DELETE
;
1929 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1931 EventRecord
* ev
= (EventRecord
*) evr
;
1932 wxToolTip::RemoveToolTips() ;
1934 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1935 if ( HiWord( menuresult
) )
1937 if ( !s_macIsInModalLoop
)
1938 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1942 wxWindow
* focus
= wxWindow::FindFocus() ;
1944 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1946 // has not been handled -> perform default
1947 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1948 if ( control
&& control
->GetMacControl() != NULL
)
1952 keychar
= short(ev
->message
& charCodeMask
);
1953 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1954 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1960 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1962 EventRecord
* ev
= (EventRecord
*) evr
;
1963 wxToolTip::RemoveToolTips() ;
1965 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1966 if ( HiWord( menuresult
) )
1971 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1977 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1984 keychar
= short(keymessage
& charCodeMask
);
1985 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1987 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1989 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1990 // and look at the character after
1992 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1993 keychar
= short(keyInfo
& charCodeMask
);
1994 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1996 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1997 long realkeyval
= keyval
;
1998 if ( keyval
== keychar
)
2000 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
2001 realkeyval
= short(keymessage
& charCodeMask
) ;
2002 keyval
= wxToupper( keyval
) ;
2005 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2006 bool handled
= false ;
2007 event
.m_shiftDown
= modifiers
& shiftKey
;
2008 event
.m_controlDown
= modifiers
& controlKey
;
2009 event
.m_altDown
= modifiers
& optionKey
;
2010 event
.m_metaDown
= modifiers
& cmdKey
;
2011 event
.m_keyCode
= keyval
;
2015 event
.m_timeStamp
= when
;
2016 event
.SetEventObject(focus
);
2017 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2018 if ( handled
&& event
.GetSkipped() )
2025 wxWindow
*ancestor
= focus
;
2028 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2031 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2032 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2035 if (ancestor
->IsTopLevel())
2037 ancestor
= ancestor
->GetParent();
2040 #endif // wxUSE_ACCEL
2044 event
.Skip( FALSE
) ;
2045 event
.SetEventType( wxEVT_CHAR
) ;
2047 event
.m_keyCode
= realkeyval
;
2049 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2050 if ( handled
&& event
.GetSkipped() )
2054 (keyval
== WXK_TAB
) &&
2055 // CS: copied the change below from wxGTK
2056 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2057 // have this style, yet choose not to process this particular TAB in which
2058 // case TAB must still work as a navigational character
2060 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2062 (focus
->GetParent()) &&
2063 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2065 wxNavigationKeyEvent new_event
;
2066 new_event
.SetEventObject( focus
);
2067 new_event
.SetDirection( !event
.ShiftDown() );
2068 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2069 new_event
.SetWindowChange( event
.ControlDown() );
2070 new_event
.SetCurrentFocus( focus
);
2071 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2072 if ( handled
&& new_event
.GetSkipped() )
2075 // backdoor handler for default return and command escape
2076 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2078 // if window is not having a focus still testing for default enter or cancel
2079 // TODO add the UMA version for ActiveNonFloatingWindow
2080 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2083 if ( keyval
== WXK_RETURN
)
2085 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2087 if ( def
&& def
->IsEnabled() )
2089 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2090 event
.SetEventObject(def
);
2091 def
->Command(event
);
2095 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2096 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2098 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2099 new_event
.SetEventObject( focus
);
2100 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2107 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2114 keychar
= short(keymessage
& charCodeMask
);
2115 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2116 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2118 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2119 // and look at the character after
2121 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2122 keychar
= short(keyInfo
& charCodeMask
);
2123 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2125 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2127 if ( keyval
== keychar
)
2129 keyval
= wxToupper( keyval
) ;
2131 bool handled
= false ;
2133 wxKeyEvent
event(wxEVT_KEY_UP
);
2134 event
.m_shiftDown
= modifiers
& shiftKey
;
2135 event
.m_controlDown
= modifiers
& controlKey
;
2136 event
.m_altDown
= modifiers
& optionKey
;
2137 event
.m_metaDown
= modifiers
& cmdKey
;
2138 event
.m_keyCode
= keyval
;
2142 event
.m_timeStamp
= when
;
2143 event
.SetEventObject(focus
);
2144 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2150 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2152 EventRecord
* ev
= (EventRecord
*) evr
;
2153 WindowRef window
= (WindowRef
) ev
->message
;
2156 bool activate
= (ev
->modifiers
& activeFlag
) ;
2157 WindowClass wclass
;
2158 ::GetWindowClass ( window
, &wclass
) ;
2159 if ( wclass
== kFloatingWindowClass
)
2161 // if it is a floater we activate/deactivate the front non-floating window instead
2162 window
= ::FrontNonFloatingWindow() ;
2164 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2166 win
->MacActivate( ev
->when
, activate
) ;
2170 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2172 EventRecord
* ev
= (EventRecord
*) evr
;
2173 WindowRef window
= (WindowRef
) ev
->message
;
2174 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2177 if ( !wxPendingDelete
.Member(win
) )
2178 win
->MacUpdate( ev
->when
) ;
2182 // since there is no way of telling this foreign window to update itself
2183 // we have to invalidate the update region otherwise we keep getting the same
2184 // event over and over again
2185 BeginUpdate( window
) ;
2186 EndUpdate( window
) ;
2190 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2192 EventRecord
* ev
= (EventRecord
*) evr
;
2193 if ( HiWord( ev
->message
) != noErr
)
2197 SetPt( &point
, 100 , 100 ) ;
2199 err
= DIBadMount( point
, ev
->message
) ;
2200 wxASSERT( err
== noErr
) ;
2204 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2206 EventRecord
* ev
= (EventRecord
*) evr
;
2207 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2209 case suspendResumeMessage
:
2211 bool isResuming
= ev
->message
& resumeFlag
;
2212 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2214 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2217 WindowRef oldFrontWindow
= NULL
;
2218 WindowRef newFrontWindow
= NULL
;
2220 // in case we don't take care of activating ourselves, we have to synchronize
2221 // our idea of the active window with the process manager's - which it already activated
2223 if ( !doesActivate
)
2224 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2226 MacResume( convertClipboard
) ;
2228 newFrontWindow
= ::FrontNonFloatingWindow() ;
2230 if ( oldFrontWindow
)
2232 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2234 win
->MacActivate( ev
->when
, false ) ;
2236 if ( newFrontWindow
)
2238 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2240 win
->MacActivate( ev
->when
, true ) ;
2245 MacSuspend( convertClipboard
) ;
2249 case mouseMovedMessage
:
2253 wxWindow
* currentMouseWindow
= NULL
;
2255 if (s_captureWindow
)
2257 currentMouseWindow
= s_captureWindow
;
2261 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2262 ¤tMouseWindow
) ;
2265 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2267 wxMouseEvent event
;
2269 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2270 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2272 event
.m_leftDown
= isDown
&& !controlDown
;
2273 event
.m_middleDown
= FALSE
;
2274 event
.m_rightDown
= isDown
&& controlDown
;
2275 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2276 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2277 event
.m_altDown
= ev
->modifiers
& optionKey
;
2278 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2279 event
.m_x
= ev
->where
.h
;
2280 event
.m_y
= ev
->where
.v
;
2281 event
.m_timeStamp
= ev
->when
;
2282 event
.SetEventObject(this);
2284 if ( wxWindow::s_lastMouseWindow
)
2286 wxMouseEvent
eventleave(event
);
2287 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2288 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2289 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2291 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2293 if ( currentMouseWindow
)
2295 wxMouseEvent
evententer(event
);
2296 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2297 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2298 evententer
.SetEventObject( currentMouseWindow
) ;
2299 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2301 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2304 short windowPart
= inNoWindow
;
2306 if ( s_captureWindow
)
2308 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2309 windowPart
= inContent
;
2313 windowPart
= ::FindWindow(ev
->where
, &window
);
2320 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2322 win
->MacMouseMoved( ev
, windowPart
) ;
2329 UMAShowArrowCursor();
2339 UMAShowArrowCursor();
2350 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2352 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2353 wxFrame
* frame
= mbar
->GetFrame();
2354 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2357 frame
->ProcessCommand(id
);
2362 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2365 return; // no menu item selected
2367 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2370 Str255 deskAccessoryName
;
2373 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2374 GetPort(&savedPort
);
2375 OpenDeskAcc(deskAccessoryName
);
2382 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2383 MacHandleMenuCommand( id
) ;