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"
41 #if wxUSE_WX_RESOURCES
42 # include "wx/resource.h"
57 #include "wx/mac/uma.h"
58 #include "wx/mac/macnotfy.h"
61 # include <CoreServices/CoreServices.h>
62 # if defined(WXMAKINGDLL)
63 # include <mach-o/dyld.h>
68 # include <ToolUtils.h>
69 # include <DiskInit.h>
73 extern wxList wxPendingDelete
;
74 extern wxList
*wxWinMacWindowList
;
75 extern wxList
*wxWinMacControlList
;
77 // statics for implementation
79 static bool s_inYield
= FALSE
;
80 static bool s_inOnIdle
= FALSE
;
83 static bool s_inReceiveEvent
= FALSE
;
84 static EventTime sleepTime
= kEventDurationNoWait
;
86 static long sleepTime
= 0 ;
89 wxApp
*wxTheApp
= NULL
;
91 #if !USE_SHARED_LIBRARY
92 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
93 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
94 EVT_IDLE(wxApp::OnIdle
)
95 EVT_END_SESSION(wxApp::OnEndSession
)
96 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
101 const short kMacMinHeap
= (29 * 1024) ;
102 // platform specific static variables
104 const short kwxMacMenuBarResource
= 1 ;
105 const short kwxMacAppleMenuId
= 1 ;
107 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
108 wxWindow
* wxApp::s_captureWindow
= NULL
;
109 int wxApp::s_lastMouseDown
= 0 ;
110 long wxApp::sm_lastMessageTime
= 0;
111 long wxApp::s_lastModifiers
= 0 ;
114 bool wxApp::s_macDefaultEncodingIsPC
= true ;
115 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
116 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
117 long wxApp::s_macPreferencesMenuItemId
= 0 ;
118 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
119 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
121 //----------------------------------------------------------------------
122 // Core Apple Event Support
123 //----------------------------------------------------------------------
125 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
150 // AEODoc Calls MacOpenFile on each of the files passed
152 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
156 DescType returnedType
;
162 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
166 err
= AECountItems(&docList
, &itemsInList
);
170 ProcessSerialNumber PSN
;
171 PSN
.highLongOfPSN
= 0 ;
172 PSN
.lowLongOfPSN
= kCurrentProcess
;
173 SetFrontProcess( &PSN
) ;
175 for (i
= 1; i
<= itemsInList
; i
++) {
176 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
177 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
178 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
184 // AEPDoc Calls MacPrintFile on each of the files passed
186 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
190 DescType returnedType
;
196 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
200 err
= AECountItems(&docList
, &itemsInList
);
204 ProcessSerialNumber PSN
;
205 PSN
.highLongOfPSN
= 0 ;
206 PSN
.lowLongOfPSN
= kCurrentProcess
;
207 SetFrontProcess( &PSN
) ;
209 for (i
= 1; i
<= itemsInList
; i
++) {
210 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
211 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
212 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
218 // AEOApp calls MacNewFile
220 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
226 // AEQuit attempts to quite the application
228 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
230 /* wxWindow* win = GetTopWindow() ;
243 //----------------------------------------------------------------------
244 // Support Routines linking the Mac...File Calls to the Document Manager
245 //----------------------------------------------------------------------
247 void wxApp::MacOpenFile(const wxString
& fileName
)
249 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
251 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
254 void wxApp::MacPrintFile(const wxString
& fileName
)
256 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
259 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
262 wxView
* view
= doc
->GetFirstView() ;
265 wxPrintout
*printout
= view
->OnCreatePrintout();
269 printer
.Print(view
->GetFrame(), printout
, TRUE
);
275 doc
->DeleteAllViews();
276 dm
->RemoveDocument(doc
) ;
282 void wxApp::MacNewFile()
286 //----------------------------------------------------------------------
287 // Carbon Event Handler
288 //----------------------------------------------------------------------
292 static const EventTypeSpec eventList
[] =
294 { kEventClassCommand
, kEventProcessCommand
} ,
295 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
297 { kEventClassApplication
, kEventAppActivated
} ,
298 { kEventClassApplication
, kEventAppDeactivated
} ,
299 // handling the quit event is not recommended by apple
300 // rather using the quit apple event - which we do
302 { kEventClassAppleEvent
, kEventAppleEvent
} ,
304 { kEventClassMouse
, kEventMouseDown
} ,
308 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
310 OSStatus result
= eventNotHandledErr
;
311 UInt32 kind
= GetEventKind( event
) ;
316 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
317 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
318 // deal with clicks in the menu bar explicitely
320 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
322 OSStatus result
= eventNotHandledErr
;
324 switch( GetEventKind(event
) )
326 case kEventMouseDown
:
331 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
332 sizeof( Point
), NULL
, &point
);
333 short windowPart
= ::FindWindow(point
, &window
);
335 if ( windowPart
== inMenuBar
)
337 MenuSelect( point
) ;
347 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
349 OSStatus result
= eventNotHandledErr
;
353 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
354 sizeof( HICommand
), NULL
, &command
);
356 MenuCommand id
= command
.commandID
;
357 if ( id
== kHICommandPreferences
)
358 id
= wxApp::s_macPreferencesMenuItemId
;
360 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
361 wxMenu
* menu
= NULL
;
362 wxMenuItem
* item
= NULL
;
365 item
= mbar
->FindItem( id
, &menu
) ;
367 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
370 switch( GetEventKind( event
) )
372 case kEventProcessCommand
:
374 if (item
->IsCheckable())
376 item
->Check( !item
->IsChecked() ) ;
379 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
383 case kEventCommandUpdateStatus
:
384 // eventually trigger an updateui round
394 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
396 OSStatus result
= eventNotHandledErr
;
397 switch ( GetEventKind( event
) )
399 case kEventAppActivated
:
401 wxTheApp
->MacResume( true ) ;
405 case kEventAppDeactivated
:
407 wxTheApp
->MacSuspend( true ) ;
417 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
419 OSStatus result
= eventNotHandledErr
;
420 switch( GetEventClass( event
) )
422 case kEventClassCommand
:
423 result
= CommandEventHandler( handler
, event
, data
) ;
425 case kEventClassApplication
:
426 result
= ApplicationEventHandler( handler
, event
, data
) ;
428 case kEventClassMenu
:
429 result
= MenuEventHandler( handler
, event
, data
) ;
431 case kEventClassMouse
:
432 result
= MouseEventHandler( handler
, event
, data
) ;
434 case kEventClassAppleEvent
:
437 wxMacConvertEventToRecord( event
, &rec
) ;
438 result
= AEProcessAppleEvent( &rec
) ;
448 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
452 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
453 // we know it's there ;-)
454 WXIMPORT
char std::__throws_bad_alloc
;
457 bool wxApp::Initialize()
463 UMAInitToolbox( 4 ) ;
464 SetEventMask( everyEvent
) ;
465 UMAShowWatchCursor() ;
467 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
468 // open shared library resources from here since we don't have
469 // __wxinitialize in Mach-O shared libraries
470 wxStAppResource::OpenSharedLibraryResource(NULL
);
474 // test the minimal configuration necessary
480 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
482 error
= kMacSTRWrongMachine
;
484 else if (theMachine
< gestaltMacPlus
)
486 error
= kMacSTRWrongMachine
;
488 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
490 error
= kMacSTROldSystem
;
492 else if ( theSystem
< 0x0860 )
494 error
= kMacSTROldSystem
;
496 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
498 error
= kMacSTRSmallSize
;
504 if ( !UMAHasAppearance() )
506 error = kMacSTRNoPre8Yet ;
512 // if we encountered any problems so far, give the error code and exit immediately
516 wxStAppResource resload
;
520 GetIndString(message
, 128, error
);
521 UMAShowArrowCursor() ;
522 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
523 itemHit
= Alert(128, nil
);
528 # if __option(profile)
529 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
534 // now avoid exceptions thrown for new (bad_alloc)
535 // FIXME CS for some changes outside wxMac does not compile anymore
537 std::__throws_bad_alloc
= 0 ;
541 wxMacSetupConverters() ;
543 s_macCursorRgn
= ::NewRgn() ;
545 wxClassInfo::InitializeClasses();
548 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
552 wxPendingEventsLocker
= new wxCriticalSection
;
555 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
556 wxTheColourDatabase
->Initialize();
560 // flush the logged messages if any and install a 'safer' log target: the
561 // default one (wxLogGui) can't be used after the resources are freed just
562 // below and the user suppliedo ne might be even more unsafe (using any
563 // wxWindows GUI function is unsafe starting from now)
564 wxLog::DontCreateOnDemand();
566 // this will flush the old messages if any
567 delete wxLog::SetActiveTarget(new wxLogStderr
);
571 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
572 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
574 wxInitializeStockLists();
575 wxInitializeStockObjects();
577 #if wxUSE_WX_RESOURCES
578 wxInitializeResourceSystem();
581 wxBitmap::InitStandardHandlers();
583 wxModule::RegisterModules();
584 if (!wxModule::InitializeModules()) {
588 wxMacCreateNotifierTable() ;
590 UMAShowArrowCursor() ;
595 bool wxApp::OnInitGui()
597 if( !wxAppBase::OnInitGui() )
601 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
603 InstallApplicationEventHandler(
604 GetwxAppEventHandlerUPP(),
605 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
608 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
609 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
610 NewAEEventHandlerUPP(AEHandleODoc
) ,
612 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
613 NewAEEventHandlerUPP(AEHandleOApp
) ,
615 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
616 NewAEEventHandlerUPP(AEHandlePDoc
) ,
618 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
619 NewAEEventHandlerUPP(AEHandleQuit
) ,
622 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
623 NewAEEventHandlerProc(AEHandleODoc
) ,
625 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
626 NewAEEventHandlerProc(AEHandleOApp
) ,
628 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
629 NewAEEventHandlerProc(AEHandlePDoc
) ,
631 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
632 NewAEEventHandlerProc(AEHandleQuit
) ,
639 void wxApp::CleanUp()
641 wxToolTip::RemoveToolTips() ;
643 // flush the logged messages if any and install a 'safer' log target: the
644 // default one (wxLogGui) can't be used after the resources are freed just
645 // below and the user suppliedo ne might be even more unsafe (using any
646 // wxWindows GUI function is unsafe starting from now)
647 wxLog::DontCreateOnDemand();
649 // this will flush the old messages if any
650 delete wxLog::SetActiveTarget(new wxLogStderr
);
653 // One last chance for pending objects to be cleaned up
654 wxTheApp
->DeletePendingObjects();
656 wxModule::CleanUpModules();
658 #if wxUSE_WX_RESOURCES
659 wxCleanUpResourceSystem();
662 wxDeleteStockObjects() ;
664 // Destroy all GDI lists, etc.
665 wxDeleteStockLists();
667 delete wxTheColourDatabase
;
668 wxTheColourDatabase
= NULL
;
670 wxBitmap::CleanUpHandlers();
672 wxMacDestroyNotifierTable() ;
673 if (wxWinMacWindowList
) {
674 delete wxWinMacWindowList
;
676 if (wxWinMacControlList
) {
677 delete wxWinMacControlList
;
679 delete wxPendingEvents
;
682 delete wxPendingEventsLocker
;
683 // If we don't do the following, we get an apparent memory leak.
684 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
687 wxClassInfo::CleanUpClasses();
690 # if __option(profile)
691 ProfilerDump( "\papp.prof" ) ;
699 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
700 // At this point we want to check if there are any memory
701 // blocks that aren't part of the wxDebugContext itself,
702 // as a special case. Then when dumping we need to ignore
703 // wxDebugContext, too.
704 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
706 wxLogDebug(wxT("There were memory leaks."));
707 wxDebugContext::Dump();
708 wxDebugContext::PrintStatistics();
710 // wxDebugContext::SetStream(NULL, NULL);
714 // do it as the very last thing because everything else can log messages
715 delete wxLog::SetActiveTarget(NULL
);
718 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
719 // close shared library resources from here since we don't have
720 // __wxterminate in Mach-O shared libraries
721 wxStAppResource::CloseSharedLibraryResource();
723 wxMacCleanupConverters() ;
725 UMACleanupToolbox() ;
726 if (s_macCursorRgn
) {
727 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
735 //----------------------------------------------------------------------
737 //----------------------------------------------------------------------
739 // extern variable for shared library resource id
740 // need to be able to find it with NSLookupAndBindSymbol
741 short gSharedLibraryResource
= kResFileNotOpened
;
743 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
744 CFBundleRef gSharedLibraryBundle
= NULL
;
745 #endif /* WXMAKINGDLL && __DARWIN__ */
747 wxStAppResource::wxStAppResource()
749 m_currentRefNum
= CurResFile() ;
750 if ( gSharedLibraryResource
!= kResFileNotOpened
)
752 UseResFile( gSharedLibraryResource
) ;
756 wxStAppResource::~wxStAppResource()
758 if ( m_currentRefNum
!= kResFileNotOpened
)
760 UseResFile( m_currentRefNum
) ;
764 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
766 gSharedLibraryResource
= kResFileNotOpened
;
769 if ( initBlock
!= NULL
) {
770 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
771 FSSpec
*fileSpec
= NULL
;
773 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
774 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
776 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
777 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
780 if (fileSpec
!= NULL
) {
781 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
786 // Open the shared library resource file if it is not yet open
789 const char *theLibPath
;
791 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
792 if (gSharedLibraryBundle
!= NULL
) {
793 // wxWindows has been bundled into a framework
794 // load the framework resources
796 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
799 // wxWindows is a simple dynamic shared library
800 // load the resources from the data fork of a separate resource file
805 OSErr theErr
= noErr
;
807 // get the library path
808 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
809 theModule
= NSModuleForSymbol(theSymbol
);
810 theLibPath
= NSLibraryNameForModule(theModule
);
812 wxLogDebug( wxT("wxMac library installation name is '%s'"),
815 // allocate copy to replace .dylib.* extension with .rsrc
816 theResPath
= strdup(theLibPath
);
817 if (theResPath
!= NULL
) {
818 theName
= strrchr(theResPath
, '/');
819 if (theName
== NULL
) {
820 // no directory elements in path
821 theName
= theResPath
;
823 // find ".dylib" shared library extension
824 theExt
= strstr(theName
, ".dylib");
825 // overwrite extension with ".rsrc"
826 strcpy(theExt
, ".rsrc");
828 wxLogDebug( wxT("wxMac resources file name is '%s'"),
831 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
832 if (theErr
!= noErr
) {
833 // try in current directory (using name only)
834 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
837 // open the resource file
838 if (theErr
== noErr
) {
839 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
840 &gSharedLibraryResource
);
842 if (theErr
!= noErr
) {
843 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
847 // free duplicated resource file path
851 #endif /* __DARWIN__ */
853 #endif /* WXMAKINGDLL */
856 void wxStAppResource::CloseSharedLibraryResource()
859 // Close the shared library resource file
860 if (gSharedLibraryResource
!= kResFileNotOpened
) {
862 if (gSharedLibraryBundle
!= NULL
) {
863 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
864 gSharedLibraryResource
);
865 gSharedLibraryBundle
= NULL
;
868 #endif /* __DARWIN__ */
870 CloseResFile(gSharedLibraryResource
);
872 gSharedLibraryResource
= kResFileNotOpened
;
874 #endif /* WXMAKINGDLL */
877 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
879 // for shared libraries we have to manually get the correct resource
880 // ref num upon initializing and releasing when terminating, therefore
881 // the __wxinitialize and __wxterminate must be used
884 void __sinit(void); /* (generated by linker) */
885 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
886 pascal void __terminate(void);
889 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
891 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
892 return __initialize( theInitBlock
) ;
895 pascal void __wxterminate(void)
897 wxStAppResource::CloseSharedLibraryResource() ;
901 #endif /* WXMAKINGDLL && !__DARWIN__ */
903 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
905 return wxApp::Initialize();
908 int WXDLLEXPORT
wxEntryInitGui()
910 return wxTheApp
->OnInitGui();
913 void WXDLLEXPORT
wxEntryCleanup()
918 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
921 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
922 // This seems to be necessary since there are 'rogue'
923 // objects present at this point (perhaps global objects?)
924 // Setting a checkpoint will ignore them as far as the
925 // memory checking facility is concerned.
926 // Of course you may argue that memory allocated in globals should be
927 // checked, but this is a reasonable compromise.
928 wxDebugContext::SetCheckpoint();
931 if (!wxEntryStart(argc
, argv
)) {
934 // create the application object or ensure that one already exists
937 // The app may have declared a global application object, but we recommend
938 // the IMPLEMENT_APP macro is used instead, which sets an initializer
939 // function for delayed, dynamic app object construction.
940 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
941 wxT("No initializer - use IMPLEMENT_APP macro.") );
943 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
946 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
949 // Mac OS X passes a process serial number command line argument when
950 // the application is launched from the Finder. This argument must be
951 // removed from the command line arguments before being handled by the
952 // application (otherwise applications would need to handle it)
955 if (strncmp(argv
[1], "-psn_", 5) == 0) {
956 // assume the argument is always the only one and remove it
961 argc
= 0 ; // currently we don't support files as parameters
963 // we could try to get the open apple events here to adjust argc and argv better
965 wxTheApp
->argc
= argc
;
967 wxTheApp
->argv
= new wxChar
*[argc
+1];
969 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
971 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
973 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
975 wxTheApp
->argv
= argv
;
978 // GUI-specific initialization, such as creating an app context.
981 // Here frames insert themselves automatically
982 // into wxTopLevelWindows by getting created
987 if ( wxTheApp
->OnInit() )
991 retValue
= wxTheApp
->OnRun();
994 // We want to initialize, but not run or exit immediately.
997 //else: app initialization failed, so we skipped OnRun()
999 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1002 // Forcibly delete the window.
1003 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
1004 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
1006 topWindow
->Close(TRUE
);
1007 wxTheApp
->DeletePendingObjects();
1012 wxTheApp
->SetTopWindow(NULL
);
1025 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1027 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1028 OSStatus err
= noErr
;
1031 switch( GetEventClass( event
) )
1033 case kEventClassKeyboard
:
1036 switch( GetEventKind(event
) )
1038 case kEventRawKeyDown
:
1039 rec
->what
= keyDown
;
1041 case kEventRawKeyRepeat
:
1042 rec
->what
= autoKey
;
1044 case kEventRawKeyUp
:
1047 case kEventRawKeyModifiersChanged
:
1048 rec
->what
= nullEvent
;
1057 unsigned char charCode
;
1059 GetMouse( &rec
->where
) ;
1061 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1062 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1063 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1064 rec
->modifiers
= modifiers
;
1065 rec
->message
= (keyCode
<< 8 ) + charCode
;
1069 case kEventClassTextInput
:
1071 switch( GetEventKind( event
) )
1073 case kEventTextInputUnicodeForKeyEvent
:
1076 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1080 unsigned char charCode
;
1082 GetMouse( &rec
->where
) ;
1083 rec
->what
= keyDown
;
1084 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1085 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1086 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1087 rec
->modifiers
= modifiers
;
1088 rec
->message
= (keyCode
<< 8 ) + charCode
;
1104 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1106 OSStatus result = eventNotHandledErr ;
1109 switch ( GetEventClass( event ) )
1111 case kEventClassKeyboard :
1112 if ( wxMacConvertEventToRecord( event , &rec ) )
1114 wxTheApp->MacHandleModifierEvents( &rec ) ;
1115 wxTheApp->MacHandleOneEvent( &rec ) ;
1119 case kEventClassTextInput :
1120 if ( wxMacConvertEventToRecord( event , &rec ) )
1122 wxTheApp->MacHandleModifierEvents( &rec ) ;
1123 wxTheApp->MacHandleOneEvent( &rec ) ;
1135 // Static member initialization
1136 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1143 #if WXWIN_COMPATIBILITY_2_2
1144 m_wantDebugOutput
= TRUE
;
1150 m_printMode
= wxPRINT_WINDOWS
;
1154 bool wxApp::Initialized()
1162 int wxApp::MainLoop()
1174 // Returns TRUE if more time is needed.
1175 bool wxApp::ProcessIdle()
1178 event
.SetEventObject(this);
1179 ProcessEvent(event
);
1181 return event
.MoreRequested();
1184 void wxApp::ExitMainLoop()
1186 m_keepGoing
= FALSE
;
1189 // Is a message/event pending?
1190 bool wxApp::Pending()
1193 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1197 return EventAvail( everyEvent
, &event
) ;
1201 // Dispatch a message.
1202 void wxApp::Dispatch()
1207 void wxApp::OnIdle(wxIdleEvent
& event
)
1209 // Avoid recursion (via ProcessEvent default case)
1216 // 'Garbage' collection of windows deleted with Close().
1217 DeletePendingObjects();
1219 // flush the logged messages if any
1220 wxLog
*pLog
= wxLog::GetActiveTarget();
1221 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1224 // Send OnIdle events to all windows
1225 bool needMore
= SendIdleEvents();
1228 event
.RequestMore(TRUE
);
1230 // If they are pending events, we must process them: pending events are
1231 // either events to the threads other than main or events posted with
1232 // wxPostEvent() functions
1233 wxMacProcessNotifierAndPendingEvents();
1243 // Send idle event to all top-level windows
1244 bool wxApp::SendIdleEvents()
1246 bool needMore
= FALSE
;
1247 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1250 wxWindow
* win
= node
->GetData();
1251 if (SendIdleEvents(win
))
1254 node
= node
->GetNext();
1259 // Send idle event to window and all subwindows
1260 bool wxApp::SendIdleEvents(wxWindow
* win
)
1262 bool needMore
= FALSE
;
1265 event
.SetEventObject(win
);
1266 win
->ProcessEvent(event
);
1268 if (event
.MoreRequested())
1271 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1274 wxWindow
* win
= node
->GetData();
1275 if (SendIdleEvents(win
))
1278 node
= node
->GetNext();
1283 void wxApp::DeletePendingObjects()
1285 wxNode
*node
= wxPendingDelete
.GetFirst();
1288 wxObject
*obj
= (wxObject
*)node
->GetData();
1292 if (wxPendingDelete
.Member(obj
))
1295 // Deleting one object may have deleted other pending
1296 // objects, so start from beginning of list again.
1297 node
= wxPendingDelete
.GetFirst();
1303 wxLogError(_("Fatal error: exiting"));
1309 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1312 GetTopWindow()->Close(TRUE
);
1315 // Default behaviour: close the application with prompts. The
1316 // user can veto the close, and therefore the end session.
1317 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1321 if (!GetTopWindow()->Close(!event
.CanVeto()))
1326 extern "C" void wxCYield() ;
1332 // Yield to other processes
1334 bool wxApp::Yield(bool onlyIfNeeded
)
1338 if ( !onlyIfNeeded
)
1340 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1349 YieldToAnyThread() ;
1351 // by definition yield should handle all non-processed events
1355 OSStatus status
= noErr
;
1358 s_inReceiveEvent
= true ;
1359 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1360 s_inReceiveEvent
= false ;
1362 if ( status
== eventLoopTimedOutErr
)
1364 // make sure next time the event loop will trigger idle events
1365 sleepTime
= kEventDurationNoWait
;
1367 else if ( status
== eventLoopQuitErr
)
1369 // according to QA1061 this may also occur when a WakeUp Process
1374 MacHandleOneEvent( theEvent
) ;
1375 ReleaseEvent(theEvent
);
1377 } while( status
== noErr
) ;
1381 // having a larger value here leads to large performance slowdowns
1382 // so we cannot give background apps more processor time here
1383 // we do so however having a large sleep value in the main event loop
1386 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1388 MacHandleModifierEvents( &event
) ;
1389 MacHandleOneEvent( &event
);
1390 if ( event
.what
!= kHighLevelEvent
)
1391 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1393 MacHandleModifierEvents( &event
) ;
1396 wxMacProcessNotifierAndPendingEvents() ;
1402 // platform specifics
1404 void wxApp::MacSuspend( bool convertClipboard
)
1407 // we have to deactive the top level windows manually
1409 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1412 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1413 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1415 node
= node
->GetNext();
1418 ::HideFloatingWindows() ;
1420 s_lastMouseDown
= 0 ;
1422 if( convertClipboard
)
1424 MacConvertPrivateToPublicScrap() ;
1428 extern wxList wxModalDialogs
;
1430 void wxApp::MacResume( bool convertClipboard
)
1432 s_lastMouseDown
= 0 ;
1433 if( convertClipboard
)
1435 MacConvertPublicToPrivateScrap() ;
1439 ::ShowFloatingWindows() ;
1440 // raise modal dialogs in case a non modal window was selected to activate the app
1442 wxNode
* node
= wxModalDialogs
.GetFirst();
1445 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1448 node
= node
->GetNext();
1453 void wxApp::MacConvertPrivateToPublicScrap()
1457 void wxApp::MacConvertPublicToPrivateScrap()
1461 void wxApp::MacDoOneEvent()
1466 s_inReceiveEvent
= true ;
1467 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1468 s_inReceiveEvent
= false ;
1469 if ( status
== eventLoopTimedOutErr
)
1471 if ( wxTheApp
->ProcessIdle() )
1472 sleepTime
= kEventDurationNoWait
;
1474 sleepTime
= kEventDurationForever
;
1476 else if ( status
== eventLoopQuitErr
)
1478 // according to QA1061 this may also occur when a WakeUp Process
1483 MacHandleOneEvent( theEvent
) ;
1484 ReleaseEvent(theEvent
);
1489 EventMask eventMask
= everyEvent
;
1491 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1493 MacHandleModifierEvents( &event
) ;
1494 MacHandleOneEvent( &event
);
1498 MacHandleModifierEvents( &event
) ;
1500 WindowPtr window
= ::FrontWindow() ;
1502 ::IdleControls( window
) ;
1504 if ( wxTheApp
->ProcessIdle() )
1507 sleepTime
= GetCaretTime() / 2 ;
1509 if ( event
.what
!= kHighLevelEvent
)
1510 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1514 DeletePendingObjects() ;
1515 wxMacProcessNotifierAndPendingEvents() ;
1518 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1521 EventTargetRef theTarget
;
1522 theTarget
= GetEventDispatcherTarget();
1523 m_macCurrentEvent
= evr
;
1524 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1526 EventRecord
* ev
= (EventRecord
*) evr
;
1527 m_macCurrentEvent
= ev
;
1529 wxApp::sm_lastMessageTime
= ev
->when
;
1534 MacHandleMouseDownEvent( ev
) ;
1535 if ( ev
->modifiers
& controlKey
)
1536 s_lastMouseDown
= 2;
1538 s_lastMouseDown
= 1;
1541 if ( s_lastMouseDown
== 2 )
1543 ev
->modifiers
|= controlKey
;
1547 ev
->modifiers
&= ~controlKey
;
1549 MacHandleMouseUpEvent( ev
) ;
1550 s_lastMouseDown
= 0;
1553 MacHandleActivateEvent( ev
) ;
1556 MacHandleUpdateEvent( ev
) ;
1560 MacHandleKeyDownEvent( ev
) ;
1563 MacHandleKeyUpEvent( ev
) ;
1566 MacHandleDiskEvent( ev
) ;
1569 MacHandleOSEvent( ev
) ;
1571 case kHighLevelEvent
:
1572 MacHandleHighLevelEvent( ev
) ;
1578 wxMacProcessNotifierAndPendingEvents() ;
1582 bool s_macIsInModalLoop
= false ;
1584 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1586 EventRecord
* ev
= (EventRecord
*) evr
;
1587 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1589 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1591 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1592 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1593 event
.m_altDown
= ev
->modifiers
& optionKey
;
1594 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1596 event
.m_x
= ev
->where
.h
;
1597 event
.m_y
= ev
->where
.v
;
1598 event
.m_timeStamp
= ev
->when
;
1599 wxWindow
* focus
= wxWindow::FindFocus() ;
1600 event
.SetEventObject(focus
);
1602 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1604 event
.m_keyCode
= WXK_CONTROL
;
1605 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1606 focus
->GetEventHandler()->ProcessEvent( event
) ;
1608 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1610 event
.m_keyCode
= WXK_SHIFT
;
1611 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1612 focus
->GetEventHandler()->ProcessEvent( event
) ;
1614 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1616 event
.m_keyCode
= WXK_ALT
;
1617 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1618 focus
->GetEventHandler()->ProcessEvent( event
) ;
1620 s_lastModifiers
= ev
->modifiers
;
1624 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1626 // we must avoid reentrancy problems when processing high level events eg printing
1627 bool former
= s_inYield
;
1629 EventRecord
* ev
= (EventRecord
*) evr
;
1630 ::AEProcessAppleEvent( ev
) ;
1631 s_inYield
= former
;
1634 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1636 EventRecord
* ev
= (EventRecord
*) evr
;
1637 wxToolTip::RemoveToolTips() ;
1640 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1641 WindowAttributes frontWindowAttributes
= NULL
;
1643 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1645 short windowPart
= ::FindWindow(ev
->where
, &window
);
1646 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1647 if ( wxPendingDelete
.Member(win
) )
1651 GetQDGlobalsScreenBits( &screenBits
);
1656 if ( s_macIsInModalLoop
)
1662 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1663 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1664 s_lastMouseDown
= 0;
1668 SystemClick( ev
, window
) ;
1669 s_lastMouseDown
= 0;
1672 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1678 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1683 Point pt
= { 0, 0 } ;
1684 SetPortWindowPort(window
) ;
1685 LocalToGlobal( &pt
) ;
1687 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1688 -1 , wxSIZE_USE_EXISTING
);
1690 s_lastMouseDown
= 0;
1694 if (TrackGoAway(window
, ev
->where
))
1699 s_lastMouseDown
= 0;
1703 Rect newContentRect
;
1704 Rect constraintRect
;
1705 constraintRect
.top
= win
->GetMinHeight() ;
1706 if ( constraintRect
.top
== -1 )
1707 constraintRect
.top
= 0 ;
1708 constraintRect
.left
= win
->GetMinWidth() ;
1709 if ( constraintRect
.left
== -1 )
1710 constraintRect
.left
= 0 ;
1711 constraintRect
.right
= win
->GetMaxWidth() ;
1712 if ( constraintRect
.right
== -1 )
1713 constraintRect
.right
= 32000 ;
1714 constraintRect
.bottom
= win
->GetMaxHeight() ;
1715 if ( constraintRect
.bottom
== -1 )
1716 constraintRect
.bottom
= 32000 ;
1718 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1719 &constraintRect
, &newContentRect
) ;
1722 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1723 newContentRect
.right
- newContentRect
.left
,
1724 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1726 s_lastMouseDown
= 0;
1731 if (TrackBox(window
, ev
->where
, windowPart
))
1733 // TODO setup size event
1734 ZoomWindow( window
, windowPart
, false ) ;
1740 Point pt
= { 0, 0 } ;
1741 SetPortWindowPort(window
) ;
1742 LocalToGlobal( &pt
) ;
1745 GetWindowPortBounds(window
, &tempRect
) ;
1746 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1747 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1750 s_lastMouseDown
= 0;
1752 case inCollapseBox
:
1753 // TODO setup size event
1754 s_lastMouseDown
= 0;
1761 SetPortWindowPort(window
) ;
1764 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1766 if ( s_macIsInModalLoop
)
1770 else if ( UMAIsWindowFloating( window
) )
1773 win
->MacMouseDown( ev
, windowPart
) ;
1778 win
->MacMouseDown( ev
, windowPart
) ;
1779 ::SelectWindow( window
) ;
1785 win
->MacMouseDown( ev
, windowPart
) ;
1793 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1795 EventRecord
* ev
= (EventRecord
*) evr
;
1798 short windowPart
= inNoWindow
;
1799 if ( wxTheApp
->s_captureWindow
)
1801 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1802 windowPart
= inContent
;
1806 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1817 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1819 win
->MacMouseUp( ev
, windowPart
) ;
1827 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1828 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1833 case kHomeCharCode
:
1836 case kEnterCharCode
:
1837 retval
= WXK_RETURN
;
1842 case kHelpCharCode
:
1845 case kBackspaceCharCode
:
1851 case kPageUpCharCode
:
1852 retval
= WXK_PAGEUP
;
1854 case kPageDownCharCode
:
1855 retval
= WXK_PAGEDOWN
;
1857 case kReturnCharCode
:
1858 retval
= WXK_RETURN
;
1860 case kFunctionKeyCharCode
:
1912 case kEscapeCharCode
:
1913 retval
= WXK_ESCAPE
;
1915 case kLeftArrowCharCode
:
1918 case kRightArrowCharCode
:
1919 retval
= WXK_RIGHT
;
1921 case kUpArrowCharCode
:
1924 case kDownArrowCharCode
:
1927 case kDeleteCharCode
:
1928 retval
= WXK_DELETE
;
1937 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1939 EventRecord
* ev
= (EventRecord
*) evr
;
1940 wxToolTip::RemoveToolTips() ;
1942 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1943 if ( HiWord( menuresult
) )
1945 if ( !s_macIsInModalLoop
)
1946 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1950 wxWindow
* focus
= wxWindow::FindFocus() ;
1952 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1954 // has not been handled -> perform default
1955 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1956 if ( control
&& control
->GetMacControl() != NULL
)
1960 keychar
= short(ev
->message
& charCodeMask
);
1961 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1962 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1968 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1970 EventRecord
* ev
= (EventRecord
*) evr
;
1971 wxToolTip::RemoveToolTips() ;
1973 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1974 if ( HiWord( menuresult
) )
1979 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1985 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1992 keychar
= short(keymessage
& charCodeMask
);
1993 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1995 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1997 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1998 // and look at the character after
2000 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2001 keychar
= short(keyInfo
& charCodeMask
);
2002 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2004 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2005 long realkeyval
= keyval
;
2006 if ( keyval
== keychar
)
2008 // 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)
2009 realkeyval
= short(keymessage
& charCodeMask
) ;
2010 keyval
= wxToupper( keyval
) ;
2013 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2014 bool handled
= false ;
2015 event
.m_shiftDown
= modifiers
& shiftKey
;
2016 event
.m_controlDown
= modifiers
& controlKey
;
2017 event
.m_altDown
= modifiers
& optionKey
;
2018 event
.m_metaDown
= modifiers
& cmdKey
;
2019 event
.m_keyCode
= keyval
;
2023 event
.m_timeStamp
= when
;
2024 event
.SetEventObject(focus
);
2025 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2026 if ( handled
&& event
.GetSkipped() )
2033 wxWindow
*ancestor
= focus
;
2036 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2039 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2040 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2043 if (ancestor
->IsTopLevel())
2045 ancestor
= ancestor
->GetParent();
2048 #endif // wxUSE_ACCEL
2052 event
.Skip( FALSE
) ;
2053 event
.SetEventType( wxEVT_CHAR
) ;
2055 event
.m_keyCode
= realkeyval
;
2057 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2058 if ( handled
&& event
.GetSkipped() )
2062 (keyval
== WXK_TAB
) &&
2063 // CS: copied the change below from wxGTK
2064 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2065 // have this style, yet choose not to process this particular TAB in which
2066 // case TAB must still work as a navigational character
2068 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2070 (focus
->GetParent()) &&
2071 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2073 wxNavigationKeyEvent new_event
;
2074 new_event
.SetEventObject( focus
);
2075 new_event
.SetDirection( !event
.ShiftDown() );
2076 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2077 new_event
.SetWindowChange( event
.ControlDown() );
2078 new_event
.SetCurrentFocus( focus
);
2079 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2080 if ( handled
&& new_event
.GetSkipped() )
2083 // backdoor handler for default return and command escape
2084 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2086 // if window is not having a focus still testing for default enter or cancel
2087 // TODO add the UMA version for ActiveNonFloatingWindow
2088 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2091 if ( keyval
== WXK_RETURN
)
2093 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2095 if ( def
&& def
->IsEnabled() )
2097 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2098 event
.SetEventObject(def
);
2099 def
->Command(event
);
2103 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2104 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2106 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2107 new_event
.SetEventObject( focus
);
2108 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2115 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2122 keychar
= short(keymessage
& charCodeMask
);
2123 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2124 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2126 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2127 // and look at the character after
2129 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2130 keychar
= short(keyInfo
& charCodeMask
);
2131 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2133 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2135 if ( keyval
== keychar
)
2137 keyval
= wxToupper( keyval
) ;
2139 bool handled
= false ;
2141 wxKeyEvent
event(wxEVT_KEY_UP
);
2142 event
.m_shiftDown
= modifiers
& shiftKey
;
2143 event
.m_controlDown
= modifiers
& controlKey
;
2144 event
.m_altDown
= modifiers
& optionKey
;
2145 event
.m_metaDown
= modifiers
& cmdKey
;
2146 event
.m_keyCode
= keyval
;
2150 event
.m_timeStamp
= when
;
2151 event
.SetEventObject(focus
);
2152 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2158 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2160 EventRecord
* ev
= (EventRecord
*) evr
;
2161 WindowRef window
= (WindowRef
) ev
->message
;
2164 bool activate
= (ev
->modifiers
& activeFlag
) ;
2165 WindowClass wclass
;
2166 ::GetWindowClass ( window
, &wclass
) ;
2167 if ( wclass
== kFloatingWindowClass
)
2169 // if it is a floater we activate/deactivate the front non-floating window instead
2170 window
= ::FrontNonFloatingWindow() ;
2172 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2174 win
->MacActivate( ev
->when
, activate
) ;
2178 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2180 EventRecord
* ev
= (EventRecord
*) evr
;
2181 WindowRef window
= (WindowRef
) ev
->message
;
2182 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2185 if ( !wxPendingDelete
.Member(win
) )
2186 win
->MacUpdate( ev
->when
) ;
2190 // since there is no way of telling this foreign window to update itself
2191 // we have to invalidate the update region otherwise we keep getting the same
2192 // event over and over again
2193 BeginUpdate( window
) ;
2194 EndUpdate( window
) ;
2198 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2200 EventRecord
* ev
= (EventRecord
*) evr
;
2201 if ( HiWord( ev
->message
) != noErr
)
2205 SetPt( &point
, 100 , 100 ) ;
2207 err
= DIBadMount( point
, ev
->message
) ;
2208 wxASSERT( err
== noErr
) ;
2212 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2214 EventRecord
* ev
= (EventRecord
*) evr
;
2215 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2217 case suspendResumeMessage
:
2219 bool isResuming
= ev
->message
& resumeFlag
;
2220 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2222 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2225 WindowRef oldFrontWindow
= NULL
;
2226 WindowRef newFrontWindow
= NULL
;
2228 // in case we don't take care of activating ourselves, we have to synchronize
2229 // our idea of the active window with the process manager's - which it already activated
2231 if ( !doesActivate
)
2232 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2234 MacResume( convertClipboard
) ;
2236 newFrontWindow
= ::FrontNonFloatingWindow() ;
2238 if ( oldFrontWindow
)
2240 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2242 win
->MacActivate( ev
->when
, false ) ;
2244 if ( newFrontWindow
)
2246 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2248 win
->MacActivate( ev
->when
, true ) ;
2253 MacSuspend( convertClipboard
) ;
2257 case mouseMovedMessage
:
2261 wxWindow
* currentMouseWindow
= NULL
;
2263 if (s_captureWindow
)
2265 currentMouseWindow
= s_captureWindow
;
2269 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2270 ¤tMouseWindow
) ;
2273 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2275 wxMouseEvent event
;
2277 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2278 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2280 event
.m_leftDown
= isDown
&& !controlDown
;
2281 event
.m_middleDown
= FALSE
;
2282 event
.m_rightDown
= isDown
&& controlDown
;
2283 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2284 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2285 event
.m_altDown
= ev
->modifiers
& optionKey
;
2286 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2287 event
.m_x
= ev
->where
.h
;
2288 event
.m_y
= ev
->where
.v
;
2289 event
.m_timeStamp
= ev
->when
;
2290 event
.SetEventObject(this);
2292 if ( wxWindow::s_lastMouseWindow
)
2294 wxMouseEvent
eventleave(event
);
2295 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2296 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2297 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2299 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2301 if ( currentMouseWindow
)
2303 wxMouseEvent
evententer(event
);
2304 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2305 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2306 evententer
.SetEventObject( currentMouseWindow
) ;
2307 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2309 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2312 short windowPart
= inNoWindow
;
2314 if ( s_captureWindow
)
2316 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2317 windowPart
= inContent
;
2321 windowPart
= ::FindWindow(ev
->where
, &window
);
2328 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2330 win
->MacMouseMoved( ev
, windowPart
) ;
2337 UMAShowArrowCursor();
2347 UMAShowArrowCursor();
2358 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2360 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2361 wxMenu
* menu
= NULL
;
2362 wxMenuItem
* item
= NULL
;
2365 item
= mbar
->FindItem( id
, &menu
) ;
2367 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2369 if (item
->IsCheckable())
2371 item
->Check( !item
->IsChecked() ) ;
2374 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2378 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2381 return; // no menu item selected
2383 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2386 Str255 deskAccessoryName
;
2389 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2390 GetPort(&savedPort
);
2391 OpenDeskAcc(deskAccessoryName
);
2398 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2399 MacHandleMenuCommand( id
) ;