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
:
402 wxTheApp
->MacResume( true ) ;
406 case kEventAppDeactivated
:
409 wxTheApp
->MacSuspend( true ) ;
419 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
421 OSStatus result
= eventNotHandledErr
;
422 switch( GetEventClass( event
) )
424 case kEventClassCommand
:
425 result
= CommandEventHandler( handler
, event
, data
) ;
427 case kEventClassApplication
:
428 result
= ApplicationEventHandler( handler
, event
, data
) ;
430 case kEventClassMenu
:
431 result
= MenuEventHandler( handler
, event
, data
) ;
433 case kEventClassMouse
:
434 result
= MouseEventHandler( handler
, event
, data
) ;
436 case kEventClassAppleEvent
:
439 wxMacConvertEventToRecord( event
, &rec
) ;
440 result
= AEProcessAppleEvent( &rec
) ;
450 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
454 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
455 // we know it's there ;-)
456 WXIMPORT
char std::__throws_bad_alloc
;
459 bool wxApp::Initialize()
465 UMAInitToolbox( 4 ) ;
466 SetEventMask( everyEvent
) ;
467 UMAShowWatchCursor() ;
469 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
470 // open shared library resources from here since we don't have
471 // __wxinitialize in Mach-O shared libraries
472 wxStAppResource::OpenSharedLibraryResource(NULL
);
476 // test the minimal configuration necessary
482 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
484 error
= kMacSTRWrongMachine
;
486 else if (theMachine
< gestaltMacPlus
)
488 error
= kMacSTRWrongMachine
;
490 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
492 error
= kMacSTROldSystem
;
494 else if ( theSystem
< 0x0860 )
496 error
= kMacSTROldSystem
;
498 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
500 error
= kMacSTRSmallSize
;
506 if ( !UMAHasAppearance() )
508 error = kMacSTRNoPre8Yet ;
514 // if we encountered any problems so far, give the error code and exit immediately
518 wxStAppResource resload
;
522 GetIndString(message
, 128, error
);
523 UMAShowArrowCursor() ;
524 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
525 itemHit
= Alert(128, nil
);
530 # if __option(profile)
531 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
536 // now avoid exceptions thrown for new (bad_alloc)
537 // FIXME CS for some changes outside wxMac does not compile anymore
539 std::__throws_bad_alloc
= 0 ;
543 wxMacSetupConverters() ;
545 s_macCursorRgn
= ::NewRgn() ;
547 wxClassInfo::InitializeClasses();
550 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
554 wxPendingEventsLocker
= new wxCriticalSection
;
557 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
558 wxTheColourDatabase
->Initialize();
562 // flush the logged messages if any and install a 'safer' log target: the
563 // default one (wxLogGui) can't be used after the resources are freed just
564 // below and the user suppliedo ne might be even more unsafe (using any
565 // wxWindows GUI function is unsafe starting from now)
566 wxLog::DontCreateOnDemand();
568 // this will flush the old messages if any
569 delete wxLog::SetActiveTarget(new wxLogStderr
);
573 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
574 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
576 wxInitializeStockLists();
577 wxInitializeStockObjects();
579 #if wxUSE_WX_RESOURCES
580 wxInitializeResourceSystem();
583 wxBitmap::InitStandardHandlers();
585 wxModule::RegisterModules();
586 if (!wxModule::InitializeModules()) {
590 wxMacCreateNotifierTable() ;
592 UMAShowArrowCursor() ;
597 bool wxApp::OnInitGui()
599 if( !wxAppBase::OnInitGui() )
603 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
605 InstallApplicationEventHandler(
606 GetwxAppEventHandlerUPP(),
607 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
610 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
611 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
612 NewAEEventHandlerUPP(AEHandleODoc
) ,
614 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
615 NewAEEventHandlerUPP(AEHandleOApp
) ,
617 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
618 NewAEEventHandlerUPP(AEHandlePDoc
) ,
620 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
621 NewAEEventHandlerUPP(AEHandleQuit
) ,
624 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
625 NewAEEventHandlerProc(AEHandleODoc
) ,
627 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
628 NewAEEventHandlerProc(AEHandleOApp
) ,
630 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
631 NewAEEventHandlerProc(AEHandlePDoc
) ,
633 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
634 NewAEEventHandlerProc(AEHandleQuit
) ,
641 void wxApp::CleanUp()
643 wxToolTip::RemoveToolTips() ;
645 // flush the logged messages if any and install a 'safer' log target: the
646 // default one (wxLogGui) can't be used after the resources are freed just
647 // below and the user suppliedo ne might be even more unsafe (using any
648 // wxWindows GUI function is unsafe starting from now)
649 wxLog::DontCreateOnDemand();
651 // this will flush the old messages if any
652 delete wxLog::SetActiveTarget(new wxLogStderr
);
655 // One last chance for pending objects to be cleaned up
656 wxTheApp
->DeletePendingObjects();
658 wxModule::CleanUpModules();
660 #if wxUSE_WX_RESOURCES
661 wxCleanUpResourceSystem();
664 wxDeleteStockObjects() ;
666 // Destroy all GDI lists, etc.
667 wxDeleteStockLists();
669 delete wxTheColourDatabase
;
670 wxTheColourDatabase
= NULL
;
672 wxBitmap::CleanUpHandlers();
674 wxMacDestroyNotifierTable() ;
675 if (wxWinMacWindowList
) {
676 delete wxWinMacWindowList
;
678 if (wxWinMacControlList
) {
679 delete wxWinMacControlList
;
681 delete wxPendingEvents
;
684 delete wxPendingEventsLocker
;
685 // If we don't do the following, we get an apparent memory leak.
686 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
689 wxClassInfo::CleanUpClasses();
692 # if __option(profile)
693 ProfilerDump( "\papp.prof" ) ;
701 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
702 // At this point we want to check if there are any memory
703 // blocks that aren't part of the wxDebugContext itself,
704 // as a special case. Then when dumping we need to ignore
705 // wxDebugContext, too.
706 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
708 wxLogDebug(wxT("There were memory leaks."));
709 wxDebugContext::Dump();
710 wxDebugContext::PrintStatistics();
712 // wxDebugContext::SetStream(NULL, NULL);
716 // do it as the very last thing because everything else can log messages
717 delete wxLog::SetActiveTarget(NULL
);
720 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
721 // close shared library resources from here since we don't have
722 // __wxterminate in Mach-O shared libraries
723 wxStAppResource::CloseSharedLibraryResource();
725 wxMacCleanupConverters() ;
727 UMACleanupToolbox() ;
728 if (s_macCursorRgn
) {
729 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
737 //----------------------------------------------------------------------
739 //----------------------------------------------------------------------
741 // extern variable for shared library resource id
742 // need to be able to find it with NSLookupAndBindSymbol
743 short gSharedLibraryResource
= kResFileNotOpened
;
745 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
746 CFBundleRef gSharedLibraryBundle
= NULL
;
747 #endif /* WXMAKINGDLL && __DARWIN__ */
749 wxStAppResource::wxStAppResource()
751 m_currentRefNum
= CurResFile() ;
752 if ( gSharedLibraryResource
!= kResFileNotOpened
)
754 UseResFile( gSharedLibraryResource
) ;
758 wxStAppResource::~wxStAppResource()
760 if ( m_currentRefNum
!= kResFileNotOpened
)
762 UseResFile( m_currentRefNum
) ;
766 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
768 gSharedLibraryResource
= kResFileNotOpened
;
771 if ( initBlock
!= NULL
) {
772 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
773 FSSpec
*fileSpec
= NULL
;
775 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
776 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
778 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
779 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
782 if (fileSpec
!= NULL
) {
783 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
788 // Open the shared library resource file if it is not yet open
791 const char *theLibPath
;
793 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
794 if (gSharedLibraryBundle
!= NULL
) {
795 // wxWindows has been bundled into a framework
796 // load the framework resources
798 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
801 // wxWindows is a simple dynamic shared library
802 // load the resources from the data fork of a separate resource file
807 OSErr theErr
= noErr
;
809 // get the library path
810 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
811 theModule
= NSModuleForSymbol(theSymbol
);
812 theLibPath
= NSLibraryNameForModule(theModule
);
814 wxLogDebug( wxT("wxMac library installation name is '%s'"),
817 // allocate copy to replace .dylib.* extension with .rsrc
818 theResPath
= strdup(theLibPath
);
819 if (theResPath
!= NULL
) {
820 theName
= strrchr(theResPath
, '/');
821 if (theName
== NULL
) {
822 // no directory elements in path
823 theName
= theResPath
;
825 // find ".dylib" shared library extension
826 theExt
= strstr(theName
, ".dylib");
827 // overwrite extension with ".rsrc"
828 strcpy(theExt
, ".rsrc");
830 wxLogDebug( wxT("wxMac resources file name is '%s'"),
833 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
834 if (theErr
!= noErr
) {
835 // try in current directory (using name only)
836 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
839 // open the resource file
840 if (theErr
== noErr
) {
841 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
842 &gSharedLibraryResource
);
844 if (theErr
!= noErr
) {
845 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
849 // free duplicated resource file path
853 #endif /* __DARWIN__ */
855 #endif /* WXMAKINGDLL */
858 void wxStAppResource::CloseSharedLibraryResource()
861 // Close the shared library resource file
862 if (gSharedLibraryResource
!= kResFileNotOpened
) {
864 if (gSharedLibraryBundle
!= NULL
) {
865 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
866 gSharedLibraryResource
);
867 gSharedLibraryBundle
= NULL
;
870 #endif /* __DARWIN__ */
872 CloseResFile(gSharedLibraryResource
);
874 gSharedLibraryResource
= kResFileNotOpened
;
876 #endif /* WXMAKINGDLL */
879 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
881 // for shared libraries we have to manually get the correct resource
882 // ref num upon initializing and releasing when terminating, therefore
883 // the __wxinitialize and __wxterminate must be used
886 void __sinit(void); /* (generated by linker) */
887 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
888 pascal void __terminate(void);
891 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
893 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
894 return __initialize( theInitBlock
) ;
897 pascal void __wxterminate(void)
899 wxStAppResource::CloseSharedLibraryResource() ;
903 #endif /* WXMAKINGDLL && !__DARWIN__ */
905 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
907 return wxApp::Initialize();
910 int WXDLLEXPORT
wxEntryInitGui()
912 return wxTheApp
->OnInitGui();
915 void WXDLLEXPORT
wxEntryCleanup()
920 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
923 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
924 // This seems to be necessary since there are 'rogue'
925 // objects present at this point (perhaps global objects?)
926 // Setting a checkpoint will ignore them as far as the
927 // memory checking facility is concerned.
928 // Of course you may argue that memory allocated in globals should be
929 // checked, but this is a reasonable compromise.
930 wxDebugContext::SetCheckpoint();
933 if (!wxEntryStart(argc
, argv
)) {
936 // create the application object or ensure that one already exists
939 // The app may have declared a global application object, but we recommend
940 // the IMPLEMENT_APP macro is used instead, which sets an initializer
941 // function for delayed, dynamic app object construction.
942 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
943 wxT("No initializer - use IMPLEMENT_APP macro.") );
945 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
948 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
951 // Mac OS X passes a process serial number command line argument when
952 // the application is launched from the Finder. This argument must be
953 // removed from the command line arguments before being handled by the
954 // application (otherwise applications would need to handle it)
957 if (strncmp(argv
[1], "-psn_", 5) == 0) {
958 // assume the argument is always the only one and remove it
963 argc
= 0 ; // currently we don't support files as parameters
965 // we could try to get the open apple events here to adjust argc and argv better
967 wxTheApp
->argc
= argc
;
969 wxTheApp
->argv
= new wxChar
*[argc
+1];
971 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
973 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
975 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
977 wxTheApp
->argv
= argv
;
980 // GUI-specific initialization, such as creating an app context.
983 // Here frames insert themselves automatically
984 // into wxTopLevelWindows by getting created
989 if ( wxTheApp
->OnInit() )
993 retValue
= wxTheApp
->OnRun();
996 // We want to initialize, but not run or exit immediately.
999 //else: app initialization failed, so we skipped OnRun()
1001 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1004 // Forcibly delete the window.
1005 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
1006 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
1008 topWindow
->Close(TRUE
);
1009 wxTheApp
->DeletePendingObjects();
1014 wxTheApp
->SetTopWindow(NULL
);
1027 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1029 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1030 OSStatus err
= noErr
;
1033 switch( GetEventClass( event
) )
1035 case kEventClassKeyboard
:
1038 switch( GetEventKind(event
) )
1040 case kEventRawKeyDown
:
1041 rec
->what
= keyDown
;
1043 case kEventRawKeyRepeat
:
1044 rec
->what
= autoKey
;
1046 case kEventRawKeyUp
:
1049 case kEventRawKeyModifiersChanged
:
1050 rec
->what
= nullEvent
;
1059 unsigned char charCode
;
1061 GetMouse( &rec
->where
) ;
1063 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1064 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1065 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1066 rec
->modifiers
= modifiers
;
1067 rec
->message
= (keyCode
<< 8 ) + charCode
;
1071 case kEventClassTextInput
:
1073 switch( GetEventKind( event
) )
1075 case kEventTextInputUnicodeForKeyEvent
:
1078 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1082 unsigned char charCode
;
1084 GetMouse( &rec
->where
) ;
1085 rec
->what
= keyDown
;
1086 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1087 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1088 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1089 rec
->modifiers
= modifiers
;
1090 rec
->message
= (keyCode
<< 8 ) + charCode
;
1106 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1108 OSStatus result = eventNotHandledErr ;
1111 switch ( GetEventClass( event ) )
1113 case kEventClassKeyboard :
1114 if ( wxMacConvertEventToRecord( event , &rec ) )
1116 wxTheApp->MacHandleModifierEvents( &rec ) ;
1117 wxTheApp->MacHandleOneEvent( &rec ) ;
1121 case kEventClassTextInput :
1122 if ( wxMacConvertEventToRecord( event , &rec ) )
1124 wxTheApp->MacHandleModifierEvents( &rec ) ;
1125 wxTheApp->MacHandleOneEvent( &rec ) ;
1137 // Static member initialization
1138 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1145 #if WXWIN_COMPATIBILITY_2_2
1146 m_wantDebugOutput
= TRUE
;
1152 m_printMode
= wxPRINT_WINDOWS
;
1156 bool wxApp::Initialized()
1164 int wxApp::MainLoop()
1176 // Returns TRUE if more time is needed.
1177 bool wxApp::ProcessIdle()
1180 event
.SetEventObject(this);
1181 ProcessEvent(event
);
1183 return event
.MoreRequested();
1186 void wxApp::ExitMainLoop()
1188 m_keepGoing
= FALSE
;
1191 // Is a message/event pending?
1192 bool wxApp::Pending()
1195 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1199 return EventAvail( everyEvent
, &event
) ;
1203 // Dispatch a message.
1204 void wxApp::Dispatch()
1209 void wxApp::OnIdle(wxIdleEvent
& event
)
1211 // Avoid recursion (via ProcessEvent default case)
1218 // 'Garbage' collection of windows deleted with Close().
1219 DeletePendingObjects();
1221 // flush the logged messages if any
1222 wxLog
*pLog
= wxLog::GetActiveTarget();
1223 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1226 // Send OnIdle events to all windows
1227 bool needMore
= SendIdleEvents();
1230 event
.RequestMore(TRUE
);
1232 // If they are pending events, we must process them: pending events are
1233 // either events to the threads other than main or events posted with
1234 // wxPostEvent() functions
1235 wxMacProcessNotifierAndPendingEvents();
1245 // Send idle event to all top-level windows
1246 bool wxApp::SendIdleEvents()
1248 bool needMore
= FALSE
;
1249 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1252 wxWindow
* win
= node
->GetData();
1253 if (SendIdleEvents(win
))
1256 node
= node
->GetNext();
1261 // Send idle event to window and all subwindows
1262 bool wxApp::SendIdleEvents(wxWindow
* win
)
1264 bool needMore
= FALSE
;
1267 event
.SetEventObject(win
);
1268 win
->ProcessEvent(event
);
1270 if (event
.MoreRequested())
1273 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1276 wxWindow
* win
= node
->GetData();
1277 if (SendIdleEvents(win
))
1280 node
= node
->GetNext();
1285 void wxApp::DeletePendingObjects()
1287 wxNode
*node
= wxPendingDelete
.GetFirst();
1290 wxObject
*obj
= (wxObject
*)node
->GetData();
1294 if (wxPendingDelete
.Member(obj
))
1297 // Deleting one object may have deleted other pending
1298 // objects, so start from beginning of list again.
1299 node
= wxPendingDelete
.GetFirst();
1305 wxLogError(_("Fatal error: exiting"));
1311 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1314 GetTopWindow()->Close(TRUE
);
1317 // Default behaviour: close the application with prompts. The
1318 // user can veto the close, and therefore the end session.
1319 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1323 if (!GetTopWindow()->Close(!event
.CanVeto()))
1328 extern "C" void wxCYield() ;
1334 // Yield to other processes
1336 bool wxApp::Yield(bool onlyIfNeeded
)
1340 if ( !onlyIfNeeded
)
1342 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1351 YieldToAnyThread() ;
1353 // by definition yield should handle all non-processed events
1357 OSStatus status
= noErr
;
1360 s_inReceiveEvent
= true ;
1361 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1362 s_inReceiveEvent
= false ;
1364 if ( status
== eventLoopTimedOutErr
)
1366 // make sure next time the event loop will trigger idle events
1367 sleepTime
= kEventDurationNoWait
;
1369 else if ( status
== eventLoopQuitErr
)
1371 // according to QA1061 this may also occur when a WakeUp Process
1376 MacHandleOneEvent( theEvent
) ;
1377 ReleaseEvent(theEvent
);
1379 } while( status
== noErr
) ;
1383 // having a larger value here leads to large performance slowdowns
1384 // so we cannot give background apps more processor time here
1385 // we do so however having a large sleep value in the main event loop
1388 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1390 MacHandleModifierEvents( &event
) ;
1391 MacHandleOneEvent( &event
);
1392 if ( event
.what
!= kHighLevelEvent
)
1393 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1395 MacHandleModifierEvents( &event
) ;
1398 wxMacProcessNotifierAndPendingEvents() ;
1404 // platform specifics
1406 void wxApp::MacSuspend( bool convertClipboard
)
1409 // we have to deactive the top level windows manually
1411 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1414 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1415 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1417 node
= node
->GetNext();
1420 ::HideFloatingWindows() ;
1422 s_lastMouseDown
= 0 ;
1424 if( convertClipboard
)
1426 MacConvertPrivateToPublicScrap() ;
1430 extern wxList wxModalDialogs
;
1432 void wxApp::MacResume( bool convertClipboard
)
1434 s_lastMouseDown
= 0 ;
1435 if( convertClipboard
)
1437 MacConvertPublicToPrivateScrap() ;
1441 ::ShowFloatingWindows() ;
1442 // raise modal dialogs in case a non modal window was selected to activate the app
1444 wxNode
* node
= wxModalDialogs
.GetFirst();
1447 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1450 node
= node
->GetNext();
1455 void wxApp::MacConvertPrivateToPublicScrap()
1459 void wxApp::MacConvertPublicToPrivateScrap()
1463 void wxApp::MacDoOneEvent()
1468 s_inReceiveEvent
= true ;
1469 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1470 s_inReceiveEvent
= false ;
1471 if ( status
== eventLoopTimedOutErr
)
1473 if ( wxTheApp
->ProcessIdle() )
1474 sleepTime
= kEventDurationNoWait
;
1476 sleepTime
= kEventDurationForever
;
1478 else if ( status
== eventLoopQuitErr
)
1480 // according to QA1061 this may also occur when a WakeUp Process
1485 MacHandleOneEvent( theEvent
) ;
1486 ReleaseEvent(theEvent
);
1487 sleepTime
= kEventDurationNoWait
;
1492 EventMask eventMask
= everyEvent
;
1494 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1496 MacHandleModifierEvents( &event
) ;
1497 MacHandleOneEvent( &event
);
1501 MacHandleModifierEvents( &event
) ;
1503 WindowPtr window
= ::FrontWindow() ;
1505 ::IdleControls( window
) ;
1507 if ( wxTheApp
->ProcessIdle() )
1510 sleepTime
= GetCaretTime() / 2 ;
1512 if ( event
.what
!= kHighLevelEvent
)
1513 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1517 DeletePendingObjects() ;
1518 wxMacProcessNotifierAndPendingEvents() ;
1521 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1524 EventTargetRef theTarget
;
1525 theTarget
= GetEventDispatcherTarget();
1526 m_macCurrentEvent
= evr
;
1527 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1529 EventRecord
* ev
= (EventRecord
*) evr
;
1530 m_macCurrentEvent
= ev
;
1532 wxApp::sm_lastMessageTime
= ev
->when
;
1537 MacHandleMouseDownEvent( ev
) ;
1538 if ( ev
->modifiers
& controlKey
)
1539 s_lastMouseDown
= 2;
1541 s_lastMouseDown
= 1;
1544 if ( s_lastMouseDown
== 2 )
1546 ev
->modifiers
|= controlKey
;
1550 ev
->modifiers
&= ~controlKey
;
1552 MacHandleMouseUpEvent( ev
) ;
1553 s_lastMouseDown
= 0;
1556 MacHandleActivateEvent( ev
) ;
1559 MacHandleUpdateEvent( ev
) ;
1563 MacHandleKeyDownEvent( ev
) ;
1566 MacHandleKeyUpEvent( ev
) ;
1569 MacHandleDiskEvent( ev
) ;
1572 MacHandleOSEvent( ev
) ;
1574 case kHighLevelEvent
:
1575 MacHandleHighLevelEvent( ev
) ;
1581 wxMacProcessNotifierAndPendingEvents() ;
1585 bool s_macIsInModalLoop
= false ;
1587 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1589 EventRecord
* ev
= (EventRecord
*) evr
;
1590 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1592 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1594 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1595 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1596 event
.m_altDown
= ev
->modifiers
& optionKey
;
1597 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1599 event
.m_x
= ev
->where
.h
;
1600 event
.m_y
= ev
->where
.v
;
1601 event
.m_timeStamp
= ev
->when
;
1602 wxWindow
* focus
= wxWindow::FindFocus() ;
1603 event
.SetEventObject(focus
);
1605 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1607 event
.m_keyCode
= WXK_CONTROL
;
1608 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1609 focus
->GetEventHandler()->ProcessEvent( event
) ;
1611 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1613 event
.m_keyCode
= WXK_SHIFT
;
1614 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1615 focus
->GetEventHandler()->ProcessEvent( event
) ;
1617 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1619 event
.m_keyCode
= WXK_ALT
;
1620 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1621 focus
->GetEventHandler()->ProcessEvent( event
) ;
1623 s_lastModifiers
= ev
->modifiers
;
1627 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1629 // we must avoid reentrancy problems when processing high level events eg printing
1630 bool former
= s_inYield
;
1632 EventRecord
* ev
= (EventRecord
*) evr
;
1633 ::AEProcessAppleEvent( ev
) ;
1634 s_inYield
= former
;
1637 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1639 EventRecord
* ev
= (EventRecord
*) evr
;
1640 wxToolTip::RemoveToolTips() ;
1643 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1644 WindowAttributes frontWindowAttributes
= NULL
;
1646 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1648 short windowPart
= ::FindWindow(ev
->where
, &window
);
1649 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1650 if ( wxPendingDelete
.Member(win
) )
1654 GetQDGlobalsScreenBits( &screenBits
);
1659 if ( s_macIsInModalLoop
)
1665 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1666 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1667 s_lastMouseDown
= 0;
1671 SystemClick( ev
, window
) ;
1672 s_lastMouseDown
= 0;
1675 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1681 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1686 Point pt
= { 0, 0 } ;
1687 SetPortWindowPort(window
) ;
1688 LocalToGlobal( &pt
) ;
1690 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1691 -1 , wxSIZE_USE_EXISTING
);
1693 s_lastMouseDown
= 0;
1697 if (TrackGoAway(window
, ev
->where
))
1702 s_lastMouseDown
= 0;
1706 Rect newContentRect
;
1707 Rect constraintRect
;
1708 constraintRect
.top
= win
->GetMinHeight() ;
1709 if ( constraintRect
.top
== -1 )
1710 constraintRect
.top
= 0 ;
1711 constraintRect
.left
= win
->GetMinWidth() ;
1712 if ( constraintRect
.left
== -1 )
1713 constraintRect
.left
= 0 ;
1714 constraintRect
.right
= win
->GetMaxWidth() ;
1715 if ( constraintRect
.right
== -1 )
1716 constraintRect
.right
= 32000 ;
1717 constraintRect
.bottom
= win
->GetMaxHeight() ;
1718 if ( constraintRect
.bottom
== -1 )
1719 constraintRect
.bottom
= 32000 ;
1721 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1722 &constraintRect
, &newContentRect
) ;
1725 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1726 newContentRect
.right
- newContentRect
.left
,
1727 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1729 s_lastMouseDown
= 0;
1734 if (TrackBox(window
, ev
->where
, windowPart
))
1736 // TODO setup size event
1737 ZoomWindow( window
, windowPart
, false ) ;
1743 Point pt
= { 0, 0 } ;
1744 SetPortWindowPort(window
) ;
1745 LocalToGlobal( &pt
) ;
1748 GetWindowPortBounds(window
, &tempRect
) ;
1749 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1750 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1753 s_lastMouseDown
= 0;
1755 case inCollapseBox
:
1756 // TODO setup size event
1757 s_lastMouseDown
= 0;
1764 SetPortWindowPort(window
) ;
1767 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1769 if ( s_macIsInModalLoop
)
1773 else if ( UMAIsWindowFloating( window
) )
1776 win
->MacMouseDown( ev
, windowPart
) ;
1781 win
->MacMouseDown( ev
, windowPart
) ;
1782 ::SelectWindow( window
) ;
1788 win
->MacMouseDown( ev
, windowPart
) ;
1796 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1798 EventRecord
* ev
= (EventRecord
*) evr
;
1801 short windowPart
= inNoWindow
;
1802 if ( wxTheApp
->s_captureWindow
)
1804 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1805 windowPart
= inContent
;
1809 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1820 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1822 win
->MacMouseUp( ev
, windowPart
) ;
1830 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1831 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1836 case kHomeCharCode
:
1839 case kEnterCharCode
:
1840 retval
= WXK_RETURN
;
1845 case kHelpCharCode
:
1848 case kBackspaceCharCode
:
1854 case kPageUpCharCode
:
1855 retval
= WXK_PAGEUP
;
1857 case kPageDownCharCode
:
1858 retval
= WXK_PAGEDOWN
;
1860 case kReturnCharCode
:
1861 retval
= WXK_RETURN
;
1863 case kFunctionKeyCharCode
:
1915 case kEscapeCharCode
:
1916 retval
= WXK_ESCAPE
;
1918 case kLeftArrowCharCode
:
1921 case kRightArrowCharCode
:
1922 retval
= WXK_RIGHT
;
1924 case kUpArrowCharCode
:
1927 case kDownArrowCharCode
:
1930 case kDeleteCharCode
:
1931 retval
= WXK_DELETE
;
1940 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1942 EventRecord
* ev
= (EventRecord
*) evr
;
1943 wxToolTip::RemoveToolTips() ;
1945 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1946 if ( HiWord( menuresult
) )
1948 if ( !s_macIsInModalLoop
)
1949 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1953 wxWindow
* focus
= wxWindow::FindFocus() ;
1955 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1957 // has not been handled -> perform default
1958 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1959 if ( control
&& control
->GetMacControl() != NULL
)
1963 keychar
= short(ev
->message
& charCodeMask
);
1964 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1965 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1971 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1973 EventRecord
* ev
= (EventRecord
*) evr
;
1974 wxToolTip::RemoveToolTips() ;
1976 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1977 if ( HiWord( menuresult
) )
1982 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1988 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1995 keychar
= short(keymessage
& charCodeMask
);
1996 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1998 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2000 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2001 // and look at the character after
2003 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2004 keychar
= short(keyInfo
& charCodeMask
);
2005 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2007 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2008 long realkeyval
= keyval
;
2009 if ( keyval
== keychar
)
2011 // 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)
2012 realkeyval
= short(keymessage
& charCodeMask
) ;
2013 keyval
= wxToupper( keyval
) ;
2016 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2017 bool handled
= false ;
2018 event
.m_shiftDown
= modifiers
& shiftKey
;
2019 event
.m_controlDown
= modifiers
& controlKey
;
2020 event
.m_altDown
= modifiers
& optionKey
;
2021 event
.m_metaDown
= modifiers
& cmdKey
;
2022 event
.m_keyCode
= keyval
;
2026 event
.m_timeStamp
= when
;
2027 event
.SetEventObject(focus
);
2028 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2029 if ( handled
&& event
.GetSkipped() )
2036 wxWindow
*ancestor
= focus
;
2039 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2042 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2043 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2046 if (ancestor
->IsTopLevel())
2048 ancestor
= ancestor
->GetParent();
2051 #endif // wxUSE_ACCEL
2055 event
.Skip( FALSE
) ;
2056 event
.SetEventType( wxEVT_CHAR
) ;
2058 event
.m_keyCode
= realkeyval
;
2060 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2061 if ( handled
&& event
.GetSkipped() )
2065 (keyval
== WXK_TAB
) &&
2066 // CS: copied the change below from wxGTK
2067 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2068 // have this style, yet choose not to process this particular TAB in which
2069 // case TAB must still work as a navigational character
2071 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2073 (focus
->GetParent()) &&
2074 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2076 wxNavigationKeyEvent new_event
;
2077 new_event
.SetEventObject( focus
);
2078 new_event
.SetDirection( !event
.ShiftDown() );
2079 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2080 new_event
.SetWindowChange( event
.ControlDown() );
2081 new_event
.SetCurrentFocus( focus
);
2082 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2083 if ( handled
&& new_event
.GetSkipped() )
2086 // backdoor handler for default return and command escape
2087 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2089 // if window is not having a focus still testing for default enter or cancel
2090 // TODO add the UMA version for ActiveNonFloatingWindow
2091 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2094 if ( keyval
== WXK_RETURN
)
2096 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2098 if ( def
&& def
->IsEnabled() )
2100 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2101 event
.SetEventObject(def
);
2102 def
->Command(event
);
2106 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2107 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2109 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2110 new_event
.SetEventObject( focus
);
2111 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2118 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2125 keychar
= short(keymessage
& charCodeMask
);
2126 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2127 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2129 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2130 // and look at the character after
2132 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2133 keychar
= short(keyInfo
& charCodeMask
);
2134 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2136 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2138 if ( keyval
== keychar
)
2140 keyval
= wxToupper( keyval
) ;
2142 bool handled
= false ;
2144 wxKeyEvent
event(wxEVT_KEY_UP
);
2145 event
.m_shiftDown
= modifiers
& shiftKey
;
2146 event
.m_controlDown
= modifiers
& controlKey
;
2147 event
.m_altDown
= modifiers
& optionKey
;
2148 event
.m_metaDown
= modifiers
& cmdKey
;
2149 event
.m_keyCode
= keyval
;
2153 event
.m_timeStamp
= when
;
2154 event
.SetEventObject(focus
);
2155 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2161 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2163 EventRecord
* ev
= (EventRecord
*) evr
;
2164 WindowRef window
= (WindowRef
) ev
->message
;
2167 bool activate
= (ev
->modifiers
& activeFlag
) ;
2168 WindowClass wclass
;
2169 ::GetWindowClass ( window
, &wclass
) ;
2170 if ( wclass
== kFloatingWindowClass
)
2172 // if it is a floater we activate/deactivate the front non-floating window instead
2173 window
= ::FrontNonFloatingWindow() ;
2175 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2177 win
->MacActivate( ev
->when
, activate
) ;
2181 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2183 EventRecord
* ev
= (EventRecord
*) evr
;
2184 WindowRef window
= (WindowRef
) ev
->message
;
2185 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2188 if ( !wxPendingDelete
.Member(win
) )
2189 win
->MacUpdate( ev
->when
) ;
2193 // since there is no way of telling this foreign window to update itself
2194 // we have to invalidate the update region otherwise we keep getting the same
2195 // event over and over again
2196 BeginUpdate( window
) ;
2197 EndUpdate( window
) ;
2201 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2203 EventRecord
* ev
= (EventRecord
*) evr
;
2204 if ( HiWord( ev
->message
) != noErr
)
2208 SetPt( &point
, 100 , 100 ) ;
2210 err
= DIBadMount( point
, ev
->message
) ;
2211 wxASSERT( err
== noErr
) ;
2215 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2217 EventRecord
* ev
= (EventRecord
*) evr
;
2218 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2220 case suspendResumeMessage
:
2222 bool isResuming
= ev
->message
& resumeFlag
;
2223 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2225 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2228 WindowRef oldFrontWindow
= NULL
;
2229 WindowRef newFrontWindow
= NULL
;
2231 // in case we don't take care of activating ourselves, we have to synchronize
2232 // our idea of the active window with the process manager's - which it already activated
2234 if ( !doesActivate
)
2235 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2237 MacResume( convertClipboard
) ;
2239 newFrontWindow
= ::FrontNonFloatingWindow() ;
2241 if ( oldFrontWindow
)
2243 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2245 win
->MacActivate( ev
->when
, false ) ;
2247 if ( newFrontWindow
)
2249 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2251 win
->MacActivate( ev
->when
, true ) ;
2256 MacSuspend( convertClipboard
) ;
2260 case mouseMovedMessage
:
2264 wxWindow
* currentMouseWindow
= NULL
;
2266 if (s_captureWindow
)
2268 currentMouseWindow
= s_captureWindow
;
2272 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2273 ¤tMouseWindow
) ;
2276 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2278 wxMouseEvent event
;
2280 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2281 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2283 event
.m_leftDown
= isDown
&& !controlDown
;
2284 event
.m_middleDown
= FALSE
;
2285 event
.m_rightDown
= isDown
&& controlDown
;
2286 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2287 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2288 event
.m_altDown
= ev
->modifiers
& optionKey
;
2289 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2290 event
.m_x
= ev
->where
.h
;
2291 event
.m_y
= ev
->where
.v
;
2292 event
.m_timeStamp
= ev
->when
;
2293 event
.SetEventObject(this);
2295 if ( wxWindow::s_lastMouseWindow
)
2297 wxMouseEvent
eventleave(event
);
2298 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2299 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2300 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2302 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2304 if ( currentMouseWindow
)
2306 wxMouseEvent
evententer(event
);
2307 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2308 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2309 evententer
.SetEventObject( currentMouseWindow
) ;
2310 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2312 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2315 short windowPart
= inNoWindow
;
2317 if ( s_captureWindow
)
2319 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2320 windowPart
= inContent
;
2324 windowPart
= ::FindWindow(ev
->where
, &window
);
2331 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2333 win
->MacMouseMoved( ev
, windowPart
) ;
2340 UMAShowArrowCursor();
2350 UMAShowArrowCursor();
2361 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2363 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2364 wxMenu
* menu
= NULL
;
2365 wxMenuItem
* item
= NULL
;
2368 item
= mbar
->FindItem( id
, &menu
) ;
2370 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2372 if (item
->IsCheckable())
2374 item
->Check( !item
->IsChecked() ) ;
2377 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2381 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2384 return; // no menu item selected
2386 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2389 Str255 deskAccessoryName
;
2392 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2393 GetPort(&savedPort
);
2394 OpenDeskAcc(deskAccessoryName
);
2401 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2402 MacHandleMenuCommand( id
) ;