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 // If we don't do the following, we get an apparent memory leak.
674 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
677 wxClassInfo::CleanUpClasses();
680 # if __option(profile)
681 ProfilerDump( "\papp.prof" ) ;
689 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
690 // At this point we want to check if there are any memory
691 // blocks that aren't part of the wxDebugContext itself,
692 // as a special case. Then when dumping we need to ignore
693 // wxDebugContext, too.
694 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
696 wxLogDebug(wxT("There were memory leaks."));
697 wxDebugContext::Dump();
698 wxDebugContext::PrintStatistics();
700 // wxDebugContext::SetStream(NULL, NULL);
704 // do it as the very last thing because everything else can log messages
705 delete wxLog::SetActiveTarget(NULL
);
708 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
709 // close shared library resources from here since we don't have
710 // __wxterminate in Mach-O shared libraries
711 wxStAppResource::CloseSharedLibraryResource();
713 wxMacCleanupConverters() ;
715 UMACleanupToolbox() ;
716 if (s_macCursorRgn
) {
717 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
725 //----------------------------------------------------------------------
727 //----------------------------------------------------------------------
729 // extern variable for shared library resource id
730 // need to be able to find it with NSLookupAndBindSymbol
731 short gSharedLibraryResource
= kResFileNotOpened
;
733 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
734 CFBundleRef gSharedLibraryBundle
= NULL
;
735 #endif /* WXMAKINGDLL && __DARWIN__ */
737 wxStAppResource::wxStAppResource()
739 m_currentRefNum
= CurResFile() ;
740 if ( gSharedLibraryResource
!= kResFileNotOpened
)
742 UseResFile( gSharedLibraryResource
) ;
746 wxStAppResource::~wxStAppResource()
748 if ( m_currentRefNum
!= kResFileNotOpened
)
750 UseResFile( m_currentRefNum
) ;
754 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
756 gSharedLibraryResource
= kResFileNotOpened
;
759 if ( initBlock
!= NULL
) {
760 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
761 FSSpec
*fileSpec
= NULL
;
763 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
764 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
766 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
767 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
770 if (fileSpec
!= NULL
) {
771 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
776 // Open the shared library resource file if it is not yet open
779 const char *theLibPath
;
781 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
782 if (gSharedLibraryBundle
!= NULL
) {
783 // wxWindows has been bundled into a framework
784 // load the framework resources
786 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
789 // wxWindows is a simple dynamic shared library
790 // load the resources from the data fork of a separate resource file
795 OSErr theErr
= noErr
;
797 // get the library path
798 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
799 theModule
= NSModuleForSymbol(theSymbol
);
800 theLibPath
= NSLibraryNameForModule(theModule
);
802 wxLogDebug( wxT("wxMac library installation name is '%s'"),
805 // allocate copy to replace .dylib.* extension with .rsrc
806 theResPath
= strdup(theLibPath
);
807 if (theResPath
!= NULL
) {
808 theName
= strrchr(theResPath
, '/');
809 if (theName
== NULL
) {
810 // no directory elements in path
811 theName
= theResPath
;
813 // find ".dylib" shared library extension
814 theExt
= strstr(theName
, ".dylib");
815 // overwrite extension with ".rsrc"
816 strcpy(theExt
, ".rsrc");
818 wxLogDebug( wxT("wxMac resources file name is '%s'"),
821 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
822 if (theErr
!= noErr
) {
823 // try in current directory (using name only)
824 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
827 // open the resource file
828 if (theErr
== noErr
) {
829 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
830 &gSharedLibraryResource
);
832 if (theErr
!= noErr
) {
833 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
837 // free duplicated resource file path
841 #endif /* __DARWIN__ */
843 #endif /* WXMAKINGDLL */
846 void wxStAppResource::CloseSharedLibraryResource()
849 // Close the shared library resource file
850 if (gSharedLibraryResource
!= kResFileNotOpened
) {
852 if (gSharedLibraryBundle
!= NULL
) {
853 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
854 gSharedLibraryResource
);
855 gSharedLibraryBundle
= NULL
;
858 #endif /* __DARWIN__ */
860 CloseResFile(gSharedLibraryResource
);
862 gSharedLibraryResource
= kResFileNotOpened
;
864 #endif /* WXMAKINGDLL */
867 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
869 // for shared libraries we have to manually get the correct resource
870 // ref num upon initializing and releasing when terminating, therefore
871 // the __wxinitialize and __wxterminate must be used
874 void __sinit(void); /* (generated by linker) */
875 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
876 pascal void __terminate(void);
879 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
881 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
882 return __initialize( theInitBlock
) ;
885 pascal void __wxterminate(void)
887 wxStAppResource::CloseSharedLibraryResource() ;
891 #endif /* WXMAKINGDLL && !__DARWIN__ */
893 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
895 return wxApp::Initialize();
898 int WXDLLEXPORT
wxEntryInitGui()
900 return wxTheApp
->OnInitGui();
903 void WXDLLEXPORT
wxEntryCleanup()
908 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
911 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
912 // This seems to be necessary since there are 'rogue'
913 // objects present at this point (perhaps global objects?)
914 // Setting a checkpoint will ignore them as far as the
915 // memory checking facility is concerned.
916 // Of course you may argue that memory allocated in globals should be
917 // checked, but this is a reasonable compromise.
918 wxDebugContext::SetCheckpoint();
921 if (!wxEntryStart(argc
, argv
)) {
924 // create the application object or ensure that one already exists
927 // The app may have declared a global application object, but we recommend
928 // the IMPLEMENT_APP macro is used instead, which sets an initializer
929 // function for delayed, dynamic app object construction.
930 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
931 wxT("No initializer - use IMPLEMENT_APP macro.") );
933 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
936 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
939 // Mac OS X passes a process serial number command line argument when
940 // the application is launched from the Finder. This argument must be
941 // removed from the command line arguments before being handled by the
942 // application (otherwise applications would need to handle it)
945 if (strncmp(argv
[1], "-psn_", 5) == 0) {
946 // assume the argument is always the only one and remove it
951 argc
= 0 ; // currently we don't support files as parameters
953 // we could try to get the open apple events here to adjust argc and argv better
955 wxTheApp
->argc
= argc
;
957 wxTheApp
->argv
= new wxChar
*[argc
+1];
959 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
961 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
963 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
965 wxTheApp
->argv
= argv
;
968 // GUI-specific initialization, such as creating an app context.
971 // Here frames insert themselves automatically
972 // into wxTopLevelWindows by getting created
977 if ( wxTheApp
->OnInit() )
981 retValue
= wxTheApp
->OnRun();
984 // We want to initialize, but not run or exit immediately.
987 //else: app initialization failed, so we skipped OnRun()
989 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
992 // Forcibly delete the window.
993 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
994 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
996 topWindow
->Close(TRUE
);
997 wxTheApp
->DeletePendingObjects();
1002 wxTheApp
->SetTopWindow(NULL
);
1015 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1017 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1018 OSStatus err
= noErr
;
1021 switch( GetEventClass( event
) )
1023 case kEventClassKeyboard
:
1026 switch( GetEventKind(event
) )
1028 case kEventRawKeyDown
:
1029 rec
->what
= keyDown
;
1031 case kEventRawKeyRepeat
:
1032 rec
->what
= autoKey
;
1034 case kEventRawKeyUp
:
1037 case kEventRawKeyModifiersChanged
:
1038 rec
->what
= nullEvent
;
1047 unsigned char charCode
;
1049 GetMouse( &rec
->where
) ;
1051 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1052 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1053 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1054 rec
->modifiers
= modifiers
;
1055 rec
->message
= (keyCode
<< 8 ) + charCode
;
1059 case kEventClassTextInput
:
1061 switch( GetEventKind( event
) )
1063 case kEventTextInputUnicodeForKeyEvent
:
1066 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1070 unsigned char charCode
;
1072 GetMouse( &rec
->where
) ;
1073 rec
->what
= keyDown
;
1074 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1075 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1076 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1077 rec
->modifiers
= modifiers
;
1078 rec
->message
= (keyCode
<< 8 ) + charCode
;
1094 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1096 OSStatus result = eventNotHandledErr ;
1099 switch ( GetEventClass( event ) )
1101 case kEventClassKeyboard :
1102 if ( wxMacConvertEventToRecord( event , &rec ) )
1104 wxTheApp->MacHandleModifierEvents( &rec ) ;
1105 wxTheApp->MacHandleOneEvent( &rec ) ;
1109 case kEventClassTextInput :
1110 if ( wxMacConvertEventToRecord( event , &rec ) )
1112 wxTheApp->MacHandleModifierEvents( &rec ) ;
1113 wxTheApp->MacHandleOneEvent( &rec ) ;
1125 // Static member initialization
1126 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1133 #if WXWIN_COMPATIBILITY_2_2
1134 m_wantDebugOutput
= TRUE
;
1140 m_printMode
= wxPRINT_WINDOWS
;
1144 bool wxApp::Initialized()
1152 int wxApp::MainLoop()
1164 // Returns TRUE if more time is needed.
1165 bool wxApp::ProcessIdle()
1168 event
.SetEventObject(this);
1169 ProcessEvent(event
);
1171 return event
.MoreRequested();
1174 void wxApp::ExitMainLoop()
1176 m_keepGoing
= FALSE
;
1179 // Is a message/event pending?
1180 bool wxApp::Pending()
1183 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1187 return EventAvail( everyEvent
, &event
) ;
1191 // Dispatch a message.
1192 void wxApp::Dispatch()
1197 void wxApp::OnIdle(wxIdleEvent
& event
)
1199 // Avoid recursion (via ProcessEvent default case)
1206 // 'Garbage' collection of windows deleted with Close().
1207 DeletePendingObjects();
1209 // flush the logged messages if any
1210 wxLog
*pLog
= wxLog::GetActiveTarget();
1211 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1214 // Send OnIdle events to all windows
1215 bool needMore
= SendIdleEvents();
1218 event
.RequestMore(TRUE
);
1220 // If they are pending events, we must process them: pending events are
1221 // either events to the threads other than main or events posted with
1222 // wxPostEvent() functions
1223 wxMacProcessNotifierAndPendingEvents();
1233 // Send idle event to all top-level windows
1234 bool wxApp::SendIdleEvents()
1236 bool needMore
= FALSE
;
1237 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1240 wxWindow
* win
= node
->GetData();
1241 if (SendIdleEvents(win
))
1244 node
= node
->GetNext();
1249 // Send idle event to window and all subwindows
1250 bool wxApp::SendIdleEvents(wxWindow
* win
)
1252 bool needMore
= FALSE
;
1255 event
.SetEventObject(win
);
1256 win
->ProcessEvent(event
);
1258 if (event
.MoreRequested())
1261 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1264 wxWindow
* win
= node
->GetData();
1265 if (SendIdleEvents(win
))
1268 node
= node
->GetNext();
1273 void wxApp::DeletePendingObjects()
1275 wxNode
*node
= wxPendingDelete
.GetFirst();
1278 wxObject
*obj
= (wxObject
*)node
->GetData();
1282 if (wxPendingDelete
.Member(obj
))
1285 // Deleting one object may have deleted other pending
1286 // objects, so start from beginning of list again.
1287 node
= wxPendingDelete
.GetFirst();
1293 wxLogError(_("Fatal error: exiting"));
1299 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1302 GetTopWindow()->Close(TRUE
);
1305 // Default behaviour: close the application with prompts. The
1306 // user can veto the close, and therefore the end session.
1307 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1311 if (!GetTopWindow()->Close(!event
.CanVeto()))
1316 extern "C" void wxCYield() ;
1322 // Yield to other processes
1324 bool wxApp::Yield(bool onlyIfNeeded
)
1328 if ( !onlyIfNeeded
)
1330 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1339 YieldToAnyThread() ;
1341 // by definition yield should handle all non-processed events
1345 OSStatus status
= noErr
;
1348 s_inReceiveEvent
= true ;
1349 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1350 s_inReceiveEvent
= false ;
1352 if ( status
== eventLoopTimedOutErr
)
1354 // make sure next time the event loop will trigger idle events
1355 sleepTime
= kEventDurationNoWait
;
1357 else if ( status
== eventLoopQuitErr
)
1359 // according to QA1061 this may also occur when a WakeUp Process
1364 MacHandleOneEvent( theEvent
) ;
1365 ReleaseEvent(theEvent
);
1367 } while( status
== noErr
) ;
1371 // having a larger value here leads to large performance slowdowns
1372 // so we cannot give background apps more processor time here
1373 // we do so however having a large sleep value in the main event loop
1376 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1378 MacHandleModifierEvents( &event
) ;
1379 MacHandleOneEvent( &event
);
1380 if ( event
.what
!= kHighLevelEvent
)
1381 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1383 MacHandleModifierEvents( &event
) ;
1386 wxMacProcessNotifierAndPendingEvents() ;
1392 // platform specifics
1394 void wxApp::MacSuspend( bool convertClipboard
)
1397 // we have to deactive the top level windows manually
1399 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1402 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1403 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1405 node
= node
->GetNext();
1408 ::HideFloatingWindows() ;
1410 s_lastMouseDown
= 0 ;
1412 if( convertClipboard
)
1414 MacConvertPrivateToPublicScrap() ;
1418 extern wxList wxModalDialogs
;
1420 void wxApp::MacResume( bool convertClipboard
)
1422 s_lastMouseDown
= 0 ;
1423 if( convertClipboard
)
1425 MacConvertPublicToPrivateScrap() ;
1429 ::ShowFloatingWindows() ;
1430 // raise modal dialogs in case a non modal window was selected to activate the app
1432 wxNode
* node
= wxModalDialogs
.GetFirst();
1435 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1438 node
= node
->GetNext();
1443 void wxApp::MacConvertPrivateToPublicScrap()
1447 void wxApp::MacConvertPublicToPrivateScrap()
1451 void wxApp::MacDoOneEvent()
1456 s_inReceiveEvent
= true ;
1457 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1458 s_inReceiveEvent
= false ;
1459 if ( status
== eventLoopTimedOutErr
)
1461 if ( wxTheApp
->ProcessIdle() )
1462 sleepTime
= kEventDurationNoWait
;
1464 sleepTime
= kEventDurationForever
;
1466 else if ( status
== eventLoopQuitErr
)
1468 // according to QA1061 this may also occur when a WakeUp Process
1473 MacHandleOneEvent( theEvent
) ;
1474 ReleaseEvent(theEvent
);
1475 sleepTime
= kEventDurationNoWait
;
1480 EventMask eventMask
= everyEvent
;
1482 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1484 MacHandleModifierEvents( &event
) ;
1485 MacHandleOneEvent( &event
);
1489 MacHandleModifierEvents( &event
) ;
1491 WindowPtr window
= ::FrontWindow() ;
1493 ::IdleControls( window
) ;
1495 if ( wxTheApp
->ProcessIdle() )
1498 sleepTime
= GetCaretTime() / 2 ;
1500 if ( event
.what
!= kHighLevelEvent
)
1501 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1505 DeletePendingObjects() ;
1506 wxMacProcessNotifierAndPendingEvents() ;
1509 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1512 EventTargetRef theTarget
;
1513 theTarget
= GetEventDispatcherTarget();
1514 m_macCurrentEvent
= evr
;
1515 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1517 EventRecord
* ev
= (EventRecord
*) evr
;
1518 m_macCurrentEvent
= ev
;
1520 wxApp::sm_lastMessageTime
= ev
->when
;
1525 MacHandleMouseDownEvent( ev
) ;
1526 if ( ev
->modifiers
& controlKey
)
1527 s_lastMouseDown
= 2;
1529 s_lastMouseDown
= 1;
1532 if ( s_lastMouseDown
== 2 )
1534 ev
->modifiers
|= controlKey
;
1538 ev
->modifiers
&= ~controlKey
;
1540 MacHandleMouseUpEvent( ev
) ;
1541 s_lastMouseDown
= 0;
1544 MacHandleActivateEvent( ev
) ;
1547 MacHandleUpdateEvent( ev
) ;
1551 MacHandleKeyDownEvent( ev
) ;
1554 MacHandleKeyUpEvent( ev
) ;
1557 MacHandleDiskEvent( ev
) ;
1560 MacHandleOSEvent( ev
) ;
1562 case kHighLevelEvent
:
1563 MacHandleHighLevelEvent( ev
) ;
1569 wxMacProcessNotifierAndPendingEvents() ;
1573 bool s_macIsInModalLoop
= false ;
1575 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1577 EventRecord
* ev
= (EventRecord
*) evr
;
1578 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1580 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1582 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1583 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1584 event
.m_altDown
= ev
->modifiers
& optionKey
;
1585 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1587 event
.m_x
= ev
->where
.h
;
1588 event
.m_y
= ev
->where
.v
;
1589 event
.m_timeStamp
= ev
->when
;
1590 wxWindow
* focus
= wxWindow::FindFocus() ;
1591 event
.SetEventObject(focus
);
1593 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1595 event
.m_keyCode
= WXK_CONTROL
;
1596 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1597 focus
->GetEventHandler()->ProcessEvent( event
) ;
1599 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1601 event
.m_keyCode
= WXK_SHIFT
;
1602 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1603 focus
->GetEventHandler()->ProcessEvent( event
) ;
1605 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1607 event
.m_keyCode
= WXK_ALT
;
1608 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1609 focus
->GetEventHandler()->ProcessEvent( event
) ;
1611 s_lastModifiers
= ev
->modifiers
;
1615 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1617 // we must avoid reentrancy problems when processing high level events eg printing
1618 bool former
= s_inYield
;
1620 EventRecord
* ev
= (EventRecord
*) evr
;
1621 ::AEProcessAppleEvent( ev
) ;
1622 s_inYield
= former
;
1625 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1627 EventRecord
* ev
= (EventRecord
*) evr
;
1628 wxToolTip::RemoveToolTips() ;
1631 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1632 WindowAttributes frontWindowAttributes
= NULL
;
1634 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1636 short windowPart
= ::FindWindow(ev
->where
, &window
);
1637 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1638 if ( wxPendingDelete
.Member(win
) )
1642 GetQDGlobalsScreenBits( &screenBits
);
1647 if ( s_macIsInModalLoop
)
1653 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1654 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1655 s_lastMouseDown
= 0;
1659 SystemClick( ev
, window
) ;
1660 s_lastMouseDown
= 0;
1663 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1669 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1674 Point pt
= { 0, 0 } ;
1675 SetPortWindowPort(window
) ;
1676 LocalToGlobal( &pt
) ;
1678 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1679 -1 , wxSIZE_USE_EXISTING
);
1681 s_lastMouseDown
= 0;
1685 if (TrackGoAway(window
, ev
->where
))
1690 s_lastMouseDown
= 0;
1694 Rect newContentRect
;
1695 Rect constraintRect
;
1696 constraintRect
.top
= win
->GetMinHeight() ;
1697 if ( constraintRect
.top
== -1 )
1698 constraintRect
.top
= 0 ;
1699 constraintRect
.left
= win
->GetMinWidth() ;
1700 if ( constraintRect
.left
== -1 )
1701 constraintRect
.left
= 0 ;
1702 constraintRect
.right
= win
->GetMaxWidth() ;
1703 if ( constraintRect
.right
== -1 )
1704 constraintRect
.right
= 32000 ;
1705 constraintRect
.bottom
= win
->GetMaxHeight() ;
1706 if ( constraintRect
.bottom
== -1 )
1707 constraintRect
.bottom
= 32000 ;
1709 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1710 &constraintRect
, &newContentRect
) ;
1713 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1714 newContentRect
.right
- newContentRect
.left
,
1715 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1717 s_lastMouseDown
= 0;
1722 if (TrackBox(window
, ev
->where
, windowPart
))
1724 // TODO setup size event
1725 ZoomWindow( window
, windowPart
, false ) ;
1731 Point pt
= { 0, 0 } ;
1732 SetPortWindowPort(window
) ;
1733 LocalToGlobal( &pt
) ;
1736 GetWindowPortBounds(window
, &tempRect
) ;
1737 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1738 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1741 s_lastMouseDown
= 0;
1743 case inCollapseBox
:
1744 // TODO setup size event
1745 s_lastMouseDown
= 0;
1752 SetPortWindowPort(window
) ;
1755 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1757 if ( s_macIsInModalLoop
)
1761 else if ( UMAIsWindowFloating( window
) )
1764 win
->MacMouseDown( ev
, windowPart
) ;
1769 win
->MacMouseDown( ev
, windowPart
) ;
1770 ::SelectWindow( window
) ;
1776 win
->MacMouseDown( ev
, windowPart
) ;
1784 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1786 EventRecord
* ev
= (EventRecord
*) evr
;
1789 short windowPart
= inNoWindow
;
1790 if ( wxTheApp
->s_captureWindow
)
1792 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1793 windowPart
= inContent
;
1797 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1808 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1810 win
->MacMouseUp( ev
, windowPart
) ;
1818 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1819 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1824 case kHomeCharCode
:
1827 case kEnterCharCode
:
1828 retval
= WXK_RETURN
;
1833 case kHelpCharCode
:
1836 case kBackspaceCharCode
:
1842 case kPageUpCharCode
:
1843 retval
= WXK_PAGEUP
;
1845 case kPageDownCharCode
:
1846 retval
= WXK_PAGEDOWN
;
1848 case kReturnCharCode
:
1849 retval
= WXK_RETURN
;
1851 case kFunctionKeyCharCode
:
1903 case kEscapeCharCode
:
1904 retval
= WXK_ESCAPE
;
1906 case kLeftArrowCharCode
:
1909 case kRightArrowCharCode
:
1910 retval
= WXK_RIGHT
;
1912 case kUpArrowCharCode
:
1915 case kDownArrowCharCode
:
1918 case kDeleteCharCode
:
1919 retval
= WXK_DELETE
;
1928 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1930 EventRecord
* ev
= (EventRecord
*) evr
;
1931 wxToolTip::RemoveToolTips() ;
1933 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1934 if ( HiWord( menuresult
) )
1936 if ( !s_macIsInModalLoop
)
1937 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1941 wxWindow
* focus
= wxWindow::FindFocus() ;
1943 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1945 // has not been handled -> perform default
1946 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1947 if ( control
&& control
->GetMacControl() != NULL
)
1951 keychar
= short(ev
->message
& charCodeMask
);
1952 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1953 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1959 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1961 EventRecord
* ev
= (EventRecord
*) evr
;
1962 wxToolTip::RemoveToolTips() ;
1964 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1965 if ( HiWord( menuresult
) )
1970 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1976 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1983 keychar
= short(keymessage
& charCodeMask
);
1984 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1986 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1988 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1989 // and look at the character after
1991 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1992 keychar
= short(keyInfo
& charCodeMask
);
1993 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1995 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1996 long realkeyval
= keyval
;
1997 if ( keyval
== keychar
)
1999 // 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)
2000 realkeyval
= short(keymessage
& charCodeMask
) ;
2001 keyval
= wxToupper( keyval
) ;
2004 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2005 bool handled
= false ;
2006 event
.m_shiftDown
= modifiers
& shiftKey
;
2007 event
.m_controlDown
= modifiers
& controlKey
;
2008 event
.m_altDown
= modifiers
& optionKey
;
2009 event
.m_metaDown
= modifiers
& cmdKey
;
2010 event
.m_keyCode
= keyval
;
2014 event
.m_timeStamp
= when
;
2015 event
.SetEventObject(focus
);
2016 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2017 if ( handled
&& event
.GetSkipped() )
2024 wxWindow
*ancestor
= focus
;
2027 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2030 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2031 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2034 if (ancestor
->IsTopLevel())
2036 ancestor
= ancestor
->GetParent();
2039 #endif // wxUSE_ACCEL
2043 event
.Skip( FALSE
) ;
2044 event
.SetEventType( wxEVT_CHAR
) ;
2046 event
.m_keyCode
= realkeyval
;
2048 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2049 if ( handled
&& event
.GetSkipped() )
2053 (keyval
== WXK_TAB
) &&
2054 // CS: copied the change below from wxGTK
2055 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2056 // have this style, yet choose not to process this particular TAB in which
2057 // case TAB must still work as a navigational character
2059 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2061 (focus
->GetParent()) &&
2062 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2064 wxNavigationKeyEvent new_event
;
2065 new_event
.SetEventObject( focus
);
2066 new_event
.SetDirection( !event
.ShiftDown() );
2067 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2068 new_event
.SetWindowChange( event
.ControlDown() );
2069 new_event
.SetCurrentFocus( focus
);
2070 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2071 if ( handled
&& new_event
.GetSkipped() )
2074 // backdoor handler for default return and command escape
2075 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2077 // if window is not having a focus still testing for default enter or cancel
2078 // TODO add the UMA version for ActiveNonFloatingWindow
2079 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2082 if ( keyval
== WXK_RETURN
)
2084 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2086 if ( def
&& def
->IsEnabled() )
2088 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2089 event
.SetEventObject(def
);
2090 def
->Command(event
);
2094 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2095 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2097 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2098 new_event
.SetEventObject( focus
);
2099 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2106 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2113 keychar
= short(keymessage
& charCodeMask
);
2114 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2115 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2117 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2118 // and look at the character after
2120 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2121 keychar
= short(keyInfo
& charCodeMask
);
2122 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2124 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2126 if ( keyval
== keychar
)
2128 keyval
= wxToupper( keyval
) ;
2130 bool handled
= false ;
2132 wxKeyEvent
event(wxEVT_KEY_UP
);
2133 event
.m_shiftDown
= modifiers
& shiftKey
;
2134 event
.m_controlDown
= modifiers
& controlKey
;
2135 event
.m_altDown
= modifiers
& optionKey
;
2136 event
.m_metaDown
= modifiers
& cmdKey
;
2137 event
.m_keyCode
= keyval
;
2141 event
.m_timeStamp
= when
;
2142 event
.SetEventObject(focus
);
2143 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2149 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2151 EventRecord
* ev
= (EventRecord
*) evr
;
2152 WindowRef window
= (WindowRef
) ev
->message
;
2155 bool activate
= (ev
->modifiers
& activeFlag
) ;
2156 WindowClass wclass
;
2157 ::GetWindowClass ( window
, &wclass
) ;
2158 if ( wclass
== kFloatingWindowClass
)
2160 // if it is a floater we activate/deactivate the front non-floating window instead
2161 window
= ::FrontNonFloatingWindow() ;
2163 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2165 win
->MacActivate( ev
->when
, activate
) ;
2169 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2171 EventRecord
* ev
= (EventRecord
*) evr
;
2172 WindowRef window
= (WindowRef
) ev
->message
;
2173 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2176 if ( !wxPendingDelete
.Member(win
) )
2177 win
->MacUpdate( ev
->when
) ;
2181 // since there is no way of telling this foreign window to update itself
2182 // we have to invalidate the update region otherwise we keep getting the same
2183 // event over and over again
2184 BeginUpdate( window
) ;
2185 EndUpdate( window
) ;
2189 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2191 EventRecord
* ev
= (EventRecord
*) evr
;
2192 if ( HiWord( ev
->message
) != noErr
)
2196 SetPt( &point
, 100 , 100 ) ;
2198 err
= DIBadMount( point
, ev
->message
) ;
2199 wxASSERT( err
== noErr
) ;
2203 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2205 EventRecord
* ev
= (EventRecord
*) evr
;
2206 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2208 case suspendResumeMessage
:
2210 bool isResuming
= ev
->message
& resumeFlag
;
2211 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2213 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2216 WindowRef oldFrontWindow
= NULL
;
2217 WindowRef newFrontWindow
= NULL
;
2219 // in case we don't take care of activating ourselves, we have to synchronize
2220 // our idea of the active window with the process manager's - which it already activated
2222 if ( !doesActivate
)
2223 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2225 MacResume( convertClipboard
) ;
2227 newFrontWindow
= ::FrontNonFloatingWindow() ;
2229 if ( oldFrontWindow
)
2231 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2233 win
->MacActivate( ev
->when
, false ) ;
2235 if ( newFrontWindow
)
2237 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2239 win
->MacActivate( ev
->when
, true ) ;
2244 MacSuspend( convertClipboard
) ;
2248 case mouseMovedMessage
:
2252 wxWindow
* currentMouseWindow
= NULL
;
2254 if (s_captureWindow
)
2256 currentMouseWindow
= s_captureWindow
;
2260 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2261 ¤tMouseWindow
) ;
2264 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2266 wxMouseEvent event
;
2268 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2269 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2271 event
.m_leftDown
= isDown
&& !controlDown
;
2272 event
.m_middleDown
= FALSE
;
2273 event
.m_rightDown
= isDown
&& controlDown
;
2274 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2275 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2276 event
.m_altDown
= ev
->modifiers
& optionKey
;
2277 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2278 event
.m_x
= ev
->where
.h
;
2279 event
.m_y
= ev
->where
.v
;
2280 event
.m_timeStamp
= ev
->when
;
2281 event
.SetEventObject(this);
2283 if ( wxWindow::s_lastMouseWindow
)
2285 wxMouseEvent
eventleave(event
);
2286 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2287 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2288 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2290 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2292 if ( currentMouseWindow
)
2294 wxMouseEvent
evententer(event
);
2295 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2296 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2297 evententer
.SetEventObject( currentMouseWindow
) ;
2298 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2300 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2303 short windowPart
= inNoWindow
;
2305 if ( s_captureWindow
)
2307 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2308 windowPart
= inContent
;
2312 windowPart
= ::FindWindow(ev
->where
, &window
);
2319 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2321 win
->MacMouseMoved( ev
, windowPart
) ;
2328 UMAShowArrowCursor();
2338 UMAShowArrowCursor();
2349 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2351 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2352 wxFrame
* frame
= mbar
->GetFrame();
2353 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2356 frame
->ProcessCommand(id
);
2361 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2364 return; // no menu item selected
2366 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2369 Str255 deskAccessoryName
;
2372 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2373 GetPort(&savedPort
);
2374 OpenDeskAcc(deskAccessoryName
);
2381 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2382 MacHandleMenuCommand( id
) ;