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 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
87 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
88 EVT_IDLE(wxApp::OnIdle
)
89 EVT_END_SESSION(wxApp::OnEndSession
)
90 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
95 const short kMacMinHeap
= (29 * 1024) ;
96 // platform specific static variables
98 const short kwxMacMenuBarResource
= 1 ;
99 const short kwxMacAppleMenuId
= 1 ;
101 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
102 wxWindow
* wxApp::s_captureWindow
= NULL
;
103 int wxApp::s_lastMouseDown
= 0 ;
104 long wxApp::sm_lastMessageTime
= 0;
105 long wxApp::s_lastModifiers
= 0 ;
108 bool wxApp::s_macDefaultEncodingIsPC
= true ;
109 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
110 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
111 long wxApp::s_macPreferencesMenuItemId
= 0 ;
112 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
113 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
119 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
126 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
129 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
131 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
134 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
136 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
139 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
141 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
144 // AEODoc Calls MacOpenFile on each of the files passed
146 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
150 DescType returnedType
;
156 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
160 err
= AECountItems(&docList
, &itemsInList
);
164 ProcessSerialNumber PSN
;
165 PSN
.highLongOfPSN
= 0 ;
166 PSN
.lowLongOfPSN
= kCurrentProcess
;
167 SetFrontProcess( &PSN
) ;
169 for (i
= 1; i
<= itemsInList
; i
++) {
170 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
171 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
172 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
178 // AEPDoc Calls MacPrintFile on each of the files passed
180 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
184 DescType returnedType
;
190 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
194 err
= AECountItems(&docList
, &itemsInList
);
198 ProcessSerialNumber PSN
;
199 PSN
.highLongOfPSN
= 0 ;
200 PSN
.lowLongOfPSN
= kCurrentProcess
;
201 SetFrontProcess( &PSN
) ;
203 for (i
= 1; i
<= itemsInList
; i
++) {
204 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
205 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
206 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
212 // AEOApp calls MacNewFile
214 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
220 // AEQuit attempts to quit the application
222 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
224 /* wxWindow* win = GetTopWindow() ;
237 //----------------------------------------------------------------------
238 // Support Routines linking the Mac...File Calls to the Document Manager
239 //----------------------------------------------------------------------
241 void wxApp::MacOpenFile(const wxString
& fileName
)
243 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
245 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
248 void wxApp::MacPrintFile(const wxString
& fileName
)
250 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
253 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
256 wxView
* view
= doc
->GetFirstView() ;
259 wxPrintout
*printout
= view
->OnCreatePrintout();
263 printer
.Print(view
->GetFrame(), printout
, TRUE
);
269 doc
->DeleteAllViews();
270 dm
->RemoveDocument(doc
) ;
276 void wxApp::MacNewFile()
280 //----------------------------------------------------------------------
281 // Carbon Event Handler
282 //----------------------------------------------------------------------
286 static const EventTypeSpec eventList
[] =
288 { kEventClassCommand
, kEventProcessCommand
} ,
289 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
291 { kEventClassApplication
, kEventAppActivated
} ,
292 { kEventClassApplication
, kEventAppDeactivated
} ,
293 // handling the quit event is not recommended by apple
294 // rather using the quit apple event - which we do
296 { kEventClassAppleEvent
, kEventAppleEvent
} ,
298 { kEventClassMouse
, kEventMouseDown
} ,
302 static pascal OSStatus
MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
304 return eventNotHandledErr
;
307 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
308 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
309 // deal with clicks in the menu bar explicitely
311 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
313 OSStatus result
= eventNotHandledErr
;
315 switch( GetEventKind(event
) )
317 case kEventMouseDown
:
322 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
323 sizeof( Point
), NULL
, &point
);
324 short windowPart
= ::FindWindow(point
, &window
);
326 if ( windowPart
== inMenuBar
)
328 MenuSelect( point
) ;
338 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
340 OSStatus result
= eventNotHandledErr
;
344 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
345 sizeof( HICommand
), NULL
, &command
);
347 MenuCommand id
= command
.commandID
;
348 if ( id
== kHICommandPreferences
)
349 id
= wxApp::s_macPreferencesMenuItemId
;
351 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
352 wxMenu
* menu
= NULL
;
353 wxMenuItem
* item
= NULL
;
356 item
= mbar
->FindItem( id
, &menu
) ;
358 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
361 switch( GetEventKind( event
) )
363 case kEventProcessCommand
:
365 if (item
->IsCheckable())
367 item
->Check( !item
->IsChecked() ) ;
370 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
374 case kEventCommandUpdateStatus
:
375 // eventually trigger an updateui round
385 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
387 OSStatus result
= eventNotHandledErr
;
388 switch ( GetEventKind( event
) )
390 case kEventAppActivated
:
393 wxTheApp
->MacResume( true ) ;
397 case kEventAppDeactivated
:
400 wxTheApp
->MacSuspend( true ) ;
410 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
412 OSStatus result
= eventNotHandledErr
;
413 switch( GetEventClass( event
) )
415 case kEventClassCommand
:
416 result
= CommandEventHandler( handler
, event
, data
) ;
418 case kEventClassApplication
:
419 result
= ApplicationEventHandler( handler
, event
, data
) ;
421 case kEventClassMenu
:
422 result
= MenuEventHandler( handler
, event
, data
) ;
424 case kEventClassMouse
:
425 result
= MouseEventHandler( handler
, event
, data
) ;
427 case kEventClassAppleEvent
:
430 wxMacConvertEventToRecord( event
, &rec
) ;
431 result
= AEProcessAppleEvent( &rec
) ;
441 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
445 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
446 // we know it's there ;-)
447 WXIMPORT
char std::__throws_bad_alloc
;
450 bool wxApp::Initialize(int argc
, wxChar
**argv
)
456 UMAInitToolbox( 4 ) ;
457 SetEventMask( everyEvent
) ;
458 UMAShowWatchCursor() ;
460 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
461 // open shared library resources from here since we don't have
462 // __wxinitialize in Mach-O shared libraries
463 wxStAppResource::OpenSharedLibraryResource(NULL
);
467 // test the minimal configuration necessary
473 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
475 error
= kMacSTRWrongMachine
;
477 else if (theMachine
< gestaltMacPlus
)
479 error
= kMacSTRWrongMachine
;
481 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
483 error
= kMacSTROldSystem
;
485 else if ( theSystem
< 0x0860 )
487 error
= kMacSTROldSystem
;
489 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
491 error
= kMacSTRSmallSize
;
497 if ( !UMAHasAppearance() )
499 error = kMacSTRNoPre8Yet ;
505 // if we encountered any problems so far, give the error code and exit immediately
509 wxStAppResource resload
;
513 GetIndString(message
, 128, error
);
514 UMAShowArrowCursor() ;
515 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
516 itemHit
= Alert(128, nil
);
521 # if __option(profile)
522 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
527 // now avoid exceptions thrown for new (bad_alloc)
528 // FIXME CS for some changes outside wxMac does not compile anymore
530 std::__throws_bad_alloc
= 0 ;
534 wxMacSetupConverters() ;
536 s_macCursorRgn
= ::NewRgn() ;
538 if ( !wxAppBase::Initialize(argc
, argv
) )
541 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
542 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
544 wxMacCreateNotifierTable() ;
546 UMAShowArrowCursor() ;
551 bool wxApp::OnInitGui()
553 if( !wxAppBase::OnInitGui() )
557 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
559 InstallApplicationEventHandler(
560 GetwxAppEventHandlerUPP(),
561 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
564 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
565 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
566 NewAEEventHandlerUPP(AEHandleODoc
) ,
568 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
569 NewAEEventHandlerUPP(AEHandleOApp
) ,
571 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
572 NewAEEventHandlerUPP(AEHandlePDoc
) ,
574 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
575 NewAEEventHandlerUPP(AEHandleQuit
) ,
578 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
579 NewAEEventHandlerProc(AEHandleODoc
) ,
581 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
582 NewAEEventHandlerProc(AEHandleOApp
) ,
584 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
585 NewAEEventHandlerProc(AEHandlePDoc
) ,
587 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
588 NewAEEventHandlerProc(AEHandleQuit
) ,
595 void wxApp::CleanUp()
597 wxToolTip::RemoveToolTips() ;
599 // One last chance for pending objects to be cleaned up
600 wxTheApp
->DeletePendingObjects();
602 wxMacDestroyNotifierTable() ;
604 delete wxWinMacWindowList
;
605 wxWinMacWindowList
= NULL
;
607 delete wxWinMacControlList
;
608 wxWinMacControlList
= NULL
;
611 # if __option(profile)
612 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
617 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
618 // close shared library resources from here since we don't have
619 // __wxterminate in Mach-O shared libraries
620 wxStAppResource::CloseSharedLibraryResource();
622 wxMacCleanupConverters() ;
624 UMACleanupToolbox() ;
625 if (s_macCursorRgn
) {
626 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
633 wxAppBase::CleanUp();
636 //----------------------------------------------------------------------
638 //----------------------------------------------------------------------
640 // extern variable for shared library resource id
641 // need to be able to find it with NSLookupAndBindSymbol
642 short gSharedLibraryResource
= kResFileNotOpened
;
644 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
645 CFBundleRef gSharedLibraryBundle
= NULL
;
646 #endif /* WXMAKINGDLL && __DARWIN__ */
648 wxStAppResource::wxStAppResource()
650 m_currentRefNum
= CurResFile() ;
651 if ( gSharedLibraryResource
!= kResFileNotOpened
)
653 UseResFile( gSharedLibraryResource
) ;
657 wxStAppResource::~wxStAppResource()
659 if ( m_currentRefNum
!= kResFileNotOpened
)
661 UseResFile( m_currentRefNum
) ;
665 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
667 gSharedLibraryResource
= kResFileNotOpened
;
670 if ( initBlock
!= NULL
) {
671 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
672 FSSpec
*fileSpec
= NULL
;
674 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
675 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
677 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
678 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
681 if (fileSpec
!= NULL
) {
682 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
687 // Open the shared library resource file if it is not yet open
690 const char *theLibPath
;
692 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
693 if (gSharedLibraryBundle
!= NULL
) {
694 // wxWindows has been bundled into a framework
695 // load the framework resources
697 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
700 // wxWindows is a simple dynamic shared library
701 // load the resources from the data fork of a separate resource file
706 OSErr theErr
= noErr
;
708 // get the library path
709 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
710 theModule
= NSModuleForSymbol(theSymbol
);
711 theLibPath
= NSLibraryNameForModule(theModule
);
713 // if we call wxLogDebug from here then, as wxTheApp hasn't been
714 // created yet when we're called from wxApp::Initialize(), wxLog
715 // is going to create a default stderr-based log target instead of
716 // the expected normal GUI one -- don't do it, if we really want
717 // to see this message just use fprintf() here
719 wxLogDebug( wxT("wxMac library installation name is '%s'"),
723 // allocate copy to replace .dylib.* extension with .rsrc
724 theResPath
= strdup(theLibPath
);
725 if (theResPath
!= NULL
) {
726 theName
= strrchr(theResPath
, '/');
727 if (theName
== NULL
) {
728 // no directory elements in path
729 theName
= theResPath
;
731 // find ".dylib" shared library extension
732 theExt
= strstr(theName
, ".dylib");
733 // overwrite extension with ".rsrc"
734 strcpy(theExt
, ".rsrc");
737 wxLogDebug( wxT("wxMac resources file name is '%s'"),
741 theErr
= FSPathMakeRef((UInt8
*) theResPath
, &theResRef
, false);
742 if (theErr
!= noErr
) {
743 // try in current directory (using name only)
744 theErr
= FSPathMakeRef((UInt8
*) theName
, &theResRef
, false);
747 // open the resource file
748 if (theErr
== noErr
) {
749 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
750 &gSharedLibraryResource
);
752 if (theErr
!= noErr
) {
755 wxT("unable to open wxMac resource file '%s'\n"),
757 #endif // __WXDEBUG__
760 // free duplicated resource file path
764 #endif /* __DARWIN__ */
766 #endif /* WXMAKINGDLL */
769 void wxStAppResource::CloseSharedLibraryResource()
772 // Close the shared library resource file
773 if (gSharedLibraryResource
!= kResFileNotOpened
) {
775 if (gSharedLibraryBundle
!= NULL
) {
776 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
777 gSharedLibraryResource
);
778 gSharedLibraryBundle
= NULL
;
781 #endif /* __DARWIN__ */
783 CloseResFile(gSharedLibraryResource
);
785 gSharedLibraryResource
= kResFileNotOpened
;
787 #endif /* WXMAKINGDLL */
790 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
792 // for shared libraries we have to manually get the correct resource
793 // ref num upon initializing and releasing when terminating, therefore
794 // the __wxinitialize and __wxterminate must be used
797 void __sinit(void); /* (generated by linker) */
798 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
799 pascal void __terminate(void);
802 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
804 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
805 return __initialize( theInitBlock
) ;
808 pascal void __wxterminate(void)
810 wxStAppResource::CloseSharedLibraryResource() ;
814 #endif /* WXMAKINGDLL && !__DARWIN__ */
816 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char *WXUNUSED(argv
)[] )
818 return wxApp::Initialize();
821 int WXDLLEXPORT
wxEntryInitGui()
823 return wxTheApp
->OnInitGui();
826 void WXDLLEXPORT
wxEntryCleanup()
831 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
834 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
835 // This seems to be necessary since there are 'rogue'
836 // objects present at this point (perhaps global objects?)
837 // Setting a checkpoint will ignore them as far as the
838 // memory checking facility is concerned.
839 // Of course you may argue that memory allocated in globals should be
840 // checked, but this is a reasonable compromise.
841 wxDebugContext::SetCheckpoint();
844 if (!wxEntryStart(argc
, argv
)) {
847 // create the application object or ensure that one already exists
850 // The app may have declared a global application object, but we recommend
851 // the IMPLEMENT_APP macro is used instead, which sets an initializer
852 // function for delayed, dynamic app object construction.
853 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
854 wxT("No initializer - use IMPLEMENT_APP macro.") );
856 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
859 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
862 // Mac OS X passes a process serial number command line argument when
863 // the application is launched from the Finder. This argument must be
864 // removed from the command line arguments before being handled by the
865 // application (otherwise applications would need to handle it)
868 if (strncmp(argv
[1], "-psn_", 5) == 0) {
869 // assume the argument is always the only one and remove it
874 argc
= 0 ; // currently we don't support files as parameters
876 // we could try to get the open apple events here to adjust argc and argv better
878 wxTheApp
->argc
= argc
;
880 wxTheApp
->argv
= new wxChar
*[argc
+1];
882 for ( mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
884 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
886 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
888 wxTheApp
->argv
= argv
;
891 // GUI-specific initialization, such as creating an app context.
894 // Here frames insert themselves automatically
895 // into wxTopLevelWindows by getting created
900 if ( wxTheApp
->OnInit() )
904 retValue
= wxTheApp
->OnRun();
907 // We want to initialize, but not run or exit immediately.
910 //else: app initialization failed, so we skipped OnRun()
912 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
915 // Forcibly delete the window.
916 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
917 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
919 topWindow
->Close(TRUE
);
920 wxTheApp
->DeletePendingObjects();
925 wxTheApp
->SetTopWindow(NULL
);
938 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
940 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
941 OSStatus err
= noErr
;
944 switch( GetEventClass( event
) )
946 case kEventClassKeyboard
:
949 switch( GetEventKind(event
) )
951 case kEventRawKeyDown
:
952 rec
->what
= keyDown
;
954 case kEventRawKeyRepeat
:
955 rec
->what
= autoKey
;
957 case kEventRawKeyUp
:
960 case kEventRawKeyModifiersChanged
:
961 rec
->what
= nullEvent
;
970 unsigned char charCode
;
972 GetMouse( &rec
->where
) ;
974 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
975 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
976 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
977 rec
->modifiers
= modifiers
;
978 rec
->message
= (keyCode
<< 8 ) + charCode
;
982 case kEventClassTextInput
:
984 switch( GetEventKind( event
) )
986 case kEventTextInputUnicodeForKeyEvent
:
989 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
993 unsigned char charCode
;
995 GetMouse( &rec
->where
) ;
996 rec
->what
= keyDown
;
997 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
998 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
999 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1000 rec
->modifiers
= modifiers
;
1001 rec
->message
= (keyCode
<< 8 ) + charCode
;
1017 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1019 OSStatus result = eventNotHandledErr ;
1022 switch ( GetEventClass( event ) )
1024 case kEventClassKeyboard :
1025 if ( wxMacConvertEventToRecord( event , &rec ) )
1027 wxTheApp->MacHandleModifierEvents( &rec ) ;
1028 wxTheApp->MacHandleOneEvent( &rec ) ;
1032 case kEventClassTextInput :
1033 if ( wxMacConvertEventToRecord( event , &rec ) )
1035 wxTheApp->MacHandleModifierEvents( &rec ) ;
1036 wxTheApp->MacHandleOneEvent( &rec ) ;
1053 #if WXWIN_COMPATIBILITY_2_2
1054 m_wantDebugOutput
= TRUE
;
1060 m_printMode
= wxPRINT_WINDOWS
;
1064 bool wxApp::Initialized()
1072 int wxApp::MainLoop()
1084 // Returns TRUE if more time is needed.
1085 bool wxApp::ProcessIdle()
1088 event
.SetEventObject(this);
1089 ProcessEvent(event
);
1091 return event
.MoreRequested();
1094 void wxApp::ExitMainLoop()
1096 m_keepGoing
= FALSE
;
1099 // Is a message/event pending?
1100 bool wxApp::Pending()
1103 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1107 return EventAvail( everyEvent
, &event
) ;
1111 // Dispatch a message.
1112 void wxApp::Dispatch()
1117 void wxApp::OnIdle(wxIdleEvent
& event
)
1119 // Avoid recursion (via ProcessEvent default case)
1126 // 'Garbage' collection of windows deleted with Close().
1127 DeletePendingObjects();
1129 // flush the logged messages if any
1130 wxLog
*pLog
= wxLog::GetActiveTarget();
1131 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1134 // Send OnIdle events to all windows
1135 bool needMore
= SendIdleEvents();
1138 event
.RequestMore(TRUE
);
1140 // If they are pending events, we must process them: pending events are
1141 // either events to the threads other than main or events posted with
1142 // wxPostEvent() functions
1143 wxMacProcessNotifierAndPendingEvents();
1146 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1147 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1150 void wxApp::WakeUpIdle()
1155 // Send idle event to all top-level windows
1156 bool wxApp::SendIdleEvents()
1158 bool needMore
= FALSE
;
1159 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1162 wxWindow
* win
= node
->GetData();
1163 if (SendIdleEvents(win
))
1166 node
= node
->GetNext();
1171 // Send idle event to window and all subwindows
1172 bool wxApp::SendIdleEvents(wxWindow
* win
)
1174 bool needMore
= FALSE
;
1177 event
.SetEventObject(win
);
1178 win
->ProcessEvent(event
);
1180 if (event
.MoreRequested())
1183 wxWindowListNode
* node
= win
->GetChildren().GetFirst();
1186 wxWindow
* win
= node
->GetData();
1187 if (SendIdleEvents(win
))
1190 node
= node
->GetNext();
1201 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1204 GetTopWindow()->Close(TRUE
);
1207 // Default behaviour: close the application with prompts. The
1208 // user can veto the close, and therefore the end session.
1209 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1213 if (!GetTopWindow()->Close(!event
.CanVeto()))
1218 extern "C" void wxCYield() ;
1224 // Yield to other processes
1226 bool wxApp::Yield(bool onlyIfNeeded
)
1230 if ( !onlyIfNeeded
)
1232 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1241 YieldToAnyThread() ;
1243 // by definition yield should handle all non-processed events
1247 OSStatus status
= noErr
;
1250 s_inReceiveEvent
= true ;
1251 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1252 s_inReceiveEvent
= false ;
1254 if ( status
== eventLoopTimedOutErr
)
1256 // make sure next time the event loop will trigger idle events
1257 sleepTime
= kEventDurationNoWait
;
1259 else if ( status
== eventLoopQuitErr
)
1261 // according to QA1061 this may also occur when a WakeUp Process
1266 MacHandleOneEvent( theEvent
) ;
1267 ReleaseEvent(theEvent
);
1269 } while( status
== noErr
) ;
1273 // having a larger value here leads to large performance slowdowns
1274 // so we cannot give background apps more processor time here
1275 // we do so however having a large sleep value in the main event loop
1278 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1280 MacHandleModifierEvents( &event
) ;
1281 MacHandleOneEvent( &event
);
1282 if ( event
.what
!= kHighLevelEvent
)
1283 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1285 MacHandleModifierEvents( &event
) ;
1288 wxMacProcessNotifierAndPendingEvents() ;
1294 // platform specifics
1296 void wxApp::MacSuspend( bool convertClipboard
)
1299 // we have to deactive the top level windows manually
1301 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1304 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1306 #if 0 // having problems right now with that
1307 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1310 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1312 node
= node
->GetNext();
1315 ::HideFloatingWindows() ;
1317 s_lastMouseDown
= 0 ;
1319 if( convertClipboard
)
1321 MacConvertPrivateToPublicScrap() ;
1325 extern wxList wxModalDialogs
;
1327 void wxApp::MacResume( bool convertClipboard
)
1329 s_lastMouseDown
= 0 ;
1330 if( convertClipboard
)
1332 MacConvertPublicToPrivateScrap() ;
1336 ::ShowFloatingWindows() ;
1337 // raise modal dialogs in case a non modal window was selected to activate the app
1339 wxNode
* node
= wxModalDialogs
.GetFirst();
1342 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1345 node
= node
->GetNext();
1350 void wxApp::MacConvertPrivateToPublicScrap()
1354 void wxApp::MacConvertPublicToPrivateScrap()
1358 void wxApp::MacDoOneEvent()
1363 s_inReceiveEvent
= true ;
1364 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1365 s_inReceiveEvent
= false ;
1366 if ( status
== eventLoopTimedOutErr
)
1368 if ( wxTheApp
->ProcessIdle() )
1369 sleepTime
= kEventDurationNoWait
;
1371 sleepTime
= kEventDurationForever
;
1373 else if ( status
== eventLoopQuitErr
)
1375 // according to QA1061 this may also occur when a WakeUp Process
1380 MacHandleOneEvent( theEvent
) ;
1381 ReleaseEvent(theEvent
);
1382 sleepTime
= kEventDurationNoWait
;
1387 EventMask eventMask
= everyEvent
;
1389 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1391 MacHandleModifierEvents( &event
) ;
1392 MacHandleOneEvent( &event
);
1396 MacHandleModifierEvents( &event
) ;
1398 WindowPtr window
= ::FrontWindow() ;
1400 ::IdleControls( window
) ;
1402 if ( wxTheApp
->ProcessIdle() )
1405 sleepTime
= GetCaretTime() / 2 ;
1407 if ( event
.what
!= kHighLevelEvent
)
1408 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1412 DeletePendingObjects() ;
1413 wxMacProcessNotifierAndPendingEvents() ;
1416 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1419 EventTargetRef theTarget
;
1420 theTarget
= GetEventDispatcherTarget();
1421 m_macCurrentEvent
= evr
;
1422 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1424 EventRecord
* ev
= (EventRecord
*) evr
;
1425 m_macCurrentEvent
= ev
;
1427 wxApp::sm_lastMessageTime
= ev
->when
;
1432 MacHandleMouseDownEvent( ev
) ;
1433 if ( ev
->modifiers
& controlKey
)
1434 s_lastMouseDown
= 2;
1436 s_lastMouseDown
= 1;
1439 if ( s_lastMouseDown
== 2 )
1441 ev
->modifiers
|= controlKey
;
1445 ev
->modifiers
&= ~controlKey
;
1447 MacHandleMouseUpEvent( ev
) ;
1448 s_lastMouseDown
= 0;
1451 MacHandleActivateEvent( ev
) ;
1454 MacHandleUpdateEvent( ev
) ;
1458 MacHandleKeyDownEvent( ev
) ;
1461 MacHandleKeyUpEvent( ev
) ;
1464 MacHandleDiskEvent( ev
) ;
1467 MacHandleOSEvent( ev
) ;
1469 case kHighLevelEvent
:
1470 MacHandleHighLevelEvent( ev
) ;
1476 wxMacProcessNotifierAndPendingEvents() ;
1480 bool s_macIsInModalLoop
= false ;
1482 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1484 EventRecord
* ev
= (EventRecord
*) evr
;
1485 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1487 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1489 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1490 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1491 event
.m_altDown
= ev
->modifiers
& optionKey
;
1492 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1494 event
.m_x
= ev
->where
.h
;
1495 event
.m_y
= ev
->where
.v
;
1496 event
.m_timeStamp
= ev
->when
;
1497 wxWindow
* focus
= wxWindow::FindFocus() ;
1498 event
.SetEventObject(focus
);
1500 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1502 event
.m_keyCode
= WXK_CONTROL
;
1503 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1504 focus
->GetEventHandler()->ProcessEvent( event
) ;
1506 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1508 event
.m_keyCode
= WXK_SHIFT
;
1509 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1510 focus
->GetEventHandler()->ProcessEvent( event
) ;
1512 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1514 event
.m_keyCode
= WXK_ALT
;
1515 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1516 focus
->GetEventHandler()->ProcessEvent( event
) ;
1518 s_lastModifiers
= ev
->modifiers
;
1522 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1524 // we must avoid reentrancy problems when processing high level events eg printing
1525 bool former
= s_inYield
;
1527 EventRecord
* ev
= (EventRecord
*) evr
;
1528 ::AEProcessAppleEvent( ev
) ;
1529 s_inYield
= former
;
1532 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1534 EventRecord
* ev
= (EventRecord
*) evr
;
1535 wxToolTip::RemoveToolTips() ;
1538 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1539 WindowAttributes frontWindowAttributes
= NULL
;
1541 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1543 short windowPart
= ::FindWindow(ev
->where
, &window
);
1544 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1545 if ( wxPendingDelete
.Member(win
) )
1549 GetQDGlobalsScreenBits( &screenBits
);
1554 if ( s_macIsInModalLoop
)
1560 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1561 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1562 s_lastMouseDown
= 0;
1566 SystemClick( ev
, window
) ;
1567 s_lastMouseDown
= 0;
1570 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1576 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1581 Point pt
= { 0, 0 } ;
1582 SetPortWindowPort(window
) ;
1583 LocalToGlobal( &pt
) ;
1585 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1586 -1 , wxSIZE_USE_EXISTING
);
1588 s_lastMouseDown
= 0;
1592 if (TrackGoAway(window
, ev
->where
))
1597 s_lastMouseDown
= 0;
1601 Rect newContentRect
;
1602 Rect constraintRect
;
1603 constraintRect
.top
= win
->GetMinHeight() ;
1604 if ( constraintRect
.top
== -1 )
1605 constraintRect
.top
= 0 ;
1606 constraintRect
.left
= win
->GetMinWidth() ;
1607 if ( constraintRect
.left
== -1 )
1608 constraintRect
.left
= 0 ;
1609 constraintRect
.right
= win
->GetMaxWidth() ;
1610 if ( constraintRect
.right
== -1 )
1611 constraintRect
.right
= 32000 ;
1612 constraintRect
.bottom
= win
->GetMaxHeight() ;
1613 if ( constraintRect
.bottom
== -1 )
1614 constraintRect
.bottom
= 32000 ;
1616 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1617 &constraintRect
, &newContentRect
) ;
1620 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1621 newContentRect
.right
- newContentRect
.left
,
1622 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1624 s_lastMouseDown
= 0;
1629 if (TrackBox(window
, ev
->where
, windowPart
))
1631 // TODO setup size event
1632 ZoomWindow( window
, windowPart
, false ) ;
1638 Point pt
= { 0, 0 } ;
1639 SetPortWindowPort(window
) ;
1640 LocalToGlobal( &pt
) ;
1643 GetWindowPortBounds(window
, &tempRect
) ;
1644 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1645 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1648 s_lastMouseDown
= 0;
1650 case inCollapseBox
:
1651 // TODO setup size event
1652 s_lastMouseDown
= 0;
1659 SetPortWindowPort(window
) ;
1662 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1664 if ( s_macIsInModalLoop
)
1668 else if ( UMAIsWindowFloating( window
) )
1671 win
->MacMouseDown( ev
, windowPart
) ;
1676 win
->MacMouseDown( ev
, windowPart
) ;
1677 ::SelectWindow( window
) ;
1683 win
->MacMouseDown( ev
, windowPart
) ;
1691 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1693 EventRecord
* ev
= (EventRecord
*) evr
;
1696 short windowPart
= inNoWindow
;
1697 if ( wxTheApp
->s_captureWindow
)
1699 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1700 windowPart
= inContent
;
1704 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1715 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1717 win
->MacMouseUp( ev
, windowPart
) ;
1725 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1726 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1731 case kHomeCharCode
:
1734 case kEnterCharCode
:
1735 retval
= WXK_RETURN
;
1740 case kHelpCharCode
:
1743 case kBackspaceCharCode
:
1749 case kPageUpCharCode
:
1750 retval
= WXK_PAGEUP
;
1752 case kPageDownCharCode
:
1753 retval
= WXK_PAGEDOWN
;
1755 case kReturnCharCode
:
1756 retval
= WXK_RETURN
;
1758 case kFunctionKeyCharCode
:
1810 case kEscapeCharCode
:
1811 retval
= WXK_ESCAPE
;
1813 case kLeftArrowCharCode
:
1816 case kRightArrowCharCode
:
1817 retval
= WXK_RIGHT
;
1819 case kUpArrowCharCode
:
1822 case kDownArrowCharCode
:
1825 case kDeleteCharCode
:
1826 retval
= WXK_DELETE
;
1835 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1837 EventRecord
* ev
= (EventRecord
*) evr
;
1838 wxToolTip::RemoveToolTips() ;
1840 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1841 if ( HiWord( menuresult
) )
1843 if ( !s_macIsInModalLoop
)
1844 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1848 wxWindow
* focus
= wxWindow::FindFocus() ;
1850 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1852 // has not been handled -> perform default
1853 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1854 if ( control
&& control
->GetMacControl() != NULL
)
1858 keychar
= short(ev
->message
& charCodeMask
);
1859 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1860 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1866 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1868 EventRecord
* ev
= (EventRecord
*) evr
;
1869 wxToolTip::RemoveToolTips() ;
1871 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1872 if ( HiWord( menuresult
) )
1877 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1883 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1890 keychar
= short(keymessage
& charCodeMask
);
1891 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1893 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1895 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1896 // and look at the character after
1898 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1899 keychar
= short(keyInfo
& charCodeMask
);
1900 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1902 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1903 long realkeyval
= keyval
;
1904 if ( keyval
== keychar
)
1906 // 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)
1907 realkeyval
= short(keymessage
& charCodeMask
) ;
1908 keyval
= wxToupper( keyval
) ;
1911 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1912 bool handled
= false ;
1913 event
.m_shiftDown
= modifiers
& shiftKey
;
1914 event
.m_controlDown
= modifiers
& controlKey
;
1915 event
.m_altDown
= modifiers
& optionKey
;
1916 event
.m_metaDown
= modifiers
& cmdKey
;
1917 event
.m_keyCode
= keyval
;
1921 event
.m_timeStamp
= when
;
1922 event
.SetEventObject(focus
);
1923 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1924 if ( handled
&& event
.GetSkipped() )
1931 wxWindow
*ancestor
= focus
;
1934 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1937 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1938 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1941 if (ancestor
->IsTopLevel())
1943 ancestor
= ancestor
->GetParent();
1946 #endif // wxUSE_ACCEL
1950 event
.Skip( FALSE
) ;
1951 event
.SetEventType( wxEVT_CHAR
) ;
1953 event
.m_keyCode
= realkeyval
;
1955 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1956 if ( handled
&& event
.GetSkipped() )
1960 (keyval
== WXK_TAB
) &&
1961 // CS: copied the change below from wxGTK
1962 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1963 // have this style, yet choose not to process this particular TAB in which
1964 // case TAB must still work as a navigational character
1966 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1968 (focus
->GetParent()) &&
1969 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1971 wxNavigationKeyEvent new_event
;
1972 new_event
.SetEventObject( focus
);
1973 new_event
.SetDirection( !event
.ShiftDown() );
1974 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1975 new_event
.SetWindowChange( event
.ControlDown() );
1976 new_event
.SetCurrentFocus( focus
);
1977 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1978 if ( handled
&& new_event
.GetSkipped() )
1981 // backdoor handler for default return and command escape
1982 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1984 // if window is not having a focus still testing for default enter or cancel
1985 // TODO add the UMA version for ActiveNonFloatingWindow
1986 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1989 if ( keyval
== WXK_RETURN
)
1991 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1993 if ( def
&& def
->IsEnabled() )
1995 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1996 event
.SetEventObject(def
);
1997 def
->Command(event
);
2001 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2002 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2004 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2005 new_event
.SetEventObject( focus
);
2006 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2013 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2020 keychar
= short(keymessage
& charCodeMask
);
2021 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2022 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2024 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2025 // and look at the character after
2027 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2028 keychar
= short(keyInfo
& charCodeMask
);
2029 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2031 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2033 if ( keyval
== keychar
)
2035 keyval
= wxToupper( keyval
) ;
2037 bool handled
= false ;
2039 wxKeyEvent
event(wxEVT_KEY_UP
);
2040 event
.m_shiftDown
= modifiers
& shiftKey
;
2041 event
.m_controlDown
= modifiers
& controlKey
;
2042 event
.m_altDown
= modifiers
& optionKey
;
2043 event
.m_metaDown
= modifiers
& cmdKey
;
2044 event
.m_keyCode
= keyval
;
2048 event
.m_timeStamp
= when
;
2049 event
.SetEventObject(focus
);
2050 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2056 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2058 EventRecord
* ev
= (EventRecord
*) evr
;
2059 WindowRef window
= (WindowRef
) ev
->message
;
2062 bool activate
= (ev
->modifiers
& activeFlag
) ;
2063 WindowClass wclass
;
2064 ::GetWindowClass ( window
, &wclass
) ;
2065 if ( wclass
== kFloatingWindowClass
)
2067 // if it is a floater we activate/deactivate the front non-floating window instead
2068 window
= ::FrontNonFloatingWindow() ;
2070 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2072 win
->MacActivate( ev
->when
, activate
) ;
2076 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2078 EventRecord
* ev
= (EventRecord
*) evr
;
2079 WindowRef window
= (WindowRef
) ev
->message
;
2080 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2083 if ( !wxPendingDelete
.Member(win
) )
2084 win
->MacUpdate( ev
->when
) ;
2088 // since there is no way of telling this foreign window to update itself
2089 // we have to invalidate the update region otherwise we keep getting the same
2090 // event over and over again
2091 BeginUpdate( window
) ;
2092 EndUpdate( window
) ;
2096 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2098 EventRecord
* ev
= (EventRecord
*) evr
;
2099 if ( HiWord( ev
->message
) != noErr
)
2103 SetPt( &point
, 100 , 100 ) ;
2105 err
= DIBadMount( point
, ev
->message
) ;
2106 wxASSERT( err
== noErr
) ;
2110 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2112 EventRecord
* ev
= (EventRecord
*) evr
;
2113 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2115 case suspendResumeMessage
:
2117 bool isResuming
= ev
->message
& resumeFlag
;
2118 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2120 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2123 WindowRef oldFrontWindow
= NULL
;
2124 WindowRef newFrontWindow
= NULL
;
2126 // in case we don't take care of activating ourselves, we have to synchronize
2127 // our idea of the active window with the process manager's - which it already activated
2129 if ( !doesActivate
)
2130 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2132 MacResume( convertClipboard
) ;
2134 newFrontWindow
= ::FrontNonFloatingWindow() ;
2136 if ( oldFrontWindow
)
2138 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2140 win
->MacActivate( ev
->when
, false ) ;
2142 if ( newFrontWindow
)
2144 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2146 win
->MacActivate( ev
->when
, true ) ;
2151 MacSuspend( convertClipboard
) ;
2155 case mouseMovedMessage
:
2159 wxWindow
* currentMouseWindow
= NULL
;
2161 if (s_captureWindow
)
2163 currentMouseWindow
= s_captureWindow
;
2167 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2168 ¤tMouseWindow
) ;
2171 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2173 wxMouseEvent event
;
2175 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2176 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2178 event
.m_leftDown
= isDown
&& !controlDown
;
2179 event
.m_middleDown
= FALSE
;
2180 event
.m_rightDown
= isDown
&& controlDown
;
2181 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2182 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2183 event
.m_altDown
= ev
->modifiers
& optionKey
;
2184 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2185 event
.m_x
= ev
->where
.h
;
2186 event
.m_y
= ev
->where
.v
;
2187 event
.m_timeStamp
= ev
->when
;
2188 event
.SetEventObject(this);
2190 if ( wxWindow::s_lastMouseWindow
)
2192 wxMouseEvent
eventleave(event
);
2193 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2194 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2195 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2197 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2199 if ( currentMouseWindow
)
2201 wxMouseEvent
evententer(event
);
2202 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2203 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2204 evententer
.SetEventObject( currentMouseWindow
) ;
2205 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2207 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2210 short windowPart
= inNoWindow
;
2212 if ( s_captureWindow
)
2214 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2215 windowPart
= inContent
;
2219 windowPart
= ::FindWindow(ev
->where
, &window
);
2226 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2228 win
->MacMouseMoved( ev
, windowPart
) ;
2235 UMAShowArrowCursor();
2245 UMAShowArrowCursor();
2256 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2258 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2259 wxFrame
* frame
= mbar
->GetFrame();
2260 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2263 frame
->ProcessCommand(id
);
2268 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2271 return; // no menu item selected
2273 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2276 Str255 deskAccessoryName
;
2279 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2280 GetPort(&savedPort
);
2281 OpenDeskAcc(deskAccessoryName
);
2288 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2289 MacHandleMenuCommand( id
) ;