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_CORE)
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
;
78 static bool s_inReceiveEvent
= FALSE
;
79 static EventTime sleepTime
= kEventDurationNoWait
;
81 static long sleepTime
= 0 ;
84 #if !USE_SHARED_LIBRARY
85 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
86 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
87 EVT_IDLE(wxApp::OnIdle
)
88 EVT_END_SESSION(wxApp::OnEndSession
)
89 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
94 const short kMacMinHeap
= (29 * 1024) ;
95 // platform specific static variables
97 const short kwxMacMenuBarResource
= 1 ;
98 const short kwxMacAppleMenuId
= 1 ;
100 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
101 wxWindow
* wxApp::s_captureWindow
= NULL
;
102 int wxApp::s_lastMouseDown
= 0 ;
103 long wxApp::sm_lastMessageTime
= 0;
104 long wxApp::s_lastModifiers
= 0 ;
107 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
108 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
109 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
110 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
111 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
113 //----------------------------------------------------------------------
114 // Core Apple Event Support
115 //----------------------------------------------------------------------
117 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
118 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
119 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
124 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
127 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
129 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
132 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
134 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
137 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
139 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
142 // AEODoc Calls MacOpenFile on each of the files passed
144 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
148 DescType returnedType
;
154 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
158 err
= AECountItems(&docList
, &itemsInList
);
162 ProcessSerialNumber PSN
;
163 PSN
.highLongOfPSN
= 0 ;
164 PSN
.lowLongOfPSN
= kCurrentProcess
;
165 SetFrontProcess( &PSN
) ;
167 for (i
= 1; i
<= itemsInList
; i
++) {
168 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
169 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
170 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
176 // AEPDoc Calls MacPrintFile on each of the files passed
178 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
182 DescType returnedType
;
188 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
192 err
= AECountItems(&docList
, &itemsInList
);
196 ProcessSerialNumber PSN
;
197 PSN
.highLongOfPSN
= 0 ;
198 PSN
.lowLongOfPSN
= kCurrentProcess
;
199 SetFrontProcess( &PSN
) ;
201 for (i
= 1; i
<= itemsInList
; i
++) {
202 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
203 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
204 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
210 // AEOApp calls MacNewFile
212 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
218 // AEQuit attempts to quit the application
220 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
222 /* wxWindow* win = GetTopWindow() ;
235 //----------------------------------------------------------------------
236 // Support Routines linking the Mac...File Calls to the Document Manager
237 //----------------------------------------------------------------------
239 void wxApp::MacOpenFile(const wxString
& fileName
)
241 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
243 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
246 void wxApp::MacPrintFile(const wxString
& fileName
)
248 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
251 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
254 wxView
* view
= doc
->GetFirstView() ;
257 wxPrintout
*printout
= view
->OnCreatePrintout();
261 printer
.Print(view
->GetFrame(), printout
, TRUE
);
267 doc
->DeleteAllViews();
268 dm
->RemoveDocument(doc
) ;
274 void wxApp::MacNewFile()
278 //----------------------------------------------------------------------
279 // Carbon Event Handler
280 //----------------------------------------------------------------------
284 static const EventTypeSpec eventList
[] =
286 { kEventClassCommand
, kEventProcessCommand
} ,
287 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
289 { kEventClassMenu
, kEventMenuOpening
},
290 { kEventClassMenu
, kEventMenuClosed
},
292 { kEventClassApplication
, kEventAppActivated
} ,
293 { kEventClassApplication
, kEventAppDeactivated
} ,
294 // handling the quit event is not recommended by apple
295 // rather using the quit apple event - which we do
297 { kEventClassAppleEvent
, kEventAppleEvent
} ,
299 { kEventClassMouse
, kEventMouseDown
} ,
300 { kEventClassMouse
, kEventMouseMoved
} ,
301 { kEventClassMouse
, kEventMouseUp
} ,
302 { kEventClassMouse
, kEventMouseDragged
} ,
306 static pascal OSStatus
307 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
309 // FIXME: this doesn't work for multiple windows
310 wxWindow
*win
= wxTheApp
->GetTopWindow();
313 // VZ: we could find the menu from its handle here by examining all
314 // the menus in the menu bar recursively but knowing that neither
315 // wxMSW nor wxGTK do it why bother...
319 GetEventParameter(event
,
320 kEventParamDirectObject
,
322 sizeof(menuRef
), NULL
,
326 wxMenuEvent
wxevent(GetEventKind(event
) == kEventMenuOpening
329 wxevent
.SetEventObject(win
);
331 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
334 return eventNotHandledErr
;
337 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
338 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
339 // deal with clicks in the menu bar explicitely
341 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
343 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
345 OSStatus result
= eventNotHandledErr
;
348 UInt32 modifiers
= 0;
349 EventMouseButton button
= 0 ;
352 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
353 sizeof( Point
), NULL
, &point
);
354 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
355 sizeof( UInt32
), NULL
, &modifiers
);
356 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
357 sizeof( EventMouseButton
), NULL
, &button
);
358 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
359 sizeof( UInt32
), NULL
, &click
);
361 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
362 modifiers
+= btnState
;
365 switch( GetEventKind(event
) )
367 case kEventMouseDown
:
371 short windowPart
= ::FindWindow(point
, &window
);
373 if ( windowPart
== inMenuBar
)
375 MenuSelect( point
) ;
380 case kEventMouseDragged
:
383 if ( wxTheApp
->s_captureWindow
)
384 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
387 case kEventMouseMoved
:
389 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
399 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
401 OSStatus result
= eventNotHandledErr
;
405 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
406 sizeof( HICommand
), NULL
, &command
);
408 MenuCommand id
= command
.commandID
;
409 if ( id
== kHICommandPreferences
)
410 id
= wxApp::s_macPreferencesMenuItemId
;
412 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
413 wxMenu
* menu
= NULL
;
414 wxMenuItem
* item
= NULL
;
418 item
= mbar
->FindItem( id
, &menu
) ;
419 // it is not 100 % sure that an menu of id 0 is really ours, safety check
420 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
427 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
430 switch( GetEventKind( event
) )
432 case kEventProcessCommand
:
434 if (item
->IsCheckable())
436 item
->Check( !item
->IsChecked() ) ;
439 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
443 case kEventCommandUpdateStatus
:
444 // eventually trigger an updateui round
454 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
456 OSStatus result
= eventNotHandledErr
;
457 switch ( GetEventKind( event
) )
459 case kEventAppActivated
:
462 wxTheApp
->MacResume( true ) ;
466 case kEventAppDeactivated
:
469 wxTheApp
->MacSuspend( true ) ;
479 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
481 OSStatus result
= eventNotHandledErr
;
482 switch( GetEventClass( event
) )
484 case kEventClassCommand
:
485 result
= CommandEventHandler( handler
, event
, data
) ;
487 case kEventClassApplication
:
488 result
= ApplicationEventHandler( handler
, event
, data
) ;
490 case kEventClassMenu
:
491 result
= MenuEventHandler( handler
, event
, data
) ;
493 case kEventClassMouse
:
494 result
= MouseEventHandler( handler
, event
, data
) ;
496 case kEventClassAppleEvent
:
499 wxMacConvertEventToRecord( event
, &rec
) ;
500 result
= AEProcessAppleEvent( &rec
) ;
510 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
514 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
515 // we know it's there ;-)
516 WXIMPORT
char std::__throws_bad_alloc
;
519 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
525 UMAInitToolbox( 4 ) ;
526 SetEventMask( everyEvent
) ;
527 UMAShowWatchCursor() ;
529 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
530 // open shared library resources from here since we don't have
531 // __wxinitialize in Mach-O shared libraries
532 wxStAppResource::OpenSharedLibraryResource(NULL
);
536 // test the minimal configuration necessary
542 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
544 error
= kMacSTRWrongMachine
;
546 else if (theMachine
< gestaltMacPlus
)
548 error
= kMacSTRWrongMachine
;
550 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
552 error
= kMacSTROldSystem
;
554 else if ( theSystem
< 0x0860 )
556 error
= kMacSTROldSystem
;
558 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
560 error
= kMacSTRSmallSize
;
566 if ( !UMAHasAppearance() )
568 error = kMacSTRNoPre8Yet ;
574 // if we encountered any problems so far, give the error code and exit immediately
578 wxStAppResource resload
;
582 GetIndString(message
, 128, error
);
583 UMAShowArrowCursor() ;
584 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
585 itemHit
= Alert(128, nil
);
590 # if __option(profile)
591 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
596 // now avoid exceptions thrown for new (bad_alloc)
597 // FIXME CS for some changes outside wxMac does not compile anymore
599 std::__throws_bad_alloc
= 0 ;
604 s_macCursorRgn
= ::NewRgn() ;
606 // Mac OS X passes a process serial number command line argument when
607 // the application is launched from the Finder. This argument must be
608 // removed from the command line arguments before being handled by the
609 // application (otherwise applications would need to handle it)
612 static const wxChar
*ARG_PSN
= _T("-psn_");
613 if ( wxStrncmp(argv
[1], ARG_PSN
, sizeof(ARG_PSN
) - 1) == 0 )
615 // remove this argument
616 memmove(argv
, argv
+ 1, argc
--);
620 if ( !wxAppBase::Initialize(argc
, argv
) )
623 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
624 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
626 wxMacCreateNotifierTable() ;
628 UMAShowArrowCursor() ;
633 bool wxApp::OnInitGui()
635 if( !wxAppBase::OnInitGui() )
639 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
641 InstallApplicationEventHandler(
642 GetwxAppEventHandlerUPP(),
643 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
646 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
647 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
648 NewAEEventHandlerUPP(AEHandleODoc
) ,
650 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
651 NewAEEventHandlerUPP(AEHandleOApp
) ,
653 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
654 NewAEEventHandlerUPP(AEHandlePDoc
) ,
656 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
657 NewAEEventHandlerUPP(AEHandleQuit
) ,
660 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
661 NewAEEventHandlerProc(AEHandleODoc
) ,
663 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
664 NewAEEventHandlerProc(AEHandleOApp
) ,
666 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
667 NewAEEventHandlerProc(AEHandlePDoc
) ,
669 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
670 NewAEEventHandlerProc(AEHandleQuit
) ,
677 void wxApp::CleanUp()
679 wxToolTip::RemoveToolTips() ;
681 // One last chance for pending objects to be cleaned up
682 wxTheApp
->DeletePendingObjects();
684 wxMacDestroyNotifierTable() ;
686 delete wxWinMacWindowList
;
687 wxWinMacWindowList
= NULL
;
689 delete wxWinMacControlList
;
690 wxWinMacControlList
= NULL
;
693 # if __option(profile)
694 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
699 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
700 // close shared library resources from here since we don't have
701 // __wxterminate in Mach-O shared libraries
702 wxStAppResource::CloseSharedLibraryResource();
705 UMACleanupToolbox() ;
706 if (s_macCursorRgn
) {
707 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
714 wxAppBase::CleanUp();
717 //----------------------------------------------------------------------
718 // misc initialization stuff
719 //----------------------------------------------------------------------
721 // extern variable for shared library resource id
722 // need to be able to find it with NSLookupAndBindSymbol
723 short gSharedLibraryResource
= kResFileNotOpened
;
725 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
726 CFBundleRef gSharedLibraryBundle
= NULL
;
727 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
729 wxStAppResource::wxStAppResource()
731 m_currentRefNum
= CurResFile() ;
732 if ( gSharedLibraryResource
!= kResFileNotOpened
)
734 UseResFile( gSharedLibraryResource
) ;
738 wxStAppResource::~wxStAppResource()
740 if ( m_currentRefNum
!= kResFileNotOpened
)
742 UseResFile( m_currentRefNum
) ;
746 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
748 gSharedLibraryResource
= kResFileNotOpened
;
750 #ifdef WXMAKINGDLL_CORE
751 if ( initBlock
!= NULL
) {
752 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
753 FSSpec
*fileSpec
= NULL
;
755 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
756 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
758 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
759 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
762 if (fileSpec
!= NULL
) {
763 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
768 // Open the shared library resource file if it is not yet open
771 const char *theLibPath
;
773 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
774 if (gSharedLibraryBundle
!= NULL
) {
775 // wxWindows has been bundled into a framework
776 // load the framework resources
778 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
781 // wxWindows is a simple dynamic shared library
782 // load the resources from the data fork of a separate resource file
786 OSErr theErr
= noErr
;
788 // get the library path
789 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
790 theModule
= NSModuleForSymbol(theSymbol
);
791 theLibPath
= NSLibraryNameForModule(theModule
);
793 // if we call wxLogDebug from here then, as wxTheApp hasn't been
794 // created yet when we're called from wxApp::Initialize(), wxLog
795 // is going to create a default stderr-based log target instead of
796 // the expected normal GUI one -- don't do it, if we really want
797 // to see this message just use fprintf() here
799 wxLogDebug( wxT("wxMac library installation name is '%s'"),
803 // allocate copy to replace .dylib.* extension with .rsrc
804 if (theLibPath
!= NULL
) {
805 theResPath
= theLibPath
;
806 // replace '_core' with '' in case of multi-lib build
807 theResPath
.Replace(wxT("_core"), wxEmptyString
);
808 // replace ".dylib" shared library extension with ".rsrc"
809 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
810 // Find the begining of the filename
811 theName
= theResPath
.AfterLast('/');
814 wxLogDebug( wxT("wxMac resources file name is '%s'"),
815 theResPath
.mb_str() );
818 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
819 if (theErr
!= noErr
) {
820 // try in current directory (using name only)
821 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
824 // open the resource file
825 if (theErr
== noErr
) {
826 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
827 &gSharedLibraryResource
);
829 if (theErr
!= noErr
) {
832 wxT("unable to open wxMac resource file '%s'\n"),
833 theResPath
.mb_str() );
834 #endif // __WXDEBUG__
839 #endif /* __DARWIN__ */
841 #endif /* WXMAKINGDLL_CORE */
844 void wxStAppResource::CloseSharedLibraryResource()
846 #ifdef WXMAKINGDLL_CORE
847 // Close the shared library resource file
848 if (gSharedLibraryResource
!= kResFileNotOpened
) {
850 if (gSharedLibraryBundle
!= NULL
) {
851 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
852 gSharedLibraryResource
);
853 gSharedLibraryBundle
= NULL
;
856 #endif /* __DARWIN__ */
858 CloseResFile(gSharedLibraryResource
);
860 gSharedLibraryResource
= kResFileNotOpened
;
862 #endif /* WXMAKINGDLL_CORE */
865 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
867 // for shared libraries we have to manually get the correct resource
868 // ref num upon initializing and releasing when terminating, therefore
869 // the __wxinitialize and __wxterminate must be used
872 void __sinit(void); /* (generated by linker) */
873 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
874 pascal void __terminate(void);
877 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
879 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
880 return __initialize( theInitBlock
) ;
883 pascal void __wxterminate(void)
885 wxStAppResource::CloseSharedLibraryResource() ;
889 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
893 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
895 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
896 OSStatus err
= noErr
;
899 switch( GetEventClass( event
) )
901 case kEventClassKeyboard
:
904 switch( GetEventKind(event
) )
906 case kEventRawKeyDown
:
907 rec
->what
= keyDown
;
909 case kEventRawKeyRepeat
:
910 rec
->what
= autoKey
;
912 case kEventRawKeyUp
:
915 case kEventRawKeyModifiersChanged
:
916 rec
->what
= nullEvent
;
925 unsigned char charCode
;
927 GetMouse( &rec
->where
) ;
929 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
930 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
931 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
932 rec
->modifiers
= modifiers
;
933 rec
->message
= (keyCode
<< 8 ) + charCode
;
937 case kEventClassTextInput
:
939 switch( GetEventKind( event
) )
941 case kEventTextInputUnicodeForKeyEvent
:
944 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
948 unsigned char charCode
;
950 GetMouse( &rec
->where
) ;
951 rec
->what
= keyDown
;
952 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
953 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
954 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
955 rec
->modifiers
= modifiers
;
956 rec
->message
= (keyCode
<< 8 ) + charCode
;
972 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
974 OSStatus result = eventNotHandledErr ;
977 switch ( GetEventClass( event ) )
979 case kEventClassKeyboard :
980 if ( wxMacConvertEventToRecord( event , &rec ) )
982 wxTheApp->MacHandleModifierEvents( &rec ) ;
983 wxTheApp->MacHandleOneEvent( &rec ) ;
987 case kEventClassTextInput :
988 if ( wxMacConvertEventToRecord( event , &rec ) )
990 wxTheApp->MacHandleModifierEvents( &rec ) ;
991 wxTheApp->MacHandleOneEvent( &rec ) ;
1005 m_printMode
= wxPRINT_WINDOWS
;
1008 m_macCurrentEvent
= NULL
;
1010 m_macCurrentEventHandlerCallRef
= NULL
;
1014 bool wxApp::Initialized()
1022 int wxApp::MainLoop()
1034 void wxApp::ExitMainLoop()
1036 m_keepGoing
= FALSE
;
1039 // Is a message/event pending?
1040 bool wxApp::Pending()
1043 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1047 return EventAvail( everyEvent
, &event
) ;
1051 // Dispatch a message.
1052 void wxApp::Dispatch()
1057 void wxApp::OnIdle(wxIdleEvent
& event
)
1059 wxAppBase::OnIdle(event
);
1061 // If they are pending events, we must process them: pending events are
1062 // either events to the threads other than main or events posted with
1063 // wxPostEvent() functions
1064 wxMacProcessNotifierAndPendingEvents();
1066 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1067 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1070 void wxApp::WakeUpIdle()
1081 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1084 GetTopWindow()->Close(TRUE
);
1087 // Default behaviour: close the application with prompts. The
1088 // user can veto the close, and therefore the end session.
1089 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1093 if (!GetTopWindow()->Close(!event
.CanVeto()))
1098 extern "C" void wxCYield() ;
1104 // Yield to other processes
1106 bool wxApp::Yield(bool onlyIfNeeded
)
1110 if ( !onlyIfNeeded
)
1112 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1121 YieldToAnyThread() ;
1123 // by definition yield should handle all non-processed events
1127 OSStatus status
= noErr
;
1130 s_inReceiveEvent
= true ;
1131 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1132 s_inReceiveEvent
= false ;
1134 if ( status
== eventLoopTimedOutErr
)
1136 // make sure next time the event loop will trigger idle events
1137 sleepTime
= kEventDurationNoWait
;
1139 else if ( status
== eventLoopQuitErr
)
1141 // according to QA1061 this may also occur when a WakeUp Process
1146 MacHandleOneEvent( theEvent
) ;
1147 ReleaseEvent(theEvent
);
1149 } while( status
== noErr
) ;
1153 // having a larger value here leads to large performance slowdowns
1154 // so we cannot give background apps more processor time here
1155 // we do so however having a large sleep value in the main event loop
1158 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1160 MacHandleModifierEvents( &event
) ;
1161 MacHandleOneEvent( &event
);
1162 if ( event
.what
!= kHighLevelEvent
)
1163 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1165 MacHandleModifierEvents( &event
) ;
1168 wxMacProcessNotifierAndPendingEvents() ;
1174 // platform specifics
1176 void wxApp::MacSuspend( bool convertClipboard
)
1179 // we have to deactive the top level windows manually
1181 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1184 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1186 #if 0 // having problems right now with that
1187 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1190 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1192 node
= node
->GetNext();
1195 ::HideFloatingWindows() ;
1197 s_lastMouseDown
= 0 ;
1199 if( convertClipboard
)
1201 MacConvertPrivateToPublicScrap() ;
1205 extern wxList wxModalDialogs
;
1207 void wxApp::MacResume( bool convertClipboard
)
1209 s_lastMouseDown
= 0 ;
1210 if( convertClipboard
)
1212 MacConvertPublicToPrivateScrap() ;
1216 ::ShowFloatingWindows() ;
1217 // raise modal dialogs in case a non modal window was selected to activate the app
1219 wxNode
* node
= wxModalDialogs
.GetFirst();
1222 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1225 node
= node
->GetNext();
1230 void wxApp::MacConvertPrivateToPublicScrap()
1234 void wxApp::MacConvertPublicToPrivateScrap()
1238 void wxApp::MacDoOneEvent()
1243 s_inReceiveEvent
= true ;
1244 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1245 s_inReceiveEvent
= false ;
1246 if ( status
== eventLoopTimedOutErr
)
1248 if ( wxTheApp
->ProcessIdle() )
1249 sleepTime
= kEventDurationNoWait
;
1251 sleepTime
= kEventDurationForever
;
1253 else if ( status
== eventLoopQuitErr
)
1255 // according to QA1061 this may also occur when a WakeUp Process
1260 MacHandleOneEvent( theEvent
) ;
1261 ReleaseEvent(theEvent
);
1262 sleepTime
= kEventDurationNoWait
;
1267 EventMask eventMask
= everyEvent
;
1269 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1271 MacHandleModifierEvents( &event
) ;
1272 MacHandleOneEvent( &event
);
1276 MacHandleModifierEvents( &event
) ;
1278 WindowPtr window
= ::FrontWindow() ;
1280 ::IdleControls( window
) ;
1282 if ( wxTheApp
->ProcessIdle() )
1285 sleepTime
= GetCaretTime() / 2 ;
1287 if ( event
.what
!= kHighLevelEvent
)
1288 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1292 DeletePendingObjects() ;
1293 wxMacProcessNotifierAndPendingEvents() ;
1296 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1299 EventTargetRef theTarget
;
1300 theTarget
= GetEventDispatcherTarget();
1301 m_macCurrentEvent
= evr
;
1302 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1304 EventRecord
* ev
= (EventRecord
*) evr
;
1305 m_macCurrentEvent
= ev
;
1307 wxApp::sm_lastMessageTime
= ev
->when
;
1312 MacHandleMouseDownEvent( ev
) ;
1313 if ( ev
->modifiers
& controlKey
)
1314 s_lastMouseDown
= 2;
1316 s_lastMouseDown
= 1;
1319 if ( s_lastMouseDown
== 2 )
1321 ev
->modifiers
|= controlKey
;
1325 ev
->modifiers
&= ~controlKey
;
1327 MacHandleMouseUpEvent( ev
) ;
1328 s_lastMouseDown
= 0;
1331 MacHandleActivateEvent( ev
) ;
1334 MacHandleUpdateEvent( ev
) ;
1338 MacHandleKeyDownEvent( ev
) ;
1341 MacHandleKeyUpEvent( ev
) ;
1344 MacHandleDiskEvent( ev
) ;
1347 MacHandleOSEvent( ev
) ;
1349 case kHighLevelEvent
:
1350 MacHandleHighLevelEvent( ev
) ;
1356 wxMacProcessNotifierAndPendingEvents() ;
1360 bool s_macIsInModalLoop
= false ;
1362 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1364 EventRecord
* ev
= (EventRecord
*) evr
;
1365 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1367 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1369 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1370 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1371 event
.m_altDown
= ev
->modifiers
& optionKey
;
1372 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1374 event
.m_x
= ev
->where
.h
;
1375 event
.m_y
= ev
->where
.v
;
1376 event
.m_timeStamp
= ev
->when
;
1377 wxWindow
* focus
= wxWindow::FindFocus() ;
1378 event
.SetEventObject(focus
);
1380 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1382 event
.m_keyCode
= WXK_CONTROL
;
1383 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1384 focus
->GetEventHandler()->ProcessEvent( event
) ;
1386 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1388 event
.m_keyCode
= WXK_SHIFT
;
1389 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1390 focus
->GetEventHandler()->ProcessEvent( event
) ;
1392 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1394 event
.m_keyCode
= WXK_ALT
;
1395 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1396 focus
->GetEventHandler()->ProcessEvent( event
) ;
1398 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1400 event
.m_keyCode
= WXK_COMMAND
;
1401 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1402 focus
->GetEventHandler()->ProcessEvent( event
) ;
1404 s_lastModifiers
= ev
->modifiers
;
1408 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1410 // we must avoid reentrancy problems when processing high level events eg printing
1411 bool former
= s_inYield
;
1413 EventRecord
* ev
= (EventRecord
*) evr
;
1414 ::AEProcessAppleEvent( ev
) ;
1415 s_inYield
= former
;
1418 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1420 EventRecord
* ev
= (EventRecord
*) evr
;
1421 wxToolTip::RemoveToolTips() ;
1424 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1425 WindowAttributes frontWindowAttributes
= NULL
;
1427 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1429 short windowPart
= ::FindWindow(ev
->where
, &window
);
1430 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1431 if ( wxPendingDelete
.Member(win
) )
1435 GetQDGlobalsScreenBits( &screenBits
);
1440 if ( s_macIsInModalLoop
)
1446 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1447 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1448 s_lastMouseDown
= 0;
1452 SystemClick( ev
, window
) ;
1453 s_lastMouseDown
= 0;
1456 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1462 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1467 Point pt
= { 0, 0 } ;
1468 SetPortWindowPort(window
) ;
1469 LocalToGlobal( &pt
) ;
1471 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1472 -1 , wxSIZE_USE_EXISTING
);
1474 s_lastMouseDown
= 0;
1478 if (TrackGoAway(window
, ev
->where
))
1483 s_lastMouseDown
= 0;
1487 Rect newContentRect
;
1488 Rect constraintRect
;
1489 constraintRect
.top
= win
->GetMinHeight() ;
1490 if ( constraintRect
.top
== -1 )
1491 constraintRect
.top
= 0 ;
1492 constraintRect
.left
= win
->GetMinWidth() ;
1493 if ( constraintRect
.left
== -1 )
1494 constraintRect
.left
= 0 ;
1495 constraintRect
.right
= win
->GetMaxWidth() ;
1496 if ( constraintRect
.right
== -1 )
1497 constraintRect
.right
= 32000 ;
1498 constraintRect
.bottom
= win
->GetMaxHeight() ;
1499 if ( constraintRect
.bottom
== -1 )
1500 constraintRect
.bottom
= 32000 ;
1502 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1503 &constraintRect
, &newContentRect
) ;
1506 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1507 newContentRect
.right
- newContentRect
.left
,
1508 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1510 s_lastMouseDown
= 0;
1515 if (TrackBox(window
, ev
->where
, windowPart
))
1517 // TODO setup size event
1518 ZoomWindow( window
, windowPart
, false ) ;
1524 Point pt
= { 0, 0 } ;
1525 SetPortWindowPort(window
) ;
1526 LocalToGlobal( &pt
) ;
1529 GetWindowPortBounds(window
, &tempRect
) ;
1530 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1531 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1534 s_lastMouseDown
= 0;
1536 case inCollapseBox
:
1537 // TODO setup size event
1538 s_lastMouseDown
= 0;
1545 SetPortWindowPort(window
) ;
1548 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1550 if ( s_macIsInModalLoop
)
1554 else if ( UMAIsWindowFloating( window
) )
1557 win
->MacMouseDown( ev
, windowPart
) ;
1561 // Activate window first
1562 ::SelectWindow( window
) ;
1566 win
->MacMouseDown( ev
, windowPart
) ;
1572 win
->MacMouseDown( ev
, windowPart
) ;
1580 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1582 EventRecord
* ev
= (EventRecord
*) evr
;
1585 short windowPart
= inNoWindow
;
1586 if ( wxTheApp
->s_captureWindow
)
1588 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1589 windowPart
= inContent
;
1593 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1604 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1606 win
->MacMouseUp( ev
, windowPart
) ;
1614 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1615 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1620 case kHomeCharCode
:
1623 case kEnterCharCode
:
1624 retval
= WXK_RETURN
;
1629 case kHelpCharCode
:
1632 case kBackspaceCharCode
:
1638 case kPageUpCharCode
:
1639 retval
= WXK_PAGEUP
;
1641 case kPageDownCharCode
:
1642 retval
= WXK_PAGEDOWN
;
1644 case kReturnCharCode
:
1645 retval
= WXK_RETURN
;
1647 case kFunctionKeyCharCode
:
1699 case kEscapeCharCode
:
1700 retval
= WXK_ESCAPE
;
1702 case kLeftArrowCharCode
:
1705 case kRightArrowCharCode
:
1706 retval
= WXK_RIGHT
;
1708 case kUpArrowCharCode
:
1711 case kDownArrowCharCode
:
1714 case kDeleteCharCode
:
1715 retval
= WXK_DELETE
;
1724 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1726 EventRecord
* ev
= (EventRecord
*) evr
;
1727 wxToolTip::RemoveToolTips() ;
1729 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1730 if ( HiWord( menuresult
) )
1732 if ( !s_macIsInModalLoop
)
1733 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1737 wxWindow
* focus
= wxWindow::FindFocus() ;
1739 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1742 // we must handle control keys the other way round, otherwise text content is updated too late
1743 // has not been handled -> perform default
1744 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1745 if ( control
&& control
->GetMacControl() != NULL
)
1749 keychar
= short(ev
->message
& charCodeMask
);
1750 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1751 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1758 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1760 EventRecord
* ev
= (EventRecord
*) evr
;
1761 wxToolTip::RemoveToolTips() ;
1763 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1764 if ( HiWord( menuresult
) )
1769 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1775 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1782 keychar
= short(keymessage
& charCodeMask
);
1783 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1785 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1787 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1788 // and look at the character after
1790 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1791 keychar
= short(keyInfo
& charCodeMask
);
1792 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1794 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1795 long realkeyval
= keyval
;
1796 if ( keyval
== keychar
)
1798 // 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)
1799 realkeyval
= short(keymessage
& charCodeMask
) ;
1800 keyval
= wxToupper( keyval
) ;
1803 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1804 bool handled
= false ;
1805 event
.m_shiftDown
= modifiers
& shiftKey
;
1806 event
.m_controlDown
= modifiers
& controlKey
;
1807 event
.m_altDown
= modifiers
& optionKey
;
1808 event
.m_metaDown
= modifiers
& cmdKey
;
1809 event
.m_keyCode
= keyval
;
1813 event
.m_timeStamp
= when
;
1814 event
.SetEventObject(focus
);
1815 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1816 if ( handled
&& event
.GetSkipped() )
1823 wxWindow
*ancestor
= focus
;
1826 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1829 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1830 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1833 if (ancestor
->IsTopLevel())
1835 ancestor
= ancestor
->GetParent();
1838 #endif // wxUSE_ACCEL
1842 event
.Skip( FALSE
) ;
1843 event
.SetEventType( wxEVT_CHAR
) ;
1845 event
.m_keyCode
= realkeyval
;
1847 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1848 if ( handled
&& event
.GetSkipped() )
1852 (keyval
== WXK_TAB
) &&
1853 // CS: copied the change below from wxGTK
1854 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1855 // have this style, yet choose not to process this particular TAB in which
1856 // case TAB must still work as a navigational character
1858 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1860 (focus
->GetParent()) &&
1861 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1863 wxNavigationKeyEvent new_event
;
1864 new_event
.SetEventObject( focus
);
1865 new_event
.SetDirection( !event
.ShiftDown() );
1866 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1867 new_event
.SetWindowChange( event
.ControlDown() );
1868 new_event
.SetCurrentFocus( focus
);
1869 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1870 if ( handled
&& new_event
.GetSkipped() )
1873 // backdoor handler for default return and command escape
1874 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1876 // if window is not having a focus still testing for default enter or cancel
1877 // TODO add the UMA version for ActiveNonFloatingWindow
1878 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1881 if ( keyval
== WXK_RETURN
)
1883 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1885 if ( def
&& def
->IsEnabled() )
1887 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1888 event
.SetEventObject(def
);
1889 def
->Command(event
);
1893 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1894 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1896 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1897 new_event
.SetEventObject( focus
);
1898 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1905 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1912 keychar
= short(keymessage
& charCodeMask
);
1913 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1914 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1916 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1917 // and look at the character after
1919 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1920 keychar
= short(keyInfo
& charCodeMask
);
1921 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1923 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1925 if ( keyval
== keychar
)
1927 keyval
= wxToupper( keyval
) ;
1929 bool handled
= false ;
1931 wxKeyEvent
event(wxEVT_KEY_UP
);
1932 event
.m_shiftDown
= modifiers
& shiftKey
;
1933 event
.m_controlDown
= modifiers
& controlKey
;
1934 event
.m_altDown
= modifiers
& optionKey
;
1935 event
.m_metaDown
= modifiers
& cmdKey
;
1936 event
.m_keyCode
= keyval
;
1940 event
.m_timeStamp
= when
;
1941 event
.SetEventObject(focus
);
1942 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1948 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1950 EventRecord
* ev
= (EventRecord
*) evr
;
1951 WindowRef window
= (WindowRef
) ev
->message
;
1954 bool activate
= (ev
->modifiers
& activeFlag
) ;
1955 WindowClass wclass
;
1956 ::GetWindowClass ( window
, &wclass
) ;
1957 if ( wclass
== kFloatingWindowClass
)
1959 // if it is a floater we activate/deactivate the front non-floating window instead
1960 window
= ::FrontNonFloatingWindow() ;
1962 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1964 win
->MacActivate( ev
->when
, activate
) ;
1968 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1970 EventRecord
* ev
= (EventRecord
*) evr
;
1971 WindowRef window
= (WindowRef
) ev
->message
;
1972 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1975 if ( !wxPendingDelete
.Member(win
) )
1976 win
->MacUpdate( ev
->when
) ;
1980 // since there is no way of telling this foreign window to update itself
1981 // we have to invalidate the update region otherwise we keep getting the same
1982 // event over and over again
1983 BeginUpdate( window
) ;
1984 EndUpdate( window
) ;
1988 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1990 EventRecord
* ev
= (EventRecord
*) evr
;
1991 if ( HiWord( ev
->message
) != noErr
)
1995 SetPt( &point
, 100 , 100 ) ;
1997 err
= DIBadMount( point
, ev
->message
) ;
1998 wxASSERT( err
== noErr
) ;
2002 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2004 EventRecord
* ev
= (EventRecord
*) evr
;
2005 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2007 case suspendResumeMessage
:
2009 bool isResuming
= ev
->message
& resumeFlag
;
2010 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2012 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2015 WindowRef oldFrontWindow
= NULL
;
2016 WindowRef newFrontWindow
= NULL
;
2018 // in case we don't take care of activating ourselves, we have to synchronize
2019 // our idea of the active window with the process manager's - which it already activated
2021 if ( !doesActivate
)
2022 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2024 MacResume( convertClipboard
) ;
2026 newFrontWindow
= ::FrontNonFloatingWindow() ;
2028 if ( oldFrontWindow
)
2030 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2032 win
->MacActivate( ev
->when
, false ) ;
2034 if ( newFrontWindow
)
2036 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2038 win
->MacActivate( ev
->when
, true ) ;
2043 MacSuspend( convertClipboard
) ;
2047 case mouseMovedMessage
:
2051 wxWindow
* currentMouseWindow
= NULL
;
2053 if (s_captureWindow
)
2055 currentMouseWindow
= s_captureWindow
;
2059 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2060 ¤tMouseWindow
) ;
2063 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2065 wxMouseEvent event
;
2067 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2068 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2070 event
.m_leftDown
= isDown
&& !controlDown
;
2071 event
.m_middleDown
= FALSE
;
2072 event
.m_rightDown
= isDown
&& controlDown
;
2073 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2074 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2075 event
.m_altDown
= ev
->modifiers
& optionKey
;
2076 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2077 event
.m_x
= ev
->where
.h
;
2078 event
.m_y
= ev
->where
.v
;
2079 event
.m_timeStamp
= ev
->when
;
2080 event
.SetEventObject(this);
2082 if ( wxWindow::s_lastMouseWindow
)
2084 wxMouseEvent
eventleave(event
);
2085 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2086 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2087 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2089 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2091 if ( currentMouseWindow
)
2093 wxMouseEvent
evententer(event
);
2094 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2095 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2096 evententer
.SetEventObject( currentMouseWindow
) ;
2097 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2099 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2102 short windowPart
= inNoWindow
;
2104 if ( s_captureWindow
)
2106 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2107 windowPart
= inContent
;
2111 windowPart
= ::FindWindow(ev
->where
, &window
);
2118 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2120 win
->MacMouseMoved( ev
, windowPart
) ;
2127 UMAShowArrowCursor();
2137 UMAShowArrowCursor();
2148 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2152 wxWindow
* currentMouseWindow
= NULL
;
2154 if (s_captureWindow
)
2156 currentMouseWindow
= s_captureWindow
;
2160 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2163 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2165 wxMouseEvent event
;
2167 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2168 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2170 event
.m_leftDown
= isDown
&& !controlDown
;
2172 event
.m_middleDown
= FALSE
;
2173 event
.m_rightDown
= isDown
&& controlDown
;
2175 event
.m_shiftDown
= modifiers
& shiftKey
;
2176 event
.m_controlDown
= modifiers
& controlKey
;
2177 event
.m_altDown
= modifiers
& optionKey
;
2178 event
.m_metaDown
= modifiers
& cmdKey
;
2182 event
.m_timeStamp
= timestamp
;
2184 if ( wxWindow::s_lastMouseWindow
)
2186 wxMouseEvent
eventleave(event
);
2187 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2188 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2189 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2192 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2193 #endif // wxUSE_TOOLTIPS
2194 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2196 if ( currentMouseWindow
)
2198 wxMouseEvent
evententer(event
);
2199 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2200 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2201 evententer
.SetEventObject( currentMouseWindow
) ;
2203 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2204 #endif // wxUSE_TOOLTIPS
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 Point pt
= { y
, x
} ;
2220 windowPart
= ::FindWindow(pt
, &window
);
2227 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2229 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2236 UMAShowArrowCursor();
2246 UMAShowArrowCursor();
2253 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2255 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2256 wxFrame
* frame
= mbar
->GetFrame();
2257 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2260 frame
->ProcessCommand(id
);
2265 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2268 return; // no menu item selected
2270 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2273 Str255 deskAccessoryName
;
2276 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2277 GetPort(&savedPort
);
2278 OpenDeskAcc(deskAccessoryName
);
2285 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2286 MacHandleMenuCommand( id
) ;