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() ;
234 //----------------------------------------------------------------------
235 // Support Routines linking the Mac...File Calls to the Document Manager
236 //----------------------------------------------------------------------
238 void wxApp::MacOpenFile(const wxString
& fileName
)
240 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
242 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
245 void wxApp::MacPrintFile(const wxString
& fileName
)
247 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
250 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
253 wxView
* view
= doc
->GetFirstView() ;
256 wxPrintout
*printout
= view
->OnCreatePrintout();
260 printer
.Print(view
->GetFrame(), printout
, TRUE
);
266 doc
->DeleteAllViews();
267 dm
->RemoveDocument(doc
) ;
273 void wxApp::MacNewFile()
277 //----------------------------------------------------------------------
278 // Carbon Event Handler
279 //----------------------------------------------------------------------
283 static const EventTypeSpec eventList
[] =
285 { kEventClassCommand
, kEventProcessCommand
} ,
286 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
288 { kEventClassMenu
, kEventMenuOpening
},
289 { kEventClassMenu
, kEventMenuClosed
},
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
} ,
299 { kEventClassMouse
, kEventMouseMoved
} ,
300 { kEventClassMouse
, kEventMouseUp
} ,
301 { kEventClassMouse
, kEventMouseDragged
} ,
305 static pascal OSStatus
306 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
308 // FIXME: this doesn't work for multiple windows
309 wxWindow
*win
= wxTheApp
->GetTopWindow();
312 // VZ: we could find the menu from its handle here by examining all
313 // the menus in the menu bar recursively but knowing that neither
314 // wxMSW nor wxGTK do it why bother...
318 GetEventParameter(event
,
319 kEventParamDirectObject
,
321 sizeof(menuRef
), NULL
,
325 wxMenuEvent
wxevent(GetEventKind(event
) == kEventMenuOpening
328 wxevent
.SetEventObject(win
);
330 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
333 return eventNotHandledErr
;
336 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
337 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
338 // deal with clicks in the menu bar explicitely
340 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
342 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
344 OSStatus result
= eventNotHandledErr
;
347 UInt32 modifiers
= 0;
348 EventMouseButton button
= 0 ;
351 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
352 sizeof( Point
), NULL
, &point
);
353 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
354 sizeof( UInt32
), NULL
, &modifiers
);
355 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
356 sizeof( EventMouseButton
), NULL
, &button
);
357 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
358 sizeof( UInt32
), NULL
, &click
);
360 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
361 modifiers
+= btnState
;
364 switch( GetEventKind(event
) )
366 case kEventMouseDown
:
370 short windowPart
= ::FindWindow(point
, &window
);
372 if ( windowPart
== inMenuBar
)
374 MenuSelect( point
) ;
379 case kEventMouseDragged
:
382 if ( wxTheApp
->s_captureWindow
)
383 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
386 case kEventMouseMoved
:
388 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
398 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
400 OSStatus result
= eventNotHandledErr
;
404 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
405 sizeof( HICommand
), NULL
, &command
);
407 MenuCommand id
= command
.commandID
;
408 if ( id
== kHICommandPreferences
)
409 id
= wxApp::s_macPreferencesMenuItemId
;
411 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
412 wxMenu
* menu
= NULL
;
413 wxMenuItem
* item
= NULL
;
417 item
= mbar
->FindItem( id
, &menu
) ;
418 // it is not 100 % sure that an menu of id 0 is really ours, safety check
419 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
426 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
429 switch( GetEventKind( event
) )
431 case kEventProcessCommand
:
433 if (item
->IsCheckable())
435 item
->Check( !item
->IsChecked() ) ;
438 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
442 case kEventCommandUpdateStatus
:
443 // eventually trigger an updateui round
453 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
455 OSStatus result
= eventNotHandledErr
;
456 switch ( GetEventKind( event
) )
458 case kEventAppActivated
:
461 wxTheApp
->MacResume( true ) ;
465 case kEventAppDeactivated
:
468 wxTheApp
->MacSuspend( true ) ;
478 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
480 OSStatus result
= eventNotHandledErr
;
481 switch( GetEventClass( event
) )
483 case kEventClassCommand
:
484 result
= CommandEventHandler( handler
, event
, data
) ;
486 case kEventClassApplication
:
487 result
= ApplicationEventHandler( handler
, event
, data
) ;
489 case kEventClassMenu
:
490 result
= MenuEventHandler( handler
, event
, data
) ;
492 case kEventClassMouse
:
493 result
= MouseEventHandler( handler
, event
, data
) ;
495 case kEventClassAppleEvent
:
498 wxMacConvertEventToRecord( event
, &rec
) ;
499 result
= AEProcessAppleEvent( &rec
) ;
509 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
513 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
514 // we know it's there ;-)
515 WXIMPORT
char std::__throws_bad_alloc
;
518 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
524 UMAInitToolbox( 4 ) ;
525 SetEventMask( everyEvent
) ;
526 UMAShowWatchCursor() ;
528 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
529 // open shared library resources from here since we don't have
530 // __wxinitialize in Mach-O shared libraries
531 wxStAppResource::OpenSharedLibraryResource(NULL
);
535 // test the minimal configuration necessary
541 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
543 error
= kMacSTRWrongMachine
;
545 else if (theMachine
< gestaltMacPlus
)
547 error
= kMacSTRWrongMachine
;
549 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
551 error
= kMacSTROldSystem
;
553 else if ( theSystem
< 0x0860 )
555 error
= kMacSTROldSystem
;
557 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
559 error
= kMacSTRSmallSize
;
565 if ( !UMAHasAppearance() )
567 error = kMacSTRNoPre8Yet ;
573 // if we encountered any problems so far, give the error code and exit immediately
577 wxStAppResource resload
;
581 GetIndString(message
, 128, error
);
582 UMAShowArrowCursor() ;
583 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
584 itemHit
= Alert(128, nil
);
589 # if __option(profile)
590 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
595 // now avoid exceptions thrown for new (bad_alloc)
596 // FIXME CS for some changes outside wxMac does not compile anymore
598 std::__throws_bad_alloc
= 0 ;
603 s_macCursorRgn
= ::NewRgn() ;
605 // Mac OS X passes a process serial number command line argument when
606 // the application is launched from the Finder. This argument must be
607 // removed from the command line arguments before being handled by the
608 // application (otherwise applications would need to handle it)
611 static const wxChar
*ARG_PSN
= _T("-psn_");
612 if ( wxStrncmp(argv
[1], ARG_PSN
, sizeof(ARG_PSN
) - 1) == 0 )
614 // remove this argument
615 memmove(argv
, argv
+ 1, argc
--);
619 if ( !wxAppBase::Initialize(argc
, argv
) )
622 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
623 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
625 wxMacCreateNotifierTable() ;
627 UMAShowArrowCursor() ;
632 bool wxApp::OnInitGui()
634 if( !wxAppBase::OnInitGui() )
638 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
640 InstallApplicationEventHandler(
641 GetwxAppEventHandlerUPP(),
642 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
645 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
646 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
647 NewAEEventHandlerUPP(AEHandleODoc
) ,
649 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
650 NewAEEventHandlerUPP(AEHandleOApp
) ,
652 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
653 NewAEEventHandlerUPP(AEHandlePDoc
) ,
655 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
656 NewAEEventHandlerUPP(AEHandleQuit
) ,
659 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
660 NewAEEventHandlerProc(AEHandleODoc
) ,
662 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
663 NewAEEventHandlerProc(AEHandleOApp
) ,
665 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
666 NewAEEventHandlerProc(AEHandlePDoc
) ,
668 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
669 NewAEEventHandlerProc(AEHandleQuit
) ,
676 void wxApp::CleanUp()
678 wxToolTip::RemoveToolTips() ;
680 // One last chance for pending objects to be cleaned up
681 wxTheApp
->DeletePendingObjects();
683 wxMacDestroyNotifierTable() ;
685 delete wxWinMacWindowList
;
686 wxWinMacWindowList
= NULL
;
688 delete wxWinMacControlList
;
689 wxWinMacControlList
= NULL
;
692 # if __option(profile)
693 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
698 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
699 // close shared library resources from here since we don't have
700 // __wxterminate in Mach-O shared libraries
701 wxStAppResource::CloseSharedLibraryResource();
704 UMACleanupToolbox() ;
705 if (s_macCursorRgn
) {
706 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
713 wxAppBase::CleanUp();
716 //----------------------------------------------------------------------
717 // misc initialization stuff
718 //----------------------------------------------------------------------
720 // extern variable for shared library resource id
721 // need to be able to find it with NSLookupAndBindSymbol
722 short gSharedLibraryResource
= kResFileNotOpened
;
724 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
725 CFBundleRef gSharedLibraryBundle
= NULL
;
726 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
728 wxStAppResource::wxStAppResource()
730 m_currentRefNum
= CurResFile() ;
731 if ( gSharedLibraryResource
!= kResFileNotOpened
)
733 UseResFile( gSharedLibraryResource
) ;
737 wxStAppResource::~wxStAppResource()
739 if ( m_currentRefNum
!= kResFileNotOpened
)
741 UseResFile( m_currentRefNum
) ;
745 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
747 gSharedLibraryResource
= kResFileNotOpened
;
749 #ifdef WXMAKINGDLL_CORE
750 if ( initBlock
!= NULL
) {
751 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
752 FSSpec
*fileSpec
= NULL
;
754 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
755 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
757 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
758 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
761 if (fileSpec
!= NULL
) {
762 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
767 // Open the shared library resource file if it is not yet open
770 const char *theLibPath
;
772 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
773 if (gSharedLibraryBundle
!= NULL
) {
774 // wxWindows has been bundled into a framework
775 // load the framework resources
777 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
780 // wxWindows is a simple dynamic shared library
781 // load the resources from the data fork of a separate resource file
785 OSErr theErr
= noErr
;
787 // get the library path
788 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
789 theModule
= NSModuleForSymbol(theSymbol
);
790 theLibPath
= NSLibraryNameForModule(theModule
);
792 // if we call wxLogDebug from here then, as wxTheApp hasn't been
793 // created yet when we're called from wxApp::Initialize(), wxLog
794 // is going to create a default stderr-based log target instead of
795 // the expected normal GUI one -- don't do it, if we really want
796 // to see this message just use fprintf() here
798 wxLogDebug( wxT("wxMac library installation name is '%s'"),
802 // allocate copy to replace .dylib.* extension with .rsrc
803 if (theLibPath
!= NULL
) {
804 theResPath
= theLibPath
;
805 // replace '_core' with '' in case of multi-lib build
806 theResPath
.Replace(wxT("_core"), wxEmptyString
);
807 // replace ".dylib" shared library extension with ".rsrc"
808 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
809 // Find the begining of the filename
810 theName
= theResPath
.AfterLast('/');
813 wxLogDebug( wxT("wxMac resources file name is '%s'"),
814 theResPath
.mb_str() );
817 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
818 if (theErr
!= noErr
) {
819 // try in current directory (using name only)
820 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
823 // open the resource file
824 if (theErr
== noErr
) {
825 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
826 &gSharedLibraryResource
);
828 if (theErr
!= noErr
) {
831 wxT("unable to open wxMac resource file '%s'\n"),
832 theResPath
.mb_str() );
833 #endif // __WXDEBUG__
838 #endif /* __DARWIN__ */
840 #endif /* WXMAKINGDLL_CORE */
843 void wxStAppResource::CloseSharedLibraryResource()
845 #ifdef WXMAKINGDLL_CORE
846 // Close the shared library resource file
847 if (gSharedLibraryResource
!= kResFileNotOpened
) {
849 if (gSharedLibraryBundle
!= NULL
) {
850 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
851 gSharedLibraryResource
);
852 gSharedLibraryBundle
= NULL
;
855 #endif /* __DARWIN__ */
857 CloseResFile(gSharedLibraryResource
);
859 gSharedLibraryResource
= kResFileNotOpened
;
861 #endif /* WXMAKINGDLL_CORE */
864 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
866 // for shared libraries we have to manually get the correct resource
867 // ref num upon initializing and releasing when terminating, therefore
868 // the __wxinitialize and __wxterminate must be used
871 void __sinit(void); /* (generated by linker) */
872 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
873 pascal void __terminate(void);
876 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
878 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
879 return __initialize( theInitBlock
) ;
882 pascal void __wxterminate(void)
884 wxStAppResource::CloseSharedLibraryResource() ;
888 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
892 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
894 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
895 OSStatus err
= noErr
;
898 switch( GetEventClass( event
) )
900 case kEventClassKeyboard
:
903 switch( GetEventKind(event
) )
905 case kEventRawKeyDown
:
906 rec
->what
= keyDown
;
908 case kEventRawKeyRepeat
:
909 rec
->what
= autoKey
;
911 case kEventRawKeyUp
:
914 case kEventRawKeyModifiersChanged
:
915 rec
->what
= nullEvent
;
924 unsigned char charCode
;
926 GetMouse( &rec
->where
) ;
928 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
929 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
930 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
931 rec
->modifiers
= modifiers
;
932 rec
->message
= (keyCode
<< 8 ) + charCode
;
936 case kEventClassTextInput
:
938 switch( GetEventKind( event
) )
940 case kEventTextInputUnicodeForKeyEvent
:
943 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
947 unsigned char charCode
;
949 GetMouse( &rec
->where
) ;
950 rec
->what
= keyDown
;
951 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
952 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
953 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
954 rec
->modifiers
= modifiers
;
955 rec
->message
= (keyCode
<< 8 ) + charCode
;
971 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
973 OSStatus result = eventNotHandledErr ;
976 switch ( GetEventClass( event ) )
978 case kEventClassKeyboard :
979 if ( wxMacConvertEventToRecord( event , &rec ) )
981 wxTheApp->MacHandleModifierEvents( &rec ) ;
982 wxTheApp->MacHandleOneEvent( &rec ) ;
986 case kEventClassTextInput :
987 if ( wxMacConvertEventToRecord( event , &rec ) )
989 wxTheApp->MacHandleModifierEvents( &rec ) ;
990 wxTheApp->MacHandleOneEvent( &rec ) ;
1004 m_printMode
= wxPRINT_WINDOWS
;
1007 m_macCurrentEvent
= NULL
;
1009 m_macCurrentEventHandlerCallRef
= NULL
;
1013 bool wxApp::Initialized()
1021 int wxApp::MainLoop()
1033 void wxApp::ExitMainLoop()
1035 m_keepGoing
= FALSE
;
1038 // Is a message/event pending?
1039 bool wxApp::Pending()
1042 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1046 return EventAvail( everyEvent
, &event
) ;
1050 // Dispatch a message.
1051 bool wxApp::Dispatch()
1058 void wxApp::OnIdle(wxIdleEvent
& event
)
1060 wxAppBase::OnIdle(event
);
1062 // If they are pending events, we must process them: pending events are
1063 // either events to the threads other than main or events posted with
1064 // wxPostEvent() functions
1065 wxMacProcessNotifierAndPendingEvents();
1067 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1068 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1071 void wxApp::WakeUpIdle()
1082 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1085 GetTopWindow()->Close(TRUE
);
1088 // Default behaviour: close the application with prompts. The
1089 // user can veto the close, and therefore the end session.
1090 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1094 if (!GetTopWindow()->Close(!event
.CanVeto()))
1099 extern "C" void wxCYield() ;
1105 // Yield to other processes
1107 bool wxApp::Yield(bool onlyIfNeeded
)
1111 if ( !onlyIfNeeded
)
1113 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1122 YieldToAnyThread() ;
1124 // by definition yield should handle all non-processed events
1128 OSStatus status
= noErr
;
1131 s_inReceiveEvent
= true ;
1132 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1133 s_inReceiveEvent
= false ;
1135 if ( status
== eventLoopTimedOutErr
)
1137 // make sure next time the event loop will trigger idle events
1138 sleepTime
= kEventDurationNoWait
;
1140 else if ( status
== eventLoopQuitErr
)
1142 // according to QA1061 this may also occur when a WakeUp Process
1147 MacHandleOneEvent( theEvent
) ;
1148 ReleaseEvent(theEvent
);
1150 } while( status
== noErr
) ;
1154 // having a larger value here leads to large performance slowdowns
1155 // so we cannot give background apps more processor time here
1156 // we do so however having a large sleep value in the main event loop
1159 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1161 MacHandleModifierEvents( &event
) ;
1162 MacHandleOneEvent( &event
);
1163 if ( event
.what
!= kHighLevelEvent
)
1164 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1166 MacHandleModifierEvents( &event
) ;
1169 wxMacProcessNotifierAndPendingEvents() ;
1175 // platform specifics
1177 void wxApp::MacSuspend( bool convertClipboard
)
1180 // we have to deactive the top level windows manually
1182 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1185 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1187 #if 0 // having problems right now with that
1188 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1191 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1193 node
= node
->GetNext();
1196 ::HideFloatingWindows() ;
1198 s_lastMouseDown
= 0 ;
1200 if( convertClipboard
)
1202 MacConvertPrivateToPublicScrap() ;
1206 extern wxList wxModalDialogs
;
1208 void wxApp::MacResume( bool convertClipboard
)
1210 s_lastMouseDown
= 0 ;
1211 if( convertClipboard
)
1213 MacConvertPublicToPrivateScrap() ;
1217 ::ShowFloatingWindows() ;
1218 // raise modal dialogs in case a non modal window was selected to activate the app
1220 wxNode
* node
= wxModalDialogs
.GetFirst();
1223 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1226 node
= node
->GetNext();
1231 void wxApp::MacConvertPrivateToPublicScrap()
1235 void wxApp::MacConvertPublicToPrivateScrap()
1239 void wxApp::MacDoOneEvent()
1244 s_inReceiveEvent
= true ;
1245 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1246 s_inReceiveEvent
= false ;
1247 if ( status
== eventLoopTimedOutErr
)
1249 if ( wxTheApp
->ProcessIdle() )
1250 sleepTime
= kEventDurationNoWait
;
1252 sleepTime
= kEventDurationForever
;
1254 else if ( status
== eventLoopQuitErr
)
1256 // according to QA1061 this may also occur when a WakeUp Process
1261 MacHandleOneEvent( theEvent
) ;
1262 ReleaseEvent(theEvent
);
1263 sleepTime
= kEventDurationNoWait
;
1268 EventMask eventMask
= everyEvent
;
1270 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1272 MacHandleModifierEvents( &event
) ;
1273 MacHandleOneEvent( &event
);
1277 MacHandleModifierEvents( &event
) ;
1279 WindowPtr window
= ::FrontWindow() ;
1281 ::IdleControls( window
) ;
1283 if ( wxTheApp
->ProcessIdle() )
1286 sleepTime
= GetCaretTime() / 2 ;
1288 if ( event
.what
!= kHighLevelEvent
)
1289 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1293 DeletePendingObjects() ;
1294 wxMacProcessNotifierAndPendingEvents() ;
1297 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1300 EventTargetRef theTarget
;
1301 theTarget
= GetEventDispatcherTarget();
1302 m_macCurrentEvent
= evr
;
1303 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1305 EventRecord
* ev
= (EventRecord
*) evr
;
1306 m_macCurrentEvent
= ev
;
1308 wxApp::sm_lastMessageTime
= ev
->when
;
1313 MacHandleMouseDownEvent( ev
) ;
1314 if ( ev
->modifiers
& controlKey
)
1315 s_lastMouseDown
= 2;
1317 s_lastMouseDown
= 1;
1320 if ( s_lastMouseDown
== 2 )
1322 ev
->modifiers
|= controlKey
;
1326 ev
->modifiers
&= ~controlKey
;
1328 MacHandleMouseUpEvent( ev
) ;
1329 s_lastMouseDown
= 0;
1332 MacHandleActivateEvent( ev
) ;
1335 MacHandleUpdateEvent( ev
) ;
1339 MacHandleKeyDownEvent( ev
) ;
1342 MacHandleKeyUpEvent( ev
) ;
1345 MacHandleDiskEvent( ev
) ;
1348 MacHandleOSEvent( ev
) ;
1350 case kHighLevelEvent
:
1351 MacHandleHighLevelEvent( ev
) ;
1357 wxMacProcessNotifierAndPendingEvents() ;
1361 bool s_macIsInModalLoop
= false ;
1363 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1365 EventRecord
* ev
= (EventRecord
*) evr
;
1366 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1368 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1370 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1371 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1372 event
.m_altDown
= ev
->modifiers
& optionKey
;
1373 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1375 event
.m_x
= ev
->where
.h
;
1376 event
.m_y
= ev
->where
.v
;
1377 event
.m_timeStamp
= ev
->when
;
1378 wxWindow
* focus
= wxWindow::FindFocus() ;
1379 event
.SetEventObject(focus
);
1381 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1383 event
.m_keyCode
= WXK_CONTROL
;
1384 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1385 focus
->GetEventHandler()->ProcessEvent( event
) ;
1387 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1389 event
.m_keyCode
= WXK_SHIFT
;
1390 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1391 focus
->GetEventHandler()->ProcessEvent( event
) ;
1393 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1395 event
.m_keyCode
= WXK_ALT
;
1396 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1397 focus
->GetEventHandler()->ProcessEvent( event
) ;
1399 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1401 event
.m_keyCode
= WXK_COMMAND
;
1402 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1403 focus
->GetEventHandler()->ProcessEvent( event
) ;
1405 s_lastModifiers
= ev
->modifiers
;
1409 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1411 // we must avoid reentrancy problems when processing high level events eg printing
1412 bool former
= s_inYield
;
1414 EventRecord
* ev
= (EventRecord
*) evr
;
1415 ::AEProcessAppleEvent( ev
) ;
1416 s_inYield
= former
;
1419 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1421 EventRecord
* ev
= (EventRecord
*) evr
;
1422 wxToolTip::RemoveToolTips() ;
1425 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1426 WindowAttributes frontWindowAttributes
= NULL
;
1428 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1430 short windowPart
= ::FindWindow(ev
->where
, &window
);
1431 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1432 if ( wxPendingDelete
.Member(win
) )
1436 GetQDGlobalsScreenBits( &screenBits
);
1441 if ( s_macIsInModalLoop
)
1447 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1448 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1449 s_lastMouseDown
= 0;
1453 SystemClick( ev
, window
) ;
1454 s_lastMouseDown
= 0;
1457 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1463 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1468 Point pt
= { 0, 0 } ;
1469 SetPortWindowPort(window
) ;
1470 LocalToGlobal( &pt
) ;
1472 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1473 -1 , wxSIZE_USE_EXISTING
);
1475 s_lastMouseDown
= 0;
1479 if (TrackGoAway(window
, ev
->where
))
1484 s_lastMouseDown
= 0;
1488 Rect newContentRect
;
1489 Rect constraintRect
;
1490 constraintRect
.top
= win
->GetMinHeight() ;
1491 if ( constraintRect
.top
== -1 )
1492 constraintRect
.top
= 0 ;
1493 constraintRect
.left
= win
->GetMinWidth() ;
1494 if ( constraintRect
.left
== -1 )
1495 constraintRect
.left
= 0 ;
1496 constraintRect
.right
= win
->GetMaxWidth() ;
1497 if ( constraintRect
.right
== -1 )
1498 constraintRect
.right
= 32000 ;
1499 constraintRect
.bottom
= win
->GetMaxHeight() ;
1500 if ( constraintRect
.bottom
== -1 )
1501 constraintRect
.bottom
= 32000 ;
1503 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1504 &constraintRect
, &newContentRect
) ;
1507 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1508 newContentRect
.right
- newContentRect
.left
,
1509 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1511 s_lastMouseDown
= 0;
1516 if (TrackBox(window
, ev
->where
, windowPart
))
1518 // TODO setup size event
1519 ZoomWindow( window
, windowPart
, false ) ;
1525 Point pt
= { 0, 0 } ;
1526 SetPortWindowPort(window
) ;
1527 LocalToGlobal( &pt
) ;
1530 GetWindowPortBounds(window
, &tempRect
) ;
1531 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1532 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1535 s_lastMouseDown
= 0;
1537 case inCollapseBox
:
1538 // TODO setup size event
1539 s_lastMouseDown
= 0;
1546 SetPortWindowPort(window
) ;
1549 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1551 if ( s_macIsInModalLoop
)
1555 else if ( UMAIsWindowFloating( window
) )
1558 win
->MacMouseDown( ev
, windowPart
) ;
1562 // Activate window first
1563 ::SelectWindow( window
) ;
1567 win
->MacMouseDown( ev
, windowPart
) ;
1573 win
->MacMouseDown( ev
, windowPart
) ;
1581 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1583 EventRecord
* ev
= (EventRecord
*) evr
;
1586 short windowPart
= inNoWindow
;
1587 if ( wxTheApp
->s_captureWindow
)
1589 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1590 windowPart
= inContent
;
1594 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1605 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1607 win
->MacMouseUp( ev
, windowPart
) ;
1615 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1616 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1621 case kHomeCharCode
:
1624 case kEnterCharCode
:
1625 retval
= WXK_RETURN
;
1630 case kHelpCharCode
:
1633 case kBackspaceCharCode
:
1639 case kPageUpCharCode
:
1640 retval
= WXK_PAGEUP
;
1642 case kPageDownCharCode
:
1643 retval
= WXK_PAGEDOWN
;
1645 case kReturnCharCode
:
1646 retval
= WXK_RETURN
;
1648 case kFunctionKeyCharCode
:
1700 case kEscapeCharCode
:
1701 retval
= WXK_ESCAPE
;
1703 case kLeftArrowCharCode
:
1706 case kRightArrowCharCode
:
1707 retval
= WXK_RIGHT
;
1709 case kUpArrowCharCode
:
1712 case kDownArrowCharCode
:
1715 case kDeleteCharCode
:
1716 retval
= WXK_DELETE
;
1725 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1727 EventRecord
* ev
= (EventRecord
*) evr
;
1728 wxToolTip::RemoveToolTips() ;
1730 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1731 if ( HiWord( menuresult
) )
1733 if ( !s_macIsInModalLoop
)
1734 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1738 wxWindow
* focus
= wxWindow::FindFocus() ;
1740 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1743 // we must handle control keys the other way round, otherwise text content is updated too late
1744 // has not been handled -> perform default
1745 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1746 if ( control
&& control
->GetMacControl() != NULL
)
1750 keychar
= short(ev
->message
& charCodeMask
);
1751 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1752 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1759 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1761 EventRecord
* ev
= (EventRecord
*) evr
;
1762 wxToolTip::RemoveToolTips() ;
1764 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1765 if ( HiWord( menuresult
) )
1770 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1776 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1783 keychar
= short(keymessage
& charCodeMask
);
1784 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1786 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1788 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1789 // and look at the character after
1791 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1792 keychar
= short(keyInfo
& charCodeMask
);
1793 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1795 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1796 long realkeyval
= keyval
;
1797 if ( keyval
== keychar
)
1799 // 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)
1800 realkeyval
= short(keymessage
& charCodeMask
) ;
1801 keyval
= wxToupper( keyval
) ;
1804 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1805 bool handled
= false ;
1806 event
.m_shiftDown
= modifiers
& shiftKey
;
1807 event
.m_controlDown
= modifiers
& controlKey
;
1808 event
.m_altDown
= modifiers
& optionKey
;
1809 event
.m_metaDown
= modifiers
& cmdKey
;
1810 event
.m_keyCode
= keyval
;
1814 event
.m_timeStamp
= when
;
1815 event
.SetEventObject(focus
);
1816 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1817 if ( handled
&& event
.GetSkipped() )
1824 wxWindow
*ancestor
= focus
;
1827 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1830 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1831 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1834 if (ancestor
->IsTopLevel())
1836 ancestor
= ancestor
->GetParent();
1839 #endif // wxUSE_ACCEL
1843 event
.Skip( FALSE
) ;
1844 event
.SetEventType( wxEVT_CHAR
) ;
1846 event
.m_keyCode
= realkeyval
;
1848 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1849 if ( handled
&& event
.GetSkipped() )
1853 (keyval
== WXK_TAB
) &&
1854 // CS: copied the change below from wxGTK
1855 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1856 // have this style, yet choose not to process this particular TAB in which
1857 // case TAB must still work as a navigational character
1859 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1861 (focus
->GetParent()) &&
1862 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1864 wxNavigationKeyEvent new_event
;
1865 new_event
.SetEventObject( focus
);
1866 new_event
.SetDirection( !event
.ShiftDown() );
1867 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1868 new_event
.SetWindowChange( event
.ControlDown() );
1869 new_event
.SetCurrentFocus( focus
);
1870 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1871 if ( handled
&& new_event
.GetSkipped() )
1874 // backdoor handler for default return and command escape
1875 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1877 // if window is not having a focus still testing for default enter or cancel
1878 // TODO add the UMA version for ActiveNonFloatingWindow
1879 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1882 if ( keyval
== WXK_RETURN
)
1884 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1886 if ( def
&& def
->IsEnabled() )
1888 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1889 event
.SetEventObject(def
);
1890 def
->Command(event
);
1894 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1895 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1897 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1898 new_event
.SetEventObject( focus
);
1899 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1906 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1913 keychar
= short(keymessage
& charCodeMask
);
1914 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1915 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1917 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1918 // and look at the character after
1920 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1921 keychar
= short(keyInfo
& charCodeMask
);
1922 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1924 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1926 if ( keyval
== keychar
)
1928 keyval
= wxToupper( keyval
) ;
1930 bool handled
= false ;
1932 wxKeyEvent
event(wxEVT_KEY_UP
);
1933 event
.m_shiftDown
= modifiers
& shiftKey
;
1934 event
.m_controlDown
= modifiers
& controlKey
;
1935 event
.m_altDown
= modifiers
& optionKey
;
1936 event
.m_metaDown
= modifiers
& cmdKey
;
1937 event
.m_keyCode
= keyval
;
1941 event
.m_timeStamp
= when
;
1942 event
.SetEventObject(focus
);
1943 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1949 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1951 EventRecord
* ev
= (EventRecord
*) evr
;
1952 WindowRef window
= (WindowRef
) ev
->message
;
1955 bool activate
= (ev
->modifiers
& activeFlag
) ;
1956 WindowClass wclass
;
1957 ::GetWindowClass ( window
, &wclass
) ;
1958 if ( wclass
== kFloatingWindowClass
)
1960 // if it is a floater we activate/deactivate the front non-floating window instead
1961 window
= ::FrontNonFloatingWindow() ;
1963 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1965 win
->MacActivate( ev
->when
, activate
) ;
1969 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1971 EventRecord
* ev
= (EventRecord
*) evr
;
1972 WindowRef window
= (WindowRef
) ev
->message
;
1973 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1976 if ( !wxPendingDelete
.Member(win
) )
1977 win
->MacUpdate( ev
->when
) ;
1981 // since there is no way of telling this foreign window to update itself
1982 // we have to invalidate the update region otherwise we keep getting the same
1983 // event over and over again
1984 BeginUpdate( window
) ;
1985 EndUpdate( window
) ;
1989 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1991 EventRecord
* ev
= (EventRecord
*) evr
;
1992 if ( HiWord( ev
->message
) != noErr
)
1996 SetPt( &point
, 100 , 100 ) ;
1998 err
= DIBadMount( point
, ev
->message
) ;
1999 wxASSERT( err
== noErr
) ;
2003 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2005 EventRecord
* ev
= (EventRecord
*) evr
;
2006 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2008 case suspendResumeMessage
:
2010 bool isResuming
= ev
->message
& resumeFlag
;
2011 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2013 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2016 WindowRef oldFrontWindow
= NULL
;
2017 WindowRef newFrontWindow
= NULL
;
2019 // in case we don't take care of activating ourselves, we have to synchronize
2020 // our idea of the active window with the process manager's - which it already activated
2022 if ( !doesActivate
)
2023 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2025 MacResume( convertClipboard
) ;
2027 newFrontWindow
= ::FrontNonFloatingWindow() ;
2029 if ( oldFrontWindow
)
2031 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2033 win
->MacActivate( ev
->when
, false ) ;
2035 if ( newFrontWindow
)
2037 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2039 win
->MacActivate( ev
->when
, true ) ;
2044 MacSuspend( convertClipboard
) ;
2048 case mouseMovedMessage
:
2052 wxWindow
* currentMouseWindow
= NULL
;
2054 if (s_captureWindow
)
2056 currentMouseWindow
= s_captureWindow
;
2060 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2061 ¤tMouseWindow
) ;
2064 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2066 wxMouseEvent event
;
2068 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2069 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2071 event
.m_leftDown
= isDown
&& !controlDown
;
2072 event
.m_middleDown
= FALSE
;
2073 event
.m_rightDown
= isDown
&& controlDown
;
2074 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2075 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2076 event
.m_altDown
= ev
->modifiers
& optionKey
;
2077 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2078 event
.m_x
= ev
->where
.h
;
2079 event
.m_y
= ev
->where
.v
;
2080 event
.m_timeStamp
= ev
->when
;
2081 event
.SetEventObject(this);
2083 if ( wxWindow::s_lastMouseWindow
)
2085 wxMouseEvent
eventleave(event
);
2086 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2087 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2088 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2090 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2092 if ( currentMouseWindow
)
2094 wxMouseEvent
evententer(event
);
2095 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2096 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2097 evententer
.SetEventObject( currentMouseWindow
) ;
2098 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2100 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2103 short windowPart
= inNoWindow
;
2105 if ( s_captureWindow
)
2107 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2108 windowPart
= inContent
;
2112 windowPart
= ::FindWindow(ev
->where
, &window
);
2119 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2121 win
->MacMouseMoved( ev
, windowPart
) ;
2128 UMAShowArrowCursor();
2138 UMAShowArrowCursor();
2149 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2153 wxWindow
* currentMouseWindow
= NULL
;
2155 if (s_captureWindow
)
2157 currentMouseWindow
= s_captureWindow
;
2161 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2164 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2166 wxMouseEvent event
;
2168 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2169 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2171 event
.m_leftDown
= isDown
&& !controlDown
;
2173 event
.m_middleDown
= FALSE
;
2174 event
.m_rightDown
= isDown
&& controlDown
;
2176 event
.m_shiftDown
= modifiers
& shiftKey
;
2177 event
.m_controlDown
= modifiers
& controlKey
;
2178 event
.m_altDown
= modifiers
& optionKey
;
2179 event
.m_metaDown
= modifiers
& cmdKey
;
2183 event
.m_timeStamp
= timestamp
;
2185 if ( wxWindow::s_lastMouseWindow
)
2187 wxMouseEvent
eventleave(event
);
2188 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2189 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2190 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2193 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2194 #endif // wxUSE_TOOLTIPS
2195 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2197 if ( currentMouseWindow
)
2199 wxMouseEvent
evententer(event
);
2200 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2201 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2202 evententer
.SetEventObject( currentMouseWindow
) ;
2204 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2205 #endif // wxUSE_TOOLTIPS
2206 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2208 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2211 short windowPart
= inNoWindow
;
2213 if ( s_captureWindow
)
2215 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2216 windowPart
= inContent
;
2220 Point pt
= { y
, x
} ;
2221 windowPart
= ::FindWindow(pt
, &window
);
2228 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2230 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2237 UMAShowArrowCursor();
2247 UMAShowArrowCursor();
2254 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2256 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2257 wxFrame
* frame
= mbar
->GetFrame();
2258 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2261 frame
->ProcessCommand(id
);
2266 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2269 return; // no menu item selected
2271 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2274 Str255 deskAccessoryName
;
2277 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2278 GetPort(&savedPort
);
2279 OpenDeskAcc(deskAccessoryName
);
2286 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2287 MacHandleMenuCommand( id
) ;