1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
41 #if wxUSE_WX_RESOURCES
42 # include "wx/resource.h"
57 #include "wx/mac/uma.h"
58 #include "wx/mac/macnotfy.h"
61 # include <CoreServices/CoreServices.h>
62 # if defined(WXMAKINGDLL)
63 # include <mach-o/dyld.h>
68 # include <ToolUtils.h>
69 # include <DiskInit.h>
73 extern wxList wxPendingDelete
;
74 extern wxList
*wxWinMacWindowList
;
75 extern wxList
*wxWinMacControlList
;
77 // statics for implementation
79 static bool s_inYield
= FALSE
;
80 static bool s_inOnIdle
= FALSE
;
83 static bool s_inReceiveEvent
= FALSE
;
84 static EventTime sleepTime
= kEventDurationNoWait
;
86 static long sleepTime
= 0 ;
89 wxApp
*wxTheApp
= NULL
;
91 #if !USE_SHARED_LIBRARY
92 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
93 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
94 EVT_IDLE(wxApp::OnIdle
)
95 EVT_END_SESSION(wxApp::OnEndSession
)
96 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
101 const short kMacMinHeap
= (29 * 1024) ;
102 // platform specific static variables
104 const short kwxMacMenuBarResource
= 1 ;
105 const short kwxMacAppleMenuId
= 1 ;
107 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
108 wxWindow
* wxApp::s_captureWindow
= NULL
;
109 int wxApp::s_lastMouseDown
= 0 ;
110 long wxApp::sm_lastMessageTime
= 0;
111 long wxApp::s_lastModifiers
= 0 ;
114 bool wxApp::s_macDefaultEncodingIsPC
= true ;
115 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
116 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
117 long wxApp::s_macPreferencesMenuItemId
= 0 ;
118 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
119 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
121 //----------------------------------------------------------------------
122 // Core Apple Event Support
123 //----------------------------------------------------------------------
125 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
150 // AEODoc Calls MacOpenFile on each of the files passed
152 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
156 DescType returnedType
;
162 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
166 err
= AECountItems(&docList
, &itemsInList
);
170 ProcessSerialNumber PSN
;
171 PSN
.highLongOfPSN
= 0 ;
172 PSN
.lowLongOfPSN
= kCurrentProcess
;
173 SetFrontProcess( &PSN
) ;
175 for (i
= 1; i
<= itemsInList
; i
++) {
176 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
177 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
178 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
184 // AEPDoc Calls MacPrintFile on each of the files passed
186 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
190 DescType returnedType
;
196 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
200 err
= AECountItems(&docList
, &itemsInList
);
204 ProcessSerialNumber PSN
;
205 PSN
.highLongOfPSN
= 0 ;
206 PSN
.lowLongOfPSN
= kCurrentProcess
;
207 SetFrontProcess( &PSN
) ;
209 for (i
= 1; i
<= itemsInList
; i
++) {
210 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
211 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
212 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
218 // AEOApp calls MacNewFile
220 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
226 // AEQuit attempts to quite the application
228 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
230 /* wxWindow* win = GetTopWindow() ;
243 //----------------------------------------------------------------------
244 // Support Routines linking the Mac...File Calls to the Document Manager
245 //----------------------------------------------------------------------
247 void wxApp::MacOpenFile(const wxString
& fileName
)
249 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
251 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
254 void wxApp::MacPrintFile(const wxString
& fileName
)
256 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
259 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
262 wxView
* view
= doc
->GetFirstView() ;
265 wxPrintout
*printout
= view
->OnCreatePrintout();
269 printer
.Print(view
->GetFrame(), printout
, TRUE
);
275 doc
->DeleteAllViews();
276 dm
->RemoveDocument(doc
) ;
282 void wxApp::MacNewFile()
286 //----------------------------------------------------------------------
287 // Carbon Event Handler
288 //----------------------------------------------------------------------
292 static const EventTypeSpec eventList
[] =
294 { kEventClassCommand
, kEventProcessCommand
} ,
295 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
297 { kEventClassApplication
, kEventAppActivated
} ,
298 { kEventClassApplication
, kEventAppDeactivated
} ,
299 // handling the quit event is not recommended by apple
300 // rather using the quit apple event - which we do
302 { kEventClassAppleEvent
, kEventAppleEvent
} ,
304 { kEventClassMouse
, kEventMouseDown
} ,
308 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
310 OSStatus result
= eventNotHandledErr
;
311 UInt32 kind
= GetEventKind( event
) ;
316 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
317 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
318 // deal with clicks in the menu bar explicitely
320 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
322 OSStatus result
= eventNotHandledErr
;
324 switch( GetEventKind(event
) )
326 case kEventMouseDown
:
331 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
332 sizeof( Point
), NULL
, &point
);
333 short windowPart
= ::FindWindow(point
, &window
);
335 if ( windowPart
== inMenuBar
)
337 MenuSelect( point
) ;
347 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
349 OSStatus result
= eventNotHandledErr
;
353 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
354 sizeof( HICommand
), NULL
, &command
);
356 MenuCommand id
= command
.commandID
;
357 if ( id
== kHICommandPreferences
)
358 id
= wxApp::s_macPreferencesMenuItemId
;
360 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
361 wxMenu
* menu
= NULL
;
362 wxMenuItem
* item
= NULL
;
365 item
= mbar
->FindItem( id
, &menu
) ;
367 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
370 switch( GetEventKind( event
) )
372 case kEventProcessCommand
:
374 if (item
->IsCheckable())
376 item
->Check( !item
->IsChecked() ) ;
379 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
383 case kEventCommandUpdateStatus
:
384 // eventually trigger an updateui round
394 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
396 OSStatus result
= eventNotHandledErr
;
397 switch ( GetEventKind( event
) )
399 case kEventAppActivated
:
401 wxTheApp
->MacResume( true ) ;
405 case kEventAppDeactivated
:
407 wxTheApp
->MacSuspend( true ) ;
417 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
419 OSStatus result
= eventNotHandledErr
;
420 switch( GetEventClass( event
) )
422 case kEventClassCommand
:
423 result
= CommandEventHandler( handler
, event
, data
) ;
425 case kEventClassApplication
:
426 result
= ApplicationEventHandler( handler
, event
, data
) ;
428 case kEventClassMenu
:
429 result
= MenuEventHandler( handler
, event
, data
) ;
431 case kEventClassMouse
:
432 result
= MouseEventHandler( handler
, event
, data
) ;
434 case kEventClassAppleEvent
:
437 wxMacConvertEventToRecord( event
, &rec
) ;
438 result
= AEProcessAppleEvent( &rec
) ;
448 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
452 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
453 // we know it's there ;-)
454 WXIMPORT
char std::__throws_bad_alloc
;
457 bool wxApp::Initialize()
463 UMAInitToolbox( 4 ) ;
464 SetEventMask( everyEvent
) ;
465 UMAShowWatchCursor() ;
467 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
468 // open shared library resources from here since we don't have
469 // __wxinitialize in Mach-O shared libraries
470 wxStAppResource::OpenSharedLibraryResource(NULL
);
474 // test the minimal configuration necessary
480 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
482 error
= kMacSTRWrongMachine
;
484 else if (theMachine
< gestaltMacPlus
)
486 error
= kMacSTRWrongMachine
;
488 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
490 error
= kMacSTROldSystem
;
492 else if ( theSystem
< 0x0860 )
494 error
= kMacSTROldSystem
;
496 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
498 error
= kMacSTRSmallSize
;
504 if ( !UMAHasAppearance() )
506 error = kMacSTRNoPre8Yet ;
512 // if we encountered any problems so far, give the error code and exit immediately
516 wxStAppResource resload
;
520 GetIndString(message
, 128, error
);
521 UMAShowArrowCursor() ;
522 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
523 itemHit
= Alert(128, nil
);
528 # if __option(profile)
529 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
534 // now avoid exceptions thrown for new (bad_alloc)
535 // FIXME CS for some changes outside wxMac does not compile anymore
537 std::__throws_bad_alloc
= 0 ;
542 s_macCursorRgn
= ::NewRgn() ;
544 wxClassInfo::InitializeClasses();
547 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
551 wxPendingEventsLocker
= new wxCriticalSection
;
554 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
555 wxTheColourDatabase
->Initialize();
559 // flush the logged messages if any and install a 'safer' log target: the
560 // default one (wxLogGui) can't be used after the resources are freed just
561 // below and the user suppliedo ne might be even more unsafe (using any
562 // wxWindows GUI function is unsafe starting from now)
563 wxLog::DontCreateOnDemand();
565 // this will flush the old messages if any
566 delete wxLog::SetActiveTarget(new wxLogStderr
);
570 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
571 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
573 wxInitializeStockLists();
574 wxInitializeStockObjects();
576 #if wxUSE_WX_RESOURCES
577 wxInitializeResourceSystem();
580 wxBitmap::InitStandardHandlers();
582 wxModule::RegisterModules();
583 if (!wxModule::InitializeModules()) {
587 wxMacCreateNotifierTable() ;
589 UMAShowArrowCursor() ;
594 bool wxApp::OnInitGui()
596 if( !wxAppBase::OnInitGui() )
600 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
602 InstallApplicationEventHandler(
603 GetwxAppEventHandlerUPP(),
604 GetEventTypeCount(eventList
), eventList
, wxTheApp
, &((EventHandlerRef
)wxTheApp
->m_macEventHandler
));
607 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
608 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
609 NewAEEventHandlerUPP(AEHandleODoc
) ,
611 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
612 NewAEEventHandlerUPP(AEHandleOApp
) ,
614 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
615 NewAEEventHandlerUPP(AEHandlePDoc
) ,
617 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
618 NewAEEventHandlerUPP(AEHandleQuit
) ,
621 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
622 NewAEEventHandlerProc(AEHandleODoc
) ,
624 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
625 NewAEEventHandlerProc(AEHandleOApp
) ,
627 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
628 NewAEEventHandlerProc(AEHandlePDoc
) ,
630 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
631 NewAEEventHandlerProc(AEHandleQuit
) ,
638 void wxApp::CleanUp()
640 wxToolTip::RemoveToolTips() ;
642 // flush the logged messages if any and install a 'safer' log target: the
643 // default one (wxLogGui) can't be used after the resources are freed just
644 // below and the user suppliedo ne might be even more unsafe (using any
645 // wxWindows GUI function is unsafe starting from now)
646 wxLog::DontCreateOnDemand();
648 // this will flush the old messages if any
649 delete wxLog::SetActiveTarget(new wxLogStderr
);
652 // One last chance for pending objects to be cleaned up
653 wxTheApp
->DeletePendingObjects();
655 wxModule::CleanUpModules();
657 #if wxUSE_WX_RESOURCES
658 wxCleanUpResourceSystem();
661 wxDeleteStockObjects() ;
663 // Destroy all GDI lists, etc.
664 wxDeleteStockLists();
666 delete wxTheColourDatabase
;
667 wxTheColourDatabase
= NULL
;
669 wxBitmap::CleanUpHandlers();
671 wxMacDestroyNotifierTable() ;
672 if (wxWinMacWindowList
) {
673 delete wxWinMacWindowList
;
675 if (wxWinMacControlList
) {
676 delete wxWinMacControlList
;
678 delete wxPendingEvents
;
681 delete wxPendingEventsLocker
;
682 // If we don't do the following, we get an apparent memory leak.
683 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
686 wxClassInfo::CleanUpClasses();
689 # if __option(profile)
690 ProfilerDump( "\papp.prof" ) ;
698 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
699 // At this point we want to check if there are any memory
700 // blocks that aren't part of the wxDebugContext itself,
701 // as a special case. Then when dumping we need to ignore
702 // wxDebugContext, too.
703 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
705 wxLogDebug(wxT("There were memory leaks."));
706 wxDebugContext::Dump();
707 wxDebugContext::PrintStatistics();
709 // wxDebugContext::SetStream(NULL, NULL);
713 // do it as the very last thing because everything else can log messages
714 delete wxLog::SetActiveTarget(NULL
);
717 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
718 // close shared library resources from here since we don't have
719 // __wxterminate in Mach-O shared libraries
720 wxStAppResource::CloseSharedLibraryResource();
723 UMACleanupToolbox() ;
724 if (s_macCursorRgn
) {
725 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
733 //----------------------------------------------------------------------
735 //----------------------------------------------------------------------
737 // extern variable for shared library resource id
738 // need to be able to find it with NSLookupAndBindSymbol
739 short gSharedLibraryResource
= kResFileNotOpened
;
741 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
742 CFBundleRef gSharedLibraryBundle
= NULL
;
743 #endif /* WXMAKINGDLL && __DARWIN__ */
745 wxStAppResource::wxStAppResource()
747 m_currentRefNum
= CurResFile() ;
748 if ( gSharedLibraryResource
!= kResFileNotOpened
)
750 UseResFile( gSharedLibraryResource
) ;
754 wxStAppResource::~wxStAppResource()
756 if ( m_currentRefNum
!= kResFileNotOpened
)
758 UseResFile( m_currentRefNum
) ;
762 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
764 gSharedLibraryResource
= kResFileNotOpened
;
767 if ( initBlock
!= NULL
) {
768 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
769 FSSpec
*fileSpec
= NULL
;
771 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
772 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
774 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
775 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
778 if (fileSpec
!= NULL
) {
779 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
784 // Open the shared library resource file if it is not yet open
787 const char *theLibPath
;
789 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
790 if (gSharedLibraryBundle
!= NULL
) {
791 // wxWindows has been bundled into a framework
792 // load the framework resources
794 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
797 // wxWindows is a simple dynamic shared library
798 // load the resources from the data fork of a separate resource file
803 OSErr theErr
= noErr
;
805 // get the library path
806 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
807 theModule
= NSModuleForSymbol(theSymbol
);
808 theLibPath
= NSLibraryNameForModule(theModule
);
810 wxLogDebug( wxT("wxMac library installation name is '%s'"),
813 // allocate copy to replace .dylib.* extension with .rsrc
814 theResPath
= strdup(theLibPath
);
815 if (theResPath
!= NULL
) {
816 theName
= strrchr(theResPath
, '/');
817 if (theName
== NULL
) {
818 // no directory elements in path
819 theName
= theResPath
;
821 // find ".dylib" shared library extension
822 theExt
= strstr(theName
, ".dylib");
823 // overwrite extension with ".rsrc"
824 strcpy(theExt
, ".rsrc");
826 wxLogDebug( wxT("wxMac resources file name is '%s'"),
829 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
830 if (theErr
!= noErr
) {
831 // try in current directory (using name only)
832 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
835 // open the resource file
836 if (theErr
== noErr
) {
837 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
838 &gSharedLibraryResource
);
840 if (theErr
!= noErr
) {
841 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
845 // free duplicated resource file path
849 #endif /* __DARWIN__ */
851 #endif /* WXMAKINGDLL */
854 void wxStAppResource::CloseSharedLibraryResource()
857 // Close the shared library resource file
858 if (gSharedLibraryResource
!= kResFileNotOpened
) {
860 if (gSharedLibraryBundle
!= NULL
) {
861 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
862 gSharedLibraryResource
);
863 gSharedLibraryBundle
= NULL
;
866 #endif /* __DARWIN__ */
868 CloseResFile(gSharedLibraryResource
);
870 gSharedLibraryResource
= kResFileNotOpened
;
872 #endif /* WXMAKINGDLL */
875 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
877 // for shared libraries we have to manually get the correct resource
878 // ref num upon initializing and releasing when terminating, therefore
879 // the __wxinitialize and __wxterminate must be used
882 void __sinit(void); /* (generated by linker) */
883 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
884 pascal void __terminate(void);
887 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
889 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
890 return __initialize( theInitBlock
) ;
893 pascal void __wxterminate(void)
895 wxStAppResource::CloseSharedLibraryResource() ;
899 #endif /* WXMAKINGDLL && !__DARWIN__ */
901 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
903 return wxApp::Initialize();
906 int WXDLLEXPORT
wxEntryInitGui()
908 return wxTheApp
->OnInitGui();
911 void WXDLLEXPORT
wxEntryCleanup()
916 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
919 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
920 // This seems to be necessary since there are 'rogue'
921 // objects present at this point (perhaps global objects?)
922 // Setting a checkpoint will ignore them as far as the
923 // memory checking facility is concerned.
924 // Of course you may argue that memory allocated in globals should be
925 // checked, but this is a reasonable compromise.
926 wxDebugContext::SetCheckpoint();
929 if (!wxEntryStart(argc
, argv
)) {
932 // create the application object or ensure that one already exists
935 // The app may have declared a global application object, but we recommend
936 // the IMPLEMENT_APP macro is used instead, which sets an initializer
937 // function for delayed, dynamic app object construction.
938 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
939 wxT("No initializer - use IMPLEMENT_APP macro.") );
941 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
944 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
947 // Mac OS X passes a process serial number command line argument when
948 // the application is launched from the Finder. This argument must be
949 // removed from the command line arguments before being handled by the
950 // application (otherwise applications would need to handle it)
953 if (strncmp(argv
[1], "-psn_", 5) == 0) {
954 // assume the argument is always the only one and remove it
959 argc
= 0 ; // currently we don't support files as parameters
961 // we could try to get the open apple events here to adjust argc and argv better
963 wxTheApp
->argc
= argc
;
964 wxTheApp
->argv
= argv
;
966 // GUI-specific initialization, such as creating an app context.
969 // Here frames insert themselves automatically
970 // into wxTopLevelWindows by getting created
975 if ( wxTheApp
->OnInit() )
979 retValue
= wxTheApp
->OnRun();
982 // We want to initialize, but not run or exit immediately.
985 //else: app initialization failed, so we skipped OnRun()
987 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
990 // Forcibly delete the window.
991 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
992 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
994 topWindow
->Close(TRUE
);
995 wxTheApp
->DeletePendingObjects();
1000 wxTheApp
->SetTopWindow(NULL
);
1013 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1015 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
1016 OSStatus err
= noErr
;
1019 switch( GetEventClass( event
) )
1021 case kEventClassKeyboard
:
1024 switch( GetEventKind(event
) )
1026 case kEventRawKeyDown
:
1027 rec
->what
= keyDown
;
1029 case kEventRawKeyRepeat
:
1030 rec
->what
= autoKey
;
1032 case kEventRawKeyUp
:
1035 case kEventRawKeyModifiersChanged
:
1036 rec
->what
= nullEvent
;
1045 unsigned char charCode
;
1047 GetMouse( &rec
->where
) ;
1049 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1050 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1051 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1052 rec
->modifiers
= modifiers
;
1053 rec
->message
= (keyCode
<< 8 ) + charCode
;
1057 case kEventClassTextInput
:
1059 switch( GetEventKind( event
) )
1061 case kEventTextInputUnicodeForKeyEvent
:
1064 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1068 unsigned char charCode
;
1070 GetMouse( &rec
->where
) ;
1071 rec
->what
= keyDown
;
1072 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1073 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1074 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1075 rec
->modifiers
= modifiers
;
1076 rec
->message
= (keyCode
<< 8 ) + charCode
;
1092 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1094 OSStatus result = eventNotHandledErr ;
1097 switch ( GetEventClass( event ) )
1099 case kEventClassKeyboard :
1100 if ( wxMacConvertEventToRecord( event , &rec ) )
1102 wxTheApp->MacHandleModifierEvents( &rec ) ;
1103 wxTheApp->MacHandleOneEvent( &rec ) ;
1107 case kEventClassTextInput :
1108 if ( wxMacConvertEventToRecord( event , &rec ) )
1110 wxTheApp->MacHandleModifierEvents( &rec ) ;
1111 wxTheApp->MacHandleOneEvent( &rec ) ;
1123 // Static member initialization
1124 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
1131 #if WXWIN_COMPATIBILITY_2_2
1132 m_wantDebugOutput
= TRUE
;
1138 m_printMode
= wxPRINT_WINDOWS
;
1142 bool wxApp::Initialized()
1150 int wxApp::MainLoop()
1162 // Returns TRUE if more time is needed.
1163 bool wxApp::ProcessIdle()
1166 event
.SetEventObject(this);
1167 ProcessEvent(event
);
1169 return event
.MoreRequested();
1172 void wxApp::ExitMainLoop()
1174 m_keepGoing
= FALSE
;
1177 // Is a message/event pending?
1178 bool wxApp::Pending()
1181 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1185 return EventAvail( everyEvent
, &event
) ;
1189 // Dispatch a message.
1190 void wxApp::Dispatch()
1195 void wxApp::OnIdle(wxIdleEvent
& event
)
1197 // Avoid recursion (via ProcessEvent default case)
1204 // 'Garbage' collection of windows deleted with Close().
1205 DeletePendingObjects();
1207 // flush the logged messages if any
1208 wxLog
*pLog
= wxLog::GetActiveTarget();
1209 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1212 // Send OnIdle events to all windows
1213 bool needMore
= SendIdleEvents();
1216 event
.RequestMore(TRUE
);
1218 // If they are pending events, we must process them: pending events are
1219 // either events to the threads other than main or events posted with
1220 // wxPostEvent() functions
1221 wxMacProcessNotifierAndPendingEvents();
1231 // Send idle event to all top-level windows
1232 bool wxApp::SendIdleEvents()
1234 bool needMore
= FALSE
;
1235 wxNode
* node
= wxTopLevelWindows
.First();
1238 wxWindow
* win
= (wxWindow
*) node
->Data();
1239 if (SendIdleEvents(win
))
1242 node
= node
->Next();
1247 // Send idle event to window and all subwindows
1248 bool wxApp::SendIdleEvents(wxWindow
* win
)
1250 bool needMore
= FALSE
;
1253 event
.SetEventObject(win
);
1254 win
->ProcessEvent(event
);
1256 if (event
.MoreRequested())
1259 wxNode
* node
= win
->GetChildren().First();
1262 wxWindow
* win
= (wxWindow
*) node
->Data();
1263 if (SendIdleEvents(win
))
1266 node
= node
->Next();
1271 void wxApp::DeletePendingObjects()
1273 wxNode
*node
= wxPendingDelete
.First();
1276 wxObject
*obj
= (wxObject
*)node
->Data();
1280 if (wxPendingDelete
.Member(obj
))
1283 // Deleting one object may have deleted other pending
1284 // objects, so start from beginning of list again.
1285 node
= wxPendingDelete
.First();
1291 wxLogError(_("Fatal error: exiting"));
1297 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1300 GetTopWindow()->Close(TRUE
);
1303 // Default behaviour: close the application with prompts. The
1304 // user can veto the close, and therefore the end session.
1305 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1309 if (!GetTopWindow()->Close(!event
.CanVeto()))
1314 extern "C" void wxCYield() ;
1320 // Yield to other processes
1322 bool wxApp::Yield(bool onlyIfNeeded
)
1326 if ( !onlyIfNeeded
)
1328 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1337 YieldToAnyThread() ;
1339 // by definition yield should handle all non-processed events
1343 OSStatus status
= noErr
;
1346 s_inReceiveEvent
= true ;
1347 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1348 s_inReceiveEvent
= false ;
1350 if ( status
== eventLoopTimedOutErr
)
1352 // make sure next time the event loop will trigger idle events
1353 sleepTime
= kEventDurationNoWait
;
1355 else if ( status
== eventLoopQuitErr
)
1357 // according to QA1061 this may also occur when a WakeUp Process
1362 MacHandleOneEvent( theEvent
) ;
1363 ReleaseEvent(theEvent
);
1365 } while( status
== noErr
) ;
1369 // having a larger value here leads to large performance slowdowns
1370 // so we cannot give background apps more processor time here
1371 // we do so however having a large sleep value in the main event loop
1374 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1376 MacHandleModifierEvents( &event
) ;
1377 MacHandleOneEvent( &event
);
1378 if ( event
.what
!= kHighLevelEvent
)
1379 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1381 MacHandleModifierEvents( &event
) ;
1384 wxMacProcessNotifierAndPendingEvents() ;
1390 // platform specifics
1392 void wxApp::MacSuspend( bool convertClipboard
)
1395 // we have to deactive the top level windows manually
1397 wxNode
* node
= wxTopLevelWindows
.First();
1400 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1401 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1403 node
= node
->Next();
1406 ::HideFloatingWindows() ;
1408 s_lastMouseDown
= 0 ;
1410 if( convertClipboard
)
1412 MacConvertPrivateToPublicScrap() ;
1416 extern wxList wxModalDialogs
;
1418 void wxApp::MacResume( bool convertClipboard
)
1420 s_lastMouseDown
= 0 ;
1421 if( convertClipboard
)
1423 MacConvertPublicToPrivateScrap() ;
1427 ::ShowFloatingWindows() ;
1428 // raise modal dialogs in case a non modal window was selected to activate the app
1430 wxNode
* node
= wxModalDialogs
.First();
1433 wxDialog
* dialog
= (wxDialog
*) node
->Data();
1436 node
= node
->Next();
1441 void wxApp::MacConvertPrivateToPublicScrap()
1445 void wxApp::MacConvertPublicToPrivateScrap()
1449 void wxApp::MacDoOneEvent()
1454 s_inReceiveEvent
= true ;
1455 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1456 s_inReceiveEvent
= false ;
1457 if ( status
== eventLoopTimedOutErr
)
1459 if ( wxTheApp
->ProcessIdle() )
1460 sleepTime
= kEventDurationNoWait
;
1462 sleepTime
= kEventDurationForever
;
1464 else if ( status
== eventLoopQuitErr
)
1466 // according to QA1061 this may also occur when a WakeUp Process
1471 MacHandleOneEvent( theEvent
) ;
1472 ReleaseEvent(theEvent
);
1477 EventMask eventMask
= everyEvent
;
1479 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1481 MacHandleModifierEvents( &event
) ;
1482 MacHandleOneEvent( &event
);
1486 MacHandleModifierEvents( &event
) ;
1488 WindowPtr window
= ::FrontWindow() ;
1490 ::IdleControls( window
) ;
1492 if ( wxTheApp
->ProcessIdle() )
1495 sleepTime
= GetCaretTime() / 2 ;
1497 if ( event
.what
!= kHighLevelEvent
)
1498 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1502 DeletePendingObjects() ;
1503 wxMacProcessNotifierAndPendingEvents() ;
1506 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1509 EventTargetRef theTarget
;
1510 theTarget
= GetEventDispatcherTarget();
1511 m_macCurrentEvent
= evr
;
1512 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1514 EventRecord
* ev
= (EventRecord
*) evr
;
1515 m_macCurrentEvent
= ev
;
1517 wxApp::sm_lastMessageTime
= ev
->when
;
1522 MacHandleMouseDownEvent( ev
) ;
1523 if ( ev
->modifiers
& controlKey
)
1524 s_lastMouseDown
= 2;
1526 s_lastMouseDown
= 1;
1529 if ( s_lastMouseDown
== 2 )
1531 ev
->modifiers
|= controlKey
;
1535 ev
->modifiers
&= ~controlKey
;
1537 MacHandleMouseUpEvent( ev
) ;
1538 s_lastMouseDown
= 0;
1541 MacHandleActivateEvent( ev
) ;
1544 MacHandleUpdateEvent( ev
) ;
1548 MacHandleKeyDownEvent( ev
) ;
1551 MacHandleKeyUpEvent( ev
) ;
1554 MacHandleDiskEvent( ev
) ;
1557 MacHandleOSEvent( ev
) ;
1559 case kHighLevelEvent
:
1560 MacHandleHighLevelEvent( ev
) ;
1566 wxMacProcessNotifierAndPendingEvents() ;
1570 bool s_macIsInModalLoop
= false ;
1572 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1574 EventRecord
* ev
= (EventRecord
*) evr
;
1575 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1577 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1579 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1580 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1581 event
.m_altDown
= ev
->modifiers
& optionKey
;
1582 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1584 event
.m_x
= ev
->where
.h
;
1585 event
.m_y
= ev
->where
.v
;
1586 event
.m_timeStamp
= ev
->when
;
1587 wxWindow
* focus
= wxWindow::FindFocus() ;
1588 event
.SetEventObject(focus
);
1590 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1592 event
.m_keyCode
= WXK_CONTROL
;
1593 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1594 focus
->GetEventHandler()->ProcessEvent( event
) ;
1596 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1598 event
.m_keyCode
= WXK_SHIFT
;
1599 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1600 focus
->GetEventHandler()->ProcessEvent( event
) ;
1602 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1604 event
.m_keyCode
= WXK_ALT
;
1605 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1606 focus
->GetEventHandler()->ProcessEvent( event
) ;
1608 s_lastModifiers
= ev
->modifiers
;
1612 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1614 // we must avoid reentrancy problems when processing high level events eg printing
1615 bool former
= s_inYield
;
1617 EventRecord
* ev
= (EventRecord
*) evr
;
1618 ::AEProcessAppleEvent( ev
) ;
1619 s_inYield
= former
;
1622 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1624 EventRecord
* ev
= (EventRecord
*) evr
;
1625 wxToolTip::RemoveToolTips() ;
1628 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1629 WindowAttributes frontWindowAttributes
= NULL
;
1631 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1633 short windowPart
= ::FindWindow(ev
->where
, &window
);
1634 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1635 if ( wxPendingDelete
.Member(win
) )
1639 GetQDGlobalsScreenBits( &screenBits
);
1644 if ( s_macIsInModalLoop
)
1650 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1651 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1652 s_lastMouseDown
= 0;
1656 SystemClick( ev
, window
) ;
1657 s_lastMouseDown
= 0;
1660 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1666 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1671 Point pt
= { 0, 0 } ;
1672 SetPortWindowPort(window
) ;
1673 LocalToGlobal( &pt
) ;
1675 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1676 -1 , wxSIZE_USE_EXISTING
);
1678 s_lastMouseDown
= 0;
1682 if (TrackGoAway(window
, ev
->where
))
1687 s_lastMouseDown
= 0;
1691 Rect newContentRect
;
1692 Rect constraintRect
;
1693 constraintRect
.top
= win
->GetMinHeight() ;
1694 if ( constraintRect
.top
== -1 )
1695 constraintRect
.top
= 0 ;
1696 constraintRect
.left
= win
->GetMinWidth() ;
1697 if ( constraintRect
.left
== -1 )
1698 constraintRect
.left
= 0 ;
1699 constraintRect
.right
= win
->GetMaxWidth() ;
1700 if ( constraintRect
.right
== -1 )
1701 constraintRect
.right
= 32000 ;
1702 constraintRect
.bottom
= win
->GetMaxHeight() ;
1703 if ( constraintRect
.bottom
== -1 )
1704 constraintRect
.bottom
= 32000 ;
1706 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1707 &constraintRect
, &newContentRect
) ;
1710 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1711 newContentRect
.right
- newContentRect
.left
,
1712 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1714 s_lastMouseDown
= 0;
1719 if (TrackBox(window
, ev
->where
, windowPart
))
1721 // TODO setup size event
1722 ZoomWindow( window
, windowPart
, false ) ;
1728 Point pt
= { 0, 0 } ;
1729 SetPortWindowPort(window
) ;
1730 LocalToGlobal( &pt
) ;
1733 GetWindowPortBounds(window
, &tempRect
) ;
1734 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1735 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1738 s_lastMouseDown
= 0;
1740 case inCollapseBox
:
1741 // TODO setup size event
1742 s_lastMouseDown
= 0;
1749 SetPortWindowPort(window
) ;
1752 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1754 if ( s_macIsInModalLoop
)
1758 else if ( UMAIsWindowFloating( window
) )
1761 win
->MacMouseDown( ev
, windowPart
) ;
1766 win
->MacMouseDown( ev
, windowPart
) ;
1767 ::SelectWindow( window
) ;
1773 win
->MacMouseDown( ev
, windowPart
) ;
1781 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1783 EventRecord
* ev
= (EventRecord
*) evr
;
1786 short windowPart
= inNoWindow
;
1787 if ( wxTheApp
->s_captureWindow
)
1789 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1790 windowPart
= inContent
;
1794 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1805 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1807 win
->MacMouseUp( ev
, windowPart
) ;
1815 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1816 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1821 case kHomeCharCode
:
1824 case kEnterCharCode
:
1825 retval
= WXK_RETURN
;
1830 case kHelpCharCode
:
1833 case kBackspaceCharCode
:
1839 case kPageUpCharCode
:
1840 retval
= WXK_PAGEUP
;
1842 case kPageDownCharCode
:
1843 retval
= WXK_PAGEDOWN
;
1845 case kReturnCharCode
:
1846 retval
= WXK_RETURN
;
1848 case kFunctionKeyCharCode
:
1900 case kEscapeCharCode
:
1901 retval
= WXK_ESCAPE
;
1903 case kLeftArrowCharCode
:
1906 case kRightArrowCharCode
:
1907 retval
= WXK_RIGHT
;
1909 case kUpArrowCharCode
:
1912 case kDownArrowCharCode
:
1915 case kDeleteCharCode
:
1916 retval
= WXK_DELETE
;
1925 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1927 EventRecord
* ev
= (EventRecord
*) evr
;
1928 wxToolTip::RemoveToolTips() ;
1930 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1931 if ( HiWord( menuresult
) )
1933 if ( !s_macIsInModalLoop
)
1934 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1938 wxWindow
* focus
= wxWindow::FindFocus() ;
1940 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1942 // has not been handled -> perform default
1943 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1944 if ( control
&& control
->GetMacControl() != NULL
)
1948 keychar
= short(ev
->message
& charCodeMask
);
1949 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1950 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1956 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1958 EventRecord
* ev
= (EventRecord
*) evr
;
1959 wxToolTip::RemoveToolTips() ;
1961 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1962 if ( HiWord( menuresult
) )
1967 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1973 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1980 keychar
= short(keymessage
& charCodeMask
);
1981 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1983 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1985 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1986 // and look at the character after
1988 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1989 keychar
= short(keyInfo
& charCodeMask
);
1990 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1992 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1993 long realkeyval
= keyval
;
1994 if ( keyval
== keychar
)
1996 // 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)
1997 realkeyval
= short(keymessage
& charCodeMask
) ;
1998 keyval
= wxToupper( keyval
) ;
2001 wxKeyEvent
event(wxEVT_KEY_DOWN
);
2002 bool handled
= false ;
2003 event
.m_shiftDown
= modifiers
& shiftKey
;
2004 event
.m_controlDown
= modifiers
& controlKey
;
2005 event
.m_altDown
= modifiers
& optionKey
;
2006 event
.m_metaDown
= modifiers
& cmdKey
;
2007 event
.m_keyCode
= keyval
;
2011 event
.m_timeStamp
= when
;
2012 event
.SetEventObject(focus
);
2013 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2014 if ( handled
&& event
.GetSkipped() )
2021 wxWindow
*ancestor
= focus
;
2024 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
2027 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
2028 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
2031 if (ancestor
->IsTopLevel())
2033 ancestor
= ancestor
->GetParent();
2036 #endif // wxUSE_ACCEL
2040 event
.Skip( FALSE
) ;
2041 event
.SetEventType( wxEVT_CHAR
) ;
2043 event
.m_keyCode
= realkeyval
;
2045 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2046 if ( handled
&& event
.GetSkipped() )
2050 (keyval
== WXK_TAB
) &&
2051 // CS: copied the change below from wxGTK
2052 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2053 // have this style, yet choose not to process this particular TAB in which
2054 // case TAB must still work as a navigational character
2056 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2058 (focus
->GetParent()) &&
2059 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2061 wxNavigationKeyEvent new_event
;
2062 new_event
.SetEventObject( focus
);
2063 new_event
.SetDirection( !event
.ShiftDown() );
2064 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2065 new_event
.SetWindowChange( event
.ControlDown() );
2066 new_event
.SetCurrentFocus( focus
);
2067 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2068 if ( handled
&& new_event
.GetSkipped() )
2071 // backdoor handler for default return and command escape
2072 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2074 // if window is not having a focus still testing for default enter or cancel
2075 // TODO add the UMA version for ActiveNonFloatingWindow
2076 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2079 if ( keyval
== WXK_RETURN
)
2081 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2083 if ( def
&& def
->IsEnabled() )
2085 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2086 event
.SetEventObject(def
);
2087 def
->Command(event
);
2091 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2092 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2094 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2095 new_event
.SetEventObject( focus
);
2096 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2103 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2110 keychar
= short(keymessage
& charCodeMask
);
2111 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2112 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2114 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2115 // and look at the character after
2117 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2118 keychar
= short(keyInfo
& charCodeMask
);
2119 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2121 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2123 if ( keyval
== keychar
)
2125 keyval
= wxToupper( keyval
) ;
2127 bool handled
= false ;
2129 wxKeyEvent
event(wxEVT_KEY_UP
);
2130 event
.m_shiftDown
= modifiers
& shiftKey
;
2131 event
.m_controlDown
= modifiers
& controlKey
;
2132 event
.m_altDown
= modifiers
& optionKey
;
2133 event
.m_metaDown
= modifiers
& cmdKey
;
2134 event
.m_keyCode
= keyval
;
2138 event
.m_timeStamp
= when
;
2139 event
.SetEventObject(focus
);
2140 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2146 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2148 EventRecord
* ev
= (EventRecord
*) evr
;
2149 WindowRef window
= (WindowRef
) ev
->message
;
2152 bool activate
= (ev
->modifiers
& activeFlag
) ;
2153 WindowClass wclass
;
2154 ::GetWindowClass ( window
, &wclass
) ;
2155 if ( wclass
== kFloatingWindowClass
)
2157 // if it is a floater we activate/deactivate the front non-floating window instead
2158 window
= ::FrontNonFloatingWindow() ;
2160 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2162 win
->MacActivate( ev
->when
, activate
) ;
2166 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2168 EventRecord
* ev
= (EventRecord
*) evr
;
2169 WindowRef window
= (WindowRef
) ev
->message
;
2170 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2173 if ( !wxPendingDelete
.Member(win
) )
2174 win
->MacUpdate( ev
->when
) ;
2178 // since there is no way of telling this foreign window to update itself
2179 // we have to invalidate the update region otherwise we keep getting the same
2180 // event over and over again
2181 BeginUpdate( window
) ;
2182 EndUpdate( window
) ;
2186 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2188 EventRecord
* ev
= (EventRecord
*) evr
;
2189 if ( HiWord( ev
->message
) != noErr
)
2193 SetPt( &point
, 100 , 100 ) ;
2195 err
= DIBadMount( point
, ev
->message
) ;
2196 wxASSERT( err
== noErr
) ;
2200 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2202 EventRecord
* ev
= (EventRecord
*) evr
;
2203 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2205 case suspendResumeMessage
:
2207 bool isResuming
= ev
->message
& resumeFlag
;
2208 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2210 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2213 WindowRef oldFrontWindow
= NULL
;
2214 WindowRef newFrontWindow
= NULL
;
2216 // in case we don't take care of activating ourselves, we have to synchronize
2217 // our idea of the active window with the process manager's - which it already activated
2219 if ( !doesActivate
)
2220 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2222 MacResume( convertClipboard
) ;
2224 newFrontWindow
= ::FrontNonFloatingWindow() ;
2226 if ( oldFrontWindow
)
2228 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2230 win
->MacActivate( ev
->when
, false ) ;
2232 if ( newFrontWindow
)
2234 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2236 win
->MacActivate( ev
->when
, true ) ;
2241 MacSuspend( convertClipboard
) ;
2245 case mouseMovedMessage
:
2249 wxWindow
* currentMouseWindow
= NULL
;
2251 if (s_captureWindow
)
2253 currentMouseWindow
= s_captureWindow
;
2257 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2258 ¤tMouseWindow
) ;
2261 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2263 wxMouseEvent event
;
2265 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2266 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2268 event
.m_leftDown
= isDown
&& !controlDown
;
2269 event
.m_middleDown
= FALSE
;
2270 event
.m_rightDown
= isDown
&& controlDown
;
2271 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2272 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2273 event
.m_altDown
= ev
->modifiers
& optionKey
;
2274 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2275 event
.m_x
= ev
->where
.h
;
2276 event
.m_y
= ev
->where
.v
;
2277 event
.m_timeStamp
= ev
->when
;
2278 event
.SetEventObject(this);
2280 if ( wxWindow::s_lastMouseWindow
)
2282 wxMouseEvent
eventleave(event
);
2283 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2284 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2285 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2287 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2289 if ( currentMouseWindow
)
2291 wxMouseEvent
evententer(event
);
2292 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2293 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2294 evententer
.SetEventObject( currentMouseWindow
) ;
2295 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2297 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2300 short windowPart
= inNoWindow
;
2302 if ( s_captureWindow
)
2304 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2305 windowPart
= inContent
;
2309 windowPart
= ::FindWindow(ev
->where
, &window
);
2316 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2318 win
->MacMouseMoved( ev
, windowPart
) ;
2325 UMAShowArrowCursor();
2335 UMAShowArrowCursor();
2346 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2348 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2349 wxMenu
* menu
= NULL
;
2350 wxMenuItem
* item
= NULL
;
2353 item
= mbar
->FindItem( id
, &menu
) ;
2355 wxCHECK_RET( item
!= NULL
&& menu
!= NULL
&& mbar
!= NULL
, wxT("error in menu item callback") );
2357 if (item
->IsCheckable())
2359 item
->Check( !item
->IsChecked() ) ;
2362 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
2366 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2369 return; // no menu item selected
2371 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2374 Str255 deskAccessoryName
;
2377 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2378 GetPort(&savedPort
);
2379 OpenDeskAcc(deskAccessoryName
);
2386 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2387 MacHandleMenuCommand( id
) ;