1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
57 # include <CoreServices/CoreServices.h>
58 # if defined(WXMAKINGDLL)
59 # include <mach-o/dyld.h>
64 # include <ToolUtils.h>
65 # include <DiskInit.h>
69 extern wxList wxPendingDelete
;
70 extern wxList
*wxWinMacWindowList
;
71 extern wxList
*wxWinMacControlList
;
73 // statics for implementation
75 static bool s_inYield
= FALSE
;
76 static bool s_inOnIdle
= FALSE
;
79 static bool s_inReceiveEvent
= FALSE
;
80 static EventTime sleepTime
= kEventDurationNoWait
;
82 static long sleepTime
= 0 ;
85 wxApp
*wxTheApp
= NULL
;
87 #if !USE_SHARED_LIBRARY
88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
90 EVT_IDLE(wxApp::OnIdle
)
91 EVT_END_SESSION(wxApp::OnEndSession
)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
97 const short kMacMinHeap
= (29 * 1024) ;
98 // platform specific static variables
100 const short kwxMacMenuBarResource
= 1 ;
101 const short kwxMacAppleMenuId
= 1 ;
103 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
104 wxWindow
* wxApp::s_captureWindow
= NULL
;
105 int wxApp::s_lastMouseDown
= 0 ;
106 long wxApp::sm_lastMessageTime
= 0;
107 long wxApp::s_lastModifiers
= 0 ;
110 bool wxApp::s_macDefaultEncodingIsPC
= true ;
111 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
112 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
113 long wxApp::s_macPreferencesMenuItemId
= 0 ;
114 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
115 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
117 //----------------------------------------------------------------------
118 // Core Apple Event Support
119 //----------------------------------------------------------------------
121 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
128 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
131 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
133 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
136 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
138 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
141 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
143 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
146 // AEODoc Calls MacOpenFile on each of the files passed
148 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
152 DescType returnedType
;
158 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
162 err
= AECountItems(&docList
, &itemsInList
);
166 ProcessSerialNumber PSN
;
167 PSN
.highLongOfPSN
= 0 ;
168 PSN
.lowLongOfPSN
= kCurrentProcess
;
169 SetFrontProcess( &PSN
) ;
171 for (i
= 1; i
<= itemsInList
; i
++) {
172 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
173 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
174 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
180 // AEPDoc Calls MacPrintFile on each of the files passed
182 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
186 DescType returnedType
;
192 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
196 err
= AECountItems(&docList
, &itemsInList
);
200 ProcessSerialNumber PSN
;
201 PSN
.highLongOfPSN
= 0 ;
202 PSN
.lowLongOfPSN
= kCurrentProcess
;
203 SetFrontProcess( &PSN
) ;
205 for (i
= 1; i
<= itemsInList
; i
++) {
206 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
207 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
208 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
214 // AEOApp calls MacNewFile
216 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
222 // AEQuit attempts to quite the application
224 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
226 /* wxWindow* win = GetTopWindow() ;
239 //----------------------------------------------------------------------
240 // Support Routines linking the Mac...File Calls to the Document Manager
241 //----------------------------------------------------------------------
243 void wxApp::MacOpenFile(const wxString
& fileName
)
245 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
247 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
250 void wxApp::MacPrintFile(const wxString
& fileName
)
252 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
255 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
258 wxView
* view
= doc
->GetFirstView() ;
261 wxPrintout
*printout
= view
->OnCreatePrintout();
265 printer
.Print(view
->GetFrame(), printout
, TRUE
);
271 doc
->DeleteAllViews();
272 dm
->RemoveDocument(doc
) ;
278 void wxApp::MacNewFile()
282 //----------------------------------------------------------------------
283 // Carbon Event Handler
284 //----------------------------------------------------------------------
288 static const EventTypeSpec eventList
[] =
290 { kEventClassCommand
, kEventProcessCommand
} ,
291 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
293 { kEventClassApplication
, kEventAppActivated
} ,
294 { kEventClassApplication
, kEventAppDeactivated
} ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
298 { kEventClassAppleEvent
, kEventAppleEvent
} ,
300 { kEventClassMouse
, kEventMouseDown
} ,
304 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
306 OSStatus result
= eventNotHandledErr
;
307 UInt32 kind
= GetEventKind( event
) ;
312 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
313 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
314 // deal with clicks in the menu bar explicitely
316 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
318 OSStatus result
= eventNotHandledErr
;
320 switch( GetEventKind(event
) )
322 case kEventMouseDown
:
327 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
328 sizeof( Point
), NULL
, &point
);
329 short windowPart
= ::FindWindow(point
, &window
);
331 if ( windowPart
== inMenuBar
)
333 MenuSelect( point
) ;
343 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
345 OSStatus result
= eventNotHandledErr
;
349 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
350 sizeof( HICommand
), NULL
, &command
);
352 MenuCommand id
= command
.commandID
;
353 if ( id
== kHICommandPreferences
)
354 id
= wxApp::s_macPreferencesMenuItemId
;
356 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
357 wxMenu
* menu
= NULL
;
358 wxMenuItem
* item
= NULL
;
361 item
= mbar
->FindItem( id
, &menu
) ;
363 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
366 switch( GetEventKind( event
) )
368 case kEventProcessCommand
:
370 if (item
->IsCheckable())
372 item
->Check( !item
->IsChecked() ) ;
375 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
379 case kEventCommandUpdateStatus
:
380 // eventually trigger an updateui round
390 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
392 OSStatus result
= eventNotHandledErr
;
393 switch ( GetEventKind( event
) )
395 case kEventAppActivated
:
398 wxTheApp
->MacResume( true ) ;
402 case kEventAppDeactivated
:
405 wxTheApp
->MacSuspend( true ) ;
415 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
417 OSStatus result
= eventNotHandledErr
;
418 switch( GetEventClass( event
) )
420 case kEventClassCommand
:
421 result
= CommandEventHandler( handler
, event
, data
) ;
423 case kEventClassApplication
:
424 result
= ApplicationEventHandler( handler
, event
, data
) ;
426 case kEventClassMenu
:
427 result
= MenuEventHandler( handler
, event
, data
) ;
429 case kEventClassMouse
:
430 result
= MouseEventHandler( handler
, event
, data
) ;
432 case kEventClassAppleEvent
:
435 wxMacConvertEventToRecord( event
, &rec
) ;
436 result
= AEProcessAppleEvent( &rec
) ;
446 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
450 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
451 // we know it's there ;-)
452 WXIMPORT
char std::__throws_bad_alloc
;
455 bool wxApp::Initialize()
461 UMAInitToolbox( 4 ) ;
462 SetEventMask( everyEvent
) ;
463 UMAShowWatchCursor() ;
465 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
466 // open shared library resources from here since we don't have
467 // __wxinitialize in Mach-O shared libraries
468 wxStAppResource::OpenSharedLibraryResource(NULL
);
472 // test the minimal configuration necessary
478 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
480 error
= kMacSTRWrongMachine
;
482 else if (theMachine
< gestaltMacPlus
)
484 error
= kMacSTRWrongMachine
;
486 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
488 error
= kMacSTROldSystem
;
490 else if ( theSystem
< 0x0860 )
492 error
= kMacSTROldSystem
;
494 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
496 error
= kMacSTRSmallSize
;
502 if ( !UMAHasAppearance() )
504 error = kMacSTRNoPre8Yet ;
510 // if we encountered any problems so far, give the error code and exit immediately
514 wxStAppResource resload
;
518 GetIndString(message
, 128, error
);
519 UMAShowArrowCursor() ;
520 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
521 itemHit
= Alert(128, nil
);
526 # if __option(profile)
527 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
532 // now avoid exceptions thrown for new (bad_alloc)
533 // FIXME CS for some changes outside wxMac does not compile anymore
535 std::__throws_bad_alloc
= 0 ;
539 wxMacSetupConverters() ;
541 s_macCursorRgn
= ::NewRgn() ;
543 wxClassInfo::InitializeClasses();
546 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
550 wxPendingEventsLocker
= new wxCriticalSection
;
553 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
554 wxTheColourDatabase
->Initialize();
558 // flush the logged messages if any and install a 'safer' log target: the
559 // default one (wxLogGui) can't be used after the resources are freed just
560 // below and the user suppliedo ne might be even more unsafe (using any
561 // wxWindows GUI function is unsafe starting from now)
562 wxLog::DontCreateOnDemand();
564 // this will flush the old messages if any
565 delete wxLog::SetActiveTarget(new wxLogStderr
);
569 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
570 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
572 wxInitializeStockLists();
573 wxInitializeStockObjects();
575 wxBitmap::InitStandardHandlers();
577 wxModule::RegisterModules();
578 if (!wxModule::InitializeModules()) {
582 wxMacCreateNotifierTable() ;
584 UMAShowArrowCursor() ;
589 bool wxApp::OnInitGui()
591 if( !wxAppBase::OnInitGui() )
595 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
597 InstallApplicationEventHandler(
598 GetwxAppEventHandlerUPP(),
599 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
602 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
603 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
604 NewAEEventHandlerUPP(AEHandleODoc
) ,
606 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
607 NewAEEventHandlerUPP(AEHandleOApp
) ,
609 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
610 NewAEEventHandlerUPP(AEHandlePDoc
) ,
612 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
613 NewAEEventHandlerUPP(AEHandleQuit
) ,
616 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
617 NewAEEventHandlerProc(AEHandleODoc
) ,
619 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
620 NewAEEventHandlerProc(AEHandleOApp
) ,
622 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
623 NewAEEventHandlerProc(AEHandlePDoc
) ,
625 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
626 NewAEEventHandlerProc(AEHandleQuit
) ,
633 void wxApp::CleanUp()
635 wxToolTip::RemoveToolTips() ;
637 // flush the logged messages if any and install a 'safer' log target: the
638 // default one (wxLogGui) can't be used after the resources are freed just
639 // below and the user suppliedo ne might be even more unsafe (using any
640 // wxWindows GUI function is unsafe starting from now)
641 wxLog::DontCreateOnDemand();
643 // this will flush the old messages if any
644 delete wxLog::SetActiveTarget(new wxLogStderr
);
647 // One last chance for pending objects to be cleaned up
648 wxTheApp
->DeletePendingObjects();
650 wxModule::CleanUpModules();
652 wxDeleteStockObjects() ;
654 // Destroy all GDI lists, etc.
655 wxDeleteStockLists();
657 delete wxTheColourDatabase
;
658 wxTheColourDatabase
= NULL
;
660 wxBitmap::CleanUpHandlers();
662 wxMacDestroyNotifierTable() ;
663 if (wxWinMacWindowList
) {
664 delete wxWinMacWindowList
;
666 if (wxWinMacControlList
) {
667 delete wxWinMacControlList
;
669 delete wxPendingEvents
;
672 delete wxPendingEventsLocker
;
673 // There is still more cleanup code that will try to use this if not NULL.
674 wxPendingEventsLocker
= NULL
;
675 // If we don't do the following, we get an apparent memory leak.
676 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
679 wxClassInfo::CleanUpClasses();
682 # if __option(profile)
683 ProfilerDump( "\papp.prof" ) ;
691 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
692 // At this point we want to check if there are any memory
693 // blocks that aren't part of the wxDebugContext itself,
694 // as a special case. Then when dumping we need to ignore
695 // wxDebugContext, too.
696 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
698 wxLogDebug(wxT("There were memory leaks."));
699 wxDebugContext::Dump();
700 wxDebugContext::PrintStatistics();
702 // wxDebugContext::SetStream(NULL, NULL);
706 // do it as the very last thing because everything else can log messages
707 delete wxLog::SetActiveTarget(NULL
);
710 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
711 // close shared library resources from here since we don't have
712 // __wxterminate in Mach-O shared libraries
713 wxStAppResource::CloseSharedLibraryResource();
715 wxMacCleanupConverters() ;
717 UMACleanupToolbox() ;
718 if (s_macCursorRgn
) {
719 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
727 //----------------------------------------------------------------------
729 //----------------------------------------------------------------------
731 // extern variable for shared library resource id
732 // need to be able to find it with NSLookupAndBindSymbol
733 short gSharedLibraryResource
= kResFileNotOpened
;
735 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
736 CFBundleRef gSharedLibraryBundle
= NULL
;
737 #endif /* WXMAKINGDLL && __DARWIN__ */
739 wxStAppResource::wxStAppResource()
741 m_currentRefNum
= CurResFile() ;
742 if ( gSharedLibraryResource
!= kResFileNotOpened
)
744 UseResFile( gSharedLibraryResource
) ;
748 wxStAppResource::~wxStAppResource()
750 if ( m_currentRefNum
!= kResFileNotOpened
)
752 UseResFile( m_currentRefNum
) ;
756 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
758 gSharedLibraryResource
= kResFileNotOpened
;
761 if ( initBlock
!= NULL
) {
762 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
763 FSSpec
*fileSpec
= NULL
;
765 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
766 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
768 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
769 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
772 if (fileSpec
!= NULL
) {
773 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
778 // Open the shared library resource file if it is not yet open
781 const char *theLibPath
;
783 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
784 if (gSharedLibraryBundle
!= NULL
) {
785 // wxWindows has been bundled into a framework
786 // load the framework resources
788 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
791 // wxWindows is a simple dynamic shared library
792 // load the resources from the data fork of a separate resource file
797 OSErr theErr
= noErr
;
799 // get the library path
800 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
801 theModule
= NSModuleForSymbol(theSymbol
);
802 theLibPath
= NSLibraryNameForModule(theModule
);
804 wxLogDebug( wxT("wxMac library installation name is '%s'"),
807 // allocate copy to replace .dylib.* extension with .rsrc
808 theResPath
= strdup(theLibPath
);
809 if (theResPath
!= NULL
) {
810 theName
= strrchr(theResPath
, '/');
811 if (theName
== NULL
) {
812 // no directory elements in path
813 theName
= theResPath
;
815 // find ".dylib" shared library extension
816 theExt
= strstr(theName
, ".dylib");
817 // overwrite extension with ".rsrc"
818 strcpy(theExt
, ".rsrc");
820 wxLogDebug( wxT("wxMac resources file name is '%s'"),
823 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
824 if (theErr
!= noErr
) {
825 // try in current directory (using name only)
826 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
829 // open the resource file
830 if (theErr
== noErr
) {
831 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
832 &gSharedLibraryResource
);
834 if (theErr
!= noErr
) {
835 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
839 // free duplicated resource file path
843 #endif /* __DARWIN__ */
845 #endif /* WXMAKINGDLL */
848 void wxStAppResource::CloseSharedLibraryResource()
851 // Close the shared library resource file
852 if (gSharedLibraryResource
!= kResFileNotOpened
) {
854 if (gSharedLibraryBundle
!= NULL
) {
855 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
856 gSharedLibraryResource
);
857 gSharedLibraryBundle
= NULL
;
860 #endif /* __DARWIN__ */
862 CloseResFile(gSharedLibraryResource
);
864 gSharedLibraryResource
= kResFileNotOpened
;
866 #endif /* WXMAKINGDLL */
869 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
871 // for shared libraries we have to manually get the correct resource
872 // ref num upon initializing and releasing when terminating, therefore
873 // the __wxinitialize and __wxterminate must be used
876 void __sinit(void); /* (generated by linker) */
877 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
878 pascal void __terminate(void);
881 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
883 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
884 return __initialize( theInitBlock
) ;
887 pascal void __wxterminate(void)
889 wxStAppResource::CloseSharedLibraryResource() ;
893 #endif /* WXMAKINGDLL && !__DARWIN__ */
895 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
897 return wxApp::Initialize();
900 int WXDLLEXPORT
wxEntryInitGui()
902 return wxTheApp
->OnInitGui();
905 void WXDLLEXPORT
wxEntryCleanup()
910 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
913 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
914 // This seems to be necessary since there are 'rogue'
915 // objects present at this point (perhaps global objects?)
916 // Setting a checkpoint will ignore them as far as the
917 // memory checking facility is concerned.
918 // Of course you may argue that memory allocated in globals should be
919 // checked, but this is a reasonable compromise.
920 wxDebugContext::SetCheckpoint();
923 if (!wxEntryStart(argc
, argv
)) {
926 // create the application object or ensure that one already exists
929 // The app may have declared a global application object, but we recommend
930 // the IMPLEMENT_APP macro is used instead, which sets an initializer
931 // function for delayed, dynamic app object construction.
932 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
933 wxT("No initializer - use IMPLEMENT_APP macro.") );
935 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
938 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
941 // Mac OS X passes a process serial number command line argument when
942 // the application is launched from the Finder. This argument must be
943 // removed from the command line arguments before being handled by the
944 // application (otherwise applications would need to handle it)
947 if (strncmp(argv
[1], "-psn_", 5) == 0) {
948 // assume the argument is always the only one and remove it
953 argc
= 0 ; // currently we don't support files as parameters
955 // we could try to get the open apple events here to adjust argc and argv better
957 wxTheApp
->argc
= argc
;
959 wxTheApp
->argv
= new wxChar
*[argc
+1];
961 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
963 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
965 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
967 wxTheApp
->argv
= argv
;
970 // GUI-specific initialization, such as creating an app context.
973 // Here frames insert themselves automatically
974 // into wxTopLevelWindows by getting created
979 if ( wxTheApp
->OnInit() )
983 retValue
= wxTheApp
->OnRun();
986 // We want to initialize, but not run or exit immediately.
989 //else: app initialization failed, so we skipped OnRun()
991 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
994 // Forcibly delete the window.
995 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
996 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
998 topWindow
->Close(TRUE
);
999 wxTheApp
->DeletePendingObjects();
1004 wxTheApp
->SetTopWindow(NULL
);
1017 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1019 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1020 OSStatus err
= noErr
;
1023 switch( GetEventClass( event
) )
1025 case kEventClassKeyboard
:
1028 switch( GetEventKind(event
) )
1030 case kEventRawKeyDown
:
1031 rec
->what
= keyDown
;
1033 case kEventRawKeyRepeat
:
1034 rec
->what
= autoKey
;
1036 case kEventRawKeyUp
:
1039 case kEventRawKeyModifiersChanged
:
1040 rec
->what
= nullEvent
;
1049 unsigned char charCode
;
1051 GetMouse( &rec
->where
) ;
1053 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1054 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1055 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1056 rec
->modifiers
= modifiers
;
1057 rec
->message
= (keyCode
<< 8 ) + charCode
;
1061 case kEventClassTextInput
:
1063 switch( GetEventKind( event
) )
1065 case kEventTextInputUnicodeForKeyEvent
:
1068 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1072 unsigned char charCode
;
1074 GetMouse( &rec
->where
) ;
1075 rec
->what
= keyDown
;
1076 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1077 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1078 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1079 rec
->modifiers
= modifiers
;
1080 rec
->message
= (keyCode
<< 8 ) + charCode
;
1096 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1098 OSStatus result = eventNotHandledErr ;
1101 switch ( GetEventClass( event ) )
1103 case kEventClassKeyboard :
1104 if ( wxMacConvertEventToRecord( event , &rec ) )
1106 wxTheApp->MacHandleModifierEvents( &rec ) ;
1107 wxTheApp->MacHandleOneEvent( &rec ) ;
1111 case kEventClassTextInput :
1112 if ( wxMacConvertEventToRecord( event , &rec ) )
1114 wxTheApp->MacHandleModifierEvents( &rec ) ;
1115 wxTheApp->MacHandleOneEvent( &rec ) ;
1127 // Static member initialization
1128 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1135 #if WXWIN_COMPATIBILITY_2_2
1136 m_wantDebugOutput
= TRUE
;
1142 m_printMode
= wxPRINT_WINDOWS
;
1146 bool wxApp::Initialized()
1154 int wxApp::MainLoop()
1166 // Returns TRUE if more time is needed.
1167 bool wxApp::ProcessIdle()
1170 event
.SetEventObject(this);
1171 ProcessEvent(event
);
1173 return event
.MoreRequested();
1176 void wxApp::ExitMainLoop()
1178 m_keepGoing
= FALSE
;
1181 // Is a message/event pending?
1182 bool wxApp::Pending()
1185 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1189 return EventAvail( everyEvent
, &event
) ;
1193 // Dispatch a message.
1194 void wxApp::Dispatch()
1199 void wxApp::OnIdle(wxIdleEvent
& event
)
1201 // Avoid recursion (via ProcessEvent default case)
1208 // 'Garbage' collection of windows deleted with Close().
1209 DeletePendingObjects();
1211 // flush the logged messages if any
1212 wxLog
*pLog
= wxLog::GetActiveTarget();
1213 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1216 // Send OnIdle events to all windows
1217 bool needMore
= SendIdleEvents();
1220 event
.RequestMore(TRUE
);
1222 // If they are pending events, we must process them: pending events are
1223 // either events to the threads other than main or events posted with
1224 // wxPostEvent() functions
1225 wxMacProcessNotifierAndPendingEvents();
1235 // Send idle event to all top-level windows
1236 bool wxApp::SendIdleEvents()
1238 bool needMore
= FALSE
;
1239 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1242 wxWindow
* win
= node
->GetData();
1243 if (SendIdleEvents(win
))
1246 node
= node
->GetNext();
1251 // Send idle event to window and all subwindows
1252 bool wxApp::SendIdleEvents(wxWindow
* win
)
1254 bool needMore
= FALSE
;
1257 event
.SetEventObject(win
);
1258 win
->ProcessEvent(event
);
1260 if (event
.MoreRequested())
1263 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1266 wxWindow
* win
= node
->GetData();
1267 if (SendIdleEvents(win
))
1270 node
= node
->GetNext();
1275 void wxApp::DeletePendingObjects()
1277 wxNode
*node
= wxPendingDelete
.GetFirst();
1280 wxObject
*obj
= (wxObject
*)node
->GetData();
1284 if (wxPendingDelete
.Member(obj
))
1287 // Deleting one object may have deleted other pending
1288 // objects, so start from beginning of list again.
1289 node
= wxPendingDelete
.GetFirst();
1295 wxLogError(_("Fatal error: exiting"));
1301 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1304 GetTopWindow()->Close(TRUE
);
1307 // Default behaviour: close the application with prompts. The
1308 // user can veto the close, and therefore the end session.
1309 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1313 if (!GetTopWindow()->Close(!event
.CanVeto()))
1318 extern "C" void wxCYield() ;
1324 // Yield to other processes
1326 bool wxApp::Yield(bool onlyIfNeeded
)
1330 if ( !onlyIfNeeded
)
1332 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1341 YieldToAnyThread() ;
1343 // by definition yield should handle all non-processed events
1347 OSStatus status
= noErr
;
1350 s_inReceiveEvent
= true ;
1351 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1352 s_inReceiveEvent
= false ;
1354 if ( status
== eventLoopTimedOutErr
)
1356 // make sure next time the event loop will trigger idle events
1357 sleepTime
= kEventDurationNoWait
;
1359 else if ( status
== eventLoopQuitErr
)
1361 // according to QA1061 this may also occur when a WakeUp Process
1366 MacHandleOneEvent( theEvent
) ;
1367 ReleaseEvent(theEvent
);
1369 } while( status
== noErr
) ;
1373 // having a larger value here leads to large performance slowdowns
1374 // so we cannot give background apps more processor time here
1375 // we do so however having a large sleep value in the main event loop
1378 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1380 MacHandleModifierEvents( &event
) ;
1381 MacHandleOneEvent( &event
);
1382 if ( event
.what
!= kHighLevelEvent
)
1383 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1385 MacHandleModifierEvents( &event
) ;
1388 wxMacProcessNotifierAndPendingEvents() ;
1394 // platform specifics
1396 void wxApp::MacSuspend( bool convertClipboard
)
1399 // we have to deactive the top level windows manually
1401 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1404 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1405 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1407 node
= node
->GetNext();
1410 ::HideFloatingWindows() ;
1412 s_lastMouseDown
= 0 ;
1414 if( convertClipboard
)
1416 MacConvertPrivateToPublicScrap() ;
1420 extern wxList wxModalDialogs
;
1422 void wxApp::MacResume( bool convertClipboard
)
1424 s_lastMouseDown
= 0 ;
1425 if( convertClipboard
)
1427 MacConvertPublicToPrivateScrap() ;
1431 ::ShowFloatingWindows() ;
1432 // raise modal dialogs in case a non modal window was selected to activate the app
1434 wxNode
* node
= wxModalDialogs
.GetFirst();
1437 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1440 node
= node
->GetNext();
1445 void wxApp::MacConvertPrivateToPublicScrap()
1449 void wxApp::MacConvertPublicToPrivateScrap()
1453 void wxApp::MacDoOneEvent()
1458 s_inReceiveEvent
= true ;
1459 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1460 s_inReceiveEvent
= false ;
1461 if ( status
== eventLoopTimedOutErr
)
1463 if ( wxTheApp
->ProcessIdle() )
1464 sleepTime
= kEventDurationNoWait
;
1466 sleepTime
= kEventDurationForever
;
1468 else if ( status
== eventLoopQuitErr
)
1470 // according to QA1061 this may also occur when a WakeUp Process
1475 MacHandleOneEvent( theEvent
) ;
1476 ReleaseEvent(theEvent
);
1477 sleepTime
= kEventDurationNoWait
;
1482 EventMask eventMask
= everyEvent
;
1484 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1486 MacHandleModifierEvents( &event
) ;
1487 MacHandleOneEvent( &event
);
1491 MacHandleModifierEvents( &event
) ;
1493 WindowPtr window
= ::FrontWindow() ;
1495 ::IdleControls( window
) ;
1497 if ( wxTheApp
->ProcessIdle() )
1500 sleepTime
= GetCaretTime() / 2 ;
1502 if ( event
.what
!= kHighLevelEvent
)
1503 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1507 DeletePendingObjects() ;
1508 wxMacProcessNotifierAndPendingEvents() ;
1511 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1514 EventTargetRef theTarget
;
1515 theTarget
= GetEventDispatcherTarget();
1516 m_macCurrentEvent
= evr
;
1517 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1519 EventRecord
* ev
= (EventRecord
*) evr
;
1520 m_macCurrentEvent
= ev
;
1522 wxApp::sm_lastMessageTime
= ev
->when
;
1527 MacHandleMouseDownEvent( ev
) ;
1528 if ( ev
->modifiers
& controlKey
)
1529 s_lastMouseDown
= 2;
1531 s_lastMouseDown
= 1;
1534 if ( s_lastMouseDown
== 2 )
1536 ev
->modifiers
|= controlKey
;
1540 ev
->modifiers
&= ~controlKey
;
1542 MacHandleMouseUpEvent( ev
) ;
1543 s_lastMouseDown
= 0;
1546 MacHandleActivateEvent( ev
) ;
1549 MacHandleUpdateEvent( ev
) ;
1553 MacHandleKeyDownEvent( ev
) ;
1556 MacHandleKeyUpEvent( ev
) ;
1559 MacHandleDiskEvent( ev
) ;
1562 MacHandleOSEvent( ev
) ;
1564 case kHighLevelEvent
:
1565 MacHandleHighLevelEvent( ev
) ;
1571 wxMacProcessNotifierAndPendingEvents() ;
1575 bool s_macIsInModalLoop
= false ;
1577 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1579 EventRecord
* ev
= (EventRecord
*) evr
;
1580 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1582 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1584 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1585 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1586 event
.m_altDown
= ev
->modifiers
& optionKey
;
1587 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1589 event
.m_x
= ev
->where
.h
;
1590 event
.m_y
= ev
->where
.v
;
1591 event
.m_timeStamp
= ev
->when
;
1592 wxWindow
* focus
= wxWindow::FindFocus() ;
1593 event
.SetEventObject(focus
);
1595 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1597 event
.m_keyCode
= WXK_CONTROL
;
1598 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1599 focus
->GetEventHandler()->ProcessEvent( event
) ;
1601 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1603 event
.m_keyCode
= WXK_SHIFT
;
1604 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1605 focus
->GetEventHandler()->ProcessEvent( event
) ;
1607 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1609 event
.m_keyCode
= WXK_ALT
;
1610 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1611 focus
->GetEventHandler()->ProcessEvent( event
) ;
1613 s_lastModifiers
= ev
->modifiers
;
1617 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1619 // we must avoid reentrancy problems when processing high level events eg printing
1620 bool former
= s_inYield
;
1622 EventRecord
* ev
= (EventRecord
*) evr
;
1623 ::AEProcessAppleEvent( ev
) ;
1624 s_inYield
= former
;
1627 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1629 EventRecord
* ev
= (EventRecord
*) evr
;
1630 wxToolTip::RemoveToolTips() ;
1633 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1634 WindowAttributes frontWindowAttributes
= NULL
;
1636 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1638 short windowPart
= ::FindWindow(ev
->where
, &window
);
1639 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1640 if ( wxPendingDelete
.Member(win
) )
1644 GetQDGlobalsScreenBits( &screenBits
);
1649 if ( s_macIsInModalLoop
)
1655 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1656 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1657 s_lastMouseDown
= 0;
1661 SystemClick( ev
, window
) ;
1662 s_lastMouseDown
= 0;
1665 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1671 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1676 Point pt
= { 0, 0 } ;
1677 SetPortWindowPort(window
) ;
1678 LocalToGlobal( &pt
) ;
1680 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1681 -1 , wxSIZE_USE_EXISTING
);
1683 s_lastMouseDown
= 0;
1687 if (TrackGoAway(window
, ev
->where
))
1692 s_lastMouseDown
= 0;
1696 Rect newContentRect
;
1697 Rect constraintRect
;
1698 constraintRect
.top
= win
->GetMinHeight() ;
1699 if ( constraintRect
.top
== -1 )
1700 constraintRect
.top
= 0 ;
1701 constraintRect
.left
= win
->GetMinWidth() ;
1702 if ( constraintRect
.left
== -1 )
1703 constraintRect
.left
= 0 ;
1704 constraintRect
.right
= win
->GetMaxWidth() ;
1705 if ( constraintRect
.right
== -1 )
1706 constraintRect
.right
= 32000 ;
1707 constraintRect
.bottom
= win
->GetMaxHeight() ;
1708 if ( constraintRect
.bottom
== -1 )
1709 constraintRect
.bottom
= 32000 ;
1711 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1712 &constraintRect
, &newContentRect
) ;
1715 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1716 newContentRect
.right
- newContentRect
.left
,
1717 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1719 s_lastMouseDown
= 0;
1724 if (TrackBox(window
, ev
->where
, windowPart
))
1726 // TODO setup size event
1727 ZoomWindow( window
, windowPart
, false ) ;
1733 Point pt
= { 0, 0 } ;
1734 SetPortWindowPort(window
) ;
1735 LocalToGlobal( &pt
) ;
1738 GetWindowPortBounds(window
, &tempRect
) ;
1739 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1740 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1743 s_lastMouseDown
= 0;
1745 case inCollapseBox
:
1746 // TODO setup size event
1747 s_lastMouseDown
= 0;
1754 SetPortWindowPort(window
) ;
1757 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1759 if ( s_macIsInModalLoop
)
1763 else if ( UMAIsWindowFloating( window
) )
1766 win
->MacMouseDown( ev
, windowPart
) ;
1771 win
->MacMouseDown( ev
, windowPart
) ;
1772 ::SelectWindow( window
) ;
1778 win
->MacMouseDown( ev
, windowPart
) ;
1786 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1788 EventRecord
* ev
= (EventRecord
*) evr
;
1791 short windowPart
= inNoWindow
;
1792 if ( wxTheApp
->s_captureWindow
)
1794 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1795 windowPart
= inContent
;
1799 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1810 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1812 win
->MacMouseUp( ev
, windowPart
) ;
1820 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1821 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1826 case kHomeCharCode
:
1829 case kEnterCharCode
:
1830 retval
= WXK_RETURN
;
1835 case kHelpCharCode
:
1838 case kBackspaceCharCode
:
1844 case kPageUpCharCode
:
1845 retval
= WXK_PAGEUP
;
1847 case kPageDownCharCode
:
1848 retval
= WXK_PAGEDOWN
;
1850 case kReturnCharCode
:
1851 retval
= WXK_RETURN
;
1853 case kFunctionKeyCharCode
:
1905 case kEscapeCharCode
:
1906 retval
= WXK_ESCAPE
;
1908 case kLeftArrowCharCode
:
1911 case kRightArrowCharCode
:
1912 retval
= WXK_RIGHT
;
1914 case kUpArrowCharCode
:
1917 case kDownArrowCharCode
:
1920 case kDeleteCharCode
:
1921 retval
= WXK_DELETE
;
1930 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1932 EventRecord
* ev
= (EventRecord
*) evr
;
1933 wxToolTip::RemoveToolTips() ;
1935 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1936 if ( HiWord( menuresult
) )
1938 if ( !s_macIsInModalLoop
)
1939 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1943 wxWindow
* focus
= wxWindow::FindFocus() ;
1945 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1947 // has not been handled -> perform default
1948 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1949 if ( control
&& control
->GetMacControl() != NULL
)
1953 keychar
= short(ev
->message
& charCodeMask
);
1954 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1955 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1961 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1963 EventRecord
* ev
= (EventRecord
*) evr
;
1964 wxToolTip::RemoveToolTips() ;
1966 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1967 if ( HiWord( menuresult
) )
1972 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1978 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1985 keychar
= short(keymessage
& charCodeMask
);
1986 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1988 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1990 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1991 // and look at the character after
1993 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1994 keychar
= short(keyInfo
& charCodeMask
);
1995 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1997 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1998 long realkeyval
= keyval
;
1999 if ( keyval
== keychar
)
2001 // 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)
2002 realkeyval
= short(keymessage
& charCodeMask
) ;
2003 keyval
= wxToupper( keyval
) ;
2006 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2007 bool handled
= false ;
2008 event
.m_shiftDown
= modifiers
& shiftKey
;
2009 event
.m_controlDown
= modifiers
& controlKey
;
2010 event
.m_altDown
= modifiers
& optionKey
;
2011 event
.m_metaDown
= modifiers
& cmdKey
;
2012 event
.m_keyCode
= keyval
;
2016 event
.m_timeStamp
= when
;
2017 event
.SetEventObject(focus
);
2018 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2019 if ( handled
&& event
.GetSkipped() )
2026 wxWindow
*ancestor
= focus
;
2029 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2032 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2033 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2036 if (ancestor
->IsTopLevel())
2038 ancestor
= ancestor
->GetParent();
2041 #endif // wxUSE_ACCEL
2045 event
.Skip( FALSE
) ;
2046 event
.SetEventType( wxEVT_CHAR
) ;
2048 event
.m_keyCode
= realkeyval
;
2050 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2051 if ( handled
&& event
.GetSkipped() )
2055 (keyval
== WXK_TAB
) &&
2056 // CS: copied the change below from wxGTK
2057 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2058 // have this style, yet choose not to process this particular TAB in which
2059 // case TAB must still work as a navigational character
2061 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2063 (focus
->GetParent()) &&
2064 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2066 wxNavigationKeyEvent new_event
;
2067 new_event
.SetEventObject( focus
);
2068 new_event
.SetDirection( !event
.ShiftDown() );
2069 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2070 new_event
.SetWindowChange( event
.ControlDown() );
2071 new_event
.SetCurrentFocus( focus
);
2072 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2073 if ( handled
&& new_event
.GetSkipped() )
2076 // backdoor handler for default return and command escape
2077 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2079 // if window is not having a focus still testing for default enter or cancel
2080 // TODO add the UMA version for ActiveNonFloatingWindow
2081 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2084 if ( keyval
== WXK_RETURN
)
2086 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2088 if ( def
&& def
->IsEnabled() )
2090 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2091 event
.SetEventObject(def
);
2092 def
->Command(event
);
2096 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2097 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2099 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2100 new_event
.SetEventObject( focus
);
2101 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2108 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2115 keychar
= short(keymessage
& charCodeMask
);
2116 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2117 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2119 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2120 // and look at the character after
2122 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2123 keychar
= short(keyInfo
& charCodeMask
);
2124 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2126 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2128 if ( keyval
== keychar
)
2130 keyval
= wxToupper( keyval
) ;
2132 bool handled
= false ;
2134 wxKeyEvent
event(wxEVT_KEY_UP
);
2135 event
.m_shiftDown
= modifiers
& shiftKey
;
2136 event
.m_controlDown
= modifiers
& controlKey
;
2137 event
.m_altDown
= modifiers
& optionKey
;
2138 event
.m_metaDown
= modifiers
& cmdKey
;
2139 event
.m_keyCode
= keyval
;
2143 event
.m_timeStamp
= when
;
2144 event
.SetEventObject(focus
);
2145 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2151 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2153 EventRecord
* ev
= (EventRecord
*) evr
;
2154 WindowRef window
= (WindowRef
) ev
->message
;
2157 bool activate
= (ev
->modifiers
& activeFlag
) ;
2158 WindowClass wclass
;
2159 ::GetWindowClass ( window
, &wclass
) ;
2160 if ( wclass
== kFloatingWindowClass
)
2162 // if it is a floater we activate/deactivate the front non-floating window instead
2163 window
= ::FrontNonFloatingWindow() ;
2165 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2167 win
->MacActivate( ev
->when
, activate
) ;
2171 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2173 EventRecord
* ev
= (EventRecord
*) evr
;
2174 WindowRef window
= (WindowRef
) ev
->message
;
2175 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2178 if ( !wxPendingDelete
.Member(win
) )
2179 win
->MacUpdate( ev
->when
) ;
2183 // since there is no way of telling this foreign window to update itself
2184 // we have to invalidate the update region otherwise we keep getting the same
2185 // event over and over again
2186 BeginUpdate( window
) ;
2187 EndUpdate( window
) ;
2191 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2193 EventRecord
* ev
= (EventRecord
*) evr
;
2194 if ( HiWord( ev
->message
) != noErr
)
2198 SetPt( &point
, 100 , 100 ) ;
2200 err
= DIBadMount( point
, ev
->message
) ;
2201 wxASSERT( err
== noErr
) ;
2205 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2207 EventRecord
* ev
= (EventRecord
*) evr
;
2208 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2210 case suspendResumeMessage
:
2212 bool isResuming
= ev
->message
& resumeFlag
;
2213 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2215 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2218 WindowRef oldFrontWindow
= NULL
;
2219 WindowRef newFrontWindow
= NULL
;
2221 // in case we don't take care of activating ourselves, we have to synchronize
2222 // our idea of the active window with the process manager's - which it already activated
2224 if ( !doesActivate
)
2225 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2227 MacResume( convertClipboard
) ;
2229 newFrontWindow
= ::FrontNonFloatingWindow() ;
2231 if ( oldFrontWindow
)
2233 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2235 win
->MacActivate( ev
->when
, false ) ;
2237 if ( newFrontWindow
)
2239 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2241 win
->MacActivate( ev
->when
, true ) ;
2246 MacSuspend( convertClipboard
) ;
2250 case mouseMovedMessage
:
2254 wxWindow
* currentMouseWindow
= NULL
;
2256 if (s_captureWindow
)
2258 currentMouseWindow
= s_captureWindow
;
2262 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2263 ¤tMouseWindow
) ;
2266 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2268 wxMouseEvent event
;
2270 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2271 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2273 event
.m_leftDown
= isDown
&& !controlDown
;
2274 event
.m_middleDown
= FALSE
;
2275 event
.m_rightDown
= isDown
&& controlDown
;
2276 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2277 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2278 event
.m_altDown
= ev
->modifiers
& optionKey
;
2279 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2280 event
.m_x
= ev
->where
.h
;
2281 event
.m_y
= ev
->where
.v
;
2282 event
.m_timeStamp
= ev
->when
;
2283 event
.SetEventObject(this);
2285 if ( wxWindow::s_lastMouseWindow
)
2287 wxMouseEvent
eventleave(event
);
2288 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2289 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2290 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2292 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2294 if ( currentMouseWindow
)
2296 wxMouseEvent
evententer(event
);
2297 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2298 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2299 evententer
.SetEventObject( currentMouseWindow
) ;
2300 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2302 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2305 short windowPart
= inNoWindow
;
2307 if ( s_captureWindow
)
2309 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2310 windowPart
= inContent
;
2314 windowPart
= ::FindWindow(ev
->where
, &window
);
2321 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2323 win
->MacMouseMoved( ev
, windowPart
) ;
2330 UMAShowArrowCursor();
2340 UMAShowArrowCursor();
2351 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2353 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2354 wxFrame
* frame
= mbar
->GetFrame();
2355 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2358 frame
->ProcessCommand(id
);
2363 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2366 return; // no menu item selected
2368 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2371 Str255 deskAccessoryName
;
2374 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2375 GetPort(&savedPort
);
2376 OpenDeskAcc(deskAccessoryName
);
2383 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2384 MacHandleMenuCommand( id
) ;